Skip to content

vllm.ir.ops

Modules:

Functions:

fused_add_rms_norm(x, x_residual, weight, epsilon, variance_size=None)

Fused add and weighted root-mean-square layer normalization.

Source code in vllm/ir/ops/layernorm.py
@register_op(allow_inplace=True)
def fused_add_rms_norm(
    x: Tensor,
    x_residual: Tensor,
    weight: Tensor | None,
    epsilon: float,
    variance_size: int | None = None,
) -> tuple[Tensor, Tensor]:
    """Fused add and weighted root-mean-square layer normalization."""
    orig_dtype = x.dtype
    x = x.to(torch.float32)
    x = x + x_residual.to(torch.float32)
    x_residual = x.to(orig_dtype)

    x_var = x if variance_size is None else x[..., :variance_size]
    variance = x_var.pow(2).mean(dim=-1, keepdim=True)
    x = x * torch.rsqrt(variance + epsilon)
    if weight is not None:
        x = x.to(weight.dtype) * weight
    return x.to(orig_dtype), x_residual

gelu_and_mul_sparse(x, std_multiplier, approximate='none')

Apply Gaussian sparsification, GELU, and gated multiplication.

Source code in vllm/ir/ops/activation.py
@register_op
def gelu_and_mul_sparse(
    x: Tensor, std_multiplier: float, approximate: str = "none"
) -> Tensor:
    """Apply Gaussian sparsification, GELU, and gated multiplication."""
    d = x.shape[-1] // 2
    gate = x[..., :d]
    # Statistics intentionally remain local to each tensor-parallel shard.
    mean = torch.mean(gate, dim=-1, keepdim=True)
    std = torch.std(gate, dim=-1, keepdim=True, unbiased=False)
    sparse_gate = F.relu(gate - (mean + std * std_multiplier))
    return F.gelu(sparse_gate, approximate=approximate) * x[..., d:]

rms_norm(x, weight, epsilon, variance_size=None)

Weighted root-mean-square layer normalization.

Source code in vllm/ir/ops/layernorm.py
@register_op
def rms_norm(
    x: Tensor, weight: Tensor | None, epsilon: float, variance_size: int | None = None
) -> Tensor:
    """Weighted root-mean-square layer normalization."""
    orig_dtype = x.dtype
    x = x.to(torch.float32)
    x_var = x if variance_size is None else x[..., :variance_size]
    variance = x_var.pow(2).mean(dim=-1, keepdim=True)
    x = x * torch.rsqrt(variance + epsilon)
    if weight is not None:
        x = x.to(weight.dtype) * weight
    return x.to(orig_dtype)

rotary_embedding_query_only(positions, query, head_size, cos_sin_cache, is_neox)

RoPE over query only, for layers that reuse another layer's KV cache.

Source code in vllm/ir/ops/rotary_embedding.py
@register_op(activations=["query"], allow_inplace=True)
def rotary_embedding_query_only(
    positions: Tensor,
    query: Tensor,
    head_size: int,
    cos_sin_cache: Tensor,
    is_neox: bool,
) -> Tensor:
    """RoPE over query only, for layers that reuse another layer's KV cache."""
    cos, sin = _lookup_cos_sin(positions, query, cos_sin_cache)
    return _apply_rope(query, cos, sin, head_size, is_neox)