|
| 1 | +# Copyright (c) 2026 BAAI. All rights reserved. |
| 2 | + |
| 3 | +from unittest.mock import MagicMock, patch |
| 4 | + |
| 5 | +from vllm_fl.ops.fused_moe import layer |
| 6 | + |
| 7 | + |
| 8 | +def _runner_with_quant_method(quant_method): |
| 9 | + runner = MagicMock() |
| 10 | + runner._quant_method = quant_method |
| 11 | + runner.moe_config = MagicMock() |
| 12 | + return runner |
| 13 | + |
| 14 | + |
| 15 | +def test_fused_moe_fl_replaces_unquantized_method(): |
| 16 | + quant_method = MagicMock(spec=layer.UnquantizedFusedMoEMethod) |
| 17 | + runner = _runner_with_quant_method(quant_method) |
| 18 | + replacement = MagicMock() |
| 19 | + |
| 20 | + with ( |
| 21 | + patch.object(layer, "_OrigFusedMoE", return_value=runner), |
| 22 | + patch.object( |
| 23 | + layer, |
| 24 | + "UnquantizedFusedMoEMethodFL", |
| 25 | + return_value=replacement, |
| 26 | + ) as replacement_cls, |
| 27 | + patch.object(layer, "replace_router_with_fl") as replace_router, |
| 28 | + ): |
| 29 | + result = layer.FusedMoEFL(test_arg=True) |
| 30 | + |
| 31 | + assert result is runner |
| 32 | + replacement_cls.assert_called_once_with(runner.moe_config) |
| 33 | + runner._replace_quant_method.assert_called_once_with(replacement) |
| 34 | + replace_router.assert_called_once_with() |
| 35 | + |
| 36 | + |
| 37 | +def test_fused_moe_fl_preserves_quantized_method(): |
| 38 | + quant_method = object() |
| 39 | + runner = _runner_with_quant_method(quant_method) |
| 40 | + |
| 41 | + with ( |
| 42 | + patch.object(layer, "_OrigFusedMoE", return_value=runner), |
| 43 | + patch.object(layer, "UnquantizedFusedMoEMethodFL") as replacement_cls, |
| 44 | + patch.object(layer, "replace_router_with_fl") as replace_router, |
| 45 | + patch.object(layer.logger, "info_once") as info_once, |
| 46 | + ): |
| 47 | + result = layer.FusedMoEFL() |
| 48 | + |
| 49 | + assert result is runner |
| 50 | + replacement_cls.assert_not_called() |
| 51 | + runner._replace_quant_method.assert_not_called() |
| 52 | + replace_router.assert_called_once_with() |
| 53 | + info_once.assert_called_once_with( |
| 54 | + "Preserving upstream quantized MoE method %s in FusedMoEFL.", |
| 55 | + "object", |
| 56 | + ) |
0 commit comments