Skip to content
Draft
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions release_notes/fragments/23139.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
pr: 23139
entries:
- category: bugfix
text: Fixed grouped MoE inference to fall back when expert groups exceed the fused gating operator's capacity, avoiding a CANN tiling error.
44 changes: 44 additions & 0 deletions tests/ut/ops/test_fused_moe.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,50 @@
from vllm_ascend.utils import vllm_version_is


@pytest.mark.parametrize(
("hidden_size", "num_experts", "num_groups", "topk_group", "uses_fused_op"),
[
pytest.param(250, 64, 8, 4, True, id="hidden-size-not-divisible"),
pytest.param(240, 64, 8, 4, True, id="top-k-fits-in-selected-groups"),
pytest.param(1280, 1040, 80, 1, False, id="aligned-experts-exceed-kernel-limit"),
],
)
def test_fused_grouped_topk_checks_expert_groups(
monkeypatch, hidden_size, num_experts, num_groups, topk_group, uses_fused_op
):
hidden_states = torch.zeros(2, hidden_size)
router_logits = torch.arange(num_experts, dtype=torch.float32).repeat(2, 1) / num_experts
weights = torch.ones(2, 8, dtype=torch.float32)
ids = torch.zeros(2, 8, dtype=torch.int32)
gating_op = MagicMock(return_value=(weights, ids, None))
monkeypatch.setattr(fused_topk_router_module.torch_npu, "npu_moe_gating_top_k", gating_op, raising=False)
monkeypatch.setattr(fused_topk_router_module, "get_current_hardware_profile", lambda: None)
router = AscendFusedTopKRouter(
top_k=8,
global_num_experts=num_experts,
use_grouped_topk=True,
num_expert_group=num_groups,
topk_group=topk_group,
)

actual_weights, actual_ids = router._compute_routing(hidden_states, router_logits, torch.int32)

if uses_fused_op:
gating_op.assert_called_once()
assert gating_op.call_args.args[0] is router_logits
assert gating_op.call_args.kwargs["k_group"] == topk_group
assert gating_op.call_args.kwargs["group_count"] == num_groups
assert actual_weights is weights
assert actual_ids is ids
else:
gating_op.assert_not_called()
expected_weights, expected_ids = AscendGroupedTopKRouter._compute_routing(
router, hidden_states, router_logits, torch.int32
)
torch.testing.assert_close(actual_weights, expected_weights)
torch.testing.assert_close(actual_ids, expected_ids)


def test_prepared_linear_input_import_does_not_cycle_through_moe():
"""Both modules must use the leaf contract, even when imports are cached."""
leaf_module = "vllm_ascend.quantization.prepared_linear_input"
Expand Down
32 changes: 17 additions & 15 deletions vllm_ascend/ops/fused_moe/router/fused_topk_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import torch_npu
from vllm.distributed.eplb.eplb_state import EplbLayerState
from vllm.model_executor.models.utils import sequence_parallel_chunk
from vllm.utils.math_utils import round_up

from vllm_ascend.ascend_forward_context import _EXTRA_CTX, MoECommType
from vllm_ascend.device.hardware_profile import HardwareCapability, get_current_hardware_profile
Expand All @@ -28,6 +29,8 @@

DEEPSEEK_V4_IMAGE_SENTINEL_BASE_ID = 129257
DEEPSEEK_V4_IMAGE_SENTINEL_COUNT = 5
MOE_GATING_EXPERT_GROUP_ALIGNMENT = 32
MOE_GATING_MAX_ALIGNED_EXPERTS = 2048


def select_deepseek_v4_vision_experts(
Expand Down Expand Up @@ -150,23 +153,22 @@ def _prepare_routing_input_ids(self, input_ids: torch.Tensor, num_tokens: int) -

def is_fused_supported(
self,
hidden_states: torch.Tensor,
router_logits: torch.Tensor,
) -> bool:
topk_group = self.topk_group if self.topk_group is not None else 1
num_expert_group = self.num_expert_group if self.num_expert_group is not None else 1
if not (
num_expert_group > 0
and hidden_states.shape[-1] % num_expert_group == 0
and hidden_states.shape[-1] // num_expert_group > 2
):
return False
if self.top_k > (hidden_states.shape[-1] / (num_expert_group * topk_group)):
if not 1 <= topk_group <= num_expert_group:
return False
if topk_group * hidden_states.shape[-1] / num_expert_group < self.top_k: # noqa: SIM103
return False
return True
experts_per_group, remainder = divmod(router_logits.shape[-1], num_expert_group)
return (
remainder == 0
and experts_per_group > 2
and 1 <= self.top_k <= experts_per_group * topk_group
and round_up(experts_per_group, MOE_GATING_EXPERT_GROUP_ALIGNMENT) * num_expert_group
<= MOE_GATING_MAX_ALIGNED_EXPERTS
)

def _can_fuse_gating_map(self, hidden_states: torch.Tensor) -> bool:
def _can_fuse_gating_map(self, router_logits: torch.Tensor) -> bool:
profile = get_current_hardware_profile()
return (
self.eplb_state is not None
Expand All @@ -175,7 +177,7 @@ def _can_fuse_gating_map(self, hidden_states: torch.Tensor) -> bool:
and self.num_expert_group in (None, 1)
and self.topk_group in (None, 1)
and (self.bias_vl is None or profile.supports(HardwareCapability.MOE_GATING_TOP_K_HASH_VISION))
and self.is_fused_supported(hidden_states)
and self.is_fused_supported(router_logits)
)

def _get_gating_map_table(self) -> torch.Tensor | None:
Expand All @@ -199,8 +201,8 @@ def _compute_routing(
input_ids: torch.Tensor | None = None,
) -> tuple[torch.Tensor, torch.Tensor]:
if self._use_gating_top_k_with_map is None:
self._use_gating_top_k_with_map = self._can_fuse_gating_map(hidden_states)
if self.bias_vl is None and not self.is_fused_supported(hidden_states):
self._use_gating_top_k_with_map = self._can_fuse_gating_map(router_logits)
if self.bias_vl is None and not self.is_fused_supported(router_logits):
return super()._compute_routing(
hidden_states=hidden_states,
router_logits=router_logits,
Expand Down
Loading