masked_select_op_mlu.cc 7.5 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 MaskedSelectedMLUKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
25 26 27
    auto input = ctx.Input<phi::DenseTensor>("X");
    auto mask = ctx.Input<phi::DenseTensor>("Mask");
    auto out = ctx.Output<phi::DenseTensor>("Y");
28 29 30 31 32 33 34 35 36 37 38 39 40 41

    auto input_dim = input->dims();
    auto mask_dim = mask->dims();
    PADDLE_ENFORCE_EQ(
        input_dim,
        mask_dim,
        platform::errors::InvalidArgument(
            "The dim size of input and mask in OP(masked_selected) "
            "must be equal, but got input dim:(%ld), mask dim: "
            "(%ld). Please check input "
            "value.",
            input_dim,
            mask_dim));

42
    phi::DenseTensor number(framework::TransToPhiDataType(VT::INT32));
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
    void* number_ptr = number.mutable_data<int32_t>({1}, ctx.GetPlace());

    out->Resize(mask->dims());
    out->mutable_data<T>(ctx.GetPlace());

    MLUCnnlTensorDesc input_desc(*input);
    MLUCnnlTensorDesc mask_desc(*mask);
    MLUCnnlTensorDesc out_desc(*out);
    MLUCnnl::Mask(ctx,
                  CNNL_MASKED_SELECT,
                  input_desc.get(),
                  GetBasePtr(input),
                  mask_desc.get(),
                  GetBasePtr(mask),
                  nullptr,
                  nullptr,
                  out_desc.get(),
                  GetBasePtr(out),
                  static_cast<uint32_t*>(number_ptr));
  }
};

template <typename T>
class MaskedSelectedGradMLUKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
69 70 71
    auto mask = ctx.Input<phi::DenseTensor>("Mask");
    auto y_grad = ctx.Input<phi::DenseTensor>(framework::GradVarName("Y"));
    auto x_grad = ctx.Output<phi::DenseTensor>(framework::GradVarName("X"));
72 73 74

    auto& dev_ctx =
        ctx.template device_context<paddle::platform::MLUDeviceContext>();
75
    phi::DenseTensor mask_int32, out_size;
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
    std::vector<int32_t> out_size_vec;
    mask_int32.mutable_data<int32_t>(mask->dims(), ctx.GetPlace());
    out_size.mutable_data<int32_t>({1}, ctx.GetPlace());

    MLUCnnlTensorDesc mask_desc(*mask);
    MLUCnnlTensorDesc mask_int32_desc(mask_int32);
    MLUCnnlTensorDesc out_size_desc(out_size);
    auto cast_type = GetCastDataType(mask->dtype(), DataType::INT32);
    MLUCnnl::Cast(ctx,
                  cast_type,
                  mask_desc.get(),
                  GetBasePtr(mask),
                  mask_int32_desc.get(),
                  GetBasePtr(&mask_int32));

    auto mask_int32_dim = phi::vectorize(mask_int32.dims());
    std::vector<int32_t> reduce_dims;
    for (size_t i = 0; i < mask_int32_dim.size(); i++) {
      reduce_dims.push_back(static_cast<int>(i));
    }

    std::string reduce_name = "reduce_sum";
    cnnlReduceOp_t reduce_op = GetMLUCnnlReduceOp(reduce_name);
    MLUCnnlReduceDesc reduce_desc(reduce_dims,
                                  reduce_op,
                                  ToCnnlDataType<int32_t>(),
                                  CNNL_NOT_PROPAGATE_NAN,
                                  CNNL_REDUCE_NO_INDICES,
                                  CNNL_32BIT_INDICES);

    MLUCnnl::Reduce(ctx,
                    true,
                    reduce_desc.get(),
                    nullptr,
                    mask_int32_desc.get(),
                    GetBasePtr(&mask_int32),
                    0,
                    nullptr,
                    nullptr,
                    out_size_desc.get(),
                    GetBasePtr(&out_size));

    paddle::framework::TensorToVector(out_size, dev_ctx, &out_size_vec);
    dev_ctx.Wait();

