norm_op.cc 3.7 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
2 3 4 5 6 7 8 9 10 11 12 13 14

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Indicesou 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. */

Y
Yi Wang 已提交
15
#include "paddle/fluid/operators/norm_op.h"
16 17 18
namespace paddle {
namespace operators {

S
sweetsky0901 已提交
19
template <typename AttrType>
20 21
class NormOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
Y
Yu Yang 已提交
22
  void Make() override {
23 24 25 26 27 28 29 30
    AddInput(
        "X",
        "(Tensor) The input tensor of norm operator. "
        "The format of input tensor is NCHW. Where N is batch size, C is the "
        "number of channels, H and W is the height and width of feature.");
    AddInput("Scale",
             "(Tensor) The input tensor of norm operator. "
             "The format of input tensor is C * 1.");
S
sweetsky0901 已提交
31 32 33
    AddAttr<AttrType>("epsilon",
                      "(float, default 1e-10) Constant "
                      "for numerical stability.")
34 35 36 37 38 39 40
        .SetDefault(1.0e-10f);
    AddOutput("Out",
              "(Tensor) The output tensor of norm operator."
              "N * M."
              "M = C * H * W");
    AddComment(R"DOC(
       "Input shape: $(N, C, H, W)$
X
xuwei06 已提交
41
        Scale shape: $(C, 1)$
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
        Output shape: $(N, C, H, W)$
        Where
        forward
          $$
            [\frac {x_{1}}{\sqrt{\sum{x_{i}^{2}}}} \frac {x_{2}}{\sqrt{\sum{x_{i}^{2}}}} \frac {x_{3}}{\sqrt{\sum{x_{i}^{2}}}} \cdot  \cdot  \cdot \frac {x_{n}}{\sqrt{\sum{x_{i}^{2}}}}]
          $$
        backward
          $$
            \frac{\frac{\mathrm{d}L }{\mathrm{d}y_{1}} - \frac {x_{1}\sum {\frac{\mathrm{d} L}{\mathrm{d} y_{j}}}x_{j}}{\sum x_{j}^{2}} }{\sqrt{\sum{x_{j}^{2}}}}
          $$
        )DOC");
  }
};

class NormOp : public framework::OperatorWithKernel {
 public:
  using framework::OperatorWithKernel::OperatorWithKernel;
  void InferShape(framework::InferShapeContext* ctx) const override {
    PADDLE_ENFORCE(ctx->HasInput("X"),
                   "Input(X) of NormOp"
                   "should not be null.");
63 64 65
    PADDLE_ENFORCE(ctx->HasInput("Scale"),
                   "Input(Scale) of NormOp"
                   "should not be null.");
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
    PADDLE_ENFORCE(ctx->HasOutput("Out"),
                   "Output(Out) of NormOp should not be null.");
    auto in_x_dims = ctx->GetInputDim("X");
    ctx->SetOutputDim("Out", in_x_dims);
  }
};

class NormOpGrad : public framework::OperatorWithKernel {
 public:
  using framework::OperatorWithKernel::OperatorWithKernel;
  void InferShape(framework::InferShapeContext* ctx) const override {
    PADDLE_ENFORCE(ctx->HasInput("X"), "Input(X) must not be null.");
    PADDLE_ENFORCE(ctx->HasOutput(framework::GradVarName("X")),
                   "Input(X@GRAD) should not be null.");
    ctx->SetOutputDim(framework::GradVarName("X"), ctx->GetInputDim("X"));
  }
};
}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
Y
Yang Yang 已提交
87
REGISTER_OPERATOR(norm, ops::NormOp, ops::NormOpMaker<float>,
88 89
                  paddle::framework::DefaultGradOpDescMaker<true>);
REGISTER_OPERATOR(norm_grad, ops::NormOpGrad);
90 91
REGISTER_OP_CPU_KERNEL(
    norm, ops::NormKernel<paddle::platform::CPUDeviceContext, float>,
S
sweetsky0901 已提交
92
    ops::NormKernel<paddle::platform::CPUDeviceContext, double, float>);
93 94
REGISTER_OP_CPU_KERNEL(
    norm_grad, ops::NormGradKernel<paddle::platform::CPUDeviceContext, float>,
S
sweetsky0901 已提交
95
    ops::NormGradKernel<paddle::platform::CPUDeviceContext, double, float>);