-
Notifications
You must be signed in to change notification settings - Fork 24.8k
pinv: forward/backward AD which is Frechet-defined in a rank-preserving neighborhood. #66092
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
Changes from 4 commits
d4ddef8
1ad418e
4775c45
346113f
23c6ccf
b154a16
3743c7d
2dbbecb
443ccc1
378abc5
0ec1de8
2b5431e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,7 +35,7 @@ | |
random_fullrank_matrix_distinct_singular_value, | ||
TEST_WITH_ROCM, IS_WINDOWS, IS_MACOS, TEST_SCIPY, | ||
torch_to_numpy_dtype_dict, TEST_WITH_ASAN, | ||
GRADCHECK_NONDET_TOL, skipIfTBB) | ||
GRADCHECK_NONDET_TOL, skipIfTBB, slowTest) | ||
import torch.testing._internal.opinfo_helper as opinfo_helper | ||
|
||
from setuptools import distutils | ||
|
@@ -2102,6 +2102,29 @@ def sample_inputs_linalg_invertible(op_info, device, dtype, requires_grad=False, | |
out.append(SampleInput(a)) | ||
return out | ||
|
||
def sample_inputs_linalg_pinv_singular(op_info, device, dtype, requires_grad=False, **kwargs): | ||
""" | ||
This function produces factors `a` and `b` to generate inputs of the form `a @ b.t()` to | ||
test the backward method of `linalg_pinv`. That way we always preserve the rank of the | ||
input no matter the perturbations applied to it by the gradcheck. | ||
Note that `pinv` is Frechet-differentiable in a rank-preserving neighborhood. | ||
""" | ||
batches = [(), (0, ), (2, ), (1, 1)] | ||
# the size of at least 30 is required to cause failures for the previous implicit implementation | ||
# of the pinv's backward method, albeit it is slow. | ||
size = [0, 3, 30, 50, 80] | ||
|
||
def generate_samples(): | ||
for batch, m, n in product(batches, size, size): | ||
for k in range(min(3, min(m, n))): | ||
# Note that by making the columns of `a` and `b` orthonormal we make sure | ||
# that the product matrix `a @ b.t()` has condition number 1. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice! This saves us a lot of pain in future debugging. Now, this note is slightly incorrect. The resulting matrix will have singular values 0 and 1, so the condition number will be infinite! Perhaps you mean that it has condition number 1 when restricted to its image? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, exactly in the image, correct, so that pinv is stable. |
||
a = torch.rand(*batch, m, k, device=device, dtype=dtype).qr().Q.requires_grad_(requires_grad) | ||
b = torch.rand(*batch, n, k, device=device, dtype=dtype).qr().Q.requires_grad_(requires_grad) | ||
yield SampleInput(a, args=(b,)) | ||
|
||
return list(generate_samples()) | ||
|
||
def sample_inputs_linalg_cond(op_info, device, dtype, requires_grad=False, **kwargs): | ||
make_arg = partial(make_tensor, dtype=dtype, device=device, requires_grad=requires_grad) | ||
|
||
|
@@ -8908,14 +8931,36 @@ def generate_std_var_kwargs(t: torch.Tensor, **kwargs): | |
dtypes=floating_and_complex_types(), | ||
check_batched_grad=False, | ||
check_batched_gradgrad=False, | ||
supports_forward_ad=True, | ||
sample_inputs_func=sample_inputs_linalg_invertible, | ||
decorators=[skipCUDAIfNoMagmaAndNoCusolver, skipCUDAIfRocm, skipCPUIfNoLapack]), | ||
OpInfo('linalg.pinv', | ||
aten_name='linalg_pinv', | ||
variant_test_name='singular', | ||
# pinv is Frechet-differentiable in a rank-preserving neighborhood, | ||
# so we feed inputs that are the products of two full-rank factors, | ||
# to avoid any rank changes caused by the perturbations in the gradcheck | ||
op=lambda a, b: torch.linalg.pinv(a @ b.transpose(-1, -2)), | ||
dtypes=floating_and_complex_types(), | ||
supports_out=False, | ||
check_batched_grad=False, | ||
check_batched_gradgrad=False, | ||
supports_forward_ad=True, | ||
sample_inputs_func=sample_inputs_linalg_pinv_singular, | ||
# Only large tensors show issues with implicit backward used prior to | ||
# explicit backward implementation. | ||
Comment on lines
+9059
to
+9060
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. a note: large tensors of low rank. In my environment I had to create a 1-rank 30x30 matrix to see issues with repeated "zeros" in the backward of SVD. |
||
decorators=[slowTest, skipCUDAIfNoMagmaAndNoCusolver, skipCUDAIfRocm, skipCPUIfNoLapack], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is the slowTest decorator working as expected here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @albanD It will apply the slowTest decorator to EVERY test generated by this OpInfo |
||
skips=( | ||
# test does not work with passing lambda for op | ||
DecorateInfo(unittest.skip("Skipped!"), 'TestJit', 'test_variant_consistency_jit'), | ||
)), | ||
OpInfo('linalg.pinv', | ||
aten_name='linalg_pinv', | ||
variant_test_name='hermitian', | ||
dtypes=floating_and_complex_types(), | ||
check_batched_grad=False, | ||
check_batched_gradgrad=False, | ||
supports_forward_ad=True, | ||
sample_inputs_func=sample_inputs_linalg_pinv_hermitian, | ||
gradcheck_wrapper=gradcheck_wrapper_hermitian_input, | ||
decorators=[skipCUDAIfNoMagma, skipCUDAIfRocm, skipCPUIfNoLapack], | ||
|
@@ -9142,6 +9187,7 @@ def generate_std_var_kwargs(t: torch.Tensor, **kwargs): | |
dtypes=floating_and_complex_types(), | ||
check_batched_grad=False, | ||
check_batched_gradgrad=False, | ||
supports_forward_ad=True, | ||
gradcheck_nondet_tol=GRADCHECK_NONDET_TOL, | ||
supports_out=False, | ||
sample_inputs_func=sample_inputs_linalg_invertible, | ||
|
Uh oh!
There was an error while loading. Please reload this page.