margin_cross_entropy_op.cc 7.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* Copyright (c) 2021 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.
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. */

D
duanboqiang 已提交
15 16
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/framework/op_version_registry.h"
17 18 19 20 21 22 23 24 25

namespace paddle {
namespace operators {

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

  void InferShape(framework::InferShapeContext* ctx) const override {
26 27 28 29
    OP_INOUT_CHECK(
        ctx->HasInput("Logits"), "Input", "Logits", "MarginCrossEntropyOp");
    OP_INOUT_CHECK(
        ctx->HasInput("Label"), "Input", "Label", "MarginCrossEntropyOp");
30

31 32 33 34
    OP_INOUT_CHECK(
        ctx->HasOutput("Softmax"), "Output", "Softmax", "MarginCrossEntropyOp");
    OP_INOUT_CHECK(
        ctx->HasOutput("Loss"), "Output", "Loss", "MarginCrossEntropyOp");
35 36 37 38 39 40 41 42 43

    auto logits_dims = ctx->GetInputDim("Logits");
    auto labels_dims = ctx->GetInputDim("Label");

    auto logits_rank = logits_dims.size();
    auto axis = logits_rank - 1;
    for (int i = 0; i < logits_rank; i++) {
      if (i != axis) {
        if (ctx->IsRuntime() || (logits_dims[i] > 0 && labels_dims[i] > 0)) {
44 45
          PADDLE_ENFORCE_EQ(logits_dims[i],
                            labels_dims[i],
46 47 48 49 50 51 52 53 54
                            platform::errors::InvalidArgument(
                                "Input(Logits) and Input(Label) should in "
                                "same shape in dimensions except axis."));
        }
      }
    }

    if (labels_dims.size() > 1) {
      PADDLE_ENFORCE_EQ(
55 56
          labels_dims[logits_rank - 1],
          1UL,
57 58 59 60
          platform::errors::InvalidArgument(
              "the last dimension of Input(Label) should be 1."
              "But received: the last dimension of Input(Label) is [%d],"
              "the last dimension is [%d]",
61 62
              labels_dims[logits_rank - 1],
              logits_rank - 1));
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
    }

    ctx->SetOutputDim("Softmax", logits_dims);

    logits_dims[axis] = 1;
    ctx->SetOutputDim("Loss", logits_dims);

    ctx->ShareLoD("Logits", /*->*/ "Softmax");
    ctx->ShareLoD("Logits", /*->*/ "Loss");
  }

