|
| 1 | +import torch |
| 2 | +import unittest |
| 3 | +from vllm_ascend.eplb.core.eplb_utils import determine_default_expert_map |
| 4 | + |
| 5 | + |
| 6 | +class TestDetermineDefaultExpertMap(unittest.TestCase): |
| 7 | + |
| 8 | + def test_world_size_1(self): |
| 9 | + global_expert_num = 8 |
| 10 | + world_size = 1 |
| 11 | + global_redundant_expert_num = 0 |
| 12 | + |
| 13 | + expected_counts = [8] |
| 14 | + expected_maps = [[0, 1, 2, 3, 4, 5, 6, 7]] |
| 15 | + |
| 16 | + local_count, expert_map = determine_default_expert_map( |
| 17 | + global_expert_num, world_size, 0, global_redundant_expert_num) |
| 18 | + |
| 19 | + self.assertEqual(local_count, expected_counts[0]) |
| 20 | + |
| 21 | + expected_tensor = torch.tensor(expected_maps[0], dtype=torch.int32) |
| 22 | + self.assertTrue(torch.all(expert_map == expected_tensor).item()) |
| 23 | + |
| 24 | + def test_equal_distribution(self): |
| 25 | + global_expert_num = 6 |
| 26 | + world_size = 3 |
| 27 | + global_redundant_expert_num = 0 |
| 28 | + |
| 29 | + expected_counts = [2, 2, 2] |
| 30 | + expected_maps = [ |
| 31 | + [0, 1, -1, -1, -1, -1], # rank 0 |
| 32 | + [-1, -1, 0, 1, -1, -1], # rank 1 |
| 33 | + [-1, -1, -1, -1, 0, 1] # rank 2 |
| 34 | + ] |
| 35 | + |
| 36 | + for rank_id in range(world_size): |
| 37 | + local_count, expert_map = determine_default_expert_map( |
| 38 | + global_expert_num, world_size, rank_id, |
| 39 | + global_redundant_expert_num) |
| 40 | + |
| 41 | + self.assertEqual( |
| 42 | + local_count, |
| 43 | + expected_counts[rank_id], |
| 44 | + ) |
| 45 | + |
| 46 | + expected_tensor = torch.tensor(expected_maps[rank_id], |
| 47 | + dtype=torch.int32) |
| 48 | + self.assertTrue(torch.all(expert_map == expected_tensor).item()) |
| 49 | + |
| 50 | + def test_unequal_distribution(self): |
| 51 | + global_expert_num = 10 |
| 52 | + world_size = 3 |
| 53 | + global_redundant_expert_num = 0 |
| 54 | + |
| 55 | + expected_counts = [3, 3, 4] |
| 56 | + expected_maps = [ |
| 57 | + [0, 1, 2, -1, -1, -1, -1, -1, -1, -1], # rank 0 |
| 58 | + [-1, -1, -1, 0, 1, 2, -1, -1, -1, -1], # rank 1 |
| 59 | + [-1, -1, -1, -1, -1, -1, 0, 1, 2, 3] # rank 2 |
| 60 | + ] |
| 61 | + |
| 62 | + for rank_id in range(world_size): |
| 63 | + local_count, expert_map = determine_default_expert_map( |
| 64 | + global_expert_num, world_size, rank_id, |
| 65 | + global_redundant_expert_num) |
| 66 | + |
| 67 | + self.assertEqual(local_count, expected_counts[rank_id]) |
| 68 | + |
| 69 | + expected_tensor = torch.tensor(expected_maps[rank_id], |
| 70 | + dtype=torch.int32) |
| 71 | + self.assertTrue(torch.all(expert_map == expected_tensor).item()) |
| 72 | + |
| 73 | + def test_with_redundancy(self): |
| 74 | + global_expert_num = 7 |
| 75 | + world_size = 3 |
| 76 | + global_redundant_expert_num = 2 |
| 77 | + |
| 78 | + expected_counts = [3, 3, 3] |
| 79 | + expected_maps = [ |
| 80 | + [0, 1, 2, -1, -1, -1, -1], # rank 0 |
| 81 | + [-1, -1, 0, 1, 2, -1, -1], # rank 1 |
| 82 | + [-1, -1, -1, -1, 0, 1, 2] # rank 2 |
| 83 | + ] |
| 84 | + |
| 85 | + for rank_id in range(world_size): |
| 86 | + local_count, expert_map = determine_default_expert_map( |
| 87 | + global_expert_num, world_size, rank_id, |
| 88 | + global_redundant_expert_num) |
| 89 | + |
| 90 | + self.assertEqual(local_count, expected_counts[rank_id]) |
| 91 | + |
| 92 | + expected_tensor = torch.tensor(expected_maps[rank_id], |
| 93 | + dtype=torch.int32) |
| 94 | + self.assertTrue(torch.all(expert_map == expected_tensor).item()) |
| 95 | + |
| 96 | + def test_redundancy_at_boundary(self): |
| 97 | + global_expert_num = 5 |
| 98 | + world_size = 2 |
| 99 | + global_redundant_expert_num = 1 |
| 100 | + |
| 101 | + expected_counts = [3, 3] |
| 102 | + expected_maps = [[0, 1, 2, -1, -1], [-1, -1, 0, 1, 2]] |
| 103 | + |
| 104 | + for rank_id in range(world_size): |
| 105 | + local_count, expert_map = determine_default_expert_map( |
| 106 | + global_expert_num, world_size, rank_id, |
| 107 | + global_redundant_expert_num) |
| 108 | + |
| 109 | + self.assertEqual(local_count, expected_counts[rank_id]) |
| 110 | + |
| 111 | + expected_tensor = torch.tensor(expected_maps[rank_id], |
| 112 | + dtype=torch.int32) |
| 113 | + self.assertTrue(torch.all(expert_map == expected_tensor).item()) |
0 commit comments