{"record":{"id":"1edc742d553d7a66","repo":"jax-ml/jax","slug":"data-batch-dimensions-not-compatible-for-data-sha","errorCode":null,"errorMessage":"data batch dimensions not compatible for {data.shape=}, {shape=}","messagePattern":"data batch dimensions not compatible for (.+?), (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/experimental/sparse/bcoo.py","lineNumber":138,"sourceCode":"class BCOOProperties(NamedTuple):\n  n_batch: int\n  n_sparse: int\n  n_dense: int\n  nse: int\n\nclass Buffer(Protocol):\n  @property\n  def shape(self) -> Shape: ...\n  @property\n  def dtype(self) -> Any: ...\n\n\ndef _validate_bcoo(data: Buffer, indices: Buffer, shape: Sequence[int]) -> BCOOProperties:\n  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","sourceCodeStart":120,"sourceCodeEnd":156,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/experimental/sparse/bcoo.py#L120-L156","documentation":"For a valid BCOO, each batch dimension of data must equal the corresponding matrix batch dim or be 1 (broadcastable). If data.shape's leading batch dims don't match shape's batch dims, _validate_bcoo raises ValueError.","triggerScenarios":"Constructing a BCOO directly with BCOO((data, indices)) where data has shape like (3, nse, ...) but the declared shape has batch dim 2, or batch dims of data not in {1, batch_size}.","commonSituations":"Manually building BCOO buffers instead of using fromdense/sparsify; reshaping or slicing data without matching indices; mismatched batch sizes between data and indices.","solutions":["Construct via high-level APIs: BCOO.fromdense, sparsify, or jax.sparse.bcoo_fromdense","Ensure data.shape[:n_batch] matches shape[:n_batch] elementwise or is 1","Use bcoo_sum_duplicates / bcoo_eliminate_zeros and reshape helpers instead of manual buffer surgery"],"exampleFix":"// before\nm = BCOO((data, indices), shape=(2, 5, 5))  # data.shape[0]==3\n// after\nm = BCOO((data[:2], indices), shape=(2, 5, 5))","handlingStrategy":"validation","validationCode":"n_batch = bcoo.indices.ndim - 2\nassert all(d in (1, s) for d, s in zip(bcoo.data.shape[:n_batch], bcoo.shape[:n_batch]))","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Build BCOOs via fromdense/sparsify, not raw buffers","After slicing, re-validate data batch dims against shape"],"tags":["jax","sparse","bcoo","shape-validation","constructor"],"backgroundTag":"shape-validation-failed","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}