slice_op_npu.cc 8.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
/* 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
13
limitations under the License. */
14 15

#include "paddle/fluid/operators/slice_op.h"
16
#include "paddle/fluid/platform/device/npu/npu_op_runner.h"
H
hong 已提交
17
#include "paddle/phi/kernels/funcs/slice_utils.h"
18 19 20 21 22

namespace paddle {
namespace operators {

using Tensor = framework::Tensor;
23
using NPUDeviceContext = platform::NPUDeviceContext;
24

25 26 27 28 29 30
void UpdateAttr(const framework::DDim& in_dims,
                const std::vector<int> axes,
                const std::vector<int> starts,
                const std::vector<int> ends,
                std::vector<int>* offsets,
                std::vector<int>* size) {
31 32 33 34
  int cnt = 0;
  for (int i = 0; i < in_dims.size(); ++i) {
    int start = 0;
    int end = in_dims[i];
35 36 37
    // NOTE(zhiqiu): Becareful that cnt may > axes.size() and result in
    // overflow.
    int axis = cnt < static_cast<int>(axes.size()) ? axes[cnt] : -1;
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
    if (axis == i) {
      start = starts[cnt];
      if (start < 0) {
        start = (start + in_dims[i]);
      }
      start = std::max(start, static_cast<int>(0));
      end = ends[cnt];
      if (end < 0) {
        end = (end + in_dims[i]);
      }
      end = std::min(end, static_cast<int>(in_dims[i]));
      cnt++;
    }

    (*offsets)[i] = start;
    (*size)[i] = end - start;
  }
}

57
template <typename T>
58 59 60 61 62 63
class SliceNPUKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
    auto* input = ctx.Input<Tensor>("Input");
    auto* out = ctx.Output<Tensor>("Out");

64 65 66 67 68 69 70 71 72 73
    auto axes_int = ctx.Attr<std::vector<int>>("axes");
    auto starts_int = ctx.Attr<std::vector<int>>("starts");
    auto ends_int = ctx.Attr<std::vector<int>>("ends");
    std::vector<int> axes(axes_int.begin(), axes_int.end());
    std::vector<int> starts(starts_int.begin(), starts_int.end());
    std::vector<int> ends(ends_int.begin(), ends_int.end());

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

74
    const auto& in_dims = input->dims();
75

76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
    // Get the accurate attribute value of starts and ends
    auto starts_tensor_list = ctx.MultiInput<Tensor>("StartsTensorList");
    if (ctx.HasInput("StartsTensor")) {
      starts = GetDataFromTensor<int>(ctx.Input<Tensor>("StartsTensor"));
    } else if (starts_tensor_list.size() > 0) {
      starts = GetDataFromTensorList<int>(starts_tensor_list);
    }

    auto ends_tensor_list = ctx.MultiInput<Tensor>("EndsTensorList");
    if (ctx.HasInput("EndsTensor")) {
      ends = GetDataFromTensor<int>(ctx.Input<Tensor>("EndsTensor"));
    } else if (ends_tensor_list.size() > 0) {
      ends = GetDataFromTensorList<int>(ends_tensor_list);
    }

    PADDLE_ENFORCE_EQ(
92 93
        starts.size(),
        axes.size(),
94 95 96
        platform::errors::InvalidArgument(
            "The size of starts must be equal to the size of axes."));
    PADDLE_ENFORCE_EQ(
97 98
        ends.size(),
        axes.size(),
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
        platform::errors::InvalidArgument(
            "The size of ends must be equal to the size of axes."));

    if (ctx.HasInput("StartsTensor") || ctx.HasInput("EndsTensor") ||
        starts_tensor_list.size() > 0 || ends_tensor_list.size() > 0) {
      // Infer output dims
      auto out_dims = out->dims();
      auto slice_dims = out_dims;
      for (size_t i = 0; i < axes.size(); ++i) {
        // when start == -1 && end == start+1
        if (starts[i] == -1 && ends[i] == 0 && infer_flags[i] == -1) {
          auto ret =
              std::find(decrease_axis.begin(), decrease_axis.end(), axes[i]);
          if (ret != decrease_axis.end()) {
            ends[i] = in_dims[axes[i]];
          }
        }
      }

H
hong 已提交
118
      phi::funcs::CheckAndUpdateSliceAttrs(in_dims, axes, &starts, &ends);
119 120
      slice_dims = phi::funcs::GetSliceDims<int>(
          in_dims, axes, starts, ends, nullptr, nullptr);
H
hong 已提交
121
      out_dims = phi::funcs::GetDecreasedDims(slice_dims, decrease_axis);
122 123 124 125

      out->Resize(out_dims);
    }

126 127 128 129 130 131 132
    out->mutable_data<T>(ctx.GetPlace());

    std::vector<int> offsets(in_dims.size());
    std::vector<int> size(in_dims.size());

