{"record":{"id":"a9699b9f23cd5a6b","repo":"jax-ml/jax","slug":"unsupported-index-dtype-s","errorCode":null,"errorMessage":"Unsupported index dtype: %s","messagePattern":"Unsupported index dtype: (.+?)","errorType":"validation","errorClass":"std::invalid_argument","httpStatus":null,"severity":"error","filePath":"jaxlib/gpu/sparse.cc","lineNumber":47,"sourceCode":"#include \"xla/tsl/python/lib/core/numpy.h\"\n\nnamespace nb = nanobind;\n\nnamespace jax {\nnamespace JAX_GPU_NAMESPACE {\nnamespace {\n\ngpusparseIndexType_t DtypeToCuSparseIndexType(const dtype& np_type) {\n  static auto* types =\n      new absl::flat_hash_map<std::pair<char, int>, gpusparseIndexType_t>({\n          {{'u', 2}, GPUSPARSE_INDEX_16U},\n          {{'i', 4}, GPUSPARSE_INDEX_32I},\n          {{'i', 8}, GPUSPARSE_INDEX_64I},\n      });\n  auto it = types->find({np_type.kind(), np_type.itemsize()});\n  if (it == types->end()) {\n    nb::str repr = nb::repr(np_type);\n    throw std::invalid_argument(\n        absl::StrFormat(\"Unsupported index dtype: %s\", repr.c_str()));\n  }\n  return it->second;\n}\n\ngpuDataType DtypeToCudaDataType(const dtype& np_type) {\n  static auto* types =\n      new absl::flat_hash_map<std::pair<char, int>, gpuDataType>({\n          {{'f', 2}, GPU_R_16F},\n          {{'c', 4}, GPU_C_16F},\n          {{'f', 4}, GPU_R_32F},\n          {{'c', 8}, GPU_C_32F},\n          {{'f', 8}, GPU_R_64F},\n          {{'c', 16}, GPU_C_64F},\n#ifdef JAX_GPU_CUDA\n          {{'i', 1}, CUDA_R_8I},\n          {{'u', 1}, CUDA_R_8U},\n          {{'i', 4}, CUDA_R_32I},","sourceCodeStart":29,"sourceCodeEnd":65,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jaxlib/gpu/sparse.cc#L29-L65","documentation":"cuSPARSE matrix indices must be 32- or 64-bit integers; this helper maps NumPy dtypes to cusparseIndexType_t and throws when the dtype's (kind, itemsize) is not in that set.","triggerScenarios":"Building a sparse matrix descriptor in jaxlib.cudasparse (or jax.experimental.sparse on GPU) with indices dtype of float, bool, uint, or 16-bit int (e.g. indices stored as np.uint32 or bfloat16).","commonSituations":"Users building BCOO/CSR matrices whose index arrays got upcast or created as unsigned/short ints; older jax versions defaulting indices to unusual dtypes.","solutions":["Cast indices to np.int32 (or np.int64 for large matrices) before the call","Check matrix nnz > INT32_MAX and use int64 indices then","Upgrade jax/jaxlib — newer versions handle index dtype conversion internally"],"exampleFix":"# before\nmat.indices.astype(jax.numpy.uint32)\n# after\nmat.indices.astype(jax.numpy.int32)","handlingStrategy":"type-guard","validationCode":"idx = np.asarray(indices)\nassert idx.dtype in (np.int32, np.int64), f'bad index dtype {idx.dtype}'","typeGuard":"def valid_index_dtype(a) -> bool:\n    return a.dtype in (np.int32, np.int64)","tryCatchPattern":null,"preventionTips":["Always create index arrays with explicit jnp.int32/int64 dtype","Cast indices before passing to cusparse-backed ops"],"tags":["jax","cuda","cusparse","sparse","dtype"],"backgroundTag":"unsupported-dtype","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}