strided_slice_op_mlu.cc 14.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* 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. */

Z
zyfncg 已提交
15
#include "paddle/fluid/framework/op_registry.h"
16
#include "paddle/fluid/operators/mlu/mlu_baseop.h"
Z
zyfncg 已提交
17
#include "paddle/fluid/operators/utils.h"
18 19 20 21 22
#include "paddle/phi/kernels/funcs/strided_slice.h"

namespace paddle {
namespace operators {

Z
zyfncg 已提交
23 24 25 26 27
using Tensor = framework::Tensor;
using Variable = framework::Variable;
using LoDTensorArray = framework::LoDTensorArray;
using DDim = framework::DDim;

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
static void ProcessStridedSliceParams(
    const std::vector<int>& axes,
    const DDim& input_dims,
    const std::vector<int64_t>& starts,
    const std::vector<int64_t>& ends,
    const std::vector<int64_t>& strides,
    const std::vector<int>& infer_flags,
    const std::vector<int>& decrease_axis,
    std::vector<int>* starts_indices_vector,
    std::vector<int>* ends_indices_vector,
    std::vector<int>* strides_indices_vector) {
  for (size_t axis = 0; axis < axes.size(); axis++) {
    int64_t start = starts[axis];
    int64_t end = ends[axis];
    int64_t stride = strides[axis];

    int axis_index = axes[axis];
    int64_t dim_size = input_dims[axis_index];

    bool decrease_axis_affect = false;
    if (start == -1 && end == 0 && infer_flags[axis] == -1) {
      auto ret =
          std::find(decrease_axis.begin(), decrease_axis.end(), axis_index);
      if (ret != decrease_axis.end()) {
        decrease_axis_affect = true;
      }
    }

    if (stride < 0) {
      if (start < 0) {
        start = std::max(start, -dim_size);
      } else {
        start = std::min(start, dim_size - 1) - dim_size;
      }
      if (end < 0) {
        end = std::max(end, -dim_size - 1);
      } else {
        end = end - dim_size;
      }
    } else {
      if (start < 0) {
        start = std::max(start, -dim_size) + dim_size;
      } else {
        start = std::min(start, dim_size - 1);
      }
      if (end < 0) {
        end = end + dim_size;
      } else {
        end = std::min(end, dim_size);
      }
    }

    if (decrease_axis_affect) {
      if (stride < 0) {
        end = start - 1;
      } else {
        end = start + 1;
      }
    }

    (*starts_indices_vector)[axis_index] = static_cast<int>(start);
    (*ends_indices_vector)[axis_index] = static_cast<int>(end);
    (*strides_indices_vector)[axis_index] = static_cast<int>(stride);
  }
}

template <typename T>
class StridedSliceMLUKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
    const Variable* input_var = ctx.InputVar("Input");
    bool is_tensor_array = input_var->IsType<LoDTensorArray>();
    PADDLE_ENFORCE_EQ(is_tensor_array,
                      false,
                      platform::errors::InvalidArgument(
                          "Tensor array as input is not supported."));
104
    int rank = ctx.Input<phi::DenseTensor>("Input")->dims().size();
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
    switch (rank) {
      case 1:
        StridedSliceCompute<1>(ctx);
        break;
      case 2:
        StridedSliceCompute<2>(ctx);
        break;
      case 3:
        StridedSliceCompute<3>(ctx);
        break;
      case 4:
        StridedSliceCompute<4>(ctx);
        break;
      case 5:
        StridedSliceCompute<5>(ctx);
        break;
      case 6:
        StridedSliceCompute<6>(ctx);
        break;
      case 7:
        StridedSliceCompute<7>(ctx);
        break;
      case 8:
        StridedSliceCompute<8>(ctx);
        break;
      default:
        PADDLE_THROW(platform::errors::InvalidArgument(
            "The rank of input is supported up to 8."));
        break;
    }
  }

