sgl-project/sglang · error · ValueError

Passing integer indices (e.g. from `enumerate(timesteps)`) a

Error message

Passing integer indices (e.g. from `enumerate(timesteps)`) as timesteps to `FlowMatchEulerDiscreteScheduler.step()` is not supported. Make sure to pass one of the `scheduler.timesteps` as a timestep.

What it means

step() rejects int / IntTensor / LongTensor timesteps because flow-match timesteps are continuous floats; integer values are ambiguous with loop indices and would break _init_step_index's searchsorted lookup.

Source

Thrown at python/sglang/multimodal_gen/runtime/models/schedulers/scheduling_flow_match_euler_discrete.py:492

            s_noise (`float`, defaults to 1.0):
                Scaling factor for noise added to the sample.
            generator (`torch.Generator`, *optional*):
                A random number generator.
            per_token_timesteps (`torch.Tensor`, *optional*):
                The timesteps for each token in the sample.
            return_dict (`bool`):
                Whether or not to return a
                [`~schedulers.scheduling_flow_match_euler_discrete.FlowMatchEulerDiscreteSchedulerOutput`] or tuple.

        Returns:
            [`~schedulers.scheduling_flow_match_euler_discrete.FlowMatchEulerDiscreteSchedulerOutput`] or `tuple`:
                If return_dict is `True`,
                [`~schedulers.scheduling_flow_match_euler_discrete.FlowMatchEulerDiscreteSchedulerOutput`] is returned,
                otherwise a tuple is returned where the first element is the sample tensor.
        """

        if isinstance(timestep, int | torch.IntTensor | torch.LongTensor):
            raise ValueError(
                (
                    "Passing integer indices (e.g. from `enumerate(timesteps)`) as timesteps to"
                    " `FlowMatchEulerDiscreteScheduler.step()` is not supported. Make sure to pass"
                    " one of the `scheduler.timesteps` as a timestep."
                ),
            )

        if self.step_index is None:
            self._init_step_index(timestep)

        # Upcast to avoid precision issues when computing prev_sample
        sample = sample.to(torch.float32)

        if per_token_timesteps is not None:
            per_token_sigmas = per_token_timesteps / self.config.num_train_timesteps

            sigmas = self.sigmas[:, None, None]
            lower_mask = sigmas < per_token_sigmas[None] - 1e-6

View on GitHub (pinned to 0132848349)

Solutions

  1. Pass the loop's t value (float), not the index
  2. Cast with float(t) or t.astype(np.float32) when timesteps come from numpy int arrays
  3. Use zip/tqdm over timesteps directly

Example fix

# before
for i, t in enumerate(scheduler.timesteps):
    sample = scheduler.step(model_output, i, sample).prev_sample
# after
for t in scheduler.timesteps:
    sample = scheduler.step(model_output, t, sample).prev_sample
Defensive patterns

Strategy: type-guard

Validate before calling

for t in scheduler.timesteps:
    t = float(t)
    scheduler.step(model_output, t, sample)

Type guard

def float_timestep(t):
    if isinstance(t, (int, np.integer, torch.IntTensor, torch.LongTensor)):
        return float(t)
    return t

Prevention

When it happens

Trigger: `for i, t in enumerate(scheduler.timesteps): scheduler.step(..., i, ...)`; passing numpy int64 t; casting timesteps with int() for logging then reusing them.

Common situations: The classic enumerate-index bug ported across diffusion codebases; timesteps arrays saved/loaded through integer numpy pipelines.

Related errors


AI-assisted analysis of sgl-project/sglang@0132848349 (2026-08-28). Data as JSON: /api/errors/e91a0a215a526ba9. Report an issue: GitHub.