roi_align_op_npu.cc 7.8 KB
Newer Older
S
shiyutang 已提交
1 2 3 4 5 6 7 8 9 10 11
/* 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. */

12
#include "paddle/fluid/framework/op_registry.h"
13
#include "paddle/fluid/platform/device/npu/npu_op_runner.h"
14
#include "paddle/phi/kernels/funcs/math_function.h"
S
shiyutang 已提交
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36

namespace paddle {
namespace operators {
using Tensor = framework::Tensor;

template <typename DeviceContext, typename T>
class ROIAlignNPUKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
    auto* X = ctx.Input<framework::Tensor>("X");              // (B,C,H,W)
    auto* ROIs = ctx.Input<framework::Tensor>("ROIs");        // (N,4)
    auto* ROIsNum = ctx.Input<framework::Tensor>("RoisNum");  // [0 1 1 2 2 2]
    auto* Out = ctx.Output<framework::Tensor>("Out");
    Out->mutable_data<T>(ctx.GetPlace());

    auto spatial_scale = ctx.Attr<float>("spatial_scale");
    auto pooled_height = ctx.Attr<int>("pooled_height");
    auto pooled_width = ctx.Attr<int>("pooled_width");
    auto sample_num = ctx.Attr<int>("sampling_ratio");
    auto aligned = ctx.Attr<bool>("aligned");
    auto roi_end_mode = 0;
    PADDLE_ENFORCE_EQ(
37 38
        aligned,
        false,
S
shiyutang 已提交
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
        platform::errors::InvalidArgument(
            "ROIAlignNPU only support Aligned attribute equaled to False"));

    framework::NPUAttributeMap attr_roi = {{"spatial_scale", spatial_scale},
                                           {"pooled_height", pooled_height},
                                           {"pooled_width", pooled_width},
                                           {"sample_num", sample_num},
                                           {"roi_end_mode", roi_end_mode}};

    auto stream =
        ctx.template device_context<paddle::platform::NPUDeviceContext>()
            .stream();

    // Combine *ROIsNum with ROIs to get new ROIs
    // change roisnum's datatype & resize
    int dtype =
        static_cast<int>(ConvertToNpuDtype(framework::proto::VarType::FP32));
    framework::NPUAttributeMap attr_cast = {{"dst_type", dtype}};
57
    Tensor ROIsNum_fp(ROIs->dtype());
58
    ROIsNum_fp.Resize(phi::make_ddim({ROIs->dims()[0], 1}));
S
shiyutang 已提交
59 60 61 62 63 64 65 66 67 68 69 70
    ROIsNum_fp.mutable_data<T>(ctx.GetPlace());

    const auto& runner_c =
        NpuOpRunner("Cast", {*ROIsNum}, {ROIsNum_fp}, attr_cast);
    runner_c.Run(stream);

    // concate to make (N, 5)
    std::vector<paddle::framework::Tensor> x_list;
    x_list.push_back(ROIsNum_fp);
    x_list.push_back(*ROIs);
    auto axis = 1;
    // output of concate
71
    Tensor ROIs_N5(ROIs->dtype());
72
    ROIs_N5.Resize(phi::make_ddim({ROIs->dims()[0], 5}));
S
shiyutang 已提交
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
    ROIs_N5.mutable_data<T>(ctx.GetPlace());

    // attribute of concate
    auto EleNum = 2;
    framework::NPUAttributeMap attr_concat = {{"N", EleNum},
                                              {"concat_dim", axis}};

    NpuOpRunner runner0;
    runner0.SetType("ConcatD")
        .AddInputs(x_list)
        .AddOutput(ROIs_N5)
        .AddInputNames({"x0", "x1"})
        .AddAttrs(attr_concat);
    runner0.Run(stream);

    const auto& runner =
        NpuOpRunner("ROIAlign", {*X, ROIs_N5}, {*Out}, attr_roi);
    runner.Run(stream);
  }
};

