|
| 1 | +# |
| 2 | +# Copyright (c) 2025 Huawei Technologies Co., Ltd. All Rights Reserved. |
| 3 | +# Copyright 2023 The vLLM team. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | +# This file is a part of the vllm-ascend project. |
| 17 | + |
| 18 | +import importlib |
| 19 | +import unittest |
| 20 | +from unittest.mock import MagicMock, patch |
| 21 | + |
| 22 | +import pytest |
| 23 | +import torch |
| 24 | + |
| 25 | +from vllm_ascend.distributed.tensor_parallel import ( |
| 26 | + _gather_along_first_dim, _gather_along_last_dim, |
| 27 | + _reduce_scatter_along_first_dim, _reduce_scatter_along_last_dim, |
| 28 | + all_to_all_hp2sp, all_to_all_sp2hp) |
| 29 | + |
| 30 | + |
| 31 | +@pytest.fixture |
| 32 | +def test_tensor(): |
| 33 | + return torch.randn(8, 16) |
| 34 | + |
| 35 | + |
| 36 | +@pytest.fixture |
| 37 | +def test_tensor_last_dim(): |
| 38 | + return torch.randn(8, 16, 32) |
| 39 | + |
| 40 | + |
| 41 | +@pytest.fixture |
| 42 | +def mock_group(): |
| 43 | + return MagicMock() |
| 44 | + |
| 45 | + |
| 46 | +@pytest.fixture(autouse=True) |
| 47 | +def mock_dist(): |
| 48 | + with patch("torch.distributed") as mock: |
| 49 | + mock.get_world_size.return_value = 4 |
| 50 | + mock.get_rank.return_value = 0 |
| 51 | + yield mock |
| 52 | + |
| 53 | + |
| 54 | +class TestDistributedCommunication(unittest.TestCase): |
| 55 | + |
| 56 | + @pytest.mark.parametrize("world_size", [1, 4]) |
| 57 | + def test_gather_along_first_dim(self, test_tensor, mock_group, mock_dist, |
| 58 | + world_size): |
| 59 | + """test _gather_along_first_dim""" |
| 60 | + mock_dist.get_world_size.return_value = world_size |
| 61 | + |
| 62 | + result = _gather_along_first_dim(test_tensor, mock_group) |
| 63 | + |
| 64 | + if world_size == 1: |
| 65 | + self.assertEqual(result.shape, (8, 16)) |
| 66 | + else: |
| 67 | + self.assertEqual(result.shape, (32, 16)) # 8*4=32 |
| 68 | + |
| 69 | + def test_gather_along_first_dim_unequal_split(self, test_tensor, |
| 70 | + mock_group): |
| 71 | + """test unequal split""" |
| 72 | + output_split_sizes = [5, 10, 15, 2] |
| 73 | + result = _gather_along_first_dim(test_tensor, mock_group, |
| 74 | + output_split_sizes) |
| 75 | + self.assertEqual(result.shape, (32, 16)) # 5+10+15+2=32 |
| 76 | + |
| 77 | + @pytest.mark.parametrize("world_size", [1, 4]) |
| 78 | + def test_gather_along_last_dim(self, test_tensor_last_dim, mock_group, |
| 79 | + mock_dist, world_size): |
| 80 | + """test _gather_along_last_dim""" |
| 81 | + mock_dist.get_world_size.return_value = world_size |
| 82 | + |
| 83 | + result = _gather_along_last_dim(test_tensor_last_dim, mock_group) |
| 84 | + |
| 85 | + self.assertEqual(result.shape, (8, 16, 32 * world_size)) |
| 86 | + |
| 87 | + @pytest.mark.parametrize("input_shape,expected_shape", [ |
| 88 | + ((32, 16), (8, 16)), |
| 89 | + ((40, 10), (10, 10)), |
| 90 | + ]) |
| 91 | + def test_reduce_scatter_along_first_dim(self, mock_group, input_shape, |
| 92 | + expected_shape): |
| 93 | + input_tensor = torch.randn(*input_shape) |
| 94 | + result = _reduce_scatter_along_first_dim(input_tensor, mock_group) |
| 95 | + self.assertEqual(result.shape, expected_shape) |
| 96 | + |
| 97 | + def test_reduce_scatter_along_last_dim(self, mock_group): |
| 98 | + input_tensor = torch.randn(8, 16, 32) |
| 99 | + result = _reduce_scatter_along_last_dim(input_tensor, mock_group) |
| 100 | + self.assertEqual(result.shape, (8, 16, 8)) |
| 101 | + |
| 102 | + @pytest.mark.parametrize("func,input_shape,expected_shape", [ |
| 103 | + ("all_gather_last_dim_from_tensor_parallel_region", (8, 16, 32), |
| 104 | + (8, 16, 128)), |
| 105 | + ("reduce_scatter_to_sequence_parallel_region", (32, 16), (8, 16)), |
| 106 | + ("reduce_scatter_last_dim_to_tensor_parallel_region", (8, 16, 32), |
| 107 | + (8, 16, 8)), |
| 108 | + ("gather_from_sequence_parallel_region", (8, 16), (32, 16)), |
| 109 | + ]) |
| 110 | + def test_wrapper_functions(self, mock_group, func, input_shape, |
| 111 | + expected_shape): |
| 112 | + """test wrapper funcs""" |
| 113 | + mod = importlib.import_module( |
| 114 | + 'vllm_ascend.distributed.tensor_parallel') |
| 115 | + globals = mod.__dict__ |
| 116 | + test_func = globals[func] |
| 117 | + input_tensor = torch.randn(*input_shape) |
| 118 | + result = test_func(input_tensor, mock_group) |
| 119 | + self.assertEqual(result.shape, expected_shape) |
| 120 | + |
| 121 | + @pytest.mark.parametrize( |
| 122 | + "input_shape,output_shape", |
| 123 | + [ |
| 124 | + ((8, 16), (32, 4)), # [num_tokens/TP, H] -> [num_tokens, H/TP] |
| 125 | + ]) |
| 126 | + def test_all_to_all_sp2hp(self, mock_group, input_shape, output_shape): |
| 127 | + input_tensor = torch.randn(*input_shape) |
| 128 | + result = all_to_all_sp2hp(input_tensor, mock_group) |
| 129 | + self.assertEqual(result.shape, output_shape) |
| 130 | + |
| 131 | + @pytest.mark.parametrize( |
| 132 | + "input_shape,output_shape", |
| 133 | + [ |
| 134 | + ((32, 4), (8, 16)), # [num_tokens, H/TP] -> [num_tokens/TP, H] |
| 135 | + ]) |
| 136 | + def test_all_to_all_hp2sp(self, mock_group, input_shape, output_shape): |
| 137 | + input_tensor = torch.randn(*input_shape) |
| 138 | + result = all_to_all_hp2sp(input_tensor, mock_group) |
| 139 | + self.assertEqual(result.shape, output_shape) |
0 commit comments