{"record":{"id":"33d3c9aec68471de","repo":"jax-ml/jax","slug":"symbolic-dimension-self-used-in-a-context-that","errorCode":null,"errorMessage":"Symbolic dimension '{self}' used in a context that requires a constant","messagePattern":"Symbolic dimension '(.+?)' used in a context that requires a constant","errorType":"exception","errorClass":"InconclusiveDimensionOperation","httpStatus":null,"severity":"error","filePath":"jax/_src/export/shape_poly.py","lineNumber":820,"sourceCode":"  def __rmod__(self, dividend):\n    if isinstance(dividend, core.Tracer) or not _convertible_to_poly(dividend):\n      return self.__jax_array__().__rmod__(dividend)\n    return _ensure_poly(dividend, \"mod\", self.scope).__mod__(self)\n\n  def __divmod__(self, divisor):\n    if isinstance(divisor, core.Tracer) or not _convertible_to_poly(divisor):\n      return self.__jax_array__().__divmod__(divisor)\n    return self._divmod(divisor)\n\n  def __rdivmod__(self, dividend):\n    if isinstance(dividend, core.Tracer) or not _convertible_to_poly(dividend):\n      return self.__jax_array__().__rdivmod__(dividend)\n    return _ensure_poly(dividend, \"divmod\", self.scope).__divmod__(self)\n\n  def __int__(self):\n    if (c := _DimExpr._to_constant(self)) is not None:\n      return c\n    raise InconclusiveDimensionOperation(f\"Symbolic dimension '{self}' used in a context that requires a constant\")\n\n  # We must overload __eq__ and __ne__, or else we get unsound defaults.\n  def __eq__(self, other: Any) -> bool:\n    if isinstance(other, type(self)):\n      if self.scope is not other.scope:\n        return False\n    elif not core.is_constant_dim(other):\n      return False\n\n    # Equality is used very frequently because expressions are cached. We could\n    # implement a more precise version based on `(self - other).bounds() = (0, 0)`\n    # but that would be too expensive. It would also have the unfortunate drawback\n    # that we cannot then cache `e.bounds()` because hashing invokes equality\n    # which would lead to infinite recursion.\n    diff = self - other\n\n    # We look for `self - other == k`, and we rely on the fact that when we\n    # normalize _DimExpr that represent integers as ints.","sourceCodeStart":802,"sourceCodeEnd":838,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/export/shape_poly.py#L802-L838","documentation":"Python calls int(x) when an object is used where a plain integer is required (e.g. range(dim), list slicing, building shapes for non-JAX APIs). A symbolic dimension has no known integer value, so it cannot be converted, raising InconclusiveDimensionOperation.","triggerScenarios":"range(symbolic_dim), np.zeros((dim,)), or any int(dim)/indexing use while tracing with polymorphic shapes under jax.export.","commonSituations":"Porting code to shape-polymorphic export where a batch dimension is used in Python-level loops, Python slicing with symbolic bounds, or passed to numpy/stdlib functions.","solutions":["Replace Python constructs with JAX equivalents (lax.iota / jnp.arange instead of range; jnp.zeros instead of np.zeros)","Move the int() usage outside the traced function or make that dimension concrete","Use jnp operations that accept traced values instead of host-side ints"],"exampleFix":"# before\nfor i in range(n): acc += x[i]  # n symbolic\n# after\nacc = jnp.sum(x, axis=0)  # or use vmap/lax.fori_loop with traced bounds","handlingStrategy":"type-guard","validationCode":"from jax._src.export.shape_poly import _DimExpr, InconclusiveDimensionOperation\nif isinstance(d, _DimExpr): use_jnp_ops(d)  # never int(d)","typeGuard":"def needs_constant(d) -> bool:\n    return isinstance(d, (int,)) and not hasattr(d, '_factors')","tryCatchPattern":"from jax._src.export import shape_poly\ntry:\n    n = int(dim)\nexcept shape_poly.InconclusiveDimensionOperation:\n    n = None  # switch to jnp.arange/lax equivalents","preventionTips":["Replace range()/np.* host calls with jnp./lax. equivalents in traced code","Audit for int(dim), slicing with symbolic bounds before export"],"tags":["jax","shape-polymorphism","int-conversion","tracing"],"backgroundTag":"symbolic-dimension-used-as-constant","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}