deformable_conv_op_mlu.cc 12.9 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
/* 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 {

template <typename T>
class DeformableConvMLUKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
25 26 27 28 29
    auto* input = ctx.Input<phi::DenseTensor>("Input");
    auto* offset = ctx.Input<phi::DenseTensor>("Offset");
    auto* mask = ctx.Input<phi::DenseTensor>("Mask");
    auto* filter = ctx.Input<phi::DenseTensor>("Filter");
    auto* output = ctx.Output<phi::DenseTensor>("Output");
30 31 32 33 34 35 36 37 38 39 40
    output->mutable_data<T>(ctx.GetPlace());

    const int groups = ctx.Attr<int>("groups");
    const int deformable_groups = ctx.Attr<int>("deformable_groups");
    const int im2col_step = ctx.Attr<int>("im2col_step");
    const std::vector<int> strides = ctx.Attr<std::vector<int>>("strides");
    const std::vector<int> paddings = ctx.Attr<std::vector<int>>("paddings");
    const std::vector<int> dilations = ctx.Attr<std::vector<int>>("dilations");

    // TODO(fwg): Remove this check when cnnl fix the bug that groups > 1.
    PADDLE_ENFORCE_EQ(
41 42
        groups == 1,
        true,
43 44 45 46 47
        platform::errors::InvalidArgument(
            "MLU deformable_conv kernel only support groups == 1, but get %d.",
            groups));

    // transform paddings from {h, w} to {top, bottom, left, right}.
48 49 50 51 52 53 54 55 56
    const std::vector<int> trans_paddings{
        paddings[0], paddings[0], paddings[1], paddings[1]};
    MLUCnnlDCNDesc dcn_desc(input->dims().size(),
                            trans_paddings.data(),
                            strides.data(),
                            dilations.data(),
                            deformable_groups,
                            groups,
                            im2col_step);
57 58

    const std::vector<int> perm_to_nhwc = {0, 2, 3, 1};
59
    phi::DenseTensor trans_input(input->dtype());
60 61
    TransposeFromMLUTensor<T>(
        ctx, perm_to_nhwc, input, &trans_input, true /*need_reshape_or_alloc*/);
62

63
    phi::DenseTensor trans_offset(offset->dtype());
64 65 66 67
    TransposeFromMLUTensor<T>(ctx,
                              perm_to_nhwc,
                              offset,
                              &trans_offset,
68 69
                              true /*need_reshape_or_alloc*/);

70
    phi::DenseTensor trans_mask(mask->dtype());
71 72
    TransposeFromMLUTensor<T>(
        ctx, perm_to_nhwc, mask, &trans_mask, true /*need_reshape_or_alloc*/);
73

74
    phi::DenseTensor trans_filter(filter->dtype());
75 76 77 78
    TransposeFromMLUTensor<T>(ctx,
                              perm_to_nhwc,
                              filter,
                              &trans_filter,
79 80
                              true /*need_reshape_or_alloc*/);

81
    phi::DenseTensor tmp_output(output->dtype());
82 83 84 85 86 87
    auto output_dims = output->dims();
    tmp_output.mutable_data<T>(
        {output_dims[0], output_dims[2], output_dims[3], output_dims[1]},
        ctx.GetPlace());

    cnnlTensorLayout_t data_layout = CNNL_LAYOUT_NHWC;
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
    MLUCnnlTensorDesc input_desc(
        trans_input, data_layout, ToCnnlDataType(trans_input.dtype()));
    MLUCnnlTensorDesc offset_desc(
        trans_offset, data_layout, ToCnnlDataType(trans_offset.dtype()));
    MLUCnnlTensorDesc mask_desc(
        trans_mask, data_layout, ToCnnlDataType(trans_mask.dtype()));
    MLUCnnlTensorDesc filter_desc(
        trans_filter, data_layout, ToCnnlDataType(trans_filter.dtype()));
    MLUCnnlTensorDesc output_desc(
        tmp_output, data_layout, ToCnnlDataType(tmp_output.dtype()));
    MLUCnnl::DCNForward(ctx,
                        dcn_desc.get(),
                        input_desc.get(),
                        GetBasePtr(&trans_input),
                        offset_desc.get(),
                        GetBasePtr(&trans_offset),
                        mask_desc.get(),
                        GetBasePtr(&trans_mask),
                        filter_desc.get(),
                        GetBasePtr(&trans_filter),
                        nullptr,
                        nullptr,
                        output_desc.get(),
                        GetBasePtr(&tmp_output));
