init v0.23.0

Signed-off-by: Sun Ruoxi <sunruoxi@4paradigm.com>
This commit is contained in:
2026-08-27 15:11:51 +08:00
parent b582a8e7d1
commit 7f8a1b1f7a
2849 changed files with 712887 additions and 22001 deletions

View File

View File

@@ -0,0 +1,85 @@
#
# Copyright (c) 2026 Huawei Technologies Co., Ltd. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from unittest.mock import patch
import pytest
import torch
from vllm_ascend._310p.fused_moe.experts_selector import select_experts
class TestExpertsSelector310:
@pytest.mark.parametrize("global_num_experts", [256, 128])
def test_select_experts(self, global_num_experts):
hidden_states = torch.randn(8, 16)
router_logits = torch.randn(8, 8)
with patch("torch_npu.npu_moe_gating_top_k_softmax") as mock_npu:
mock_npu.return_value = (
torch.randn(8, 2),
torch.randint(0, 8, (8, 2), dtype=torch.int32),
None,
)
topk_weights, topk_ids = select_experts(
hidden_states=hidden_states,
router_logits=router_logits,
top_k=2,
use_grouped_topk=False,
renormalize=True,
topk_group=None,
num_expert_group=None,
custom_routing_function=None,
scoring_func="softmax",
e_score_correction_bias=None,
global_num_experts=global_num_experts,
)
mock_npu.assert_called_once()
assert topk_weights.shape == (8, 2)
assert topk_ids.shape == (8, 2)
def test_select_experts_chunks_large_token_batch(self):
num_tokens = 2050
hidden_states = torch.randn(num_tokens, 16)
router_logits = torch.randn(num_tokens, 8)
def mock_gating(logits, k):
return (
torch.ones(logits.shape[0], k),
torch.zeros(logits.shape[0], k, dtype=torch.int32),
None,
)
with patch(
"torch_npu.npu_moe_gating_top_k_softmax",
side_effect=mock_gating,
) as mock_npu:
topk_weights, topk_ids = select_experts(
hidden_states=hidden_states,
router_logits=router_logits,
top_k=2,
use_grouped_topk=False,
renormalize=True,
custom_routing_function=None,
scoring_func="softmax",
)
assert [call.args[0].shape[0] for call in mock_npu.call_args_list] == [1024, 1024, 2]
assert topk_weights.shape == (num_tokens, 2)
assert topk_ids.shape == (num_tokens, 2)
assert torch.all(topk_weights == 0.5)

View File

