{"record":{"id":"097cd94d93115eef","repo":"jax-ml/jax","slug":"subset-by-index-must-be-a-tuple-of-size-2-097cd9","errorCode":null,"errorMessage":"subset_by_index must be a tuple of size 2.","messagePattern":"subset_by_index must be a tuple of size 2\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/_src/tpu/linalg/svd.py","lineNumber":177,"sourceCode":"      'specified to use `svd` within JAX transformations.')\n\n  hermitian = core.concrete_or_error(\n      bool,\n      hermitian,\n      'The `hermitian` argument must be statically '\n      'specified to use `svd` within JAX transformations.',\n  )\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","sourceCodeStart":159,"sourceCodeEnd":195,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/tpu/linalg/svd.py#L159-L195","documentation":"jax._src.tpu.linalg.svd raises this when the subset_by_index argument passed to jax.scipy.linalg.svd (TPU path) is not a 2-element tuple. subset_by_index selects a contiguous range of singular values/vectors to compute, and it must be specified as exactly (start, end) with concrete Python ints.","triggerScenarios":"Calling svd(a, subset_by_index=(0,)) or subset_by_index=[0, 3, 5] — any value whose len() != 2, e.g. passing a single int or a 3-tuple.","commonSituations":"Confusing subset_by_index with numpy's subset_by_index style APIs, or computing the tuple dynamically (e.g. from array shape arithmetic) and accidentally producing a 1- or 3-element sequence.","solutions":["Pass a 2-tuple of concrete ints: subset_by_index=(start, end), end exclusive, 0 <= start < end <= min(m, n).","If you don't need a partial SVD, omit subset_by_index entirely.","If computing bounds dynamically, assert len == 2 and both are Python ints (use operator.index) before calling."],"exampleFix":"// before\nu, s, vt = svd(a, subset_by_index=(k,))\n// after\nu, s, vt = svd(a, subset_by_index=(0, k))","handlingStrategy":"validation","validationCode":"from operator import index\ndef valid_subset(sb, a):\n    rank = min(a.shape[-2], a.shape[-1])\n    assert sb is None or (len(sb) == 2 and all(isinstance(index(i), int) for i in sb)), 'need (start, end)'\n    return sb","typeGuard":"def is_subset_by_index(x) -> bool:\n    return x is None or (isinstance(x, tuple) and len(x) == 2 and all(isinstance(v, int) for v in x))","tryCatchPattern":"try:\n    u, s, vt = svd(a, subset_by_index=sb)\nexcept ValueError as e:\n    if 'tuple of size 2' in str(e): raise ValueError(f'bad subset_by_index: {sb!r}')\n    raise","preventionTips":["Always build subset_by_index as an explicit (start, end) tuple literal or from validated ints.","Wrap dynamic bound computation with assert len(sb) == 2."],"tags":["jax","tpu","svd","linalg","argument-validation"],"backgroundTag":"invalid-argument-shape","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}