p_norm_op.cc 6.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/* Copyright (c) 2020 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.
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. */

#include "paddle/fluid/operators/p_norm_op.h"
#include <memory>
#include <string>
#include <vector>

namespace paddle {
namespace operators {

class PnormOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
  void Make() override {
    AddInput("X", "(Tensor) A tensor of rank >= axis.");
    AddAttr<float>("porder",
28 29 30
                   "(float, default 2) The porder is the p order vector norm "
                   "to calculate. Available for porder=0, inf, -inf and any "
                   "real number.")
31 32
        .SetDefault(2.0f);
    AddAttr<int>("axis",
33
                 "The axis on which to apply norm operation. If axis < 0, "
34 35 36 37
                 "the dimension to pnorm is rank(X) + axis. -1 is "
                 "the last dimension.")
        .SetDefault(-1);
    AddAttr<float>("epsilon",
38
                   "(float, default 1e-12) The epsilon value is used "
39 40 41 42
                   "to avoid division by zero.")
        .SetDefault(1.0e-12f);
    AddAttr<bool>(
        "keepdim",
43
        "(bool, default false) Whether to keep the dimensions as the input.")
44
        .SetDefault(false);
myq406450149's avatar
myq406450149 已提交
45 46 47 48 49

    AddAttr<bool>("asvector",
                  "(bool, default false) as vector norm when axis is None and "
                  "input is matrix, ")
        .SetDefault(false);
50
    AddOutput("Out", "(Tensor) Output result tensor of p-norm");
51
    AddComment(R"DOC(
52 53
Pnorm Operator.
Given a tensor X, compute Lp-norm of X.
54

55 56 57 58 59 60 61 62 63 64 65 66 67 68
When p = 0, defining $0^0 = 0$, the zero-norm of X is simply the number of non-zero elements of X.
$$
||X||_{0} = \lim_{p \rightarrow 0} \sum_i |x_i|^p
$$

When p = inf, the inf-norm of X is the maximum element of X.
$$
||X||_\infty = \max_i |x_i|
$$

When p = -inf, the negative-inf-norm of X is the minimum element of X.
$$
||X||_{-\infty} = \min_i |x_i|
$$
69

70
Otherwise, the p-norm of X follows the formula,
71
$$
72
||X||_{p} = (\sum_i |x_i|^p)^{1/p}
73
$$
74
where, $\sum_i $ is calculated along the `axis` dimension.
75 76 77 78 79 80 81 82 83 84 85

)DOC");
  }
};

class PnormOp : public framework::OperatorWithKernel {
 public:
  using framework::OperatorWithKernel::OperatorWithKernel;
  void InferShape(framework::InferShapeContext* ctx) const override {
    OP_INOUT_CHECK(ctx->HasInput("X"), "Input", "X", "p_norm");
    OP_INOUT_CHECK(ctx->HasOutput("Out"), "Output", "Out", "p_norm");
86 87
    auto x_dim = ctx->GetInputDim("X");
    auto x_rank = x_dim.size();
88 89
    int axis = ctx->Attrs().Get<int>("axis");
    bool keepdim = ctx->Attrs().Get<bool>("keepdim");
90 91 92 93 94 95 96 97 98 99 100 101 102 103

    PADDLE_ENFORCE_GE(axis, -x_rank,
                      platform::errors::InvalidArgument(
                          "Attr(axis) value should be in range [-R, R-1], R is "
                          "the rank of Input(X). But received axis: %d, R: %d. "
                          "Current Input(X)'s shape is=[%s].",
                          axis, x_rank, x_dim));
    PADDLE_ENFORCE_LT(axis, x_rank,
                      platform::errors::InvalidArgument(
                          "Attr(axis) value should be in range [-R, R-1], R is "
                          "the rank of Input(X). But received axis: %d, R: %d. "
                          "Current Input(X)'s shape is=[%s].",
                          axis, x_rank, x_dim));

104
    std::vector<int> reduce_dims;
myq406450149's avatar
myq406450149 已提交
105 106 107
    bool asvector = ctx->Attrs().Get<bool>("asvector");
    if (asvector) {
      reduce_dims.emplace_back(1);
myq406450149's avatar
myq406450149 已提交
108 109 110 111 112 113
      if (keepdim) {
        for (int i = 1; i < x_dim.size(); ++i) {
          reduce_dims.emplace_back(1);
        }
        x_dim = framework::make_ddim(reduce_dims);
      }
myq406450149's avatar
myq406450149 已提交
114 115 116 117 118
    } else {
      if (axis < 0) axis = x_dim.size() + axis;
      for (int i = 0; i < x_dim.size(); ++i) {
        if (i != axis) reduce_dims.emplace_back(x_dim[i]);
      }
119
    }
120 121
    x_dim[axis] = 1;

122
    if (keepdim) {
123
      ctx->SetOutputDim("Out", x_dim);
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
    } else {
      ctx->SetOutputDim("Out", framework::make_ddim(reduce_dims));
    }
  }
};

class PnormOpGrad : public framework::OperatorWithKernel {
 public:
  using framework::OperatorWithKernel::OperatorWithKernel;
  void InferShape(framework::InferShapeContext* ctx) const override {
    OP_INOUT_CHECK(ctx->HasInput("X"), "Input", "X", "p_norm");
    OP_INOUT_CHECK(ctx->HasInput("Out"), "Input", "Out", "p_norm");
    OP_INOUT_CHECK(ctx->HasInput(framework::GradVarName("Out")), "Input",
                   "Out@GRAD", "p_norm");
    OP_INOUT_CHECK(ctx->HasOutput(framework::GradVarName("X")), "Output",
                   "X@GRAD", "p_norm");
    ctx->SetOutputDim(framework::GradVarName("X"), ctx->GetInputDim("X"));
  }
};

template <typename T>
class PnormOpGradOpMaker : public framework::SingleGradOpMaker<T> {
 public:
  using framework::SingleGradOpMaker<T>::SingleGradOpMaker;

 protected:
  void Apply(GradOpPtr<T> op) const override {
    op->SetType("p_norm_grad");
    op->SetAttrMap(this->Attrs());
    op->SetInput("X", this->Input("X"));
    op->SetInput("Out", this->Output("Out"));
    op->SetInput(framework::GradVarName("Out"), this->OutputGrad("Out"));
    op->SetOutput(framework::GradVarName("X"), this->InputGrad("X"));
  }
};

}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
using CPU = paddle::platform::CPUDeviceContext;

REGISTER_OPERATOR(p_norm, ops::PnormOp, ops::PnormOpMaker,
                  ops::PnormOpGradOpMaker<paddle::framework::OpDesc>,
                  ops::PnormOpGradOpMaker<paddle::imperative::OpBase>);
REGISTER_OPERATOR(p_norm_grad, ops::PnormOpGrad);
REGISTER_OP_CPU_KERNEL(p_norm, ops::PnormKernel<CPU, float>,
                       ops::PnormKernel<CPU, double>);
REGISTER_OP_CPU_KERNEL(p_norm_grad, ops::PnormGradKernel<CPU, float>,
                       ops::PnormGradKernel<CPU, double>);