{"record":{"id":"0120759d10b143b1","repo":"jax-ml/jax","slug":"got-nse-expected-to-be-between-0-and-sparse-s","errorCode":null,"errorMessage":"got {nse=}, expected to be between 0 and {sparse_size}","messagePattern":"got (.+?), expected to be between 0 and (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/experimental/sparse/random.py","lineNumber":76,"sourceCode":"    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}.\")\n  @vmap\n  def _indices(key):\n    if not sparse_shape:\n      return jnp.zeros((nse, n_sparse), dtype=indices_dtype)\n    flat_ind = random.choice(key, sparse_size, shape=(nse,),\n                             replace=not unique_indices).astype(indices_dtype)","sourceCodeStart":58,"sourceCodeEnd":94,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/experimental/sparse/random.py#L58-L94","documentation":"random_bcoo checks that nse (number of specified elements) lies in [0, sparse_size), where sparse_size is the product of the sparse dimensions. A fractional nse in (0,1) is interpreted as a density fraction; values >= sparse_size or negative are rejected.","triggerScenarios":"Passing an absolute nse larger than or equal to the number of sparse elements (e.g. nse=16 for a 4x4 matrix), or a negative nse, or a density fraction >= 1.","commonSituations":"Treating nse as a density (passing 0.5 works but 1.0 fails); computing nse from a bigger matrix shape than the one passed; off-by-one when nse == sparse_size for a 'fully dense' sparse matrix.","solutions":["Pass nse < math.prod(sparse_dims), or use a fractional density like 0.1 for sparse random matrices","For a dense result use random.normal/keyed dense generation instead","Recompute nse = int(density * sparse_size) with density < 1"],"exampleFix":"// before\nM = random_bcoo(key, shape=(4, 4), nse=16)\n// after\nM = random_bcoo(key, shape=(4, 4), nse=0.5)  # ~8 elements","handlingStrategy":"validation","validationCode":"sparse_size = math.prod(shape[n_batch:len(shape)-n_dense])\nassert 0 <= nse < sparse_size, f'nse={nse} out of range for sparse size {sparse_size}'","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use fractional nse (0-1) for densities and absolute counts strictly below sparse_size","Compute nse from the same shape object passed to random_bcoo"],"tags":["jax","sparse","bcoo","random","nse"],"backgroundTag":"invalid-sparsity-parameter","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}