|
13 | 13 | # This file is a part of the vllm-ascend project.
|
14 | 14 | #
|
15 | 15 |
|
| 16 | +from unittest.mock import MagicMock, patch |
| 17 | + |
| 18 | +import torch |
| 19 | +from vllm.distributed.parallel_state import GroupCoordinator |
| 20 | + |
16 | 21 | from tests.ut.base import TestBase
|
| 22 | +from vllm_ascend.patch.worker.patch_common.patch_distributed import \ |
| 23 | + GroupCoordinatorPatch |
17 | 24 |
|
18 | 25 |
|
19 | 26 | class TestPatchDistributed(TestBase):
|
20 | 27 |
|
21 |
| - def test_GroupCoordinator_patched(self): |
22 |
| - from vllm.distributed.parallel_state import GroupCoordinator |
| 28 | + def setUp(self): |
| 29 | + self.mock_group_ranks = [[0, 1]] |
| 30 | + self.mock_local_rank = 0 |
| 31 | + self.mock_backend = "hccl" |
| 32 | + self.mock_use_device_comm = True |
| 33 | + |
| 34 | + patcher_get_rank = patch("torch.distributed.get_rank", return_value=0) |
| 35 | + patcher_new_group = patch("torch.distributed.new_group", |
| 36 | + return_value=MagicMock()) |
| 37 | + patcher_is_cuda_alike = patch( |
| 38 | + "vllm.platforms.current_platform.is_cuda_alike", return_value=True) |
| 39 | + patcher_device_comm_cls = patch( |
| 40 | + "vllm.distributed.parallel_state.resolve_obj_by_qualname", |
| 41 | + return_value=MagicMock()) |
| 42 | + |
| 43 | + self.mock_get_rank = patcher_get_rank.start() |
| 44 | + self.mock_new_group = patcher_new_group.start() |
| 45 | + self.mock_is_cuda_alike = patcher_is_cuda_alike.start() |
| 46 | + self.mock_resolve_obj = patcher_device_comm_cls.start() |
23 | 47 |
|
24 |
| - from vllm_ascend.patch.worker.patch_common.patch_distributed import \ |
25 |
| - GroupCoordinatorPatch |
| 48 | + self.addCleanup(patcher_get_rank.stop) |
| 49 | + self.addCleanup(patcher_new_group.stop) |
| 50 | + self.addCleanup(patcher_is_cuda_alike.stop) |
| 51 | + self.addCleanup(patcher_device_comm_cls.stop) |
26 | 52 |
|
| 53 | + self.group_coordinator = GroupCoordinatorPatch( |
| 54 | + group_ranks=self.mock_group_ranks, |
| 55 | + local_rank=self.mock_local_rank, |
| 56 | + torch_distributed_backend=self.mock_backend, |
| 57 | + use_device_communicator=self.mock_use_device_comm) |
| 58 | + |
| 59 | + def test_GroupCoordinator_patched(self): |
27 | 60 | self.assertIs(GroupCoordinator, GroupCoordinatorPatch)
|
| 61 | + |
| 62 | + def test_all_to_all_returns_input_when_world_size_1(self): |
| 63 | + self.group_coordinator.world_size = 1 |
| 64 | + input_tensor = torch.randn(2, 3) |
| 65 | + output = self.group_coordinator.all_to_all(input_tensor) |
| 66 | + self.assertTrue(torch.equal(output, input_tensor)) |
| 67 | + |
| 68 | + def test_all_to_all_raises_assertion_on_invalid_scatter_dim(self): |
| 69 | + input_tensor = torch.randn(2, 3) |
| 70 | + with self.assertRaises(AssertionError) as cm: |
| 71 | + self.group_coordinator.all_to_all(input_tensor, scatter_dim=2) |
| 72 | + self.assertIn("Invalid scatter dim", str(cm.exception)) |
| 73 | + |
| 74 | + def test_all_to_all_raises_assertion_on_invalid_gather_dim(self): |
| 75 | + input_tensor = torch.randn(2, 3) |
| 76 | + with self.assertRaises(AssertionError) as cm: |
| 77 | + self.group_coordinator.all_to_all(input_tensor, gather_dim=2) |
| 78 | + self.assertIn("Invalid gather dim", str(cm.exception)) |
| 79 | + |
| 80 | + def test_all_to_all_calls_device_communicator_with_correct_args(self): |
| 81 | + mock_communicator = MagicMock() |
| 82 | + self.group_coordinator.device_communicator = mock_communicator |
| 83 | + |
| 84 | + input_tensor = torch.randn(2, 3) |
| 85 | + scatter_dim = 0 |
| 86 | + gather_dim = 1 |
| 87 | + scatter_sizes = [1, 1] |
| 88 | + gather_sizes = [1, 1] |
| 89 | + |
| 90 | + self.group_coordinator.all_to_all(input_tensor, |
| 91 | + scatter_dim=scatter_dim, |
| 92 | + gather_dim=gather_dim, |
| 93 | + scatter_sizes=scatter_sizes, |
| 94 | + gather_sizes=gather_sizes) |
| 95 | + |
| 96 | + mock_communicator.all_to_all.assert_called_once_with( |
| 97 | + input_tensor, scatter_dim, gather_dim, scatter_sizes, gather_sizes) |
| 98 | + |
| 99 | + def test_all_to_all_calls_device_communicator_without_sizes(self): |
| 100 | + mock_communicator = MagicMock() |
| 101 | + self.group_coordinator.device_communicator = mock_communicator |
| 102 | + |
| 103 | + input_tensor = torch.randn(2, 3) |
| 104 | + scatter_dim = 0 |
| 105 | + gather_dim = 1 |
| 106 | + |
| 107 | + self.group_coordinator.all_to_all(input_tensor, |
| 108 | + scatter_dim=scatter_dim, |
| 109 | + gather_dim=gather_dim) |
| 110 | + |
| 111 | + mock_communicator.all_to_all.assert_called_once_with( |
| 112 | + input_tensor, scatter_dim, gather_dim, None, None) |
0 commit comments