Skip to content

vllm.compilation.passes.fusion.matcher_utils

Classes:

Functions:

MatcherCustomOp

Bases: ABC

Methods:

  • inputs –

    Utility for inputs to the pattern.

Source code in vllm/compilation/passes/fusion/matcher_utils.py
class MatcherCustomOp(ABC):
    def __init__(self, enabled: bool) -> None:
        config = get_current_vllm_config()
        self.model_dtype = config.model_config.dtype if config.model_config else None
        self.device = config.device_config.device if config.device_config else None

        self.enabled = enabled
        self.forward = self.forward_custom if enabled else self.forward_native

    @abstractmethod
    def forward_custom(self, *args: Any, **kwargs: Any) -> Any:
        pass

    @abstractmethod
    def forward_native(self, *args: Any, **kwargs: Any) -> Any:
        pass

    def __call__(self, *args: Any, **kwargs: Any) -> Any:
        return self.forward(*args, **kwargs)

    def empty(self, *args: Any, **kwargs: Any) -> torch.Tensor:
        return torch.empty(*args, dtype=self.model_dtype, device=self.device, **kwargs)

    def empty_int64(self, *args: Any, **kwargs: Any) -> torch.Tensor:
        return torch.empty(*args, dtype=torch.int64, device=self.device, **kwargs)

    def empty_f32(self, *args: Any, **kwargs: Any) -> torch.Tensor:
        return torch.empty(*args, dtype=torch.float32, device=self.device, **kwargs)

    def inputs(self) -> list[torch.Tensor]:
        """Utility for inputs to the pattern."""
        raise NotImplementedError

inputs()

Utility for inputs to the pattern.

Source code in vllm/compilation/passes/fusion/matcher_utils.py
def inputs(self) -> list[torch.Tensor]:
    """Utility for inputs to the pattern."""
    raise NotImplementedError

MatcherRMSNormGated

Bases: MatcherCustomOp

Matches RMSNormGated with norm_before_gate=True and group_size=None.

Source code in vllm/compilation/passes/fusion/matcher_utils.py
class MatcherRMSNormGated(MatcherCustomOp):
    """Matches RMSNormGated with norm_before_gate=True and group_size=None."""

    def __init__(
        self,
        epsilon: float,
        enabled: bool | None = None,
        norm_before_gate: bool = True,
        group_size: int | None = None,
        activation: str = "swish",
    ) -> None:
        if enabled is None:
            enabled = RMSNormGated.enabled()

        super().__init__(enabled)
        self.epsilon = epsilon
        self.norm_before_gate = norm_before_gate
        self.group_size = group_size
        self.activation = activation

    def inputs(self) -> list[torch.Tensor]:
        x = self.empty(5, 16)
        z = self.empty(5, 16)
        weight = self.empty(16)
        return [x, z, weight]

    def forward_custom(
        self,
        x: torch.Tensor,
        z: torch.Tensor,
        weight: torch.Tensor,
    ) -> torch.Tensor:
        return torch.ops.vllm.fla_rms_norm_gated(
            x,
            z,
            weight,
            self.epsilon,
            self.group_size,
            self.norm_before_gate,
            self.activation,
        )

    def forward_native(
        self,
        x: torch.Tensor,
        z: torch.Tensor,
        weight: torch.Tensor,
    ) -> torch.Tensor:
        return RMSNormGated.forward_static(
            x,
            z,
            weight,
            self.epsilon,
            self.model_dtype,
            group_size=self.group_size,
            norm_before_gate=self.norm_before_gate,
        )

MatcherRotaryEmbedding

Bases: MatcherCustomOp

Matches base RoPE as RotaryEmbedding emits it: the flashinfer custom op, the in-place _C op on XPU with the custom op enabled, else the IR node.

Source code in vllm/compilation/passes/fusion/matcher_utils.py
class MatcherRotaryEmbedding(MatcherCustomOp):
    """Matches base RoPE as RotaryEmbedding emits it: the flashinfer custom op,
    the in-place `_C` op on XPU with the custom op enabled, else the IR node."""

    def __init__(
        self,
        is_neox: bool,
        head_size: int,
        num_heads: int,
        num_kv_heads: int,
        use_flashinfer: bool = False,
    ) -> None:
        super().__init__(enabled=use_flashinfer or rope_custom_op_is_c_kernel())
        self.rotary_op = FLASHINFER_ROTARY_OP if use_flashinfer else ROTARY_OP
        self.is_neox = is_neox
        self.head_size = head_size
        self.num_heads = num_heads
        self.num_kv_heads = num_kv_heads
        self.q_size = self.num_heads * self.head_size
        self.kv_size = self.num_kv_heads * self.head_size

    def inputs(self) -> list[torch.Tensor]:
        positions = self.empty_int64(5)
        query = self.empty(5, self.q_size)
        key = self.empty(5, self.kv_size)
        cos_sin_cache = self.empty(4096, self.head_size)
        return [positions, query, key, cos_sin_cache]

    def forward_custom(
        self,
        positions: torch.Tensor,
        query: torch.Tensor,
        key: torch.Tensor | None,
        cos_sin_cache: torch.Tensor,
    ) -> tuple[torch.Tensor, torch.Tensor | None]:
        result = auto_functionalized(
            self.rotary_op,
            positions=positions,
            query=query,
            key=key,
            head_size=self.head_size,
            cos_sin_cache=cos_sin_cache,
            is_neox=self.is_neox,
        )
        query_out = result[1]
        key_out = result[2] if len(result) > 2 else None
        return query_out, key_out

    def forward_native(
        self,
        positions: torch.Tensor,
        query: torch.Tensor,
        key: torch.Tensor,
        cos_sin_cache: torch.Tensor,
    ) -> tuple[torch.Tensor, torch.Tensor]:
        return ir.ops.rotary_embedding(
            positions, query, key, self.head_size, cos_sin_cache, self.is_neox
        )

aiter_fused_rope_supported(match, head_size)

Whether AITER's fused_qk_rope_reshape_and_cache supports the match: it needs full rotary and a power-of-2 head size.

Source code in vllm/compilation/passes/fusion/matcher_utils.py
def aiter_fused_rope_supported(match: Match, head_size: int) -> bool:
    """Whether AITER's fused_qk_rope_reshape_and_cache supports the match: it
    needs full rotary and a power-of-2 head size."""
    cache = match.kwargs["cos_sin_cache"].meta["val"]
    return cache.shape[-1] == head_size and next_power_of_2(head_size) == head_size

rope_custom_op_is_c_kernel()

XPU's RotaryEmbedding.forward_xpu calls the in-place _C kernel directly instead of the vLLM IR op, so its graphs contain _C.rotary_embedding.

Source code in vllm/compilation/passes/fusion/matcher_utils.py
def rope_custom_op_is_c_kernel() -> bool:
    """XPU's RotaryEmbedding.forward_xpu calls the in-place `_C` kernel directly
    instead of the vLLM IR op, so its graphs contain `_C.rotary_embedding`."""
    return current_platform.is_xpu() and RotaryEmbedding.enabled()