jax icon indicating copy to clipboard operation
jax copied to clipboard

jnp.linalg.matrix_rank fails under shape polymorphism (jax.export): '_DimExpr' object has no attribute 'astype'

Open anna-researcher opened this issue 4 days ago • 4 comments

Description

jnp.linalg.matrix_rank cannot be exported with symbolic matrix dimensions. The default tolerance computes max(M.shape[-2:]) with NumPy and calls .astype on the result, which fails for symbolic dimensions:

import jax, jax.numpy as jnp
from jax import export

S = lambda spec: jax.ShapeDtypeStruct(export.symbolic_shape(spec), jnp.float32)
for spec in ["b, b", "a, b"]:   # square, non-square
    try:
        export.export(jax.jit(jnp.linalg.matrix_rank))(S(spec))
        print(spec, "-> OK")
    except Exception as e:
        print(spec, "->", type(e).__name__ + ":", e)
b, b -> AttributeError: '_DimExpr' object has no attribute 'astype'
a, b -> InconclusiveDimensionOperation: Symbolic dimension comparison 'a' >= 'b' is inconclusive.

The result is the same on jax 0.12.0.1 and 0.12.1.dev20261111 (CPU). Every other step of matrix_rank (svd with compute_uv=False, the comparison and the sum) works with symbolic shapes. Only the default rtol computation fails. Passing rtol explicitly avoids it.

Cause. jax/_src/numpy/linalg.py, matrix_rank (line 470 on main):

    rtol = S.max(-1) * np.max(M.shape[-2:]).astype(S.dtype) * jnp.finfo(S.dtype).eps
  • For a static shape, np.max of the tuple returns a NumPy integer, which has .astype.
  • For (b, b), np.max returns the _DimExpr b, which has no .astype.
  • For (a, b), np.max has to compare a and b, which is inconclusive.

Possible fix.

    rtol = S.max(-1) * jnp.asarray(core.max_dim(*M.shape[-2:]), S.dtype) * jnp.finfo(S.dtype).eps

I tested a patched copy of matrix_rank on both versions above. For specs (b, b), (a, b) and (n, a, b), the export and call succeed, and the result equals the static jnp.linalg.matrix_rank on rank-deficient inputs. The static result is unchanged.

Searched before filing (2026-11-30) in jax-ml/jax: matrix_rank shape polymorphism, matrix_rank export, "'_DimExpr' object has no attribute". Nothing matching.

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

jax:    0.12.1.dev20261111
jaxlib: 0.12.1.dev20261111
numpy:  2.5.4
python: 3.12.7 | packaged by Anaconda, Inc. | (main, Oct  4 2024, 13:27:36) [GCC 11.2.0]
device info: cpu-1, 1 local devices"
process_count: 1
platform: uname_result(system='Linux', release='6.8.0-139-generic', version='#139-Ubuntu SMP PREEMPT_DYNAMIC Sat Aug  1 03:52:05 UTC 2026', machine='x86_64')
JAX_PLATFORMS=cpu

Also reproduces with jax/jaxlib 0.12.0.1 (numpy 2.4.6, python 3.12.13) on the same machine.

anna-researcher avatar Nov 30 '26 23:11 anna-researcher

The fix you propose looks fine.

gnecula avatar Dec 01 '26 14:12 gnecula

The fix you propose looks fine.

Thanks for taking a look @gnecula ! Would you be ok with me sending a PR for this? I'd make the rtol change in matrix_rank and add a shape-polymorphism test covering (b, b), (a, b) and a batched (n, a, b) case.

anna-researcher avatar Dec 02 '26 04:12 anna-researcher

Yes, please do. When you add tests, please try to add it using the harness in _POLY_SHAPE_TEST_HARNESSES

gnecula avatar Dec 02 '26 04:12 gnecula

Yes, please do. When you add tests, please try to add it using the harness in _POLY_SHAPE_TEST_HARNESSES

@gnecula I've opened jax-ml/jax#43112 with the fix, plus tests in _POLY_SHAPE_TEST_HARNESSES covering (b, b), (a, b) and (n, a, b). Would you mind taking a look when you have time? Thanks!

anna-researcher avatar Dec 02 '26 20:12 anna-researcher