@@ -0,0 +1,179 @@
#
# Copyright (c) 2026 Huawei Technologies Co., Ltd. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from unittest.mock import MagicMock, call, patch
import torch
from tests.ut.base import TestBase
from vllm_ascend._310p.fused_moe.moe_comm_method import AllGatherCommImpl310
from vllm_ascend._310p.fused_moe.moe_mlp import unified_apply_mlp
from vllm_ascend.ops.fused_moe.moe_runtime_args import (
MoEMlpComputeInput,
MoEQuantParams,
MoEWeights,
)
from vllm_ascend.quantization.quant_type import QuantType
def build_mlp_compute_input_fixture(
*,
hidden_states: torch.Tensor,
w1: torch.Tensor,
w2: torch.Tensor,
group_list: torch.Tensor,
with_quant: bool,
w1_scale: torch.Tensor | None = None,
w2_scale: torch.Tensor | None = None,
group_list_type: int = 1,
) -> MoEMlpComputeInput:
return MoEMlpComputeInput(
hidden_states=hidden_states,
group_list=group_list,
group_list_type=group_list_type,
dynamic_scale=None,
topk_scales=None,
weights=MoEWeights(w1=w1, w2=w2, w1_scale=w1_scale, w2_scale=w2_scale),
quant=MoEQuantParams(quant_type=QuantType.W8A8 if with_quant else QuantType.NONE),
fusion=False,
activation="silu",
need_trans=False,
dynamic_eplb=False,
)
class TestUnifiedApplyMLP310(TestBase):
@patch("vllm_ascend._310p.fused_moe.moe_comm_method.unified_apply_mlp")
def test_all_gather_apply_mlp_returns_common_tuple_contract(self, mock_unified_apply_mlp):
mlp_compute_input = MagicMock(spec=MoEMlpComputeInput)
mlp_output = torch.randn(10, 20, dtype=torch.float16)
mock_unified_apply_mlp.return_value = mlp_output
comm_impl = AllGatherCommImpl310.__new__(AllGatherCommImpl310)
output, before_gmm2_evt = comm_impl._apply_mlp(mlp_compute_input)
self.assertIs(output, mlp_output)
self.assertIsNone(before_gmm2_evt)
mock_unified_apply_mlp.assert_called_once_with(mlp_compute_input=mlp_compute_input)
@patch("torch_npu.npu_grouped_matmul", create=True)
@patch("torch_npu.npu_swiglu")
def test_unified_apply_mlp_without_quantization_310(self, mock_npu_swiglu, mock_npu_grouped_matmul):
mock_gmm1_out = torch.randn(10, 40, dtype=torch.float16)
mock_gmm2_out = torch.randn(10, 20, dtype=torch.float16)
mock_npu_grouped_matmul.side_effect = [[mock_gmm1_out], [mock_gmm2_out]]
mock_npu_swiglu_output = torch.randn(10, 40, dtype=torch.float16)
mock_npu_swiglu.return_value = mock_npu_swiglu_output
hidden_states = torch.randn(10, 20, dtype=torch.float16)
w1 = torch.randn(5, 20, 40, dtype=torch.float16)
w2 = torch.randn(5, 40, 20, dtype=torch.float16)
group_list = torch.tensor([2, 4, 6, 8, 10], dtype=torch.int64)
result = unified_apply_mlp(
mlp_compute_input=build_mlp_compute_input_fixture(
hidden_states=hidden_states,
w1=w1,
w2=w2,
group_list=group_list,
with_quant=False,
)
)
self.assertEqual(mock_npu_grouped_matmul.call_count, 2)
mock_npu_grouped_matmul.assert_has_calls(
[
call(
x=[hidden_states], weight=[w1], split_item=2, group_list_type=1, group_type=0, group_list=group_list
),
call(
x=[mock_npu_swiglu_output],
weight=[w2],
split_item=2,
group_list_type=1,
group_type=0,
group_list=group_list,
),
],
any_order=True,
)
mock_npu_swiglu.assert_called_once()
mock_npu_swiglu.assert_called_with(mock_gmm1_out)
self.assertEqual(result.shape, hidden_states.shape)
self.assertEqual(result.dtype, torch.float16)
@patch("torch.cumsum")
@patch("torch_npu.npu_quant_grouped_matmul_dequant", create=True)
@patch("torch_npu.npu_swiglu")
def test_unified_apply_mlp_with_quantization_310(
self, mock_npu_swiglu, mock_npu_quant_grouped_matmul_dequant, mock_cumsum
):
mock_cumsum_out = torch.arange(0, 10, dtype=torch.int64)
mock_cumsum.return_value = mock_cumsum_out
mock_gmm1_out = torch.randn(10, 40, dtype=torch.float16)
mock_gmm2_out = torch.randn(10, 20, dtype=torch.float16)
mock_npu_quant_grouped_matmul_dequant.side_effect = [mock_gmm1_out, mock_gmm2_out]
mock_npu_swiglu_output = torch.randn(10, 40, dtype=torch.float16)
mock_npu_swiglu.return_value = mock_npu_swiglu_output
hidden_states = torch.randn(10, 20, dtype=torch.float16)
w1 = torch.randn(5, 20, 40, dtype=torch.float16)
w1_scale = torch.rand(5, 40, dtype=torch.float32)
w2 = torch.randn(5, 40, 20, dtype=torch.float16)
w2_scale = torch.rand(5, 40, dtype=torch.float32)
group_list = torch.tensor([2, 4, 6, 8, 10], dtype=torch.int64)
result = unified_apply_mlp(
mlp_compute_input=build_mlp_compute_input_fixture(
hidden_states=hidden_states,
w1=w1,
w2=w2,
group_list=group_list,
with_quant=True,
w1_scale=w1_scale,
w2_scale=w2_scale,
)
)
mock_cumsum.assert_called_once()
self.assertEqual(mock_npu_quant_grouped_matmul_dequant.call_count, 2)
mock_npu_quant_grouped_matmul_dequant.assert_has_calls(
[
call(
x=hidden_states,
quantized_weight=w1,
weight_scale=w1_scale,
group_list=mock_cumsum_out,
quant_mode="pertoken",
),
call(
x=mock_npu_swiglu_output,
quantized_weight=w2,
weight_scale=w2_scale,
group_list=mock_cumsum_out,
quant_mode="pertoken",
),
],
any_order=True,
)
mock_npu_swiglu.assert_called_once()
mock_npu_swiglu.assert_called_with(mock_gmm1_out)
self.assertEqual(result.shape, hidden_states.shape)
self.assertEqual(result.dtype, torch.float16)

