sgl-project/sglang · error · ValueError

Expected x.shape[-1] to be even for split rotary, got {last}

Error message

Expected x.shape[-1] to be even for split rotary, got {last}.

What it means

LTX-2's split rotary embedding halves the feature dimension (rotate_half style), so the last axis must be even. apply_split_rotary_emb checks x.shape[-1] % 2 before reshaping to (..., 2, r) and errors on odd sizes.

Source

Thrown at python/sglang/multimodal_gen/runtime/models/dits/ltx_2.py:347

        and x.is_contiguous()
        and cos.is_cuda
        and sin.is_cuda
    ):
        from sglang.kernels.ops.diffusion import apply_ltx2_split_rotary_emb

        return apply_ltx2_split_rotary_emb(x, cos, sin)

    x_dtype = x.dtype
    needs_reshape = False
    if x.ndim != 4 and cos.ndim == 4:
        b = x.shape[0]
        _, h, t, _ = cos.shape
        x = x.reshape(b, t, h, -1).swapaxes(1, 2)
        needs_reshape = True

    last = x.shape[-1]
    if last % 2 != 0:
        raise ValueError(
            f"Expected x.shape[-1] to be even for split rotary, got {last}."
        )
    r = last // 2

    split_x = x.reshape(*x.shape[:-1], 2, r)
    first_x = split_x[..., :1, :]
    second_x = split_x[..., 1:, :]

    cos_u = cos.unsqueeze(-2)
    sin_u = sin.unsqueeze(-2)

    out = split_x * cos_u
    first_out = out[..., :1, :]
    second_out = out[..., 1:, :]
    first_out.addcmul_(-sin_u, second_x)
    second_out.addcmul_(sin_u, first_x)

    out = out.reshape(*out.shape[:-2], last)

View on GitHub (pinned to 0132848349)

Solutions

  1. Use an even head_dim / last-dim size in the model config
  2. Fix upstream slicing so the tensor reaching this function keeps its full even width
  3. Route tensors with odd dims (if intentional) through a padding step or the interleaved rope path

Example fix

# before
q = q[..., :67]               # odd trailing dim
q = apply_split_rotary_emb(q, cos, sin)

# after
q = q[..., :68]               # even head_dim
q = apply_split_rotary_emb(q, cos, sin)
Defensive patterns

Strategy: validation

Validate before calling

assert x.shape[-1] % 2 == 0, f'split rotary needs even last dim, got {x.shape[-1]}'

Type guard

def has_even_last_dim(t: torch.Tensor) -> bool:\n    return t.shape[-1] % 2 == 0

Prevention

When it happens

Trigger: Calling apply_split_rotary_emb with a tensor whose last dim is odd, e.g. head_dim of 67, or a partially sliced/projected q/k tensor with an off-by-one cut.

Common situations: Custom head dims in config; slicing q/k after projection with wrong end index; feeding interleaved-layout tensors into the split-rotary path.

Related errors


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