{"record":{"id":"487a4a96b62642f5","repo":"jax-ml/jax","slug":"invalid-n-batch-n-dense-for-shape","errorCode":null,"errorMessage":"Invalid {n_batch=}, {n_dense=} for {shape=}","messagePattern":"Invalid (.+?), (.+?) for (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/experimental/sparse/random.py","lineNumber":70,"sourceCode":"    n_dense : number of batch dimensions. must satisfy ``n_dense >= 0`` and\n      ``n_batch + n_dense <= len(shape)``.\n    unique_indices : boolean specifying whether indices should be unique\n      (default: True).\n    sorted_indices : boolean specifying whether indices should be row-sorted in\n      lexicographical order (default: False).\n    generator : function for generating random values accepting a key, shape,\n      and dtype. It defaults to :func:`jax.random.uniform`, and may be any\n      function with a similar signature.\n    **kwds : additional keyword arguments to pass to ``generator``.\n\n  Returns:\n    arr : a sparse.BCOO array with the specified properties.\n  \"\"\"\n  shape = tuple(map(operator.index, shape))\n  n_batch = operator.index(n_batch)\n  n_dense = operator.index(n_dense)\n  if n_batch < 0 or n_dense < 0 or n_batch + n_dense > len(shape):\n    raise ValueError(f\"Invalid {n_batch=}, {n_dense=} for {shape=}\")\n  n_sparse = len(shape) - n_batch - n_dense\n  batch_shape, sparse_shape, dense_shape = map(tuple, split_list(shape, [n_batch, n_sparse]))\n  batch_size = math.prod(batch_shape)\n  sparse_size = math.prod(sparse_shape)\n  if not 0 <= nse < sparse_size:\n    raise ValueError(f\"got {nse=}, expected to be between 0 and {sparse_size}\")\n  if 0 < nse < 1:\n    nse = int(math.ceil(nse * sparse_size))\n  assert not isinstance(nse, float)\n  nse = operator.index(nse)\n\n  data_shape = batch_shape + (nse,) + dense_shape\n  indices_shape = batch_shape + (nse, n_sparse)\n  if indices_dtype is None:\n    indices_dtype = dtypes.default_int_dtype()\n  if sparse_size > jnp.iinfo(indices_dtype).max:\n    raise ValueError(f\"{indices_dtype=} does not have enough range to generate \"\n                     f\"sparse indices of size {sparse_size}.\")","sourceCodeStart":52,"sourceCodeEnd":88,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/experimental/sparse/random.py#L52-L88","documentation":"random_bcoo validates the layout split of the output shape: n_batch (batch dims), n_dense (dense/trailing dims) must each be >= 0 and their sum must not exceed the total rank of shape.","triggerScenarios":"Calling jax.experimental.sparse.random_bcoo(key, shape, n_batch, n_dense) with negative counts or n_batch + n_dense > len(shape).","commonSituations":"Computing n_dense = len(shape) - n_batch and then also passing n_batch, double-counting; rank changes after refactoring shape; passing len(shape) as n_dense for a fully-dense layout.","solutions":["Ensure 0 <= n_batch and 0 <= n_dense and n_batch + n_dense <= len(shape)","For an all-sparse layout pass n_batch=0, n_dense=0","Recompute the counts from the final shape right before the call"],"exampleFix":"// before\nM = random_bcoo(key, shape=(4, 4), n_batch=1, n_dense=4)\n// after\nM = random_bcoo(key, shape=(4, 4), n_batch=0, n_dense=0)","handlingStrategy":"validation","validationCode":"assert n_batch >= 0 and n_dense >= 0 and n_batch + n_dense <= len(shape)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Derive n_batch/n_dense from the final shape at the call site","Remember n_sparse = rank - n_batch - n_dense must be >= 1"],"tags":["jax","sparse","bcoo","random","validation"],"backgroundTag":"invalid-layout-parameters","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}