/* 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 Licnse. */ #include "paddle/fluid/operators/slice_op.h" #include "paddle/fluid/operators/npu_op_runner.h" namespace paddle { namespace operators { using Tensor = framework::Tensor; using NPUDeviceContext = platform::NPUDeviceContext; void UpdateAttr(const framework::DDim& in_dims, const std::vector axes, const std::vector starts, const std::vector ends, std::vector* offsets, std::vector* size) { int cnt = 0; for (int i = 0; i < in_dims.size(); ++i) { int start = 0; int end = in_dims[i]; // NOTE(zhiqiu): Becareful that cnt may > axes.size() and result in // overflow. int axis = cnt < static_cast(axes.size()) ? axes[cnt] : -1; if (axis == i) { start = starts[cnt]; if (start < 0) { start = (start + in_dims[i]); } start = std::max(start, static_cast(0)); end = ends[cnt]; if (end < 0) { end = (end + in_dims[i]); } end = std::min(end, static_cast(in_dims[i])); cnt++; } (*offsets)[i] = start; (*size)[i] = end - start; } } template class SliceNPUKernel : public framework::OpKernel { public: void Compute(const framework::ExecutionContext& ctx) const override { auto* input = ctx.Input("Input"); auto* out = ctx.Output("Out"); auto axes_int = ctx.Attr>("axes"); auto starts_int = ctx.Attr>("starts"); auto ends_int = ctx.Attr>("ends"); std::vector axes(axes_int.begin(), axes_int.end()); std::vector starts(starts_int.begin(), starts_int.end()); std::vector ends(ends_int.begin(), ends_int.end()); auto decrease_axis = ctx.Attr>("decrease_axis"); auto infer_flags = ctx.Attr>("infer_flags"); const auto& in_dims = input->dims(); // Get the accurate attribute value of starts and ends auto starts_tensor_list = ctx.MultiInput("StartsTensorList"); if (ctx.HasInput("StartsTensor")) { starts = GetDataFromTensor(ctx.Input("StartsTensor")); } else if (starts_tensor_list.size() > 0) { starts = GetDataFromTensorList(starts_tensor_list); } auto ends_tensor_list = ctx.MultiInput("EndsTensorList"); if (ctx.HasInput("EndsTensor")) { ends = GetDataFromTensor(ctx.Input("EndsTensor")); } else if (ends_tensor_list.size() > 0) { ends = GetDataFromTensorList(ends_tensor_list); } PADDLE_ENFORCE_EQ( starts.size(), axes.size(), platform::errors::InvalidArgument( "The size of starts must be equal to the size of axes.")); PADDLE_ENFORCE_EQ( ends.size(), axes.size(), 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]]; } } } CheckAndUpdateSliceAttrs(in_dims, axes, &starts, &ends); slice_dims = GetSliceDims(in_dims, axes, starts, ends, nullptr, nullptr); out_dims = GetDecreasedDims(slice_dims, decrease_axis); out->Resize(out_dims); } out->mutable_data(ctx.GetPlace()); std::vector offsets(in_dims.size()); std::vector size(in_dims.size()); UpdateAttr(in_dims, axes, starts, ends, &offsets, &size); auto stream = ctx.template device_context().stream(); const auto& runner = NpuOpRunner("SliceD", {*input}, {*out}, {{"offsets", offsets}, {"size", size}}); runner.Run(stream); } }; template class SliceGradNPUKernel : public framework::OpKernel { public: void Compute(const framework::ExecutionContext& ctx) const override { auto* input = ctx.Input("Input"); auto* dout = ctx.Input(framework::GradVarName("Out")); auto* dinput = ctx.Output(framework::GradVarName("Input")); auto axes_int = ctx.Attr>("axes"); auto starts_int = ctx.Attr>("starts"); auto ends_int = ctx.Attr>("ends"); std::vector axes(axes_int.begin(), axes_int.end()); std::vector starts(starts_int.begin(), starts_int.end()); std::vector ends(ends_int.begin(), ends_int.end()); // Get the accurate attribute value of starts and ends auto starts_tensor_list = ctx.MultiInput("StartsTensorList"); if (ctx.HasInput("StartsTensor")) { starts = GetDataFromTensor(ctx.Input("StartsTensor")); } else if (starts_tensor_list.size() > 0) { starts = GetDataFromTensorList(starts_tensor_list); } auto ends_tensor_list = ctx.MultiInput("EndsTensorList"); if (ctx.HasInput("EndsTensor")) { ends = GetDataFromTensor(ctx.Input("EndsTensor")); } else if (ends_tensor_list.size() > 0) { ends = GetDataFromTensorList(ends_tensor_list); } const auto& in_dims = input->dims(); int rank = in_dims.size(); std::vector offsets(rank); std::vector size(rank); UpdateAttr(in_dims, axes, starts, ends, &offsets, &size); std::vector> paddings(rank, std::vector(2)); for (int i = 0; i < rank; ++i) { paddings[i][0] = static_cast(offsets[i]); paddings[i][1] = static_cast(in_dims[i] - size[i] - offsets[i]); } Tensor tmp_dout; tmp_dout.ShareDataWith(*dout); auto out_dims = dout->dims(); auto decrease_axis = ctx.Attr>("decrease_axis"); auto decrease_size = decrease_axis.size(); if (decrease_size > 0) { if (decrease_size == static_cast(in_dims.size())) { out_dims = framework::make_ddim(std::vector(decrease_size, 1)); } else { std::vector 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; } } out_dims = framework::make_ddim(origin_out_shape); } tmp_dout.Resize(out_dims); } dinput->mutable_data(ctx.GetPlace()); auto stream = ctx.template device_context() .stream(); const auto& runner = NpuOpRunner("PadD", {tmp_dout}, {*dinput}, {{"paddings", paddings}}); runner.Run(stream); } }; } // namespace operators } // namespace paddle namespace ops = paddle::operators; REGISTER_OP_NPU_KERNEL(slice, ops::SliceNPUKernel, ops::SliceNPUKernel, #ifdef PADDLE_WITH_ASCEND_INT64 ops::SliceNPUKernel, #endif ops::SliceNPUKernel); REGISTER_OP_NPU_KERNEL(slice_grad, ops::SliceGradNPUKernel, ops::SliceGradNPUKernel, ops::SliceGradNPUKernel);