Skip to content

[0-size Tensor No.144] Add 0-size Tensor support for paddle.nextafter API. #73008

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

Merged
merged 8 commits into from
Jun 10, 2025
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
5 changes: 5 additions & 0 deletions paddle/phi/kernels/cpu/elementwise_kernel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ void NextafterKernel(const Context& dev_ctx,
const DenseTensor& x,
const DenseTensor& y,
DenseTensor* out) {
if (x.numel() == 0 || y.numel() == 0) {
dev_ctx.template Alloc<T>(out);
return;
}
dev_ctx.template Alloc<T>(out);
auto x_dims = x.dims();
auto y_dims = y.dims();
Expand Down Expand Up @@ -210,5 +214,6 @@ PD_REGISTER_KERNEL(copysign,
double,
phi::dtype::float16,
phi::dtype::bfloat16) {}

PD_REGISTER_KERNEL(
nextafter, CPU, ALL_LAYOUT, phi::NextafterKernel, float, double) {}
4 changes: 4 additions & 0 deletions paddle/phi/kernels/kps/elementwise_kernel.cu
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,10 @@ void NextafterKernel(const Context& dev_ctx,
const DenseTensor& x,
const DenseTensor& y,
DenseTensor* out) {
if (x.numel() == 0 || y.numel() == 0) {
dev_ctx.template Alloc<T>(out);
return;
}
std::vector<const DenseTensor*> inputs = {&x, &y};
std::vector<DenseTensor*> outputs = {out};
dev_ctx.template Alloc<T>(out);
Expand Down
26 changes: 26 additions & 0 deletions test/legacy_test/test_nextafter_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,5 +149,31 @@ def init_shape(self):
self.y_shape = (1,)


class TestNextafterOPZeroDim1(TestNextafterOP):
def setUp(self):
self.op_type = "nextafter"
self.python_api = paddle.nextafter
self.init_dtype()

x = np.random.rand(0, 3, 2).astype(self.dtype)
y = np.random.rand(0, 3, 2).astype(self.dtype)
out = np.nextafter(x, y)
self.inputs = {'x': x, 'y': y}
self.outputs = {'out': out}


class TestNextafterOPZeroDim2(TestNextafterOP):
def setUp(self):
self.op_type = "nextafter"
self.python_api = paddle.nextafter
self.init_dtype()

x = np.random.rand(4, 0, 2).astype(self.dtype)
y = np.random.rand(4, 0, 2).astype(self.dtype)
out = np.nextafter(x, y)
self.inputs = {'x': x, 'y': y}
self.outputs = {'out': out}


if __name__ == "__main__":
unittest.main()
Loading