|
| 1 | +/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. |
| 2 | +
|
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | +
|
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | +Unless required by applicable law or agreed to in writing, software |
| 10 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +See the License for the specific language governing permissions and |
| 13 | +limitations under the License. */ |
| 14 | + |
| 15 | +#include <string> |
| 16 | +#include <vector> |
| 17 | + |
| 18 | +#include "paddle/fluid/operators/fused_elemwise_activation_op.h" |
| 19 | + |
| 20 | +namespace paddle { |
| 21 | +namespace operators { |
| 22 | + |
| 23 | +class FusedElemwiseActivationOp : public framework::OperatorWithKernel { |
| 24 | + public: |
| 25 | + using framework::OperatorWithKernel::OperatorWithKernel; |
| 26 | + |
| 27 | + void InferShape(framework::InferShapeContext *ctx) const override { |
| 28 | + PADDLE_ENFORCE( |
| 29 | + ctx->HasInput("X"), |
| 30 | + "Input(X) of FusedElemwiseActivationOp op should not be null."); |
| 31 | + PADDLE_ENFORCE( |
| 32 | + ctx->HasInput("Y"), |
| 33 | + "Input(Y) of FusedElemwiseActivationOp op should not be null."); |
| 34 | + PADDLE_ENFORCE( |
| 35 | + ctx->HasOutput("Out"), |
| 36 | + "Output(Out) of FusedElemwiseActivationOp op should not be null."); |
| 37 | + |
| 38 | + auto x_dim = ctx->GetInputDim("X"); |
| 39 | + auto y_dim = ctx->GetInputDim("Y"); |
| 40 | + PADDLE_ENFORCE_GE(x_dim.size(), y_dim.size(), |
| 41 | + "Rank of first input must >= rank of second input."); |
| 42 | + |
| 43 | + ctx->SetOutputDim("Out", x_dim); |
| 44 | + ctx->ShareLoD("X", /*->*/ "Out"); |
| 45 | + } |
| 46 | + |
| 47 | + protected: |
| 48 | + framework::OpKernelType GetExpectedKernelType( |
| 49 | + const framework::ExecutionContext &ctx) const override { |
| 50 | + PADDLE_ENFORCE_EQ(ctx.Input<framework::Tensor>("X")->type(), |
| 51 | + ctx.Input<framework::Tensor>("Y")->type(), |
| 52 | + "The element's type of input should be the same."); |
| 53 | + auto input_data_type = |
| 54 | + framework::ToDataType(ctx.Input<framework::Tensor>("X")->type()); |
| 55 | + return framework::OpKernelType(input_data_type, ctx.GetPlace()); |
| 56 | + } |
| 57 | +}; |
| 58 | + |
| 59 | +class FusedElemwiseActivationMaker : public framework::OpProtoAndCheckerMaker { |
| 60 | + public: |
| 61 | + void Make() override { |
| 62 | + AddInput("X", "(vector<Tensor>)"); |
| 63 | + AddInput("Y", "(vector<Tensor>)"); |
| 64 | + AddOutput("Out", "vector<Tensor>"); |
| 65 | + AddAttr<int>("axis", |
| 66 | + "axis is used by elementwise_op, the default value is -1.") |
| 67 | + .SetDefault(-1); |
| 68 | + AddAttr<float>("scale", |
| 69 | + "scale is used by scale_op, the default value is 0.0.") |
| 70 | + .SetDefault(0.0); |
| 71 | + AddAttr<bool>("recomputation", |
| 72 | + "Whether to recompute the Out." |
| 73 | + "fused_elemwise_activation_grad has two methods to get the " |
| 74 | + "dx and dy, one " |
| 75 | + "is to use the 'Out', and the other is not to use it. " |
| 76 | + "The former method will save the time of recomputing the " |
| 77 | + "'Out', but it must occupy the memory to store the 'out'. " |
| 78 | + "While, the later method can avoid occupying the memory, " |
| 79 | + "but it must recompute the 'Out'. The default value is true.") |
| 80 | + .SetDefault(true); |
| 81 | + AddAttr<std::vector<std::string>>("functor_list", |
| 82 | + "The functors that should be fused.") |
| 83 | + .AddCustomChecker([&](const std::vector<std::string> &functor_list) { |
| 84 | + PADDLE_ENFORCE(ValidCheck(functor_list)); |
| 85 | + }); |
| 86 | + |
| 87 | + AddComment(R"DOC( |
| 88 | +FusedElemwiseActivation Operator. |
| 89 | +
|
| 90 | +At present, FusedElemwiseActivation only supports Two kinds of compound |
| 91 | +operators (elementwise_op and activation_op): |
| 92 | +
|
| 93 | + Z = Binary(X, Unary(Y)) |
| 94 | + Z = Unary(Binary(X, Y)) |
| 95 | +
|
| 96 | +The attributions of activation_op can be get from fused_elemwise_activation_op's |
| 97 | +attributions. functor_list records the functors to be fused, for example |
| 98 | +"scale,elementwise_add". |
| 99 | +
|
| 100 | +)DOC"); |
| 101 | + } |
| 102 | + |
| 103 | + private: |
| 104 | + bool ValidCheck(const std::vector<std::string> &functors) { |
| 105 | + std::unordered_set<std::string> unary_fun = {"scale", "relu"}; |
| 106 | + std::unordered_set<std::string> binary_fun = {"elementwise_add"}; |
| 107 | + |
| 108 | + std::string unary_fun_str; |
| 109 | + if (binary_fun.count(functors[0])) { |
| 110 | + unary_fun_str = functors[1]; |
| 111 | + } else if (binary_fun.count(functors[1])) { |
| 112 | + unary_fun_str = functors[0]; |
| 113 | + } else { |
| 114 | + PADDLE_THROW("%s and %s are not included in fused_list.", functors[0], |
| 115 | + functors[1]); |
| 116 | + } |
| 117 | + PADDLE_ENFORCE_EQ(unary_fun.count(unary_fun_str), 1, |
| 118 | + "%s is not included in fused_list.", unary_fun_str); |
| 119 | + return true; |
| 120 | + } |
| 121 | +}; |
| 122 | + |
| 123 | +class FusedElemwiseActivationGradMaker |
| 124 | + : public framework::SingleGradOpDescMaker { |
| 125 | + public: |
| 126 | + using framework::SingleGradOpDescMaker::SingleGradOpDescMaker; |
| 127 | + |
| 128 | + protected: |
| 129 | + std::unique_ptr<framework::OpDesc> Apply() const override { |
| 130 | + auto *op_desc_ptr = new framework::OpDesc(); |
| 131 | + op_desc_ptr->SetType(this->ForwardOpType() + "_grad"); |
| 132 | + |
| 133 | + for (auto &input_param : this->InputNames()) { |
| 134 | + op_desc_ptr->SetInput(input_param, this->Input(input_param)); |
| 135 | + op_desc_ptr->SetOutput(framework::GradVarName(input_param), |
| 136 | + this->InputGrad(input_param, true)); |
| 137 | + } |
| 138 | + |
| 139 | + for (auto &output_param : this->OutputNames()) { |
| 140 | + op_desc_ptr->SetInput(output_param, this->Output(output_param)); |
| 141 | + op_desc_ptr->SetInput(framework::GradVarName(output_param), |
| 142 | + this->OutputGrad(output_param)); |
| 143 | + } |
| 144 | + op_desc_ptr->SetAttrMap(this->Attrs()); |
| 145 | + |
| 146 | + std::vector<std::string> functor_names = |
| 147 | + boost::get<std::vector<std::string>>( |
| 148 | + op_desc_ptr->GetAttr("functor_list")); |
| 149 | + functor_names[0] += "_grad"; |
| 150 | + functor_names[1] += "_grad"; |
| 151 | + op_desc_ptr->SetAttr("functor_list", functor_names); |
| 152 | + return std::unique_ptr<framework::OpDesc>(op_desc_ptr); |
| 153 | + } |
| 154 | +}; |
| 155 | + |
| 156 | +class FusedElemwiseActivationOpGrad : public framework::OperatorWithKernel { |
| 157 | + public: |
| 158 | + using framework::OperatorWithKernel::OperatorWithKernel; |
| 159 | + |
| 160 | + void InferShape(framework::InferShapeContext *ctx) const override { |
| 161 | + PADDLE_ENFORCE(ctx->HasInput("X"), "Input(X) should not be null"); |
| 162 | + PADDLE_ENFORCE(ctx->HasInput("Y"), "Input(Y) should not be null"); |
| 163 | + PADDLE_ENFORCE(ctx->HasInput(framework::GradVarName("Out")), |
| 164 | + "Input(Out@GRAD) should not be null"); |
| 165 | + |
| 166 | + auto x_dims = ctx->GetInputDim("X"); |
| 167 | + auto y_dims = ctx->GetInputDim("Y"); |
| 168 | + auto out_dims = ctx->GetInputDim(framework::GradVarName("Out")); |
| 169 | + |
| 170 | + PADDLE_ENFORCE_GE(x_dims.size(), y_dims.size(), |
| 171 | + "Rank of first input must >= rank of second input."); |
| 172 | + |
| 173 | + auto x_grad_name = framework::GradVarName("X"); |
| 174 | + auto y_grad_name = framework::GradVarName("Y"); |
| 175 | + if (ctx->HasOutput(x_grad_name)) { |
| 176 | + ctx->SetOutputDim(x_grad_name, x_dims); |
| 177 | + } |
| 178 | + if (ctx->HasOutput(y_grad_name)) { |
| 179 | + ctx->SetOutputDim(y_grad_name, y_dims); |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + protected: |
| 184 | + framework::OpKernelType GetExpectedKernelType( |
| 185 | + const framework::ExecutionContext &ctx) const override { |
| 186 | + auto input_data_type_index = ctx.Input<framework::Tensor>("X")->type(); |
| 187 | + PADDLE_ENFORCE_EQ(input_data_type_index, |
| 188 | + ctx.Input<framework::Tensor>("Y")->type(), |
| 189 | + "The element's type of input should be the same."); |
| 190 | + PADDLE_ENFORCE_EQ( |
| 191 | + input_data_type_index, |
| 192 | + ctx.Input<framework::Tensor>(framework::GradVarName("Out"))->type(), |
| 193 | + "The element's type of input should be the same."); |
| 194 | + |
| 195 | + auto input_data_type = framework::ToDataType(input_data_type_index); |
| 196 | + return framework::OpKernelType(input_data_type, ctx.GetPlace()); |
| 197 | + } |
| 198 | +}; |
| 199 | +} // namespace operators |
| 200 | +} // namespace paddle |
| 201 | + |
| 202 | +namespace ops = paddle::operators; |
| 203 | +REGISTER_OPERATOR(fused_elemwise_activation, ops::FusedElemwiseActivationOp, |
| 204 | + ops::FusedElemwiseActivationMaker, |
| 205 | + ops::FusedElemwiseActivationGradMaker); |
| 206 | +REGISTER_OPERATOR(fused_elemwise_activation_grad, |
| 207 | + ops::FusedElemwiseActivationOpGrad); |
| 208 | + |
| 209 | +REGISTER_OP_CPU_KERNEL( |
| 210 | + fused_elemwise_activation, |
| 211 | + ops::FusedElemwiseActivationKernel<paddle::platform::CPUDeviceContext, |
| 212 | + float>, |
| 213 | + ops::FusedElemwiseActivationKernel<paddle::platform::CPUDeviceContext, |
| 214 | + double>); |
| 215 | + |
| 216 | +REGISTER_OP_CPU_KERNEL( |
| 217 | + fused_elemwise_activation_grad, |
| 218 | + ops::FusedElemwiseActivationGradKernel<paddle::platform::CPUDeviceContext, |
| 219 | + float>, |
| 220 | + ops::FusedElemwiseActivationGradKernel<paddle::platform::CPUDeviceContext, |
| 221 | + double>); |
0 commit comments