fused_elemwise_activation_op.cc 14.7 KB
Newer Older
C
chengduo 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* Copyright (c) 2018 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. */

W
Wu Yi 已提交
15
#include "paddle/fluid/operators/fused/fused_elemwise_activation_op.h"
16 17
#include <memory>
#include <unordered_set>
C
chengduo 已提交
18 19 20 21

namespace paddle {
namespace operators {

C
chengduo 已提交
22
bool IsUnaryCompound(const std::vector<std::string> &functor_list) {
23 24 25 26 27
  PADDLE_ENFORCE_EQ(
      functor_list.size(), 2,
      platform::errors::InvalidArgument(
          "Invalid functor list size %d, which should be equal to %d.",
          functor_list.size(), 2));
28 29 30 31 32 33
  static std::unordered_set<std::string> binary_fun = {
      "elementwise_add", "elementwise_mul", "elementwise_add_grad",
      "elementwise_mul_grad"};
  return binary_fun.count(functor_list[1]) != 0;
}

C
chengduo 已提交
34
bool HasInPlaceUnary(const std::vector<std::string> &functor_list) {
35 36 37 38 39
  PADDLE_ENFORCE_EQ(
      functor_list.size(), 2,
      platform::errors::InvalidArgument(
          "Invalid functor list size %d, which should be equal to %d.",
          functor_list.size(), 2));
C
chengduo 已提交
40 41 42 43 44 45 46 47 48
  static std::unordered_set<std::string> InplaceOpSet = {"relu", "relu_grad"};
  bool is_in_place = false;
  for (auto &func_name : functor_list) {
    is_in_place |= (InplaceOpSet.count(func_name) == 1);
  }
  return is_in_place;
}

bool InputXCanBeAbsent(const std::vector<std::string> &functor_list) {
49 50 51 52 53
  PADDLE_ENFORCE_EQ(
      functor_list.size(), 2,
      platform::errors::InvalidArgument(
          "Invalid functor list size %d, which should be equal to %d.",
          functor_list.size(), 2));
54 55 56 57 58 59 60 61 62 63 64
  static std::unordered_set<std::string> binary_fun = {"elementwise_add_grad"};
  return binary_fun.count(functor_list[0]) != 0 ||
         binary_fun.count(functor_list[1]) != 0;
}

/*
 * Whether the compound function is supported.
 * For Unary(Binary(X, Y)), the intermediate_out's shape is the same the final
 * out.
 */
static bool IsSupportedCompound(const std::vector<std::string> &functors) {
65 66 67 68 69
  PADDLE_ENFORCE_EQ(
      functors.size(), 2UL,
      platform::errors::InvalidArgument(
          "Invalid functor list size %d, which should be equal to %d.",
          functors.size(), 2));
70 71 72

  static std::unordered_set<std::string> unary_fun = {"scale", "relu", "tanh",
                                                      "sigmoid"};
73 74 75 76 77 78 79 80 81
  static std::unordered_set<std::string> binary_fun = {"elementwise_add",
                                                       "elementwise_mul"};

  std::string unary_fun_str;
  if (binary_fun.count(functors[0])) {
    unary_fun_str = functors[1];
  } else if (binary_fun.count(functors[1])) {
    unary_fun_str = functors[0];
  } else {
82 83
    PADDLE_THROW(platform::errors::InvalidArgument(
        "%s and %s are not included in fused_list.", functors[0], functors[1]));
84 85
  }
  PADDLE_ENFORCE_EQ(unary_fun.count(unary_fun_str), 1,
86 87
                    platform::errors::InvalidArgument(
                        "%s is not included in fused_list.", unary_fun_str));
88 89 90
  return true;
}

C
chengduo 已提交
91 92 93 94 95
class FusedElemwiseActivationOp : public framework::OperatorWithKernel {
 public:
  using framework::OperatorWithKernel::OperatorWithKernel;

