{"record":{"id":"ff14275ef1c75ed6","repo":"jax-ml/jax","slug":"symbolic-dimension-cannot-be-raised-to-negative-po","errorCode":null,"errorMessage":"Symbolic dimension cannot be raised to negative powers: '{self}' ** '{power}'","messagePattern":"Symbolic dimension cannot be raised to negative powers: '(.+?)' \\*\\* '(.+?)'","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/_src/export/shape_poly.py","lineNumber":772,"sourceCode":"      return self.__jax_array__().__rmul__(other)\n    if isinstance(other, int):\n      if other == 1: return self\n      if other == 0: return 0\n      return _DimExpr._linear_combination(self, other, 0, 0, self.scope)\n    return _ensure_poly(other, \"mul\", self.scope).__mul__(self)\n\n  def __pow__(self, power: core.DimSize, modulo=None):\n    if modulo is not None:\n      raise NotImplementedError(\"__pow__ modulo not implemented\")\n    if is_symbolic_dim(power):\n      return power.__rpow__(self)\n    if power != int(power):\n      raise ValueError(f\"Symbolic dimension cannot be raised to non-integer powers: '{self}' ** '{power}'\")\n    if power >= 0:\n      return functools.reduce(op.mul, [self] * power, 1)\n    # We don't support negative powers, because JAX does not allow negative\n    # powers for integers\n    raise ValueError(f\"Symbolic dimension cannot be raised to negative powers: '{self}' ** '{power}'\")\n\n  def __rpow__(self, other, modulo=None):\n    if modulo is not None:\n      raise NotImplementedError(\"__rpow__ modulo not implemented\")\n    return self.__jax_array__().__rpow__(other)\n\n  def __floordiv__(self, divisor):\n    if isinstance(divisor, core.Tracer) or not _convertible_to_poly(divisor):\n      return self.__jax_array__().__floordiv__(divisor)\n    return self._divmod(divisor)[0]\n\n  def __rfloordiv__(self, other):\n    if isinstance(other, core.Tracer) or not _convertible_to_poly(other):\n      return self.__jax_array__().__rfloordiv__(other)\n    return _ensure_poly(other, \"floordiv\", self.scope).__floordiv__(self)\n\n  def __truediv__(self, divisor):\n    # Used for \"/\", which always returns a float","sourceCodeStart":754,"sourceCodeEnd":790,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/export/shape_poly.py#L754-L790","documentation":"JAX's symbolic dimension algebra does not support negative integer powers because JAX itself forbids negative powers on integer types. dim ** -1 therefore raises this ValueError.","triggerScenarios":"dim ** -1 or int ** negative where dim is symbolic (e.g. computing reciprocal counts from a batch dimension) during tracing/export.","commonSituations":"Generic Python numeric code that computes 1/n as n ** -1 running under polymorphic export or vmap with symbolic dims.","solutions":["Compute the reciprocal on a float array instead of on the symbolic dim (dim_as_float = jnp.asarray(dim, jnp.float32))","Rewrite 1/dim as division in the calling expression on floats","Avoid exponentiating dims with negative exponents; keep dims for shapes only"],"exampleFix":"# before\ninv = batch_dim ** -1\n# after\ninv = 1.0 / jnp.asarray(batch_dim, jnp.float32)","handlingStrategy":"type-guard","validationCode":"assert power >= 0 or not is_symbolic_dim(base), 'negative powers unsupported on symbolic dims'","typeGuard":"def safe_exp(base, p):\n    if p < 0 and is_symbolic_dim(base): return 1.0 / jnp.asarray(base, jnp.float32)\n    return base ** p","tryCatchPattern":"try:\n    r = dim ** -1\nexcept ValueError:\n    r = 1.0 / jnp.asarray(dim, jnp.float32)","preventionTips":["Compute reciprocals on float arrays, never on dims","Keep symbolic dims strictly for shape bookkeeping"],"tags":["jax","shape-polymorphism","pow","negative-exponent"],"backgroundTag":"symbolic-dimension-invalid-exponent","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}