|
1 | 1 | # SPDX-License-Identifier: Apache-2.0 |
2 | 2 | # SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
3 | 3 |
|
| 4 | +from types import SimpleNamespace |
| 5 | + |
4 | 6 | import pytest |
5 | 7 | import ray |
6 | 8 | import torch |
|
13 | 15 | multi_process_parallel, |
14 | 16 | ) |
15 | 17 | from vllm.distributed import get_tp_group |
| 18 | +from vllm.model_executor.layers.fused_moe.experts.trtllm_mxfp4_moe import ( |
| 19 | + TrtLlmMxfp4ExpertsMonolithic, |
| 20 | +) |
16 | 21 | from vllm.model_executor.layers.fused_moe.moe_output import UnfinalizedMoEOutput |
| 22 | +from vllm.model_executor.layers.fused_moe.runner.moe_runner import MoERunner |
17 | 23 | from vllm.model_executor.warmup.cutedsl_warmup import cutedsl_warmup |
| 24 | +from vllm.models.kimi_k3.nvidia import latent_moe_runner |
18 | 25 | from vllm.models.kimi_k3.nvidia.ops.latent_moe_tail import KimiK3LatentMoETailOp |
19 | 26 | from vllm.platforms import current_platform |
20 | 27 |
|
|
24 | 31 | TOP_K = 8 |
25 | 32 |
|
26 | 33 |
|
| 34 | +def test_deferred_finalize_enabled_before_moe_kernel_setup( |
| 35 | + monkeypatch: pytest.MonkeyPatch, |
| 36 | +) -> None: |
| 37 | + class FakeMoEConfig: |
| 38 | + tp_size = 8 |
| 39 | + dp_size = 1 |
| 40 | + ep_size = 1 |
| 41 | + pcp_size = 1 |
| 42 | + is_sequence_parallel = False |
| 43 | + hidden_dim = LATENT_SIZE |
| 44 | + hidden_dim_unpadded = LATENT_SIZE |
| 45 | + experts_per_token = 16 |
| 46 | + defer_moe_finalize = False |
| 47 | + defer_moe_finalize_max_num_tokens = -1 |
| 48 | + |
| 49 | + @property |
| 50 | + def use_deferred_moe_finalize(self) -> bool: |
| 51 | + return self.defer_moe_finalize |
| 52 | + |
| 53 | + moe_config = FakeMoEConfig() |
| 54 | + quant_method = SimpleNamespace( |
| 55 | + experts_cls=TrtLlmMxfp4ExpertsMonolithic, |
| 56 | + moe_kernel=None, |
| 57 | + ) |
| 58 | + norm_weight = torch.empty(LATENT_SIZE, dtype=torch.bfloat16) |
| 59 | + transform = SimpleNamespace( |
| 60 | + norm=SimpleNamespace(weight=norm_weight, variance_epsilon=EPS), |
| 61 | + up_proj=SimpleNamespace( |
| 62 | + weight=SimpleNamespace(shape=(HIDDEN_SIZE, LATENT_SIZE)) |
| 63 | + ), |
| 64 | + ) |
| 65 | + |
| 66 | + def fake_runner_init(runner, *args, **kwargs) -> None: |
| 67 | + runner.moe_config = moe_config |
| 68 | + runner.routed_experts = SimpleNamespace(quant_method=quant_method) |
| 69 | + runner._shared_experts = object() |
| 70 | + runner.routed_output_transform = transform |
| 71 | + |
| 72 | + initialized_with: dict[str, object] = {} |
| 73 | + tail_op = SimpleNamespace(contract=SimpleNamespace(max_num_tokens=128)) |
| 74 | + |
| 75 | + def fake_tail_initialize(**kwargs): |
| 76 | + initialized_with.update(kwargs) |
| 77 | + return tail_op |
| 78 | + |
| 79 | + monkeypatch.setattr(MoERunner, "__init__", fake_runner_init) |
| 80 | + monkeypatch.setattr(latent_moe_runner.torch.cuda, "Event", lambda: object()) |
| 81 | + monkeypatch.setattr( |
| 82 | + latent_moe_runner, |
| 83 | + "current_platform", |
| 84 | + SimpleNamespace( |
| 85 | + is_cuda=lambda: True, |
| 86 | + is_device_capability_family=lambda capability: capability == 100, |
| 87 | + ), |
| 88 | + ) |
| 89 | + monkeypatch.setattr( |
| 90 | + latent_moe_runner, |
| 91 | + "get_current_vllm_config", |
| 92 | + lambda: SimpleNamespace( |
| 93 | + parallel_config=SimpleNamespace(use_ubatching=False), |
| 94 | + model_config=SimpleNamespace(enable_sleep_mode=False), |
| 95 | + ), |
| 96 | + ) |
| 97 | + monkeypatch.setattr(KimiK3LatentMoETailOp, "initialize", fake_tail_initialize) |
| 98 | + |
| 99 | + latent_moe_runner.LatentMoERunner() |
| 100 | + |
| 101 | + assert moe_config.defer_moe_finalize |
| 102 | + assert moe_config.defer_moe_finalize_max_num_tokens == 128 |
| 103 | + assert initialized_with["experts_per_token"] == 16 |
| 104 | + |
| 105 | + |
27 | 106 | def _make_deferred_routed_output( |
28 | 107 | num_tokens: int, |
29 | 108 | device: torch.device, |
|
0 commit comments