sgl-project/sglang · error · ValueError

{name} must be a torch.Tensor

Error message

{name} must be a torch.Tensor

What it means

The validator _validate_unit_timestep requires the timestep argument to be an actual torch.Tensor, not a Python float/int or numpy array. The minimax_h3 flow-matching formulation normalizes timesteps to unit-scale tensors, so a scalar timestep passed straight from a discrete schedule breaks the API contract.

Source

Thrown at python/sglang/multimodal_gen/runtime/models/schedulers/scheduling_minimax_h3_euler_ancestral.py:17

# SPDX-License-Identifier: Apache-2.0
from __future__ import annotations

import math
from typing import Any

import torch


def _require_finite_tensor(tensor: torch.Tensor, name: str) -> None:
    if not bool(torch.isfinite(tensor).all().item()):
        raise ValueError(f"{name} must be finite")


def _validate_unit_timestep(timestep: torch.Tensor, name: str) -> None:
    if not isinstance(timestep, torch.Tensor):
        raise ValueError(f"{name} must be a torch.Tensor")
    if not torch.is_floating_point(timestep):
        raise ValueError(f"{name} must be a floating point tensor")
    _require_finite_tensor(timestep, name)
    out_of_range = (timestep < 0) | (timestep > 1)
    if bool(out_of_range.any().item()):
        raise ValueError(f"{name} must be in [0, 1]")


def _validate_sigma(value: float, name: str) -> float:
    sigma = float(value)
    if not math.isfinite(sigma):
        raise ValueError(f"{name} must be finite")
    if sigma < 0.0:
        raise ValueError(f"{name} must be non-negative")
    return sigma


def _validate_timestep_sigma_pair(

View on GitHub (pinned to 0132848349)

Solutions

  1. Wrap the timestep in a tensor: torch.tensor(t, dtype=torch.float32, device=xt.device)
  2. Build the whole (timestep, sigma) schedule as float tensors once at schedule-construction time so the scheduler always receives tensors

Example fix

# before
x0 = minimax_h3_rf_v_to_x0(xt, v, timestep=0.7)  # ValueError

# after
x0 = minimax_h3_rf_v_to_x0(xt, v, timestep=torch.tensor(0.7, device=xt.device))
Defensive patterns

Strategy: type-guard

Validate before calling

assert isinstance(timestep, torch.Tensor), "timestep must be a torch.Tensor"

Type guard

def is_timestep_tensor(t) -> bool:
    return isinstance(t, torch.Tensor)

Prevention

When it happens

Trigger: Calling minimax_h3_rf_v_to_x0 or _validate_timestep_sigma_pair with timestep=0.7 (float), timestep=700 (int), or a numpy array instead of a torch tensor.

Common situations: Porting code from diffusers schedulers where step(model_output, timestep, sample) conventionally receives an int or scalar timestep; passing numpy floats or Python numbers from a hand-rolled denoise loop into this flow-matching scheduler, which needs tensors for its elementwise `1 - timestep` sigma computation.

Understand the failure class

Background: "Wrong argument type", "must be a string", "expected Array or Prism::Scope": TypeError and ArgumentError when a library receives a value of the wrong type — this error's family across 28 libraries.

Related errors


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