Z
zhulei 已提交
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
template <typename T>
class ROIAlignNPUGradKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
    auto* in = ctx.Input<framework::Tensor>("X");
    auto* rois = ctx.Input<framework::LoDTensor>("ROIs");
    auto* out_grad =
        ctx.Input<framework::Tensor>(framework::GradVarName("Out"));
    auto* in_grad = ctx.Output<framework::Tensor>(framework::GradVarName("X"));

    auto pooled_height = ctx.Attr<int>("pooled_height");
    auto pooled_width = ctx.Attr<int>("pooled_width");
    auto spatial_scale = ctx.Attr<float>("spatial_scale");
    auto sample_num = ctx.Attr<int>("sampling_ratio");
    auto in_dims = in->dims();
    auto aligned = ctx.Attr<bool>("aligned");

    int rois_num = rois->dims()[0];

    auto place = ctx.GetPlace();
    auto stream =
        ctx.template device_context<paddle::platform::NPUDeviceContext>()
            .stream();

    if (!in_grad) {
      return;
    }
    in_grad->mutable_data<T>(place);

    PADDLE_ENFORCE_EQ(
124 125
        aligned,
        false,
Z
zhulei 已提交
126 127 128
        platform::errors::InvalidArgument(
            "ROIAlignGradNPU only support Aligned attribute equaled to False"));
    PADDLE_ENFORCE_EQ(
129 130
        ctx.HasInput("RoisNum"),
        true,
Z
zhulei 已提交
131 132 133
        platform::errors::NotFound("Input(RoisNum) of ROIAlignGradOp "
                                   "is not found while using NPU."));
    PADDLE_ENFORCE_EQ(
134 135
        framework::TransToProtoVarType(rois->dtype()),
        framework::proto::VarType::FP32,
Z
zhulei 已提交
136 137 138 139 140 141 142 143 144 145 146
        platform::errors::InvalidArgument(
            "ROIAlignGradNPU only support ROIs type equaled to FP32."));

    // Cast RoisNum to fp32 tensor
    auto* RoisNum = ctx.Input<framework::Tensor>("RoisNum");
    Tensor ROIs_N5;
    ROIs_N5.mutable_data<float>({rois_num, 5}, place);
    Tensor ROIsNum_fp;
    ROIsNum_fp.mutable_data<T>(RoisNum->dims(), place);  // shape = [rois_num]
    int nputype_fp32 =
        static_cast<int>(ConvertToNpuDtype(framework::proto::VarType::FP32));
147 148
    const auto& runner_cast = NpuOpRunner(
        "Cast", {*RoisNum}, {ROIsNum_fp}, {{"dst_type", nputype_fp32}});
Z
zhulei 已提交
149 150 151 152 153 154 155
    runner_cast.Run(stream);
    ROIsNum_fp.Resize({rois_num, 1});

    // Combine *ROIsNum with ROIs to get new ROIs
    std::vector<paddle::framework::Tensor> x_list;
    x_list.push_back(ROIsNum_fp);
    x_list.push_back(*rois);
156 157
    const auto& runner_concat = NpuOpRunner(
        "ConcatD", {x_list}, {ROIs_N5}, {{"N", 2}, {"concat_dim", 1}});
Z
zhulei 已提交
158 159
    runner_concat.Run(stream);

160 161 162 163
    //  If CANN version code is less than 504, by analysis, in order to match
    //  cpu grad version, rois[:,3:5] should substrate 1 before call ascend grad
    //  function
#if (CANN_VERSION_CODE < 504000)
Z
zhulei 已提交
164 165 166 167 168 169 170 171
    std::vector<float> vec_dlt = {0, 0, 0, -1.0f, -1.0f};
    Tensor tsr_dlt;
    tsr_dlt.mutable_data<float>({5}, place);
    framework::TensorFromVector<float>(vec_dlt, ctx.device_context(), &tsr_dlt);
    ctx.template device_context<paddle::platform::NPUDeviceContext>().Wait();
    const auto& runner_add =
        NpuOpRunner("AddV2", {ROIs_N5, tsr_dlt}, {ROIs_N5}, {});
    runner_add.Run(stream);
172
#endif
Z
zhulei 已提交
173 174 175 176

    //  Call ascend RoiAlignGrad function
    int roi_end_mode = 0;
    const auto& runner_roi_align_grad =
177 178 179
        NpuOpRunner("ROIAlignGrad",
                    {*out_grad, ROIs_N5},
                    {*in_grad},
180
                    {{"xdiff_shape", phi::vectorize<int>(in_dims)},
Z
zhulei 已提交
181 182 183 184 185 186 187 188 189
                     {"pooled_width", pooled_width},
                     {"pooled_height", pooled_height},
                     {"spatial_scale", spatial_scale},
                     {"sample_num", sample_num},
                     {"roi_end_mode", roi_end_mode}});
    runner_roi_align_grad.Run(stream);
  }
};

S
shiyutang 已提交
190 191 192 193 194 195 196 197 198
}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
REGISTER_OP_NPU_KERNEL(
    roi_align,
    ops::ROIAlignNPUKernel<paddle::platform::NPUDeviceContext, float>,
    ops::ROIAlignNPUKernel<paddle::platform::NPUDeviceContext, double>,
    ops::ROIAlignNPUKernel<paddle::platform::NPUDeviceContext, int>);
Z
zhulei 已提交
199

200 201
REGISTER_OP_NPU_KERNEL(roi_align_grad,
                       ops::ROIAlignNPUGradKernel<float>,
Z
zhulei 已提交
202 203
                       ops::ROIAlignNPUGradKernel<double>,
                       ops::ROIAlignNPUGradKernel<int>);