{"record":{"id":"66d4d67aa59146ae","repo":"jax-ml/jax","slug":"invalid-indices-shape-for-nse-n-batch","errorCode":null,"errorMessage":"Invalid ={indices.shape=} for {nse=}, {n_batch=}, {n_dense=}","messagePattern":"Invalid =(.+?) for (.+?), (.+?), (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/experimental/sparse/bcoo.py","lineNumber":154,"sourceCode":"  shape = tuple(shape)\n  if any(s1 not in (1, s2) for s1, s2 in safe_zip(data.shape[:n_batch], shape[:n_batch])):\n    raise ValueError(f\"data batch dimensions not compatible for {data.shape=}, {shape=}\")\n  if data.shape[n_batch:] != (nse,) + shape[n_batch + n_sparse:]:\n    raise ValueError(f\"Invalid {data.shape=} for {nse=}, {n_batch=}, {n_dense=}\")\n  return props\n\n\ndef _validate_bcoo_indices(indices: Buffer, shape: Sequence[int]) -> BCOOProperties:\n  assert jnp.issubdtype(indices.dtype, jnp.integer)\n  shape = tuple(shape)\n  nse, n_sparse = indices.shape[-2:]\n  n_batch = len(indices.shape) - 2\n  n_dense = len(shape) - n_batch - n_sparse\n  assert n_dense >= 0\n  if any(s1 not in (1, s2) for s1, s2 in safe_zip(indices.shape[:n_batch], shape[:n_batch])):\n    raise ValueError(f\"indices batch dimensions not compatible for {indices.shape=}, {shape=}\")\n  if indices.shape[n_batch:] != (nse, n_sparse):\n    raise ValueError(f\"Invalid ={indices.shape=} for {nse=}, {n_batch=}, {n_dense=}\")\n  return BCOOProperties(n_batch=n_batch, n_sparse=n_sparse, n_dense=n_dense, nse=nse)\n\n\n#----------------------------------------------------------------------\n# bcoo_todense\n\nbcoo_todense_p = core.Primitive('bcoo_todense')\n\ndef bcoo_todense(mat: BCOO) -> Array:\n  \"\"\"Convert batched sparse matrix to a dense matrix.\n\n  Args:\n    mat: BCOO matrix.\n\n  Returns:\n    mat_dense: dense version of ``mat``.\n  \"\"\"\n  return _bcoo_todense(mat.data, mat.indices, spinfo=mat._info)","sourceCodeStart":136,"sourceCodeEnd":172,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/experimental/sparse/bcoo.py#L136-L172","documentation":"The trailing part of BCOO indices must be exactly (nse, n_sparse) with n_sparse = indices.shape[-1]; this check is effectively an internal consistency assert expressed as an error. It fires when the indices layout is malformed after the batch dims.","triggerScenarios":"Corrupt or hand-mangled indices arrays where indices.shape[n_batch:] != (nse, n_sparse); almost always reached through _validate_bcoo or extract primitives with inconsistent buffers.","commonSituations":"Direct buffer manipulation, JIT caching stale buffers with different nse, or a bug in code producing indices.","solutions":["Rebuild the BCOO from scratch via fromdense or round-trip through todense","Avoid mutating .indices/.data in place; use public bcoo_* helpers","Note the message has a stray '=' (format quirk); treat it as internal invariant failure"],"exampleFix":null,"handlingStrategy":"fallback","validationCode":"assert indices.ndim >= 2 and indices.shape[-2:] == (indices.shape[-2], indices.shape[-1])","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Treat this as an internal invariant: rebuild the BCOO from scratch","Avoid in-place mutation of .indices"],"tags":["jax","sparse","bcoo","indices","invariant"],"backgroundTag":"shape-validation-failed","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}