sgl-project/sglang · error · ValueError

Sequence length {seq_length} exceeds the maximum {max_positi

Error message

Sequence length {seq_length} exceeds the maximum {max_positions}.

What it means

CLIP checks the requested sequence length against the position embedding table size (max_position_embeddings). Sequences longer than the learned position table cannot be embedded and are rejected.

Source

Thrown at python/sglang/srt/models/clip.py:127

            persistent=False,
        )

    def forward(
        self,
        input_ids: Optional[torch.LongTensor] = None,
        position_ids: Optional[torch.LongTensor] = None,
        inputs_embeds: Optional[torch.FloatTensor] = None,
    ) -> torch.Tensor:
        if input_ids is not None:
            seq_length = input_ids.shape[-1]
        elif inputs_embeds is not None:
            seq_length = inputs_embeds.shape[-2]
        else:
            raise ValueError("Either input_ids or inputs_embeds must be provided.")

        max_positions = self.position_embedding.weight.shape[0]
        if seq_length > max_positions:
            raise ValueError(
                f"Sequence length {seq_length} exceeds the maximum {max_positions}."
            )

        if position_ids is None:
            position_ids = self.position_ids[:, :seq_length]

        if inputs_embeds is None:
            inputs_embeds = self.token_embedding(input_ids)

        position_embeddings = self.position_embedding(position_ids)
        embeddings = inputs_embeds + position_embeddings

        return embeddings


class CLIPMLP(nn.Module):

    def __init__(

View on GitHub (pinned to 0132848349)

Solutions

  1. Truncate/chunk the input so seq_length <= max_positions
  2. Use a model with a larger max_position_embeddings (e.g. Long-CLIP style checkpoint) for long inputs

Example fix

# before
emb.forward(input_ids=ids)  # len(ids)=120, max=77
# after
emb.forward(input_ids=ids[:77])
Defensive patterns

Strategy: validation

Validate before calling

assert seq_len <= model.position_embedding.weight.shape[0], "seq exceeds CLIP max positions"

Prevention

When it happens

Trigger: Feeding images/text whose token count exceeds config.max_position_embeddings of the CLIP model (e.g. 77 for CLIP text encoders).

Common situations: Long prompts or high-resolution patches producing more tokens than the encoder supports; chunking/preprocessing omitted.

Related errors


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