Skip to content
New issue

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

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

Already on GitHub? # to your account

[static op generation] coalesce_tensor #53570

Merged
merged 3 commits into from
May 9, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
111 changes: 0 additions & 111 deletions paddle/fluid/operators/coalesce_tensor_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -400,120 +400,9 @@ class CoalesceTensorOp : public framework::OperatorWithKernel {
}
};

class CoalesceTensorOpMaker : public framework::OpProtoAndCheckerMaker {
Copy link
Contributor

Choose a reason for hiding this comment

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

  1. 把这个文件删除。
  2. paddle/phi/kernels/coalesce_tensor_kernel.cc约276行修改为:
PD_REGISTER_KERNEL(coalesce_tensor,
                   CPU,
                   ALL_LAYOUT,
                   phi::CoalesceTensorKernel,
                   int,
                   float,
                   double) {
  kernel->InputAt(0).SetBackend(phi::Backend::ALL_BACKEND);
  kernel->OutputAt(1).SetDataType(phi::DataType::UNDEFINED);
}

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

public:
void Make() override {
AddInput("Input",
"(vector<phi::DenseTensor>) The input tensors of"
" coalesce_tensor operator.")
.AsDuplicable();
AddOutput("Output",
"(vector<phi::DenseTensor>) The output "
"tensors of coalesce_tensor operator. And the address "
"of output tensors are continuous, they are sliced from the "
"tensor of FusedOutput.")
.AsDuplicable();
AddOutput("FusedOutput",
"(phi::DenseTensor) The output tensor "
"of coalesce_tensor operator. And the tensors of"
" Output is sliced from the tensor of FusedOutput.");
AddAttr<int>("dtype", "The output data type.");
AddAttr<bool>("copy_data", "Whether to copy the Input value to Output.")
.SetDefault(false);
AddAttr<bool>("set_constant",
"Whether to set the Output with a constant value.")
.SetDefault(false);
AddAttr<bool>("persist_output",
"Whether to persist the original Output value.")
.SetDefault(false);
AddAttr<float>("constant",
"If set_constant is true, the constant value will be used "
"to set the Output.")
.SetDefault(0.0);
AddAttr<bool>("check_name",
"Whether to check the name of Input and Output to ensure "
"they are the same separately.")
.SetDefault(false);
AddAttr<bool>("use_align",
"Whether to consider memory chunk and take alignment into "
"account for inputs and outputs.")
.SetDefault(true);
AddAttr<int>("align_size", "The alignment size when use_align is True")
.SetDefault(-1);
AddAttr<int>("user_defined_size_of_dtype",
"The user defined size of dtype. This is used to coalesce "
"grad vars and merged_grad vars at the same time. For some "
"strategy, the dtype of fused_grad_vars and the dtype of "
"fused_grad_merged_vars are not identical, which will cause "
"the shape of these two coalesced vars are different. To "
"make sure the shape of these two vars are identical with "
"each other, this attr is added.")
.SetDefault(-1);
AddAttr<std::vector<int64_t>>(
"concated_shapes",
"The concated shapes of each shape of the input tensors. "
"If any of the input tensors are not inited, this is used to "
"init the output tensor shape, together with "
"attribute(concated_ranks).")
.SetDefault({});
AddAttr<std::vector<int64_t>>(
"concated_ranks",
"The concated ranks of each rank of the input tensors. "
"If any of the input tensors are not inited, this is used to "
"init the output tensor shape, together with "
"attribute(concated_shapes).")
.SetDefault({});
AddComment(R"DOC(
CoalesceTensor Operator.

coalesce_tensor is used to make the address of Output
continuous according to the Input. This Op will alloc a big tensor
according to the tensors of Input, the dtype is the same with those input tensors,
the size is the sum of those input tensors' numel, and the dim of the big
tensor is {sum(numel)}. And the big tensor is stored in FusedOutput.
The tensors of Output are sliced from the tensor of FusedOutput.
Note that, the dtype of Input should be the same, and the dim of Input
and Output should equal.
The tensors of Input and Output could be the same or different. And
coalesce_tensor allows copying the value of Input to Output, or
setting the Output with a constant value, or persist the original Output
value.

)DOC");
}
};

} // namespace operators
} // namespace paddle

