jax icon indicating copy to clipboard operation
jax copied to clipboard

Doc: default accumulation type for dot_general is unspecified and varies by hardware generation

Open shungcp opened this issue 3 months ago • 6 comments

Description

Context

When preferred_element_type is not passed to jax.lax.dot / jax.lax.dot_general, the output dtype follows the usual type promotion rules (bf16 × bf16 -> bf16) and the accumulation type is left unspecified — the backend is free to choose it, and that choice can differ across hardware generations of the same backend.

This is not hypothetical. A recently merged fix in sglang-jax (https://github.com/sgl-project/sglang-jax/pull/1462) reports a model that decoded correctly on TPU v6e producing degenerate repetition on TPU v7x, traced to two un-pinned batched dot_generals in an absorbed-MLA attention path ('thd,rhd->thr' and 'thr,rhd->thd', batched over heads, contracting only 128 and 512 elements respectively). A standalone probe on the same shapes is reported to measure relative error 0.127 with the default accumulation vs 0.0000 with f32 accumulation (reference absmax 40.7) — consistent with the two generations having chosen different default accumulation precision. Adding preferred_element_type=jnp.float32 to both call sites fixed it.

There was no error, no warning, and nothing in the JAX documentation that would have led a user to suspect the default could change between generations.

Proposed documentation improvement

The jax.lax.dot docstring currently describes preferred_element_type as providing "a hint to the compiler to accumulate the dot product using this data type". XLA's own DotGeneral documentation is more explicit — it says the parameter "recommends the accumulation type for the given operation, however it is not guaranteed. This allows for some hardware backends to instead accumulate in a different type" (https://openxla.org/xla/operation_semantics#dotgeneral).

Neither page states the two things a user actually needs to know:

  1. Silent generation-to-generation differences. The default accumulator precision is unspecified and can vary silently depending on the underlying hardware generation, so identical code can produce different numerics on two TPU generations with no error and no warning.

  2. Explicit pinning is the guarantee. Setting precision to a DotAlgorithm / DotAlgorithmPreset (e.g. DotAlgorithmPreset.BF16_BF16_F32) guarantees the accumulation type — the docstring already says it is "the algorithm that must be used to accumulate" — and fails with UNIMPLEMENTED at compile time if the backend doesn't support it, rather than degrading silently. That makes it the right recommendation for code that must be numerically stable across generations. preferred_element_type usually achieves the same thing in practice, but by specification it is only a hint.

Aligning the JAX wording with XLA's, and adding a sentence pointing to DotAlgorithm for cross-generation guarantees, would save future users this debugging round.

Target location

The precision / preferred_element_type parameter docs in the jax.lax.dot docstring:

https://github.com/jax-ml/jax/blob/b5f0b86b385b4f6b5229b4ab908de9ca947c5d66/jax/_src/lax/lax.py#L2570-L2588

jax.lax.dot_general is documented as an alias of jax.lax.dot — its body is a one-line docstring plus a delegating call — so a single edit covers both entries on the API reference pages.

System info (python version, jaxlib version, accelerator, etc.)

jax:    0.11.0
jaxlib: 0.11.0
numpy:  2.5.1
python: 3.12.13 (main, Mar 10 2026, 18:17:25) [Clang 21.1.4 ]
device info: TPU v6 lite-8, 8 local devices"
process_count: 1
platform: uname_result(system='Linux', node='t1v-n-3a2da504-w-0', release='6.8.0-1015-gcp', version='#17~22.04.1-Ubuntu SMP Tue Sep  3 16:11:52 UTC 2024', machine='x86_64')

This also relates to accelerator generation v7x, which I don't currently have access to.

shungcp avatar Aug 11 '26 06:08 shungcp

Cc @jakevdp @mattjj for visibility on improving the jax.lax.dot docstring regarding accumulator precision semantics across hardware generations.

shungcp avatar Aug 11 '26 06:08 shungcp

Thank you so much for flagging and explaining this!

mattjj avatar Aug 12 '26 14:08 mattjj

@hawkinsp can we do more than a docs change? Like, actually pin things down by default (rather than leaving anything generation-dependent), or raise an error of some kind?

mattjj avatar Aug 12 '26 14:08 mattjj

Related: #39869

jakevdp avatar Aug 12 '26 16:08 jakevdp

Pinging the open question from above before the thread goes stale: @mattjj's docs-vs-pinning question to @hawkinsp and the relation to #39869 (string precision inputs possibly being deprecated) seem undecided after a month. To keep this unblocked either way: a docs-only stopgap PR is ready to go this week if useful — in the jax.lax.dot/dot_general param docs, align preferred_element_type with XLA's wording (a hint; backends may accumulate differently), and add a sentence that the default accumulation type is unspecified and can vary silently across hardware generations, with a DotAlgorithm/preset being the only way to pin it (compile-time UNIMPLEMENTED instead of silent drift). Both facts stand independently of how #39869 resolves; if a stricter default or an error is planned, the wording would just shrink later. @hawkinsp @jakevdp — should I open that docs PR now, or hold until the semantics direction is settled?

abhinav-phi avatar Sep 11 '26 10:09 abhinav-phi

I would like to pick up the docs-only stopgap proposed here (no PR has materialized since it was offered): clarify in the jax.lax.dot/dot_general preferred_element_type docs that it is only a hint per XLA semantics, and that the default accumulation type is unspecified and can vary silently across hardware generations, pointing at DotAlgorithm/DotAlgorithmPreset for exact control. Opening a PR shortly.

DeepikaMamidipelly009 avatar Sep 24 '26 17:09 DeepikaMamidipelly009