sgl-project/sglang · error · TypeError

pair_postprocess must return a torch.Tensor

Error message

pair_postprocess must return a torch.Tensor

What it means

A custom pair_postprocess callable must return a torch.Tensor; returning None, a numpy array, a tuple, or a list triggers this TypeError inside _apply_postprocess during cache refresh.

Source

Thrown at python/sglang/multimodal_gen/runtime/models/schedulers/flow_match_pair.py:519

            )
        else:
            sigma_to = self.timestep_to_sigma(timestep_to)
        prev_sample = sample + model_output * (sigma_to - sigma_from)
        return prev_sample

    def _refresh_pair_cache(self) -> None:
        if self.timesteps is None or self.sigmas is None:
            raise RuntimeError("Scheduler not initialized; call set_timesteps() first")

        def _apply_postprocess(pairs: torch.Tensor, source: str) -> torch.Tensor:
            if self._pair_postprocess_fn is None:
                return pairs
            if self._pair_postprocess_requires_source:
                modified = self._pair_postprocess_fn(pairs, source=source)
            else:
                modified = self._pair_postprocess_fn(pairs)
            if not isinstance(modified, torch.Tensor):
                raise TypeError("pair_postprocess must return a torch.Tensor")
            if modified.shape != pairs.shape:
                raise ValueError("pair_postprocess must return the same shape as input")
            return modified

        base_pairs_timesteps = self._make_pairs_from_vector(self.timesteps)
        base_pairs_sigmas = self._make_pairs_from_vector(self.sigmas)

        self.pair_timesteps = _apply_postprocess(base_pairs_timesteps, "timesteps")
        self.pair_sigmas = _apply_postprocess(base_pairs_sigmas, "sigmas")


EntryClass = FlowMatchPairScheduler

View on GitHub (pinned to 0132848349)

Solutions

  1. Make the callable return a torch.Tensor (convert with torch.from_numpy(...)/torch.as_tensor(...))
  2. Ensure the function has an explicit return of the modified tensor
  3. Check the callable is not writing results to a captured external variable instead of returning

Example fix

# before
def pp(pairs, source):
    pairs = pairs.cpu().numpy() * 2  # returns ndarray
# after
def pp(pairs, source):
    return pairs * 2
Defensive patterns

Strategy: type-guard

Validate before calling

out = fn(pairs)
assert isinstance(out, torch.Tensor), type(out)

Type guard

def returns_tensor(fn, *a, **k) -> bool:
    return isinstance(fn(*a, **k), torch.Tensor)

Prevention

When it happens

Trigger: Registering a hook via set_pair_postprocess that returns e.g. a numpy array, a (tensor, meta) tuple, or mutates in place and implicitly returns None.

Common situations: Adapting diffusers-style callback functions that return numpy; forgetting a return statement in a lambda-style postprocess.

Related errors


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