112 113

    const std::vector<int> perm_to_nchw = {0, 3, 1, 2};
114 115 116 117
    TransposeFromMLUTensor<T>(ctx,
                              perm_to_nchw,
                              &tmp_output,
                              output,
118 119 120 121 122 123 124 125
                              false /*need_reshape_or_alloc*/);
  }
};

template <typename T>
class DeformableConvGradMLUKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
    const phi::DenseTensor* output_grad =
        ctx.Input<phi::DenseTensor>(framework::GradVarName("Output"));
    auto* input_grad =
        ctx.Output<phi::DenseTensor>(framework::GradVarName("Input"));
    auto* filter_grad =
        ctx.Output<phi::DenseTensor>(framework::GradVarName("Filter"));
    auto* offset_grad =
        ctx.Output<phi::DenseTensor>(framework::GradVarName("Offset"));
    auto* mask_grad =
        ctx.Output<phi::DenseTensor>(framework::GradVarName("Mask"));

    const phi::DenseTensor* input = ctx.Input<phi::DenseTensor>("Input");
    auto* offset = ctx.Input<phi::DenseTensor>("Offset");
    auto* mask = ctx.Input<phi::DenseTensor>("Mask");
    auto* filter = ctx.Input<phi::DenseTensor>("Filter");
141 142 143 144 145 146 147 148 149

    int groups = ctx.Attr<int>("groups");
    int deformable_groups = ctx.Attr<int>("deformable_groups");
    int im2col_step = ctx.Attr<int>("im2col_step");
    std::vector<int> strides = ctx.Attr<std::vector<int>>("strides");
    std::vector<int> paddings = ctx.Attr<std::vector<int>>("paddings");
    std::vector<int> dilations = ctx.Attr<std::vector<int>>("dilations");

    // TODO(fwg): Remove this check when cnnl fix the bug that groups > 1.
150 151
    PADDLE_ENFORCE_EQ(groups == 1,
                      true,
152 153 154 155 156 157
                      platform::errors::InvalidArgument(
                          "MLU deformable_conv_grad kernel only support groups "
                          "== 1, but get %d.",
                          groups));

    // transform paddings from {h, w} to {top, bottom, left, right}.
158 159 160 161 162 163 164 165 166
    const std::vector<int> trans_paddings{
        paddings[0], paddings[0], paddings[1], paddings[1]};
    MLUCnnlDCNDesc dcn_desc(input->dims().size(),
                            trans_paddings.data(),
                            strides.data(),
                            dilations.data(),
                            deformable_groups,
                            groups,
                            im2col_step);
167

168
    phi::DenseTensor tmp_input_grad;
169 170 171 172 173
    auto input_dims = input->dims();
    tmp_input_grad.mutable_data<T>(
        {input_dims[0], input_dims[2], input_dims[3], input_dims[1]},
        ctx.GetPlace());

174
    phi::DenseTensor tmp_filter_grad;
175 176 177 178 179
    auto filter_dims = filter->dims();
    tmp_filter_grad.mutable_data<T>(
        {filter_dims[0], filter_dims[2], filter_dims[3], filter_dims[1]},
        ctx.GetPlace());

180
    phi::DenseTensor tmp_offset_grad;
181 182 183 184 185
    auto offset_dims = offset->dims();
    tmp_offset_grad.mutable_data<T>(
        {offset_dims[0], offset_dims[2], offset_dims[3], offset_dims[1]},
        ctx.GetPlace());

186
    phi::DenseTensor tmp_mask_grad;
187 188 189 190 191 192
    auto mask_dims = mask->dims();
    tmp_mask_grad.mutable_data<T>(
        {mask_dims[0], mask_dims[2], mask_dims[3], mask_dims[1]},
        ctx.GetPlace());

    const std::vector<int> perm_to_nhwc = {0, 2, 3, 1};
193
    phi::DenseTensor trans_output_grad(output_grad->dtype());
194 195 196
    TransposeFromMLUTensor<T>(ctx,
                              perm_to_nhwc,
                              output_grad,
197 198 199
                              &trans_output_grad,
                              true /*need_reshape_or_alloc*/);

200
    phi::DenseTensor trans_input(input->dtype());
201 202
    TransposeFromMLUTensor<T>(
        ctx, perm_to_nhwc, input, &trans_input, true /*need_reshape_or_alloc*/);
203

204
    phi::DenseTensor trans_offset(offset->dtype());
205 206 207 208
    TransposeFromMLUTensor<T>(ctx,
                              perm_to_nhwc,
                              offset,
                              &trans_offset,
209 210
                              true /*need_reshape_or_alloc*/);