 private:
  template <size_t D>
  void StridedSliceCompute(const framework::ExecutionContext& ctx) const {
    auto place = ctx.GetPlace();

142 143
    auto in = ctx.Input<phi::DenseTensor>("Input");
    auto out = ctx.Output<phi::DenseTensor>("Out");
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
    auto in_dims = in->dims();

    // list<int>
    auto starts_int = ctx.Attr<std::vector<int>>("starts");
    auto ends_int = ctx.Attr<std::vector<int>>("ends");
    auto strides_int = ctx.Attr<std::vector<int>>("strides");

    std::vector<int64_t> starts(starts_int.begin(), starts_int.end());
    std::vector<int64_t> ends(ends_int.begin(), ends_int.end());
    std::vector<int64_t> strides(strides_int.begin(), strides_int.end());

    auto axes = ctx.Attr<std::vector<int>>("axes");
    auto infer_flags = ctx.Attr<std::vector<int>>("infer_flags");
    auto decrease_axis = ctx.Attr<std::vector<int>>("decrease_axis");

    // vector<Tensor<int32>>
    auto list_new_starts_tensor =
161
        ctx.MultiInput<phi::DenseTensor>("StartsTensorList");
162
    auto list_new_ends_tensor =
163
        ctx.MultiInput<phi::DenseTensor>("EndsTensorList");
164
    auto list_new_strides_tensor =
165
        ctx.MultiInput<phi::DenseTensor>("StridesTensorList");
166 167 168 169 170

    // Tensor<int32>
    if (list_new_starts_tensor.size() > 0) {
      starts = GetDataFromTensorList<int64_t>(list_new_starts_tensor);
    } else if (ctx.HasInput("StartsTensor")) {
171
      auto* starts_tensor = ctx.Input<phi::DenseTensor>("StartsTensor");
172 173 174 175 176 177
      starts = GetDataFromTensor<int64_t>(starts_tensor);
    }

    if (list_new_ends_tensor.size() > 0) {
      ends = GetDataFromTensorList<int64_t>(list_new_ends_tensor);
    } else if (ctx.HasInput("EndsTensor")) {
178
      auto* ends_tensor = ctx.Input<phi::DenseTensor>("EndsTensor");
179 180 181 182 183 184
      ends = GetDataFromTensor<int64_t>(ends_tensor);
    }

    if (list_new_strides_tensor.size() > 0) {
      strides = GetDataFromTensorList<int64_t>(list_new_strides_tensor);
    } else if (ctx.HasInput("StridesTensor")) {
185
      auto* strides_tensor = ctx.Input<phi::DenseTensor>("StridesTensor");
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 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 269 270 271
      strides = GetDataFromTensor<int64_t>(strides_tensor);
    }

    // out dims calculation
    std::vector<int64_t> out_dims_vector(in_dims.size(), -1);
    phi::funcs::StridedSliceOutDims(starts,
                                    ends,
                                    strides,
                                    axes,
                                    infer_flags,
                                    in_dims,
                                    decrease_axis,
                                    out_dims_vector.data(),
                                    axes.size(),
                                    false);
    framework::DDim out_dims(phi::make_ddim(out_dims_vector));

    // construct the starts_indices, ends_indices and strides_indices tensor for
    // calling StridedSlice op
    std::vector<int> starts_indices_vector(D, 0);
    std::vector<int> ends_indices_vector(out_dims_vector.begin(),
                                         out_dims_vector.end());
    std::vector<int> strides_indices_vector(D, 1);

    ProcessStridedSliceParams(axes,
                              in_dims,
                              starts,
                              ends,
                              strides,
                              infer_flags,
                              decrease_axis,
                              &starts_indices_vector,
                              &ends_indices_vector,
                              &strides_indices_vector);

    auto out_dims_origin = out_dims;
    if (decrease_axis.size() > 0) {
      std::vector<int64_t> new_out_shape;
      for (size_t i = 0; i < decrease_axis.size(); ++i) {
        PADDLE_ENFORCE_EQ(
            out_dims[decrease_axis[i]],
            1,
            platform::errors::InvalidArgument(
                "the size of decrease dimension should be 1, but received %d.",
                out_dims[decrease_axis[i]]));
        out_dims_origin[decrease_axis[i]] = 0;
      }

      for (int i = 0; i < out_dims_origin.size(); ++i) {
        if (out_dims_origin[i] != 0) {
          new_out_shape.push_back(out_dims_origin[i]);
        }
      }
      if (new_out_shape.size() == 0) {
        new_out_shape.push_back(1);
      }
      out_dims_origin = phi::make_ddim(new_out_shape);
    }

    out->Resize(out_dims_origin);
    out->mutable_data<T>(place);

    MLUCnnlTensorDesc in_desc(*in);
    MLUCnnlTensorDesc out_desc(
        out_dims_vector.size(), out_dims_vector.data(), ToCnnlDataType<T>());
    MLUCnnl::StridedSlice(ctx,
                          starts_indices_vector.data(),
                          ends_indices_vector.data(),
                          strides_indices_vector.data(),
                          in_desc.get(),
                          GetBasePtr(in),
                          out_desc.get(),
                          GetBasePtr(out));
  }
};

template <typename T>
class StridedSliceGradMLUKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
    const Variable* input_var = ctx.InputVar("Input");
    bool is_tensor_array = input_var->IsType<LoDTensorArray>();
    PADDLE_ENFORCE_EQ(is_tensor_array,
                      false,
                      platform::errors::InvalidArgument(
                          "Tensor array as input is not supported."));
