{"record":{"id":"c734c979d0abc19f","repo":"jax-ml/jax","slug":"index-in-subset-by-index-1-exceeds-matrix-size-c734c9","errorCode":null,"errorMessage":"Index in subset_by_index[1] exceeds matrix size.","messagePattern":"Index in subset_by_index\\[1\\] exceeds matrix size\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/_src/tpu/linalg/svd.py","lineNumber":190,"sourceCode":"      '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\n    is_flip = True\n\n  u_out_null: Array | None\n  q: Array | None","sourceCodeStart":172,"sourceCodeEnd":208,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/tpu/linalg/svd.py#L172-L208","documentation":"The end index of subset_by_index cannot exceed the matrix rank, i.e. min(m, n) of the input's last two dims. Only up to min(m, n) singular values exist, so the range must fit within that.","triggerScenarios":"svd(a, subset_by_index=(0, min(m, n) + 1)), or using the larger dimension instead of min(m, n) when computing the end bound, or running the same top-k code against smaller matrices than it was written for.","commonSituations":"Hard-coded k larger than the matrix's smaller dimension; batched code where some inputs have different shapes; assuming end can be max(m, n).","solutions":["Clamp end: end = min(end, min(a.shape[-2], a.shape[-1])).","For full SVD, omit subset_by_index instead of passing (0, rank).","Add a shape assertion in data pipelines feeding matrices of varying sizes."],"exampleFix":"// before\nsvd(a, subset_by_index=(0, 64))  # fails when min(m, n) < 64\n// after\nrank = min(a.shape[-2], a.shape[-1])\nsvd(a, subset_by_index=(0, min(64, rank)))","handlingStrategy":"validation","validationCode":"rank = min(a.shape[-2], a.shape[-1])\nlo, hi = max(0, sb[0]), min(sb[1], rank)\nassert lo < hi","typeGuard":"def fits_rank(sb, a) -> bool:\n    return sb[1] <= min(a.shape[-2], a.shape[-1])","tryCatchPattern":"try:\n    svd(a, subset_by_index=sb)\nexcept ValueError as e:\n    if 'exceeds matrix size' in str(e): sb = (sb[0], min(a.shape[-1], a.shape[-2]))\n    else: raise","preventionTips":["Derive end from the input's shape every call, never hard-code k.","In batched pipelines with varying shapes, clamp per-sample or pad inputs."],"tags":["jax","tpu","svd","linalg","bounds-check"],"backgroundTag":"index-out-of-range","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}