|
| 1 | +// Copyright (c) 2022 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 | +#include "paddle/phi/kernels/set_value_kernel.h" |
| 16 | + |
| 17 | +#include "paddle/phi/backends/xpu/enforce_xpu.h" |
| 18 | +#include "paddle/phi/backends/xpu/xpu_context.h" |
| 19 | +#include "paddle/phi/core/kernel_registry.h" |
| 20 | + |
| 21 | +#include "paddle/phi/common/int_array.h" |
| 22 | +#include "paddle/phi/common/scalar.h" |
| 23 | +#include "paddle/phi/core/dense_tensor.h" |
| 24 | +#include "paddle/phi/core/tensor_utils.h" |
| 25 | +#include "paddle/phi/kernels/empty_kernel.h" |
| 26 | +#include "paddle/phi/kernels/funcs/broadcast_function.h" |
| 27 | +#include "paddle/phi/kernels/funcs/eigen/common.h" |
| 28 | +#include "paddle/phi/kernels/funcs/eigen/eigen_function.h" |
| 29 | +#include "paddle/phi/kernels/funcs/elementwise_functor.h" |
| 30 | +#include "paddle/phi/kernels/funcs/slice_utils.h" |
| 31 | + |
| 32 | +namespace phi { |
| 33 | + |
| 34 | +template <typename T, typename Context> |
| 35 | +void SetTensorValueKernel(const Context& dev_ctx, |
| 36 | + const DenseTensor& x, |
| 37 | + const DenseTensor& value, |
| 38 | + const IntArray& starts, |
| 39 | + const IntArray& ends, |
| 40 | + const IntArray& steps, |
| 41 | + const std::vector<int64_t>& axes, |
| 42 | + const std::vector<int64_t>& decrease_axes, |
| 43 | + const std::vector<int64_t>& none_axes, |
| 44 | + DenseTensor* out) { |
| 45 | + using XPUType = typename XPUTypeTrait<T>::Type; |
| 46 | + out->Resize(x.dims()); |
| 47 | + dev_ctx.template Alloc<T>(out); |
| 48 | + |
| 49 | + const XPUType* x_data = reinterpret_cast<const XPUType*>(x.data<T>()); |
| 50 | + const XPUType* v_data = reinterpret_cast<const XPUType*>(value.data<T>()); |
| 51 | + XPUType* y_data = reinterpret_cast<XPUType*>(out->data<T>()); |
| 52 | + |
| 53 | + std::vector<int64_t> starts_vec = starts.GetData(); |
| 54 | + std::vector<int64_t> ends_vec = ends.GetData(); |
| 55 | + std::vector<int64_t> steps_vec = steps.GetData(); |
| 56 | + |
| 57 | + std::vector<int> starts_vec_int32; |
| 58 | + for (size_t i = 0; i < starts_vec.size(); ++i) { |
| 59 | + starts_vec_int32.push_back(starts_vec[i]); |
| 60 | + } |
| 61 | + |
| 62 | + std::vector<int> ends_vec_int32; |
| 63 | + for (size_t i = 0; i < ends_vec.size(); ++i) { |
| 64 | + ends_vec_int32.push_back(ends_vec[i]); |
| 65 | + } |
| 66 | + |
| 67 | + std::vector<int> steps_vec_int32; |
| 68 | + for (size_t i = 0; i < steps_vec.size(); ++i) { |
| 69 | + steps_vec_int32.push_back(steps_vec[i]); |
| 70 | + } |
| 71 | + |
| 72 | + std::vector<int> axes_int32; |
| 73 | + for (size_t i = 0; i < axes.size(); ++i) { |
| 74 | + axes_int32.push_back(axes[i]); |
| 75 | + } |
| 76 | + |
| 77 | + std::vector<int> decrease_axes_int32; |
| 78 | + for (size_t i = 0; i < decrease_axes.size(); ++i) { |
| 79 | + decrease_axes_int32.push_back(decrease_axes[i]); |
| 80 | + } |
| 81 | + |
| 82 | + std::vector<int> none_axes_int32; |
| 83 | + for (size_t i = 0; i < none_axes.size(); ++i) { |
| 84 | + none_axes_int32.push_back(none_axes[i]); |
| 85 | + } |
| 86 | + |
| 87 | + auto x_dims = x.dims(); |
| 88 | + std::vector<int> x_shape; |
| 89 | + for (int i = 0; i < x_dims.size(); ++i) { |
| 90 | + x_shape.push_back(x_dims[i]); |
| 91 | + } |
| 92 | + |
| 93 | + auto v_dims = value.dims(); |
| 94 | + std::vector<int> v_shape; |
| 95 | + for (int i = 0; i < v_dims.size(); ++i) { |
| 96 | + v_shape.push_back(v_dims[i]); |
| 97 | + } |
| 98 | + |
| 99 | + int r = xpu::set_value(dev_ctx.x_context(), |
| 100 | + x_data, |
| 101 | + v_data, |
| 102 | + y_data, |
| 103 | + x_shape, |
| 104 | + v_shape, |
| 105 | + starts_vec_int32, |
| 106 | + ends_vec_int32, |
| 107 | + steps_vec_int32, |
| 108 | + axes_int32, |
| 109 | + decrease_axes_int32, |
| 110 | + none_axes_int32); |
| 111 | + PADDLE_ENFORCE_XDNN_SUCCESS(r, "set_value"); |
| 112 | +} |
| 113 | + |
| 114 | +template <typename T, typename Context> |
| 115 | +void SetValueKernel(const Context& dev_ctx, |
| 116 | + const DenseTensor& x, |
| 117 | + const IntArray& starts, |
| 118 | + const IntArray& ends, |
| 119 | + const IntArray& steps, |
| 120 | + const std::vector<int64_t>& axes, |
| 121 | + const std::vector<int64_t>& decrease_axes, |
| 122 | + const std::vector<int64_t>& none_axes, |
| 123 | + const std::vector<int64_t>& shape, |
| 124 | + const std::vector<Scalar>& values, |
| 125 | + DenseTensor* out) { |
| 126 | + std::vector<T> assgin_values; |
| 127 | + assgin_values.reserve(values.size()); |
| 128 | + for (const auto& val : values) { |
| 129 | + assgin_values.push_back(val.to<T>()); |
| 130 | + } |
| 131 | + DenseTensor value_tensor = Empty<T>(dev_ctx, shape); |
| 132 | + paddle::framework::TensorFromVector(assgin_values, dev_ctx, &value_tensor); |
| 133 | + value_tensor.Resize(phi::make_ddim(shape)); |
| 134 | + |
| 135 | + SetTensorValueKernel<T, Context>(dev_ctx, |
| 136 | + x, |
| 137 | + value_tensor, |
| 138 | + starts, |
| 139 | + ends, |
| 140 | + steps, |
| 141 | + axes, |
| 142 | + decrease_axes, |
| 143 | + none_axes, |
| 144 | + out); |
| 145 | +} |
| 146 | + |
| 147 | +} // namespace phi |
| 148 | + |
| 149 | +PD_REGISTER_KERNEL(set_value, |
| 150 | + XPU, |
| 151 | + ALL_LAYOUT, |
| 152 | + phi::SetValueKernel, |
| 153 | + float, |
| 154 | + phi::dtype::float16, |
| 155 | + int, |
| 156 | + int64_t) {} |
| 157 | + |
| 158 | +PD_REGISTER_KERNEL(set_value_with_tensor, |
| 159 | + XPU, |
| 160 | + ALL_LAYOUT, |
| 161 | + phi::SetTensorValueKernel, |
| 162 | + float, |
| 163 | + phi::dtype::float16, |
| 164 | + int, |
| 165 | + int64_t) {} |
0 commit comments