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

[Divide by 0 Error] add pinv check #49951

Merged
merged 7 commits into from
Jan 30, 2023
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
8 changes: 8 additions & 0 deletions paddle/phi/kernels/cpu/svd_kernel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,14 @@ void SvdKernel(const Context& dev_ctx,
// int k = std::min(rows, cols);
// int col_u = full ? rows : k;
// int col_v = full ? cols : k;
PADDLE_ENFORCE_LT(
0,
rows,
errors::InvalidArgument("The row of Input(X) should be greater than 0."));
PADDLE_ENFORCE_LT(
0,
cols,
errors::InvalidArgument("The col of Input(X) should be greater than 0."));
int batches = numel / (rows * cols);
auto* U_out = dev_ctx.template Alloc<phi::dtype::Real<T>>(U);
auto* VH_out = dev_ctx.template Alloc<phi::dtype::Real<T>>(VH);
Expand Down
9 changes: 9 additions & 0 deletions paddle/phi/kernels/gpu/svd_kernel.cu
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,15 @@ void SvdKernel(const Context& dev_ctx,
int m = dims[rank - 2];
int n = dims[rank - 1];

PADDLE_ENFORCE_LT(
0,
m,
errors::InvalidArgument("The row of Input(X) should be greater than 0."));
PADDLE_ENFORCE_LT(
0,
n,
errors::InvalidArgument("The col of Input(X) should be greater than 0."));

auto* u_data = dev_ctx.template Alloc<phi::dtype::Real<T>>(U);
auto* vh_data = dev_ctx.template Alloc<phi::dtype::Real<T>>(VH);
auto* s_data = dev_ctx.template Alloc<phi::dtype::Real<T>>(S);
Expand Down
22 changes: 22 additions & 0 deletions python/paddle/fluid/tests/unittests/test_linalg_pinv_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,5 +280,27 @@ def init_config(self):
self.hermitian = True


class TestDivByZero(unittest.TestCase):
def pinv_zero_input_static(self):

paddle.enable_static()
array = np.array([], dtype=np.float32)
x = paddle.to_tensor(np.reshape(array, [0, 0]), dtype='float32')
paddle.linalg.pinv(x)

def pinv_zero_input_dynamic(self):

paddle.disable_static()
array = np.array([], dtype=np.float32)
x = paddle.to_tensor(np.reshape(array, [0, 0]), dtype='float32')
paddle.linalg.pinv(x)

def test_div_by_zero(self):

with self.assertRaises(ValueError):
self.pinv_zero_input_dynamic()
self.pinv_zero_input_static()


if __name__ == '__main__':
unittest.main()