{"record":{"id":"6d636c0b7ee3905a","repo":"jax-ml/jax","slug":"sparse-rule-for-primitive-is-not-implemented-bec","errorCode":null,"errorMessage":"sparse rule for {primitive} is not implemented because it would result in dense output. If this is your intent, use sparse.todense() to convert your arguments to dense matrices.","messagePattern":"sparse rule for (.+?) is not implemented because it would result in dense output\\. If this is your intent, use sparse\\.todense\\(\\) to convert your arguments to dense matrices\\.","errorType":"exception","errorClass":"NotImplementedError","httpStatus":null,"severity":"error","filePath":"jax/experimental/sparse/transform.py","lineNumber":129,"sourceCode":"  lax.acos_p,\n  lax.acosh_p,\n  lax.bessel_i0e_p,\n  lax.cos_p,\n  lax.cosh_p,\n  lax.eq_p,\n  lax.exp_p,\n  lax.ge_p,\n  lax.gt_p,\n  lax.le_p,\n  lax.lt_p,\n  lax.log_p,\n  lax.ne_p,\n  lax.xor_p\n]\n\ndef _raise_unimplemented_primitive(primitive):\n  if primitive in _densifying_primitives:\n    raise NotImplementedError(f\"sparse rule for {primitive} is not implemented because it \"\n                              \"would result in dense output. If this is your intent, use \"\n                              \"sparse.todense() to convert your arguments to dense matrices.\")\n  raise NotImplementedError(f\"sparse rule for {primitive} is not implemented.\")\n\n\nArray = Any\nArrayOrSparse = Any\n\n\nclass SparsifyEnv:\n  \"\"\"Environment for sparse jaxpr evaluation.\n\n  The environment is essentially a collection of buffers and/or tracers\n  that may be shared between one or more SparsifyValue objects, which\n  represent sparse or dense arrays via indices into the list of buffers.\n  \"\"\"\n  _buffers : list[Array]\n","sourceCodeStart":111,"sourceCodeEnd":147,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/experimental/sparse/transform.py#L111-L147","documentation":"When running code under jax.experimental.sparse.sparsify, some LAX primitives (e.g. comparisons like ne, xor, and other _densifying_primitives) applied to sparse operands would produce dense output, which sparsify refuses to do silently.","triggerScenarios":"Inside a @sparse.sparsify-decorated function, applying densifying ops (lax.ne, xor, etc.) to a sparse BCOO/BCSR value, e.g. `(M != 0)` or boolean ops on sparse matrices.","commonSituations":"Comparing sparse matrices elementwise, building masks, or using bitwise ops on sparse values inside sparsified code.","solutions":["Convert the sparse argument to dense first with sparse.todense(M) before the op","Restructure to use sparsity-aware ops (e.g. operate on M.data instead)","Use sparse-aware primitives that preserve sparsity"],"exampleFix":"// before\n@sparse.sparsify\ndef f(M):\n  return (M != 0).sum()\n// after\n@sparse.sparsify\ndef f(M):\n  return (sparse.todense(M) != 0).sum()","handlingStrategy":"fallback","validationCode":"# inspect supported vs densifying primitives before writing sparsify code\nfrom jax.experimental.sparse import transform\n# avoid elementwise comparisons/bitwise ops on sparse values in sparsified fns","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Materialize sparse operands with sparse.todense() before comparison/bitwise ops","Operate on .data for elementwise predicates when semantics allow"],"tags":["jax","sparse","sparsify","lax","not-implemented"],"backgroundTag":"sparse-autodiff-unsupported-op","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}