jax
jax copied to clipboard
`jax.nn.dot_product_attention` incompatible with `jax.jacobian`
Description
Hi,
jax.nn.dot_product_attention is currently incompatible with jax.jacobian. The following code:
import jax
import jax.numpy as jnp
from functools import partial
k1, k2, k3 = jax.random.split(jax.random.PRNGKey(0), 3)
q = jax.random.normal(k1, (2, 4, 16, 32), dtype=jnp.float16)
k = jax.random.normal(k2, (2, 4, 16, 32), dtype=jnp.float16)
v = jax.random.normal(k3, (2, 4, 16, 32), dtype=jnp.float16)
attn = partial(jax.nn.dot_product_attention, implementation='cudnn')
jax.jacobian(attn)(q, k, v)
produces
Traceback (most recent call last):
File "cudnn_bug_repro.py", line 11, in <module>
jax.jacobian(attn)(q, k, v)
File "jax/_src/api.py", line 812, in jacfun
jac = vmap(pullback)(_std_basis(y))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "jax/_src/cudnn/fused_attention_stablehlo.py", line 473, in _dot_product_attention_bwd_rule
grads = _dot_product_attention_bwd_p_wrapper.bind(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "jax/_src/cudnn/fused_attention_stablehlo.py", line 888, in _dot_product_attention_bwd_batcher
grad_output = jnp.reshape(grad_output, (B,) + query.shape[-3:])
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "jax/_src/numpy/lax_numpy.py", line 1980, in reshape
return a.reshape(shape, order=order) # pyrefly: ignore[missing-attribute]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "jax/_src/numpy/array_methods.py", line 375, in _reshape
newshape = _compute_newshape(self, args[0] if len(args) == 1 else args)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "jax/_src/numpy/array_methods.py", line 542, in _compute_newshape
raise TypeError(f"cannot reshape array of shape {arr.shape} (size {arr.size}) "
TypeError: cannot reshape array of shape (4096, 2, 4, 16, 32) (size 16777216) into shape (2, 4, 16, 32) (size 4096)
The issue has something to do with the vmap axis being handled improperly, but I am unsure what the correct fix for this issue is.
Best, Tim
System info (python version, jaxlib version, accelerator, etc.)
jax: 0.10.2.dev20260616+6afcac469
jaxlib: 0.10.1
numpy: 2.4.6
python: 3.11.15 (main, Jun 11 2026, 15:20:16) [GCC 14.3.0]
device info: NVIDIA GeForce RTX 3080 Ti-1, 1 local devices"
process_count: 1
platform: uname_result(system='Linux', node='darwin', release='6.8.0-124-generic', version='#124~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Tue May 26 21:05:19 UTC ', machine='x86_64')
$ nvidia-smi
Tue Jun 16 08:41:03 2026
+-----------------------------------------------------------------------------------------+
| NVIDIA-SMI 580.159.03 Driver Version: 580.159.03 CUDA Version: 13.0 |
+-----------------------------------------+------------------------+----------------------+
| GPU Name Persistence-M | Bus-Id Disp.A | Volatile Uncorr. ECC |
| Fan Temp Perf Pwr:Usage/Cap | Memory-Usage | GPU-Util Compute M. |
| | | MIG M. |
|=========================================+========================+======================|
| 0 NVIDIA GeForce RTX 3080 Ti Off | 00000000:0B:00.0 On | N/A |
| 0% 49C P8 43W / 370W | 1341MiB / 12288MiB | 25% Default |
| | | N/A |
+-----------------------------------------+------------------------+----------------------+
+-----------------------------------------------------------------------------------------+
| Processes: |
| GPU GI CI PID Type Process name GPU Memory |
| ID ID Usage |
|=========================================================================================|
| No running processes found |
+-----------------------------------------------------------------------------------------+
I'd like to work on this!
Tried to work on this - #39451. #38495 seem to be inactive and has reformatted a lot of files.
The fp8 batchers have the same bug will open a separate PR.
fix for fp8 batchers - #39452, stacked on #39451