Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

【HEU】[Paddle Tensor 第二期 API支持 0-size Tensor] paddle.any 支持 0-size tensor #70489

Merged
merged 1 commit into from
Dec 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions paddle/phi/kernels/reduce_any_kernel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
#include "paddle/phi/kernels/reduce_any_kernel.h"

#include "paddle/phi/backends/all_context.h"
#include "paddle/phi/common/int_array.h"
#include "paddle/phi/core/kernel_registry.h"
#include "paddle/phi/kernels/full_kernel.h"

namespace phi {

Expand All @@ -25,6 +27,14 @@ void AnyKernel(const Context& dev_ctx,
const std::vector<int64_t>& dims,
bool keep_dim,
DenseTensor* out) {
if (x.numel() == 0) {
dev_ctx.template Alloc<bool>(out);
if (out->numel() > 0) {
std::vector<int64_t> vec_dims = common::vectorize(out->dims());
phi::Full<bool, Context>(dev_ctx, phi::IntArray(vec_dims), 0, out);
}
return;
}
bool reduce_all = recompute_reduce_all(x, dims);
AnyRawKernel<T>(dev_ctx, x, dims, keep_dim, reduce_all, out);
}
Expand Down
89 changes: 89 additions & 0 deletions test/legacy_test/test_reduce_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -2336,6 +2336,95 @@ def test_zero_size(self):
self._test_all(place, axis, keepdim, dtype)


class TestAnyZero(unittest.TestCase):
def setUp(self):
np.random.seed(123)
self.shape = [1, 0, 2]
self.dtypes = [
"bool",
"float32",
"float64",
"int32",
"complex64",
"complex128",
]
self.places = [base.CPUPlace()]
if core.is_compiled_with_cuda():
self.places.append(base.CUDAPlace(0))

def calculate_expected_result(self, x_np, axis, keepdim):
expected_result = np.any(x_np, axis=axis, keepdims=keepdim)
return expected_result

def check_result(
self, static_result, expected_result, axis, keepdim, dtype, place
):
self.assertTrue(
(static_result == expected_result).all(),
f"Static Mode - Shape: {self.shape}, Axis: {axis}, Keepdim: {keepdim}, Dtype: {dtype}, Place: {place}",
)

def _test_static(self, place, axis, keepdim, dtype):
with static_guard():
with base.program_guard(
paddle.static.Program(), paddle.static.Program()
):
input = paddle.static.data(
name="x", shape=self.shape, dtype=dtype
)
result = paddle.any(x=input, axis=axis, keepdim=keepdim)
x_np = np.zeros(self.shape, dtype=dtype)

exe = base.Executor(place)
fetches = exe.run(
feed={"x": x_np},
fetch_list=[result],
)
expected_result = self.calculate_expected_result(
x_np, axis, keepdim
)
self.check_result(
fetches[0], expected_result, axis, keepdim, dtype, place
)

def _test_dygraph(self, place, axis, keepdim, dtype):
with dygraph_guard():
x_np = np.zeros(self.shape, dtype=dtype)
x = paddle.to_tensor(x_np)
dygraph_result = paddle.any(x, axis=axis, keepdim=keepdim).numpy()
expected_result = self.calculate_expected_result(
x_np, axis, keepdim
)
self.check_result(
dygraph_result, expected_result, axis, keepdim, dtype, place
)

def _test_any(self, place, axis, keepdim, dtype):
self._test_dygraph(place, axis, keepdim, dtype)
self._test_static(place, axis, keepdim, dtype)

def test_zero_size(self):
axes_options = [
None,
0,
1,
2,
-1,
-2,
(),
(0, 1),
(0, 2),
(1, 2),
(-1, -2),
]
keepdims_options = [True, False]
for place in self.places:
for dtype in self.dtypes:
for axis in axes_options:
for keepdim in keepdims_options:
self._test_any(place, axis, keepdim, dtype)


if __name__ == '__main__':
paddle.enable_static()
unittest.main()
Loading