smooth_l1_loss_op.cc 4.8 KB
Newer Older
Y
yangyaming 已提交
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 28 29 30 31 32 33 34 35
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.

   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. */

#include "paddle/operators/smooth_l1_loss_op.h"

namespace paddle {
namespace operators {

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

 protected:
  void InferShape(const framework::InferShapeContext& ctx) const override {
    PADDLE_ENFORCE_NOT_NULL(ctx.InputVar("X"),
                            "Input of SmoothL1LossOp must be initialized.");
    PADDLE_ENFORCE_NOT_NULL(ctx.InputVar("Y"),
                            "Target of SmoothL1LossOp must be initialized.");

    auto* x = ctx.Input<framework::Tensor>("X");
    auto* y = ctx.Input<framework::Tensor>("Y");
    PADDLE_ENFORCE_EQ(x->dims(), y->dims(),
                      "Dimensions of SmoothL1LossOp's input and target "
                      "must be same.");
36
    PADDLE_ENFORCE_GE(x->dims().size(), 2,
Y
yangyaming 已提交
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
                      "Tensor rank of SmoothL1LossOp's input must be "
                      "at least 2.");
    auto* inside_weight = ctx.Input<framework::Tensor>("InsideWeight");
    if (inside_weight) {
      auto* outside_weight = ctx.Input<framework::Tensor>("OutsideWeight");
      PADDLE_ENFORCE_NOT_NULL(outside_weight,
                              "If weights are provided, must specify both "
                              "inside and outside weights.");
      PADDLE_ENFORCE_EQ(inside_weight->dims(), x->dims(),
                        "Dimensions of inside weight must be same with input.");
      PADDLE_ENFORCE_EQ(
          outside_weight->dims(), x->dims(),
          "Dimensions of outside weight must be same with input.");
    }

52 53
    auto* diff = ctx.Output<framework::LoDTensor>("Diff");
    auto* out = ctx.Output<framework::LoDTensor>("Out");
Y
yangyaming 已提交
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
    diff->Resize(x->dims());
    // loss is a two-rank tensor
    out->Resize({x->dims()[0], 1});
  }
};

template <typename AttrType>
class SmoothL1LossOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
  SmoothL1LossOpMaker(framework::OpProto* proto,
                      framework::OpAttrChecker* op_checker)
      : OpProtoAndCheckerMaker(proto, op_checker) {
    AddInput("X", "Input of SmoothL1LossOp.");
    AddInput("Y", "Target of SmoothL1LossOp.");
    AddInput("InsideWeight", "Optional input to scale (X-Y).");
    AddInput("OutsideWeight", "Optinal input to scale smooth l1 loss.");
70
    AddOutput("Diff", "Intermediate variable to cache Win*(X-Y).")
Y
yangyaming 已提交
71 72
        .AsIntermediate();
    AddOutput("Out", "Final smooth l1 loss of inputs.");
Y
yangyaming 已提交
73 74
    AddAttr<AttrType>("sigma", "Hyper parameter, default value is 3.0 .")
        .SetDefault(3.0);
Y
yangyaming 已提交
75 76
    AddComment(R"DOC(
Compute SmoothL1Loss for input and target.
77

Y
yangyaming 已提交
78 79 80
The equation is:
loss = 0.5 * (sigma * (x - y)) ^ 2 if abs(x - y) < 1 / sigma^2
       abs(x - y) - 0.5 / sigma^2  otherwise
81

Y
yangyaming 已提交
82 83 84 85 86 87 88 89 90 91 92 93 94
)DOC");
  }
};

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

 protected:
  void InferShape(const framework::InferShapeContext& ctx) const override {
    auto in_dims = ctx.Input<framework::Tensor>("X")->dims();
    auto out_dims =
        ctx.Input<framework::Tensor>(framework::GradVarName("Out"))->dims();
95 96 97 98
    auto* x_grad =
        ctx.Output<framework::LoDTensor>(framework::GradVarName("X"));
    auto* y_grad =
        ctx.Output<framework::LoDTensor>(framework::GradVarName("Y"));
Y
yangyaming 已提交
99

100
    PADDLE_ENFORCE_GE(out_dims.size(), 2,
Y
yangyaming 已提交
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
                      "Tensor rank of output gradient should be 2.");
    PADDLE_ENFORCE_EQ(out_dims[0], in_dims[0],
                      "First dimension of ouptut gradient must be "
                      "same with input.");
    PADDLE_ENFORCE_EQ(out_dims[1], 1,
                      "Second dimension of output gradient must be 1.");

    if (x_grad) x_grad->Resize(in_dims);
    if (y_grad) y_grad->Resize(in_dims);
  }
};

}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
REGISTER_OP(smooth_l1_loss, ops::SmoothL1LossOp,
Y
yangyaming 已提交
118 119
            ops::SmoothL1LossOpMaker<float>, smooth_l1_loss_grad,
            ops::SmoothL1LossGradOp);
Y
yangyaming 已提交
120 121 122 123 124
REGISTER_OP_CPU_KERNEL(
    smooth_l1_loss, ops::SmoothL1LossKernel<paddle::platform::CPUPlace, float>);
REGISTER_OP_CPU_KERNEL(
    smooth_l1_loss_grad,
    ops::SmoothL1LossGradKernel<paddle::platform::CPUPlace, float>);