norm_op.cc 3.8 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. */

15 16 17
#include <memory>
#include <string>
#include <vector>
18

H
hong 已提交
19
#include "paddle/fluid/framework/infershape_utils.h"
H
hong 已提交
20
#include "paddle/fluid/framework/op_registry.h"
H
hong 已提交
21
#include "paddle/phi/infermeta/unary.h"
22

23 24 25 26 27
namespace paddle {
namespace operators {

class NormOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
Y
Yu Yang 已提交
28
  void Make() override {
29 30 31 32 33 34 35 36
    AddInput("X", "(Tensor) A tensor of rank >= axis.");
    AddAttr<int>("axis",
                 "The axis on which to apply normalization. If axis < 0, "
                 "the dimension to normalization is rank(X) + axis. -1 is "
                 "the last dimension.");
    AddAttr<float>("epsilon",
                   "(float, default 1e-10) The epsilon value is used "
                   "to avoid division by zero.")
37
        .SetDefault(1.0e-10f);
38 39 40
    AddOutput("Norm",
              "(Tensor) A tensor saved the `sqrt(sum(x) + epsion)` will "
              "be used in backward kernel.")
41 42 43 44 45 46
        .AsIntermediate()
        .AsExtra();
    AddAttr<bool>("is_test",
                  "(bool, default false) Set to true for inference only, false "
                  "for training.")
        .SetDefault(false);
47
    AddOutput("Out", "(Tensor) A tensor of the same shape as X.");
48
    AddComment(R"DOC(
49 50 51 52 53 54 55 56

Given a tensor, apply 2-normalization along the provided axis.

$$
y = \frac{x}{ \sqrt{\sum {x^2} + epsion }}
$$

where, $\sum {x^2}$ is calculated along the `axis` dimension.
57

58
)DOC");
59 60 61 62 63 64 65 66 67 68 69
  }
};

class NormOp : public framework::OperatorWithKernel {
  using framework::OperatorWithKernel::OperatorWithKernel;
};

class NormOpGrad : public framework::OperatorWithKernel {
 public:
  using framework::OperatorWithKernel::OperatorWithKernel;
  void InferShape(framework::InferShapeContext* ctx) const override {
70
    OP_INOUT_CHECK(ctx->HasInput("X"), "Input", "X", "NormOpGrad");
71 72 73 74
    OP_INOUT_CHECK(ctx->HasOutput(framework::GradVarName("X")),
                   "Input",
                   "X@GRAD",
                   "NormOpGrad");
75 76 77
    ctx->SetOutputDim(framework::GradVarName("X"), ctx->GetInputDim("X"));
  }
};
78

H
hong 已提交
79 80
template <typename T>
class NormOpGradOpMaker : public framework::SingleGradOpMaker<T> {
81
 public:
H
hong 已提交
82
  using framework::SingleGradOpMaker<T>::SingleGradOpMaker;
83 84

 protected:
85
  void Apply(GradOpPtr<T> op) const override {
86
    op->SetType("norm_grad");
H
hong 已提交
87 88 89
    op->SetAttrMap(this->Attrs());
    op->SetInput("X", this->Input("X"));
    op->SetInput(framework::GradVarName("Out"), this->OutputGrad("Out"));
F
furnace 已提交
90
#ifndef PADDLE_WITH_ASCEND_CL
H
hong 已提交
91
    op->SetInput("Norm", this->Output("Norm"));
F
furnace 已提交
92 93 94
#else
    op->SetInput("Out", this->Output("Out"));
#endif
H
hong 已提交
95
    op->SetOutput(framework::GradVarName("X"), this->InputGrad("X"));
96 97 98
  }
};

99 100 101 102
}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
L
Leo Chen 已提交
103
using CPU = phi::CPUContext;
104

105 106
DECLARE_INFER_SHAPE_FUNCTOR(norm,
                            NormInferShapeFunctor,
H
hong 已提交
107 108
                            PD_INFER_META(phi::NormInferMeta));

109 110 111
REGISTER_OPERATOR(norm,
                  ops::NormOp,
                  ops::NormOpMaker,
H
hong 已提交
112
                  ops::NormOpGradOpMaker<paddle::framework::OpDesc>,
H
hong 已提交
113 114
                  ops::NormOpGradOpMaker<paddle::imperative::OpBase>,
                  NormInferShapeFunctor);
115
REGISTER_OPERATOR(norm_grad, ops::NormOpGrad);