{"record":{"id":"f8faa0e9b03bb2d5","repo":"jax-ml/jax","slug":"invalid-name-set-in-op-name-op-valid-range-is-f8faa0","errorCode":null,"errorMessage":"Invalid {name} set in {op_name} op; valid range is [0, {rank}); got: {invalid_dim}.","messagePattern":"Invalid (.+?) set in (.+?) op; valid range is \\[0, (.+?)\\); got: (.+?)\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"jax/_src/lax/slicing.py","lineNumber":1799,"sourceCode":"    if dims[i] < dims[i - 1]:\n      raise TypeError(f\"{name} in {op_name} op must be sorted; got {dims}\")\n\ndef _dims_in_range(dims, rank, op_name, name):\n  for dim in dims:\n    if dim < 0 or dim >= rank:\n      raise TypeError(f\"Invalid {name} set in {op_name} op; valid range is \"\n                      f\"[0, {rank}); got: {dim}.\")\n\ndef _sorted_dims_in_range(dims, rank, op_name, name):\n  if len(dims) == 0:\n    return\n  invalid_dim = None\n  if dims[0] < 0:\n    invalid_dim = dims[0]\n  elif dims[-1] >= rank:\n    invalid_dim = dims[-1]\n  if invalid_dim:\n    raise TypeError(f\"Invalid {name} set in {op_name} op; valid range is \"\n                    f\"[0, {rank}); got: {invalid_dim}.\")\n\ndef _no_duplicate_dims(dims, op_name, name):\n  if len(set(dims)) != len(dims):\n    raise TypeError(f\"{name} in {op_name} op must not repeat; got: {dims}.\")\n\ndef _disjoint_dims(dims1, dims2, op_name, name1, name2):\n  if not set(dims1).isdisjoint(set(dims2)):\n    raise TypeError(f\"{name1} and {name2} in {op_name} op must be disjoint; \"\n                    f\"got: {dims1} and {dims2}.\")\n\ndef _gather_shape_rule(operand, indices, *, dimension_numbers,\n                       slice_sizes, unique_indices, indices_are_sorted,\n                       mode, fill_value):\n  \"\"\"Validates the well-formedness of the arguments to Gather.\n\n  The code implements the checks based on the detailed operation semantics of\n  XLA's `Gather <https://www.openxla.org/xla/operation_semantics#gather>`_","sourceCodeStart":1781,"sourceCodeEnd":1817,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/lax/slicing.py#L1781-L1817","documentation":"Optimized bounds check used by gather/scatter shape rules for already-sorted dim lists: it only inspects the first and last elements, and raises if dims[0] < 0 or dims[-1] >= rank. It produces the same 'Invalid {name} set' message as the linear check but identifies the boundary offender; empty lists pass trivially.","triggerScenarios":"Supplying a sorted dim list whose extremes are out of bounds, e.g. start_index_map=(0, 5) for a rank-3 operand to lax.gather, or update_window_dims starting at a negative value to lax.scatter.","commonSituations":"Same class of bugs as the unsorted variant: dimension numbers copied from another rank's config, computing 'last axis' as rank instead of rank-1, or dims derived from index tensors whose depth exceeds the operand rank.","solutions":["Check dims[0] >= 0 and dims[-1] < rank for every list in the dimension numbers; fix the value shown in the message.","Derive dim lists from operand.ndim at runtime rather than constants.","Use jnp.take / .at[] style APIs to avoid manual dimension numbers entirely."],"exampleFix":"# before\nrank = x.ndim  # 3\ndnums = lax.GatherDimensionNumbers(\n    offset_dims=(), collapsed_slice_dims=(0, 3), start_index_map=(0,))\nout = lax.gather(x, idx, dnums, slice_sizes=(1,))  # 3 >= rank -> TypeError\n\n# after\ndnums = lax.GatherDimensionNumbers(\n    offset_dims=(1,), collapsed_slice_dims=(0, 2), start_index_map=(0,))\nout = lax.gather(x, idx, dnums, slice_sizes=(1, 1))","handlingStrategy":"validation","validationCode":"def validate_sorted_dims(dims, rank, name):\n    if dims and (dims[0] < 0 or dims[-1] >= rank):\n        raise ValueError(f\"{name} out of range [0, {rank}): {dims}\")\n    return dims","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Validate list extremes against operand rank when dim lists are already sorted.","Regenerate dimension numbers automatically when operand rank changes in a refactor.","Keep a single helper that builds dnums so rank checks happen in one place."],"tags":["jax","lax","gather","scatter","dimension-out-of-range"],"backgroundTag":"dimension-out-of-range","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}