    UpdateAttr(in_dims, axes, starts, ends, &offsets, &size);

133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
    auto& dev_ctx = ctx.template device_context<NPUDeviceContext>();
    auto stream = dev_ctx.stream();
#if CANN_VERSION_CODE < 512000
    const auto& runner =
        NpuOpRunner("SliceD", {*input}, {*out}, {{"offsets", offsets}, {
                                                   "size",
                                                   size
                                                 }});
#else
    NpuOpRunner runner;
    runner.SetType("Slice")
        .AddInput(*input)
        .AddInput(std::move(offsets))
        .AddInput(std::move(size))
        .AddOutput(*out);
#endif
149 150 151 152
    runner.Run(stream);
  }
};

153
template <typename T>
154 155 156 157 158 159 160
class SliceGradNPUKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
    auto* input = ctx.Input<Tensor>("Input");
    auto* dout = ctx.Input<Tensor>(framework::GradVarName("Out"));
    auto* dinput = ctx.Output<Tensor>(framework::GradVarName("Input"));

161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
    auto axes_int = ctx.Attr<std::vector<int>>("axes");
    auto starts_int = ctx.Attr<std::vector<int>>("starts");
    auto ends_int = ctx.Attr<std::vector<int>>("ends");
    std::vector<int> axes(axes_int.begin(), axes_int.end());
    std::vector<int> starts(starts_int.begin(), starts_int.end());
    std::vector<int> ends(ends_int.begin(), ends_int.end());

    // Get the accurate attribute value of starts and ends
    auto starts_tensor_list = ctx.MultiInput<Tensor>("StartsTensorList");
    if (ctx.HasInput("StartsTensor")) {
      starts = GetDataFromTensor<int>(ctx.Input<Tensor>("StartsTensor"));
    } else if (starts_tensor_list.size() > 0) {
      starts = GetDataFromTensorList<int>(starts_tensor_list);
    }

    auto ends_tensor_list = ctx.MultiInput<Tensor>("EndsTensorList");
    if (ctx.HasInput("EndsTensor")) {
      ends = GetDataFromTensor<int>(ctx.Input<Tensor>("EndsTensor"));
    } else if (ends_tensor_list.size() > 0) {
      ends = GetDataFromTensorList<int>(ends_tensor_list);
    }

183
    const auto& in_dims = input->dims();
184 185 186 187 188 189 190 191 192 193 194 195
    int rank = in_dims.size();

    std::vector<int> offsets(rank);
    std::vector<int> size(rank);
    UpdateAttr(in_dims, axes, starts, ends, &offsets, &size);

    std::vector<std::vector<int64_t>> paddings(rank, std::vector<int64_t>(2));
    for (int i = 0; i < rank; ++i) {
      paddings[i][0] = static_cast<int64_t>(offsets[i]);
      paddings[i][1] = static_cast<int64_t>(in_dims[i] - size[i] - offsets[i]);
    }

A
Aganlengzi 已提交
196 197 198 199 200 201 202
    Tensor tmp_dout;
    tmp_dout.ShareDataWith(*dout);
    auto out_dims = dout->dims();
    auto decrease_axis = ctx.Attr<std::vector<int>>("decrease_axis");
    auto decrease_size = decrease_axis.size();
    if (decrease_size > 0) {
      if (decrease_size == static_cast<size_t>(in_dims.size())) {
203
        out_dims = phi::make_ddim(std::vector<int>(decrease_size, 1));
A
Aganlengzi 已提交
204 205 206 207 208 209 210 211 212 213 214 215
      } else {
        std::vector<int> origin_out_shape(out_dims.size() + decrease_size, -1);
        for (size_t i = 0; i < decrease_size; ++i) {
          origin_out_shape[decrease_axis[i]] = 1;
        }
        int index = 0;
        for (size_t i = 0; i < origin_out_shape.size(); ++i) {
          if (origin_out_shape[i] == -1) {
            origin_out_shape[i] = out_dims[index];
            ++index;
          }
        }
216
        out_dims = phi::make_ddim(origin_out_shape);
A
Aganlengzi 已提交
217 218 219 220
      }
      tmp_dout.Resize(out_dims);
    }

221 222 223 224
    dinput->mutable_data<T>(ctx.GetPlace());
    auto stream =
        ctx.template device_context<paddle::platform::NPUDeviceContext>()
            .stream();
L
Leo Chen 已提交
225
    const auto& runner =
A
Aganlengzi 已提交
226
        NpuOpRunner("PadD", {tmp_dout}, {*dinput}, {{"paddings", paddings}});
227 228 229 230 231 232 233 234 235
    runner.Run(stream);
  }
};

}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;

236 237
REGISTER_OP_NPU_KERNEL(slice,
                       ops::SliceNPUKernel<float>,
238 239 240 241 242 243
                       ops::SliceNPUKernel<int>,
#ifdef PADDLE_WITH_ASCEND_INT64
                       ops::SliceNPUKernel<int64_t>,
#endif
                       ops::SliceNPUKernel<paddle::platform::float16>);

244 245
REGISTER_OP_NPU_KERNEL(slice_grad,
                       ops::SliceGradNPUKernel<float>,
246 247
                       ops::SliceGradNPUKernel<int>,
                       ops::SliceGradNPUKernel<paddle::platform::float16>);