View File

@@ -0,0 +1,109 @@
#
# Copyright (c) 2026 Huawei Technologies Co., Ltd. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from unittest.mock import patch
import torch
import torch.nn.functional as F
from vllm_ascend._310p.fused_moe.fused_moe import (
AscendFusedMoE310,
)
class _DummyGate(torch.nn.Module):
def forward(self, hidden_states: torch.Tensor):
# Keep gate output deterministic: sigmoid(0)=0.5.
return torch.zeros(
hidden_states.shape[0],
1,
dtype=hidden_states.dtype,
device=hidden_states.device,
), None
class _DummySharedExperts(torch.nn.Module):
def __init__(self, with_gate: bool):
super().__init__()
self.expert_gate = _DummyGate() if with_gate else None
def forward(self, hidden_states: torch.Tensor):
out = hidden_states * 2.0 + 1.0
if self.expert_gate is not None:
gate_out, _ = self.expert_gate(hidden_states)
out = F.sigmoid(gate_out) * out
return out
def _build_layer(shared_experts: torch.nn.Module | None) -> AscendFusedMoE310:
layer = AscendFusedMoE310.__new__(AscendFusedMoE310)
# The test bypasses full layer init with __new__, so we must initialize
# nn.Module internals before assigning child modules.
torch.nn.Module.__init__(layer)
layer._shared_experts = shared_experts
return layer
def test_forward_shared_experts_without_gate_310():
layer = _build_layer(_DummySharedExperts(with_gate=False))
hidden_states = torch.randn(4, 8)
output = layer._forward_shared_experts(hidden_states)
expected = hidden_states * 2.0 + 1.0
torch.testing.assert_close(output, expected)
def test_forward_shared_experts_with_gate_310():
layer = _build_layer(_DummySharedExperts(with_gate=True))
hidden_states = torch.randn(4, 8)
output = layer._forward_shared_experts(hidden_states)
expected = 0.5 * (hidden_states * 2.0 + 1.0)
torch.testing.assert_close(output, expected)
def test_forward_impl_with_shared_experts_returns_tuple_310():
layer = _build_layer(_DummySharedExperts(with_gate=True))
hidden_states = torch.randn(3, 8)
router_logits = torch.randn(3, 8)
routed_out = torch.randn(3, 8)
with patch.object(AscendFusedMoE310, "forward_impl", return_value=routed_out):
shared_out, routed = layer.shared_forward_impl(hidden_states, router_logits)
expected_shared = 0.5 * (hidden_states * 2.0 + 1.0)
torch.testing.assert_close(shared_out, expected_shared)
torch.testing.assert_close(routed, routed_out)
def test_forward_impl_without_shared_experts_integration_310():
layer = _build_layer(None)
hidden_states = torch.randn(3, 8)
assert layer._forward_shared_experts(hidden_states) is None
def test_forward_impl_without_shared_experts_returns_routed_only_310():
layer = _build_layer(None)
hidden_states = torch.randn(3, 8)
router_logits = torch.randn(3, 8)
routed_out = torch.randn(3, 8)
with patch.object(AscendFusedMoE310, "forward_impl", return_value=routed_out):
output = layer.shared_forward_impl(hidden_states, router_logits)
torch.testing.assert_close(output, routed_out)
def test_is_internal_router_is_false_310():
layer = _build_layer(_DummySharedExperts(with_gate=True))
assert layer.is_internal_router is False