|
| 1 | +/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. |
| 2 | +
|
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | +
|
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | +Unless required by applicable law or agreed to in writing, software |
| 10 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +See the License for the specific language governing permissions and |
| 13 | +limitations under the License. */ |
| 14 | + |
| 15 | +#ifdef PADDLE_WITH_XPU |
| 16 | + |
| 17 | +#include <algorithm> |
| 18 | +#include <memory> |
| 19 | +#include <string> |
| 20 | +#include <vector> |
| 21 | +#include "paddle/fluid/operators/slice_op.h" |
| 22 | + |
| 23 | +namespace paddle { |
| 24 | +namespace operators { |
| 25 | + |
| 26 | +using Tensor = framework::Tensor; |
| 27 | + |
| 28 | +template <typename DeviceContext, typename T> |
| 29 | +class SliceXPUKernel : public framework::OpKernel<T> { |
| 30 | + public: |
| 31 | + void Compute(const framework::ExecutionContext& ctx) const override { |
| 32 | + auto in = ctx.Input<framework::Tensor>("Input"); |
| 33 | + auto out = ctx.Output<framework::Tensor>("Out"); |
| 34 | + auto axes = ctx.Attr<std::vector<int>>("axes"); |
| 35 | + auto starts = ctx.Attr<std::vector<int>>("starts"); |
| 36 | + auto ends = ctx.Attr<std::vector<int>>("ends"); |
| 37 | + auto in_dims = in->dims(); |
| 38 | + |
| 39 | + // prepare starts, ends on XPU |
| 40 | + int dim_value = 0, start = 0, end = 0; |
| 41 | + // If a negative value is passed for any of the start or end indices, |
| 42 | + // it represents number of elements before the end of that dimension. |
| 43 | + // If the value passed to start or end is larger than the n |
| 44 | + // (the number of elements in this dimension), it represents n. |
| 45 | + for (size_t i = 0; i < axes.size(); ++i) { |
| 46 | + dim_value = in_dims[axes[i]]; |
| 47 | + start = starts[i]; |
| 48 | + end = ends[i]; |
| 49 | + start = start < 0 ? (start + dim_value) : start; |
| 50 | + end = end < 0 ? (end + dim_value) : end; |
| 51 | + start = std::max(start, 0); |
| 52 | + end = std::max(end, 0); |
| 53 | + end = std::min(end, dim_value); |
| 54 | + PADDLE_ENFORCE_GT(end, start, platform::errors::InvalidArgument( |
| 55 | + "end should greater than start")); |
| 56 | + starts[i] = start; |
| 57 | + ends[i] = end; |
| 58 | + } |
| 59 | + size_t shape_size = in_dims.size(); |
| 60 | + // the slice XPU kernel require that the length of `start`, `end` must be |
| 61 | + // equal |
| 62 | + // to the dims size of input tensor, therefore, if shape_size > axes.size(), |
| 63 | + // the `starts_extension` and `ends_extension` is necessary. |
| 64 | + std::vector<int> starts_extension(shape_size, 0); |
| 65 | + std::vector<int> ends_extension(shape_size, 0); |
| 66 | + if (shape_size > axes.size()) { |
| 67 | + for (size_t i = 0; i < shape_size; ++i) { |
| 68 | + ends_extension[i] = in_dims[i]; |
| 69 | + } |
| 70 | + for (size_t i = 0; i < axes.size(); ++i) { |
| 71 | + starts_extension[axes[i]] = starts[i]; |
| 72 | + ends_extension[axes[i]] = ends[i]; |
| 73 | + } |
| 74 | + } else { |
| 75 | + starts_extension = std::move(starts); |
| 76 | + ends_extension = std::move(ends); |
| 77 | + } |
| 78 | + |
| 79 | + // prepare shape on XPU |
| 80 | + std::vector<int> shape(shape_size, 0); |
| 81 | + for (size_t i = 0; i < shape_size; ++i) { |
| 82 | + shape[i] = in_dims[i]; |
| 83 | + } |
| 84 | + |
| 85 | + auto& dev_ctx = ctx.template device_context<DeviceContext>(); |
| 86 | + auto* in_data = in->data<T>(); |
| 87 | + auto* out_data = out->mutable_data<T>(ctx.GetPlace()); |
| 88 | + |
| 89 | + int r = xpu::slice_forward(dev_ctx.x_context(), shape.data(), |
| 90 | + starts_extension.data(), ends_extension.data(), |
| 91 | + shape_size, in_data, out_data); |
| 92 | + PADDLE_ENFORCE_EQ(r, XPU_SUCCESS, |
| 93 | + platform::errors::External("XPU slice kernel error!")); |
| 94 | + } |
| 95 | +}; |
| 96 | + |
| 97 | +template <typename DeviceContext, typename T> |
| 98 | +class SliceGradXPUKernel : public framework::OpKernel<T> { |
| 99 | + public: |
| 100 | + void Compute(const framework::ExecutionContext& ctx) const override { |
| 101 | + auto* d_out = ctx.Input<framework::Tensor>(framework::GradVarName("Out")); |
| 102 | + auto* d_in = ctx.Output<framework::Tensor>(framework::GradVarName("Input")); |
| 103 | + d_in->mutable_data<T>(ctx.GetPlace()); |
| 104 | + |
| 105 | + auto in_dims = d_in->dims(); |
| 106 | + auto axes = ctx.Attr<std::vector<int>>("axes"); |
| 107 | + auto starts = ctx.Attr<std::vector<int>>("starts"); |
| 108 | + auto ends = ctx.Attr<std::vector<int>>("ends"); |
| 109 | + |
| 110 | + // prepare starts, ends on XPU |
| 111 | + int dim_value = 0, start = 0, end = 0; |
| 112 | + // If a negative value is passed for any of the start or end indices, |
| 113 | + // it represents number of elements before the end of that dimension. |
| 114 | + // If the value passed to start or end is larger than the n |
| 115 | + // (the number of elements in this dimension), it represents n. |
| 116 | + for (size_t i = 0; i < axes.size(); ++i) { |
| 117 | + dim_value = in_dims[axes[i]]; |
| 118 | + start = starts[i]; |
| 119 | + end = ends[i]; |
| 120 | + start = start < 0 ? (start + dim_value) : start; |
| 121 | + end = end < 0 ? (end + dim_value) : end; |
| 122 | + start = std::max(start, 0); |
| 123 | + end = std::max(end, 0); |
| 124 | + end = std::min(end, dim_value); |
| 125 | + PADDLE_ENFORCE_GT(end, start, platform::errors::InvalidArgument( |
| 126 | + "end should greater than start")); |
| 127 | + starts[i] = start; |
| 128 | + ends[i] = end; |
| 129 | + } |
| 130 | + size_t shape_size = in_dims.size(); |
| 131 | + // the slice XPU kernel require that the length of `start`, `end` must be |
| 132 | + // equal |
| 133 | + // to the dims size of input tensor, therefore, if shape_size > axes.size(), |
| 134 | + // the `starts_extension` and `ends_extension` is necessary. |
| 135 | + std::vector<int> starts_extension(shape_size, 0); |
| 136 | + std::vector<int> ends_extension(shape_size, 0); |
| 137 | + if (shape_size > axes.size()) { |
| 138 | + for (size_t i = 0; i < shape_size; ++i) { |
| 139 | + ends_extension[i] = in_dims[i]; |
| 140 | + } |
| 141 | + for (size_t i = 0; i < axes.size(); ++i) { |
| 142 | + starts_extension[axes[i]] = starts[i]; |
| 143 | + ends_extension[axes[i]] = ends[i]; |
| 144 | + } |
| 145 | + } |
| 146 | + int* starts_device = nullptr; |
| 147 | + int* ends_device = nullptr; |
| 148 | + int* starts_host = |
| 149 | + shape_size > axes.size() ? starts_extension.data() : starts.data(); |
| 150 | + int* ends_host = |
| 151 | + shape_size > axes.size() ? ends_extension.data() : ends.data(); |
| 152 | + PADDLE_ENFORCE_EQ( |
| 153 | + xpu_malloc((void**)(&starts_device), shape_size * sizeof(int)), |
| 154 | + XPU_SUCCESS, platform::errors::External("XPU has no enough memory")); |
| 155 | + PADDLE_ENFORCE_EQ( |
| 156 | + xpu_malloc((void**)(&ends_device), shape_size * sizeof(int)), |
| 157 | + XPU_SUCCESS, platform::errors::External("XPU has no enough memory")); |
| 158 | + memory::Copy(BOOST_GET_CONST(platform::XPUPlace, ctx.GetPlace()), |
| 159 | + starts_device, platform::CPUPlace(), starts_host, |
| 160 | + shape_size * sizeof(int)); |
| 161 | + memory::Copy(BOOST_GET_CONST(platform::XPUPlace, ctx.GetPlace()), |
| 162 | + ends_device, platform::CPUPlace(), ends_host, |
| 163 | + shape_size * sizeof(int)); |
| 164 | + |
| 165 | + // prepare shape on XPU |
| 166 | + std::vector<int> shape(shape_size, 0); |
| 167 | + for (size_t i = 0; i < shape_size; ++i) { |
| 168 | + shape[i] = in_dims[i]; |
| 169 | + } |
| 170 | + int* shape_device = nullptr; |
| 171 | + PADDLE_ENFORCE_EQ( |
| 172 | + xpu_malloc((void**)(&shape_device), shape_size * sizeof(int)), |
| 173 | + XPU_SUCCESS, platform::errors::External("XPU has no enough memory")); |
| 174 | + memory::Copy(BOOST_GET_CONST(platform::XPUPlace, ctx.GetPlace()), |
| 175 | + shape_device, platform::CPUPlace(), shape.data(), |
| 176 | + shape_size * sizeof(int)); |
| 177 | + |
| 178 | + auto& dev_ctx = ctx.template device_context<DeviceContext>(); |
| 179 | + int r = |
| 180 | + xpu::slice_backward(dev_ctx.x_context(), shape_device, starts_device, |
| 181 | + ends_device, shape_size, d_out->data<T>(), |
| 182 | + d_in->data<T>(), d_in->numel(), d_out->numel()); |
| 183 | + PADDLE_ENFORCE_EQ(r, XPU_SUCCESS, |
| 184 | + platform::errors::External("xpu slice kernel error")); |
| 185 | + dev_ctx.Wait(); |
| 186 | + // free device data |
| 187 | + xpu_free(shape_device); |
| 188 | + xpu_free(starts_device); |
| 189 | + xpu_free(ends_device); |
| 190 | + } |
| 191 | +}; |
| 192 | + |
| 193 | +} // namespace operators |
| 194 | +} // namespace paddle |
| 195 | + |
| 196 | +namespace ops = paddle::operators; |
| 197 | + |
| 198 | +REGISTER_OP_XPU_KERNEL( |
| 199 | + slice, ops::SliceXPUKernel<paddle::platform::XPUDeviceContext, float>); |
| 200 | +REGISTER_OP_XPU_KERNEL( |
| 201 | + slice_grad, |
| 202 | + ops::SliceGradXPUKernel<paddle::platform::XPUDeviceContext, float>); |
| 203 | +#endif |
0 commit comments