DECLARE_INFER_SHAPE_FUNCTOR(coalesce_tensor,
CoalesceTensorInferShapeFunctor,
PD_INFER_META(phi::CoalesceTensorInferMeta));

REGISTER_OPERATOR(coalesce_tensor,
paddle::operators::CoalesceTensorOp,
paddle::operators::CoalesceTensorOpMaker,
CoalesceTensorInferShapeFunctor);
namespace ops = paddle::operators;
namespace plat = paddle::platform;

REGISTER_OP_VERSION(coalesce_tensor)
.AddCheckpoint(
R"ROC(
Upgrade coalesce_tensor: add a new attribute [use_align].)ROC",
paddle::framework::compatible::OpVersionDesc().NewAttr(
"use_align",
"In order to optionally take memory alignment into account when "
"coalescing tensors. The default value is true to be compatible "
"with before.",
true))
.AddCheckpoint(
R"ROC(
Upgrade coalesce_tensor: add a new attribute [align_size].)ROC",
paddle::framework::compatible::OpVersionDesc().NewAttr(
"align_size",
"In order to optionally take memory alignment into account when "
"coalescing tensors. The default value is -1 and use the default "
"align_size "
"of each place to be compatible with before.",
-1));
9 changes: 0 additions & 9 deletions paddle/phi/api/yaml/legacy_ops.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -171,15 +171,6 @@
data_type : x
inplace : (x -> out), (input_found_infinite -> output_found_infinite)

- op : coalesce_tensor
args : (Tensor[] input, DataType dtype, bool copy_data = false, bool set_constant = false, bool persist_output = false, float constant = 0.0, bool use_align = true, int align_size = -1, int size_of_dtype = -1, int64_t[] concated_shapes = {}, int64_t[] concated_ranks = {})
output : Tensor[](output){input.size()}, Tensor(fused_output)
infer_meta :
func : CoalesceTensorInferMeta
kernel :
func : coalesce_tensor
data_type : dtype

- op : concat
args : (Tensor[] x, Scalar(int64_t) axis)
output : Tensor
Expand Down
6 changes: 6 additions & 0 deletions paddle/phi/api/yaml/op_compat.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,12 @@
outputs :
out : Out

- op : coalesce_tensor
inputs :
{input : Input}
outputs :
{output : Output, fused_output : FusedOutput}
Copy link
Contributor

Choose a reason for hiding this comment

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

attrs : 
  {size_of_dtype : user_defined_size_of_dtype}

Copy link
Member Author

Choose a reason for hiding this comment

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

Done


- op : complex
backward : complex_grad
inputs :
Expand Down
18 changes: 18 additions & 0 deletions paddle/phi/api/yaml/op_version.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,24 @@
- add_input : Max
comment : Pass the mix, min value as input, not attribute. Max is dispensable.

- op : coalesce_tensor
version :
- checkpoint : "Upgrade coalesce_tensor: add a new attribute [use_align]."
action :
- add_attr : use_align
comment : In order to optionally take memory alignment into account when
coalescing tensors. The default value is true to be compatible
with before.
default : "true"
- checkpoint : "Upgrade coalesce_tensor: add a new attribute [align_size]."
action :
- add_attr : align_size
comment : In order to optionally take memory alignment into account when
coalescing tensors. The default value is -1 and use the default
align_size
of each place to be compatible with before.
default : -1

- op : embedding
version :
- checkpoint : Upgrade flip, add new attr [axis] and delete attr [dims]
Expand Down
9 changes: 9 additions & 0 deletions paddle/phi/api/yaml/ops.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,15 @@
func : clip_by_norm {dense -> dense}
clip_by_norm_sr {selected_rows -> selected_rows}

- op : coalesce_tensor
args : (Tensor[] input, DataType dtype, bool copy_data = false, bool set_constant = false, bool persist_output = false, float constant = 0.0, bool use_align = true, int align_size = -1, int size_of_dtype = -1, int64_t[] concated_shapes = {}, int64_t[] concated_ranks = {})
output : Tensor[](output){input.size()}, Tensor(fused_output)
infer_meta :
func : CoalesceTensorInferMeta
kernel :
func : coalesce_tensor
data_type : dtype

- op : complex
args : (Tensor real, Tensor imag)
output : Tensor
Expand Down
38 changes: 0 additions & 38 deletions paddle/phi/ops/compat/coalesce_tensor_sig.cc

This file was deleted.