Skip to content

Commit 6464575

Browse files
committed
[0-size Tensor No.92、94-97] Add 0-size Tensor support for isfinite/isinf/isnan (PaddlePaddle#72807)
* Fix * Fix * Fix
1 parent 75e5195 commit 6464575

File tree

4 files changed

+64
-0
lines changed

4 files changed

+64
-0
lines changed

paddle/phi/kernels/impl/isfinite_kernel_impl.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,18 +481,30 @@ template <typename T, typename Context>
481481
void IsfiniteKernel(const Context& dev_ctx,
482482
const DenseTensor& x,
483483
DenseTensor* out) {
484+
if (out && out->numel() == 0) {
485+
dev_ctx.template Alloc<bool>(out);
486+
return;
487+
}
484488
IsfiniteFunctor<Context, T>()(dev_ctx, x, out);
485489
}
486490
template <typename T, typename Context>
487491
void IsinfKernel(const Context& dev_ctx,
488492
const DenseTensor& x,
489493
DenseTensor* out) {
494+
if (out && out->numel() == 0) {
495+
dev_ctx.template Alloc<bool>(out);
496+
return;
497+
}
490498
IsinfFunctor<Context, T>()(dev_ctx, x, out);
491499
}
492500
template <typename T, typename Context>
493501
void IsnanKernel(const Context& dev_ctx,
494502
const DenseTensor& x,
495503
DenseTensor* out) {
504+
if (out && out->numel() == 0) {
505+
dev_ctx.template Alloc<bool>(out);
506+
return;
507+
}
496508
IsnanFunctor<Context, T>()(dev_ctx, x, out);
497509
}
498510
} // namespace phi

paddle/phi/kernels/selected_rows/impl/isfinite_kernel_impl.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ namespace phi {
2323
template <typename T, typename Context> \
2424
void isfinite##SR( \
2525
const Context& ctx, const SelectedRows& x, SelectedRows* out) { \
26+
if (out && out->numel() == 0) { \
27+
ctx.template Alloc<bool>(out); \
28+
return; \
29+
} \
2630
ctx.template Alloc<bool>(out); \
2731
Isinf##Kernel<T, Context>(ctx, x.value(), out->mutable_value()); \
2832
}

paddle/phi/kernels/xpu/isfinite_kernel.cc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ namespace phi {
2323
template <typename T, typename Context>
2424
void IsnanKernel(const Context& ctx, const DenseTensor& x, DenseTensor* out) {
2525
using XPUType = typename XPUTypeTrait<T>::Type;
26+
if (out && out->numel() == 0) {
27+
ctx.template Alloc<bool>(out);
28+
return;
29+
}
2630
auto* out_data = ctx.template Alloc<bool>(out);
2731
int r = xpu::isnan<XPUType>(ctx.x_context(),
2832
reinterpret_cast<const XPUType*>(x.data<T>()),
@@ -36,6 +40,10 @@ void IsfiniteKernel(const Context& ctx,
3640
const DenseTensor& x,
3741
DenseTensor* out) {
3842
using XPUType = typename XPUTypeTrait<T>::Type;
43+
if (out && out->numel() == 0) {
44+
ctx.template Alloc<bool>(out);
45+
return;
46+
}
3947
auto* out_data = ctx.template Alloc<bool>(out);
4048
int r = xpu::isfinite<XPUType>(ctx.x_context(),
4149
reinterpret_cast<const XPUType*>(x.data<T>()),
@@ -47,6 +55,10 @@ void IsfiniteKernel(const Context& ctx,
4755
template <typename T, typename Context>
4856
void IsinfKernel(const Context& ctx, const DenseTensor& x, DenseTensor* out) {
4957
using XPUType = typename XPUTypeTrait<T>::Type;
58+
if (out && out->numel() == 0) {
59+
ctx.template Alloc<bool>(out);
60+
return;
61+
}
5062
auto* out_data = ctx.template Alloc<bool>(out);
5163
int r = xpu::isinf<XPUType>(ctx.x_context(),
5264
reinterpret_cast<const XPUType*>(x.data<T>()),

test/legacy_test/test_isfinite_v2_op.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,42 @@ def test_isneginf_bad_x():
352352
self.assertRaises(TypeError, test_isneginf_bad_x)
353353

354354

355+
def create_test_class(op_type, dtype, shape):
356+
class Cls(unittest.TestCase):
357+
def test_zero_size(self):
358+
paddle.disable_static()
359+
numpy_tensor_1 = np.random.rand(*shape).astype(dtype)
360+
paddle_x = paddle.to_tensor(numpy_tensor_1)
361+
paddle_x.stop_gradient = False
362+
363+
paddle_api = eval(f"paddle.{op_type}")
364+
paddle_out = paddle_api(paddle_x)
365+
numpy_api = eval(f"np.{op_type}")
366+
numpy_out = numpy_api(numpy_tensor_1)
367+
368+
np.testing.assert_allclose(
369+
paddle_out.numpy(),
370+
numpy_out,
371+
1e-2,
372+
1e-2,
373+
)
374+
np.testing.assert_allclose(
375+
paddle_out.shape,
376+
numpy_out.shape,
377+
)
378+
379+
cls_name = f"{op_type}{dtype}_0SizeTest"
380+
Cls.__name__ = cls_name
381+
globals()[cls_name] = Cls
382+
383+
384+
op_list = ["isfinite", "isinf", "isnan"]
385+
for op in op_list:
386+
create_test_class(op, "float32", [3, 4, 0])
387+
create_test_class(op, "float64", [3, 4, 0, 3, 4])
388+
create_test_class(op, "int32", [3, 4, 0])
389+
create_test_class(op, "int64", [3, 4, 0, 3, 4])
390+
355391
if __name__ == '__main__':
356392
paddle.enable_static()
357393
unittest.main()

0 commit comments

Comments
 (0)