Shape polymorphism: detrend, hessenberg, unstack and einsum raise internal errors (AttributeError / bare AssertionError) instead of a clear error
Description
Four functions fail with an internal Python error when exported with symbolic dimensions they cannot support. JAX normally reports unsupported cases with InconclusiveDimensionOperation or a ValueError/TypeError that names the problem. These four don't. In two cases JAX already tries to raise a clear error, but the error path itself crashes.
import jax, jax.numpy as jnp
from jax import export
import jax.scipy.signal, jax.scipy.linalg
S = lambda spec: jax.ShapeDtypeStruct(export.symbolic_shape(spec), jnp.float32)
def ex(name, f, *specs):
try:
export.export(jax.jit(f))(*specs)
print(name, "-> OK")
except Exception as e:
print(name, "->", type(e).__name__ + ":", str(e).splitlines()[0][:150])
ex("1. detrend", jax.scipy.signal.detrend, S("b"))
ex("2. hessenberg", jax.scipy.linalg.hessenberg, S("b, b"))
ex("3. unstack", jnp.unstack, S("b, 3"))
a, b, c, d = export.symbolic_shape("a, b, c, d")
ex("4. einsum", lambda x, y: jnp.einsum("ij,jk->ik", x, y),
jax.ShapeDtypeStruct((a, b), jnp.float32), jax.ShapeDtypeStruct((c, d), jnp.float32))
1. detrend -> AttributeError: '_DimExpr' object has no attribute 'astype'
2. hessenberg -> AttributeError: 'jaxlib.mlir._mlir_libs._mlir.ir.BlockArgument' object has no attribute 'shape'
3. unstack -> AttributeError: 'NoneType' object has no attribute 'process_primitive'
4. einsum -> AssertionError: Incompatible reduction dimensions: lhs.shape=(c, d) lhs_names=jk rhs.shape=(a, b) rhs_names=ij
All four results are the same on jax 0.12.0.1 and 0.12.1.dev20261111 (CPU). Line numbers are for jax main.
1. jax.scipy.signal.detrend (type='linear', the default), jax/_src/scipy/signal.py:547:
jnp.arange(1, Npts + 1, dtype=data_arr.dtype) / Npts.astype(data_arr.dtype)
With a symbolic length N, bp_arr = np.sort(np.unique(np.r_[0, bp, N])) is an object array, so Npts is a _DimExpr, which has no .astype. Replacing it with jnp.asarray(Npts, dtype=data_arr.dtype) (tested on both versions) turns this into a regular InconclusiveDimensionOperation from a later symbolic comparison. So linear detrend over a symbolic axis is then refused cleanly. type='constant' already works.
2. jax.scipy.linalg.hessenberg, _hessenberg_cpu_lowering in jax/_src/lax/linalg.py:1424:
if not core.is_constant_dim(n):
raise ValueError("hessenberg requires the last dimension of a to be "
f"constant, got a.shape of {a.shape}.")
a is the MLIR value, so formatting the message raises an AttributeError. It should use a_aval.shape. With that change (tested by re-registering the patched lowering), the intended ValueError: hessenberg requires the last dimension of a to be constant, got a.shape of (b, b). is raised. Batch dimensions can still be symbolic: (n, 4, 4) exports and matches.
3. jnp.unstack / lax.unstack on a symbolic axis. The number of results is a symbolic dimension. The unstack rules compute (tuple(shape),) * num_results (_unstack_shape_rule, jax/_src/lax/lax.py:8996; the dtype and weak-type rules do the same). Multiplying a tuple by a _DimExpr goes to _DimExpr.__rmul__ → __jax_array__ → dim_as_value_p.bind outside a trace, which fails with the NoneType error. A check in lax.unstack such as
if not core.is_constant_dim(arr.shape[axis]):
raise ValueError(f"unstack requires a constant size along axis {axis}, got shape {arr.shape}")
gives a clear error (tested). Unstacking along a constant axis of a symbolic array already works.
4. jnp.einsum with contracted dimensions that are not provably equal, jax/_src/numpy/einsum.py:566. A bare assert checks the contracted sizes. Under shape polymorphism, two different symbols compare unequal, so the user gets a bare AssertionError, with a comment above it saying it "can fail non-deterministically". For the same inputs, x @ y raises TypeError: dot_general requires contracting dimensions to have the same shape, got (b,) and (c,). Replacing the assert with a TypeError/ValueError naming both shapes would match dot_general. We hit this when the two sizes are related but were exported as separate symbols: a jax.scipy.linalg.companion matrix of a length-d0 vector (size d0 - 1) contracted with a length-d1 vector, with d0, d1 >= 2.
Searched before filing (2026-11-30) in jax-ml/jax: detrend symbolic, detrend export, "'_DimExpr' object has no attribute", hessenberg export, hessenberg BlockArgument, hessenberg symbolic, hessenberg shape polymorphism, unstack symbolic, unstack export, unstack process_primitive, einsum "Incompatible reduction dimensions", einsum shape polymorphism, einsum symbolic assertion. Nothing matching. #7397 (jax2tf einsum with several contractions, closed) is a different problem.
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.
Hi @gnecula, I noticed #43038 includes "Fixes #43020", so this issue would close once it merges. From what I can tell, #43038 covers the hessenberg part, but detrend, unstack and einsum from the original report are still open. I just wanted to check with you on how you'd like to handle those.
If it helps, I'd be happy to send a PR for the remaining three:
detrend: use jnp.asarray(Npts, dtype=...) instead of .astype, so a symbolic length gets the usual inconclusive-dimension error. unstack: raise a clear ValueError in lax.unstack when the size of the unstacked axis is symbolic. einsum: turn the bare assert into a ValueError (the same type as the concrete-shape mismatch), and update the existing incompatible_contractions_error harness.
I'd add the tests to _POLY_SHAPE_TEST_HARNESSES. Would that be ok, and would you prefer to keep this issue open for them or have me open a new one? Thanks for your time!
Feel free to submit PRs for the remaining issues.
Feel free to submit PRs for the remaining issues.
Thanks @gnecula! I opened #43202 for detrend, unstack and einsum. Since hessenberg is covered by #43038, I did not include it here.