From ab00c96cbe3e675aa4df8cc7208a496ba4881b8c Mon Sep 17 00:00:00 2001 From: lijin23 <41257772+lj970926@users.noreply.github.com> Date: Thu, 20 Jul 2023 11:17:28 +0800 Subject: [PATCH] [XPU][PHI Kernels] bind reduce_max_int64 set_value_bool sin_grad_fp32 cos_grad_fp32 for XPU (#55375) * bind kernels for xpu * format code * format code * 0d support for set value * refine set_value --- paddle/phi/backends/xpu/xpu2_op_list.cc | 9 +- .../phi/kernels/xpu/activation_grad_kernel.cc | 51 ++++ paddle/phi/kernels/xpu/reduce_max_kernel.cc | 2 +- paddle/phi/kernels/xpu/set_value_kernel.cc | 112 ++++----- test/xpu/test_set_value_op_xpu.py | 222 +++++------------- 5 files changed, 166 insertions(+), 230 deletions(-) diff --git a/paddle/phi/backends/xpu/xpu2_op_list.cc b/paddle/phi/backends/xpu/xpu2_op_list.cc index 1e32cdba98d..c146108917e 100644 --- a/paddle/phi/backends/xpu/xpu2_op_list.cc +++ b/paddle/phi/backends/xpu/xpu2_op_list.cc @@ -591,7 +591,9 @@ XPUOpMap& get_kl2_ops() { {"reduce_any", XPUKernelSet({phi::DataType::BOOL})}, {"reduce_max_grad", XPUKernelSet({phi::DataType::FLOAT32})}, {"reduce_max", - XPUKernelSet({phi::DataType::FLOAT32, phi::DataType::INT32})}, + XPUKernelSet({phi::DataType::FLOAT32, + phi::DataType::INT32, + phi::DataType::INT64})}, {"reduce_mean_grad", XPUKernelSet({phi::DataType::FLOAT32})}, {"reduce_mean", XPUKernelSet({phi::DataType::FLOAT32, phi::DataType::FLOAT16})}, @@ -663,7 +665,8 @@ XPUOpMap& get_kl2_ops() { XPUKernelSet({phi::DataType::FLOAT32, phi::DataType::INT32, phi::DataType::INT64, - phi::DataType::FLOAT16})}, + phi::DataType::FLOAT16, + phi::DataType::BOOL})}, {"set_value_grad", XPUKernelSet({phi::DataType::FLOAT32, phi::DataType::INT32, @@ -935,7 +938,9 @@ XPUOpMap& get_kl2_ops() { phi::DataType::FLOAT32, phi::DataType::FLOAT16})}, {"sin", XPUKernelSet({phi::DataType::FLOAT32})}, + {"sin_grad", XPUKernelSet({phi::DataType::FLOAT32})}, {"cos", XPUKernelSet({phi::DataType::FLOAT32})}, + {"cos_grad", XPUKernelSet({phi::DataType::FLOAT32})}, {"linspace", XPUKernelSet({phi::DataType::FLOAT32, phi::DataType::INT32})}, {"randint", XPUKernelSet({phi::DataType::INT32, phi::DataType::INT64})}, diff --git a/paddle/phi/kernels/xpu/activation_grad_kernel.cc b/paddle/phi/kernels/xpu/activation_grad_kernel.cc index 62548434eb2..a40156c1031 100644 --- a/paddle/phi/kernels/xpu/activation_grad_kernel.cc +++ b/paddle/phi/kernels/xpu/activation_grad_kernel.cc @@ -565,6 +565,52 @@ struct XPUSoftPlusGradFunctor : public funcs::BaseActivationFunctor { } }; +template +struct XPUSinGradFunctor : public funcs::BaseActivationFunctor { + using XPUType = typename XPUTypeTrait::Type; + template + void operator()(const Context& dev_ctx, + const DenseTensor* x, + const DenseTensor* out, + const DenseTensor* dout, + DenseTensor* dx) const { + int64_t len = dx->numel(); + auto dx_data = dev_ctx.template Alloc(dx); + auto dout_data = dout->data(); + auto x_data = x->data(); + + int r = xpu::sin_grad(dev_ctx.x_context(), + reinterpret_cast(x_data), + reinterpret_cast(dout_data), + reinterpret_cast(dx_data), + len); + PADDLE_ENFORCE_XDNN_SUCCESS(r, "sin_grad"); + } +}; + +template +struct XPUCosGradFunctor : public funcs::BaseActivationFunctor { + using XPUType = typename XPUTypeTrait::Type; + template + void operator()(const Context& dev_ctx, + const DenseTensor* x, + const DenseTensor* out, + const DenseTensor* dout, + DenseTensor* dx) const { + int64_t len = dx->numel(); + auto dx_data = dev_ctx.template Alloc(dx); + auto dout_data = dout->data(); + auto x_data = x->data(); + + int r = xpu::cos_grad(dev_ctx.x_context(), + reinterpret_cast(x_data), + reinterpret_cast(dout_data), + reinterpret_cast(dx_data), + len); + PADDLE_ENFORCE_XDNN_SUCCESS(r, "cos_grad"); + } +}; + DEFINE_XPU_ACTIVATION_GRAD_KERNEL_DEPOUT(Exp, XPUExpGradFunctor); DEFINE_XPU_ACTIVATION_GRAD_KERNEL_DEPOUT(Reciprocal, XPUReciprocalGradFunctor); DEFINE_XPU_ACTIVATION_GRAD_KERNEL_DEPOUT(Sigmoid, XPUSigmoidGradFunctor); @@ -576,6 +622,8 @@ DEFINE_XPU_ACTIVATION_GRAD_KERNEL_DEPOUT(Relu6, XPURelu6GradFunctor); DEFINE_XPU_ACTIVATION_GRAD_KERNEL_DEPX(Log, XPULogGradFunctor); DEFINE_XPU_ACTIVATION_GRAD_KERNEL_DEPX(Square, XPUSquareGradFunctor); DEFINE_XPU_ACTIVATION_GRAD_KERNEL_DEPX(Swish, XPUSwishGradFunctor); +DEFINE_XPU_ACTIVATION_GRAD_KERNEL_DEPX(Sin, XPUSinGradFunctor); +DEFINE_XPU_ACTIVATION_GRAD_KERNEL_DEPX(Cos, XPUCosGradFunctor); DEFINE_XPU_ACT_GRAD_KERNEL_WITH_ONE_ATTRS_DEPX(Mish, XPUMishGradFunctor, @@ -664,4 +712,7 @@ PD_REGISTER_ACTIVATION_GRAD_KERNEL(sqrt_grad, SqrtGradKernel) PD_REGISTER_ACTIVATION_GRAD_KERNEL(mish_grad, MishGradKernel) PD_REGISTER_ACTIVATION_GRAD_KERNEL(swish_grad, SwishGradKernel) PD_REGISTER_ACTIVATION_GRAD_KERNEL(softplus_grad, SoftplusGradKernel) +PD_REGISTER_ACTIVATION_GRAD_KERNEL(sin_grad, SinGradKernel) +PD_REGISTER_ACTIVATION_GRAD_KERNEL(cos_grad, CosGradKernel) + PD_REGISTER_KERNEL(pow_grad, XPU, ALL_LAYOUT, phi::PowGradKernel, float) {} diff --git a/paddle/phi/kernels/xpu/reduce_max_kernel.cc b/paddle/phi/kernels/xpu/reduce_max_kernel.cc index afe7f4a7314..1bc56a3990e 100644 --- a/paddle/phi/kernels/xpu/reduce_max_kernel.cc +++ b/paddle/phi/kernels/xpu/reduce_max_kernel.cc @@ -48,4 +48,4 @@ void MaxKernel(const Context& dev_ctx, } // namespace phi -PD_REGISTER_KERNEL(max, XPU, ALL_LAYOUT, phi::MaxKernel, float, int) {} +PD_REGISTER_KERNEL(max, XPU, ALL_LAYOUT, phi::MaxKernel, float, int, int64_t) {} diff --git a/paddle/phi/kernels/xpu/set_value_kernel.cc b/paddle/phi/kernels/xpu/set_value_kernel.cc index 0d984be8b2f..db5ce1ca0ef 100644 --- a/paddle/phi/kernels/xpu/set_value_kernel.cc +++ b/paddle/phi/kernels/xpu/set_value_kernel.cc @@ -14,16 +14,12 @@ #include "paddle/phi/kernels/set_value_kernel.h" +#include +#include + #include "paddle/phi/backends/xpu/enforce_xpu.h" -#include "paddle/phi/backends/xpu/xpu_context.h" #include "paddle/phi/core/kernel_registry.h" - -#include "paddle/phi/common/int_array.h" -#include "paddle/phi/common/scalar.h" -#include "paddle/phi/core/dense_tensor.h" #include "paddle/phi/core/tensor_utils.h" -#include "paddle/phi/kernels/empty_kernel.h" -#include "paddle/phi/kernels/funcs/elementwise_base.h" #include "paddle/phi/kernels/funcs/slice_utils.h" #include "paddle/phi/kernels/xpu/elementwise.h" @@ -84,6 +80,14 @@ void SetValueImpl(const Context& dev_ctx, DenseTensor* out) { using XPUType = typename XPUTypeTrait::Type; auto in_dims = in.dims(); + + auto new_value_dims = value_dims; + + // support for 0-d tensor + if (value_dims.size() == 0) { + new_value_dims = {1}; + } + std::vector starts_local = starts.GetData(); std::vector ends_local = ends.GetData(); std::vector steps_local = steps.GetData(); @@ -165,41 +169,7 @@ void SetValueImpl(const Context& dev_ctx, return; } } - - // Because strided_slice does not support the case of stride < 0 - // temporarily, the coordinates of starts_indices, ends_indices - // and strides_indices need to be converted. - // This logic may be deleted in the future. - bool need_flip = false; - for (size_t i = 0; i < RANK; ++i) { - if (strides_indices[i] < 0) { - if (!need_flip) { - need_flip = true; - } - flip_axis.push_back(i); - strides_indices[i] = strides_indices[i] * (-1); - ends_indices[i] = starts_indices[i] + 1; - starts_indices[i] = - starts_indices[i] - (slice_dims[i] - 1) * strides_indices[i]; - } - } - - auto out_shape = phi::vectorize(out->dims()); - auto slice_shape = phi::vectorize(slice_dims); - r = xpu::strided_slice(dev_ctx.x_context(), - reinterpret_cast(out->data()), - slice_data, - out_shape, - starts_indices, - ends_indices, - strides_indices); - PADDLE_ENFORCE_XDNN_SUCCESS(r, "strided_slice"); - - r = xpu::constant(dev_ctx.x_context(), slice_data, slice_numels, XPUType(0)); - PADDLE_ENFORCE_XDNN_SUCCESS(r, "constant"); - - // Step 2: Set a tensor with the same shape as out tensor. And its data at - // '_index' is the same as value, and data out of '_index' to zero + // Step 2: Set slice tensor // - Step 2.1 Set slice tensor with value @@ -215,26 +185,50 @@ void SetValueImpl(const Context& dev_ctx, // If do broadcasting on Tensor with shape [3] and [3], the result's shape // is [3], which is right. - CheckIsDimsMatch(slice_dims_for_assign, value_dims); - // XPUElementwise can do broadcasting + CheckIsDimsMatch(slice_dims_for_assign, new_value_dims); + + // do broadcasting auto f = [](xpu::Context* ctx, const XPUType* x, - const XPUType* y, + const XPUType* y, /*unused*/ XPUType* z, const std::vector& xshape, - const std::vector& yshape) { - return xpu::broadcast_add(ctx, x, y, z, xshape, yshape); + const std::vector& zshape) { + return xpu::broadcast(ctx, x, z, xshape, zshape); }; + XPUElementwise(dev_ctx, - reinterpret_cast(slice_data), - slice_dims_for_assign, value_data, - value_dims, + new_value_dims, + nullptr, + slice_dims_for_assign, -1, reinterpret_cast(slice_data), f); // - Step 2.2 If stride < 0, flip the slice_tensor. + // Because strided_slice_view_update does not support the case of stride < 0 + // temporarily, the coordinates of starts_indices, ends_indices + // and strides_indices need to be converted. + // This logic may be deleted in the future. + + bool need_flip = false; + for (size_t i = 0; i < RANK; ++i) { + if (strides_indices[i] < 0) { + if (!need_flip) { + need_flip = true; + } + flip_axis.push_back(i); + strides_indices[i] = strides_indices[i] * (-1); + ends_indices[i] = starts_indices[i] + 1; + starts_indices[i] = + starts_indices[i] - (slice_dims[i] - 1) * strides_indices[i]; + } + } + + auto out_shape = phi::vectorize(out->dims()); + auto slice_shape = phi::vectorize(slice_dims); + if (need_flip) { r = xpu::flip(dev_ctx.x_context(), reinterpret_cast(slice_data), @@ -392,29 +386,20 @@ void SetValueKernel(const Context& dev_ctx, const std::vector& shape, const std::vector& values, DenseTensor* out) { - using XPUType = typename XPUTypeTrait::Type; std::vector assign_values; assign_values.reserve(values.size()); for (const auto& val : values) { assign_values.push_back(val.to()); } - xpu::ctx_guard RAII_GUARD(dev_ctx.x_context()); auto value_dims = phi::make_ddim(shape); - XPUType* value_data = - RAII_GUARD.alloc_l3_or_gm(phi::product(value_dims)); - phi::CPUPlace src_place; - auto dst_place = dev_ctx.GetPlace(); - memory_utils::Copy(dst_place, - value_data, - src_place, - assign_values.data(), - assign_values.size() * sizeof(T)); + DenseTensor value_tensor; + TensorFromVector(assign_values, dev_ctx, &value_tensor); SetValueKernelImpl(dev_ctx, x, - reinterpret_cast(value_data), + value_tensor.data(), value_dims, starts, ends, @@ -434,7 +419,8 @@ PD_REGISTER_KERNEL(set_value, float, phi::dtype::float16, int, - int64_t) {} + int64_t, + bool) {} PD_REGISTER_KERNEL(set_value_with_tensor, XPU, diff --git a/test/xpu/test_set_value_op_xpu.py b/test/xpu/test_set_value_op_xpu.py index 3bf665f76a3..1cc4bb4fcd4 100644 --- a/test/xpu/test_set_value_op_xpu.py +++ b/test/xpu/test_set_value_op_xpu.py @@ -57,6 +57,8 @@ class XPUTestSetValueOp(XPUOpTestWrapper): def set_dtype(self): self.dtype = self.in_type + if self.in_type == np.bool_: + self.dtype = "bool" def _call_setitem(self, x): x[0, 0] = self.value @@ -215,6 +217,8 @@ class XPUTestSetValueOp(XPUOpTestWrapper): def set_dtype(self): if self.in_type == np.float16: self.dtype = "float32" + elif self.in_type == np.bool_: + self.dtype = "bool" else: self.dtype = self.in_type @@ -308,6 +312,8 @@ class XPUTestSetValueOp(XPUOpTestWrapper): def set_dtype(self): if self.in_type == np.float16: self.dtype = "float32" + elif self.in_type == np.bool_: + self.dtype = "bool" else: self.dtype = self.in_type @@ -327,13 +333,9 @@ class XPUTestSetValueOp(XPUOpTestWrapper): def _get_answer(self): self.data[5:2:-1] = self.value - class XPUTestSetValueItemSliceNegetiveStep2(XPUTestSetValueApi): - def set_dtype(self): - if self.in_type == np.float16: - self.dtype = "float32" - else: - self.dtype = self.in_type - + class XPUTestSetValueItemSliceNegetiveStep2( + XPUTestSetValueItemSliceNegetiveStep + ): def set_shape(self): self.shape = [5] @@ -351,13 +353,9 @@ class XPUTestSetValueOp(XPUOpTestWrapper): def _get_answer(self): self.data[1::-1] = self.value - class XPUTestSetValueItemSliceNegetiveStep3(XPUTestSetValueApi): - def set_dtype(self): - if self.in_type == np.float16: - self.dtype = "float32" - else: - self.dtype = self.in_type - + class XPUTestSetValueItemSliceNegetiveStep3( + XPUTestSetValueItemSliceNegetiveStep + ): def set_shape(self): self.shape = [3] @@ -375,6 +373,14 @@ class XPUTestSetValueOp(XPUOpTestWrapper): self.data[::-1] = self.value class XPUTestSetValueItemSliceNegetiveStep4(XPUTestSetValueApi): + def set_dtype(self): + if self.in_type == np.float16: + self.dtype = "float32" + elif self.in_type == np.bool_: + self.dtype = "bool" + else: + self.dtype = self.in_type + def set_shape(self): self.shape = [3, 4, 5] @@ -395,6 +401,14 @@ class XPUTestSetValueOp(XPUOpTestWrapper): # 1.2.3 step < 0 and stride < -1 class XPUTestSetValueItemSliceNegetiveStep5(XPUTestSetValueApi): + def set_dtype(self): + if self.in_type == np.float16: + self.dtype = "float32" + elif self.in_type == np.bool_: + self.dtype = "bool" + else: + self.dtype = self.in_type + def set_shape(self): self.shape = [5, 5, 5] @@ -484,6 +498,8 @@ class XPUTestSetValueOp(XPUOpTestWrapper): def set_dtype(self): if self.in_type == np.float16: self.dtype = "float32" + elif self.in_type == np.bool_: + self.dtype = "bool" else: self.dtype = self.in_type @@ -499,13 +515,7 @@ class XPUTestSetValueOp(XPUOpTestWrapper): def _get_answer(self): self.data[0] = self.value - class XPUTestSetValueItemTensor2(XPUTestSetValueApi): - def set_dtype(self): - if self.in_type == np.float16: - self.dtype = "float32" - else: - self.dtype = self.in_type - + class XPUTestSetValueItemTensor2(XPUTestSetValueItemTensor): def _call_setitem(self, x): zero = paddle.full([1], 0, dtype="int32") two = paddle.full([1], 2, dtype="int64") @@ -520,13 +530,7 @@ class XPUTestSetValueOp(XPUOpTestWrapper): def _get_answer(self): self.data[0:2] = self.value - class XPUTestSetValueItemTensor3(XPUTestSetValueApi): - def set_dtype(self): - if self.in_type == np.float16: - self.dtype = "float32" - else: - self.dtype = self.in_type - + class XPUTestSetValueItemTensor3(XPUTestSetValueItemTensor): def _call_setitem(self, x): zero = paddle.full([1], 0, dtype="int32") two = paddle.full([1], 2, dtype="int64") @@ -543,13 +547,7 @@ class XPUTestSetValueOp(XPUOpTestWrapper): def _get_answer(self): self.data[0:-1, 0:2] = self.value - class XPUTestSetValueItemTensor4(XPUTestSetValueApi): - def set_dtype(self): - if self.in_type == np.float16: - self.dtype = "float32" - else: - self.dtype = self.in_type - + class XPUTestSetValueItemTensor4(XPUTestSetValueItemTensor): def _call_setitem(self, x): zero = paddle.full([1], 0, dtype="int32") two = paddle.full([1], 2, dtype="int64") @@ -566,13 +564,7 @@ class XPUTestSetValueOp(XPUOpTestWrapper): def _get_answer(self): self.data[0:-1, 0:2, ::2] = self.value - class XPUTestSetValueItemTensor5(XPUTestSetValueApi): - def set_dtype(self): - if self.in_type == np.float16: - self.dtype = "float32" - else: - self.dtype = self.in_type - + class XPUTestSetValueItemTensor5(XPUTestSetValueItemTensor): def _call_setitem(self, x): zero = paddle.full([1], 0, dtype="int32") two = paddle.full([1], 2, dtype="int64") @@ -591,13 +583,7 @@ class XPUTestSetValueOp(XPUOpTestWrapper): def _get_answer(self): self.data[0:, 1:2:2, :] = self.value - class XPUTestSetValueItemTensor6(XPUTestSetValueApi): - def set_dtype(self): - if self.in_type == np.float16: - self.dtype = "float32" - else: - self.dtype = self.in_type - + class XPUTestSetValueItemTensor6(XPUTestSetValueItemTensor): def set_shape(self): self.shape = [3, 4, 5] @@ -624,6 +610,8 @@ class XPUTestSetValueOp(XPUOpTestWrapper): def set_dtype(self): if self.in_type == np.float16: self.dtype = "float32" + elif self.in_type == np.bool_: + self.dtype = "bool" else: self.dtype = self.in_type @@ -637,13 +625,7 @@ class XPUTestSetValueOp(XPUOpTestWrapper): def _get_answer(self): self.data[None] = self.value - class XPUTestSetValueItemNone2(XPUTestSetValueApi): - def set_dtype(self): - if self.in_type == np.float16: - self.dtype = "float32" - else: - self.dtype = self.in_type - + class XPUTestSetValueItemNone2(XPUTestSetValueItemNone1): def _call_setitem(self, x): x[0, None, 1] = self.value @@ -654,13 +636,7 @@ class XPUTestSetValueOp(XPUOpTestWrapper): def _get_answer(self): self.data[0, None, 1] = self.value - class XPUTestSetValueItemNone3(XPUTestSetValueApi): - def set_dtype(self): - if self.in_type == np.float16: - self.dtype = "float32" - else: - self.dtype = self.in_type - + class XPUTestSetValueItemNone3(XPUTestSetValueItemNone1): def _call_setitem(self, x): x[:, None, None, 1] = self.value @@ -673,13 +649,7 @@ class XPUTestSetValueOp(XPUOpTestWrapper): def _get_answer(self): self.data[:, None, None, 1] = self.value - class XPUTestSetValueItemNone4(XPUTestSetValueApi): - def set_dtype(self): - if self.in_type == np.float16: - self.dtype = "float32" - else: - self.dtype = self.in_type - + class XPUTestSetValueItemNone4(XPUTestSetValueItemNone1): def _call_setitem(self, x): x[0, 0, None, 1] = self.value @@ -690,13 +660,7 @@ class XPUTestSetValueOp(XPUOpTestWrapper): def _get_answer(self): self.data[0, 0, None, 1] = self.value - class XPUTestSetValueItemNone5(XPUTestSetValueApi): - def set_dtype(self): - if self.in_type == np.float16: - self.dtype = "float32" - else: - self.dtype = self.in_type - + class XPUTestSetValueItemNone5(XPUTestSetValueItemNone1): def _call_setitem(self, x): x[0, None, 0, None, 1] = self.value @@ -707,13 +671,7 @@ class XPUTestSetValueOp(XPUOpTestWrapper): def _get_answer(self): self.data[0, None, 0, None, 1] = self.value - class XPUTestSetValueItemNone6(XPUTestSetValueApi): - def set_dtype(self): - if self.in_type == np.float16: - self.dtype = "float32" - else: - self.dtype = self.in_type - + class XPUTestSetValueItemNone6(XPUTestSetValueItemNone1): def _call_setitem(self, x): x[None, 0, 0, None, 0] = self.value @@ -724,13 +682,7 @@ class XPUTestSetValueOp(XPUOpTestWrapper): def _get_answer(self): self.data[None, 0, 0, None, 0] = self.value - class XPUTestSetValueItemNone7(XPUTestSetValueApi): - def set_dtype(self): - if self.in_type == np.float16: - self.dtype = "float32" - else: - self.dtype = self.in_type - + class XPUTestSetValueItemNone7(XPUTestSetValueItemNone1): def _call_setitem(self, x): x[:, None, 1] = np.zeros(self.shape)[:, None, 0] @@ -745,13 +697,7 @@ class XPUTestSetValueOp(XPUOpTestWrapper): def _get_answer(self): self.data[:, None, 1] = np.zeros(self.shape)[:, None, 0] - class XPUTestSetValueItemNone8(XPUTestSetValueApi): - def set_dtype(self): - if self.in_type == np.float16: - self.dtype = "float32" - else: - self.dtype = self.in_type - + class XPUTestSetValueItemNone8(XPUTestSetValueItemNone1): def _call_setitem(self, x): x[:, 1, None] = np.zeros(self.shape)[:, 0, None] @@ -766,13 +712,7 @@ class XPUTestSetValueOp(XPUOpTestWrapper): def _get_answer(self): self.data[:, 1, None] = np.zeros(self.shape)[:, 0, None] - class XPUTestSetValueItemNone9(XPUTestSetValueApi): - def set_dtype(self): - if self.in_type == np.float16: - self.dtype = "float32" - else: - self.dtype = self.in_type - + class XPUTestSetValueItemNone9(XPUTestSetValueItemNone1): def _call_setitem(self, x): x[None, :, 1, ..., None] = np.zeros(self.shape)[0, 0, :, None] @@ -789,13 +729,7 @@ class XPUTestSetValueOp(XPUOpTestWrapper): 0, 0, :, None ] - class XPUTestSetValueItemNone10(XPUTestSetValueApi): - def set_dtype(self): - if self.in_type == np.float16: - self.dtype = "float32" - else: - self.dtype = self.in_type - + class XPUTestSetValueItemNone10(XPUTestSetValueItemNone1): def _call_setitem(self, x): x[..., None, :, None] = np.zeros(self.shape)[..., None, :, None] @@ -815,10 +749,7 @@ class XPUTestSetValueOp(XPUOpTestWrapper): # 1.6 item is list or Tensor of bol class XPUTestSetValueItemBool1(XPUTestSetValueApi): def set_dtype(self): - if self.in_type == np.float16: - self.dtype = "float32" - else: - self.dtype = self.in_type + self.dtype = "float32" def _call_setitem(self, x): x[[True, False]] = self.value @@ -832,10 +763,7 @@ class XPUTestSetValueOp(XPUOpTestWrapper): class XPUTestSetValueItemBool2(XPUTestSetValueApi): def set_dtype(self): - if self.in_type == np.float16: - self.dtype = "float32" - else: - self.dtype = self.in_type + self.dtype = "float32" def _call_setitem(self, x): x[[False, False]] = self.value @@ -849,10 +777,7 @@ class XPUTestSetValueOp(XPUOpTestWrapper): class XPUTestSetValueItemBool3(XPUTestSetValueApi): def set_dtype(self): - if self.in_type == np.float16: - self.dtype = "float32" - else: - self.dtype = self.in_type + self.dtype = "float32" def _call_setitem(self, x): x[[False, True]] = np.zeros(self.shape[2]) @@ -866,10 +791,7 @@ class XPUTestSetValueOp(XPUOpTestWrapper): class XPUTestSetValueItemBool4(XPUTestSetValueApi): def set_dtype(self): - if self.in_type == np.float16: - self.dtype = "float32" - else: - self.dtype = self.in_type + self.dtype = "float32" def _call_setitem(self, x): idx = paddle.assign(np.array([False, True])) @@ -885,10 +807,7 @@ class XPUTestSetValueOp(XPUOpTestWrapper): class XPUTestSetValueItemBool5(XPUTestSetValueApi): def set_dtype(self): - if self.in_type == np.float16: - self.dtype = "float32" - else: - self.dtype = self.in_type + self.dtype = "float32" def _call_setitem(self, x): idx = paddle.assign( @@ -910,10 +829,7 @@ class XPUTestSetValueOp(XPUOpTestWrapper): class XPUTestSetValueItemBool6(XPUTestSetValueApi): def set_dtype(self): - if self.in_type == np.float16: - self.dtype = "float32" - else: - self.dtype = self.in_type + self.dtype = "float32" def _call_setitem(self, x): x[0, ...] = 0 @@ -1047,6 +963,8 @@ class XPUTestSetValueOp(XPUOpTestWrapper): def set_dtype(self): if self.in_type == np.float16: self.dtype = "float32" + elif self.in_type == np.bool_: + self.dtype = "bool" else: self.dtype = self.in_type @@ -1063,13 +981,7 @@ class XPUTestSetValueOp(XPUOpTestWrapper): def _get_answer(self): self.data[0] = self.value - class XPUTestSetValueValueShape2(XPUTestSetValueApi): - def set_dtype(self): - if self.in_type == np.float16: - self.dtype = "float32" - else: - self.dtype = self.in_type - + class XPUTestSetValueValueShape2(XPUTestSetValueValueShape1): def set_value(self): self.value = np.array([[3, 4, 5, 6]]) # shape is (1,4) @@ -1083,13 +995,7 @@ class XPUTestSetValueOp(XPUOpTestWrapper): def _get_answer(self): self.data[0:1] = self.value - class XPUTestSetValueValueShape3(XPUTestSetValueApi): - def set_dtype(self): - if self.in_type == np.float16: - self.dtype = "float32" - else: - self.dtype = self.in_type - + class XPUTestSetValueValueShape3(XPUTestSetValueValueShape1): def set_value(self): self.value = np.array( [[1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3]] @@ -1105,13 +1011,7 @@ class XPUTestSetValueOp(XPUOpTestWrapper): def _get_answer(self): self.data[0] = self.value - class XPUTestSetValueValueShape4(XPUTestSetValueApi): - def set_dtype(self): - if self.in_type == np.float16: - self.dtype = "float32" - else: - self.dtype = self.in_type - + class XPUTestSetValueValueShape4(XPUTestSetValueValueShape1): def set_value(self): self.value = np.array( [[1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3]] @@ -1129,13 +1029,7 @@ class XPUTestSetValueOp(XPUOpTestWrapper): def _get_answer(self): self.data[0] = self.value - class XPUTestSetValueValueShape5(XPUTestSetValueApi): - def set_dtype(self): - if self.in_type == np.float16: - self.dtype = "float32" - else: - self.dtype = self.in_type - + class XPUTestSetValueValueShape5(XPUTestSetValueValueShape1): def set_value(self): self.value = np.array([3, 3, 3]).astype(self.dtype) -- GitLab