272
    int rank = ctx.Input<phi::DenseTensor>("Input")->dims().size();
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310

    switch (rank) {
      case 1:
        StridedSliceGradCompute<1>(ctx);
        break;
      case 2:
        StridedSliceGradCompute<2>(ctx);
        break;
      case 3:
        StridedSliceGradCompute<3>(ctx);
        break;
      case 4:
        StridedSliceGradCompute<4>(ctx);
        break;
      case 5:
        StridedSliceGradCompute<5>(ctx);
        break;
      case 6:
        StridedSliceGradCompute<6>(ctx);
        break;
      case 7:
        StridedSliceGradCompute<7>(ctx);
        break;
      case 8:
        StridedSliceGradCompute<8>(ctx);
        break;
      default:
        PADDLE_THROW(platform::errors::InvalidArgument(
            "The rank of input is supported up to 8."));
        break;
    }
  }

 private:
  template <size_t D>
  void StridedSliceGradCompute(const framework::ExecutionContext& ctx) const {
    auto place = ctx.GetPlace();

311
    auto* input = ctx.Input<phi::DenseTensor>("Input");
312
    auto input_dims = input->dims();
313 314
    auto* dout = ctx.Input<phi::DenseTensor>(framework::GradVarName("Out"));
    auto* dx = ctx.Output<phi::DenseTensor>(framework::GradVarName("Input"));
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
    dx->mutable_data<T>(input_dims, place);

    auto starts_int = ctx.Attr<std::vector<int>>("starts");
    auto ends_int = ctx.Attr<std::vector<int>>("ends");
    auto strides_int = ctx.Attr<std::vector<int>>("strides");

    std::vector<int64_t> starts(starts_int.begin(), starts_int.end());
    std::vector<int64_t> ends(ends_int.begin(), ends_int.end());
    std::vector<int64_t> strides(strides_int.begin(), strides_int.end());

    auto axes = ctx.Attr<std::vector<int>>("axes");
    auto infer_flags = ctx.Attr<std::vector<int>>("infer_flags");
    auto decrease_axis = ctx.Attr<std::vector<int>>("decrease_axis");

    auto list_new_ends_tensor =
330
        ctx.MultiInput<phi::DenseTensor>("EndsTensorList");
331
    auto list_new_starts_tensor =
332
        ctx.MultiInput<phi::DenseTensor>("StartsTensorList");
333
    auto list_new_strides_tensor =
334
        ctx.MultiInput<phi::DenseTensor>("StridesTensorList");
335 336 337 338

    if (list_new_starts_tensor.size() > 0) {
      starts = GetDataFromTensorList<int64_t>(list_new_starts_tensor);
    } else if (ctx.HasInput("StartsTensor")) {
339
      auto* starts_tensor = ctx.Input<phi::DenseTensor>("StartsTensor");
340 341 342 343 344 345
      starts = GetDataFromTensor<int64_t>(starts_tensor);
    }

    if (list_new_ends_tensor.size() > 0) {
      ends = GetDataFromTensorList<int64_t>(list_new_ends_tensor);
    } else if (ctx.HasInput("EndsTensor")) {
346
      auto* ends_tensor = ctx.Input<phi::DenseTensor>("EndsTensor");
347 348 349 350 351 352
      ends = GetDataFromTensor<int64_t>(ends_tensor);
    }

    if (list_new_strides_tensor.size() > 0) {
      strides = GetDataFromTensorList<int64_t>(list_new_strides_tensor);
    } else if (ctx.HasInput("StridesTensor")) {
353
      auto* strides_tensor = ctx.Input<phi::DenseTensor>("StridesTensor");
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
      strides = GetDataFromTensor<int64_t>(strides_tensor);
    }

    std::vector<int64_t> out_dims_vector(input_dims.size(), -1);
    phi::funcs::StridedSliceOutDims(starts,
                                    ends,
                                    strides,
                                    axes,
                                    infer_flags,
                                    input_dims,
                                    decrease_axis,
                                    out_dims_vector.data(),
                                    axes.size(),
                                    false);

    std::vector<int> starts_indices_vector(D, 0);
    std::vector<int> ends_indices_vector(out_dims_vector.begin(),
                                         out_dims_vector.end());
    std::vector<int> strides_indices_vector(D, 1);

    ProcessStridedSliceParams(axes,
                              input_dims,
                              starts,
                              ends,
                              strides,
                              infer_flags,
                              decrease_axis,
                              &starts_indices_vector,
                              &ends_indices_vector,
                              &strides_indices_vector);

    MLUCnnlTensorDesc dout_desc(
        out_dims_vector.size(), out_dims_vector.data(), ToCnnlDataType<T>());
    MLUCnnlTensorDesc dx_desc(*input);
    MLUCnnl::StridedSliceGrad(ctx,
                              starts_indices_vector.data(),
                              ends_indices_vector.data(),
                              strides_indices_vector.data(),
                              dout_desc.get(),
                              GetBasePtr(dout),
                              dx_desc.get(),
                              GetBasePtr(dx));
  }
};

}  // namespace operators
}  // namespace paddle

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

REGISTER_OP_MLU_KERNEL(strided_slice,
                       ops::StridedSliceMLUKernel<plat::float16>,
                       ops::StridedSliceMLUKernel<bool>,
                       ops::StridedSliceMLUKernel<int>,
                       ops::StridedSliceMLUKernel<int64_t>,
                       ops::StridedSliceMLUKernel<float>);

REGISTER_OP_MLU_KERNEL(strided_slice_grad,
                       ops::StridedSliceGradMLUKernel<plat::float16>,
                       ops::StridedSliceGradMLUKernel<float>,
                       ops::StridedSliceGradMLUKernel<bool>,
                       ops::StridedSliceGradMLUKernel<int>,
                       ops::StridedSliceGradMLUKernel<int64_t>);