sgl-project/sglang · error · ValueError

SinusoidsPositionEmbedding needs even channels input

Error message

SinusoidsPositionEmbedding needs even channels input

What it means

SinusoidsPositionEmbedding builds sin/cos pairs, so the channel count must be even; odd channels make channels//2 arithmetic invalid.

Source

Thrown at python/sglang/srt/models/qwen3_omni_moe.py:169

        hidden_states, _ = self.fc2(hidden_states)
        hidden_states = residual + hidden_states

        if hidden_states.dtype == torch.float16:
            clamp_value = torch.finfo(hidden_states.dtype).max - 1000
            hidden_states = torch.clamp(
                hidden_states, min=-clamp_value, max=clamp_value
            )

        outputs = (hidden_states,)

        return outputs


class SinusoidsPositionEmbedding(nn.Module):
    def __init__(self, length, channels, max_timescale=10000):
        super().__init__()
        if channels % 2 != 0:
            raise ValueError("SinusoidsPositionEmbedding needs even channels input")
        log_timescale_increment = np.log(max_timescale) / (channels // 2 - 1)
        inv_timescales = torch.exp(
            -log_timescale_increment * torch.arange(channels // 2).float()
        )
        scaled_time = (
            torch.arange(length)[:, np.newaxis] * inv_timescales[np.newaxis, :]
        )
        self.register_buffer(
            "positional_embedding",
            torch.cat([torch.sin(scaled_time), torch.cos(scaled_time)], dim=1),
            persistent=False,
        )

    def forward(self, seqlen: int):
        return self.positional_embedding[:seqlen, :]


def _get_feat_extract_output_lengths(input_lengths):

View on GitHub (pinned to 0132848349)

Solutions

  1. Correct config.json so the corresponding hidden size is even
  2. Use the official Qwen3-Omni config values

Example fix

# before
SinusoidsPositionEmbedding(length=1500, channels=767)
# after
SinusoidsPositionEmbedding(length=1500, channels=768)
Defensive patterns

Strategy: validation

Validate before calling

assert channels % 2 == 0, f"channels must be even, got {channels}"

Type guard

def is_even(n: int) -> bool: return n % 2 == 0

Prevention

When it happens

Trigger: Constructing Qwen3-Omni MoE (audio encoder) with a config whose embedding channels value (e.g. audio hidden size) is odd.

Common situations: Custom or fine-tuned Qwen3-Omni config with an odd audio/vision embedding dimension.

Related errors


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