squared_l2_norm_op.cc 2.8 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
2

L
Luo Tao 已提交
3 4 5
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
6

L
Luo Tao 已提交
7
    http://www.apache.org/licenses/LICENSE-2.0
8

L
Luo Tao 已提交
9 10 11 12 13
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. */
14

15 16 17 18
#include "paddle/fluid/framework/infershape_utils.h"
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/phi/core/infermeta_utils.h"
#include "paddle/phi/infermeta/unary.h"
H
Huihuang Zheng 已提交
19

20 21 22 23 24 25 26 27 28 29
namespace paddle {
namespace operators {

using framework::Tensor;

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

H
hong 已提交
30 31
template <typename T>
class SquaredL2NormGradOpMaker : public framework::SingleGradOpMaker<T> {
H
Huihuang Zheng 已提交
32
 public:
H
hong 已提交
33
  using framework::SingleGradOpMaker<T>::SingleGradOpMaker;
H
Huihuang Zheng 已提交
34 35

 protected:
36
  void Apply(GradOpPtr<T> op) const override {
H
Huihuang Zheng 已提交
37 38
    op->SetType("squared_l2_norm_grad");

H
hong 已提交
39 40
    op->SetInput(framework::GradVarName("Out"), this->OutputGrad("Out"));
    op->SetInput("X", this->Input("X"));
H
Huihuang Zheng 已提交
41

H
hong 已提交
42
    op->SetOutput(framework::GradVarName("X"), this->InputGrad("X"));
H
Huihuang Zheng 已提交
43

H
hong 已提交
44
    op->SetAttrMap(this->Attrs());
H
Huihuang Zheng 已提交
45 46 47
  }
};

48 49 50 51 52 53 54
class SquaredL2NormGradOp : public framework::OperatorWithKernel {
 public:
  using framework::OperatorWithKernel::OperatorWithKernel;
};

class SquaredL2NormOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
Y
Yu Yang 已提交
55
  void Make() override {
56
    AddInput("X", "(Tensor) The input of squared_l2_norm op.");
57
    AddOutput("Out", "(Scalar) The output of squared_l2_norm op.");
58 59 60 61 62
    AddComment(R"DOC(
SquaredL2Norm Operator.

Computes the squared L2 norm of a tensor.

63
$$Out = \sum_{i} X_{i}^2$$
64 65 66 67 68 69 70 71 72

)DOC");
  }
};

}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
73 74 75 76 77 78 79 80 81

DECLARE_INFER_SHAPE_FUNCTOR(squared_l2_norm,
                            SquaredL2NormInferShapeFunctor,
                            PD_INFER_META(phi::SquaredL2NormInferMeta));

DECLARE_INFER_SHAPE_FUNCTOR(squared_l2_norm_grad,
                            SquaredL2NormGradInferShapeFunctor,
                            PD_INFER_META(phi::UnchangedInferMeta));

82 83
REGISTER_OPERATOR(squared_l2_norm,
                  ops::SquaredL2NormOp,
H
hong 已提交
84 85
                  ops::SquaredL2NormOpMaker,
                  ops::SquaredL2NormGradOpMaker<paddle::framework::OpDesc>,
86 87 88 89 90 91
                  ops::SquaredL2NormGradOpMaker<paddle::imperative::OpBase>,
                  SquaredL2NormInferShapeFunctor);

REGISTER_OPERATOR(squared_l2_norm_grad,
                  ops::SquaredL2NormGradOp,
                  SquaredL2NormGradInferShapeFunctor);