  void InferShape(framework::InferShapeContext *ctx) const override {
96 97 98 99 100 101 102 103 104 105 106 107
    PADDLE_ENFORCE_EQ(
        ctx->HasInput("X"), true,
        platform::errors::InvalidArgument(
            "Input(X) of FusedElemwiseActivationOp op should not be null."));
    PADDLE_ENFORCE_EQ(
        ctx->HasInput("Y"), true,
        platform::errors::InvalidArgument(
            "Input(Y) of FusedElemwiseActivationOp op should not be null."));
    PADDLE_ENFORCE_EQ(
        ctx->HasOutput("Out"), true,
        platform::errors::InvalidArgument(
            "Output(Out) of FusedElemwiseActivationOp op should not be null."));
C
chengduo 已提交
108 109 110 111

    auto x_dim = ctx->GetInputDim("X");
    auto y_dim = ctx->GetInputDim("Y");

112 113
    // Whether the shape of Y is a continuous subsequence of X,
    // For more information please refer to the op's introduction.
C
chengduo 已提交
114
    bool bcast_y = IsBcastY(x_dim, y_dim);
115 116 117 118

    auto &out_dim = bcast_y ? x_dim : y_dim;
    std::string out_lod = bcast_y ? "X" : "Y";

C
chengduo 已提交
119
    if (ctx->Attrs().Get<bool>("save_intermediate_out")) {
120 121 122 123 124
      PADDLE_ENFORCE_EQ(
          ctx->HasOutput("IntermediateOut"), true,
          platform::errors::InvalidArgument(
              "Output(IntermediateOut) of FusedElemwiseActivationOp "
              "should not be null."));
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142

      if (IsUnaryCompound(
              ctx->Attrs().Get<std::vector<std::string>>("functor_list"))) {
        // for Unary(Binary(X, Y)), the shape and lod of out and
        // intermediate_out are the same.
        ctx->SetOutputDim("IntermediateOut", out_dim);
        // set the lod of intermediate_out
        ctx->ShareLoD(out_lod, /*->*/ "IntermediateOut");
      } else {
        // for Binary(X, Unary(Y)), the shape and lod of Y and
        // intermediate_out are the same.
        ctx->SetOutputDim("IntermediateOut", y_dim);
        // set the lod of intermediate_out
        ctx->ShareLoD("Y", /*->*/ "IntermediateOut");
      }
    }
    ctx->SetOutputDim("Out", out_dim);
    ctx->ShareLoD(out_lod, /*->*/ "Out");
C
chengduo 已提交
143 144
  }

C
chengduo 已提交
145 146 147 148 149 150 151 152 153 154 155 156 157 158
  static bool IsBcastY(const framework::DDim &x_dim,
                       const framework::DDim &y_dim) {
    bool bcast_y = x_dim.size() >= y_dim.size();
    if (x_dim.size() == y_dim.size()) {
      for (int i = 0; i < x_dim.size(); ++i) {
        if (x_dim[i] < y_dim[i]) {
          bcast_y = false;
          break;
        }
      }
    }
    return bcast_y;
  }

C
chengduo 已提交
159 160 161 162 163
 protected:
  framework::OpKernelType GetExpectedKernelType(
      const framework::ExecutionContext &ctx) const override {
    PADDLE_ENFORCE_EQ(ctx.Input<framework::Tensor>("X")->type(),
                      ctx.Input<framework::Tensor>("Y")->type(),
164 165
                      platform::errors::InvalidArgument(
                          "The element's type of input should be the same."));
166 167
    return framework::OpKernelType(
        OperatorWithKernel::IndicateVarDataType(ctx, "X"), ctx.GetPlace());
C
chengduo 已提交
168 169 170 171 172 173
  }
};

class FusedElemwiseActivationMaker : public framework::OpProtoAndCheckerMaker {
 public:
  void Make() override {
174 175 176 177 178 179 180 181 182 183 184 185 186
    AddInput(
        "X",
        "(Tensor) The input tensor of fused_elemwise_activation operator.");
    AddInput(
        "Y",
        "(Tensor) The input tensor of fused_elemwise_activation operator.");
    AddOutput("Out",
              "vector<Tensor> The output tensor of fused_elemwise_activation "
              "operator.");
    AddOutput("IntermediateOut",
              "Tensor The IntermediateOut tensor of fused_elemwise_activation "
              "operator.")
        .AsIntermediate();
C
chengduo 已提交
187 188 189 190 191 192
    AddAttr<int>("axis",
                 "axis is used by elementwise_op, the default value is -1.")
        .SetDefault(-1);
    AddAttr<float>("scale",
                   "scale is used by scale_op, the default value is 0.0.")
        .SetDefault(0.0);
C
chengduo 已提交
193
    AddAttr<bool>("save_intermediate_out",
194 195
                  "Whether to save the intermediate_out.")
        .SetDefault(false);
C
chengduo 已提交
196 197 198
    AddAttr<std::vector<std::string>>("functor_list",
                                      "The functors that should be fused.")
        .AddCustomChecker([&](const std::vector<std::string> &functor_list) {
199 200 201 202
          PADDLE_ENFORCE_EQ(
              IsSupportedCompound(functor_list), true,
              platform::errors::InvalidArgument(
                  "the input functors should support compounding."));
C
chengduo 已提交
203 204 205 206 207 208 209 210 211 212 213
        });

    AddComment(R"DOC(
FusedElemwiseActivation Operator.

At present, FusedElemwiseActivation only supports Two kinds of compound
operators (elementwise_op and activation_op):

    Z = Binary(X, Unary(Y))
    Z = Unary(Binary(X, Y))

214
There are two cases for this operator:
C
chengduo 已提交
215

216 217
1. The shape of $Y$ and $X$ is the same.
2. The shape of $Y$ is a continuous subsequence of $X$ or the shape of $X$ is a continuous subsequence of $Y$.
C
chengduo 已提交
218

219
For case 2 (assume that the shape of $Y$ is a continuous subsequence of $X$ ):
C
chengduo 已提交
220

221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
1. Broadcast $Y$ to match the shape of $X$, where $axis$ is the start dimension index
   for broadcasting $Y$ onto $X$.
2. If $axis$ is -1 (default), $axis = rank(X) - rank(Y)$.
3. The trailing dimensions of size 1 for $Y$ will be ignored for the consideration of
   subsequence, such as shape(Y) = (2, 1) => (2).

For example:

  .. code-block:: python

    shape(X) = (2, 3, 4, 5), shape(Y) = (,)
    shape(X) = (2, 3, 4, 5), shape(Y) = (5,)
    shape(X) = (2, 3, 4, 5), shape(Y) = (4, 5), with axis=-1(default) or axis=2
    shape(X) = (2, 3, 4, 5), shape(Y) = (3, 4), with axis=1
    shape(X) = (2, 3, 4, 5), shape(Y) = (2), with axis=0
    shape(X) = (2, 3, 4, 5), shape(Y) = (2, 1), with axis=0


The inputs $X$ and $Y$ can carry the different LoD information.
But the output only shares the LoD information with the one whose shape is the same with Out.
The attributions of activation_op can be get from fused_elemwise_activation_op's.
The functor_list records the functions to be fused, for example
["scale", "elementwise_add"].

)DOC");
C
chengduo 已提交
246 247 248
  }
};

H
hong 已提交
249
template <typename T>
C
chengduo 已提交
250
class FusedElemwiseActivationGradMaker
H
hong 已提交
251
    : public framework::SingleGradOpMaker<T> {
C
chengduo 已提交
252
 public:
H
hong 已提交
253
  using framework::SingleGradOpMaker<T>::SingleGradOpMaker;
C
chengduo 已提交
254 255

 protected:
256
  void Apply(GradOpPtr<T> grad_op) const override {
C
chengduo 已提交
257
    grad_op->SetType(this->ForwardOpType() + "_grad");
C
chengduo 已提交
258 259

    for (auto &input_param : this->InputNames()) {
C
chengduo 已提交
260 261 262
      grad_op->SetInput(input_param, this->Input(input_param));
      grad_op->SetOutput(framework::GradVarName(input_param),
                         this->InputGrad(input_param, true));
C
chengduo 已提交
263 264
    }

C
chengduo 已提交
265 266
    grad_op->SetInput("Out", this->Output("Out"));
    grad_op->SetInput(framework::GradVarName("Out"), this->OutputGrad("Out"));
267

C
chengduo 已提交
268
    grad_op->SetAttrMap(this->Attrs());
C
chengduo 已提交
269

270 271
    std::vector<std::string> functor_names = BOOST_GET_CONST(
        std::vector<std::string>, grad_op->GetAttr("functor_list"));
C
chengduo 已提交
272

C
chengduo 已提交
273 274
    functor_names[0] += "_grad";
    functor_names[1] += "_grad";
C
chengduo 已提交
275 276
    grad_op->SetAttr("functor_list", functor_names);

277
    if (BOOST_GET_CONST(bool, grad_op->GetAttr("save_intermediate_out"))) {
H
hong 已提交
278
      // PADDLE_ENFORCE_NE(Output("IntermediateOut").size(), 0);
C
chengduo 已提交
279 280 281 282
      grad_op->SetInput("IntermediateOut", this->Output("IntermediateOut"));
      grad_op->SetOutput(framework::GradVarName("IntermediateOut"),
                         this->OutputGrad("IntermediateOut"));
    } else {
283 284 285
      grad_op->SetInput("IntermediateOut", this->EmptyOutput());
      grad_op->SetOutput(framework::GradVarName("IntermediateOut"),
                         this->EmptyOutputGrad());
C
chengduo 已提交
286
    }
C
chengduo 已提交
287 288 289 290 291 292 293 294
  }
};

class FusedElemwiseActivationOpGrad : public framework::OperatorWithKernel {
 public:
  using framework::OperatorWithKernel::OperatorWithKernel;

