huber_loss_op_mlu.cc 7.2 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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 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 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 174 175 176 177 178 179 180 181 182 183 184 185 186 187
/* Copyright (c) 2022 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. */

#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/operators/mlu/mlu_baseop.h"

namespace paddle {
namespace operators {

using Tensor = phi::DenseTensor;

template <typename T>
class HuberLossMLUKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
    auto& dev_ctx = GetDevCtxFromCTX(ctx);
    auto* x = ctx.Input<Tensor>("X");
    auto* y = ctx.Input<Tensor>("Y");
    auto* residual = ctx.Output<Tensor>("Residual");
    auto* out = ctx.Output<Tensor>("Out");
    auto delta = ctx.Attr<float>("delta");

    auto place = ctx.GetPlace();

    // compute y-x
    cnnlDataType_t data_type = ToCnnlDataType<T>();
    residual->mutable_data<T>(x->dims(), place);
    MLUCnnlTensorDesc x_desc(*x);
    MLUCnnlOpTensorDesc sub_op_desc(
        CNNL_OP_TENSOR_SUB, data_type, CNNL_NOT_PROPAGATE_NAN);
    MLUCnnl::OpTensor(ctx,
                      sub_op_desc.get(),
                      x_desc.get(),
                      GetBasePtr(y),
                      x_desc.get(),
                      GetBasePtr(x),
                      x_desc.get(),
                      GetBasePtr(residual),
                      data_type);

    // compute smoothl1loss
    out->mutable_data<T>(x->dims(), place);
    cnnlSmoothL1LossAlgorithm_t smoothl1_algo =
        CNNL_SMOOTHL1LOSS_REDUCTION_NONE;  // defines whether to do reduction
                                           // here
    MLUCnnl::SmoothL1LossForward(ctx,
                                 x_desc.get(),
                                 GetBasePtr(x),
                                 x_desc.get(), /* target has same shape as x */
                                 GetBasePtr(y),
                                 static_cast<float>(delta),
                                 smoothl1_algo,
                                 x_desc.get(), /* out has same shape as x */
                                 GetBasePtr(out));

    // compute multiply by delta
    framework::Tensor scale_tensor, bias_tensor;
    scale_tensor = ctx.AllocateTmpTensor<T, MLUDeviceContext>({1}, dev_ctx);
    bias_tensor = ctx.AllocateTmpTensor<T, MLUDeviceContext>({1}, dev_ctx);
    FillMLUTensorWithHostValue(ctx, static_cast<T>(delta), &scale_tensor);
    FillMLUTensorWithHostValue(ctx, static_cast<T>(0.f), &bias_tensor);
    const int axis = std::max(out->dims().size() - 1, 0);

    MLUCnnlTensorDesc scale_desc(scale_tensor);
    MLUCnnlTensorDesc bias_desc(bias_tensor);
    MLUCnnlTensorDesc out_desc(*out);
    MLUCnnl::Scale(ctx,
                   axis,
                   out_desc.get(),
                   GetBasePtr(out),
                   scale_desc.get(),
                   GetBasePtr(&scale_tensor),
                   bias_desc.get(),
                   GetBasePtr(&bias_tensor),
                   out_desc.get(),
                   GetBasePtr(out));
  }
};

template <typename T>
class HuberLossGradMLUKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
    auto& dev_ctx = GetDevCtxFromCTX(ctx);
    auto* residual = ctx.Input<Tensor>("Residual");
    auto* dout = ctx.Input<Tensor>(framework::GradVarName("Out"));
    auto* dx = ctx.Output<Tensor>(framework::GradVarName("X"));
    auto* dy = ctx.Output<Tensor>(framework::GradVarName("Y"));
    auto delta = ctx.Attr<float>("delta");

    auto place = ctx.GetPlace();

    Tensor t_grad_rd;
    t_grad_rd =
        ctx.AllocateTmpTensor<T, MLUDeviceContext>(residual->dims(), dev_ctx);
    MLUCnnlTensorDesc t_grad_rd_desc(t_grad_rd);
    if (dx || dy) {
      Tensor t_zero;
      t_zero =
          ctx.AllocateTmpTensor<T, MLUDeviceContext>(residual->dims(), dev_ctx);
      FillMLUTensorWithHostValue(ctx, static_cast<T>(0.f), &t_zero);

      MLUCnnlTensorDesc residual_desc(*residual);
      MLUCnnlTensorDesc dout_desc(*dout);

      cnnlSmoothL1LossAlgorithm_t smoothl1_algo =
          CNNL_SMOOTHL1LOSS_REDUCTION_NONE;  // defines whether to do reduction
                                             // here
      MLUCnnl::SmoothL1LossBackward(ctx,
                                    residual_desc.get(),
                                    GetBasePtr(residual),
                                    residual_desc.get(),
                                    GetBasePtr(&t_zero),
                                    dout_desc.get(),
                                    GetBasePtr(dout),
                                    static_cast<float>(delta),
                                    smoothl1_algo,
                                    t_grad_rd_desc.get(),
                                    GetBasePtr(&t_grad_rd));
    }
    // compute multiply by delta
    framework::Tensor scale_tensor, bias_tensor;
    scale_tensor = ctx.AllocateTmpTensor<T, MLUDeviceContext>({1}, dev_ctx);
    bias_tensor = ctx.AllocateTmpTensor<T, MLUDeviceContext>({1}, dev_ctx);

    FillMLUTensorWithHostValue(ctx, static_cast<T>(0.f), &bias_tensor);
    const int axis = std::max(t_grad_rd.dims().size() - 1, 0);

    MLUCnnlTensorDesc scale_desc(scale_tensor);
    MLUCnnlTensorDesc bias_desc(bias_tensor);

    if (dx) {
      dx->mutable_data<T>(place);
      FillMLUTensorWithHostValue(ctx, static_cast<T>(-delta), &scale_tensor);
      MLUCnnlTensorDesc out_desc(*dx);
      MLUCnnl::Scale(ctx,
                     axis,
                     t_grad_rd_desc.get(),
                     GetBasePtr(&t_grad_rd),
                     scale_desc.get(),
                     GetBasePtr(&scale_tensor),
                     bias_desc.get(),
                     GetBasePtr(&bias_tensor),
                     out_desc.get(),
                     GetBasePtr(dx));
    }
    if (dy) {
      dy->mutable_data<T>(place);
      FillMLUTensorWithHostValue(ctx, static_cast<T>(delta), &scale_tensor);
      MLUCnnlTensorDesc out_desc(*dy);
      MLUCnnl::Scale(ctx,
                     axis,
                     t_grad_rd_desc.get(),
                     GetBasePtr(&t_grad_rd),
                     scale_desc.get(),
                     GetBasePtr(&scale_tensor),
                     bias_desc.get(),
                     GetBasePtr(&bias_tensor),
                     out_desc.get(),
                     GetBasePtr(dy));
    }
  }
};

}  // namespace operators
}  // namespace paddle

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

REGISTER_OP_MLU_KERNEL(huber_loss,
                       ops::HuberLossMLUKernel<float>,
                       ops::HuberLossMLUKernel<plat::float16>);
REGISTER_OP_MLU_KERNEL(huber_loss_grad,
                       ops::HuberLossGradMLUKernel<float>,
                       ops::HuberLossGradMLUKernel<plat::float16>);