Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PIR] Refine op yaml system #59364

Merged
merged 14 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions paddle/fluid/framework/type_info.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ limitations under the License. */
#include "paddle/fluid/framework/feed_fetch_type.h"
#include "paddle/fluid/framework/raw_tensor.h"
#include "paddle/fluid/framework/string_array.h"
#include "paddle/fluid/pir/dialect/operator/ir/ir_selected_rows.h"
#include "paddle/fluid/pir/dialect/operator/ir/ir_tensor.h"
#include "paddle/fluid/prim/utils/static/desc_tensor.h"
#include "paddle/fluid/primitive/type/lazy_tensor.h"
Expand Down Expand Up @@ -46,5 +47,6 @@ template class TypeInfoTraits<phi::TensorBase, paddle::primitive::LazyTensor>;
template class TypeInfoTraits<phi::TensorBase,
paddle::framework::VariableRefArray>;
template class TypeInfoTraits<phi::TensorBase, paddle::dialect::IrTensor>;
template class TypeInfoTraits<phi::TensorBase, paddle::dialect::IrSelectedRows>;

} // namespace phi
4 changes: 2 additions & 2 deletions paddle/fluid/ir_adaptor/translator/op_translator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -978,9 +978,9 @@ struct EmbeddingGradOpTranscriber : public OpTranscriber {
bool is_sparse = paddle::get<bool>(op_desc.GetAttr("is_sparse"));

if (is_sparse) {
target_op_name = "pd_op.embedding_grad_sparse";
target_op_name = "pd_op.embedding_sparse_grad";
} else {
target_op_name = "pd_op.embedding_grad_dense";
target_op_name = "pd_op.embedding_grad";
}
VLOG(6) << "[op name normalizing: " << op_desc.Type() << " to "
<< target_op_name;
Expand Down
10 changes: 2 additions & 8 deletions paddle/fluid/pir/dialect/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,9 @@ set(op_compat_yaml_file ${PADDLE_SOURCE_DIR}/paddle/phi/api/yaml/op_compat.yaml)
set(op_forward_yaml_file1
${PADDLE_SOURCE_DIR}/paddle/fluid/operators/generator/parsed_ops/ops.parsed.yaml
)
set(op_forward_yaml_file2
${PADDLE_SOURCE_DIR}/paddle/fluid/operators/generator/parsed_ops/legacy_ops.parsed.yaml
)
set(op_backward_yaml_file1
${PADDLE_SOURCE_DIR}/paddle/fluid/operators/generator/parsed_ops/backward_ops.parsed.yaml
)
set(op_backward_yaml_file2
${PADDLE_SOURCE_DIR}/paddle/fluid/operators/generator/parsed_ops/legacy_backward_ops.parsed.yaml
)
set(fused_op_forward_yaml_file
${PADDLE_SOURCE_DIR}/paddle/fluid/operators/generator/parsed_ops/fused_ops.parsed.yaml
)
Expand All @@ -44,7 +38,7 @@ set(op_yaml_file4 ${parsed_op_dir}/ops_backward.parsed.yaml)
set(op_yaml_file5 ${parsed_op_dir}/update_ops.parsed.yaml)

set(op_yaml_files
${op_forward_yaml_file1},${op_forward_yaml_file2},${op_backward_yaml_file1},${op_backward_yaml_file2},${fused_op_forward_yaml_file},${fused_op_backward_yaml_file},${op_yaml_file3},${op_yaml_file4},${op_yaml_file5}
${op_forward_yaml_file1},${op_backward_yaml_file1},${fused_op_forward_yaml_file},${fused_op_backward_yaml_file},${op_yaml_file3},${op_yaml_file4},${op_yaml_file5}
)
set(op_namespace paddle,dialect)
set(dialect_name pd_op)
Expand Down Expand Up @@ -78,7 +72,7 @@ set(generated_files_pd_op "${op_header_file}" "${op_source_file}"
"${op_vjp_source_file}")

set(api_gen_yaml_files
${op_forward_yaml_file1},${op_forward_yaml_file2},${op_backward_yaml_file1},${op_backward_yaml_file2},${op_yaml_file3},${op_yaml_file4},${op_yaml_file5}
${op_forward_yaml_file1},${op_backward_yaml_file1},${op_yaml_file3},${op_yaml_file4},${op_yaml_file5}
)
set(api_gen_file
${PADDLE_SOURCE_DIR}/paddle/fluid/pir/dialect/op_generator/api_gen.py)
Expand Down
6 changes: 5 additions & 1 deletion paddle/fluid/pir/dialect/op_generator/api_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
update_ops,
)