  void InferShape(framework::InferShapeContext *ctx) const override {
295 296 297
    PADDLE_ENFORCE_EQ(ctx->HasInput(framework::GradVarName("Out")), true,
                      platform::errors::InvalidArgument(
                          "Input(Out@Grad) should not be null."));
C
chengduo 已提交
298 299 300 301 302

    auto functor_list =
        ctx->Attrs().Get<std::vector<std::string>>("functor_list");

    if (ctx->Attrs().Get<bool>("save_intermediate_out")) {
303 304 305
      PADDLE_ENFORCE_EQ(ctx->HasInput("IntermediateOut"), true,
                        platform::errors::InvalidArgument(
                            "Input(IntermediateOut) should not be null."));
306
    } else {
C
chengduo 已提交
307
      if (!InputXCanBeAbsent(functor_list)) {
308 309 310
        PADDLE_ENFORCE_EQ(
            ctx->HasInput("X"), true,
            platform::errors::InvalidArgument("Input(X) should not be null."));
C
chengduo 已提交
311
      }
312
    }
C
chengduo 已提交
313 314 315

    auto x_grad_name = framework::GradVarName("X");
    auto y_grad_name = framework::GradVarName("Y");
C
chengduo 已提交
316
    auto inter_grad_name = framework::GradVarName("IntermediateOut");
317

C
chengduo 已提交
318
    if (ctx->HasOutput(x_grad_name)) {
319 320 321 322 323 324
      if (ctx->HasInputs("X")) {
        ctx->SetOutputDim(x_grad_name, ctx->GetInputDim("X"));
        ctx->ShareLoD("X", x_grad_name);
      } else {
        // Currently, only when Binary is elementwise_add or elementwise_sub,
        // the "X" could be absent.
325 326 327 328 329
        PADDLE_ENFORCE_EQ(
            InputXCanBeAbsent(functor_list), true,
            platform::errors::InvalidArgument(
                "Only when BinaryFunctor is elementwise_add, the 'X' "
                "could be absent."));
330

C
chengduo 已提交
331 332
        // Node: If "X" is absence, the shape of Y should be a continuous
        // subsequence of X, otherwise, we could not infer the shape of dx.
333 334 335 336 337

        ctx->SetOutputDim(x_grad_name,
                          ctx->GetInputDim(framework::GradVarName("Out")));
        ctx->ShareLoD(framework::GradVarName("Out"), x_grad_name);
      }
C
chengduo 已提交
338
    }
C
chengduo 已提交
339

C
chengduo 已提交
340
    if (ctx->HasOutput(y_grad_name)) {
341 342 343
      PADDLE_ENFORCE_EQ(
          ctx->HasInput("Y"), true,
          platform::errors::InvalidArgument("Input(Y) should not be null."));
344 345
      ctx->SetOutputDim(y_grad_name, ctx->GetInputDim("Y"));
      ctx->ShareLoD("Y", y_grad_name);
C
chengduo 已提交
346
    }
C
chengduo 已提交
347 348 349 350 351 352 353 354 355 356 357 358

    if (ctx->HasOutput(inter_grad_name)) {
      // For Unary(Binary(X, Y)), IntermediateOut should not be empty.
      if (IsUnaryCompound(functor_list)) {
        ctx->SetOutputDim(inter_grad_name,
                          ctx->GetInputDim(framework::GradVarName("Out")));
        ctx->ShareLoD(framework::GradVarName("Out"), inter_grad_name);
      } else {
        ctx->SetOutputDim(inter_grad_name, ctx->GetInputDim("Y"));
        ctx->ShareLoD("Y", inter_grad_name);
      }
    }
C
chengduo 已提交
359 360 361 362 363
  }