 protected:
  framework::OpKernelType GetExpectedKernelType(
      const framework::ExecutionContext& ctx) const override {
    return framework::OpKernelType(
        OperatorWithKernel::IndicateVarDataType(ctx, "Logits"),
        ctx.device_context());
  }
};

class MarginCrossEntropyOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
  void Make() {
    AddInput("Logits",
             "(Tensor, default: Tensor<float>), The input tensor of unscaled "
             "log probabilities, whose dimension :attr:`axis` should be scaled "
             "by softmax.");
    AddInput(
        "Label",
        "(Tensor) The input tensor of groud truth label. Label is a "
        "Tensor<int64> in same shape with Input(Logits) except the shape in "
        "dimension :attr:`axis` as 1.");
    AddOutput(
        "Softmax",
        "(Tensor, default: Tensor<float>), A tensor in same shape with "
        "Input(Logits). "
        "The outputs value of softmax activation by given the input batch, "
        "which will be used in backward calculation.");
    AddOutput("Loss",
              "(Tensor, default: Tensor<float>), A tensor in same shape with "
              "Input(Logits) "
              "except the shape in dimension :attr:`axis` as 1. The cross "
              "entropy loss.");
    AddAttr<bool>("return_softmax",
                  "(bool default false) A flag to indicate "
                  "whether to return softmax.")
        .SetDefault(false);
    AddAttr<int>("ring_id", "(int default 0) nccl communication ring id.")
        .SetDefault(0);
    AddAttr<int>("rank", "(int default 0) rank id for MarginCrossEntropy.")
        .SetDefault(0);
    AddAttr<int>("nranks", "(int default 1) nranks id for MarginCrossEntropy.")
        .SetDefault(1);
    AddAttr<float>("margin1", "(float default 1.0) margin1 for MarginLoss.")
        .SetDefault(1.0);
    AddAttr<float>("margin2", "(float default 0.5) margin2 for MarginLoss.")
        .SetDefault(0.5);
    AddAttr<float>("margin3", "(float default 0.0) margin3 for MarginLoss.")
        .SetDefault(0.0);
    AddAttr<float>("scale", "(float default 64.0) scale for MarginLoss.")
        .SetDefault(64.0);
    AddComment(R"DOC(
MarginCrossEntropy Operator
.. math::

    L=-\frac{1}{N}\sum^N_{i=1}\log\frac{e^{s(cos(m_{1}\theta_{y_i}+m_{2})-m_{3})}}{e^{s(cos(m_{1}\theta_{y_i}+m_{2})-m_{3})}+\sum^n_{j=1,j\neq y_i} e^{scos\theta_{y_i}}}

where the :math: `\theta_{y_i}` is the angle between the feature :math: `x` and
the representation of class :math: `i`. The details of ArcFace loss
could be referred to https://arxiv.org/abs/1801.07698.

Note that the Op supports model parallel and single GPU. And Logits.shape[-1] can be different each rank.

)DOC");
  }
};

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

  void InferShape(framework::InferShapeContext* ctx) const override {
145 146
    PADDLE_ENFORCE_EQ(ctx->HasInput(framework::GradVarName("Loss")),
                      true,
147 148
                      platform::errors::InvalidArgument(
                          "Input(Loss@Grad) should not be null."));
149 150
    PADDLE_ENFORCE_EQ(ctx->HasInput("Softmax"),
                      true,
151 152 153
                      platform::errors::InvalidArgument(
                          "Input(Softmax) should be not null."));
    PADDLE_ENFORCE_EQ(
154 155
        ctx->HasInput("Label"),
        true,
156 157
        platform::errors::InvalidArgument("Input(Label) should be not null."));

158 159
    PADDLE_ENFORCE_EQ(ctx->HasOutput(framework::GradVarName("Logits")),
                      true,
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
                      platform::errors::InvalidArgument(
                          "Output(Logits@Grad) should be not null."));

    ctx->SetOutputDim(framework::GradVarName("Logits"),
                      ctx->GetInputDim("Softmax"));
  }

 protected:
  framework::OpKernelType GetExpectedKernelType(
      const framework::ExecutionContext& ctx) const override {
    return framework::OpKernelType(OperatorWithKernel::IndicateVarDataType(
                                       ctx, framework::GradVarName("Loss")),
                                   ctx.device_context());
  }
};

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

 protected:
  void Apply(GradOpPtr<T> op) const override {
    op->SetType("margin_cross_entropy_grad");

    op->SetInput("Softmax", this->Output("Softmax"));
    op->SetInput("Logits", this->Input("Logits"));
    op->SetInput("Label", this->Input("Label"));
    op->SetInput(framework::GradVarName("Loss"), this->OutputGrad("Loss"));
    op->SetAttrMap(this->Attrs());
    op->SetOutput(framework::GradVarName("Logits"), this->InputGrad("Logits"));
  }
};

}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
namespace plat = paddle::platform;

REGISTER_OPERATOR(
201 202
    margin_cross_entropy,
    ops::MarginCrossEntropyOp,
203 204 205 206 207
    ops::MarginCrossEntropyOpMaker,
    ops::MarginCrossEntropyOpGradMaker<paddle::framework::OpDesc>,
    ops::MarginCrossEntropyOpGradMaker<paddle::imperative::OpBase>);

REGISTER_OPERATOR(margin_cross_entropy_grad, ops::MarginCrossEntropyOpGrad);