jax icon indicating copy to clipboard operation
jax copied to clipboard

jax.nn.scaled_matmul raises NotImplementedError on CPU with no graceful error

Open anna-researcher opened this issue 5 months ago • 3 comments

Description

jax.nn.scaled_matmul crashes immediately on CPU with an internal NotImplementedError rather than raising a clear user-facing error indicating the function requires GPU/cuDNN.

Minimal reproducer

import jax
import jax.numpy as jnp

key = jax.random.PRNGKey(0)
x = jax.random.normal(key, [3, 16, 16], dtype=jnp.bfloat16)

out = jax.nn.scaled_matmul(x, x, x, x)

Error

NotImplementedError: MLIR translation rule for primitive 'scaled_matmul' not found for platform cpu

Expected behavior

A clear error message stating that scaled_matmul requires a GPU with cuDNN support, not an internal MLIR lowering error.

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

jax: 0.10.1 jaxlib: 0.10.1 numpy: 2.4.6 python: 3.12.13 | packaged by Anaconda, Inc. | (main, Mar 19 2026, 20:20:58) [GCC 14.3.0] device info: cpu-1, 1 local devices JAX_PLATFORMS=cpu

anna-researcher avatar Jun 26 '26 23:06 anna-researcher

I would like to fix this

Root cause: jax.nn.scaled_matmul has no platform check. It directly calls a primitive that only registers MLIR lowerings for cuda/rocm, so it throws a raw internal NotImplementedError on CPU.

Plan: Add a clear NotImplementedError early in scaled_matmul() explaining the GPU requirement + add a test.

coder-jayp avatar Jun 27 '26 04:06 coder-jayp

Pinged @IvyZX, who reviewed the initial contribution in #26345. Is there reasonable way for us to implement this on CPU?

(For what it's worth, I don't think the error here is all that problematic: it explicitly tells you the issue: the function is not implemented on CPU)

jakevdp avatar Jun 27 '26 17:06 jakevdp

Is there reasonable way for us to implement this on CPU?

If we don't care for it to be numerically identical with GPU, then yes, this is just a standard quantized matmul. In fact, the docstring already documented the exact code to do it.

I'm not sure how to match the GPU/rocm version identically though.

IvyZX avatar Jun 30 '26 20:06 IvyZX