 protected:
  framework::OpKernelType GetExpectedKernelType(
      const framework::ExecutionContext &ctx) const override {
364 365
    return framework::OpKernelType(
        OperatorWithKernel::IndicateVarDataType(ctx, "Y"), ctx.GetPlace());
C
chengduo 已提交
366 367 368 369 370 371
  }
};
}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
H
hong 已提交
372 373 374 375 376
REGISTER_OPERATOR(
    fused_elemwise_activation, ops::FusedElemwiseActivationOp,
    ops::FusedElemwiseActivationMaker,
    ops::FusedElemwiseActivationGradMaker<paddle::framework::OpDesc>,
    ops::FusedElemwiseActivationGradMaker<paddle::imperative::OpBase>);
C
chengduo 已提交
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
REGISTER_OPERATOR(fused_elemwise_activation_grad,
                  ops::FusedElemwiseActivationOpGrad);

REGISTER_OP_CPU_KERNEL(
    fused_elemwise_activation,
    ops::FusedElemwiseActivationKernel<paddle::platform::CPUDeviceContext,
                                       float>,
    ops::FusedElemwiseActivationKernel<paddle::platform::CPUDeviceContext,
                                       double>);

REGISTER_OP_CPU_KERNEL(
    fused_elemwise_activation_grad,
    ops::FusedElemwiseActivationGradKernel<paddle::platform::CPUDeviceContext,
                                           float>,
    ops::FusedElemwiseActivationGradKernel<paddle::platform::CPUDeviceContext,
                                           double>);