PD_MANUAL_API_LIST = {
'embedding_grad',
}

H_FILE_TEMPLATE = """

#pragma once
Expand Down Expand Up @@ -204,7 +208,7 @@ def _parse_yaml(self, op_yaml_files, op_compat_yaml_file):
def _need_skip(self, op_info, op_name):
return (
op_info.infer_meta_func is None and op_name not in PD_MANUAL_OP_LIST
)
) or op_name in PD_MANUAL_API_LIST

def _is_optional_input(self, op_info, input_name):
name_list = op_info.input_name_list
Expand Down
35 changes: 24 additions & 11 deletions paddle/fluid/pir/dialect/op_generator/op_build_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ def GenBuildOutputs(
paddle::dialect::IrMetaTensor meta_{name};
paddle::dialect::IrTensor ir_tensor_{name};
if ({name}_.impl() != nullptr) {{
paddle::dialect::DenseTensorType {name} = {name}_.type().dyn_cast<paddle::dialect::DenseTensorType>();
{type} {name} = {name}_.type().dyn_cast<{type}>();
VLOG(4) << "Builder construction dense_{name}";
ir_tensor_{name} = paddle::dialect::IrTensor(paddle::dialect::TransToPhiDataType({name}.dtype()),
{name}.dims(),
Expand Down Expand Up @@ -432,6 +432,9 @@ def GenBuildOutputs(

CREATE_OUTPUT_METATENSOR_TEMPLATE = """ paddle::dialect::IrTensor dense_{name};
paddle::dialect::IrMetaTensor meta_{name}(&dense_{name});
"""
CREATE_OUTPUT_METASELETEROWS_TEMPLATE = """ paddle::dialect::IrSelectedRows dense_{name};
paddle::dialect::IrMetaTensor meta_{name}(&dense_{name});
"""
CREATE_OUTPUT_VEC_METATENSOR_TEMPLATE = """ std::vector<paddle::dialect::IrTensor> vec_dense_{name}(({output_size}), paddle::dialect::IrTensor());
std::vector<paddle::dialect::IrMetaTensor> vec_meta_{name};
Expand All @@ -454,8 +457,8 @@ def GenBuildOutputs(
# is a Tensor
else:
if op_input_optional_list[idx] == 'false':
build_output_str += " paddle::dialect::DenseTensorType {name} = {name}_.type().dyn_cast<paddle::dialect::DenseTensorType>(); (void){name};\n".format(
name=op_input_name_list[idx]
build_output_str += " {type} {name} = {name}_.type().dyn_cast<{type}>(); (void){name};\n".format(
type=op_input_type_list[idx], name=op_input_name_list[idx]
)

# Prepare mutable attributes
Expand Down Expand Up @@ -526,7 +529,8 @@ def GenBuildOutputs(
if op_input_optional_list[input_index] == 'true':
build_output_str += (
CREATE_OPTIONAL_INPUT_METATENSOR_TEMPLATE.format(
name=op_infer_meta_map['param'][idx]
name=op_infer_meta_map['param'][idx],
type=op_input_type_list[idx],
)
)
else:
Expand All @@ -552,10 +556,18 @@ def GenBuildOutputs(
infer_meta_args.append(f"meta_{op_output_name_list[idx]}")
# is a Tensor
else:
build_output_str += CREATE_OUTPUT_METATENSOR_TEMPLATE.format(
name=op_output_name_list[idx]
)
infer_meta_args.append(f"&meta_{op_output_name_list[idx]}")
if op_output_type_list[idx] == "paddle::dialect::DenseTensorType":
build_output_str += CREATE_OUTPUT_METATENSOR_TEMPLATE.format(
name=op_output_name_list[idx]
)
infer_meta_args.append(f"&meta_{op_output_name_list[idx]}")
else:
build_output_str += (
CREATE_OUTPUT_METASELETEROWS_TEMPLATE.format(
name=op_output_name_list[idx]
)
)
infer_meta_args.append(f"&meta_{op_output_name_list[idx]}")

# Execute infer meta function
CREATE_INFER_META_FUNC_TEMPLATE = """
Expand All @@ -579,13 +591,13 @@ def GenBuildOutputs(
build_output_str += "\n std::vector<pir::Type> argument_outputs;"

CREATE_OUTPUT_DENSE_TENSOR_TEMPLATE = """
pir::Type {name}_dense_tensor_type = paddle::dialect::DenseTensorType::get(pir::IrContext::Instance(), paddle::dialect::TransToIrDataType(dense_{name}.dtype()), dense_{name}.dims(), dense_{name}.layout(), dense_{name}.lod(), dense_{name}.offset());
pir::Type {name}_dense_tensor_type = {type}::get(pir::IrContext::Instance(), paddle::dialect::TransToIrDataType(dense_{name}.dtype()), dense_{name}.dims(), dense_{name}.layout(), dense_{name}.lod(), dense_{name}.offset());
argument_outputs.push_back({name}_dense_tensor_type);
"""

CREATE_OUTPUT_INPLACE_OPTIONAL_DENSE_TENSOR_TEMPLATE = """
if ({input_name}_.impl() != nullptr) {{
pir::Type {output_name}_dense_tensor_type = paddle::dialect::DenseTensorType::get(pir::IrContext::Instance(), paddle::dialect::TransToIrDataType(dense_{output_name}.dtype()), dense_{output_name}.dims(), dense_{output_name}.layout(), dense_{output_name}.lod(), dense_{output_name}.offset());
pir::Type {output_name}_dense_tensor_type = {type}::get(pir::IrContext::Instance(), paddle::dialect::TransToIrDataType(dense_{output_name}.dtype()), dense_{output_name}.dims(), dense_{output_name}.layout(), dense_{output_name}.lod(), dense_{output_name}.offset());
argument_outputs.push_back({output_name}_dense_tensor_type);
}} else {{
pir::Type {output_name}_type;
Expand Down Expand Up @@ -622,11 +634,12 @@ def GenBuildOutputs(
CREATE_OUTPUT_INPLACE_OPTIONAL_DENSE_TENSOR_TEMPLATE.format(
input_name=op_inplace_map[output_name],
output_name=output_name,
type=op_output_type_list[idx],
)
)
else:
build_output_str += CREATE_OUTPUT_DENSE_TENSOR_TEMPLATE.format(
name=output_name
type=op_output_type_list[idx], name=output_name
)

build_output_str += " argument.AddOutputs(argument_outputs.begin(), argument_outputs.end());\n"
Expand Down
1 change: 1 addition & 0 deletions paddle/fluid/pir/dialect/op_generator/op_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ class {op_name} : public pir::Op<{op_name}{interfaces}{traits}> {{
#include "paddle/fluid/pir/dialect/operator/ir/op_type.h"
#include "paddle/fluid/pir/dialect/operator/ir/op_attribute.h"
#include "paddle/fluid/pir/dialect/operator/ir/ir_tensor.h"
#include "paddle/fluid/pir/dialect/operator/ir/ir_selected_rows.h"
#include "paddle/fluid/pir/dialect/operator/ir/ir_meta_tensor.h"
#include "paddle/pir/core/builtin_attribute.h"
#include "paddle/pir/core/builtin_type.h"
Expand Down
5 changes: 4 additions & 1 deletion paddle/fluid/pir/dialect/operator/ir/ir_meta_tensor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// limitations under the License.

#include "paddle/fluid/pir/dialect/operator/ir/ir_meta_tensor.h"
#include "paddle/fluid/pir/dialect/operator/ir/ir_selected_rows.h"
#include "paddle/fluid/pir/dialect/operator/ir/ir_tensor.h"

namespace paddle {
Expand Down Expand Up @@ -82,7 +83,9 @@ void IrMetaTensor::share_meta(const MetaTensor& meta_tensor) {

bool IrMetaTensor::initialized() const { return tensor_ != nullptr; }

bool IrMetaTensor::is_selected_rows() const { return false; }
bool IrMetaTensor::is_selected_rows() const {
return IrSelectedRows::classof(tensor_);
}

bool IrMetaTensor::is_tensor_array() const { return false; }

Expand Down
68 changes: 68 additions & 0 deletions paddle/fluid/pir/dialect/operator/ir/ir_selected_rows.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "paddle/fluid/pir/dialect/operator/ir/ir_selected_rows.h"

#include "paddle/pir/core/enforce.h"

namespace paddle {
namespace dialect {
IrSelectedRows::IrSelectedRows(phi::DataType dtype,
const phi::DDim& dims,
phi::DataLayout layout,
const LoD& lod,
size_t offset)
: dims_(dims), dtype_(dtype), layout_(layout), lod_(lod), offset_(offset) {}

IrSelectedRows::IrSelectedRows(const IrSelectedRows& other) {
dims_ = other.dims();
dtype_ = other.dtype();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

下面这几个构造函数是不是都可以用=default 就可以了。因为成员里没有指针对象,默认的应该够用?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

感谢建议,单独 pr 完善一下~

layout_ = other.layout();
lod_ = other.lod();
offset_ = other.offset();
}

IrSelectedRows& IrSelectedRows::operator=(const IrSelectedRows& other) {
dims_ = other.dims();
dtype_ = other.dtype();
layout_ = other.layout();
lod_ = other.lod();
offset_ = other.offset();
return *this;
}

IrSelectedRows& IrSelectedRows::operator=(IrSelectedRows&& other) noexcept {
dims_ = std::move(other.dims());
dtype_ = other.dtype();
layout_ = other.layout();
lod_ = std::move(other.lod());
offset_ = other.offset();
return *this;
}

int64_t IrSelectedRows::numel() const { return phi::product(dims_); }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里会出现 [128, 3, -1, -1]的场景么?或者说这里的ddim都是非负数了已经

Copy link
Contributor Author

@zhangbo9674 zhangbo9674 Nov 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

可能会出现,IrTensor 和 IrSelectRows 仅会PIR组网的 infer meta 阶段使用,全局搜索了一下框架目前所有的 infer_meta 函数,共有2个使用到 numel 的地方:NanmedianInferMeta、FullWithTensorInferMeta,但这两个函数并未对这种情况做处理。目前执行未发现错误应该是因为执行期间还会执行一次 infer_meta.
后续我单独 pr 对IrTensor 和 IrSelectRows 的 numel 接口进行完善,在存在-1的情况下返回-1,此外再对NanmedianInferMeta、FullWithTensorInferMeta 这两个 infer_meta 的实现进行完善


const phi::Place& IrSelectedRows::place() const {
IR_THROW("Don't use IrSelectedRows::place method.");
}

void* IrSelectedRows::AllocateFrom(phi::Allocator* allocator,
phi::DataType dtype,
size_t requested_size,
bool fake_alloc) {
IR_THROW("Don't use IrSelectedRows::AllocateFrom method.");
}

} // namespace dialect
} // namespace paddle
91 changes: 91 additions & 0 deletions paddle/fluid/pir/dialect/operator/ir/ir_selected_rows.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#pragma once

#include "paddle/phi/core/allocator.h"
#include "paddle/phi/core/tensor_base.h"
#include "paddle/phi/core/tensor_meta.h"

namespace paddle {
namespace dialect {

using LoD = std::vector<std::vector<size_t>>;

class IrSelectedRows
: public phi::TensorBase,
public phi::TypeInfoTraits<phi::TensorBase, IrSelectedRows> {
public:
IrSelectedRows() = default;

IrSelectedRows(phi::DataType dtype,
const phi::DDim& dims,
phi::DataLayout layout,
const LoD& lod,
size_t offset = 0);

IrSelectedRows(IrSelectedRows&& other) = default;

IrSelectedRows(const IrSelectedRows& other);

IrSelectedRows& operator=(const IrSelectedRows& other);

IrSelectedRows& operator=(IrSelectedRows&& other) noexcept;

virtual ~IrSelectedRows() = default;

public:
static const char* name() { return "IrSelectedRows"; }

int64_t numel() const override;

const phi::DDim& dims() const noexcept override { return dims_; }

void SetDims(const phi::DDim& dims) { dims_ = dims; }

const phi::Place& place() const override;

phi::DataType dtype() const noexcept override { return dtype_; }

void SetDtype(phi::DataType dtype) { dtype_ = dtype; }

phi::DataLayout layout() const noexcept override { return layout_; }

void SetLayout(phi::DataLayout layout) { layout_ = layout; }

const LoD& lod() const noexcept { return lod_; }

void SetLod(LoD lod) { lod_ = lod; }

size_t offset() const noexcept { return offset_; }

bool valid() const noexcept override { return true; }

bool initialized() const override { return true; }

void* AllocateFrom(phi::Allocator* allocator,
phi::DataType dtype,
size_t requested_size = 0,
bool fake_alloc = false) override;

private:
phi::DDim dims_;
phi::DataType dtype_{phi::DataType::FLOAT32};
phi::DataLayout layout_{phi::DataLayout::ANY};
LoD lod_;
size_t offset_{0};
};

} // namespace dialect
} // namespace paddle
15 changes: 11 additions & 4 deletions paddle/fluid/pir/dialect/operator/ir/manual_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,18 @@ pir::OpResult embedding_grad(const pir::Value& x,
bool sparse) {
if (weight.type().isa<paddle::dialect::DenseTensorType>()) {
if (sparse) {
return paddle::dialect::embedding_grad_sparse(
x, weight, out_grad, padding_idx, sparse);
auto embedding_grad_op =
ApiBuilder::Instance()
.GetBuilder()
->Build<paddle::dialect::EmbeddingSparseGradOp>(
x, weight, out_grad, padding_idx);
return embedding_grad_op.weight_grad();
} else {
return paddle::dialect::embedding_grad_dense(
x, weight, out_grad, padding_idx, sparse);
auto embedding_grad_op = ApiBuilder::Instance()
.GetBuilder()
->Build<paddle::dialect::EmbeddingGradOp>(
x, weight, out_grad, padding_idx);
return embedding_grad_op.weight_grad();
}
} else {
PADDLE_THROW(phi::errors::Unimplemented(
Expand Down
1 change: 1 addition & 0 deletions paddle/fluid/pir/dialect/operator/ir/manual_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#include "paddle/fluid/pir/dialect/operator/ir/manual_op.h"
#include "paddle/fluid/pir/dialect/operator/ir/ir_meta_tensor.h"
#include "paddle/fluid/pir/dialect/operator/ir/ir_selected_rows.h"
#include "paddle/fluid/pir/dialect/operator/ir/ir_tensor.h"
#include "paddle/fluid/pir/dialect/operator/ir/op_attribute.h"
#include "paddle/fluid/pir/dialect/operator/ir/op_type.h"
Expand Down
Loading