1
+ import pytest
2
+ import numpy as np
3
+ from unittest .mock import patch
4
+ from vllm_ascend .eplb .core .policy .policy_dynamic_ep import DynamicEplb
5
+
6
+ class TestDynamicEplb :
7
+
8
+ def test_add_redundant_basic (self ):
9
+ current_expert_table = np .array ([[[0 , 1 ], [1 , 0 ]]])
10
+ expert_workload = np .array ([[[2 , 3 ], [4 , 1 ]]])
11
+ num_original_expert = 2
12
+ result = DynamicEplb .add_redundant (current_expert_table , expert_workload , num_original_expert )
13
+ expected = np .array ([[2 + 1 , 3 + 4 ]])
14
+ assert np .array_equal (result , expected )
15
+
16
+ def test_get_redundant_num (self ):
17
+ counts = np .array ([2 , 1 , 3 ])
18
+ assert DynamicEplb .get_redundant_num (3 , counts ) == 3
19
+
20
+ def test_calculate_max_heat_per_layer (self ):
21
+ workload_table = np .array ([[[1 ,2 ],[3 ,4 ]], [[2 ,2 ],[1 ,1 ]]])
22
+ max_heat = DynamicEplb .calculate_max_heat_per_layer (workload_table , 2 )
23
+ assert max_heat == [7 , 4 ]
24
+
25
+ def test_constraint_expert_local_exchange (self ):
26
+ current = [[[0 ,1 ],[2 ,3 ]]]
27
+ global_dep = [[[1 ,0 ],[3 ,2 ]]]
28
+ new_dep = DynamicEplb .constraint_expert_local_exchange (current , global_dep )
29
+ assert new_dep == [[[0 ,1 ],[2 ,3 ]]]
30
+
31
+ def test_compute_balanced_pack_redundancy_normal (self ):
32
+ origin_weights = [(0 , 10 ), (1 , 20 )]
33
+ result , boxes = DynamicEplb .compute_balanced_pack_redundancy (origin_weights , 2 , 1 )
34
+ assert isinstance (result , list ) and len (result ) == 2
35
+
36
+ def test_compute_balanced_pack_redundancy_card0 (self ):
37
+ origin_weights = [(0 , 10 )]
38
+ with pytest .raises (RuntimeError ):
39
+ DynamicEplb .compute_balanced_pack_redundancy (origin_weights , 0 , 0 )
40
+
41
+ def test_compute_balanced_pack_normal (self ):
42
+ origin_weights = np .array ([(0 , 10 ), (1 , 20 )], dtype = object )
43
+ result , boxes = DynamicEplb .compute_balanced_pack (origin_weights , 2 )
44
+ assert isinstance (result , list ) and len (result ) == 2
45
+
46
+ def test_compute_balanced_pack_card0 (self ):
47
+ origin_weights = np .array ([(0 , 10 )], dtype = object )
48
+ with pytest .raises (RuntimeError ):
49
+ DynamicEplb .compute_balanced_pack (origin_weights , 0 )
50
+
51
+ def test_original_compute_balanced_pack_redundancy (self ):
52
+ origin_weights = [(0 , 5 ), (1 , 10 )]
53
+ result , boxes = DynamicEplb .original_compute_balanced_pack_redundancy (origin_weights , 2 , 1 )
54
+ assert isinstance (result , list ) and len (result ) == 2
55
+
56
+ def test_rebalance_experts_normal (self ):
57
+ expert_table = np .array ([[[0 ,1 ],[1 ,0 ]]])
58
+ workload = np .array ([[[2 ,3 ],[4 ,1 ]]])
59
+ policy = DynamicEplb (config = None )
60
+ change , priority , new_dep = policy .rebalance_experts (expert_table , workload )
61
+ assert change in [0 ,1 ]
62
+ assert isinstance (priority , np .ndarray )
63
+ assert isinstance (new_dep , list )
64
+ assert np .array (new_dep ).shape == expert_table .shape
65
+
66
+ def test_rebalance_experts_exceptions (self ):
67
+ policy = DynamicEplb (config = None )
68
+
69
+ # case1: num_original_expert != expert_num
70
+ expert_table = np .array ([[[0 ,1 ],[1 ,0 ]]])
71
+ workload = np .array ([[[2 ,3 ],[4 ,1 ]]])
72
+ with patch .object (DynamicEplb , 'add_redundant' , return_value = np .array ([[1 ,2 ,3 ]])):
73
+ with pytest .raises (ValueError ):
74
+ policy .rebalance_experts (expert_table , workload )
75
+
76
+ # case2: num_npus <= 0
77
+ expert_table_zero = np .array ([[]]) # 1 layer, 0 NPU, 0 experts
78
+ workload_zero = np .array ([[]])
79
+ with pytest .raises (ValueError ):
80
+ policy .rebalance_experts (expert_table_zero , workload_zero )
81
+
82
+ # case3: num_npus < num_redundancy_expert
83
+ expert_table_small = np .array ([[[0 ,0 ]]]) # 1 layer, 1 NPU, 2 experts
84
+ workload_small = np .array ([[[1 ,1 ]]])
85
+ with patch .object (DynamicEplb , 'get_redundant_num' , return_value = 2 ):
86
+ with pytest .raises (ValueError ):
87
+ policy .rebalance_experts (expert_table_small , workload_small )
0 commit comments