{"record":{"id":"84c0060866179a1c","repo":"jax-ml/jax","slug":"transpose-permutation-isn-t-a-permutation-of-opera-84c006","errorCode":null,"errorMessage":"transpose permutation isn't a permutation of operand dimensions, got permutation {permutation} for shape {shape}.","messagePattern":"transpose permutation isn't a permutation of operand dimensions, got permutation (.+?) for shape (.+?)\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"jax/experimental/sparse/bcoo.py","lineNumber":539,"sourceCode":"  \"\"\"\n  buffers = _bcoo_transpose(mat.data, mat.indices, permutation=permutation, spinfo=mat._info)\n  out_shape = tuple(mat.shape[p] for p in permutation)\n  return BCOO(buffers, shape=out_shape, unique_indices=mat.unique_indices)\n\ndef _bcoo_transpose(data: Array, indices: Array, *,\n                    permutation: Sequence[int], spinfo: SparseInfo) -> tuple[Array, Array]:\n  permutation = tuple(permutation)\n  if permutation == tuple(range(len(spinfo.shape))):\n    return data, indices\n  else:\n    return bcoo_transpose_p.bind(data, indices, permutation=permutation,\n                                 spinfo=spinfo)\n\ndef _validate_permutation(data, indices, permutation, shape):\n  if not isinstance(permutation, (tuple, list, np.ndarray)):\n    raise TypeError(f\"transpose permutation must be a tuple/list/ndarray, got {type(permutation)}.\")\n  if tuple(sorted(permutation)) != tuple(range(len(shape))):\n    raise TypeError(\"transpose permutation isn't a permutation of operand dimensions, \"\n                    f\"got permutation {permutation} for shape {shape}.\")\n  n_batch, n_sparse, n_dense, _ = _validate_bcoo(data, indices, shape)\n  batch_perm = permutation[:n_batch]\n  sparse_perm = [p - n_batch for p in permutation[n_batch: n_batch + n_sparse]]\n  dense_perm = [p - n_sparse - n_batch for p in permutation[n_batch + n_sparse:]]\n  if n_batch and tuple(sorted(batch_perm)) != tuple(range(n_batch)):\n    raise NotImplementedError(\"transpose permutation cannot permute batch axes with non-batch axes; \"\n                              f\"got permutation {permutation}, with {n_batch=}.\")\n  if n_dense and tuple(sorted(dense_perm)) != tuple(range(n_dense)):\n    raise NotImplementedError(\"transpose permutation cannot permute dense axes with non-dense axes; \"\n                              f\"got permutation {permutation}, with {n_dense=}.\")\n  return batch_perm, sparse_perm, dense_perm\n\n@bcoo_transpose_p.def_impl\ndef _bcoo_transpose_impl(data, indices, *, permutation: Sequence[int], spinfo: SparseInfo):\n  batch_perm, sparse_perm, dense_perm = _validate_permutation(data, indices, permutation, spinfo.shape)\n  n_batch = len(batch_perm)\n  indices = indices[..., sparse_perm].transpose(*batch_perm, n_batch, n_batch + 1)","sourceCodeStart":521,"sourceCodeEnd":557,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/experimental/sparse/bcoo.py#L521-L557","documentation":"The transpose permutation must be a valid permutation of range(len(shape)) — each axis index exactly once. Duplicates, missing axes, out-of-range values, or wrong length raise TypeError.","triggerScenarios":"bcoo.transpose((0, 0)) on a 2D matrix, transpose((1, 2)) on a 2D shape, or a permutation with wrong length from reshape logic.","commonSituations":"Programmatically built permutations with bugs; reusing a permutation computed for a different-rank array; off-by-one axis indices.","solutions":["Validate perm covers all axes: check sorted(perm) == list(range(ndim)) before calling","Fix permutation generation logic for the actual rank"],"exampleFix":"// before\nout = bcoo.transpose((0,))  # 2D matrix, missing axis 1\n// after\nout = bcoo.transpose((1, 0))","handlingStrategy":"validation","validationCode":"assert sorted(map(int, perm)) == list(range(len(shape))), 'not a valid permutation'","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Unit-test permutation generators against sorted(perm) == range(ndim)","Check len(perm) == arr.ndim before transpose"],"tags":["jax","sparse","bcoo","transpose","permutation"],"backgroundTag":"invalid-permutation","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}