{"record":{"id":"e158f12bc8868c93","repo":"jax-ml/jax","slug":"fill-value-shape-fv-aval-shape-is-not-broadcast","errorCode":null,"errorMessage":"fill_value shape {fv_aval.shape} is not broadcast-compatible with batch shape {batch_shape}","messagePattern":"fill_value shape (.+?) is not broadcast-compatible with batch shape (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/_src/numpy/hijax.py","lineNumber":219,"sourceCode":"        raise ValueError(f\"size must be a positive integer; got {size=}\")\n    if not dtypes.issubdtype(out_dtype, np.integer):\n      raise ValueError(f\"out_dtype must be integer typed; got {out_dtype=}\")\n    if not all(0 <= ax < a_aval.ndim for ax in axes):\n      raise ValueError(f\"axes out of range for array with {a_aval.ndim} dimensions:  {axes=}\")\n    if len(axes) != len(set(axes)):\n      raise ValueError(f\"duplicate axes are not allowed: {axes=}\")\n    if fill_value_avals and len(fill_value_avals) != len(axes):\n      raise ValueError(f\"Expected {len(axes)} fill values, got {len(fill_value_avals)}\")\n    if any(fv.dtype != out_dtype for fv in fill_value_avals):\n      raise ValueError(f\"Expected fill values to have dtype {out_dtype}, got {fill_value_avals}\")\n    batch_shape = tuple(\n        s for i, s in enumerate(a_aval.shape) if i not in axes\n    )\n    for fv_aval in fill_value_avals:\n      try:\n        broadcasted = lax.broadcast_shapes(fv_aval.shape, batch_shape)\n      except ValueError as e:\n        raise ValueError(\n            f\"fill_value shape {fv_aval.shape} is not broadcast-compatible with \"\n            f\"batch shape {batch_shape}\"\n        ) from e\n      if broadcasted != batch_shape:\n        raise ValueError(\n            f\"fill_value shape {fv_aval.shape} cannot be broadcast to \"\n            f\"batch shape {batch_shape} without expanding it.\"\n        )\n    self.in_avals = (a_aval, *fill_value_avals)\n\n    # Evaluate shape to set out_aval\n    self.out_aval = tree_util.tree_map(core.typeof, api.eval_shape(\n        functools.partial(_nonzero_impl, size=size, axes=axes, out_dtype=out_dtype),\n        a_aval, *fill_value_avals))\n\n    self.params = dict(\n        size=size,\n        axes=axes,","sourceCodeStart":201,"sourceCodeEnd":237,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/numpy/hijax.py#L201-L237","documentation":"Raised by the Nonzero HiJAX primitive when a fill_value's shape cannot be broadcast together with the batch shape (the input shape with the reduced axes removed). lax.broadcast_shapes raised a ValueError, which is re-raased with this clearer message. Fill values must be scalars or broadcast-compatible with the batch dims.","triggerScenarios":"Supplying a fill_value with shape (3,) when the batch shape is (4,) — the shapes conflict and cannot broadcast; or non-scalar fill values with incompatible trailing dims in direct primitive construction.","commonSituations":"Passing per-batch fill values computed from an array shaped against an older layout; broadcasting rules violated because dims of size 1 were expected.","solutions":["Use scalar fill values (shape ()), which always broadcast","Reshape fill values to be broadcast-compatible with batch_shape, or to exactly batch_shape"],"exampleFix":"# before\nfv = jnp.zeros((3,))  # batch shape is (4,)\n# after\nfv = jnp.zeros(())  # scalar broadcasts to any batch shape","handlingStrategy":"validation","validationCode":"try:\n    from jax._src.lax import lax\n    lax.broadcast_shapes(fv.shape, batch_shape)\nexcept ValueError:\n    raise SystemExit('fill_value incompatible with batch shape')","typeGuard":"def fill_broadcastable(fv_shape, batch_shape) -> bool:\n    try:\n        from jax._src.lax import lax\n        return lax.broadcast_shapes(tuple(fv_shape), tuple(batch_shape)) is not None\n    except ValueError:\n        return False","tryCatchPattern":null,"preventionTips":["Default to scalar fill values (shape ()), which always broadcast","Validate shapes with lax.broadcast_shapes before direct primitive construction"],"tags":["jax","nonzero","fill-value","broadcasting"],"backgroundTag":"broadcast-shape-mismatch","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}