|
| 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 pytest |
| 19 | +import torch |
| 20 | +from pytest_mock import MockerFixture |
| 21 | + |
| 22 | +from tests.ut.base import PytestBase |
| 23 | +from vllm_ascend.ops.moe.comm_utils import ( |
| 24 | + _gather_along_first_dim, async_all_to_all, |
| 25 | + gather_from_sequence_parallel_region) |
| 26 | + |
| 27 | + |
| 28 | +class TestDistributedCommunication(PytestBase): |
| 29 | + |
| 30 | + @pytest.fixture(autouse=True) |
| 31 | + def context(self, mocker: MockerFixture): |
| 32 | + mocker.patch("torch.npu.current_device", return_value="cpu") |
| 33 | + mocker.patch("torch.distributed.get_world_size", return_value=4) |
| 34 | + |
| 35 | + mocker.patch("torch.distributed.get_rank", return_value=0) |
| 36 | + |
| 37 | + @pytest.mark.parametrize( |
| 38 | + "input_tensor, output_split_sizes, input_split_sizes", |
| 39 | + [(torch.randn(8, 16), [2, 2, 2, 2], [2, 2, 2, 2]), |
| 40 | + (torch.randn(16, 32), None, None)]) |
| 41 | + def test_async_all_to_all(self, input_tensor, output_split_sizes, |
| 42 | + input_split_sizes, mocker: MockerFixture): |
| 43 | + """Test async_all_to_all""" |
| 44 | + mock_group = mocker.MagicMock() |
| 45 | + mocker.patch("torch.distributed.all_to_all_single", |
| 46 | + return_value=mocker.MagicMock()) |
| 47 | + |
| 48 | + _, a2a_out, handle = async_all_to_all(input_tensor, output_split_sizes, |
| 49 | + input_split_sizes, mock_group) |
| 50 | + |
| 51 | + # Check if the output tensor is created properly |
| 52 | + if output_split_sizes is None: |
| 53 | + assert a2a_out.shape == input_tensor.shape |
| 54 | + else: |
| 55 | + total_output_size = sum(output_split_sizes) |
| 56 | + expected_shape = [total_output_size] + list( |
| 57 | + input_tensor.size())[1:] |
| 58 | + assert a2a_out.shape == torch.Size(expected_shape) |
| 59 | + |
| 60 | + # Ensure handle is returned from async operation |
| 61 | + assert handle is not None |
| 62 | + assert isinstance(handle, mocker.MagicMock) |
| 63 | + |
| 64 | + @pytest.mark.parametrize("world_size, test_tensor, expected", |
| 65 | + [(1, torch.randn(8, 16), (8, 16)), |
| 66 | + (4, torch.randn(8, 16), (32, 16))]) |
| 67 | + def test_gather_along_first_dim(self, test_tensor, expected, world_size, |
| 68 | + mocker: MockerFixture): |
| 69 | + """Test _gather_along_first_dim""" |
| 70 | + mocker.patch("torch.distributed.get_world_size", |
| 71 | + return_value=world_size) |
| 72 | + |
| 73 | + result = _gather_along_first_dim(test_tensor, mocker.MagicMock()) |
| 74 | + |
| 75 | + assert result.shape == expected |
| 76 | + |
| 77 | + @pytest.mark.parametrize("input_tensor, output_split_sizes", |
| 78 | + [(torch.randn(8, 16), None), |
| 79 | + (torch.randn(8, 16), [2, 2, 2, 2])]) |
| 80 | + def test_gather_from_sequence_parallel_region(self, input_tensor, |
| 81 | + output_split_sizes, |
| 82 | + mocker: MockerFixture): |
| 83 | + """Test gather_from_sequence_parallel_region""" |
| 84 | + mock_group = mocker.MagicMock() |
| 85 | + |
| 86 | + result = gather_from_sequence_parallel_region(input_tensor, mock_group, |
| 87 | + output_split_sizes) |
| 88 | + |
| 89 | + # If output_split_sizes is not provided, result should have expanded first dimension by world size |
| 90 | + if output_split_sizes is None: |
| 91 | + expected_shape = [input_tensor.shape[0] * 4] + list( |
| 92 | + input_tensor.shape[1:]) |
| 93 | + assert result.shape == torch.Size(expected_shape) |
| 94 | + else: |
| 95 | + # If output_split_sizes is provided, result shape is dictated by sum of output_split_sizes |
| 96 | + expected_shape = [sum(output_split_sizes)] + list( |
| 97 | + input_tensor.shape[1:]) |
| 98 | + assert result.shape == torch.Size(expected_shape) |
0 commit comments