Skip to content

[Bugfix] Fix a bug when 0 dim linalg det backwards - #177498

Closed
Skylion007 wants to merge 2 commits into
pytorch:mainfrom
Skylion007:skylion007/fix-linalg-det-backward-zero-2026-03-15
Closed

Skylion007 wants to merge 2 commits into
pytorch:mainfrom
Skylion007:skylion007/fix-linalg-det-backward-zero-2026-03-15

Conversation

@Skylion007

@Skylion007 Skylion007 commented Mar 15, 2026 •

Copy link
Copy Markdown
Collaborator

Zero strided tensor should have gradients be a zero stride tensor, not an undefined Tensor (implicitly 1). This can cause issues with 2nd order and higher derivatives. It also violates the invariants that grad.shape == tensor.shape

@pytorch-bot

pytorch-bot Bot commented Mar 15, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/177498

Note: Links to docs will display an error until the docs builds have been completed.

❌ 1 New Failure

As of commit 311518d with merge base 19afa71 (image):

NEW FAILURE - The following job has failed:

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@pytorch-bot pytorch-bot Bot added the release notes: linalg_frontend release notes category label Mar 15, 2026
@Skylion007 Skylion007 added the module: correctness (silent) issue that returns an incorrect result silently label Mar 15, 2026
@Skylion007
Skylion007 requested review from atalman and malfet March 15, 2026 23:57

@lezcano lezcano left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

SG in principle, but should add a proper regression test.

Comment thread test/test_linalg.py
Comment on lines +1053 to +1062
# When A has 0 elements (e.g. empty batch), backward should return a
# zeros tensor with the same shape as A, not an undefined tensor.
for shape in [(0, 3, 3), (2, 0, 0)]:
A = torch.randn(shape, device=device, dtype=dtype, requires_grad=True)
det = torch.linalg.det(A)
det.backward(torch.ones_like(det))
self.assertIsNotNone(A.grad)
self.assertEqual(A.grad.shape, A.shape)
self.assertEqual(A.grad, torch.zeros_like(A))

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This is not a regression test tho, is it?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

ah, you are checking the shape, fine.

@lezcano

lezcano commented Mar 16, 2026

Copy link
Copy Markdown
Collaborator

@pytorchbot merge

@pytorch-bot pytorch-bot Bot added the ciflow/trunk Trigger trunk jobs on your pull request label Mar 16, 2026
@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge started

Your change will be merged once all checks pass (ETA 0-4 Hours).

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge failed

Reason: 1 mandatory check(s) failed. The first few are:

Dig deeper by viewing the failures on hud

Details for Dev Infra team Raised by workflow job

Failing merge rule: Core Maintainers

@Skylion007

Copy link
Copy Markdown
Collaborator Author

@pytorchbot merge -i

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge started

Your change will be merged while ignoring the following 1 checks: pull / linux-jammy-cpu-py3.10-gcc11-bazel-test / build-and-test (default, 1, 1, lf.linux.4xlarge)

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

EmanueleCoradin pushed a commit to EmanueleCoradin/pytorch that referenced this pull request Mar 30, 2026
Zero strided tensor should have gradients be a zero stride tensor, not an undefined Tensor (implicitly 1). This can cause issues with 2nd order and higher derivatives. It also violates the invariants that grad.shape == tensor.shape
Pull Request resolved: pytorch#177498
Approved by: https://github.com/lezcano
AaronWang04 pushed a commit to AaronWang04/pytorch that referenced this pull request Mar 31, 2026
Zero strided tensor should have gradients be a zero stride tensor, not an undefined Tensor (implicitly 1). This can cause issues with 2nd order and higher derivatives. It also violates the invariants that grad.shape == tensor.shape
Pull Request resolved: pytorch#177498
Approved by: https://github.com/lezcano
gplutop7 pushed a commit to gplutop7/pytorch that referenced this pull request Jul 15, 2026
Zero strided tensor should have gradients be a zero stride tensor, not an undefined Tensor (implicitly 1). This can cause issues with 2nd order and higher derivatives. It also violates the invariants that grad.shape == tensor.shape
Pull Request resolved: pytorch#177498
Approved by: https://github.com/lezcano
akashveramd pushed a commit to akashveramd/pytorch that referenced this pull request Jul 21, 2026
…DAIfNoMagmaAndNoCusolver)

Kept both skipCUDAIfNoMagmaAndNoCusolver and skipCUDAIfNoMagmaAndNoLinalgsolver in
imports (2.11 still has tests using the former). Added skipCUDAIf import.
Dropped test_det_backward (requires C++ changes from two unrelated PRs pytorch#171225/pytorch#177498).
All 22 decorator swaps from the upstream PR applied as-is.

Co-authored-by: Cursor <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/trunk Trigger trunk jobs on your pull request Merged module: correctness (silent) issue that returns an incorrect result silently open source release notes: linalg_frontend release notes category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants