{"record":{"id":"aa04530d97e23fa5","repo":"jax-ml/jax","slug":"indices-in-subset-by-index-must-be-non-negative-aa0453","errorCode":null,"errorMessage":"Indices in subset_by_index must be non-negative.","messagePattern":"Indices in subset_by_index must be non-negative\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/_src/tpu/linalg/svd.py","lineNumber":186,"sourceCode":"  max_iterations = core.concrete_or_error(\n      int,\n      max_iterations,\n      'The `max_iterations` argument must be statically '\n      'specified to use `svd` within JAX transformations.',\n  )\n\n  if subset_by_index is not None:\n    if len(subset_by_index) != 2:\n      raise ValueError('subset_by_index must be a tuple of size 2.')\n    # Make sure subset_by_index is a concrete tuple.\n    subset_by_index = (\n        operator.index(subset_by_index[0]),\n        operator.index(subset_by_index[1]),\n    )\n    if subset_by_index[0] >= subset_by_index[1]:\n      raise ValueError('Got empty index range in subset_by_index.')\n    if subset_by_index[0] < 0:\n      raise ValueError('Indices in subset_by_index must be non-negative.')\n    m, n = a.shape\n    rank = n if n < m else m\n    if subset_by_index[1] > rank:\n      raise ValueError('Index in subset_by_index[1] exceeds matrix size.')\n    if full_matrices and subset_by_index != (0, rank):\n      raise ValueError(\n          'full_matrices and subset_by_index cannot be both be set.'\n      )\n    # By convention, eigenvalues are numbered in non-decreasing order, while\n    # singular values are numbered non-increasing order, so change\n    # subset_by_index accordingly.\n    subset_by_index = (rank - subset_by_index[1], rank - subset_by_index[0])\n\n  m, n = a.shape\n  is_flip = False\n  if m < n:\n    a = a.T.conj()\n    m, n = a.shape","sourceCodeStart":168,"sourceCodeEnd":204,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/tpu/linalg/svd.py#L168-L204","documentation":"Both elements of subset_by_index must be non-negative integers. Negative indices (Python-style negative indexing) are not supported for selecting a singular value range in the TPU SVD.","triggerScenarios":"svd(a, subset_by_index=(-2, 3)) or any start < 0, e.g. porting numpy-style a[-3:] indexing logic into subset_by_index.","commonSituations":"Developers assuming numpy negative-index semantics; range computed as min(k, n-k) minus a constant that can dip below zero for small matrices.","solutions":["Normalize negative indices to positive: start = start % rank before calling.","Use only 0-based non-negative bounds: (0, k) for top-k.","Validate bounds against matrix shape min(m, n) before the call."],"exampleFix":"// before\nsvd(a, subset_by_index=(-k, rank))\n// after\nsvd(a, subset_by_index=(rank - k, rank))","handlingStrategy":"validation","validationCode":"if sb[0] < 0:\n    rank = min(a.shape[-2], a.shape[-1])\n    sb = (sb[0] % rank, sb[1])  # or: (rank + sb[0], rank)","typeGuard":"def has_nonneg_bounds(sb) -> bool:\n    return all(i >= 0 for i in sb)","tryCatchPattern":"try:\n    svd(a, subset_by_index=sb)\nexcept ValueError as e:\n    if 'non-negative' in str(e): sb = tuple(i % rank for i in sb)\n    else: raise","preventionTips":["Never reuse numpy negative-slice idioms with subset_by_index.","Convert -k to rank - k before the call."],"tags":["jax","tpu","svd","linalg","negative-index"],"backgroundTag":"negative-index-not-supported","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}