{"record":{"id":"64a33151a901f376","repo":"jax-ml/jax","slug":"got-empty-index-range-in-subset-by-index-64a331","errorCode":null,"errorMessage":"Got empty index range in subset_by_index.","messagePattern":"Got empty index range in subset_by_index\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/_src/tpu/linalg/svd.py","lineNumber":184,"sourceCode":"  )\n\n  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:","sourceCodeStart":166,"sourceCodeEnd":202,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/tpu/linalg/svd.py#L166-L202","documentation":"The subset_by_index range (start, end) must satisfy start < end; an 'empty' range like (3, 3) or (5, 2) is rejected. The SVD computation must select at least one singular value.","triggerScenarios":"svd(a, subset_by_index=(k, k)) or svd(a, subset_by_index=(hi, lo)) where start >= end, e.g. computing end as start due to an off-by-one or degenerate parameter like k=0 producing (0, 0).","commonSituations":"Off-by-one errors when computing the range from a rank/top-k parameter; requesting zero singular values when a data-dependent count collapses to 0.","solutions":["Ensure subset_by_index[0] < subset_by_index[1]; for top-k use (0, k) with k >= 1.","Clamp: start = max(0, start); end = max(start + 1, min(end, rank)).","Skip the svd call entirely when the requested count is zero instead of passing an empty range."],"exampleFix":"// before\nsvd(a, subset_by_index=(k, k))\n// after\nif k > 0:\n    svd(a, subset_by_index=(0, k))","handlingStrategy":"validation","validationCode":"start, end = sb\nassert 0 <= start < end <= min(a.shape[-2], a.shape[-1]), f'invalid subset range {sb}'","typeGuard":"def is_nonempty_range(sb) -> bool:\n    return isinstance(sb, tuple) and len(sb) == 2 and sb[0] < sb[1]","tryCatchPattern":"try:\n    svd(a, subset_by_index=(lo, hi))\nexcept ValueError as e:\n    if 'empty index range' in str(e): hi = lo + 1  # or skip\n    else: raise","preventionTips":["Treat requested-count k == 0 as a no-op in caller code instead of calling svd.","Clamp computed ranges: end = max(end, start + 1)."],"tags":["jax","tpu","svd","linalg","off-by-one"],"backgroundTag":"empty-range-validation","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}