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

cholesky_solve_backward: speed up using output_mask #112981

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion tools/autograd/derivatives.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@
L: cholesky_jvp(self_t, L, upper)

- name: cholesky_solve(Tensor self, Tensor input2, bool upper=False) -> Tensor
self, input2: cholesky_solve_backward(grad, self, input2, result, upper)
self, input2: cholesky_solve_backward(grad, self, input2, result, upper, grad_input_mask)
result: cholesky_solve_jvp(result, input2_p, input2_t, self_t, upper)

- name: cholesky_inverse(Tensor self, bool upper=False) -> Tensor
Expand Down
17 changes: 10 additions & 7 deletions torch/csrc/autograd/FunctionsManual.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4450,19 +4450,22 @@ std::tuple<Tensor, Tensor> cholesky_solve_backward(
const Tensor& self,
const Tensor& input2,
const Tensor& result,
const bool upper) {
const bool upper,
std::array<bool, 2> output_mask) {
at::NoTF32Guard disable_tf32;
Tensor grad_self, grad_input2;
if (grad_x.defined()) {
grad_self = grad_x.cholesky_solve(input2, /*upper=*/upper);

Tensor common_term = at::matmul(grad_self, result.mH());
common_term = common_term + common_term.mH();
if (output_mask[1]) {
Tensor common_term = at::matmul(grad_self, result.mH());
common_term = common_term + common_term.mH();

if (upper) {
grad_input2 = -at::matmul(input2, common_term);
} else {
grad_input2 = -at::matmul(common_term, input2);
if (upper) {
grad_input2 = -at::matmul(input2, common_term);
} else {
grad_input2 = -at::matmul(common_term, input2);
}
}
}
return std::tuple<Tensor, Tensor>{grad_self, grad_input2};
Expand Down
3 changes: 2 additions & 1 deletion torch/csrc/autograd/FunctionsManual.h
Original file line number Diff line number Diff line change
Expand Up @@ -743,7 +743,8 @@ std::tuple<Tensor, Tensor> cholesky_solve_backward(
const Tensor& self,
const Tensor& input2,
const Tensor& result,
const bool upper);
const bool upper,
std::array<bool, 2> output_mask);
Tensor cholesky_solve_jvp(
const Tensor& X,
const Tensor& U,
Expand Down