torch.det returns an incorrect second derivative under nested JVP
- Dominant language
- Python
- Stars
- 103k
- Forks
- 29.5k
- PR merge metrics
- PR metrics pending
Description
### 🐛 Describe the bug
`torch.det` returns `8.82720588235294` for a nested forward-mode second derivative whose mathematically correct value is `4.0` in `float64`.
The determinant is exactly `(t + 2) * (2 * t + 3) - 1`. Its second derivative is identically `4`, and the matrix is nonsingular at `t = 0.7`. PyTorch’s nested JVP is not `4`.
This issue reproduces on CPU, so CUDA availability does not affect the result.
```python
import torch
dtype = torch.float64
def f(t):
one = torch.ones((), dtype=t.dtype)
a = torch.stack((torch.stack((t + 2, one)), torch.stack((one, 2 * t + 3))))
return torch.det(a)
def jvp1(t):
return torch.func.jvp(f, (t,), (torch.ones_like(t),))[1]
t = torch.tensor(0.7, dtype=dtype)
_, actual = torch.func.jvp(jvp1, (t,), (torch.ones_like(t),))
expected = torch.tensor(4.0, dtype=dtype)
print("actual:")
print(actual)
print("expected:")
print(expected)
```
### Actual vs expected result
```text
actual:
8.82720588235294
expected:
4.0
```
The discrepancy occurs in the nested (second-order) forward-mode derivative. The second derivative of this determinant is exactly `4`.
### Versions
```text
PyTorch version: 2.12.0+cu130
Is debug build: False
CUDA used to build PyTorch: 13.0
ROCM used to build PyTorch: N/A
OS: Ubuntu 24.04.3 LTS (x86_64)
Python version: 3.13.13 | packaged by Anaconda, Inc.
Python platform: Linux x86_64
Is CUDA available: False
GPU models:
GPU 0: NVIDIA RTX 6000 Ada Generation
GPU 1: NVIDIA RTX 6000 Ada Generation
Nvidia driver version: 570.211.01
CPU:
Model name: AMD Ryzen Threadripper PRO 7985WX 64-Cores
CPU(s): 128
Versions of relevant libraries:
[pip3] numpy==2.4.6
[pip3] torch==2.12.0
[pip3] triton==3.7.0
```
cc @jianyuh @nikitaved @mruberry @walterddr @Lezcano @Chillee @samdow @kshitij12345
Contributor guide
Research direction
Start by running the provided CPU reproduction with torch.func.jvp nested around torch.det and inspect the forward-mode derivative path for determinant. Compare the nested result with the expected second derivative of 4.0; done means the reproduction returns the mathematically correct value in float64.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- machine-learning
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Active
- Clarity
- Clearly specified
- Newbie friendliness
- 68/100