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 2 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/impl/nextafter_kernel_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ void NextafterKernel(const Context& ctx,
const DenseTensor& x,
const DenseTensor& y,
DenseTensor* out) {
if (x.numel() == 0 || y.numel() == 0) {
ctx.template Alloc<typename NextafterOut<T>::type>(out);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

用这个分配即可ctx.template Alloc<T>(out);; typename NextafterOut<T>::type影响可阅读性

return;
}

auto* out_data = ctx.template Alloc<T>(out);
auto x_data = x.data<T>();
auto y_data = y.data<T>();
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 @@ -114,5 +114,31 @@ def init_dtype(self):
self.dtype = np.float32


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