From c52c91d1a1a3395953c2ee157feb9001b0425af4 Mon Sep 17 00:00:00 2001 From: YibinLiu666 <2632839426@qq.com> Date: Fri, 24 Nov 2023 07:54:34 +0000 Subject: [PATCH 01/12] support select_scatter op --- paddle/fluid/primitive/primitive.yaml | 1 + paddle/phi/api/ext/tensor_compat.h | 1 + paddle/phi/api/yaml/backward.yaml | 10 + paddle/phi/api/yaml/op_compat.yaml | 9 + paddle/phi/api/yaml/ops.yaml | 11 + .../kernels/cpu/select_scatter_grad_kernel.cc | 87 +++++++ .../phi/kernels/cpu/select_scatter_kernel.cc | 78 +++++++ .../kernels/gpu/select_scatter_grad_kernel.cu | 121 ++++++++++ .../phi/kernels/gpu/select_scatter_kernel.cu | 88 +++++++ .../phi/kernels/select_scatter_grad_kernel.h | 34 +++ paddle/phi/kernels/select_scatter_kernel.h | 31 +++ python/paddle/__init__.py | 2 + python/paddle/tensor/__init__.py | 2 + python/paddle/tensor/manipulation.py | 101 ++++++++ test/legacy_test/test_select_scatter_op.py | 217 ++++++++++++++++++ test/white_list/pir_op_test_white_list | 1 + 16 files changed, 794 insertions(+) create mode 100644 paddle/phi/kernels/cpu/select_scatter_grad_kernel.cc create mode 100644 paddle/phi/kernels/cpu/select_scatter_kernel.cc create mode 100644 paddle/phi/kernels/gpu/select_scatter_grad_kernel.cu create mode 100644 paddle/phi/kernels/gpu/select_scatter_kernel.cu create mode 100644 paddle/phi/kernels/select_scatter_grad_kernel.h create mode 100644 paddle/phi/kernels/select_scatter_kernel.h create mode 100644 test/legacy_test/test_select_scatter_op.py diff --git a/paddle/fluid/primitive/primitive.yaml b/paddle/fluid/primitive/primitive.yaml index 85ffc28a20d20..8daac0cb3d24e 100644 --- a/paddle/fluid/primitive/primitive.yaml +++ b/paddle/fluid/primitive/primitive.yaml @@ -52,3 +52,4 @@ - cast - sign - slice +- select_scatter diff --git a/paddle/phi/api/ext/tensor_compat.h b/paddle/phi/api/ext/tensor_compat.h index 202876d4dc698..ec6b297d961a8 100644 --- a/paddle/phi/api/ext/tensor_compat.h +++ b/paddle/phi/api/ext/tensor_compat.h @@ -131,6 +131,7 @@ using experimental::round; using experimental::rsqrt; using experimental::scatter; using experimental::scatter_nd_add; +using experimental::select_scatter; using experimental::selu; using experimental::send_u_recv; using experimental::send_ue_recv; diff --git a/paddle/phi/api/yaml/backward.yaml b/paddle/phi/api/yaml/backward.yaml index af49d76cfb2ff..fdefcee997735 100644 --- a/paddle/phi/api/yaml/backward.yaml +++ b/paddle/phi/api/yaml/backward.yaml @@ -1965,6 +1965,16 @@ data_type : out_grad optional : summed_ids +- backward_op : select_scatter_grad + forward : select_scatter (Tensor src, Tensor values, int axis, int index) -> Tensor(out) + args : (Tensor src, Tensor values, Tensor out_grad, int axis, int index) + output : Tensor(src_grad), Tensor(values_grad) + infer_meta : + func : GeneralBinaryGradInferMeta + param : [src, values] + kernel : + func : select_scatter_grad + - backward_op : selu_grad forward : selu (Tensor x, float scale=1.0507009873554804934193349852946, float alpha=1.6732632423543772848170429916717) -> Tensor(out) args : (Tensor out, Tensor out_grad, float scale, float alpha) diff --git a/paddle/phi/api/yaml/op_compat.yaml b/paddle/phi/api/yaml/op_compat.yaml index 70805002ec3a5..7018047946660 100755 --- a/paddle/phi/api/yaml/op_compat.yaml +++ b/paddle/phi/api/yaml/op_compat.yaml @@ -2647,6 +2647,15 @@ outputs : {out : Out, summed_ids : SummedIds} +- op : select_scatter + backward : select_scatter_grad + inputs : + {src : Src, values : Values} + outputs : + out : Result + attrs : + {axis : Axis, index : Index} + - op : self_dp_attention inputs : x : X diff --git a/paddle/phi/api/yaml/ops.yaml b/paddle/phi/api/yaml/ops.yaml index ebb21fbeb195d..d14d86564d458 100644 --- a/paddle/phi/api/yaml/ops.yaml +++ b/paddle/phi/api/yaml/ops.yaml @@ -2254,6 +2254,17 @@ intermediate : summed_ids backward : segment_pool_grad +- op : select_scatter + args : (Tensor src, Tensor values, int axis, int index) + output : Tensor(out) + infer_meta : + func : UnchangedInferMeta + param : [src] + kernel : + func : select_scatter + data_type : src + backward : select_scatter_grad + - op : selu args : (Tensor x, float scale=1.0507009873554804934193349852946, float alpha=1.6732632423543772848170429916717) output : Tensor diff --git a/paddle/phi/kernels/cpu/select_scatter_grad_kernel.cc b/paddle/phi/kernels/cpu/select_scatter_grad_kernel.cc new file mode 100644 index 0000000000000..96b36fb460e48 --- /dev/null +++ b/paddle/phi/kernels/cpu/select_scatter_grad_kernel.cc @@ -0,0 +1,87 @@ +// Copyright (c) 2022 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 License. + +#include "paddle/phi/kernels/select_scatter_grad_kernel.h" + +#include "paddle/phi/backends/cpu/cpu_context.h" +#include "paddle/phi/common/data_type.h" +#include "paddle/phi/common/place.h" +#include "paddle/phi/core/kernel_registry.h" +#include "paddle/phi/core/tensor_utils.h" + +namespace phi { + +template +void SelectScatterGradKernel(const Context& dev_ctx, + const DenseTensor& src, + const DenseTensor& values, + const DenseTensor& out_grad, + int axis, + int index, + DenseTensor* src_grad, + DenseTensor* value_grad) { + PADDLE_ENFORCE_EQ(dev_ctx.GetPlace().GetType() == phi::AllocationType::CPU, + true, + errors::PreconditionNotMet( + "SelectScatterGradOpKernel only runs on CPU.")); + if (!src_grad && !value_grad) return; + auto* out_grad_data = out_grad.data(); + auto src_dims = out_grad.dims(); + int64_t select_index_size = src_dims[axis]; + int64_t inner_dim_size = 1; + int64_t outer_dim_size = 1; + for (int i = 0; i < axis; i++) { + inner_dim_size *= src_dims[i]; + } + + for (int i = axis + 1; i < src_dims.size(); i++) { + outer_dim_size *= src_dims[i]; + } + if (src_grad) { + phi::Copy(dev_ctx, out_grad, dev_ctx.GetPlace(), false, src_grad); + auto* src_grad_data = src_grad->data(); + int64_t src_offset = 0; + for (int i = 0; i < inner_dim_size; i++) { + src_offset = + i * select_index_size * outer_dim_size + index * outer_dim_size; + memset(src_grad_data + src_offset, 0, sizeof(T) * outer_dim_size); + } + } + if (value_grad) { + value_grad->Resize(values.dims()); + dev_ctx.template Alloc(value_grad); + auto* values_grad_data = value_grad->data(); + int64_t values_offset = 0, src_offset = 0; + for (int i = 0; i < inner_dim_size; i++) { + src_offset = + i * select_index_size * outer_dim_size + index * outer_dim_size; + values_offset = i * outer_dim_size; + memcpy(values_grad_data + values_offset, + out_grad_data + src_offset, + sizeof(T) * outer_dim_size); + } + } +} + +} // namespace phi + +PD_REGISTER_KERNEL(select_scatter_grad, + CPU, + ALL_LAYOUT, + phi::SelectScatterGradKernel, + float, + double, + int, + uint8_t, + int64_t) {} diff --git a/paddle/phi/kernels/cpu/select_scatter_kernel.cc b/paddle/phi/kernels/cpu/select_scatter_kernel.cc new file mode 100644 index 0000000000000..4e62dd96a2d71 --- /dev/null +++ b/paddle/phi/kernels/cpu/select_scatter_kernel.cc @@ -0,0 +1,78 @@ +// Copyright (c) 2022 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 License. + +#include "paddle/phi/kernels/select_scatter_kernel.h" +#include "glog/logging.h" +#include "paddle/phi/backends/cpu/cpu_context.h" +#include "paddle/phi/common/data_type.h" +#include "paddle/phi/common/place.h" +#include "paddle/phi/core/kernel_registry.h" +#include "paddle/phi/core/tensor_utils.h" + +namespace phi { + +template +void SelectScatterKernel(const Context& dev_ctx, + const DenseTensor& src, + const DenseTensor& values, + int axis, + int index, + DenseTensor* out) { + PADDLE_ENFORCE_EQ( + dev_ctx.GetPlace().GetType() == phi::AllocationType::CPU, + true, + errors::PreconditionNotMet("SelectScatterKernel only runs on CPU.")); + phi::Copy(dev_ctx, src, dev_ctx.GetPlace(), false, out); + auto* values_data = values.data(); + auto* out_data = out->data(); + int64_t src_size = src.numel(); + int64_t values_size = values.numel(); + auto src_dims = src.dims(); + if (src_size == 0 || values_size == 0) { + VLOG(3) << "zero size input found"; + phi::errors::InvalidArgument("src_size, values_size cannot be 0"); + return; + } + int64_t select_index_size = src_dims[axis]; + int64_t inner_dim_size = 1; + int64_t outer_dim_size = 1; + for (int i = 0; i < axis; i++) { + inner_dim_size *= src_dims[i]; + } + + for (int i = axis + 1; i < src_dims.size(); i++) { + outer_dim_size *= src_dims[i]; + } + int64_t src_offset = 0, values_offset = 0; + for (int i = 0; i < inner_dim_size; i++) { + src_offset = + i * select_index_size * outer_dim_size + index * outer_dim_size; + values_offset = i * outer_dim_size; + memcpy(out_data + src_offset, + values_data + values_offset, + sizeof(T) * outer_dim_size); + } +} + +} // namespace phi + +PD_REGISTER_KERNEL(select_scatter, + CPU, + ALL_LAYOUT, + phi::SelectScatterKernel, + float, + double, + int, + uint8_t, + int64_t) {} diff --git a/paddle/phi/kernels/gpu/select_scatter_grad_kernel.cu b/paddle/phi/kernels/gpu/select_scatter_grad_kernel.cu new file mode 100644 index 0000000000000..1b7ebd2543eea --- /dev/null +++ b/paddle/phi/kernels/gpu/select_scatter_grad_kernel.cu @@ -0,0 +1,121 @@ +// Copyright (c) 2022 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 License. + +#include "paddle/phi/kernels/select_scatter_grad_kernel.h" + +#include "paddle/phi/backends/gpu/gpu_context.h" +#include "paddle/phi/common/place.h" +#include "paddle/phi/core/kernel_registry.h" +#include "paddle/phi/core/tensor_utils.h" +#include "paddle/phi/core/utils/data_type.h" + +namespace phi { + +template +__global__ void SelectScatterSrcGradGPUKernel(T* src_grad_data, + int index, + int64_t select_index_size, + int64_t outer_dim_size, + int64_t numel) { + int tid = threadIdx.x + blockIdx.x * blockDim.x; + if (tid >= numel) return; + int64_t i, j; + i = tid / outer_dim_size; + j = tid % outer_dim_size; + int64_t src_offset = + i * select_index_size * outer_dim_size + index * outer_dim_size + j; + src_grad_data[src_offset] = 0; +} + +template +__global__ void SelectScatterValuesGradGPUKernel(T* values_grad_data, + const T* out_grad_data, + int index, + int64_t select_index_size, + int64_t outer_dim_size, + int64_t numel) { + int tid = threadIdx.x + blockIdx.x * blockDim.x; + if (tid >= numel) return; + int64_t i, j; + i = tid / outer_dim_size; + j = tid % outer_dim_size; + int64_t src_offset = + i * select_index_size * outer_dim_size + index * outer_dim_size + j; + int64_t values_offset = i * outer_dim_size + j; + values_grad_data[values_offset] = out_grad_data[src_offset]; +} + +template +void SelectScatterGradKernel(const Context& dev_ctx, + const DenseTensor& src, + const DenseTensor& values, + const DenseTensor& out_grad, + int axis, + int index, + DenseTensor* src_grad, + DenseTensor* value_grad) { + PADDLE_ENFORCE_EQ(dev_ctx.GetPlace().GetType() == phi::AllocationType::GPU, + true, + errors::PreconditionNotMet( + "SelectScatterGradOpCUDAKernel only runs on GPU.")); + + if (!src_grad && !value_grad) return; + auto* out_grad_data = out_grad.data(); + auto src_dims = out_grad.dims(); + int64_t select_index_size = src_dims[axis]; + int64_t inner_dim_size = 1; + int64_t outer_dim_size = 1; + for (int i = 0; i < axis; i++) { + inner_dim_size *= src_dims[i]; + } + + for (int i = axis + 1; i < src_dims.size(); i++) { + outer_dim_size *= src_dims[i]; + } + int block = 512; + int64_t n = inner_dim_size * outer_dim_size; + int64_t grid = (n + block - 1) / block; + auto stream = reinterpret_cast(dev_ctx).stream(); + if (src_grad) { + phi::Copy(dev_ctx, out_grad, dev_ctx.GetPlace(), false, src_grad); + auto* src_grad_data = src_grad->data(); + SelectScatterSrcGradGPUKernel<<>>( + src_grad_data, index, select_index_size, outer_dim_size, n); + } + if (value_grad) { + value_grad->Resize(values.dims()); + dev_ctx.template Alloc(value_grad); + auto* value_grad_data = value_grad->data(); + SelectScatterValuesGradGPUKernel + <<>>(value_grad_data, + out_grad_data, + index, + select_index_size, + outer_dim_size, + n); + } +} + +} // namespace phi + +PD_REGISTER_KERNEL(select_scatter_grad, + GPU, + ALL_LAYOUT, + phi::SelectScatterGradKernel, + float, + double, + int64_t, + int, + phi::dtype::float16, + phi::dtype::bfloat16) {} diff --git a/paddle/phi/kernels/gpu/select_scatter_kernel.cu b/paddle/phi/kernels/gpu/select_scatter_kernel.cu new file mode 100644 index 0000000000000..9147929b6e0c1 --- /dev/null +++ b/paddle/phi/kernels/gpu/select_scatter_kernel.cu @@ -0,0 +1,88 @@ +// Copyright (c) 2022 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 License. + +#include "paddle/phi/kernels/select_scatter_kernel.h" + +#include "paddle/phi/backends/gpu/gpu_context.h" +#include "paddle/phi/common/place.h" +#include "paddle/phi/core/kernel_registry.h" +#include "paddle/phi/core/tensor_utils.h" +#include "paddle/phi/core/utils/data_type.h" + +namespace phi { +template +__global__ void SelectScatterGPUKernel(T* out_data, + const T* values_data, + int index, + int64_t select_index_size, + int64_t outer_dim_size, + int64_t numel) { + int tid = threadIdx.x + blockIdx.x * blockDim.x; + if (tid >= numel) return; + int64_t i, j; + i = tid / outer_dim_size; + j = tid % outer_dim_size; + int64_t src_offset = + i * select_index_size * outer_dim_size + index * outer_dim_size + j; + int64_t values_offset = i * outer_dim_size + j; + out_data[src_offset] = values_data[values_offset]; +} + +template +void SelectScatterKernel(const Context& dev_ctx, + const DenseTensor& src, + const DenseTensor& values, + int axis, + int index, + DenseTensor* out) { + PADDLE_ENFORCE_EQ(dev_ctx.GetPlace().GetType() == phi::AllocationType::GPU, + true, + errors::PreconditionNotMet( + "SelectScatterCUDAKernel only runs on GPU device.")); + + phi::Copy(dev_ctx, src, dev_ctx.GetPlace(), false, out); + auto* values_data = values.data(); + auto* out_data = out->data(); + int64_t src_size = src.numel(); + int64_t values_size = values.numel(); + auto src_dims = src.dims(); + int64_t select_index_size = src_dims[axis]; + int64_t inner_dim_size = 1; + int64_t outer_dim_size = 1; + for (int i = 0; i < axis; i++) { + inner_dim_size *= src_dims[i]; + } + + for (int i = axis + 1; i < src_dims.size(); i++) { + outer_dim_size *= src_dims[i]; + } + int block = 512; + int64_t n = inner_dim_size * outer_dim_size; + int64_t grid = (n + block - 1) / block; + auto stream = reinterpret_cast(dev_ctx).stream(); + SelectScatterGPUKernel<<>>( + out_data, values_data, index, select_index_size, outer_dim_size, n); +} +} // namespace phi + +PD_REGISTER_KERNEL(select_scatter, + GPU, + ALL_LAYOUT, + phi::SelectScatterKernel, + float, + double, + int64_t, + int, + phi::dtype::float16, + phi::dtype::bfloat16) {} diff --git a/paddle/phi/kernels/select_scatter_grad_kernel.h b/paddle/phi/kernels/select_scatter_grad_kernel.h new file mode 100644 index 0000000000000..18bdfda6d7391 --- /dev/null +++ b/paddle/phi/kernels/select_scatter_grad_kernel.h @@ -0,0 +1,34 @@ +// Copyright (c) 2022 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 License. + +#pragma once + +#include +#include + +#include "paddle/phi/core/dense_tensor.h" + +namespace phi { + +template +void SelectScatterGradKernel(const Context& dev_ctx, + const DenseTensor& src, + const DenseTensor& values, + const DenseTensor& out_grad, + int axis, + int index, + DenseTensor* src_grad, + DenseTensor* value_grad); + +} // namespace phi diff --git a/paddle/phi/kernels/select_scatter_kernel.h b/paddle/phi/kernels/select_scatter_kernel.h new file mode 100644 index 0000000000000..adcbfeb7d70bb --- /dev/null +++ b/paddle/phi/kernels/select_scatter_kernel.h @@ -0,0 +1,31 @@ +// Copyright (c) 2022 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 License. + +#pragma once + +#include +#include +#include "paddle/phi/core/dense_tensor.h" + +namespace phi { + +template +void SelectScatterKernel(const Context& dev_ctx, + const DenseTensor& src, + const DenseTensor& values, + int axis, + int index, + DenseTensor* out); + +} // namespace phi diff --git a/python/paddle/__init__.py b/python/paddle/__init__.py index 5e87e9514c0e9..233e2a56ebdba 100644 --- a/python/paddle/__init__.py +++ b/python/paddle/__init__.py @@ -242,6 +242,7 @@ tolist, take_along_axis, put_along_axis, + select_scatter, tensordot, as_complex, as_real, @@ -891,6 +892,7 @@ 'renorm_', 'take_along_axis', 'put_along_axis', + 'select_scatter', 'multigammaln', 'multigammaln_', 'nan_to_num', diff --git a/python/paddle/tensor/__init__.py b/python/paddle/tensor/__init__.py index b96045d35faf6..4af305d4df1b3 100644 --- a/python/paddle/tensor/__init__.py +++ b/python/paddle/tensor/__init__.py @@ -156,6 +156,7 @@ from .manipulation import as_complex # noqa: F401 from .manipulation import take_along_axis # noqa: F401 from .manipulation import put_along_axis # noqa: F401 +from .manipulation import select_scatter # noqa: F401 from .manipulation import put_along_axis_ # noqa: F401 from .manipulation import as_real # noqa: F401 from .manipulation import moveaxis # noqa: F401 @@ -677,6 +678,7 @@ 'repeat_interleave', 'take_along_axis', 'put_along_axis', + 'select_scatter', 'put_along_axis_', 'exponential_', 'heaviside', diff --git a/python/paddle/tensor/manipulation.py b/python/paddle/tensor/manipulation.py index e6d52b7802c91..232086a3e87b6 100644 --- a/python/paddle/tensor/manipulation.py +++ b/python/paddle/tensor/manipulation.py @@ -5898,3 +5898,104 @@ def diagonal_scatter(x, y, offset=0, axis1=0, axis2=1, name=None): """ return fill_diagonal_tensor(x, y, offset, axis1, axis2, name) + + +def select_scatter(src, values, axis, index): + """ + Embeds the values of the values tensor into src at the given index of axis. + + Args: + src (Tensor) : The Destination Tensor. + values (Tensor) : The tensor to embed into src. + axis (int) : the dimension to insert the slice into. + index (int) : the index to select with. + + Returns: + Tensor, same dtype and shape with src + + Examples: + .. code-block:: python + + >>> import paddle + + >>> x = paddle.zeros((2,3,4)).astype("float32") + >>> values = paddle.ones((2,4)).astype("float32") + >>> res = paddle.select_scatter(x,values,1,1) + >>> print(res) + Tensor(shape=[2, 3, 4], dtype=float32, place=Place(cpu), stop_gradient=True, + [[[0., 0., 0., 0.], + [1., 1., 1., 1.], + [0., 0., 0., 0.]], + + [[0., 0., 0., 0.], + [1., 1., 1., 1.], + [0., 0., 0., 0.]]]) + + """ + if src.dtype != values.dtype: + raise TypeError("the dtype of src and value must be equal.") + src_shape = src.shape + value_shape = values.shape + if not isinstance(src_shape, list): + src_shape = list(src_shape) + if index < 0: + index += src_shape[axis] + if axis < 0: + axis += len(src_shape) + del src_shape[axis] + if len(src_shape) != len(value_shape): + raise RuntimeError( + "expected values to have a size equal to the slice of src. value size = " + + str(value_shape) + + " slice size = " + + str(src_shape) + ) + for i in range(len(src_shape)): + if src_shape[i] != value_shape[i]: + raise RuntimeError( + "expected values to have a size equal to the slice of src. value size = " + + str(value_shape) + + " slice size = " + + str(src_shape) + ) + if in_dynamic_or_pir_mode(): + return _C_ops.select_scatter(src, values, axis, index) + else: + check_variable_and_dtype( + src, + 'src', + [ + 'float16', + 'float32', + 'float64', + 'int32', + 'int64', + 'uint8', + 'uint16', + ], + 'select_scatter', + ) + check_variable_and_dtype( + values, + 'values', + [ + 'float16', + 'float32', + 'float64', + 'int32', + 'int64', + 'uint8', + 'uint16', + ], + 'select_scatter', + ) + helper = LayerHelper('select_scatter', **locals()) + dtype = helper.input_dtype() + result = helper.create_variable_for_type_inference(dtype) + helper.append_op( + type="select_scatter", + inputs={"Src": src, "Values": values}, + attrs={"Axis": axis, "Index": index}, + outputs={"Result": result}, + ) + return result diff --git a/test/legacy_test/test_select_scatter_op.py b/test/legacy_test/test_select_scatter_op.py new file mode 100644 index 0000000000000..867936410c0bb --- /dev/null +++ b/test/legacy_test/test_select_scatter_op.py @@ -0,0 +1,217 @@ +# 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 License. + +import copy +import unittest + +import numpy as np +from op_test import OpTest, convert_float_to_uint16 + +import paddle +from paddle.framework import core +from paddle.pir_utils import test_with_pir_api + +paddle.enable_static() + + +class TestSelectScatterOp(OpTest): + def setUp(self): + self.init_data() + self.op_type = "select_scatter" + self.python_api = paddle.tensor.select_scatter + self.xnp = np.random.random(self.x_shape).astype(self.x_type) + self.value_np = np.random.random(self.value_shape).astype( + self.value_type + ) + # numpy put_along_axis is an inplace operation. + self.target = copy.deepcopy(self.xnp) + for i in range(10): + for j in range(10): + self.target[i, self.index, j] = self.value_np[i, j] + self.inputs = { + 'Src': self.xnp, + 'Values': self.value_np, + } + self.attrs = {'Axis': self.axis, 'Index': self.index} + self.outputs = {'Result': self.target} + + def test_check_output(self): + self.check_output(check_pir=True) + + def test_check_grad(self): + self.check_grad(["Src", "Values"], "Result", check_pir=True) + + def init_data(self): + self.dtype = 'float64' + self.x_type = "float64" + self.x_shape = (10, 10, 10) + self.value_type = "float64" + self.value_shape = (10, 10) + self.axis = 1 + self.axis_type = "int64" + self.index = 1 + self.index_type = "int64" + + +class TestPutAlongAxisFP16Op(TestSelectScatterOp): + def init_data(self): + self.dtype = 'float16' + self.x_type = "float16" + self.x_shape = (10, 10, 10) + self.value_type = "float16" + self.value_shape = (10, 10) + self.axis = 1 + self.axis_type = "int64" + self.index = 1 + self.index_type = "int64" + + +@unittest.skipIf( + not core.is_compiled_with_cuda() + or not core.is_bfloat16_supported(core.CUDAPlace(0)), + "core is not complied with CUDA and not support the bfloat16", +) +class TestPutAlongAxisBF16Op(OpTest): + def setUp(self): + self.init_data() + self.op_type = "select_scatter" + self.python_api = paddle.tensor.select_scatter + self.xnp = np.random.random(self.x_shape).astype(self.x_type) + self.value_np = np.random.random(self.value_shape).astype( + self.value_type + ) + # numpy put_along_axis is an inplace operation. + self.target = copy.deepcopy(self.xnp) + for i in range(10): + for j in range(10): + self.target[i, self.index, j] = self.value_np[i, j] + self.inputs = { + 'Src': self.xnp, + 'Values': self.value_np, + } + self.attrs = {'Axis': self.axis, 'Index': self.index} + self.outputs = {'Result': self.target} + + self.inputs['Src'] = convert_float_to_uint16(self.inputs['Src']) + self.inputs['Values'] = convert_float_to_uint16(self.inputs['Values']) + self.outputs['Result'] = convert_float_to_uint16(self.outputs['Result']) + self.place = core.CUDAPlace(0) + + def test_check_output(self): + self.check_output_with_place(self.place, check_pir=True) + + def test_check_grad(self): + self.check_grad_with_place( + self.place, ["Src", "Values"], "Result", check_pir=True + ) + + def init_data(self): + self.dtype = np.uint16 + self.x_type = "float32" + self.x_shape = (10, 10, 10) + self.value_type = "float32" + self.value_shape = (10, 10) + self.axis = 1 + self.axis_type = "int64" + self.index = 1 + self.index_type = "int64" + + +class TestSelectScatterAPI(unittest.TestCase): + def setUp(self): + np.random.seed(0) + self.shape = [2, 3, 4] + self.x_np = np.random.random(self.shape).astype(np.float32) + self.place = [paddle.CPUPlace()] + self.axis = 1 + self.index = 1 + self.value_shape = [2, 4] + self.value_np = np.random.random(self.value_shape).astype(np.float32) + self.x_feed = copy.deepcopy(self.x_np) + if core.is_compiled_with_cuda(): + self.place.append(paddle.CUDAPlace(0)) + + @test_with_pir_api + def test_api_static(self): + paddle.enable_static() + + def run(place): + with paddle.static.program_guard(paddle.static.Program()): + x = paddle.static.data('Src', self.shape) + value = paddle.static.data('Values', self.value_shape) + out = paddle.select_scatter(x, value, self.axis, self.index) + exe = paddle.static.Executor(place) + res = exe.run( + feed={ + 'Src': self.x_feed, + 'Values': self.value_np, + }, + fetch_list=[out], + ) + + out_ref = copy.deepcopy(self.x_np) + for i in range(2): + for j in range(4): + out_ref[i, self.index, j] = self.value_np[i, j] + for out in res: + np.testing.assert_allclose(out, out_ref, rtol=0.001) + + for place in self.place: + run(place) + + def test_api_dygraph(self): + def run(place): + paddle.disable_static(place) + x_tensor = paddle.to_tensor(self.x_np) + value_tensor = paddle.to_tensor(self.value_np) + out = paddle.select_scatter( + x_tensor, value_tensor, self.axis, self.index + ) + out_ref = copy.deepcopy(self.x_np) + for i in range(2): + for j in range(4): + out_ref[i, self.index, j] = self.value_np[i, j] + np.testing.assert_allclose(out.numpy(), out_ref, rtol=0.001) + + paddle.enable_static() + + for place in self.place: + run(place) + + def test_error(self): + try: + x_tensor = paddle.to_tensor(self.x_np) + value_tensor = paddle.to_tensor(self.value_np).astype("int8") + res = paddle.select_scatter(x_tensor, value_tensor, 1, 1) + except Exception as error: + self.assertIsInstance(error, TypeError) + + try: + x_tensor = paddle.to_tensor(self.x_np) + value_tensor = paddle.to_tensor(self.value_np).reshape((2, 2, 2)) + res = paddle.select_scatter(x_tensor, value_tensor, 1, 1) + except Exception as error: + self.assertIsInstance(error, RuntimeError) + + try: + x_tensor = paddle.to_tensor(self.x_np) + value_tensor = paddle.to_tensor([[2, 2], [2, 2]]).astype(np.float32) + res = paddle.select_scatter(x_tensor, value_tensor, 1, 1) + except Exception as error: + self.assertIsInstance(error, RuntimeError) + + +if __name__ == "__main__": + paddle.enable_static() + unittest.main() diff --git a/test/white_list/pir_op_test_white_list b/test/white_list/pir_op_test_white_list index bbe46bef912ce..a491c06e7cf37 100644 --- a/test/white_list/pir_op_test_white_list +++ b/test/white_list/pir_op_test_white_list @@ -262,6 +262,7 @@ test_searchsorted_op test_seed_op test_segment_ops test_segment_ops_static_build +test_select_scatter_op test_selu_op test_sgd_op test_shape_mkldnn_op From 19c0ab46ae4903bfc5b3d251c8f116bda607ad21 Mon Sep 17 00:00:00 2001 From: YibinLiu666 <2632839426@qq.com> Date: Fri, 24 Nov 2023 14:10:40 +0000 Subject: [PATCH 02/12] fix example code --- python/paddle/tensor/manipulation.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/paddle/tensor/manipulation.py b/python/paddle/tensor/manipulation.py index 232086a3e87b6..79e713da24181 100644 --- a/python/paddle/tensor/manipulation.py +++ b/python/paddle/tensor/manipulation.py @@ -5927,7 +5927,7 @@ def select_scatter(src, values, axis, index): [1., 1., 1., 1.], [0., 0., 0., 0.]], - [[0., 0., 0., 0.], + [[0., 0., 0., 0.], [1., 1., 1., 1.], [0., 0., 0., 0.]]]) From 0816c09fc24fea6ca62a1f710902b88faf942d3b Mon Sep 17 00:00:00 2001 From: YibinLiu666 <2632839426@qq.com> Date: Sat, 25 Nov 2023 02:37:36 +0000 Subject: [PATCH 03/12] fix sc --- python/paddle/tensor/manipulation.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/python/paddle/tensor/manipulation.py b/python/paddle/tensor/manipulation.py index 79e713da24181..c276148faea90 100644 --- a/python/paddle/tensor/manipulation.py +++ b/python/paddle/tensor/manipulation.py @@ -5923,13 +5923,13 @@ def select_scatter(src, values, axis, index): >>> res = paddle.select_scatter(x,values,1,1) >>> print(res) Tensor(shape=[2, 3, 4], dtype=float32, place=Place(cpu), stop_gradient=True, - [[[0., 0., 0., 0.], - [1., 1., 1., 1.], - [0., 0., 0., 0.]], + [[[0., 0., 0., 0.], + [1., 1., 1., 1.], + [0., 0., 0., 0.]], - [[0., 0., 0., 0.], - [1., 1., 1., 1.], - [0., 0., 0., 0.]]]) + [[0., 0., 0., 0.], + [1., 1., 1., 1.], + [0., 0., 0., 0.]]]) """ if src.dtype != values.dtype: From b927bcf4eb8e70c1f45aebed919507ba56d504b3 Mon Sep 17 00:00:00 2001 From: YibinLiu666 <2632839426@qq.com> Date: Sat, 25 Nov 2023 04:45:34 +0000 Subject: [PATCH 04/12] update example --- python/paddle/tensor/manipulation.py | 1 - 1 file changed, 1 deletion(-) diff --git a/python/paddle/tensor/manipulation.py b/python/paddle/tensor/manipulation.py index c276148faea90..cae832cbf5277 100644 --- a/python/paddle/tensor/manipulation.py +++ b/python/paddle/tensor/manipulation.py @@ -5926,7 +5926,6 @@ def select_scatter(src, values, axis, index): [[[0., 0., 0., 0.], [1., 1., 1., 1.], [0., 0., 0., 0.]], - [[0., 0., 0., 0.], [1., 1., 1., 1.], [0., 0., 0., 0.]]]) From 3d1772c7d7425541e9fa2ad5a35704437ee42994 Mon Sep 17 00:00:00 2001 From: YibinLiu666 <2632839426@qq.com> Date: Thu, 30 Nov 2023 03:27:37 +0000 Subject: [PATCH 05/12] remove unused files --- .../phi/kernels/select_scatter_grad_kernel.h | 34 ------------------- paddle/phi/kernels/select_scatter_kernel.h | 31 ----------------- test/white_list/pir_op_test_white_list | 1 - 3 files changed, 66 deletions(-) delete mode 100644 paddle/phi/kernels/select_scatter_grad_kernel.h delete mode 100644 paddle/phi/kernels/select_scatter_kernel.h diff --git a/paddle/phi/kernels/select_scatter_grad_kernel.h b/paddle/phi/kernels/select_scatter_grad_kernel.h deleted file mode 100644 index 18bdfda6d7391..0000000000000 --- a/paddle/phi/kernels/select_scatter_grad_kernel.h +++ /dev/null @@ -1,34 +0,0 @@ -// Copyright (c) 2022 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 License. - -#pragma once - -#include -#include - -#include "paddle/phi/core/dense_tensor.h" - -namespace phi { - -template -void SelectScatterGradKernel(const Context& dev_ctx, - const DenseTensor& src, - const DenseTensor& values, - const DenseTensor& out_grad, - int axis, - int index, - DenseTensor* src_grad, - DenseTensor* value_grad); - -} // namespace phi diff --git a/paddle/phi/kernels/select_scatter_kernel.h b/paddle/phi/kernels/select_scatter_kernel.h deleted file mode 100644 index adcbfeb7d70bb..0000000000000 --- a/paddle/phi/kernels/select_scatter_kernel.h +++ /dev/null @@ -1,31 +0,0 @@ -// Copyright (c) 2022 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 License. - -#pragma once - -#include -#include -#include "paddle/phi/core/dense_tensor.h" - -namespace phi { - -template -void SelectScatterKernel(const Context& dev_ctx, - const DenseTensor& src, - const DenseTensor& values, - int axis, - int index, - DenseTensor* out); - -} // namespace phi diff --git a/test/white_list/pir_op_test_white_list b/test/white_list/pir_op_test_white_list index a491c06e7cf37..bbe46bef912ce 100644 --- a/test/white_list/pir_op_test_white_list +++ b/test/white_list/pir_op_test_white_list @@ -262,7 +262,6 @@ test_searchsorted_op test_seed_op test_segment_ops test_segment_ops_static_build -test_select_scatter_op test_selu_op test_sgd_op test_shape_mkldnn_op From aadc94823d7a4586b12fde69fe32b2f93ee63d66 Mon Sep 17 00:00:00 2001 From: YibinLiu666 <2632839426@qq.com> Date: Thu, 30 Nov 2023 03:31:32 +0000 Subject: [PATCH 06/12] add name --- python/paddle/tensor/manipulation.py | 49 ++++++++++++++-------------- 1 file changed, 25 insertions(+), 24 deletions(-) diff --git a/python/paddle/tensor/manipulation.py b/python/paddle/tensor/manipulation.py index b61dfe7df59f7..a27f745175ad7 100644 --- a/python/paddle/tensor/manipulation.py +++ b/python/paddle/tensor/manipulation.py @@ -5923,18 +5923,19 @@ def diagonal_scatter(x, y, offset=0, axis1=0, axis2=1, name=None): return fill_diagonal_tensor(x, y, offset, axis1, axis2, name) -def select_scatter(src, values, axis, index): +def select_scatter(x, values, axis, index, name=None): """ - Embeds the values of the values tensor into src at the given index of axis. + Embeds the values of the values tensor into x at the given index of axis. Args: - src (Tensor) : The Destination Tensor. - values (Tensor) : The tensor to embed into src. + x (Tensor) : The Destination Tensor. + values (Tensor) : The tensor to embed into x. axis (int) : the dimension to insert the slice into. index (int) : the index to select with. + name (str, optional): Name for the operation (optional, default is None). Returns: - Tensor, same dtype and shape with src + Tensor, same dtype and shape with x Examples: .. code-block:: python @@ -5954,29 +5955,29 @@ def select_scatter(src, values, axis, index): [0., 0., 0., 0.]]]) """ - src_shape = src.shape + x_shape = x.shape value_shape = values.shape - if not isinstance(src_shape, list): - src_shape = list(src_shape) + if not isinstance(x_shape, list): + x_shape = list(x_shape) if index < 0: - index += src_shape[axis] + index += x_shape[axis] if axis < 0: - axis += len(src_shape) - del src_shape[axis] - if len(src_shape) != len(value_shape): + axis += len(x_shape) + del x_shape[axis] + if len(x_shape) != len(value_shape): raise RuntimeError( - "expected values to have a size equal to the slice of src. value size = " + "expected values to have a size equal to the slice of x. value size = " + str(value_shape) + " slice size = " - + str(src_shape) + + str(x_shape) ) - for i in range(len(src_shape)): - if src_shape[i] != value_shape[i]: + for i in range(len(x_shape)): + if x_shape[i] != value_shape[i]: raise RuntimeError( - "expected values to have a size equal to the slice of src. value size = " + "expected values to have a size equal to the slice of x. value size = " + str(value_shape) + " slice size = " - + str(src_shape) + + str(x_shape) ) from ..base.framework import default_main_program @@ -5986,7 +5987,7 @@ def select_scatter(src, values, axis, index): axes = [axis] none_axes = [] decrease_axes = [axis] - inputs = {'Input': src} + inputs = {'Input': x} attrs = { 'axes': axes, 'starts': starts, @@ -6015,7 +6016,7 @@ def select_scatter(src, values, axis, index): del attrs['steps'] # step2. Parse values - dtype = src.dtype + dtype = x.dtype attrs['dtype'] = dtype values = values.astype(dtype) @@ -6023,7 +6024,7 @@ def select_scatter(src, values, axis, index): if in_dynamic_or_pir_mode(): return _C_ops.set_value_with_tensor( - src, + x, values, starts, ends, @@ -6037,10 +6038,10 @@ def select_scatter(src, values, axis, index): if helper.main_program.current_block_idx != 0: # not in global block, we should create a global variable. output = helper._create_global_variable_for_type_inference( - dtype=src.dtype + dtype=x.dtype ) else: - output = helper.create_variable_for_type_inference(dtype=src.dtype) + output = helper.create_variable_for_type_inference(dtype=x.dtype) cur_block = default_main_program().current_block() cur_block.append_op( type="set_value", @@ -6052,6 +6053,6 @@ def select_scatter(src, values, axis, index): # map var to the new output paddle.jit.api.ProgramTranslator.get_instance()._inplace_map.add( - cur_block.program, src.desc.id(), output + cur_block.program, x.desc.id(), output ) return output From 131d251a69a5f1b53cade037f5e85d41cc76761d Mon Sep 17 00:00:00 2001 From: YibinLiu666 <2632839426@qq.com> Date: Thu, 30 Nov 2023 08:14:59 +0000 Subject: [PATCH 07/12] fix conflict --- python/paddle/tensor/__init__.py | 768 ++++++++++++++++--------------- 1 file changed, 398 insertions(+), 370 deletions(-) diff --git a/python/paddle/tensor/__init__.py b/python/paddle/tensor/__init__.py index 23f8fa863f91f..e799fe19e55b6 100644 --- a/python/paddle/tensor/__init__.py +++ b/python/paddle/tensor/__init__.py @@ -12,377 +12,405 @@ # See the License for the specific language governing permissions and # limitations under the License. -from .attribute import is_complex # noqa: F401 -from .attribute import is_integer # noqa: F401 -from .attribute import rank # noqa: F401 -from .attribute import shape # noqa: F401 -from .attribute import real # noqa: F401 -from .attribute import imag # noqa: F401 -from .attribute import is_floating_point # noqa: F401 -from .creation import create_parameter # noqa: F401 -from .creation import create_tensor # noqa: F401 -from .creation import to_tensor # noqa: F401 -from .creation import diag # noqa: F401 -from .creation import diagflat # noqa: F401 -from .creation import diag_embed # noqa: F401 -from .creation import eye # noqa: F401 -from .creation import linspace # noqa: F401 -from .creation import fill_constant # noqa: F401 -from .creation import ones # noqa: F401 -from .creation import ones_like # noqa: F401 -from .creation import zeros # noqa: F401 -from .creation import zeros_like # noqa: F401 -from .creation import arange # noqa: F401 -from .creation import full # noqa: F401 -from .creation import full_like # noqa: F401 -from .creation import triu # noqa: F401 -from .creation import triu_ # noqa: F401 -from .creation import tril # noqa: F401 -from .creation import tril_ # noqa: F401 -from .creation import meshgrid # noqa: F401 -from .creation import empty # noqa: F401 -from .creation import empty_like # noqa: F401 -from .creation import complex # noqa: F401 -from .creation import polar # noqa: F401 -from .creation import cauchy_ # noqa: F401 -from .creation import geometric_ # noqa: F401 -from .linalg import matmul # noqa: F401 -from .linalg import dot # noqa: F401 -from .linalg import cov # noqa: F401 -from .linalg import corrcoef # noqa: F401 -from .linalg import norm # noqa: F401 -from .linalg import pca_lowrank # noqa: F401 -from .linalg import cond # noqa: F401 -from .linalg import transpose # noqa: F401 -from .linalg import transpose_ # noqa: F401 -from .linalg import lstsq # noqa: F401 -from .linalg import dist # noqa: F401 -from .linalg import t # noqa: F401 -from .linalg import t_ # noqa: F401 -from .linalg import cross # noqa: F401 -from .linalg import cholesky # noqa: F401 -from .linalg import bmm # noqa: F401 -from .linalg import histogram # noqa: F401 -from .linalg import bincount # noqa: F401 -from .linalg import mv # noqa: F401 -from .linalg import eig # noqa: F401 -from .linalg import matrix_power # noqa: F401 -from .linalg import qr # noqa: F401 -from .linalg import eigvals # noqa: F401 -from .linalg import multi_dot # noqa: F401 -from .linalg import svd # noqa: F401 -from .linalg import eigvalsh # noqa: F401 -from .linalg import eigh # noqa: F401 -from .linalg import pinv # noqa: F401 -from .linalg import solve # noqa: F401 -from .linalg import cholesky_solve # noqa: F401 -from .linalg import lu # noqa: F401 -from .linalg import lu_unpack # noqa: F401 -from .linalg import cdist # noqa: F401 -from .logic import equal # noqa: F401 -from .logic import equal_ # noqa: F401 -from .logic import greater_equal # noqa: F401 -from .logic import greater_equal_ # noqa: F401 -from .logic import greater_than # noqa: F401 -from .logic import greater_than_ # noqa: F401 -from .logic import is_empty # noqa: F401 -from .logic import less_equal # noqa: F401 -from .logic import less_equal_ # noqa: F401 -from .logic import less_than # noqa: F401 -from .logic import less_than_ # noqa: F401 -from .logic import logical_and # noqa: F401 -from .logic import logical_and_ # noqa: F401 -from .logic import logical_not # noqa: F401 -from .logic import logical_not_ # noqa: F401 -from .logic import logical_or # noqa: F401 -from .logic import logical_or_ # noqa: F401 -from .logic import logical_xor # noqa: F401 -from .logic import logical_xor_ # noqa: F401 -from .logic import bitwise_and # noqa: F401 -from .logic import bitwise_and_ # noqa: F401 -from .logic import bitwise_or # noqa: F401 -from .logic import bitwise_or_ # noqa: F401 -from .logic import bitwise_xor # noqa: F401 -from .logic import bitwise_xor_ # noqa: F401 -from .logic import bitwise_not # noqa: F401 -from .logic import bitwise_not_ # noqa: F401 -from .logic import not_equal # noqa: F401 -from .logic import not_equal_ # noqa: F401 -from .logic import allclose # noqa: F401 -from .logic import isclose # noqa: F401 -from .logic import equal_all # noqa: F401 -from .logic import is_tensor # noqa: F401 -from .manipulation import atleast_1d # noqa: F401 -from .manipulation import atleast_2d # noqa: F401 -from .manipulation import atleast_3d # noqa: F401 -from .manipulation import cast # noqa: F401 -from .manipulation import cast_ # noqa: F401 -from .manipulation import concat # noqa: F401 -from .manipulation import expand # noqa: F401 -from .manipulation import broadcast_to # noqa: F401 -from .manipulation import broadcast_tensors # noqa: F401 -from .manipulation import expand_as # noqa: F401 -from .manipulation import tile # noqa: F401 -from .manipulation import flatten # noqa: F401 -from .manipulation import flatten_ # noqa: F401 -from .manipulation import gather # noqa: F401 -from .manipulation import gather_nd # noqa: F401 -from .manipulation import reshape # noqa: F401 -from .manipulation import reshape_ # noqa: F401 -from .manipulation import flip as reverse # noqa: F401 -from .manipulation import scatter # noqa: F401 -from .manipulation import scatter_ # noqa: F401 -from .manipulation import scatter_nd_add # noqa: F401 -from .manipulation import scatter_nd # noqa: F401 -from .manipulation import shard_index # noqa: F401 -from .manipulation import slice # noqa: F401 -from .manipulation import split # noqa: F401 -from .manipulation import vsplit # noqa: F401 -from .manipulation import squeeze # noqa: F401 -from .manipulation import squeeze_ # noqa: F401 -from .manipulation import stack # noqa: F401 -from .manipulation import strided_slice # noqa: F401 -from .manipulation import unique # noqa: F401 -from .manipulation import unique_consecutive # noqa: F401 -from .manipulation import unsqueeze # noqa: F401 -from .manipulation import unsqueeze_ # noqa: F401 -from .manipulation import unstack # noqa: F401 -from .manipulation import flip # noqa: F401 -from .manipulation import rot90 # noqa: F401 -from .manipulation import unbind # noqa: F401 -from .manipulation import roll # noqa: F401 -from .manipulation import chunk # noqa: F401 -from .manipulation import tensordot # noqa: F401 -from .manipulation import as_complex # noqa: F401 -from .manipulation import take_along_axis # noqa: F401 -from .manipulation import put_along_axis # noqa: F401 -from .manipulation import select_scatter # noqa: F401 -from .manipulation import put_along_axis_ # noqa: F401 -from .manipulation import as_real # noqa: F401 -from .manipulation import moveaxis # noqa: F401 -from .manipulation import repeat_interleave # noqa: F401 -from .manipulation import index_add # noqa: F401 -from .manipulation import index_add_ # noqa: F401 -from .manipulation import index_put # noqa: F401 -from .manipulation import index_put_ # noqa: F401 -from .manipulation import unflatten # noqa: F401 -from .manipulation import as_strided # noqa: F401 -from .manipulation import view # noqa: F401 -from .manipulation import view_as # noqa: F401 -from .manipulation import unfold # noqa: F401 -from .manipulation import masked_fill # noqa: F401 -from .manipulation import masked_fill_ # noqa: F401 -from .manipulation import index_fill # noqa: F401 -from .manipulation import index_fill_ # noqa: F401 -from .manipulation import diagonal_scatter # noqa: F401 -from .math import abs # noqa: F401 -from .math import abs_ # noqa: F401 -from .math import acos # noqa: F401 -from .math import acos_ # noqa: F401 -from .math import asin # noqa: F401 -from .math import asin_ # noqa: F401 -from .math import atan # noqa: F401 -from .math import atan_ # noqa: F401 -from .math import ceil # noqa: F401 -from .math import ceil_ # noqa: F401 -from .math import cos # noqa: F401 -from .math import cos_ # noqa: F401 -from .math import tan # noqa: F401 -from .math import tan_ # noqa: F401 -from .math import cosh # noqa: F401 -from .math import cosh_ # noqa: F401 -from .math import cumsum # noqa: F401 -from .math import cumsum_ # noqa: F401 -from .math import cummax # noqa: F401 -from .math import cummin # noqa: F401 -from .math import cumprod # noqa: F401 -from .math import cumprod_ # noqa: F401 -from .math import logcumsumexp # noqa: F401 -from .math import logit # noqa: F401 -from .math import logit_ # noqa: F401 -from .math import exp # noqa: F401 -from .math import exp_ # noqa: F401 -from .math import expm1 # noqa: F401 -from .math import floor # noqa: F401 -from .math import floor_ # noqa: F401 -from .math import increment # noqa: F401 -from .math import log # noqa: F401 -from .math import log_ # noqa: F401 -from .math import multiplex # noqa: F401 -from .math import pow # noqa: F401 -from .math import pow_ # noqa: F401 -from .math import reciprocal # noqa: F401 -from .math import reciprocal_ # noqa: F401 -from .math import round # noqa: F401 -from .math import round_ # noqa: F401 -from .math import rsqrt # noqa: F401 -from .math import rsqrt_ # noqa: F401 -from .math import scale # noqa: F401 -from .math import scale_ # noqa: F401 -from .math import sign # noqa: F401 -from .math import sin # noqa: F401 -from .math import sin_ # noqa: F401 -from .math import sinh # noqa: F401 -from .math import sinh_ # noqa: F401 -from .math import sqrt # noqa: F401 -from .math import sqrt_ # noqa: F401 -from .math import square # noqa: F401 -from .math import stanh # noqa: F401 -from .math import sum # noqa: F401 -from .math import multigammaln # noqa: F401 -from .math import multigammaln_ # noqa: F401 -from .math import nan_to_num # noqa: F401 -from .math import nan_to_num_ # noqa: F401 -from .math import nansum # noqa: F401 -from .math import nanmean # noqa: F401 -from .math import count_nonzero # noqa: F401 -from .math import tanh # noqa: F401 -from .math import tanh_ # noqa: F401 -from .math import add_n # noqa: F401 -from .math import max # noqa: F401 -from .math import amax # noqa: F401 -from .math import maximum # noqa: F401 -from .math import min # noqa: F401 -from .math import amin # noqa: F401 -from .math import minimum # noqa: F401 -from .math import mm # noqa: F401 -from .math import divide # noqa: F401 -from .math import divide_ # noqa: F401 -from .math import floor_divide # noqa: F401 -from .math import floor_divide_ # noqa: F401 -from .math import remainder # noqa: F401 -from .math import remainder_ # noqa: F401 -from .math import mod # noqa: F401 -from .math import mod_ # noqa: F401 -from .math import floor_mod # noqa: F401 -from .math import floor_mod_ # noqa: F401 -from .math import multiply # noqa: F401 -from .math import multiply_ # noqa: F401 -from .math import add # noqa: F401 -from .math import add_ # noqa: F401 -from .math import subtract # noqa: F401 -from .math import subtract_ # noqa: F401 -from .math import atan2 # noqa: F401 -from .math import logsumexp # noqa: F401 -from .math import logaddexp # noqa: F401 -from .math import inverse # noqa: F401 -from .math import log2 # noqa: F401 -from .math import log2_ # noqa: F401 -from .math import log10 # noqa: F401 -from .math import log10_ # noqa: F401 -from .math import log1p # noqa: F401 -from .math import log1p_ # noqa: F401 -from .math import erf # noqa: F401 -from .math import addmm # noqa: F401 -from .math import addmm_ # noqa: F401 -from .math import clip # noqa: F401 -from .math import clip_ # noqa: F401 -from .math import trace # noqa: F401 -from .math import kron # noqa: F401 -from .math import isfinite # noqa: F401 -from .math import isinf # noqa: F401 -from .math import isnan # noqa: F401 -from .math import prod # noqa: F401 -from .math import all # noqa: F401 -from .math import any # noqa: F401 -from .math import broadcast_shape # noqa: F401 -from .math import conj # noqa: F401 -from .math import trunc # noqa: F401 -from .math import trunc_ # noqa: F401 -from .math import digamma # noqa: F401 -from .math import digamma_ # noqa: F401 -from .math import neg # noqa: F401 -from .math import neg_ # noqa: F401 -from .math import lgamma # noqa: F401 -from .math import lgamma_ # noqa: F401 -from .math import diagonal # noqa: F401 -from .math import acosh # noqa: F401 -from .math import acosh_ # noqa: F401 -from .math import asinh # noqa: F401 -from .math import asinh_ # noqa: F401 -from .math import atanh # noqa: F401 -from .math import atanh_ # noqa: F401 -from .math import lerp # noqa: F401 -from .math import lerp_ # noqa: F401 -from .math import erfinv # noqa: F401 -from .math import erfinv_ # noqa: F401 -from .math import rad2deg # noqa: F401 -from .math import deg2rad # noqa: F401 -from .math import gcd # noqa: F401 -from .math import gcd_ # noqa: F401 -from .math import lcm # noqa: F401 -from .math import lcm_ # noqa: F401 -from .math import diff # noqa: F401 -from .math import angle # noqa: F401 -from .math import fmax # noqa: F401 -from .math import fmin # noqa: F401 -from .math import inner # noqa: F401 -from .math import outer # noqa: F401 -from .math import heaviside # noqa: F401 -from .math import frac # noqa: F401 -from .math import frac_ # noqa: F401 -from .math import sgn # noqa: F401 -from .math import take # noqa: F401 -from .math import frexp # noqa: F401 -from .math import ldexp # noqa: F401 -from .math import ldexp_ # noqa: F401 -from .math import trapezoid # noqa: F401 -from .math import cumulative_trapezoid # noqa: F401 -from .math import sigmoid # noqa: F401 -from .math import sigmoid_ # noqa: F401 -from .math import vander # noqa: F401 -from .math import nextafter # noqa: F401 -from .math import i0 # noqa: F401 -from .math import i0_ # noqa: F401 -from .math import i0e # noqa: F401 -from .math import i1 # noqa: F401 -from .math import i1e # noqa: F401 -from .math import polygamma # noqa: F401 -from .math import polygamma_ # noqa: F401 -from .math import renorm # noqa: F401 -from .math import renorm_ # noqa: F401 -from .math import hypot # noqa: F401 -from .math import hypot_ # noqa: F401 - -from .random import multinomial # noqa: F401 -from .random import standard_normal # noqa: F401 -from .random import normal # noqa: F401 -from .random import normal_ # noqa: F401 -from .random import uniform # noqa: F401 -from .random import uniform_ # noqa: F401 -from .random import randn # noqa: F401 -from .random import rand # noqa: F401 -from .random import randint # noqa: F401 -from .random import randint_like # noqa: F401 -from .random import randperm # noqa: F401 -from .random import poisson # noqa: F401 -from .random import exponential_ # noqa: F401 -from .search import argmax # noqa: F401 -from .search import argmin # noqa: F401 -from .search import argsort # noqa: F401 -from .search import searchsorted # noqa: F401 -from .search import bucketize # noqa: F401 -from .search import topk # noqa: F401 -from .search import where # noqa: F401 -from .search import where_ # noqa: F401 -from .search import index_select # noqa: F401 -from .search import nonzero # noqa: F401 -from .search import sort # noqa: F401 -from .search import index_sample # noqa: F401 -from .search import masked_select # noqa: F401 -from .search import kthvalue # noqa: F401 -from .search import mode # noqa: F401 -from .search import top_p_sampling - -from .stat import mean # noqa: F401 -from .stat import std # noqa: F401 -from .stat import var # noqa: F401 -from .stat import numel # noqa: F401 -from .stat import median # noqa: F401 -from .stat import nanmedian # noqa: F401 -from .stat import quantile # noqa: F401 -from .stat import nanquantile # noqa: F401 +from ..signal import ( # noqa: F401 + istft, + stft, +) +from .array import ( # noqa: F401 + array_length, + array_read, + array_write, + create_array, +) +from .attribute import ( # noqa: F401 + imag, + is_complex, + is_floating_point, + is_integer, + rank, + real, + shape, +) +from .creation import ( # noqa: F401 + arange, + cauchy_, + complex, + create_parameter, + create_tensor, + diag, + diag_embed, + diagflat, + empty, + empty_like, + eye, + fill_constant, + full, + full_like, + geometric_, + linspace, + meshgrid, + ones, + ones_like, + polar, + to_tensor, + tril, + tril_, + triu, + triu_, + zeros, + zeros_like, +) +from .einsum import einsum # noqa: F401 +from .linalg import ( # noqa: F401 + bincount, + bmm, + cdist, + cholesky, + cholesky_solve, + cond, + corrcoef, + cov, + cross, + dist, + dot, + eig, + eigh, + eigvals, + eigvalsh, + histogram, + householder_product, + lstsq, + lu, + lu_unpack, + matmul, + matrix_power, + multi_dot, + mv, + norm, + pca_lowrank, + pinv, + qr, + solve, + svd, + t, + t_, + transpose, + transpose_, +) +from .logic import ( # noqa: F401 + allclose, + bitwise_and, + bitwise_and_, + bitwise_not, + bitwise_not_, + bitwise_or, + bitwise_or_, + bitwise_xor, + bitwise_xor_, + equal, + equal_, + equal_all, + greater_equal, + greater_equal_, + greater_than, + greater_than_, + is_empty, + is_tensor, + isclose, + less_equal, + less_equal_, + less_than, + less_than_, + logical_and, + logical_and_, + logical_not, + logical_not_, + logical_or, + logical_or_, + logical_xor, + logical_xor_, + not_equal, + not_equal_, +) +from .manipulation import ( # noqa: F401 + as_complex, + as_real, + as_strided, + atleast_1d, + atleast_2d, + atleast_3d, + broadcast_tensors, + broadcast_to, + cast, + cast_, + chunk, + concat, + diagonal_scatter, + expand, + expand_as, + flatten, + flatten_, + flip, + flip as reverse, + gather, + gather_nd, + index_add, + index_add_, + index_fill, + index_fill_, + index_put, + index_put_, + masked_fill, + masked_fill_, + moveaxis, + put_along_axis, + put_along_axis_, + repeat_interleave, + reshape, + reshape_, + roll, + rot90, + scatter, + scatter_, + scatter_nd, + scatter_nd_add, + select_scatter, + shard_index, + slice, + split, + squeeze, + squeeze_, + stack, + strided_slice, + take_along_axis, + tensordot, + tile, + unbind, + unflatten, + unfold, + unique, + unique_consecutive, + unsqueeze, + unsqueeze_, + unstack, + view, + view_as, + vsplit, +) +from .math import ( # noqa: F401 + abs, + abs_, + acos, + acos_, + acosh, + acosh_, + add, + add_, + add_n, + addmm, + addmm_, + all, + amax, + amin, + angle, + any, + asin, + asin_, + asinh, + asinh_, + atan, + atan2, + atan_, + atanh, + atanh_, + broadcast_shape, + ceil, + ceil_, + clip, + clip_, + conj, + cos, + cos_, + cosh, + cosh_, + count_nonzero, + cummax, + cummin, + cumprod, + cumprod_, + cumsum, + cumsum_, + cumulative_trapezoid, + deg2rad, + diagonal, + diff, + digamma, + digamma_, + divide, + divide_, + erf, + erfinv, + erfinv_, + exp, + exp_, + expm1, + floor, + floor_, + floor_divide, + floor_divide_, + floor_mod, + floor_mod_, + fmax, + fmin, + frac, + frac_, + frexp, + gcd, + gcd_, + heaviside, + hypot, + hypot_, + i0, + i0_, + i0e, + i1, + i1e, + increment, + inner, + inverse, + isfinite, + isinf, + isnan, + kron, + lcm, + lcm_, + ldexp, + ldexp_, + lerp, + lerp_, + lgamma, + lgamma_, + log, + log1p, + log1p_, + log2, + log2_, + log10, + log10_, + log_, + logaddexp, + logcumsumexp, + logit, + logit_, + logsumexp, + max, + maximum, + min, + minimum, + mm, + mod, + mod_, + multigammaln, + multigammaln_, + multiplex, + multiply, + multiply_, + nan_to_num, + nan_to_num_, + nanmean, + nansum, + neg, + neg_, + nextafter, + outer, + polygamma, + polygamma_, + pow, + pow_, + prod, + rad2deg, + reciprocal, + reciprocal_, + remainder, + remainder_, + renorm, + renorm_, + round, + round_, + rsqrt, + rsqrt_, + scale, + scale_, + sgn, + sigmoid, + sigmoid_, + sign, + sin, + sin_, + sinh, + sinh_, + sqrt, + sqrt_, + square, + stanh, + subtract, + subtract_, + sum, + take, + tan, + tan_, + tanh, + tanh_, + trace, + trapezoid, + trunc, + trunc_, + vander, +) +from .random import ( # noqa: F401 + exponential_, + multinomial, + normal, + normal_, + poisson, + rand, + randint, + randint_like, + randn, + randperm, + standard_normal, + uniform, + uniform_, +) +from .search import ( # noqa: F401 + argmax, + argmin, + argsort, + bucketize, + index_sample, + index_select, + kthvalue, + masked_select, + mode, + nonzero, + searchsorted, + sort, + top_p_sampling, + topk, + where, + where_, +) +from .stat import ( # noqa: F401 + mean, + median, + nanmedian, + nanquantile, + numel, + quantile, + std, + var, +) from .to_string import set_printoptions # noqa: F401 # this list used in math_op_patch.py for _binary_creator_ From 6512ec8fac1fa3e1fa03b454349feecd78762ad4 Mon Sep 17 00:00:00 2001 From: YibinLiu666 <2632839426@qq.com> Date: Thu, 30 Nov 2023 08:17:18 +0000 Subject: [PATCH 08/12] update --- python/paddle/tensor/__init__.py | 1 - 1 file changed, 1 deletion(-) diff --git a/python/paddle/tensor/__init__.py b/python/paddle/tensor/__init__.py index e799fe19e55b6..0d42053ef7500 100644 --- a/python/paddle/tensor/__init__.py +++ b/python/paddle/tensor/__init__.py @@ -79,7 +79,6 @@ eigvals, eigvalsh, histogram, - householder_product, lstsq, lu, lu_unpack, From d6ac9f67f94d3686e0c36d25b6835f4f05d9bb5c Mon Sep 17 00:00:00 2001 From: YibinLiu666 <2632839426@qq.com> Date: Thu, 30 Nov 2023 12:05:21 +0000 Subject: [PATCH 09/12] remove --- python/paddle/tensor/manipulation.py | 19 ------------------- 1 file changed, 19 deletions(-) diff --git a/python/paddle/tensor/manipulation.py b/python/paddle/tensor/manipulation.py index a27f745175ad7..9b6fa1adac7f6 100644 --- a/python/paddle/tensor/manipulation.py +++ b/python/paddle/tensor/manipulation.py @@ -5997,25 +5997,6 @@ def select_scatter(x, values, axis, index, name=None): 'none_axes': none_axes, } - StartsTensorList = None - EndsTensorList = None - StepsTensorList = None - - if paddle.utils._contain_var(starts): - StartsTensorList = paddle.utils._convert_to_tensor_list(starts) - inputs['StartsTensorList'] = StartsTensorList - del attrs['starts'] - - if paddle.utils._contain_var(ends): - EndsTensorList = paddle.utils._convert_to_tensor_list(ends) - inputs['EndsTensorList'] = EndsTensorList - del attrs['ends'] - if paddle.utils._contain_var(steps): - StepsTensorList = paddle.utils._convert_to_tensor_list(steps) - inputs['StepsTensorList'] = StepsTensorList - del attrs['steps'] - - # step2. Parse values dtype = x.dtype attrs['dtype'] = dtype From 4573b2c0412a2b41e81399e3d92bb6831e135aad Mon Sep 17 00:00:00 2001 From: YibinLiu666 <2632839426@qq.com> Date: Fri, 1 Dec 2023 03:24:40 +0000 Subject: [PATCH 10/12] update --- python/paddle/tensor/manipulation.py | 12 +----------- test/legacy_test/test_select_scatter_op.py | 11 ++++------- 2 files changed, 5 insertions(+), 18 deletions(-) diff --git a/python/paddle/tensor/manipulation.py b/python/paddle/tensor/manipulation.py index 9b6fa1adac7f6..8c8fc3cfd1163 100644 --- a/python/paddle/tensor/manipulation.py +++ b/python/paddle/tensor/manipulation.py @@ -6016,13 +6016,7 @@ def select_scatter(x, values, axis, index, name=None): ) else: helper = LayerHelper('select_scatter', **locals()) - if helper.main_program.current_block_idx != 0: - # not in global block, we should create a global variable. - output = helper._create_global_variable_for_type_inference( - dtype=x.dtype - ) - else: - output = helper.create_variable_for_type_inference(dtype=x.dtype) + output = helper.create_variable_for_type_inference(dtype=x.dtype) cur_block = default_main_program().current_block() cur_block.append_op( type="set_value", @@ -6032,8 +6026,4 @@ def select_scatter(x, values, axis, index, name=None): inplace_map={"Input": "Out"}, ) - # map var to the new output - paddle.jit.api.ProgramTranslator.get_instance()._inplace_map.add( - cur_block.program, x.desc.id(), output - ) return output diff --git a/test/legacy_test/test_select_scatter_op.py b/test/legacy_test/test_select_scatter_op.py index 7a82423982d43..ecb5a0927e31b 100644 --- a/test/legacy_test/test_select_scatter_op.py +++ b/test/legacy_test/test_select_scatter_op.py @@ -149,20 +149,17 @@ def setUp(self): if core.is_compiled_with_cuda(): self.place.append(paddle.CUDAPlace(0)) - def test_error(self): - try: + def test_len_of_shape_not_equal_error(self): + with self.assertRaises(RuntimeError): x_tensor = paddle.to_tensor(self.x_np) value_tensor = paddle.to_tensor(self.value_np).reshape((2, 2, 2)) res = paddle.select_scatter(x_tensor, value_tensor, 1, 1) - except Exception as error: - self.assertIsInstance(error, RuntimeError) - try: + def test_one_of_size_not_equal_error(self): + with self.assertRaises(RuntimeError): x_tensor = paddle.to_tensor(self.x_np) value_tensor = paddle.to_tensor([[2, 2], [2, 2]]).astype(np.float32) res = paddle.select_scatter(x_tensor, value_tensor, 1, 1) - except Exception as error: - self.assertIsInstance(error, RuntimeError) if __name__ == "__main__": From 195247c840dcbdcdff1ed85bae85dfcee986c14d Mon Sep 17 00:00:00 2001 From: YibinLiu666 <2632839426@qq.com> Date: Tue, 5 Dec 2023 04:09:10 +0000 Subject: [PATCH 11/12] add type --- python/paddle/tensor/manipulation.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/python/paddle/tensor/manipulation.py b/python/paddle/tensor/manipulation.py index 8c8fc3cfd1163..d939874dbd087 100644 --- a/python/paddle/tensor/manipulation.py +++ b/python/paddle/tensor/manipulation.py @@ -5928,8 +5928,8 @@ def select_scatter(x, values, axis, index, name=None): Embeds the values of the values tensor into x at the given index of axis. Args: - x (Tensor) : The Destination Tensor. - values (Tensor) : The tensor to embed into x. + x (Tensor) : The Destination Tensor. Supported data types are `bool`, `float16`, `float32`, `float64`, `uint8`, `int8`, `int16`, `int32`, `int64`, `bfloat16`, `complex64`, `complex64`. + values (Tensor) : The tensor to embed into x. Supported data types are `bool`, `float16`, `float32`, `float64`, `uint8`, `int8`, `int16`, `int32`, `int64`, `bfloat16`, `complex64`, `complex64`. axis (int) : the dimension to insert the slice into. index (int) : the index to select with. name (str, optional): Name for the operation (optional, default is None). From cd2bc761a54b41902567f1d93a98281c37b39ae9 Mon Sep 17 00:00:00 2001 From: YibinLiu666 <2632839426@qq.com> Date: Wed, 6 Dec 2023 11:35:56 +0800 Subject: [PATCH 12/12] update type --- python/paddle/tensor/manipulation.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/python/paddle/tensor/manipulation.py b/python/paddle/tensor/manipulation.py index d939874dbd087..b73ad7aac6075 100644 --- a/python/paddle/tensor/manipulation.py +++ b/python/paddle/tensor/manipulation.py @@ -5928,8 +5928,8 @@ def select_scatter(x, values, axis, index, name=None): Embeds the values of the values tensor into x at the given index of axis. Args: - x (Tensor) : The Destination Tensor. Supported data types are `bool`, `float16`, `float32`, `float64`, `uint8`, `int8`, `int16`, `int32`, `int64`, `bfloat16`, `complex64`, `complex64`. - values (Tensor) : The tensor to embed into x. Supported data types are `bool`, `float16`, `float32`, `float64`, `uint8`, `int8`, `int16`, `int32`, `int64`, `bfloat16`, `complex64`, `complex64`. + x (Tensor) : The Destination Tensor. Supported data types are `bool`, `float16`, `float32`, `float64`, `uint8`, `int8`, `int16`, `int32`, `int64`, `bfloat16`, `complex64`, `complex128`. + values (Tensor) : The tensor to embed into x. Supported data types are `bool`, `float16`, `float32`, `float64`, `uint8`, `int8`, `int16`, `int32`, `int64`, `bfloat16`, `complex64`, `complex128`. axis (int) : the dimension to insert the slice into. index (int) : the index to select with. name (str, optional): Name for the operation (optional, default is None).