{"record":{"id":"f1a6cbc1224c03c0","repo":"jax-ml/jax","slug":"indices-batch-dimensions-not-compatible-for-indic","errorCode":null,"errorMessage":"indices batch dimensions not compatible for {indices.shape=}, {shape=}","messagePattern":"indices batch dimensions not compatible for (.+?), (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/experimental/sparse/bcoo.py","lineNumber":152,"sourceCode":"  props = _validate_bcoo_indices(indices, shape)\n  n_batch, n_sparse, n_dense, nse = props\n  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``.","sourceCodeStart":134,"sourceCodeEnd":170,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/experimental/sparse/bcoo.py#L134-L170","documentation":"Each batch dim of the indices array of a BCOO must equal the corresponding matrix batch dim or be 1 (broadcastable). Otherwise _validate_bcoo_indices raises ValueError.","triggerScenarios":"Directly constructing or slicing BCOO indices whose leading (batch) dims mismatch the declared shape's batch dims, e.g. indices.shape=(4,n,d) with shape batch dim 3.","commonSituations":"Manual indices manipulation; concatenating BCOOs with different batch sizes; off-by-one batch slicing.","solutions":["Align indices batch dims with shape batch dims (or 1) before constructing BCOO","Use bcoo_concatenate / BCOO methods rather than np.concatenate on buffers"],"exampleFix":"// before\nm = BCOO((data, indices), shape=(3, 5, 5))  # indices.shape[0]==4\n// after\nm = BCOO((data, indices), shape=(4, 5, 5))","handlingStrategy":"validation","validationCode":"n_batch = indices.ndim - 2\nassert all(d in (1, s) for d, s in zip(indices.shape[:n_batch], shape[:n_batch]))","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use bcoo_concatenate for combining sparse arrays","Check indices batch dims after any manual slicing"],"tags":["jax","sparse","bcoo","indices","shape-validation"],"backgroundTag":"shape-validation-failed","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}