{"record":{"id":"bb8d84dd37ee2a11","repo":"jax-ml/jax","slug":"bscr-from-bcoo-requires-n-sparse-2-got-arr-n-spa","errorCode":null,"errorMessage":"BSCR.from_bcoo requires n_sparse=2; got {arr.n_sparse=}","messagePattern":"BSCR\\.from_bcoo requires n_sparse=2; got (.+?)","errorType":"exception","errorClass":"NotImplementedError","httpStatus":null,"severity":"error","filePath":"jax/experimental/sparse/bcsr.py","lineNumber":980,"sourceCode":"  @classmethod\n  def fromdense(cls, mat, *, nse=None, index_dtype=np.int32, n_dense=0,\n                n_batch=0):\n    \"\"\"Create a BCSR array from a (dense) :class:`Array`.\"\"\"\n    return bcsr_fromdense(mat, nse=nse, index_dtype=index_dtype,\n                          n_dense=n_dense, n_batch=n_batch)\n\n  def todense(self):\n    \"\"\"Create a dense version of the array.\"\"\"\n    return bcsr_todense(self)\n\n  def to_bcoo(self) -> bcoo.BCOO:\n    coo_indices = _bcsr_to_bcoo(self.indices, self.indptr, shape=self.shape)\n    return bcoo.BCOO((self.data, coo_indices), shape=self.shape)\n\n  @classmethod\n  def from_bcoo(cls, arr: bcoo.BCOO) -> BCSR:\n    if arr.n_sparse != 2:\n      raise NotImplementedError(f\"BSCR.from_bcoo requires n_sparse=2; got {arr.n_sparse=}\")\n    if not arr.indices_sorted:\n      arr = arr.sort_indices()\n    indices, indptr = _bcoo_to_bcsr(\n        arr.indices, shape=arr.shape, index_dtype=arr.indices.dtype\n    )\n    return cls((arr.data, indices, indptr), shape=arr.shape)\n\n  @classmethod\n  def from_scipy_sparse(cls, mat, *, index_dtype=None, n_dense=0, n_batch=0):\n    \"\"\"Create a BCSR array from a :mod:`scipy.sparse` array.\"\"\"\n    if n_dense != 0 or n_batch != 0:\n      raise NotImplementedError(\"BCSR from_scipy_sparse with nonzero n_dense/n_batch.\")\n\n    if mat.ndim != 2:\n      raise ValueError(f\"BCSR from_scipy_sparse requires 2D array; {mat.ndim}D is given.\")\n\n    mat = mat.tocsr()\n    data = jnp.asarray(mat.data)","sourceCodeStart":962,"sourceCodeEnd":998,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/experimental/sparse/bcsr.py#L962-L998","documentation":"BCSR.from_bcoo converts a BCOO array into the BCSR format, which requires exactly 2 sparse dimensions (one indptr/row dim and one column-index dim). If the BCOO array has n_sparse != 2, the conversion is structurally impossible and a NotImplementedError is raised. Convert to a 2-sparse-dim BCOO first, or keep BCOO.","triggerScenarios":"Calling BCSR.from_bcoo(bcoo_array) where bcoo_array.n_sparse (len(shape) - n_dense - n_batch) is not 2 — e.g. a 3-sparse-dim BCOO, or a 1D-sparse BCOO vector. Indirectly hit via BCSR operations (todense, matvec, eliminate_zeros, sum_duplicates, broadcast_in_dim, concatenate) that internally convert from BCOO with the wrong layout.","commonSituations":"Building a BCOO with all dimensions sparse (the default) and then feeding it to BCSR; batched pipelines where batch dims were not declared via n_batch in the BCOO.","solutions":["Construct the source BCOO with n_batch/n_dense set so exactly 2 dims remain sparse","If the extra dims are batch dims, rebuild the BCOO with n_batch=... or use bcoo.reshape_to_batched to move leading dims into batch dims before converting","Keep the array as BCOO if you truly need n_sparse != 2","Wrap conversions in try/except NotImplementedError and fall back to BCOO paths"],"exampleFix":"# before\nx = bcoo.bcoo_fromdense(dense_3d)  # n_sparse=3\ny = BCSR.from_bcoo(x)  # NotImplementedError\n\n# after\nx = bcoo.reshape_to_batched(bcoo.bcoo_fromdense(dense_3d), 1)  # 1 batch + 2 sparse\ny = BCSR.from_bcoo(x)","handlingStrategy":"validation","validationCode":"assert arr.n_sparse == 2, f'n_sparse={arr.n_sparse}; move leading dims to n_batch or use BCOO'","typeGuard":"def bcoo_is_bcsr_convertible(arr) -> bool:\n    return arr.n_sparse == 2","tryCatchPattern":"try:\n    b = BCSR.from_bcoo(arr)\nexcept NotImplementedError:\n    b = arr  # keep as BCOO","preventionTips":["Construct BCOOs destined for BCSR with explicit n_batch/n_dense from the start","Check arr.n_sparse before converting","Use bcoo.reshape_to_batched to fix layout before conversion"],"tags":["jax","sparse","bcsr","bcoo","format-conversion"],"backgroundTag":"sparse-format-conversion-unsupported","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}