未验证 提交 a7070f34 编写于 作者: W Wang Xin 提交者: GitHub

add autogen code support for expand_as op (#52797)

* add autogen code support for expand_as op

* bug fixed
上级 9bdf7f02
/* Copyright (c) 2019 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/operators/expand_as_v2_op.h"
#include <memory>
#include <vector>
#include "paddle/fluid/framework/infershape_utils.h"
#include "paddle/fluid/framework/op_version_registry.h"
#include "paddle/phi/infermeta/binary.h"
namespace paddle {
namespace operators {
class ExpandAsV2Op : public framework::OperatorWithKernel {
public:
using framework::OperatorWithKernel::OperatorWithKernel;
phi::KernelKey GetExpectedKernelType(
const framework::ExecutionContext& ctx) const {
return phi::KernelKey(OperatorWithKernel::IndicateVarDataType(ctx, "X"),
ctx.device_context().GetPlace());
}
};
class ExpandAsV2OpMaker : public framework::OpProtoAndCheckerMaker {
public:
void Make() override {
AddInput("X",
"(Tensor, default Tensor<float>). A tensor with rank in [1, 6]."
"X is the input to be expanded.");
AddInput("Y",
"(Tensor, default Tensor<float>). A tensor with rank in [1, 6]."
"Expand X according to the shape of Y.")
.AsDispensable();
AddOutput("Out",
"(Tensor, default Tensor<float>). A tensor with rank in [1, 6]."
"The rank of Output(Out) have the same with Input(X). "
"After expanding, size of each dimension of Output(Out) is equal "
"to size of the corresponding dimension of Input(X) multiplying "
"the corresponding value given by Attr(expand_times).");
AddAttr<std::vector<int>>("target_shape",
"Expand shape for each dimension.")
.SetDefault({});
AddComment(R"DOC(
Expand the input to the given shape.
)DOC");
}
};
class ExpandAsV2GradOp : public framework::OperatorWithKernel {
public:
using framework::OperatorWithKernel::OperatorWithKernel;
protected:
void InferShape(framework::InferShapeContext* ctx) const override {
OP_INOUT_CHECK(ctx->HasInput("X"), "Input", "X", "ExpandAsV2Grad");
OP_INOUT_CHECK(ctx->HasInput(framework::GradVarName("Out")),
"Input",
framework::GradVarName("Out"),
"ExpandAsV2Grad");
auto x_dims = ctx->GetInputDim("X");
auto x_grad_name = framework::GradVarName("X");
if (ctx->HasOutput(x_grad_name)) {
ctx->SetOutputDim(x_grad_name, x_dims);
}
}
phi::KernelKey GetExpectedKernelType(
const framework::ExecutionContext& ctx) const override {
return phi::KernelKey(OperatorWithKernel::IndicateVarDataType(
ctx, framework::GradVarName("Out")),
ctx.device_context().GetPlace());
}
};
template <typename T>
class ExpandAsV2GradOpMaker : public framework::SingleGradOpMaker<T> {
public:
using framework::SingleGradOpMaker<T>::SingleGradOpMaker;
protected:
void Apply(GradOpPtr<T> op) const override {
op->SetType("expand_as_v2_grad");
op->SetInput("X", this->Input("X"));
op->SetInput(framework::GradVarName("Out"), this->OutputGrad("Out"));
op->SetOutput(framework::GradVarName("X"), this->InputGrad("X"));
op->SetAttrMap(this->Attrs());
}
};
DECLARE_NO_NEED_BUFFER_VARS_INFERER(ExpandAsV2GradNoNeedBufVarsInferer, "X");
} // namespace operators
} // namespace paddle
namespace ops = paddle::operators;
DECLARE_INFER_SHAPE_FUNCTOR(expand_as_v2,
ExpandAsInferShapeFunctor,
PD_INFER_META(phi::ExpandAsInferMeta));
REGISTER_OPERATOR(expand_as_v2,
ops::ExpandAsV2Op,
ops::ExpandAsV2OpMaker,
ops::ExpandAsV2GradOpMaker<paddle::framework::OpDesc>,
ops::ExpandAsV2GradOpMaker<paddle::imperative::OpBase>,
ExpandAsInferShapeFunctor);
REGISTER_OPERATOR(expand_as_v2_grad,
ops::ExpandAsV2GradOp,
ops::ExpandAsV2GradNoNeedBufVarsInferer);
REGISTER_OP_VERSION(expand_as_v2)
.AddCheckpoint(R"ROC(fix expand_as_v2 and add new input [Y])ROC",
paddle::framework::compatible::OpVersionDesc().NewInput(
"Y", "Expand X according to the shape of Y"));
......@@ -543,6 +543,17 @@
inplace : (out_grad -> x_grad)
composite : exp_grad(out, out_grad, x_grad)
- backward_op : expand_as_grad
forward : expand_as (Tensor x, Tensor y, int[] target_shape = {}) -> Tensor(out)
args : (Tensor x, Tensor out_grad, int[] target_shape)
output : Tensor(x_grad)
infer_meta :
func : UnchangedInferMeta
param : [x]
kernel :
func : expand_as_grad
no_need_buffer : x
- backward_op : expm1_grad
forward : expm1 (Tensor x) -> Tensor(out)
args : (Tensor out, Tensor out_grad)
......
......@@ -348,17 +348,6 @@
invoke : embedding_grad_impl(x, weight, out_grad, padding_idx, sparse, weight_grad)
no_need_buffer : weight
- backward_op : expand_as_grad
forward : expand_as (Tensor x, Tensor y, int[] target_shape) -> Tensor(out)
args : (Tensor x, Tensor out_grad, int[] target_shape)
output : Tensor(x_grad)
infer_meta :
func : UnchangedInferMeta
param : [x]
kernel :
func : expand_as_grad
no_need_buffer : x
- backward_op : expand_double_grad
forward : expand_grad (Tensor x, Tensor grad_out, IntArray shape) -> Tensor(grad_x)
args : (Tensor grad_x_grad, IntArray shape)
......
......@@ -435,16 +435,6 @@
func : expand
backward : expand_grad
- op : expand_as
args : (Tensor x, Tensor y, int[] target_shape)
output : Tensor(out)
infer_meta :
func : ExpandAsInferMeta
kernel :
func : expand_as
optional : y
backward : expand_as_grad
- op : exponential_
args : (Tensor x, float lam)
output : Tensor(out)
......
......@@ -740,9 +740,9 @@
attrs : [bool use_mkldnn = false, str mkldnn_data_type = "float32"]
- op : expand_as (expand_as_v2)
backward : expand_as_grad (expand_as_v2_grad)
inputs :
x : X
y : Y
{x : X, y : Y}
outputs :
out : Out
......
......@@ -66,6 +66,13 @@
comment : In order to force fill output variable to gpu memory.
default : "false"
- op : expand_as_v2
version :
- checkpoint : fix expand_as_v2 and add new input [Y].
action :
- add_input : Y
comment : Expand X according to the shape of Y.
- op : flip
version :
- checkpoint : Upgrade flip, add new attr [axis] and delete attr [dims]
......
......@@ -608,6 +608,17 @@
inplace : (x -> out)
backward : exp_grad
- op : expand_as
args : (Tensor x, Tensor y, int[] target_shape = {})
output : Tensor(out)
infer_meta :
func : ExpandAsInferMeta
kernel :
func : expand_as
data_type : x
optional : y
backward : expand_as_grad
- op : expm1
args : (Tensor x)
output : Tensor
......
// Copyright (c) 2022 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/phi/core/compat/op_utils.h"
namespace phi {
KernelSignature ExpandAsOpArgumentMapping(const ArgumentMappingContext& ctx) {
return KernelSignature("expand_as", {"X", "Y"}, {"target_shape"}, {"Out"});
}
KernelSignature ExpandAsGradOpArgumentMapping(
const ArgumentMappingContext& ctx) {
return KernelSignature(
"expand_as_grad", {"X", "Out@GRAD"}, {"target_shape"}, {"X@GRAD"});
}
} // namespace phi
PD_REGISTER_BASE_KERNEL_NAME(expand_as_v2, expand_as);
PD_REGISTER_BASE_KERNEL_NAME(expand_as_v2_grad, expand_as_grad);
PD_REGISTER_ARG_MAPPING_FN(expand_as_v2, phi::ExpandAsOpArgumentMapping);
PD_REGISTER_ARG_MAPPING_FN(expand_as_v2_grad,
phi::ExpandAsGradOpArgumentMapping);
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册