XLA dot_product_attention returns nonzero outputs and gradients for fully masked queries
Description
Found that jax.nn.dot_product_attention(..., implementation="xla") incorrectly incorporates masked values when a query has no valid keys.
Instead of excluding those values, it returns their mean and propagates nonzero gradients through them.
The issue reproduces on JAX 0.12.0.1, both CPU and NVIDIA Tesla T4 GPU, in eager and JIT execution. PyTorch returns zero outputs and gradients for the equivalent mask.
More importantly, I reproduced the impact on Transformer training, where excluded memory affected predictions, parameter gradients, and subsequent optimization.
Minimal reproduction
import jax
import jax.numpy as jnp
q = jnp.array([[[[1., 0.]], [[0., 1.]]]])
v = jnp.array([[[[3., 5.]], [[7., 11.]]]])
mask = jnp.array([[[[False, False],
[True, False]]]])
def attention(values):
return jax.nn.dot_product_attention(
q, q, values, mask=mask, implementation="xla"
)
print("Forward:", attention(v).reshape(2, 2))
print("JIT:", jax.jit(attention)(v).reshape(2, 2))
print("Gradient:", jax.grad(
lambda x: attention(x)[0, 0].sum()
)(v).reshape(2, 2))
Observed on CPU and GPU:
Forward: [[5. 8.]
[3. 5.]]
JIT: [[5. 8.]
[3. 5.]]
Gradient: [[0.5 0.5]
[0.5 0.5]]
The first query is fully masked, yet its output is [5, 8] rather than zero. The masked values also receive gradients.
Setting key_value_seq_lengths=0 produces the same nonzero attention output.
GPU verification
I independently compared JAX XLA against PyTorch CUDA on a Tesla T4.
| Measurement | JAX XLA | PyTorch |
|---|---|---|
| Fully masked output | [5, 8] | [0, 0] |
| Masked-value gradients | 0.5 per element | 0 |
| Fully masked output after adding 100 to excluded values | [105, 108] | Not tested |
After 16 identical SGD steps:
Value-projection parameter divergence (L2):
0.478680Maximum logit difference on examples with valid context:
0.668965
This demonstrates that an example with no valid retrieved tokens can influence shared model parameters and subsequently alter predictions for other examples.
All 14 training checks passed on GPU. The experiment used deterministic synthetic data; it demonstrates an optimization difference, not a generalization-quality difference.
Likely root cause
In jax/_src/nn/functions.py, _apply_masks replaces excluded logits with a finite large negative value.
When every key is masked, all logits become equal. The subsequent softmax therefore assigns uniform probabilities to excluded positions, producing their mean and allowing gradients to propagate.
Expected behavior
Fully masked queries should not depend on excluded values, either in the forward or backward pass.
Zero outputs and gradients would match PyTorch's behavior. If fully masked rows are unsupported, explicitly documenting or rejecting them would be preferable to silently incorporating masked values.
This potentially affects variable-length batches, empty retrieval contexts, padding masks, and Transformer training workloads.
Suggested regression coverage: fully masked and valid rows, zero KV lengths, eager/JIT execution, CPU/GPU, and masked-value gradients.
System info
JAX / jaxlib: 0.12.0.1
NumPy: 2.5.4
Python: 3.13.15
GPU: NVIDIA Tesla T4
NVIDIA driver: 580.178.04
CUDA driver version: 13.0
JAX backend: XLA GPU (cuda:0)
PyTorch reference: 2.11.0+cu128
Platform: Linux x86_64
JAX_ENABLE_X64=0
XLA_PYTHON_CLIENT_PREALLOCATE=falseAlso reproduced on CPU with the same JAX version.
Verification: CPU 5/5, GPU 9/9, Transformer training 14/14.
Related but distinct: #23349 concerns general key_value_seq_lengths masking behavior.