10000 pinv: forward/backward AD which is Frechet-defined in a rank-preserving neighborhood. by nikitaved · Pull Request #66092 · pytorch/pytorch · GitHub
[go: up one dir, main page]

Skip to content

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

Closed
wants to merge 12 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 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 aten/src/ATen/native/native_functions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10553,18 +10553,26 @@
- func: linalg_pinv(Tensor self, float rcond=1e-15, bool hermitian=False) -> Tensor
python_module: linalg
variants: function
dispatch:
CompositeExplicitAutograd: linalg_pinv

- func: linalg_pinv.rcond_tensor(Tensor self, Tensor rcond, bool hermitian=False) -> Tensor
python_module: linalg
variants: function
dispatch:
CompositeExplicitAutograd: linalg_pinv

- func: linalg_pinv.out(Tensor self, float rcond=1e-15, bool hermitian=False, *, Tensor(a!) out) -> Tensor(a!)
python_module: linalg
variants: function
dispatch:
CompositeExplicitAutograd: linalg_pinv_out

- func: linalg_pinv.out_rcond_tensor(Tensor self, Tensor rcond, bool hermitian=False, *, Tensor(a!) out) -> Tensor(a!)
python_module: linalg
variants: function
dispatch:
CompositeExplicitAutograd: linalg_pinv_out

- func: linalg_solve(Tensor input, Tensor other) -> Tensor
python_module: linalg
Expand Down
4 changes: 4 additions & 0 deletions tools/autograd/derivatives.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -749,6 +749,10 @@
self: -at::matmul(inverse.conj().transpose(-2, -1), at::matmul(grad, inverse.conj().transpose(-2, -1)))
inverse: -at::matmul(at::matmul(inverse, self_t), inverse)

- name: linalg_pinv(Tensor self, float rcond=1e-15, bool hermitian=False) -> Tensor
self: pinv_backward(grad, result, self)
result: pinv_jvp(self_p, result, self_t)

- name: isnan(Tensor self) -> Tensor
self: non_differentiable

Expand Down
1 change: 1 addition & 0 deletions tools/autograd/gen_variable_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@
'index', 'masked_fill', 'cross', 'lu_unpack', 'renorm', '_conj_physical',
'scatter', 'scatter_add', 'sigmoid', 'sigmoid_backward', 'trapezoid', 'cumulative_trapezoid',
'conj_physical_', '_neg_view', '_reshape_alias', '_det_lu_based_helper', 'lu_solve', '_lu_with_info',
'linalg_pinv',
}

GRADIENT_IMPLEMENTED_FOR_SPARSE_COMPLEX = {
Expand Down
54 changes: 54 additions & 0 deletions torch/csrc/autograd/FunctionsManual.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1059,6 +1059,60 @@ Tensor cholesky_inverse_backward(Tensor grad, Tensor L, bool upper, Tensor inver
return grad_L;
}

// The formula for forward AD is adapted from
//
// Golub, Gene H., and Victor Pereyra. "The Differentiation of Pseudo-Inverses and Nonlinear
// Least Squares Problems Whose Variables Separate."
// SIAM Journal on Numerical Analysis 10(2). (1973). 413-432. doi: 10.1137/0710036
Tensor pinv_jvp(
const Tensor& A,
const Tensor& pinvA,
const Tensor& dA
) {
auto m = A.size(-2);
auto n = A.size(-1);
auto dAh = dA.transpose(-1, -2).conj();
auto pinvAh = pinvA.transpose(-1, -2).conj();
// optimization to produce matrices of the smallest dimension
if (m <= n) {
auto K = pinvAh.matmul(dAh);
return pinvA.matmul(K - K.transpose(-1, -2).conj() - K.matmul(A.matmul(pinvA)))
+ (dAh - pinvA.matmul(A.matmul(dAh))).matmul(pinvAh.matmul(pinvA));
}
else {
auto K = pinvA.matmul(dA);
auto Kh = K.transpose(-1, -2).conj();
return (Kh - K - pinvA.matmul(A).matmul(Kh)).matmul(pinvA)
+ (pinvA.matmul(pinvAh)).matmul(dAh - (dAh.matmul(A)).matmul(pinvA));
}
}

Tensor pinv_backward(
const Tensor& grad,
const Tensor& pinvA,
const Tensor& A
) {
auto m = A.size(-2);
auto n = A.size(-1);
auto pinvAh = pinvA.transpose(-1, -2).conj();
auto gradh = grad.transpose(-1, -2).conj();
// optimization to produce matrices of the smallest dimension
if (m <= n) {
auto K = gradh.matmul(pinvA);
auto KpinvAh = K.matmul(pinvAh);
return - (pinvA.matmul(K)).transpose(-1, -2).conj()
+ KpinvAh - (A.matmul(pinvA)).matmul(KpinvAh)
+ (pinvAh.matmul(pinvA)).matmul(gradh - K.matmul(A));
}
else {
auto K = pinvA.matmul(gradh);
auto pinvAhK = pinvAh.matmul(K);
return - (K.matmul(pinvA)).transpose(-1, -2).conj()
+ (gradh - A.matmul(K)).matmul(pinvA).matmul(pinvAh)
+ pinvAhK - pinvAhK.matmul(pinvA).matmul(A);
}
}

Tensor split_with_sizes_backward(const std::vector<torch::autograd::Variable> &grads,
IntArrayRef split_sizes, int64_t dim, IntArrayRef sizes, const at::TensorOptions &options) {
dim = at::maybe_wrap_dim(dim, sizes.size());
Expand Down
10 changes: 10 additions & 0 deletions torch/csrc/autograd/FunctionsManual.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,16 @@ at::Tensor masked_scatter_backward(const at::Tensor & grad, const at::Tensor & m
at::Tensor cholesky_backward(at::Tensor grad, bool upper, at::Tensor L);
at::Tensor cholesky_jvp(const at::Tensor& input_tangent, const at::Tensor& L, bool upper);
at::Tensor cholesky_inverse_backward(at::Tensor grad, at::Tensor L, bool upper, at::Tensor inverse);
Tensor pinv_jvp(
const Tensor& A,
const Tensor& pinvA,
const Tensor& dA
);
Tensor pinv_backward(
const Tensor& grad,
const Tensor& pinvA,
const Tensor& A
);
at::Tensor split_with_sizes_backward(const std::vector<torch::autograd::Variable> &grads,
IntArrayRef split_sizes, int64_t dim, IntArrayRef sizes, const at::TensorOptions &options);
at::Tensor split_backward(const std::vector<torch::autograd::Variable> &grads, int64_t split_size, int64_t dim, at::IntArrayRef sizes, const at::TensorOptions &options);
Expand Down
48 changes: 47 additions & 1 deletion torch/testing/_internal/common_methods_invocations.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Copy link
Collaborator

Choose a reason for hiding this comment

The 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?

Copy link
Collaborator Author
@nikitaved nikitaved Oct 5, 2021

Choose a reason for hiding this comment

The 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)

Expand Down Expand Up @@ -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
Copy link
Collaborator Author
@nikitaved nikitaved Oct 4, 2021

Choose a reason for hiding this comment

The 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],
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is the slowTest decorator working as expected here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes!

Copy link
Collaborator

Choose a reason for hiding this comment

The 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],
Expand Down Expand Up @@ -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,
Expand Down
0