Skip to content
Open
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 4 additions & 8 deletions vllm_fl/dispatch/backends/vendor/gcu/gcu.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,11 @@ def rotary_embedding(
)

def attention_backend(self, use_mla: bool = False, use_sparse: bool = False) -> str:
from vllm.v1.attention.backends.registry import AttentionBackendEnum

if use_mla:
if use_sparse:
raise NotImplementedError("GCU does not support sparse attention yet")
raise NotImplementedError("GCU does not support MLA yet")

import flash_attn.vllm_flash_attn

sys.modules["vllm.vllm_flash_attn"] = flash_attn.vllm_flash_attn

return AttentionBackendEnum.FLASH_ATTN.get_path()
# GCU uses a standalone flash_attn backend (AttentionGCUBackend) that calls
# Enflame's native flash_attn_varlen_func directly, without depending on
# vllm upstream FlashAttentionBackend / FlashAttentionImpl.
return "vllm_fl.dispatch.backends.vendor.gcu.impl.attention.AttentionGCUBackend"
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# Copyright (c) 2026 BAAI. All rights reserved.

"""GCU fix for ``apply_moe_activation``.

The upstream ``apply_moe_activation`` directly calls ``torch.ops._C.*`` custom
ops (e.g. ``silu_and_mul``, ``gelu_and_mul``) that are CUDA-only. On GCU
these ops are not available.

The vllm_fl variant (:func:`vllm_fl.ops.fused_moe.activation.apply_moe_activation`)
uses ``CachedOp("silu_and_mul")`` / ``CachedOp("gelu_and_mul")``, which
dispatches through the FlagOS system and selects a GCU-compatible
implementation.
"""

from __future__ import annotations

import logging

import torch

logger = logging.getLogger(__name__)

_patched = False


def apply_moe_activation_gcu(
activation,
output: torch.Tensor,
input: torch.Tensor,
) -> torch.Tensor:
"""GCU-compatible ``apply_moe_activation`` via the FlagOS dispatch system."""
from vllm_fl.ops.fused_moe.activation import apply_moe_activation as _fl_impl

return _fl_impl(activation, output, input)


def apply_moe_activation_gcu_patch() -> None:
"""Patch ``apply_moe_activation`` for GCU devices."""
global _patched
if _patched:
return

gcu = getattr(torch, "gcu", None)
if gcu is None or not gcu.is_available():
return

# Modules that hold a reference to apply_moe_activation via
# ``from ...activation import apply_moe_activation``.
_IMPORTERS: list[str] = [
"vllm.model_executor.layers.fused_moe.activation",
"vllm.model_executor.layers.fused_moe.modular_kernel",
"vllm.model_executor.layers.fused_moe.fused_moe",
"vllm.model_executor.layers.fused_moe.experts.cutlass_moe",
"vllm.model_executor.layers.fused_moe.fused_marlin_moe",
"vllm.model_executor.layers.quantization.gguf",
]

try:
for module_name in _IMPORTERS:
try:
mod = __import__(module_name, fromlist=["apply_moe_activation"])
except ImportError:
continue
if hasattr(mod, "apply_moe_activation"):
mod.apply_moe_activation = apply_moe_activation_gcu

_patched = True
logger.info(
"Patched apply_moe_activation for GCU (using FlagOS dispatch)"
)
except Exception as exc:
logger.warning(
"Failed to patch apply_moe_activation for GCU: %s",
exc,
)
Loading
Loading