jax-ml/jax · error · ValueError

dynamic_slice requires mode='promise_in_bounds' or mode='cli

Error message

dynamic_slice requires mode='promise_in_bounds' or mode='clip'

What it means

to_dynamic_slice mirrors to_static_slice's mode restriction: only PROMISE_IN_BOUNDS and CLIP are valid. Modes like DROP or FILL cannot be honored by lax.dynamic_slice, so passing them raises ValueError.

Source

Thrown at jax/_src/numpy/indexing.py:481

      rev_axes=rev_axes,
      squeeze_axes=squeeze_axes,
      newaxis_dims=newaxis_dims,
    )

  def to_dynamic_slice(
      self, *,
      arr_is_sharded: bool = False,
      normalize_indices: bool = True,
      mode: str | slicing.GatherScatterMode | None) -> _DynamicSliceIndexer:
    """Convert to DynamicSliceIndexer data structure.

    If this is not possible, raise a ValueError, TypeError, or IndexError.
    """
    if mode is not None:
      parsed_mode = slicing.GatherScatterMode.from_any(mode)
      if parsed_mode not in [
          slicing.GatherScatterMode.PROMISE_IN_BOUNDS, slicing.GatherScatterMode.CLIP]:
        raise ValueError("dynamic_slice requires mode='promise_in_bounds' or mode='clip'")

    # For sharded inputs, indexing (like x[0]) and partial slices (like x[:2] as
    # opposed to x[:]) lead to incorrect sharding semantics when computed via slice.
    # TODO(yashkatariya): fix slice with sharding
    if arr_is_sharded and self.has_partial_slices():
      raise ValueError("dynamic_slice with partial slices does not support nontrivial array sharding.")

    for position, pidx in enumerate(self.indices):
      if pidx.typ in [IndexType.INTEGER, IndexType.ELLIPSIS, IndexType.NONE]:
        pass
      elif pidx.typ == IndexType.DYNAMIC_SLICE:
        assert isinstance(pidx.index, indexing.Slice)
        if pidx.index.stride != 1:
          raise TypeError("dynamic_slice: only unit steps supported in slice."
                          f" Got {pidx.index} at position {position}")
      elif pidx.typ == IndexType.SLICE:
        assert isinstance(pidx.index, slice)
        if pidx.index.step is not None and pidx.index.step not in [-1, 1]:

View on GitHub (pinned to 1e1c6a8fc0)

Solutions

  1. Remove the mode kwarg (default promise_in_bounds)
  2. Use mode='clip' to tolerate out-of-range dynamic starts
  3. Use jnp.take/lax.gather if fill/drop semantics are required

Example fix

// before
y = x[jax.ds(i, 5)]  # with mode='fill' configured
// after
y = x[jax.ds(i, 5)]  # default promise_in_bounds, ensure 0 <= i <= n-5
Defensive patterns

Strategy: validation

Validate before calling

assert mode in (None, 'promise_in_bounds', 'clip') or mode is None

Prevention

When it happens

Trigger: Calling the dynamic-slice lowering with mode='drop' or ('fill', value) — e.g. jnp.take with mode='fill' on a path that lowers to dynamic_slice, or x[jax.ds(...)] with an incompatible mode kwarg.

Common situations: Copy-pasting mode='fill' from jnp.take calls into dynamic-slice-based code; configuring unique_indices/mode globally for gather semantics.

Related errors


AI-assisted analysis of jax-ml/jax@1e1c6a8fc0 (2026-08-27). Data as JSON: /api/errors/153ae55c55ac9458. Report an issue: GitHub.