211
    phi::DenseTensor trans_mask(mask->dtype());
212 213
    TransposeFromMLUTensor<T>(
        ctx, perm_to_nhwc, mask, &trans_mask, true /*need_reshape_or_alloc*/);
214

215
    phi::DenseTensor trans_filter(filter->dtype());
216 217 218 219
    TransposeFromMLUTensor<T>(ctx,
                              perm_to_nhwc,
                              filter,
                              &trans_filter,
220 221 222 223
                              true /*need_reshape_or_alloc*/);

    cnnlTensorLayout_t data_layout = CNNL_LAYOUT_NHWC;
    MLUCnnlTensorDesc output_grad_desc(
224 225
        trans_output_grad,
        data_layout,
226
        ToCnnlDataType(trans_output_grad.dtype()));
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
    MLUCnnlTensorDesc input_desc(
        trans_input, data_layout, ToCnnlDataType(trans_input.dtype()));
    MLUCnnlTensorDesc offset_desc(
        trans_offset, data_layout, ToCnnlDataType(trans_offset.dtype()));
    MLUCnnlTensorDesc mask_desc(
        trans_mask, data_layout, ToCnnlDataType(trans_mask.dtype()));
    MLUCnnlTensorDesc filter_desc(
        trans_filter, data_layout, ToCnnlDataType(trans_filter.dtype()));

    MLUCnnl::DCNBackwardData(ctx,
                             dcn_desc.get(),
                             input_desc.get(),
                             GetBasePtr(&trans_input),
                             offset_desc.get(),
                             GetBasePtr(&trans_offset),
                             mask_desc.get(),
                             GetBasePtr(&trans_mask),
                             filter_desc.get(),
                             GetBasePtr(&trans_filter),
                             output_grad_desc.get(),
                             GetBasePtr(&trans_output_grad),
                             input_desc.get(),
                             GetBasePtr(&tmp_input_grad),
                             offset_desc.get(),
                             GetBasePtr(&tmp_offset_grad),
                             mask_desc.get(),
                             GetBasePtr(&tmp_mask_grad));

    MLUCnnl::DCNBackwardWeight(ctx,
                               dcn_desc.get(),
                               input_desc.get(),
                               GetBasePtr(&trans_input),
                               offset_desc.get(),
                               GetBasePtr(&trans_offset),
                               mask_desc.get(),
                               GetBasePtr(&trans_mask),
                               output_grad_desc.get(),
                               GetBasePtr(&trans_output_grad),
                               filter_desc.get(),
                               GetBasePtr(&tmp_filter_grad),
                               nullptr,
                               nullptr);
269 270 271 272

    const std::vector<int> perm_to_nchw = {0, 3, 1, 2};
    if (input_grad) {
      input_grad->mutable_data<T>(ctx.GetPlace());
273 274 275 276
      TransposeFromMLUTensor<T>(ctx,
                                perm_to_nchw,
                                &tmp_input_grad,
                                input_grad,
277 278 279 280 281
                                false /*need_reshape_or_alloc*/);
    }

    if (filter_grad) {
      filter_grad->mutable_data<T>(ctx.GetPlace());
282 283 284 285 286
      TransposeFromMLUTensor<T>(ctx,
                                perm_to_nchw,
                                &tmp_filter_grad,
                                filter_grad,
                                false /*need_reshape_or_alloc*/);
287 288 289 290
    }

    if (offset_grad) {
      offset_grad->mutable_data<T>(ctx.GetPlace());
291 292 293 294 295
      TransposeFromMLUTensor<T>(ctx,
                                perm_to_nchw,
                                &tmp_offset_grad,
                                offset_grad,
                                false /*need_reshape_or_alloc*/);
296 297 298 299
    }

    if (mask_grad) {
      mask_grad->mutable_data<T>(ctx.GetPlace());
300 301 302 303
      TransposeFromMLUTensor<T>(ctx,
                                perm_to_nchw,
                                &tmp_mask_grad,
                                mask_grad,
304 305 306 307 308 309 310 311 312 313 314 315 316 317
                                false /*need_reshape_or_alloc*/);
    }
  }
};

}  // namespace operators
}  // namespace paddle

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

REGISTER_OP_MLU_KERNEL(deformable_conv, ops::DeformableConvMLUKernel<float>);
REGISTER_OP_MLU_KERNEL(deformable_conv_grad,
                       ops::DeformableConvGradMLUKernel<float>);