121
    phi::DenseTensor mask_int32_tmp;
122 123
    mask_int32_tmp.ShareDataWith(mask_int32);
    mask_int32_tmp.Resize({mask_int32.numel()});
124
    phi::DenseTensor topk_v2_out(framework::TransToPhiDataType(VT::INT32)),
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
        indices_int32(framework::TransToPhiDataType(VT::INT32));
    topk_v2_out.mutable_data<int32_t>({mask_int32.numel()}, ctx.GetPlace());
    indices_int32.mutable_data<int32_t>({mask_int32.numel()}, ctx.GetPlace());

    MLUCnnlTensorDesc topk_v2_out_desc(topk_v2_out);
    MLUCnnlTensorDesc indices_int32_desc(indices_int32);
    MLUCnnlTensorDesc mask_int32_tmp_desc(mask_int32_tmp);

    const int dim = 0;
    MLUCnnl::TopK(ctx,
                  mask_int32.numel(),
                  dim,
                  true,
                  false,
                  mask_int32_tmp_desc.get(),
                  GetBasePtr(&mask_int32_tmp),
                  topk_v2_out_desc.get(),
                  GetBasePtr(&topk_v2_out),
                  indices_int32_desc.get(),
                  GetBasePtr(&indices_int32));

    auto stream = ctx.template device_context<MLUDeviceContext>().stream();

148
    phi::DenseTensor indices_int32_out;
149 150 151 152 153 154 155 156
    indices_int32_out.mutable_data<int32_t>({out_size_vec[0]}, ctx.GetPlace());
    memory::Copy(ctx.GetPlace(),
                 GetBasePtr(&indices_int32_out),
                 ctx.GetPlace(),
                 GetBasePtr(&indices_int32),
                 out_size_vec[0] * sizeof(int32_t),
                 stream);

157
    phi::DenseTensor y_grad_tmp_out;
158 159 160 161 162 163 164 165 166
    y_grad_tmp_out.mutable_data<T>({out_size_vec[0]}, ctx.GetPlace());
    MLUCnnlTensorDesc y_grad_tmp_out_desc(y_grad_tmp_out);
    memory::Copy(ctx.GetPlace(),
                 GetBasePtr(&y_grad_tmp_out),
                 ctx.GetPlace(),
                 GetBasePtr(y_grad),
                 out_size_vec[0] * sizeof(T),
                 stream);

167
    phi::DenseTensor indices_int32_tmp;
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 201 202 203 204
    indices_int32_tmp.ShareDataWith(indices_int32_out);
    indices_int32_tmp.Resize({out_size_vec[0], 1});
    MLUCnnlTensorDesc indices_int32_tmp_desc(indices_int32_tmp);

    const cnnlScatterNdMode_t mode = CNNL_SCATTERND_UPDATE;
    x_grad->Resize({x_grad->numel()});
    x_grad->mutable_data<T>(ctx.GetPlace());
    MLUCnnlTensorDesc x_grad_desc(*x_grad);
    MLUCnnl::ScatterNd(ctx,
                       mode,
                       indices_int32_tmp_desc.get(),
                       GetBasePtr(&indices_int32_tmp),
                       y_grad_tmp_out_desc.get(),
                       GetBasePtr(&y_grad_tmp_out),
                       nullptr,
                       nullptr,
                       x_grad_desc.get(),
                       GetBasePtr(x_grad));
    x_grad->Resize(mask->dims());
  }
};

}  // namespace operators
}  // namespace paddle

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

REGISTER_OP_MLU_KERNEL(masked_select,
                       ops::MaskedSelectedMLUKernel<float>,
                       ops::MaskedSelectedMLUKernel<int>,
                       ops::MaskedSelectedMLUKernel<plat::float16>);

REGISTER_OP_MLU_KERNEL(masked_select_grad,
                       ops::MaskedSelectedGradMLUKernel<float>,
                       ops::MaskedSelectedGradMLUKernel<int>,
                       ops::MaskedSelectedGradMLUKernel<plat::float16>);