From f9ba2e53311caa58a6c2a36787267b500a1640c5 Mon Sep 17 00:00:00 2001 From: zeroRains Date: Mon, 25 Mar 2024 13:38:44 +0000 Subject: [PATCH 01/30] add the sum_as op for paddle - part(forward) --- paddle/phi/api/yaml/ops.yaml | 9 +++ paddle/phi/infermeta/binary.cc | 20 ++++++ paddle/phi/infermeta/binary.h | 6 ++ paddle/phi/kernels/cpu/sum_as_kernel.cc | 57 ++++++++++++++++ paddle/phi/kernels/funcs/common_shape.h | 18 ++++++ paddle/phi/kernels/gpu/sum_as_kernel.cu | 53 +++++++++++++++ paddle/phi/kernels/sum_as_kernel.h | 32 +++++++++ python/paddle/__init__.py | 2 + python/paddle/tensor/__init__.py | 2 + python/paddle/tensor/math.py | 86 +++++++++++++++++++++++++ test/legacy_test/test_sum_as_op.py | 61 ++++++++++++++++++ 11 files changed, 346 insertions(+) create mode 100644 paddle/phi/kernels/cpu/sum_as_kernel.cc create mode 100644 paddle/phi/kernels/gpu/sum_as_kernel.cu create mode 100644 paddle/phi/kernels/sum_as_kernel.h create mode 100644 test/legacy_test/test_sum_as_op.py diff --git a/paddle/phi/api/yaml/ops.yaml b/paddle/phi/api/yaml/ops.yaml index 4759da3105e4c0..0a16edf8a0551f 100755 --- a/paddle/phi/api/yaml/ops.yaml +++ b/paddle/phi/api/yaml/ops.yaml @@ -2751,6 +2751,15 @@ func : stanh backward : stanh_grad +- op : sum_as + args : (Tensor x, Tensor y, DataType dtype=DataType::UNDEFINED, bool keepdim=false) + output : Tensor(out) + infer_meta : + func : SumAsInferMeta + kernel : + func : sum_as + data_type : x + - op : svd args : (Tensor x, bool full_matrices = false) output : Tensor(u), Tensor(s), Tensor(vh) diff --git a/paddle/phi/infermeta/binary.cc b/paddle/phi/infermeta/binary.cc index 97edce9ad79530..886b88f51ca10b 100644 --- a/paddle/phi/infermeta/binary.cc +++ b/paddle/phi/infermeta/binary.cc @@ -3047,6 +3047,26 @@ void SequenceMaskInferMeta(const MetaTensor& x, y->set_dtype(out_dtype); } +void SumAsInferMeta(const MetaTensor& x, + const MetaTensor& y, + DataType dtype, + bool keep_dim, + MetaTensor* out) { + DataType out_dtype; + if (dtype != DataType::UNDEFINED) { + out_dtype = dtype; + } else { + if (x.dtype() == DataType::BOOL || x.dtype() == DataType::INT32) { + out_dtype = DataType::INT64; + } else { + out_dtype = x.dtype(); + } + } + out->set_dtype(out_dtype); + out->set_dims(y.dims()); + out->set_layout(x.layout()); +} + void SoftmaxMaskFuseInferMeta(const MetaTensor& x, const MetaTensor& mask, MetaTensor* out) { diff --git a/paddle/phi/infermeta/binary.h b/paddle/phi/infermeta/binary.h index 77bc9251970131..6c950b5a59f323 100644 --- a/paddle/phi/infermeta/binary.h +++ b/paddle/phi/infermeta/binary.h @@ -524,6 +524,12 @@ void ShuffleBatchInferMeta(const MetaTensor& x, ); +void SumAsInferMeta(const MetaTensor& x, + const MetaTensor& y, + DataType dtype, + bool keep_dim, + MetaTensor* out); + void SoftmaxMaskFuseInferMeta(const MetaTensor& x, const MetaTensor& mask, MetaTensor* out); diff --git a/paddle/phi/kernels/cpu/sum_as_kernel.cc b/paddle/phi/kernels/cpu/sum_as_kernel.cc new file mode 100644 index 00000000000000..951ebeb9797967 --- /dev/null +++ b/paddle/phi/kernels/cpu/sum_as_kernel.cc @@ -0,0 +1,57 @@ + +// Copyright (c) 2024 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/sum_as_kernel.h" + +#include "paddle/phi/core/device_context.h" +#include "paddle/phi/core/kernel_registry.h" +#include "paddle/phi/kernels/cpu/reduce.h" + +namespace phi { + +template +void SumAsKernel(const Context& dev_ctx, + const DenseTensor& x, + const DenseTensor& y, + DataType out_dtype, + bool keep_dim, + DenseTensor* out) { + auto reduce_dim = phi::funcs::GetReduceDims(x, y); + bool reduce_all = recompute_reduce_all(x, reduce_dim); + if (out_dtype == DataType::UNDEFINED && out->dtype() != x.dtype()) { + out_dtype = out->dtype(); + } + phi::Reduce( + dev_ctx, x, reduce_all, reduce_dim, keep_dim, out_dtype, out); +} + +} // namespace phi + +PD_REGISTER_KERNEL(sum_as, + CPU, + ALL_LAYOUT, + phi::SumAsKernel, + bool, + float, + double, + phi::dtype::float16, + phi::dtype::bfloat16, + int16_t, + int, + int64_t, + uint8_t, + int8_t) { + kernel->OutputAt(0).SetDataType(phi::DataType::UNDEFINED); +} diff --git a/paddle/phi/kernels/funcs/common_shape.h b/paddle/phi/kernels/funcs/common_shape.h index 19f2fa1f2fac4c..b2ec7da4d8c9b3 100644 --- a/paddle/phi/kernels/funcs/common_shape.h +++ b/paddle/phi/kernels/funcs/common_shape.h @@ -297,5 +297,23 @@ inline void FCOutputSize(const DDim &in_dims, out_dims.push_back(w_dims1); } +inline std::vector GetReduceDims(const DenseTensor &in, + const DenseTensor &out) { + std::vector reduce_dims; + auto in_dims = in.dims(); + auto out_dims = out.dims(); + + int diff = in_dims.size() - out_dims.size(); + for (int i = 0; i < diff; ++i) { + reduce_dims.push_back(i); + } + for (int i = diff; i < in_dims.size(); ++i) { + if (out_dims[i - diff] != in_dims[i]) { + reduce_dims.push_back(i); + } + } + return reduce_dims; +} + } // namespace funcs } // namespace phi diff --git a/paddle/phi/kernels/gpu/sum_as_kernel.cu b/paddle/phi/kernels/gpu/sum_as_kernel.cu new file mode 100644 index 00000000000000..bfc62d7d447da5 --- /dev/null +++ b/paddle/phi/kernels/gpu/sum_as_kernel.cu @@ -0,0 +1,53 @@ + +// Copyright (c) 2024 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/sum_as_kernel.h" + +#include "paddle/phi/backends/gpu/gpu_context.h" +#include "paddle/phi/core/kernel_registry.h" +#include "paddle/phi/kernels/reduce_sum_kernel.h" + +namespace phi { + +template +void SumAsKernel(const Context& dev_ctx, + const DenseTensor& x, + const DenseTensor& y, + DataType out_dtype, + bool keep_dim, + DenseTensor* out) { + auto reduce_dim = phi::funcs::GetReduceDims(x, y); + dev_ctx.template Alloc(out); + phi::SumKernel(dev_ctx, x, reduce_dim, out_dtype, keep_dim, out); +} + +} // namespace phi + +PD_REGISTER_KERNEL(sum_as, + GPU, + ALL_LAYOUT, + phi::SumAsKernel, + bool, + float, + double, + phi::dtype::float16, + phi::dtype::bfloat16, + int16_t, + int, + int64_t, + uint8_t, + int8_t) { + kernel->OutputAt(0).SetDataType(phi::DataType::UNDEFINED); +} diff --git a/paddle/phi/kernels/sum_as_kernel.h b/paddle/phi/kernels/sum_as_kernel.h new file mode 100644 index 00000000000000..f2356c8828c902 --- /dev/null +++ b/paddle/phi/kernels/sum_as_kernel.h @@ -0,0 +1,32 @@ +// Copyright (c) 2024 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 "paddle/phi/core/dense_tensor.h" +#include "paddle/phi/core/device_context.h" +#include "paddle/phi/kernels/funcs/common_shape.h" +#include "paddle/phi/kernels/funcs/reduce_functor.h" + +namespace phi { + +template +void SumAsKernel(const Context& dev_ctx, + const DenseTensor& x, + const DenseTensor& y, + DataType out_dtype, + bool keep_dim, + DenseTensor* out); + +} // namespace phi diff --git a/python/paddle/__init__.py b/python/paddle/__init__.py index 05cff990c18379..7adcff2ac86eca 100644 --- a/python/paddle/__init__.py +++ b/python/paddle/__init__.py @@ -494,6 +494,7 @@ stanh, subtract, sum, + sum_as, take, tan, tan_, @@ -846,6 +847,7 @@ 'ones', 'not_equal', 'sum', + 'sum_as', 'nansum', 'nanmean', 'count_nonzero', diff --git a/python/paddle/tensor/__init__.py b/python/paddle/tensor/__init__.py index 4513bcbdba8f82..212e263c17a5d4 100644 --- a/python/paddle/tensor/__init__.py +++ b/python/paddle/tensor/__init__.py @@ -382,6 +382,7 @@ subtract, subtract_, sum, + sum_as, take, tan, tan_, @@ -523,6 +524,7 @@ 'square', 'stanh', 'sum', + 'sum_as', 'multigammaln', 'multigammaln_', 'nan_to_num', diff --git a/python/paddle/tensor/math.py b/python/paddle/tensor/math.py index a931912ae95727..c1ec699b81db34 100644 --- a/python/paddle/tensor/math.py +++ b/python/paddle/tensor/math.py @@ -1581,6 +1581,92 @@ def sum(x, axis=None, dtype=None, keepdim=False, name=None): return out +def sum_as(x, y, dtype=None, keepdim=False, name=None): + """ + Computes the sum_as of tensor elements over the given dimension. + + Args: + x (Tensor): An N-D Tensor, the data type is bool, float16, float32, float64, int32 or int64. + axis (int|list|tuple, optional): The dimensions along which the sum is performed. If + :attr:`None`, sum all elements of :attr:`x` and return a + Tensor with a single element, otherwise must be in the + range :math:`[-rank(x), rank(x))`. If :math:`axis[i] < 0`, + the dimension to reduce is :math:`rank + axis[i]`. + dtype (str, optional): The dtype of output Tensor. The default value is None, the dtype + of output is the same as input Tensor `x`. + keepdim (bool, optional): Whether to reserve the reduced dimension in the + output Tensor. The result Tensor will have one fewer dimension + than the :attr:`x` unless :attr:`keepdim` is true, default + value is False. + name (str, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`. + + Returns: + Tensor: Results of summation operation on the specified axis of input Tensor `x`, + if `x.dtype='bool'`, `x.dtype='int32'`, it's data type is `'int64'`, + otherwise it's data type is the same as `x`. + + Examples: + + """ + + dtype_flag = False + if dtype is not None: + dtype_flag = True + dtype = convert_np_dtype_to_dtype_(dtype) + + if in_dynamic_or_pir_mode(): + return _C_ops.sum_as(x, y, dtype, keepdim) + else: + attrs = {'keep_dim': keepdim} + + if dtype_flag: + attrs.update({'in_dtype': x.dtype, 'out_dtype': dtype}) + + check_variable_and_dtype( + x, + 'x', + [ + 'bool', + 'uint16', + 'float16', + 'float32', + 'float64', + 'int16', + 'int32', + 'int64', + ], + 'sum_as', + ) + check_variable_and_dtype( + y, + 'y', + [ + 'bool', + 'uint16', + 'float16', + 'float32', + 'float64', + 'int16', + 'int32', + 'int64', + ], + 'sum_as', + ) + + helper = LayerHelper('sum_as', **locals()) + if dtype_flag: + out = helper.create_variable_for_type_inference(dtype=dtype) + else: + out = helper.create_variable_for_type_inference(dtype=x.dtype) + helper.append_op( + type='sum_x', + inputs={'x': x, 'Y': y}, + outputs={'out': out}, + attrs=attrs, + ) + return out + + def nan_to_num(x, nan=0.0, posinf=None, neginf=None, name=None): """ Replaces NaN, positive infinity, and negative infinity values in input tensor. diff --git a/test/legacy_test/test_sum_as_op.py b/test/legacy_test/test_sum_as_op.py new file mode 100644 index 00000000000000..38ebcc71bc866c --- /dev/null +++ b/test/legacy_test/test_sum_as_op.py @@ -0,0 +1,61 @@ +# Copyright (c) 2024 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 unittest + +import numpy as np +from op_test import OpTest + +import paddle + +np.random.seed(100) +paddle.seed(100) + + +def sum_as_net(x, y): + out_dtype = x.dtype + diff = len(x.shape) - len(y.shape) + axis = [] + for i in range(diff): + axis.append(i) + for i in len(y.shape): + if y.shape[i] != x.shape[i + diff]: + axis.append(i + diff) + return np.sum(x, axis=axis, dtype=out_dtype) + + +class TestSumAsOp(OpTest): + def setUp(self): + self.op_type = "sum_as" + self.python_api = paddle.sum_as + self.init_config() + self.inputs = {'x': self.x, 'y': self.y} + self.target = sum_as_net(self.inputs['x'], self.inputs['y']) + self.outputs = {'out': self.target} + + def test_check_output(self): + self.check_output() + + def test_check_grad(self): + # self.check_grad(['x', 'y'], ['out']) + pass + + def init_config(self): + self.x = np.random.randn(200, 600).astype('float64') + self.y = np.random.randn(200, 600).astype('float64') + + +if __name__ == "__main__": + paddle.enable_static() + unittest.main() From 6ab282a0213a2dc9911c218f9a846c4d77df72f1 Mon Sep 17 00:00:00 2001 From: zeroRains Date: Mon, 25 Mar 2024 16:12:09 +0000 Subject: [PATCH 02/30] fix the test bug --- test/legacy_test/test_sum_as_op.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/legacy_test/test_sum_as_op.py b/test/legacy_test/test_sum_as_op.py index 38ebcc71bc866c..e005040feb7341 100644 --- a/test/legacy_test/test_sum_as_op.py +++ b/test/legacy_test/test_sum_as_op.py @@ -29,10 +29,10 @@ def sum_as_net(x, y): axis = [] for i in range(diff): axis.append(i) - for i in len(y.shape): + for i in range(len(y.shape)): if y.shape[i] != x.shape[i + diff]: axis.append(i + diff) - return np.sum(x, axis=axis, dtype=out_dtype) + return np.sum(x, axis=tuple(axis), dtype=out_dtype) class TestSumAsOp(OpTest): @@ -52,7 +52,7 @@ def test_check_grad(self): pass def init_config(self): - self.x = np.random.randn(200, 600).astype('float64') + self.x = np.random.randn(300, 200, 600).astype('float64') self.y = np.random.randn(200, 600).astype('float64') From 7b2b5d65c0f3aa84413d0db9ccdf6cb04d61f2c4 Mon Sep 17 00:00:00 2001 From: zeroRains Date: Wed, 27 Mar 2024 14:53:54 +0000 Subject: [PATCH 03/30] add the sum_as_grad but have bug in test --- paddle/phi/api/yaml/backward.yaml | 10 +++ paddle/phi/api/yaml/ops.yaml | 1 + paddle/phi/kernels/cpu/sum_as_grad_kernel.cc | 60 +++++++++++++++++ paddle/phi/kernels/gpu/sum_as_grad_kernel.cu | 70 ++++++++++++++++++++ paddle/phi/kernels/sum_as_grad_kernel.h | 32 +++++++++ test/legacy_test/test_sum_as_op.py | 3 +- 6 files changed, 174 insertions(+), 2 deletions(-) create mode 100644 paddle/phi/kernels/cpu/sum_as_grad_kernel.cc create mode 100644 paddle/phi/kernels/gpu/sum_as_grad_kernel.cu create mode 100644 paddle/phi/kernels/sum_as_grad_kernel.h diff --git a/paddle/phi/api/yaml/backward.yaml b/paddle/phi/api/yaml/backward.yaml index c53f81cad71f41..ae2e954dd6acce 100644 --- a/paddle/phi/api/yaml/backward.yaml +++ b/paddle/phi/api/yaml/backward.yaml @@ -2388,6 +2388,16 @@ kernel : func : stanh_grad +- backward_op : sum_as_grad + forward : sum_as(Tensor x, Tensor y, DataType dtype=DataType::UNDEFINED, bool keepdim=false) -> Tensor(out) + args : (Tensor x, Tensor y, Tensor out_grad, bool keepdim) + output : Tensor(x_grad) + infer_meta : + func : UnchangedInferMeta + param : [x] + kernel : + func : sum_as_grad + - backward_op : svd_grad forward : svd (Tensor x, bool full_matrices = false) -> Tensor(u), Tensor(s), Tensor(vh) args : (Tensor x, Tensor u, Tensor vh, Tensor s, Tensor u_grad, Tensor vh_grad, Tensor s_grad, bool full_matrices) diff --git a/paddle/phi/api/yaml/ops.yaml b/paddle/phi/api/yaml/ops.yaml index 0a16edf8a0551f..12c0fc5808ccfc 100755 --- a/paddle/phi/api/yaml/ops.yaml +++ b/paddle/phi/api/yaml/ops.yaml @@ -2759,6 +2759,7 @@ kernel : func : sum_as data_type : x + backward : sum_as_grad - op : svd args : (Tensor x, bool full_matrices = false) diff --git a/paddle/phi/kernels/cpu/sum_as_grad_kernel.cc b/paddle/phi/kernels/cpu/sum_as_grad_kernel.cc new file mode 100644 index 00000000000000..a7c7c57638aa85 --- /dev/null +++ b/paddle/phi/kernels/cpu/sum_as_grad_kernel.cc @@ -0,0 +1,60 @@ + +// Copyright (c) 2024 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/sum_as_kernel.h" + +#include "paddle/phi/core/device_context.h" +#include "paddle/phi/core/kernel_registry.h" +#include "paddle/phi/kernels/impl/reduce_grad.h" + +namespace phi { + +template +void SumAsGradKernel(const Context& dev_ctx, + const DenseTensor& x, + const DenseTensor& y, + const DenseTensor& out_grad, + bool keep_dim, + DenseTensor* x_grad) { + auto reduce_dim = phi::funcs::GetReduceDims(x, y); + bool reduce_all = recompute_reduce_all(x, reduce_dim); + ReduceGradKernel(dev_ctx, + x, + paddle::none, + out_grad, + reduce_dim, + keep_dim, + reduce_all, + x_grad); +} + +} // namespace phi + +PD_REGISTER_KERNEL(sum_as_grad, + CPU, + ALL_LAYOUT, + phi::SumAsGradKernel, + bool, + float, + double, + phi::dtype::float16, + phi::dtype::bfloat16, + int16_t, + int, + int64_t, + uint8_t, + int8_t) { + kernel->OutputAt(0).SetDataType(phi::DataType::UNDEFINED); +} diff --git a/paddle/phi/kernels/gpu/sum_as_grad_kernel.cu b/paddle/phi/kernels/gpu/sum_as_grad_kernel.cu new file mode 100644 index 00000000000000..034f0eb6f8df91 --- /dev/null +++ b/paddle/phi/kernels/gpu/sum_as_grad_kernel.cu @@ -0,0 +1,70 @@ + +// Copyright (c) 2024 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/sum_as_grad_kernel.h" + +#include "paddle/phi/backends/gpu/gpu_context.h" +#include "paddle/phi/core/kernel_registry.h" +#include "paddle/phi/kernels/funcs/reduce_function.h" +#include "paddle/phi/kernels/gpu/reduce_grad.h" + +namespace phi { + +template +void SumAsGradKernel(const Context& dev_ctx, + const DenseTensor& x, + const DenseTensor& y, + const DenseTensor& out_grad, + bool keep_dim, + DenseTensor* x_grad) { + auto reduce_dim = phi::funcs::GetReduceDims(x, y); + bool reduce_all = recompute_reduce_all(x, reduce_dim); + auto update_dims = common::vectorize(x.dims()); + for (auto i : reduce_dim) { + update_dims[i] = 1; + } + + DenseTensor new_out_grad(out_grad.type()); + new_out_grad.ShareDataWith(out_grad); + new_out_grad.Resize(common::make_ddim(update_dims)); + + dev_ctx.Alloc(x_grad, x.dtype()); + using MPType = typename phi::dtype::MPTypeTrait::Type; + phi::ReduceGrad>( + dev_ctx, + &new_out_grad, + x_grad, + x.dtype(), + phi::kps::IdentityFunctor()); +} + +} // namespace phi + +PD_REGISTER_KERNEL(sum_as_grad, + GPU, + ALL_LAYOUT, + phi::SumAsGradKernel, + bool, + float, + double, + phi::dtype::float16, + phi::dtype::bfloat16, + int16_t, + int, + int64_t, + uint8_t, + int8_t) { + kernel->OutputAt(0).SetDataType(phi::DataType::UNDEFINED); +} diff --git a/paddle/phi/kernels/sum_as_grad_kernel.h b/paddle/phi/kernels/sum_as_grad_kernel.h new file mode 100644 index 00000000000000..ce02033b2251ac --- /dev/null +++ b/paddle/phi/kernels/sum_as_grad_kernel.h @@ -0,0 +1,32 @@ +// Copyright (c) 2024 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 "paddle/phi/core/dense_tensor.h" +#include "paddle/phi/core/device_context.h" +#include "paddle/phi/kernels/funcs/common_shape.h" +#include "paddle/phi/kernels/funcs/reduce_functor.h" + +namespace phi { + +template +void SumAsGradKernel(const Context& dev_ctx, + const DenseTensor& x, + const DenseTensor& y, + const DenseTensor& out_grad, + bool keep_dim, + DenseTensor* x_grad); + +} // namespace phi diff --git a/test/legacy_test/test_sum_as_op.py b/test/legacy_test/test_sum_as_op.py index e005040feb7341..c956fd938a93ed 100644 --- a/test/legacy_test/test_sum_as_op.py +++ b/test/legacy_test/test_sum_as_op.py @@ -48,8 +48,7 @@ def test_check_output(self): self.check_output() def test_check_grad(self): - # self.check_grad(['x', 'y'], ['out']) - pass + self.check_grad(['x'], ['out']) def init_config(self): self.x = np.random.randn(300, 200, 600).astype('float64') From f9a62ade749f048d6dc5ec8e0721bdde7790eb3b Mon Sep 17 00:00:00 2001 From: zeroRains Date: Thu, 28 Mar 2024 13:21:28 +0000 Subject: [PATCH 04/30] remove uncessary args but backward computing still have bug --- paddle/phi/api/yaml/backward.yaml | 4 ++-- paddle/phi/api/yaml/ops.yaml | 2 +- paddle/phi/infermeta/binary.cc | 16 ++++------------ paddle/phi/infermeta/binary.h | 6 +----- paddle/phi/kernels/cpu/sum_as_grad_kernel.cc | 3 +-- paddle/phi/kernels/cpu/sum_as_kernel.cc | 11 ++--------- paddle/phi/kernels/gpu/sum_as_grad_kernel.cu | 3 +-- paddle/phi/kernels/gpu/sum_as_kernel.cu | 8 ++------ paddle/phi/kernels/sum_as_grad_kernel.h | 1 - paddle/phi/kernels/sum_as_kernel.h | 2 -- 10 files changed, 14 insertions(+), 42 deletions(-) diff --git a/paddle/phi/api/yaml/backward.yaml b/paddle/phi/api/yaml/backward.yaml index ae2e954dd6acce..923591f098b58e 100644 --- a/paddle/phi/api/yaml/backward.yaml +++ b/paddle/phi/api/yaml/backward.yaml @@ -2389,8 +2389,8 @@ func : stanh_grad - backward_op : sum_as_grad - forward : sum_as(Tensor x, Tensor y, DataType dtype=DataType::UNDEFINED, bool keepdim=false) -> Tensor(out) - args : (Tensor x, Tensor y, Tensor out_grad, bool keepdim) + forward : sum_as(Tensor x, Tensor y) -> Tensor(out) + args : (Tensor x, Tensor y, Tensor out_grad) output : Tensor(x_grad) infer_meta : func : UnchangedInferMeta diff --git a/paddle/phi/api/yaml/ops.yaml b/paddle/phi/api/yaml/ops.yaml index 12c0fc5808ccfc..a79b2f8047f44a 100755 --- a/paddle/phi/api/yaml/ops.yaml +++ b/paddle/phi/api/yaml/ops.yaml @@ -2752,7 +2752,7 @@ backward : stanh_grad - op : sum_as - args : (Tensor x, Tensor y, DataType dtype=DataType::UNDEFINED, bool keepdim=false) + args : (Tensor x, Tensor y) output : Tensor(out) infer_meta : func : SumAsInferMeta diff --git a/paddle/phi/infermeta/binary.cc b/paddle/phi/infermeta/binary.cc index 886b88f51ca10b..8729f9b49ce653 100644 --- a/paddle/phi/infermeta/binary.cc +++ b/paddle/phi/infermeta/binary.cc @@ -3047,20 +3047,12 @@ void SequenceMaskInferMeta(const MetaTensor& x, y->set_dtype(out_dtype); } -void SumAsInferMeta(const MetaTensor& x, - const MetaTensor& y, - DataType dtype, - bool keep_dim, - MetaTensor* out) { +void SumAsInferMeta(const MetaTensor& x, const MetaTensor& y, MetaTensor* out) { DataType out_dtype; - if (dtype != DataType::UNDEFINED) { - out_dtype = dtype; + if (x.dtype() == DataType::BOOL || x.dtype() == DataType::INT32) { + out_dtype = DataType::INT64; } else { - if (x.dtype() == DataType::BOOL || x.dtype() == DataType::INT32) { - out_dtype = DataType::INT64; - } else { - out_dtype = x.dtype(); - } + out_dtype = x.dtype(); } out->set_dtype(out_dtype); out->set_dims(y.dims()); diff --git a/paddle/phi/infermeta/binary.h b/paddle/phi/infermeta/binary.h index 6c950b5a59f323..9039f2e7b077b3 100644 --- a/paddle/phi/infermeta/binary.h +++ b/paddle/phi/infermeta/binary.h @@ -524,11 +524,7 @@ void ShuffleBatchInferMeta(const MetaTensor& x, ); -void SumAsInferMeta(const MetaTensor& x, - const MetaTensor& y, - DataType dtype, - bool keep_dim, - MetaTensor* out); +void SumAsInferMeta(const MetaTensor& x, const MetaTensor& y, MetaTensor* out); void SoftmaxMaskFuseInferMeta(const MetaTensor& x, const MetaTensor& mask, diff --git a/paddle/phi/kernels/cpu/sum_as_grad_kernel.cc b/paddle/phi/kernels/cpu/sum_as_grad_kernel.cc index a7c7c57638aa85..688a11bfa11404 100644 --- a/paddle/phi/kernels/cpu/sum_as_grad_kernel.cc +++ b/paddle/phi/kernels/cpu/sum_as_grad_kernel.cc @@ -26,7 +26,6 @@ void SumAsGradKernel(const Context& dev_ctx, const DenseTensor& x, const DenseTensor& y, const DenseTensor& out_grad, - bool keep_dim, DenseTensor* x_grad) { auto reduce_dim = phi::funcs::GetReduceDims(x, y); bool reduce_all = recompute_reduce_all(x, reduce_dim); @@ -35,7 +34,7 @@ void SumAsGradKernel(const Context& dev_ctx, paddle::none, out_grad, reduce_dim, - keep_dim, + false, reduce_all, x_grad); } diff --git a/paddle/phi/kernels/cpu/sum_as_kernel.cc b/paddle/phi/kernels/cpu/sum_as_kernel.cc index 951ebeb9797967..7eafbf8b8a2dbd 100644 --- a/paddle/phi/kernels/cpu/sum_as_kernel.cc +++ b/paddle/phi/kernels/cpu/sum_as_kernel.cc @@ -25,16 +25,11 @@ template void SumAsKernel(const Context& dev_ctx, const DenseTensor& x, const DenseTensor& y, - DataType out_dtype, - bool keep_dim, DenseTensor* out) { auto reduce_dim = phi::funcs::GetReduceDims(x, y); bool reduce_all = recompute_reduce_all(x, reduce_dim); - if (out_dtype == DataType::UNDEFINED && out->dtype() != x.dtype()) { - out_dtype = out->dtype(); - } phi::Reduce( - dev_ctx, x, reduce_all, reduce_dim, keep_dim, out_dtype, out); + dev_ctx, x, reduce_all, reduce_dim, false, out->type(), out); } } // namespace phi @@ -52,6 +47,4 @@ PD_REGISTER_KERNEL(sum_as, int, int64_t, uint8_t, - int8_t) { - kernel->OutputAt(0).SetDataType(phi::DataType::UNDEFINED); -} + int8_t) {} diff --git a/paddle/phi/kernels/gpu/sum_as_grad_kernel.cu b/paddle/phi/kernels/gpu/sum_as_grad_kernel.cu index 034f0eb6f8df91..5f3d6eea181db3 100644 --- a/paddle/phi/kernels/gpu/sum_as_grad_kernel.cu +++ b/paddle/phi/kernels/gpu/sum_as_grad_kernel.cu @@ -27,7 +27,6 @@ void SumAsGradKernel(const Context& dev_ctx, const DenseTensor& x, const DenseTensor& y, const DenseTensor& out_grad, - bool keep_dim, DenseTensor* x_grad) { auto reduce_dim = phi::funcs::GetReduceDims(x, y); bool reduce_all = recompute_reduce_all(x, reduce_dim); @@ -46,7 +45,7 @@ void SumAsGradKernel(const Context& dev_ctx, dev_ctx, &new_out_grad, x_grad, - x.dtype(), + out_grad.dtype(), phi::kps::IdentityFunctor()); } diff --git a/paddle/phi/kernels/gpu/sum_as_kernel.cu b/paddle/phi/kernels/gpu/sum_as_kernel.cu index bfc62d7d447da5..4efc61a5cd139f 100644 --- a/paddle/phi/kernels/gpu/sum_as_kernel.cu +++ b/paddle/phi/kernels/gpu/sum_as_kernel.cu @@ -25,12 +25,10 @@ template void SumAsKernel(const Context& dev_ctx, const DenseTensor& x, const DenseTensor& y, - DataType out_dtype, - bool keep_dim, DenseTensor* out) { auto reduce_dim = phi::funcs::GetReduceDims(x, y); dev_ctx.template Alloc(out); - phi::SumKernel(dev_ctx, x, reduce_dim, out_dtype, keep_dim, out); + phi::SumKernel(dev_ctx, x, reduce_dim, out->type(), false, out); } } // namespace phi @@ -48,6 +46,4 @@ PD_REGISTER_KERNEL(sum_as, int, int64_t, uint8_t, - int8_t) { - kernel->OutputAt(0).SetDataType(phi::DataType::UNDEFINED); -} + int8_t) {} diff --git a/paddle/phi/kernels/sum_as_grad_kernel.h b/paddle/phi/kernels/sum_as_grad_kernel.h index ce02033b2251ac..cc15e619c8ff2e 100644 --- a/paddle/phi/kernels/sum_as_grad_kernel.h +++ b/paddle/phi/kernels/sum_as_grad_kernel.h @@ -26,7 +26,6 @@ void SumAsGradKernel(const Context& dev_ctx, const DenseTensor& x, const DenseTensor& y, const DenseTensor& out_grad, - bool keep_dim, DenseTensor* x_grad); } // namespace phi diff --git a/paddle/phi/kernels/sum_as_kernel.h b/paddle/phi/kernels/sum_as_kernel.h index f2356c8828c902..fcceccadeb4dea 100644 --- a/paddle/phi/kernels/sum_as_kernel.h +++ b/paddle/phi/kernels/sum_as_kernel.h @@ -25,8 +25,6 @@ template void SumAsKernel(const Context& dev_ctx, const DenseTensor& x, const DenseTensor& y, - DataType out_dtype, - bool keep_dim, DenseTensor* out); } // namespace phi From b7760daee9ca7827ebf4700ff1a49507636607d2 Mon Sep 17 00:00:00 2001 From: zeroRains Date: Fri, 29 Mar 2024 10:26:46 +0000 Subject: [PATCH 05/30] fix the python registor --- python/paddle/tensor/math.py | 24 ++----- test/legacy_test/test_sum_as_op.py | 107 ++++++++++++++++++++++++----- 2 files changed, 96 insertions(+), 35 deletions(-) diff --git a/python/paddle/tensor/math.py b/python/paddle/tensor/math.py index c1ec699b81db34..a6dda221ce7a4b 100644 --- a/python/paddle/tensor/math.py +++ b/python/paddle/tensor/math.py @@ -1581,7 +1581,7 @@ def sum(x, axis=None, dtype=None, keepdim=False, name=None): return out -def sum_as(x, y, dtype=None, keepdim=False, name=None): +def sum_as(x, y, name=None): """ Computes the sum_as of tensor elements over the given dimension. @@ -1609,19 +1609,9 @@ def sum_as(x, y, dtype=None, keepdim=False, name=None): """ - dtype_flag = False - if dtype is not None: - dtype_flag = True - dtype = convert_np_dtype_to_dtype_(dtype) - if in_dynamic_or_pir_mode(): - return _C_ops.sum_as(x, y, dtype, keepdim) + return _C_ops.sum_as(x, y) else: - attrs = {'keep_dim': keepdim} - - if dtype_flag: - attrs.update({'in_dtype': x.dtype, 'out_dtype': dtype}) - check_variable_and_dtype( x, 'x', @@ -1654,15 +1644,11 @@ def sum_as(x, y, dtype=None, keepdim=False, name=None): ) helper = LayerHelper('sum_as', **locals()) - if dtype_flag: - out = helper.create_variable_for_type_inference(dtype=dtype) - else: - out = helper.create_variable_for_type_inference(dtype=x.dtype) + out = helper.create_variable_for_type_inference(dtype=x.dtype) helper.append_op( - type='sum_x', - inputs={'x': x, 'Y': y}, + type='sum_as', + inputs={'x': x, 'y': y}, outputs={'out': out}, - attrs=attrs, ) return out diff --git a/test/legacy_test/test_sum_as_op.py b/test/legacy_test/test_sum_as_op.py index c956fd938a93ed..91797e681a288c 100644 --- a/test/legacy_test/test_sum_as_op.py +++ b/test/legacy_test/test_sum_as_op.py @@ -15,7 +15,6 @@ import unittest import numpy as np -from op_test import OpTest import paddle @@ -35,24 +34,100 @@ def sum_as_net(x, y): return np.sum(x, axis=tuple(axis), dtype=out_dtype) -class TestSumAsOp(OpTest): - def setUp(self): - self.op_type = "sum_as" - self.python_api = paddle.sum_as - self.init_config() - self.inputs = {'x': self.x, 'y': self.y} - self.target = sum_as_net(self.inputs['x'], self.inputs['y']) - self.outputs = {'out': self.target} +# class TestSumAsOp(OpTest): +# def setUp(self): +# self.op_type = "sum_as" +# self.python_api = paddle.sum_as +# self.init_config() +# self.inputs = {'x': self.x, 'y': self.y} +# self.target = sum_as_net(self.inputs['x'], self.inputs['y']) +# self.outputs = {'out': self.target} - def test_check_output(self): - self.check_output() +# def test_check_output(self): +# self.check_output() - def test_check_grad(self): - self.check_grad(['x'], ['out']) +# def test_check_grad(self): +# self.check_grad(['x'], ['out']) - def init_config(self): - self.x = np.random.randn(300, 200, 600).astype('float64') - self.y = np.random.randn(200, 600).astype('float64') +# def init_config(self): +# self.x = np.random.randn(300, 200, 600).astype('float64') +# self.y = np.random.randn(200, 600).astype('float64') + + +class TestSumAsOp2(unittest.TestCase): + def test_sum_as(self): + np.random.seed(2023) + data = np.random.randn(300, 200, 600).astype('float64') + x = paddle.to_tensor(data, stop_gradient=False) + y = paddle.to_tensor(np.random.randn(200, 600).astype('float64')) + + # forward + p = paddle.to_tensor(data, stop_gradient=False) + reduce_dims = [0] + out = paddle.sum_as(x, y) + ans = paddle.sum(p, axis=reduce_dims) + np.testing.assert_allclose(out.numpy(), ans.numpy(), rtol=1e-5) + + # backward + out.backward() + x_grad = x.grad.numpy() + p.backward() + p_grad = p.grad.numpy() + np.testing.assert_allclose(x_grad, p_grad, rtol=1e-5) + + +class TestSumAsOp3(unittest.TestCase): + def test_sum_as_static(self): + np.random.seed(2023) + paddle.disable_static() + + def forward(x, y): + return paddle.sum_as(x, y) + + paddle.disable_static() + data = np.random.randn(300, 200, 600).astype('float64') + x = paddle.to_tensor(data, stop_gradient=False) + y = paddle.to_tensor(np.random.randn(200, 600).astype('float64')) + + out_dynamic = forward(x, y) + out_dynamic_result = out_dynamic.numpy() + out_dynamic.backward() + x_dynamic_grad = x.grad.numpy() + x.stop_gradient = False + + paddle.enable_static() + + with paddle.static.program_guard(paddle.static.Program()): + x_static = paddle.static.data( + name='x', shape=[300, 200, 600], dtype='float64' + ) + y_static = paddle.static.data( + name='y', shape=[200, 600], dtype='float64' + ) + out_static = forward(x_static, y_static) + loss = paddle.mean(out_static) + + exe = paddle.static.Executor() + x_np = data + y_np = y.numpy() + res = exe.run( + paddle.static.default_main_program(), + fetch_list=[loss, out_static], + feed={'x': x_np, 'y': y_np}, + ) + loss_val, out_static_result = res + + np.testing.assert_allclose( + out_static_result, out_dynamic_result, rtol=1e-5 + ) + + exe.run( + paddle.static.default_main_program(), + fetch_list=[x_static.grad], + feed={'x': x_np, 'y': y_np}, + ) + x_static_grad = x_static.grad.numpy() + np.testing.assert_allclose(x_static_grad, x_dynamic_grad, rtol=1e-5) if __name__ == "__main__": From 5c8bc4c8547941f5085c94a096df0971f31455aa Mon Sep 17 00:00:00 2001 From: zeroRains Date: Sat, 30 Mar 2024 12:00:43 +0000 Subject: [PATCH 06/30] modif the test and and some case --- test/legacy_test/test_sum_as_op.py | 254 ++++++++++++++++++++--------- 1 file changed, 175 insertions(+), 79 deletions(-) diff --git a/test/legacy_test/test_sum_as_op.py b/test/legacy_test/test_sum_as_op.py index 91797e681a288c..4951c064071a69 100644 --- a/test/legacy_test/test_sum_as_op.py +++ b/test/legacy_test/test_sum_as_op.py @@ -22,8 +22,7 @@ paddle.seed(100) -def sum_as_net(x, y): - out_dtype = x.dtype +def get_reduce_dims(x, y): diff = len(x.shape) - len(y.shape) axis = [] for i in range(diff): @@ -31,104 +30,201 @@ def sum_as_net(x, y): for i in range(len(y.shape)): if y.shape[i] != x.shape[i + diff]: axis.append(i + diff) - return np.sum(x, axis=tuple(axis), dtype=out_dtype) + return axis -# class TestSumAsOp(OpTest): -# def setUp(self): -# self.op_type = "sum_as" -# self.python_api = paddle.sum_as -# self.init_config() -# self.inputs = {'x': self.x, 'y': self.y} -# self.target = sum_as_net(self.inputs['x'], self.inputs['y']) -# self.outputs = {'out': self.target} +class TestSumAsOp(unittest.TestCase): + def init_type(self): + self.dtype = 'float64' -# def test_check_output(self): -# self.check_output() + def init_shape(self): + self.shape_x = [300, 200, 600] + self.shape_y = [200, 600] -# def test_check_grad(self): -# self.check_grad(['x'], ['out']) + def init_data(self): + self.x = np.random.random(self.shape_x).astype(self.dtype) + self.y = np.random.random(self.shape_y).astype(self.dtype) -# def init_config(self): -# self.x = np.random.randn(300, 200, 600).astype('float64') -# self.y = np.random.randn(200, 600).astype('float64') - - -class TestSumAsOp2(unittest.TestCase): - def test_sum_as(self): + def init(self): np.random.seed(2023) - data = np.random.randn(300, 200, 600).astype('float64') - x = paddle.to_tensor(data, stop_gradient=False) - y = paddle.to_tensor(np.random.randn(200, 600).astype('float64')) + self.init_type() + self.init_shape() + self.init_data() - # forward - p = paddle.to_tensor(data, stop_gradient=False) - reduce_dims = [0] - out = paddle.sum_as(x, y) - ans = paddle.sum(p, axis=reduce_dims) - np.testing.assert_allclose(out.numpy(), ans.numpy(), rtol=1e-5) + def test_sum_as_dynamic(self): + self.init() + paddle.disable_static() + x = paddle.to_tensor(self.x, stop_gradient=False) + y = paddle.to_tensor(self.y) + p = paddle.to_tensor(self.x, stop_gradient=False) - # backward + out = paddle.sum_as(x, y) out.backward() x_grad = x.grad.numpy() - p.backward() + + reduce_dims = get_reduce_dims(self.x, self.y) + ans = paddle.sum(p, axis=reduce_dims) + ans.backward() p_grad = p.grad.numpy() - np.testing.assert_allclose(x_grad, p_grad, rtol=1e-5) + # check forward + np.testing.assert_allclose(out.numpy(), ans.numpy(), rtol=1e-6) + # check backward + np.testing.assert_allclose(x_grad, p_grad, rtol=1e-6) -class TestSumAsOp3(unittest.TestCase): def test_sum_as_static(self): - np.random.seed(2023) - paddle.disable_static() - - def forward(x, y): + self.init() + + def base_net(flag=None): + if flag == 'static': + # static graph + paddle.enable_static() + main_program = paddle.static.Program() + with paddle.static.program_guard(main_program): + x = paddle.static.data('x', self.shape_x, dtype=self.dtype) + y = paddle.static.data('y', self.shape_y, dtype=self.dtype) + x.stop_gradient = False + out = paddle.sum_as(x, y) + gradients = paddle.static.gradients(out, [x]) + exe = paddle.static.Executor() + + [fwd, dx] = exe.run( + feed={'x': self.x, 'y': self.y}, + fetch_list=[out, gradients], + ) + else: + # dynamic graph + paddle.disable_static() + x = paddle.to_tensor(self.x, stop_gradient=False) + y = paddle.to_tensor(self.y) + # axis = get_reduce_dims(x,y) + # fwd = paddle.sum(x, axis=axis) + fwd = paddle.sum_as(x, y) + fwd.backward() + dx = x.grad.numpy() + fwd = fwd.numpy() + return fwd, dx + + res_ref = base_net() + res = base_net('static') + for ref, actual in zip(res_ref, res): + np.testing.assert_allclose(ref, actual, rtol=1e-6) + + def test_sum_as_dynamic_to_static(self): + self.init() + paddle.core._set_prim_all_enabled(True) + + def func(x, y): return paddle.sum_as(x, y) - paddle.disable_static() - data = np.random.randn(300, 200, 600).astype('float64') - x = paddle.to_tensor(data, stop_gradient=False) - y = paddle.to_tensor(np.random.randn(200, 600).astype('float64')) - - out_dynamic = forward(x, y) - out_dynamic_result = out_dynamic.numpy() - out_dynamic.backward() - x_dynamic_grad = x.grad.numpy() - x.stop_gradient = False - - paddle.enable_static() - - with paddle.static.program_guard(paddle.static.Program()): - x_static = paddle.static.data( - name='x', shape=[300, 200, 600], dtype='float64' - ) - y_static = paddle.static.data( - name='y', shape=[200, 600], dtype='float64' - ) - out_static = forward(x_static, y_static) - loss = paddle.mean(out_static) - - exe = paddle.static.Executor() - x_np = data - y_np = y.numpy() - res = exe.run( - paddle.static.default_main_program(), - fetch_list=[loss, out_static], - feed={'x': x_np, 'y': y_np}, - ) - loss_val, out_static_result = res + static_func = paddle.jit.to_static(func, full_graph=True) + + # ==== dygraph computation ==== + x = paddle.to_tensor(self.x, stop_gradient=False) + y = paddle.to_tensor(self.y) + ref_out = func(x, y) + ref_out.backward() + ref_grad = x.grad.numpy() + x.clear_gradient() + + # ==== to static compuatation ==== + actual_out = static_func(x, y) + actual_out.backward() + actual_grad = x.grad.numpy() + + paddle.core._set_prim_all_enabled(False) np.testing.assert_allclose( - out_static_result, out_dynamic_result, rtol=1e-5 + ref_out.numpy(), actual_out.numpy(), atol=1e-6, rtol=1e-6 ) - exe.run( - paddle.static.default_main_program(), - fetch_list=[x_static.grad], - feed={'x': x_np, 'y': y_np}, - ) - x_static_grad = x_static.grad.numpy() - np.testing.assert_allclose(x_static_grad, x_dynamic_grad, rtol=1e-5) + np.testing.assert_allclose(ref_grad, actual_grad, atol=1e-6, rtol=1e-6) + + +class TestSumAsOp2(TestSumAsOp): + def init_shape(self): + self.shape_x = [300, 200, 600] + self.shape_y = [600] + + +class TestSumAsOp3(TestSumAsOp): + def init_type(self): + self.dtype = 'float32' + +# class TestSumAsOp4(TestSumAsOp): +# def init_type(self): +# self.dtype = 'bool' + + +class TestSumAsOp5(TestSumAsOp): + def init_type(self): + self.dtype = 'float16' + + +class TestSumAsOp6(TestSumAsOp): + def init_type(self): + self.dtype = 'uint16' + + +class TestSumAsOp7(TestSumAsOp): + def init_type(self): + self.dtype = 'int16' + + +# class TestSumAsOp8(TestSumAsOp): +# def init_type(self): +# self.dtype = 'int32' + + +class TestSumAsOp9(TestSumAsOp): + def init_type(self): + self.dtype = 'int64' + + +''' +# op_test +class TestSumAsOp4(OpTest): + + def setUp(self): + self.init_dtype() + self.init_input() + self.init_attrs() + self.calc_output() + + self.python_api = paddle.sum_as + self.public_python_api = paddle.sum_as + self.op_type = "sum_as" + self.prim_op_type = "prim" + self.inputs = {'x': self.x, 'y': self.y} + self.outputs = {'out': self.out} + self.if_enable_cinn() + + def init_dtype(self): + self.dtype = np.float64 + + def init_input(self): + self.x = np.random.randn(300, 200, 600).astype(self.dtype) + self.y = np.random.randn(200, 600).astype(self.dtype) + + def init_attrs(self): + self.attrs = {'dim': [0]} + + def if_enable_cinn(self): + pass + + def calc_output(self): + self.out = self.x.sum(axis=tuple(self.attrs['dim'])) + + def test_check_output(self): + self.check_output() + + def test_check_grad(self): + self.check_grad( + ['x','y'], + 'out', + ) +''' if __name__ == "__main__": paddle.enable_static() From a16be62aeabeebd09ed2c7a097d09cf044debfa0 Mon Sep 17 00:00:00 2001 From: zeroRains Date: Sat, 30 Mar 2024 12:26:05 +0000 Subject: [PATCH 07/30] modify the description of python api --- python/paddle/tensor/math.py | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/python/paddle/tensor/math.py b/python/paddle/tensor/math.py index a6dda221ce7a4b..8b6bd0bf509517 100644 --- a/python/paddle/tensor/math.py +++ b/python/paddle/tensor/math.py @@ -1583,30 +1583,33 @@ def sum(x, axis=None, dtype=None, keepdim=False, name=None): def sum_as(x, y, name=None): """ - Computes the sum_as of tensor elements over the given dimension. + Computes the sum of tensor elements make the shape of its result equal to the shape of y. Args: x (Tensor): An N-D Tensor, the data type is bool, float16, float32, float64, int32 or int64. - axis (int|list|tuple, optional): The dimensions along which the sum is performed. If - :attr:`None`, sum all elements of :attr:`x` and return a - Tensor with a single element, otherwise must be in the - range :math:`[-rank(x), rank(x))`. If :math:`axis[i] < 0`, - the dimension to reduce is :math:`rank + axis[i]`. - dtype (str, optional): The dtype of output Tensor. The default value is None, the dtype - of output is the same as input Tensor `x`. - keepdim (bool, optional): Whether to reserve the reduced dimension in the - output Tensor. The result Tensor will have one fewer dimension - than the :attr:`x` unless :attr:`keepdim` is true, default - value is False. + y (Tensor): An N-D Tensor, the length of x shape must greater than or equal to the length of + y shape. The data type is bool, float16, float32, float64, int32 or int64. name (str, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`. Returns: - Tensor: Results of summation operation on the specified axis of input Tensor `x`, + Tensor: The result of a calculation along some axis of the input tensor x, whose shape is the same as the shape of the input tensor y, if `x.dtype='bool'`, `x.dtype='int32'`, it's data type is `'int64'`, otherwise it's data type is the same as `x`. Examples: - + .. code-block:: python + >>> x = paddle.to_tensor([[1, 2, 3, 4], [5, 6, 7, 8]]) + >>> x + Tensor(shape=[2, 4], dtype=int64, place=Place(gpu:0), stop_gradient=True, + [[1, 2, 3, 4], + [5, 6, 7, 8]]) + >>> y = paddle.to_tensor([1, 2, 3, 4]) + >>> y + Tensor(shape=[4], dtype=int64, place=Place(gpu:0), stop_gradient=True, + [1, 2, 3, 4]) + >>> paddle.sum_as(x, y) + Tensor(shape=[4], dtype=int64, place=Place(gpu:0), stop_gradient=True, + [6 , 8 , 10, 12]) """ if in_dynamic_or_pir_mode(): From da822f5f1e12e24d9e4716d84f2b4f516971a5b2 Mon Sep 17 00:00:00 2001 From: zeroRains Date: Sat, 30 Mar 2024 12:30:36 +0000 Subject: [PATCH 08/30] fix tyop --- python/paddle/tensor/math.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/paddle/tensor/math.py b/python/paddle/tensor/math.py index 8b6bd0bf509517..f3f638d8b46c6b 100644 --- a/python/paddle/tensor/math.py +++ b/python/paddle/tensor/math.py @@ -1592,7 +1592,7 @@ def sum_as(x, y, name=None): name (str, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`. Returns: - Tensor: The result of a calculation along some axis of the input tensor x, whose shape is the same as the shape of the input tensor y, + Tensor: The sum of the input tensor x along some axis has the same shape as the shape of the input tensor y, if `x.dtype='bool'`, `x.dtype='int32'`, it's data type is `'int64'`, otherwise it's data type is the same as `x`. From 38bd3329a9747e18d19b67c4dfeb95d2404176ce Mon Sep 17 00:00:00 2001 From: zeroRains Date: Sun, 31 Mar 2024 06:06:41 +0000 Subject: [PATCH 09/30] fix the bug in test which write base on OpTest --- test/legacy_test/test_sum_as_op.py | 206 ++++++++--------------------- 1 file changed, 52 insertions(+), 154 deletions(-) diff --git a/test/legacy_test/test_sum_as_op.py b/test/legacy_test/test_sum_as_op.py index 4951c064071a69..4dda6a37d8b911 100644 --- a/test/legacy_test/test_sum_as_op.py +++ b/test/legacy_test/test_sum_as_op.py @@ -15,6 +15,7 @@ import unittest import numpy as np +from op_test import OpTest import paddle @@ -33,198 +34,95 @@ def get_reduce_dims(x, y): return axis -class TestSumAsOp(unittest.TestCase): - def init_type(self): - self.dtype = 'float64' +class TestSumAsOp(OpTest): + def setUp(self): + self.init_dtype() + self.init_shape() + self.init_input() + self.init_attrs() + self.calc_output() + + self.python_api = paddle.sum_as + self.public_python_api = paddle.sum_as + self.op_type = "sum_as" + self.prim_op_type = "prim" + self.inputs = {'x': self.x, 'y': self.y} + self.outputs = {'out': self.out} + self.if_enable_cinn() + + def init_dtype(self): + self.dtype = np.float64 def init_shape(self): - self.shape_x = [300, 200, 600] - self.shape_y = [200, 600] + self.shape_x = [30, 10, 6] + self.shape_y = [10, 6] - def init_data(self): + def init_input(self): self.x = np.random.random(self.shape_x).astype(self.dtype) self.y = np.random.random(self.shape_y).astype(self.dtype) - def init(self): - np.random.seed(2023) - self.init_type() - self.init_shape() - self.init_data() - - def test_sum_as_dynamic(self): - self.init() - paddle.disable_static() - x = paddle.to_tensor(self.x, stop_gradient=False) - y = paddle.to_tensor(self.y) - p = paddle.to_tensor(self.x, stop_gradient=False) - - out = paddle.sum_as(x, y) - out.backward() - x_grad = x.grad.numpy() - - reduce_dims = get_reduce_dims(self.x, self.y) - ans = paddle.sum(p, axis=reduce_dims) - ans.backward() - p_grad = p.grad.numpy() - - # check forward - np.testing.assert_allclose(out.numpy(), ans.numpy(), rtol=1e-6) - # check backward - np.testing.assert_allclose(x_grad, p_grad, rtol=1e-6) - - def test_sum_as_static(self): - self.init() - - def base_net(flag=None): - if flag == 'static': - # static graph - paddle.enable_static() - main_program = paddle.static.Program() - with paddle.static.program_guard(main_program): - x = paddle.static.data('x', self.shape_x, dtype=self.dtype) - y = paddle.static.data('y', self.shape_y, dtype=self.dtype) - x.stop_gradient = False - out = paddle.sum_as(x, y) - gradients = paddle.static.gradients(out, [x]) - exe = paddle.static.Executor() - - [fwd, dx] = exe.run( - feed={'x': self.x, 'y': self.y}, - fetch_list=[out, gradients], - ) - else: - # dynamic graph - paddle.disable_static() - x = paddle.to_tensor(self.x, stop_gradient=False) - y = paddle.to_tensor(self.y) - # axis = get_reduce_dims(x,y) - # fwd = paddle.sum(x, axis=axis) - fwd = paddle.sum_as(x, y) - fwd.backward() - dx = x.grad.numpy() - fwd = fwd.numpy() - return fwd, dx - - res_ref = base_net() - res = base_net('static') - for ref, actual in zip(res_ref, res): - np.testing.assert_allclose(ref, actual, rtol=1e-6) - - def test_sum_as_dynamic_to_static(self): - self.init() - paddle.core._set_prim_all_enabled(True) - - def func(x, y): - return paddle.sum_as(x, y) - - static_func = paddle.jit.to_static(func, full_graph=True) - - # ==== dygraph computation ==== - x = paddle.to_tensor(self.x, stop_gradient=False) - y = paddle.to_tensor(self.y) - ref_out = func(x, y) - ref_out.backward() - ref_grad = x.grad.numpy() - x.clear_gradient() - - # ==== to static compuatation ==== - actual_out = static_func(x, y) - actual_out.backward() - actual_grad = x.grad.numpy() - - paddle.core._set_prim_all_enabled(False) - - np.testing.assert_allclose( - ref_out.numpy(), actual_out.numpy(), atol=1e-6, rtol=1e-6 - ) + def init_attrs(self): + self.attrs = {'dim': [0]} - np.testing.assert_allclose(ref_grad, actual_grad, atol=1e-6, rtol=1e-6) + def if_enable_cinn(self): + pass + def calc_output(self): + self.out = self.x.sum(axis=tuple(self.attrs['dim'])) -class TestSumAsOp2(TestSumAsOp): - def init_shape(self): - self.shape_x = [300, 200, 600] - self.shape_y = [600] + def test_check_output(self): + self.check_output() + def test_check_grad(self): + self.check_grad( + ['x'], + 'out', + ) -class TestSumAsOp3(TestSumAsOp): + +class TestSumAsOp2(TestSumAsOp): def init_type(self): self.dtype = 'float32' -# class TestSumAsOp4(TestSumAsOp): -# def init_type(self): -# self.dtype = 'bool' - - -class TestSumAsOp5(TestSumAsOp): +class TestSumAsOp3(TestSumAsOp): def init_type(self): self.dtype = 'float16' -class TestSumAsOp6(TestSumAsOp): +class TestSumAsOp4(TestSumAsOp): def init_type(self): self.dtype = 'uint16' -class TestSumAsOp7(TestSumAsOp): +class TestSumAsOp5(TestSumAsOp): def init_type(self): self.dtype = 'int16' -# class TestSumAsOp8(TestSumAsOp): -# def init_type(self): -# self.dtype = 'int32' - - -class TestSumAsOp9(TestSumAsOp): +class TestSumAsOp6(TestSumAsOp): def init_type(self): self.dtype = 'int64' -''' -# op_test -class TestSumAsOp4(OpTest): +class TestSumAsOp7(TestSumAsOp): + def init_type(self): + self.dtype = 'bool' - def setUp(self): - self.init_dtype() - self.init_input() - self.init_attrs() - self.calc_output() - self.python_api = paddle.sum_as - self.public_python_api = paddle.sum_as - self.op_type = "sum_as" - self.prim_op_type = "prim" - self.inputs = {'x': self.x, 'y': self.y} - self.outputs = {'out': self.out} - self.if_enable_cinn() +class TestSumAsOp8(TestSumAsOp): + def init_type(self): + self.dtype = 'int32' - def init_dtype(self): - self.dtype = np.float64 - def init_input(self): - self.x = np.random.randn(300, 200, 600).astype(self.dtype) - self.y = np.random.randn(200, 600).astype(self.dtype) +class TestSumAsOp9(TestSumAsOp): + def init_shape(self): + self.shape_x = [30, 10, 6] + self.shape_y = [6] def init_attrs(self): - self.attrs = {'dim': [0]} - - def if_enable_cinn(self): - pass + self.attrs = {'dim': [0, 1]} - def calc_output(self): - self.out = self.x.sum(axis=tuple(self.attrs['dim'])) - - def test_check_output(self): - self.check_output() - - def test_check_grad(self): - self.check_grad( - ['x','y'], - 'out', - ) -''' if __name__ == "__main__": paddle.enable_static() From 9e7fa93c3f8591295cbb710eced7f10dac42eead Mon Sep 17 00:00:00 2001 From: zeroRains Date: Sun, 31 Mar 2024 06:08:36 +0000 Subject: [PATCH 10/30] remove the useless function in test --- test/legacy_test/test_sum_as_op.py | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/test/legacy_test/test_sum_as_op.py b/test/legacy_test/test_sum_as_op.py index 4dda6a37d8b911..ad2016d37a0423 100644 --- a/test/legacy_test/test_sum_as_op.py +++ b/test/legacy_test/test_sum_as_op.py @@ -23,17 +23,6 @@ paddle.seed(100) -def get_reduce_dims(x, y): - diff = len(x.shape) - len(y.shape) - axis = [] - for i in range(diff): - axis.append(i) - for i in range(len(y.shape)): - if y.shape[i] != x.shape[i + diff]: - axis.append(i + diff) - return axis - - class TestSumAsOp(OpTest): def setUp(self): self.init_dtype() From d8c22f2f5b67e47a6d0072116ff0cdf7d3a3b2bc Mon Sep 17 00:00:00 2001 From: zeroRains Date: Sun, 31 Mar 2024 07:40:18 +0000 Subject: [PATCH 11/30] modift the size of the test tenor --- test/legacy_test/test_sum_as_op.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/legacy_test/test_sum_as_op.py b/test/legacy_test/test_sum_as_op.py index ad2016d37a0423..4579c139862f31 100644 --- a/test/legacy_test/test_sum_as_op.py +++ b/test/legacy_test/test_sum_as_op.py @@ -43,7 +43,7 @@ def init_dtype(self): self.dtype = np.float64 def init_shape(self): - self.shape_x = [30, 10, 6] + self.shape_x = [10, 10, 6] self.shape_y = [10, 6] def init_input(self): @@ -106,7 +106,7 @@ def init_type(self): class TestSumAsOp9(TestSumAsOp): def init_shape(self): - self.shape_x = [30, 10, 6] + self.shape_x = [10, 10, 6] self.shape_y = [6] def init_attrs(self): From 6101781f0b92a0256cd9a74a6cec876db9b55a79 Mon Sep 17 00:00:00 2001 From: cyber-pioneer <116002591+cyber-pioneer@users.noreply.github.com> Date: Mon, 1 Apr 2024 18:37:29 +0800 Subject: [PATCH 12/30] Update test/legacy_test/test_sum_as_op.py --- test/legacy_test/test_sum_as_op.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/legacy_test/test_sum_as_op.py b/test/legacy_test/test_sum_as_op.py index 4579c139862f31..1295401787c235 100644 --- a/test/legacy_test/test_sum_as_op.py +++ b/test/legacy_test/test_sum_as_op.py @@ -60,7 +60,7 @@ def calc_output(self): self.out = self.x.sum(axis=tuple(self.attrs['dim'])) def test_check_output(self): - self.check_output() + self.check_output(check_pir=True) def test_check_grad(self): self.check_grad( From 91d6d2b7013aa9229b783223fdc320fc665b11ea Mon Sep 17 00:00:00 2001 From: cyber-pioneer <116002591+cyber-pioneer@users.noreply.github.com> Date: Mon, 1 Apr 2024 18:37:41 +0800 Subject: [PATCH 13/30] Update test/legacy_test/test_sum_as_op.py --- test/legacy_test/test_sum_as_op.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/legacy_test/test_sum_as_op.py b/test/legacy_test/test_sum_as_op.py index 1295401787c235..5e19b2bf8082d1 100644 --- a/test/legacy_test/test_sum_as_op.py +++ b/test/legacy_test/test_sum_as_op.py @@ -66,7 +66,7 @@ def test_check_grad(self): self.check_grad( ['x'], 'out', - ) + check_pir=True,) class TestSumAsOp2(TestSumAsOp): From 11e96ed0fbee6f0c07db96daf6ab0250fd9b8844 Mon Sep 17 00:00:00 2001 From: zeroRains Date: Mon, 1 Apr 2024 11:01:08 +0000 Subject: [PATCH 14/30] fix code style --- test/legacy_test/test_sum_as_op.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/legacy_test/test_sum_as_op.py b/test/legacy_test/test_sum_as_op.py index 5e19b2bf8082d1..b3ebff5ed62bf6 100644 --- a/test/legacy_test/test_sum_as_op.py +++ b/test/legacy_test/test_sum_as_op.py @@ -66,7 +66,8 @@ def test_check_grad(self): self.check_grad( ['x'], 'out', - check_pir=True,) + check_pir=True, + ) class TestSumAsOp2(TestSumAsOp): From 4636403bd1d01045bfde4b44b5e0afb2fc4c4b2e Mon Sep 17 00:00:00 2001 From: zeroRains Date: Thu, 4 Apr 2024 07:32:21 +0000 Subject: [PATCH 15/30] add dynamic shape test and modify the doc --- python/paddle/pir/core.py | 13 ++++--- python/paddle/tensor/math.py | 22 ++++++------ test/legacy_test/test_sum_as_op.py | 56 ++++++++++++++++++++++++++++++ 3 files changed, 75 insertions(+), 16 deletions(-) diff --git a/python/paddle/pir/core.py b/python/paddle/pir/core.py index 1c5c12c94a6ae1..0d4a313523f88f 100644 --- a/python/paddle/pir/core.py +++ b/python/paddle/pir/core.py @@ -86,12 +86,15 @@ def convert_np_dtype_to_dtype_(np_dtype): """ # Convert the data type string to numpy data type. - if np_dtype == "bfloat16": - # since there is still no support for bfloat16 in NumPy, - # uint16 is used for casting bfloat16 - dtype = np.dtype("uint16") + if isinstance(np_dtype, str): + if np_dtype == "bfloat16": + # since there is still no support for bfloat16 in NumPy, + # uint16 is used for casting bfloat16 + dtype = np.dtype("uint16") + else: + dtype = np.dtype(np_dtype) else: - dtype = np.dtype(np_dtype) + dtype = np_dtype if dtype in np_type_to_paddle_type.keys(): return np_type_to_paddle_type[dtype] diff --git a/python/paddle/tensor/math.py b/python/paddle/tensor/math.py index f250f4d53417a3..5ef0cdd35bd213 100644 --- a/python/paddle/tensor/math.py +++ b/python/paddle/tensor/math.py @@ -1582,29 +1582,29 @@ def sum_as(x, y, name=None): Args: x (Tensor): An N-D Tensor, the data type is bool, float16, float32, float64, int32 or int64. - y (Tensor): An N-D Tensor, the length of x shape must greater than or equal to the length of - y shape. The data type is bool, float16, float32, float64, int32 or int64. - name (str, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`. + y (Tensor): An N-D Tensor, the length of x shape must greater than or equal to the length of y shape. The data type is bool, float16, float32, float64, int32 or int64. Returns: - Tensor: The sum of the input tensor x along some axis has the same shape as the shape of the input tensor y, - if `x.dtype='bool'`, `x.dtype='int32'`, it's data type is `'int64'`, - otherwise it's data type is the same as `x`. + Tensor: The sum of the input tensor x along some axis has the same shape as the shape of the input tensor y, if `x.dtype='bool'`, `x.dtype='int32'`, it's data type is `'int64'`, otherwise it's data type is the same as `x`. Examples: .. code-block:: python + + >>> import paddle + >>> x = paddle.to_tensor([[1, 2, 3, 4], [5, 6, 7, 8]]) >>> x Tensor(shape=[2, 4], dtype=int64, place=Place(gpu:0), stop_gradient=True, - [[1, 2, 3, 4], - [5, 6, 7, 8]]) + [[1, 2, 3, 4], + [5, 6, 7, 8]]) >>> y = paddle.to_tensor([1, 2, 3, 4]) >>> y Tensor(shape=[4], dtype=int64, place=Place(gpu:0), stop_gradient=True, - [1, 2, 3, 4]) - >>> paddle.sum_as(x, y) + [1, 2, 3, 4]) + >>> res = paddle.sum_as(x, y) + >>> res Tensor(shape=[4], dtype=int64, place=Place(gpu:0), stop_gradient=True, - [6 , 8 , 10, 12]) + [6 , 8 , 10, 12]) """ if in_dynamic_or_pir_mode(): diff --git a/test/legacy_test/test_sum_as_op.py b/test/legacy_test/test_sum_as_op.py index b3ebff5ed62bf6..d5f08f0d1733dc 100644 --- a/test/legacy_test/test_sum_as_op.py +++ b/test/legacy_test/test_sum_as_op.py @@ -18,11 +18,27 @@ from op_test import OpTest import paddle +from paddle.static import InputSpec np.random.seed(100) paddle.seed(100) +def sum_as_net(x, y): + return paddle.sum_as(x, y) + + +def apply_to_static(net, use_cinn, input_spec=None): + build_strategy = paddle.static.BuildStrategy() + build_strategy.build_cinn_pass = use_cinn + return paddle.jit.to_static( + net, + input_spec=input_spec, + build_strategy=build_strategy, + full_graph=True, + ) + + class TestSumAsOp(OpTest): def setUp(self): self.init_dtype() @@ -114,6 +130,46 @@ def init_attrs(self): self.attrs = {'dim': [0, 1]} +class TestSumAsDynamicShape(unittest.TestCase): + def setUp(self): + np.random.seed(2023) + self.shape_x = [300, 200, 100] + self.shape_y = [200, 100] + self.dtype_x = "float32" + self.dtype_y = "float32" + self.init_x_shape = [None, 200, 100] + self.init_y_shape = [200, 100] + self.x = np.random.random(self.shape_x).astype(self.dtype_x) + self.y = np.random.random(self.shape_y).astype(self.dtype_y) + self.net = sum_as_net + self.enable_cinn = False + self.tol = 1e-6 + + def base_net(self, flag=None): + x = paddle.to_tensor(self.x) + y = paddle.to_tensor(self.y) + if flag == "static": + fn = apply_to_static( + self.net, + use_cinn=self.enable_cinn, + input_spec=[ + InputSpec(shape=self.init_x_shape, dtype=self.dtype_x), + InputSpec(shape=self.init_y_shape, dtype=self.dtype_y), + ], + ) + fn.eval() + else: + fn = self.net + res = fn(x, y) + return res + + def test_all_dynamic(self): + res_ref = self.base_net() + res = self.base_net("static") + for ref, actual in zip(res_ref, res): + np.testing.assert_allclose(ref, actual, rtol=self.tol) + + if __name__ == "__main__": paddle.enable_static() unittest.main() From 89f0d8b8c3fc5850ebb8288acdb99a085e23cd74 Mon Sep 17 00:00:00 2001 From: Zero Rains Date: Thu, 4 Apr 2024 16:29:14 +0800 Subject: [PATCH 16/30] Update test_sum_as_op.py --- test/legacy_test/test_sum_as_op.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/legacy_test/test_sum_as_op.py b/test/legacy_test/test_sum_as_op.py index d5f08f0d1733dc..6154553db97e32 100644 --- a/test/legacy_test/test_sum_as_op.py +++ b/test/legacy_test/test_sum_as_op.py @@ -137,8 +137,8 @@ def setUp(self): self.shape_y = [200, 100] self.dtype_x = "float32" self.dtype_y = "float32" - self.init_x_shape = [None, 200, 100] - self.init_y_shape = [200, 100] + self.init_x_shape = [None, None, 100] + self.init_y_shape = [None, 100] self.x = np.random.random(self.shape_x).astype(self.dtype_x) self.y = np.random.random(self.shape_y).astype(self.dtype_y) self.net = sum_as_net From e7dd69a871b40be02e356a970ffe44a526d59f0f Mon Sep 17 00:00:00 2001 From: Zero Rains Date: Thu, 4 Apr 2024 16:32:37 +0800 Subject: [PATCH 17/30] Update test_sum_as_op.py --- test/legacy_test/test_sum_as_op.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/test/legacy_test/test_sum_as_op.py b/test/legacy_test/test_sum_as_op.py index 6154553db97e32..be0e23bef5abe9 100644 --- a/test/legacy_test/test_sum_as_op.py +++ b/test/legacy_test/test_sum_as_op.py @@ -48,9 +48,7 @@ def setUp(self): self.calc_output() self.python_api = paddle.sum_as - self.public_python_api = paddle.sum_as self.op_type = "sum_as" - self.prim_op_type = "prim" self.inputs = {'x': self.x, 'y': self.y} self.outputs = {'out': self.out} self.if_enable_cinn() From 133939184821cc834cbcaab1422c78f356a5556f Mon Sep 17 00:00:00 2001 From: zeroRains Date: Fri, 5 Apr 2024 06:57:58 +0000 Subject: [PATCH 18/30] fix the bug in convert_np_dtype_to_dtype_ --- python/paddle/pir/core.py | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/python/paddle/pir/core.py b/python/paddle/pir/core.py index 0d4a313523f88f..861b73ba8be0e7 100644 --- a/python/paddle/pir/core.py +++ b/python/paddle/pir/core.py @@ -86,13 +86,10 @@ def convert_np_dtype_to_dtype_(np_dtype): """ # Convert the data type string to numpy data type. - if isinstance(np_dtype, str): - if np_dtype == "bfloat16": - # since there is still no support for bfloat16 in NumPy, - # uint16 is used for casting bfloat16 - dtype = np.dtype("uint16") - else: - dtype = np.dtype(np_dtype) + if isinstance(np_dtype, str) and np_dtype == "bfloat16": + # since there is still no support for bfloat16 in NumPy, + # uint16 is used for casting bfloat16 + dtype = np.dtype("uint16") else: dtype = np_dtype From 76ff0e484d8a0206a53d4405f8daed7cdc500502 Mon Sep 17 00:00:00 2001 From: Zero Rains Date: Fri, 5 Apr 2024 15:45:29 +0800 Subject: [PATCH 19/30] Update core.py --- python/paddle/pir/core.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/paddle/pir/core.py b/python/paddle/pir/core.py index 861b73ba8be0e7..543091f1025489 100644 --- a/python/paddle/pir/core.py +++ b/python/paddle/pir/core.py @@ -91,7 +91,7 @@ def convert_np_dtype_to_dtype_(np_dtype): # uint16 is used for casting bfloat16 dtype = np.dtype("uint16") else: - dtype = np_dtype + dtype = np.dtype(np_dtype) if dtype in np_type_to_paddle_type.keys(): return np_type_to_paddle_type[dtype] From 05ad7c1f75ee023ec9015611c06eefef9dc0ed26 Mon Sep 17 00:00:00 2001 From: zeroRains Date: Sun, 7 Apr 2024 09:01:05 +0000 Subject: [PATCH 20/30] change the variable name --- paddle/phi/api/yaml/backward.yaml | 4 ++-- paddle/phi/api/yaml/ops.yaml | 2 +- paddle/phi/infermeta/binary.cc | 6 ++++-- paddle/phi/infermeta/binary.h | 4 +++- paddle/phi/kernels/cpu/sum_as_grad_kernel.cc | 5 ++--- paddle/phi/kernels/cpu/sum_as_kernel.cc | 5 ++--- paddle/phi/kernels/gpu/sum_as_grad_kernel.cu | 4 ++-- paddle/phi/kernels/gpu/sum_as_kernel.cu | 4 ++-- paddle/phi/kernels/sum_as_grad_kernel.h | 2 +- paddle/phi/kernels/sum_as_kernel.h | 2 +- python/paddle/tensor/math.py | 22 ++++++++++---------- test/legacy_test/test_sum_as_op.py | 6 +++--- 12 files changed, 34 insertions(+), 32 deletions(-) diff --git a/paddle/phi/api/yaml/backward.yaml b/paddle/phi/api/yaml/backward.yaml index ceeb62db8db3ae..ae368a7ec6cd0d 100644 --- a/paddle/phi/api/yaml/backward.yaml +++ b/paddle/phi/api/yaml/backward.yaml @@ -2408,8 +2408,8 @@ func : stanh_grad - backward_op : sum_as_grad - forward : sum_as(Tensor x, Tensor y) -> Tensor(out) - args : (Tensor x, Tensor y, Tensor out_grad) + forward : sum_as(Tensor x, Tensor target) -> Tensor(out) + args : (Tensor x, Tensor target, Tensor out_grad) output : Tensor(x_grad) infer_meta : func : UnchangedInferMeta diff --git a/paddle/phi/api/yaml/ops.yaml b/paddle/phi/api/yaml/ops.yaml index 36228453011905..c9606cbeb4ab7d 100755 --- a/paddle/phi/api/yaml/ops.yaml +++ b/paddle/phi/api/yaml/ops.yaml @@ -2770,7 +2770,7 @@ backward : stanh_grad - op : sum_as - args : (Tensor x, Tensor y) + args : (Tensor x, Tensor target) output : Tensor(out) infer_meta : func : SumAsInferMeta diff --git a/paddle/phi/infermeta/binary.cc b/paddle/phi/infermeta/binary.cc index 113efc993c80d0..c24468742b5582 100644 --- a/paddle/phi/infermeta/binary.cc +++ b/paddle/phi/infermeta/binary.cc @@ -3047,7 +3047,9 @@ void SequenceMaskInferMeta(const MetaTensor& x, y->set_dtype(out_dtype); } -void SumAsInferMeta(const MetaTensor& x, const MetaTensor& y, MetaTensor* out) { +void SumAsInferMeta(const MetaTensor& x, + const MetaTensor& target, + MetaTensor* out) { DataType out_dtype; if (x.dtype() == DataType::BOOL || x.dtype() == DataType::INT32) { out_dtype = DataType::INT64; @@ -3055,7 +3057,7 @@ void SumAsInferMeta(const MetaTensor& x, const MetaTensor& y, MetaTensor* out) { out_dtype = x.dtype(); } out->set_dtype(out_dtype); - out->set_dims(y.dims()); + out->set_dims(target.dims()); out->set_layout(x.layout()); } diff --git a/paddle/phi/infermeta/binary.h b/paddle/phi/infermeta/binary.h index 9039f2e7b077b3..cd0bce6c61ad80 100644 --- a/paddle/phi/infermeta/binary.h +++ b/paddle/phi/infermeta/binary.h @@ -524,7 +524,9 @@ void ShuffleBatchInferMeta(const MetaTensor& x, ); -void SumAsInferMeta(const MetaTensor& x, const MetaTensor& y, MetaTensor* out); +void SumAsInferMeta(const MetaTensor& x, + const MetaTensor& target, + MetaTensor* out); void SoftmaxMaskFuseInferMeta(const MetaTensor& x, const MetaTensor& mask, diff --git a/paddle/phi/kernels/cpu/sum_as_grad_kernel.cc b/paddle/phi/kernels/cpu/sum_as_grad_kernel.cc index 688a11bfa11404..871921727e0a5b 100644 --- a/paddle/phi/kernels/cpu/sum_as_grad_kernel.cc +++ b/paddle/phi/kernels/cpu/sum_as_grad_kernel.cc @@ -1,4 +1,3 @@ - // Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); @@ -24,10 +23,10 @@ namespace phi { template void SumAsGradKernel(const Context& dev_ctx, const DenseTensor& x, - const DenseTensor& y, + const DenseTensor& target, const DenseTensor& out_grad, DenseTensor* x_grad) { - auto reduce_dim = phi::funcs::GetReduceDims(x, y); + auto reduce_dim = phi::funcs::GetReduceDims(x, target); bool reduce_all = recompute_reduce_all(x, reduce_dim); ReduceGradKernel(dev_ctx, x, diff --git a/paddle/phi/kernels/cpu/sum_as_kernel.cc b/paddle/phi/kernels/cpu/sum_as_kernel.cc index 7eafbf8b8a2dbd..562e4d9a1f3947 100644 --- a/paddle/phi/kernels/cpu/sum_as_kernel.cc +++ b/paddle/phi/kernels/cpu/sum_as_kernel.cc @@ -1,4 +1,3 @@ - // Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); @@ -24,9 +23,9 @@ namespace phi { template void SumAsKernel(const Context& dev_ctx, const DenseTensor& x, - const DenseTensor& y, + const DenseTensor& target, DenseTensor* out) { - auto reduce_dim = phi::funcs::GetReduceDims(x, y); + auto reduce_dim = phi::funcs::GetReduceDims(x, target); bool reduce_all = recompute_reduce_all(x, reduce_dim); phi::Reduce( dev_ctx, x, reduce_all, reduce_dim, false, out->type(), out); diff --git a/paddle/phi/kernels/gpu/sum_as_grad_kernel.cu b/paddle/phi/kernels/gpu/sum_as_grad_kernel.cu index 5f3d6eea181db3..ea41eb36373ff5 100644 --- a/paddle/phi/kernels/gpu/sum_as_grad_kernel.cu +++ b/paddle/phi/kernels/gpu/sum_as_grad_kernel.cu @@ -25,10 +25,10 @@ namespace phi { template void SumAsGradKernel(const Context& dev_ctx, const DenseTensor& x, - const DenseTensor& y, + const DenseTensor& target, const DenseTensor& out_grad, DenseTensor* x_grad) { - auto reduce_dim = phi::funcs::GetReduceDims(x, y); + auto reduce_dim = phi::funcs::GetReduceDims(x, target); bool reduce_all = recompute_reduce_all(x, reduce_dim); auto update_dims = common::vectorize(x.dims()); for (auto i : reduce_dim) { diff --git a/paddle/phi/kernels/gpu/sum_as_kernel.cu b/paddle/phi/kernels/gpu/sum_as_kernel.cu index 4efc61a5cd139f..8cd20073653755 100644 --- a/paddle/phi/kernels/gpu/sum_as_kernel.cu +++ b/paddle/phi/kernels/gpu/sum_as_kernel.cu @@ -24,9 +24,9 @@ namespace phi { template void SumAsKernel(const Context& dev_ctx, const DenseTensor& x, - const DenseTensor& y, + const DenseTensor& target, DenseTensor* out) { - auto reduce_dim = phi::funcs::GetReduceDims(x, y); + auto reduce_dim = phi::funcs::GetReduceDims(x, target); dev_ctx.template Alloc(out); phi::SumKernel(dev_ctx, x, reduce_dim, out->type(), false, out); } diff --git a/paddle/phi/kernels/sum_as_grad_kernel.h b/paddle/phi/kernels/sum_as_grad_kernel.h index cc15e619c8ff2e..5ae7126371c240 100644 --- a/paddle/phi/kernels/sum_as_grad_kernel.h +++ b/paddle/phi/kernels/sum_as_grad_kernel.h @@ -24,7 +24,7 @@ namespace phi { template void SumAsGradKernel(const Context& dev_ctx, const DenseTensor& x, - const DenseTensor& y, + const DenseTensor& target, const DenseTensor& out_grad, DenseTensor* x_grad); diff --git a/paddle/phi/kernels/sum_as_kernel.h b/paddle/phi/kernels/sum_as_kernel.h index fcceccadeb4dea..cbc8813b95c465 100644 --- a/paddle/phi/kernels/sum_as_kernel.h +++ b/paddle/phi/kernels/sum_as_kernel.h @@ -24,7 +24,7 @@ namespace phi { template void SumAsKernel(const Context& dev_ctx, const DenseTensor& x, - const DenseTensor& y, + const DenseTensor& target, DenseTensor* out); } // namespace phi diff --git a/python/paddle/tensor/math.py b/python/paddle/tensor/math.py index 5ef0cdd35bd213..972baebfbe9e8a 100644 --- a/python/paddle/tensor/math.py +++ b/python/paddle/tensor/math.py @@ -1576,16 +1576,16 @@ def sum(x, axis=None, dtype=None, keepdim=False, name=None): return out -def sum_as(x, y, name=None): +def sum_as(x, target, name=None): """ - Computes the sum of tensor elements make the shape of its result equal to the shape of y. + Computes the sum of tensor elements make the shape of its result equal to the shape of target. Args: x (Tensor): An N-D Tensor, the data type is bool, float16, float32, float64, int32 or int64. - y (Tensor): An N-D Tensor, the length of x shape must greater than or equal to the length of y shape. The data type is bool, float16, float32, float64, int32 or int64. + target (Tensor): An N-D Tensor, the length of x shape must greater than or equal to the length of target shape. The data type is bool, float16, float32, float64, int32 or int64. Returns: - Tensor: The sum of the input tensor x along some axis has the same shape as the shape of the input tensor y, if `x.dtype='bool'`, `x.dtype='int32'`, it's data type is `'int64'`, otherwise it's data type is the same as `x`. + Tensor: The sum of the input tensor x along some axis has the same shape as the shape of the input tensor target, if `x.dtype='bool'`, `x.dtype='int32'`, it's data type is `'int64'`, otherwise it's data type is the same as `x`. Examples: .. code-block:: python @@ -1597,18 +1597,18 @@ def sum_as(x, y, name=None): Tensor(shape=[2, 4], dtype=int64, place=Place(gpu:0), stop_gradient=True, [[1, 2, 3, 4], [5, 6, 7, 8]]) - >>> y = paddle.to_tensor([1, 2, 3, 4]) - >>> y + >>> target = paddle.to_tensor([1, 2, 3, 4]) + >>> target Tensor(shape=[4], dtype=int64, place=Place(gpu:0), stop_gradient=True, [1, 2, 3, 4]) - >>> res = paddle.sum_as(x, y) + >>> res = paddle.sum_as(x, target) >>> res Tensor(shape=[4], dtype=int64, place=Place(gpu:0), stop_gradient=True, [6 , 8 , 10, 12]) """ if in_dynamic_or_pir_mode(): - return _C_ops.sum_as(x, y) + return _C_ops.sum_as(x, target) else: check_variable_and_dtype( x, @@ -1626,8 +1626,8 @@ def sum_as(x, y, name=None): 'sum_as', ) check_variable_and_dtype( - y, - 'y', + target, + 'target', [ 'bool', 'uint16', @@ -1645,7 +1645,7 @@ def sum_as(x, y, name=None): out = helper.create_variable_for_type_inference(dtype=x.dtype) helper.append_op( type='sum_as', - inputs={'x': x, 'y': y}, + inputs={'x': x, 'target': target}, outputs={'out': out}, ) return out diff --git a/test/legacy_test/test_sum_as_op.py b/test/legacy_test/test_sum_as_op.py index be0e23bef5abe9..361625bb88fc26 100644 --- a/test/legacy_test/test_sum_as_op.py +++ b/test/legacy_test/test_sum_as_op.py @@ -24,8 +24,8 @@ paddle.seed(100) -def sum_as_net(x, y): - return paddle.sum_as(x, y) +def sum_as_net(x, target): + return paddle.sum_as(x, target) def apply_to_static(net, use_cinn, input_spec=None): @@ -49,7 +49,7 @@ def setUp(self): self.python_api = paddle.sum_as self.op_type = "sum_as" - self.inputs = {'x': self.x, 'y': self.y} + self.inputs = {'x': self.x, 'target': self.y} self.outputs = {'out': self.out} self.if_enable_cinn() From 19a39ef08ddad08657247fb5836525773a5df492 Mon Sep 17 00:00:00 2001 From: zeroRains Date: Sun, 7 Apr 2024 09:17:32 +0000 Subject: [PATCH 21/30] remove spaces --- paddle/phi/kernels/gpu/sum_as_grad_kernel.cu | 1 - paddle/phi/kernels/gpu/sum_as_kernel.cu | 1 - 2 files changed, 2 deletions(-) diff --git a/paddle/phi/kernels/gpu/sum_as_grad_kernel.cu b/paddle/phi/kernels/gpu/sum_as_grad_kernel.cu index ea41eb36373ff5..d13b774d9e58ab 100644 --- a/paddle/phi/kernels/gpu/sum_as_grad_kernel.cu +++ b/paddle/phi/kernels/gpu/sum_as_grad_kernel.cu @@ -1,4 +1,3 @@ - // Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/paddle/phi/kernels/gpu/sum_as_kernel.cu b/paddle/phi/kernels/gpu/sum_as_kernel.cu index 8cd20073653755..cdab8602301e58 100644 --- a/paddle/phi/kernels/gpu/sum_as_kernel.cu +++ b/paddle/phi/kernels/gpu/sum_as_kernel.cu @@ -1,4 +1,3 @@ - // Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); From 7141a7054991ed14e1b6705483bb6a7d80ec3e6b Mon Sep 17 00:00:00 2001 From: zeroRains Date: Mon, 8 Apr 2024 15:21:54 +0000 Subject: [PATCH 22/30] add an assert for get_reduce_dims --- paddle/phi/kernels/funcs/common_shape.h | 26 +++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/paddle/phi/kernels/funcs/common_shape.h b/paddle/phi/kernels/funcs/common_shape.h index 73342fac9461b4..1b32a37a3b61ee 100644 --- a/paddle/phi/kernels/funcs/common_shape.h +++ b/paddle/phi/kernels/funcs/common_shape.h @@ -302,12 +302,30 @@ inline std::vector GetReduceDims(const DenseTensor &in, auto out_dims = out.dims(); int diff = in_dims.size() - out_dims.size(); - for (int i = 0; i < diff; ++i) { - reduce_dims.push_back(i); + + std::vector out_shape(diff, 1); + for (int i = 0; i < out_dims.size(); ++i) { + out_shape.push_back(out_dims[i]); } - for (int i = diff; i < in_dims.size(); ++i) { - if (out_dims[i - diff] != in_dims[i]) { + + for (int i = 0; i < in_dims.size(); ++i) { + if (out_shape[i] != in_dims[i] && out_shape[i] == 1) { reduce_dims.push_back(i); + } else { + PADDLE_ENFORCE_EQ( + in_dims[i], + out_shape[i], + phi::errors::InvalidArgument( + "ReduceDims dimension mismatch. Operands could " + "not be broadcast together with the shape of in_dims = [%s] and " + "the shape of out_shape = [%s]. Received [%d] in X is not equal " + "to " + "[%d] in Y at i:%d.", + in_dims, + out_dims, + in_dims[i], + out_shape[i], + i)); } } return reduce_dims; From 009cf48579f3d2f5631a009b225f8954024ebf34 Mon Sep 17 00:00:00 2001 From: zeroRains Date: Tue, 9 Apr 2024 03:52:57 +0000 Subject: [PATCH 23/30] fix the bug --- paddle/phi/kernels/funcs/common_shape.h | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/paddle/phi/kernels/funcs/common_shape.h b/paddle/phi/kernels/funcs/common_shape.h index 1b32a37a3b61ee..e3363e430b1e63 100644 --- a/paddle/phi/kernels/funcs/common_shape.h +++ b/paddle/phi/kernels/funcs/common_shape.h @@ -300,31 +300,27 @@ inline std::vector GetReduceDims(const DenseTensor &in, std::vector reduce_dims; auto in_dims = in.dims(); auto out_dims = out.dims(); - int diff = in_dims.size() - out_dims.size(); - - std::vector out_shape(diff, 1); - for (int i = 0; i < out_dims.size(); ++i) { - out_shape.push_back(out_dims[i]); + for (int i = 0; i < diff; ++i) { + reduce_dims.push_back(i); } - - for (int i = 0; i < in_dims.size(); ++i) { - if (out_shape[i] != in_dims[i] && out_shape[i] == 1) { - reduce_dims.push_back(i); + for (int i = 0; i < out_dims.size(); ++i) { + if (out_dims[i] == 1 && in_dims[i + diff] != 1) { + reduce_dims.push_back(i + diff); } else { PADDLE_ENFORCE_EQ( in_dims[i], - out_shape[i], + out_dims[i + diff], phi::errors::InvalidArgument( "ReduceDims dimension mismatch. Operands could " "not be broadcast together with the shape of in_dims = [%s] and " - "the shape of out_shape = [%s]. Received [%d] in X is not equal " + "the shape of out_dims = [%s]. Received [%d] in X is not equal " "to " "[%d] in Y at i:%d.", in_dims, out_dims, - in_dims[i], - out_shape[i], + in_dims[i + diff], + out_dims[i], i)); } } From 9fc3081bdad2621143ced8ca103acb759ed67f11 Mon Sep 17 00:00:00 2001 From: Zero Rains Date: Tue, 9 Apr 2024 12:45:13 +0800 Subject: [PATCH 24/30] Update common_shape.h --- paddle/phi/kernels/funcs/common_shape.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/paddle/phi/kernels/funcs/common_shape.h b/paddle/phi/kernels/funcs/common_shape.h index e3363e430b1e63..c998b7f484fa46 100644 --- a/paddle/phi/kernels/funcs/common_shape.h +++ b/paddle/phi/kernels/funcs/common_shape.h @@ -309,8 +309,8 @@ inline std::vector GetReduceDims(const DenseTensor &in, reduce_dims.push_back(i + diff); } else { PADDLE_ENFORCE_EQ( - in_dims[i], - out_dims[i + diff], + in_dims[i + diff], + out_dims[i], phi::errors::InvalidArgument( "ReduceDims dimension mismatch. Operands could " "not be broadcast together with the shape of in_dims = [%s] and " From 21157bc0f02e975c2f1675e96273cde16ed22fef Mon Sep 17 00:00:00 2001 From: zeroRains Date: Tue, 9 Apr 2024 11:18:41 +0000 Subject: [PATCH 25/30] modife sum_as to reduce_as --- paddle/phi/api/yaml/backward.yaml | 20 +++++++++---------- paddle/phi/api/yaml/ops.yaml | 20 +++++++++---------- paddle/phi/infermeta/binary.cc | 6 +++--- paddle/phi/infermeta/binary.h | 6 +++--- ...rad_kernel.cc => reduce_as_grad_kernel.cc} | 16 +++++++-------- .../{sum_as_kernel.cc => reduce_as_kernel.cc} | 14 ++++++------- ...rad_kernel.cu => reduce_as_grad_kernel.cu} | 16 +++++++-------- .../{sum_as_kernel.cu => reduce_as_kernel.cu} | 14 ++++++------- ...{sum_as_kernel.h => reduce_as_as_kernel.h} | 8 ++++---- ..._grad_kernel.h => reduce_as_grad_kernel.h} | 10 +++++----- python/paddle/__init__.py | 4 ++-- python/paddle/tensor/__init__.py | 4 ++-- python/paddle/tensor/math.py | 14 ++++++------- ...test_sum_as_op.py => test_reduce_as_op.py} | 14 ++++++------- 14 files changed, 83 insertions(+), 83 deletions(-) rename paddle/phi/kernels/cpu/{sum_as_grad_kernel.cc => reduce_as_grad_kernel.cc} (83%) rename paddle/phi/kernels/cpu/{sum_as_kernel.cc => reduce_as_kernel.cc} (83%) rename paddle/phi/kernels/gpu/{sum_as_grad_kernel.cu => reduce_as_grad_kernel.cu} (83%) rename paddle/phi/kernels/gpu/{sum_as_kernel.cu => reduce_as_kernel.cu} (82%) rename paddle/phi/kernels/{sum_as_kernel.h => reduce_as_as_kernel.h} (84%) rename paddle/phi/kernels/{sum_as_grad_kernel.h => reduce_as_grad_kernel.h} (78%) rename test/legacy_test/{test_sum_as_op.py => test_reduce_as_op.py} (94%) diff --git a/paddle/phi/api/yaml/backward.yaml b/paddle/phi/api/yaml/backward.yaml index ae368a7ec6cd0d..8634e208e15b9d 100644 --- a/paddle/phi/api/yaml/backward.yaml +++ b/paddle/phi/api/yaml/backward.yaml @@ -1876,6 +1876,16 @@ func : reciprocal_grad inplace : (out_grad -> x_grad) +- backward_op : reduce_as_grad + forward : reduce_as(Tensor x, Tensor target) -> Tensor(out) + args : (Tensor x, Tensor target, Tensor out_grad) + output : Tensor(x_grad) + infer_meta : + func : UnchangedInferMeta + param : [x] + kernel : + func : reduce_as_grad + - backward_op : relu6_grad forward : relu6 (Tensor x) -> Tensor(out) args : (Tensor out, Tensor out_grad) @@ -2407,16 +2417,6 @@ kernel : func : stanh_grad -- backward_op : sum_as_grad - forward : sum_as(Tensor x, Tensor target) -> Tensor(out) - args : (Tensor x, Tensor target, Tensor out_grad) - output : Tensor(x_grad) - infer_meta : - func : UnchangedInferMeta - param : [x] - kernel : - func : sum_as_grad - - backward_op : svd_grad forward : svd (Tensor x, bool full_matrices = false) -> Tensor(u), Tensor(s), Tensor(vh) args : (Tensor x, Tensor u, Tensor vh, Tensor s, Tensor u_grad, Tensor vh_grad, Tensor s_grad, bool full_matrices) diff --git a/paddle/phi/api/yaml/ops.yaml b/paddle/phi/api/yaml/ops.yaml index c9606cbeb4ab7d..6ed188b88318e5 100755 --- a/paddle/phi/api/yaml/ops.yaml +++ b/paddle/phi/api/yaml/ops.yaml @@ -2287,6 +2287,16 @@ inplace : (x -> out) backward : reciprocal_grad +- op : reduce_as + args : (Tensor x, Tensor target) + output : Tensor(out) + infer_meta : + func : ReduceAsInferMeta + kernel : + func : reduce_as + data_type : x + backward : reduce_as_grad + - op : reindex_graph args : (Tensor x, Tensor neighbors, Tensor count, Tensor hashtable_value, Tensor hashtable_index) output : Tensor(reindex_src), Tensor(reindex_dst), Tensor(out_nodes) @@ -2769,16 +2779,6 @@ func : stanh backward : stanh_grad -- op : sum_as - args : (Tensor x, Tensor target) - output : Tensor(out) - infer_meta : - func : SumAsInferMeta - kernel : - func : sum_as - data_type : x - backward : sum_as_grad - - op : svd args : (Tensor x, bool full_matrices = false) output : Tensor(u), Tensor(s), Tensor(vh) diff --git a/paddle/phi/infermeta/binary.cc b/paddle/phi/infermeta/binary.cc index c24468742b5582..fac05b3f608c2b 100644 --- a/paddle/phi/infermeta/binary.cc +++ b/paddle/phi/infermeta/binary.cc @@ -3047,9 +3047,9 @@ void SequenceMaskInferMeta(const MetaTensor& x, y->set_dtype(out_dtype); } -void SumAsInferMeta(const MetaTensor& x, - const MetaTensor& target, - MetaTensor* out) { +void ReduceAsInferMeta(const MetaTensor& x, + const MetaTensor& target, + MetaTensor* out) { DataType out_dtype; if (x.dtype() == DataType::BOOL || x.dtype() == DataType::INT32) { out_dtype = DataType::INT64; diff --git a/paddle/phi/infermeta/binary.h b/paddle/phi/infermeta/binary.h index cd0bce6c61ad80..e7c3c87de8098a 100644 --- a/paddle/phi/infermeta/binary.h +++ b/paddle/phi/infermeta/binary.h @@ -524,9 +524,9 @@ void ShuffleBatchInferMeta(const MetaTensor& x, ); -void SumAsInferMeta(const MetaTensor& x, - const MetaTensor& target, - MetaTensor* out); +void ReduceAsInferMeta(const MetaTensor& x, + const MetaTensor& target, + MetaTensor* out); void SoftmaxMaskFuseInferMeta(const MetaTensor& x, const MetaTensor& mask, diff --git a/paddle/phi/kernels/cpu/sum_as_grad_kernel.cc b/paddle/phi/kernels/cpu/reduce_as_grad_kernel.cc similarity index 83% rename from paddle/phi/kernels/cpu/sum_as_grad_kernel.cc rename to paddle/phi/kernels/cpu/reduce_as_grad_kernel.cc index 871921727e0a5b..8789a76cfd0777 100644 --- a/paddle/phi/kernels/cpu/sum_as_grad_kernel.cc +++ b/paddle/phi/kernels/cpu/reduce_as_grad_kernel.cc @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include "paddle/phi/kernels/sum_as_kernel.h" +#include "paddle/phi/kernels/reduce_as_kernel.h" #include "paddle/phi/core/device_context.h" #include "paddle/phi/core/kernel_registry.h" @@ -21,11 +21,11 @@ namespace phi { template -void SumAsGradKernel(const Context& dev_ctx, - const DenseTensor& x, - const DenseTensor& target, - const DenseTensor& out_grad, - DenseTensor* x_grad) { +void ReduceAsGradKernel(const Context& dev_ctx, + const DenseTensor& x, + const DenseTensor& target, + const DenseTensor& out_grad, + DenseTensor* x_grad) { auto reduce_dim = phi::funcs::GetReduceDims(x, target); bool reduce_all = recompute_reduce_all(x, reduce_dim); ReduceGradKernel(dev_ctx, @@ -40,10 +40,10 @@ void SumAsGradKernel(const Context& dev_ctx, } // namespace phi -PD_REGISTER_KERNEL(sum_as_grad, +PD_REGISTER_KERNEL(reduce_as_grad, CPU, ALL_LAYOUT, - phi::SumAsGradKernel, + phi::ReduceAsGradKernel, bool, float, double, diff --git a/paddle/phi/kernels/cpu/sum_as_kernel.cc b/paddle/phi/kernels/cpu/reduce_as_kernel.cc similarity index 83% rename from paddle/phi/kernels/cpu/sum_as_kernel.cc rename to paddle/phi/kernels/cpu/reduce_as_kernel.cc index 562e4d9a1f3947..25661bd829a203 100644 --- a/paddle/phi/kernels/cpu/sum_as_kernel.cc +++ b/paddle/phi/kernels/cpu/reduce_as_kernel.cc @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include "paddle/phi/kernels/sum_as_kernel.h" +#include "paddle/phi/kernels/reduce_as_kernel.h" #include "paddle/phi/core/device_context.h" #include "paddle/phi/core/kernel_registry.h" @@ -21,10 +21,10 @@ namespace phi { template -void SumAsKernel(const Context& dev_ctx, - const DenseTensor& x, - const DenseTensor& target, - DenseTensor* out) { +void ReduceAsKernel(const Context& dev_ctx, + const DenseTensor& x, + const DenseTensor& target, + DenseTensor* out) { auto reduce_dim = phi::funcs::GetReduceDims(x, target); bool reduce_all = recompute_reduce_all(x, reduce_dim); phi::Reduce( @@ -33,10 +33,10 @@ void SumAsKernel(const Context& dev_ctx, } // namespace phi -PD_REGISTER_KERNEL(sum_as, +PD_REGISTER_KERNEL(reduce_as, CPU, ALL_LAYOUT, - phi::SumAsKernel, + phi::ReduceAsKernel, bool, float, double, diff --git a/paddle/phi/kernels/gpu/sum_as_grad_kernel.cu b/paddle/phi/kernels/gpu/reduce_as_grad_kernel.cu similarity index 83% rename from paddle/phi/kernels/gpu/sum_as_grad_kernel.cu rename to paddle/phi/kernels/gpu/reduce_as_grad_kernel.cu index d13b774d9e58ab..cbd297326e14a6 100644 --- a/paddle/phi/kernels/gpu/sum_as_grad_kernel.cu +++ b/paddle/phi/kernels/gpu/reduce_as_grad_kernel.cu @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include "paddle/phi/kernels/sum_as_grad_kernel.h" +#include "paddle/phi/kernels/reduce_as_grad_kernel.h" #include "paddle/phi/backends/gpu/gpu_context.h" #include "paddle/phi/core/kernel_registry.h" @@ -22,11 +22,11 @@ namespace phi { template -void SumAsGradKernel(const Context& dev_ctx, - const DenseTensor& x, - const DenseTensor& target, - const DenseTensor& out_grad, - DenseTensor* x_grad) { +void ReduceAsGradKernel(const Context& dev_ctx, + const DenseTensor& x, + const DenseTensor& target, + const DenseTensor& out_grad, + DenseTensor* x_grad) { auto reduce_dim = phi::funcs::GetReduceDims(x, target); bool reduce_all = recompute_reduce_all(x, reduce_dim); auto update_dims = common::vectorize(x.dims()); @@ -50,10 +50,10 @@ void SumAsGradKernel(const Context& dev_ctx, } // namespace phi -PD_REGISTER_KERNEL(sum_as_grad, +PD_REGISTER_KERNEL(reduce_as_grad, GPU, ALL_LAYOUT, - phi::SumAsGradKernel, + phi::ReduceAsGradKernel, bool, float, double, diff --git a/paddle/phi/kernels/gpu/sum_as_kernel.cu b/paddle/phi/kernels/gpu/reduce_as_kernel.cu similarity index 82% rename from paddle/phi/kernels/gpu/sum_as_kernel.cu rename to paddle/phi/kernels/gpu/reduce_as_kernel.cu index cdab8602301e58..1555d2b59b7c40 100644 --- a/paddle/phi/kernels/gpu/sum_as_kernel.cu +++ b/paddle/phi/kernels/gpu/reduce_as_kernel.cu @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include "paddle/phi/kernels/sum_as_kernel.h" +#include "paddle/phi/kernels/reduce_as_kernel.h" #include "paddle/phi/backends/gpu/gpu_context.h" #include "paddle/phi/core/kernel_registry.h" @@ -21,10 +21,10 @@ namespace phi { template -void SumAsKernel(const Context& dev_ctx, - const DenseTensor& x, - const DenseTensor& target, - DenseTensor* out) { +void ReduceAsKernel(const Context& dev_ctx, + const DenseTensor& x, + const DenseTensor& target, + DenseTensor* out) { auto reduce_dim = phi::funcs::GetReduceDims(x, target); dev_ctx.template Alloc(out); phi::SumKernel(dev_ctx, x, reduce_dim, out->type(), false, out); @@ -32,10 +32,10 @@ void SumAsKernel(const Context& dev_ctx, } // namespace phi -PD_REGISTER_KERNEL(sum_as, +PD_REGISTER_KERNEL(reduce_as, GPU, ALL_LAYOUT, - phi::SumAsKernel, + phi::ReduceAsKernel, bool, float, double, diff --git a/paddle/phi/kernels/sum_as_kernel.h b/paddle/phi/kernels/reduce_as_as_kernel.h similarity index 84% rename from paddle/phi/kernels/sum_as_kernel.h rename to paddle/phi/kernels/reduce_as_as_kernel.h index cbc8813b95c465..ad62ddb6e0674c 100644 --- a/paddle/phi/kernels/sum_as_kernel.h +++ b/paddle/phi/kernels/reduce_as_as_kernel.h @@ -22,9 +22,9 @@ namespace phi { template -void SumAsKernel(const Context& dev_ctx, - const DenseTensor& x, - const DenseTensor& target, - DenseTensor* out); +void ReduceAsKernel(const Context& dev_ctx, + const DenseTensor& x, + const DenseTensor& target, + DenseTensor* out); } // namespace phi diff --git a/paddle/phi/kernels/sum_as_grad_kernel.h b/paddle/phi/kernels/reduce_as_grad_kernel.h similarity index 78% rename from paddle/phi/kernels/sum_as_grad_kernel.h rename to paddle/phi/kernels/reduce_as_grad_kernel.h index 5ae7126371c240..577af8ffb7eb97 100644 --- a/paddle/phi/kernels/sum_as_grad_kernel.h +++ b/paddle/phi/kernels/reduce_as_grad_kernel.h @@ -22,10 +22,10 @@ namespace phi { template -void SumAsGradKernel(const Context& dev_ctx, - const DenseTensor& x, - const DenseTensor& target, - const DenseTensor& out_grad, - DenseTensor* x_grad); +void ReduceAsGradKernel(const Context& dev_ctx, + const DenseTensor& x, + const DenseTensor& target, + const DenseTensor& out_grad, + DenseTensor* x_grad); } // namespace phi diff --git a/python/paddle/__init__.py b/python/paddle/__init__.py index 7adcff2ac86eca..9b7cd40224babc 100644 --- a/python/paddle/__init__.py +++ b/python/paddle/__init__.py @@ -474,6 +474,7 @@ prod, rad2deg, reciprocal, + reduce_as, remainder, remainder_, renorm, @@ -494,7 +495,6 @@ stanh, subtract, sum, - sum_as, take, tan, tan_, @@ -847,7 +847,7 @@ 'ones', 'not_equal', 'sum', - 'sum_as', + 'reduce_as', 'nansum', 'nanmean', 'count_nonzero', diff --git a/python/paddle/tensor/__init__.py b/python/paddle/tensor/__init__.py index 7daa8fa530d4e9..936edb9c428fb9 100644 --- a/python/paddle/tensor/__init__.py +++ b/python/paddle/tensor/__init__.py @@ -357,6 +357,7 @@ rad2deg, reciprocal, reciprocal_, + reduce_as, remainder, remainder_, renorm, @@ -383,7 +384,6 @@ subtract, subtract_, sum, - sum_as, take, tan, tan_, @@ -526,7 +526,7 @@ 'square', 'stanh', 'sum', - 'sum_as', + 'reduce_as', 'multigammaln', 'multigammaln_', 'nan_to_num', diff --git a/python/paddle/tensor/math.py b/python/paddle/tensor/math.py index b38b6659b288df..a3f2c087f63ea1 100644 --- a/python/paddle/tensor/math.py +++ b/python/paddle/tensor/math.py @@ -1576,7 +1576,7 @@ def sum(x, axis=None, dtype=None, keepdim=False, name=None): return out -def sum_as(x, target, name=None): +def reduce_as(x, target, name=None): """ Computes the sum of tensor elements make the shape of its result equal to the shape of target. @@ -1601,14 +1601,14 @@ def sum_as(x, target, name=None): >>> target Tensor(shape=[4], dtype=int64, place=Place(gpu:0), stop_gradient=True, [1, 2, 3, 4]) - >>> res = paddle.sum_as(x, target) + >>> res = paddle.reduce_as(x, target) >>> res Tensor(shape=[4], dtype=int64, place=Place(gpu:0), stop_gradient=True, [6 , 8 , 10, 12]) """ if in_dynamic_or_pir_mode(): - return _C_ops.sum_as(x, target) + return _C_ops.reduce_as(x, target) else: check_variable_and_dtype( x, @@ -1623,7 +1623,7 @@ def sum_as(x, target, name=None): 'int32', 'int64', ], - 'sum_as', + 'reduce_as', ) check_variable_and_dtype( target, @@ -1638,13 +1638,13 @@ def sum_as(x, target, name=None): 'int32', 'int64', ], - 'sum_as', + 'reduce_as', ) - helper = LayerHelper('sum_as', **locals()) + helper = LayerHelper('reduce_as', **locals()) out = helper.create_variable_for_type_inference(dtype=x.dtype) helper.append_op( - type='sum_as', + type='reduce_as', inputs={'x': x, 'target': target}, outputs={'out': out}, ) diff --git a/test/legacy_test/test_sum_as_op.py b/test/legacy_test/test_reduce_as_op.py similarity index 94% rename from test/legacy_test/test_sum_as_op.py rename to test/legacy_test/test_reduce_as_op.py index 361625bb88fc26..53f35eba9a08b3 100644 --- a/test/legacy_test/test_sum_as_op.py +++ b/test/legacy_test/test_reduce_as_op.py @@ -24,8 +24,8 @@ paddle.seed(100) -def sum_as_net(x, target): - return paddle.sum_as(x, target) +def reduce_as_net(x, target): + return paddle.reduce_as(x, target) def apply_to_static(net, use_cinn, input_spec=None): @@ -47,8 +47,8 @@ def setUp(self): self.init_attrs() self.calc_output() - self.python_api = paddle.sum_as - self.op_type = "sum_as" + self.python_api = paddle.reduce_as + self.op_type = "reduce_as" self.inputs = {'x': self.x, 'target': self.y} self.outputs = {'out': self.out} self.if_enable_cinn() @@ -131,15 +131,15 @@ def init_attrs(self): class TestSumAsDynamicShape(unittest.TestCase): def setUp(self): np.random.seed(2023) - self.shape_x = [300, 200, 100] - self.shape_y = [200, 100] + self.shape_x = [300, 20, 100] + self.shape_y = [20, 100] self.dtype_x = "float32" self.dtype_y = "float32" self.init_x_shape = [None, None, 100] self.init_y_shape = [None, 100] self.x = np.random.random(self.shape_x).astype(self.dtype_x) self.y = np.random.random(self.shape_y).astype(self.dtype_y) - self.net = sum_as_net + self.net = reduce_as_net self.enable_cinn = False self.tol = 1e-6 From c96d295ae276ec34dfe6f4520bf01e6908f04856 Mon Sep 17 00:00:00 2001 From: zeroRains Date: Tue, 9 Apr 2024 11:34:03 +0000 Subject: [PATCH 26/30] fix the file name --- paddle/phi/kernels/{reduce_as_as_kernel.h => reduce_as_kernel.h} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename paddle/phi/kernels/{reduce_as_as_kernel.h => reduce_as_kernel.h} (100%) diff --git a/paddle/phi/kernels/reduce_as_as_kernel.h b/paddle/phi/kernels/reduce_as_kernel.h similarity index 100% rename from paddle/phi/kernels/reduce_as_as_kernel.h rename to paddle/phi/kernels/reduce_as_kernel.h From 7f2168ee5fd10dbf71bd6124e2b93f2b281254e9 Mon Sep 17 00:00:00 2001 From: zeroRains Date: Wed, 17 Apr 2024 07:13:47 +0000 Subject: [PATCH 27/30] modify the test time --- test/legacy_test/CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/test/legacy_test/CMakeLists.txt b/test/legacy_test/CMakeLists.txt index 1e6a577901b48f..4d026ac680097a 100644 --- a/test/legacy_test/CMakeLists.txt +++ b/test/legacy_test/CMakeLists.txt @@ -1442,3 +1442,4 @@ set_pit_tests_properties() set_tests_properties(test_fractional_max_pool2d_op PROPERTIES TIMEOUT 120) set_tests_properties(test_fractional_max_pool3d_op PROPERTIES TIMEOUT 120) +set_tests_properties(test_reduce_as_op PROPERTIES TIMEOUT 30) From 1ce4a5b5918501c1512347c1fe25a0de98a1b442 Mon Sep 17 00:00:00 2001 From: cyber-pioneer Date: Thu, 18 Apr 2024 05:00:58 +0000 Subject: [PATCH 28/30] fix test case --- test/legacy_test/test_assign_pos_op.py | 8 -- .../legacy_test/test_assign_pos_op_dygraph.py | 86 +++++++++++++++++++ 2 files changed, 86 insertions(+), 8 deletions(-) create mode 100644 test/legacy_test/test_assign_pos_op_dygraph.py diff --git a/test/legacy_test/test_assign_pos_op.py b/test/legacy_test/test_assign_pos_op.py index ff50a2310c8c5e..3d5e117258d034 100644 --- a/test/legacy_test/test_assign_pos_op.py +++ b/test/legacy_test/test_assign_pos_op.py @@ -118,14 +118,6 @@ def test_api_static(self): ) assert_allclose(res[0], self.out, self.cum_count) - def test_api_dygraph(self): - paddle.disable_static() - x = paddle.to_tensor(self.x) - cum_count = paddle.to_tensor(self.cum_count).astype(x.dtype) - - out = utils._assign_pos(x, cum_count) - assert_allclose(out.numpy(), self.out, self.cum_count) - if __name__ == '__main__': paddle.enable_static() diff --git a/test/legacy_test/test_assign_pos_op_dygraph.py b/test/legacy_test/test_assign_pos_op_dygraph.py new file mode 100644 index 00000000000000..2f6219f1356039 --- /dev/null +++ b/test/legacy_test/test_assign_pos_op_dygraph.py @@ -0,0 +1,86 @@ +# 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. + +import unittest + +import numpy as np + +import paddle +from paddle.base import core +from paddle.distributed.models.moe import utils + + +def assign_pos(x, _cum_count): + cum_count = np.copy(_cum_count) + x = x.reshape(-1) + res = np.zeros((cum_count[-1],), dtype=np.int64) + for i, idx in enumerate(x): + p = cum_count[idx] + cum_count[idx] -= 1 + if p >= 1: + res[p - 1] = i + return res + + +def count(x, upper_num): + res = np.zeros((upper_num,)).astype(int) + for i in x.reshape(-1): + if i >= 0 and i < len(res): + res[i] += 1 + return res + + +# why defining the assert function specially? +# Becasue assign_pos_op is multithread-op, which can make the order of numbers +# in each counter(bin) is random. But the numbers set is certain in each counter(bin). +np_allclose = np.allclose + + +def assert_allclose(res, out, cum_count): + c0 = 0 + for c in cum_count: + if c == c0: + continue + data1 = np.copy(res[c0:c]) + data2 = np.copy(out[c0:c]) + data1.sort() + data2.sort() + assert np_allclose(data2, data1) + c0 = c + return True + + +@unittest.skipIf( + not core.is_compiled_with_cuda(), "core is not compiled with CUDA" +) +class TestAssignPosAPI(unittest.TestCase): + def setUp(self): + self.x = np.random.randint(0, 16, size=(100, 2)).astype("int64") + y = count(self.x, 16) + self.cum_count = np.cumsum(y).astype(self.x.dtype) + self.out = assign_pos(self.x, self.cum_count) + self.place = paddle.CUDAPlace(0) + + def test_api_dygraph(self): + paddle.disable_static() + x = paddle.to_tensor(self.x) + cum_count = paddle.to_tensor(self.cum_count).astype(x.dtype) + + out = utils._assign_pos(x, cum_count) + assert_allclose(out.numpy(), self.out, self.cum_count) + + +if __name__ == '__main__': + paddle.enable_static() + unittest.main() From d8ac7197c9c73acd2b1fbcde33ab679c72bc60c2 Mon Sep 17 00:00:00 2001 From: zeroRains Date: Thu, 18 Apr 2024 10:54:21 +0000 Subject: [PATCH 29/30] fix the date --- test/legacy_test/test_assign_pos_op_dygraph.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/legacy_test/test_assign_pos_op_dygraph.py b/test/legacy_test/test_assign_pos_op_dygraph.py index 2f6219f1356039..9f5476aeb496aa 100644 --- a/test/legacy_test/test_assign_pos_op_dygraph.py +++ b/test/legacy_test/test_assign_pos_op_dygraph.py @@ -1,4 +1,4 @@ -# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. +# Copyright (c) 2024 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. From 0dfe7fa87aa0d4709fece0cec08c1d633da9770d Mon Sep 17 00:00:00 2001 From: zeroRains Date: Thu, 18 Apr 2024 14:25:46 +0000 Subject: [PATCH 30/30] fix code style --- test/legacy_test/CMakeLists.txt | 1 - 1 file changed, 1 deletion(-) diff --git a/test/legacy_test/CMakeLists.txt b/test/legacy_test/CMakeLists.txt index 860b38b4d20437..5c69fd1258eff5 100644 --- a/test/legacy_test/CMakeLists.txt +++ b/test/legacy_test/CMakeLists.txt @@ -1056,4 +1056,3 @@ endif() set_pit_tests_properties() set_tests_properties(test_reduce_as_op PROPERTIES TIMEOUT 30) -