|
| 1 | +import os |
| 2 | +from unittest.mock import patch |
| 3 | + |
| 4 | +import pytest |
| 5 | + |
| 6 | +from checkpoint_engine.ps import ( |
| 7 | + _get_my_rdma_device, |
| 8 | + _get_rdma_devices, |
| 9 | + _ibv_get_device_list, |
| 10 | + _parse_NCCL_IB_HCA, |
| 11 | +) |
| 12 | + |
| 13 | + |
| 14 | +@pytest.fixture |
| 15 | +def mock_available_devices() -> list[str]: |
| 16 | + """Provide mock available device list""" |
| 17 | + return ["mlx5_0", "mlx5_1", "mlx4_0", "mlx4_1"] |
| 18 | + |
| 19 | + |
| 20 | +def test_detect_ibv_list(): |
| 21 | + """Test detection of _ibv_get_device_list function""" |
| 22 | + # Skip this test if no real infiniband devices exist |
| 23 | + if not os.path.exists("/sys/class/infiniband"): |
| 24 | + pytest.skip("No infiniband devices found on system") |
| 25 | + |
| 26 | + real_ibv_list = sorted(os.listdir("/sys/class/infiniband")) |
| 27 | + if real_ibv_list: |
| 28 | + devices = _ibv_get_device_list() |
| 29 | + assert isinstance(devices, list) |
| 30 | + |
| 31 | + |
| 32 | +def test_parse_max_hcas_limit(): |
| 33 | + """Test maximum HCA quantity limit""" |
| 34 | + # Create mock data with more than 32 devices |
| 35 | + many_devices = [f"device_{i}" for i in range(50)] |
| 36 | + result = _parse_NCCL_IB_HCA("", many_devices) |
| 37 | + assert len(result) == 32 |
| 38 | + assert result == many_devices[:32] |
| 39 | + |
| 40 | + |
| 41 | +def test_get_rdma_devices_no_env_vars(mock_available_devices: list[str]): |
| 42 | + """Test _get_rdma_devices with no environment variables""" |
| 43 | + with ( |
| 44 | + patch.dict(os.environ, clear=True), |
| 45 | + patch("checkpoint_engine.ps._ibv_get_device_list", return_value=mock_available_devices), |
| 46 | + ): |
| 47 | + devices = _get_rdma_devices() |
| 48 | + assert sorted(devices) == sorted(mock_available_devices) |
| 49 | + |
| 50 | + |
| 51 | +@pytest.mark.parametrize( |
| 52 | + "input_value,expected", |
| 53 | + [ |
| 54 | + pytest.param("", ["mlx5_0", "mlx5_1", "mlx4_0", "mlx4_1"], id="empty string"), |
| 55 | + pytest.param(" \t\n ", ["mlx5_0", "mlx5_1", "mlx4_0", "mlx4_1"], id="whitespace"), |
| 56 | + pytest.param("None", [], id="None string"), |
| 57 | + pytest.param("^", ["mlx5_0", "mlx5_1", "mlx4_0", "mlx4_1"], id="caret"), |
| 58 | + pytest.param("^=", ["mlx5_0", "mlx5_1", "mlx4_0", "mlx4_1"], id="caret-equals"), |
| 59 | + pytest.param("=^", [], id="equals-caret"), |
| 60 | + pytest.param("^^", ["mlx5_0", "mlx5_1", "mlx4_0", "mlx4_1"], id="double-caret"), |
| 61 | + pytest.param("=", [], id="equals"), |
| 62 | + pytest.param("==", [], id="double-equals"), |
| 63 | + ], |
| 64 | +) |
| 65 | +def test_parse_basic_cases( |
| 66 | + input_value: str, expected: list[str], mock_available_devices: list[str] |
| 67 | +): |
| 68 | + """Test basic parsing cases: empty string, whitespace, None""" |
| 69 | + result = _parse_NCCL_IB_HCA(input_value, mock_available_devices) |
| 70 | + assert result == expected |
| 71 | + |
| 72 | + |
| 73 | +@pytest.mark.parametrize( |
| 74 | + "input_value,expected", |
| 75 | + [ |
| 76 | + # prefix |
| 77 | + ("mlx5_0", ["mlx5_0"]), |
| 78 | + ("mlx5", ["mlx5_0", "mlx5_1"]), |
| 79 | + # exact match |
| 80 | + ("=mlx5_0", ["mlx5_0"]), |
| 81 | + ("=mlx5_0,mlx5_1", ["mlx5_0", "mlx5_1"]), |
| 82 | + # ignore ports, whitespace and duplicated commas |
| 83 | + ("mlx5_0:1,mlx5_1:2", ["mlx5_0", "mlx5_1"]), |
| 84 | + ("mlx5_0:1,mlx5_1", ["mlx5_0", "mlx5_1"]), |
| 85 | + (" mlx5_0 , mlx5_1 ", ["mlx5_0", "mlx5_1"]), |
| 86 | + ("mlx5_0,,mlx5_1", ["mlx5_0", "mlx5_1"]), |
| 87 | + # exclusion |
| 88 | + ("^mlx5_0", ["mlx5_1", "mlx4_0", "mlx4_1"]), |
| 89 | + ("^mlx5_0,mlx5_1", ["mlx4_0", "mlx4_1"]), |
| 90 | + ("^mlx5", ["mlx4_0", "mlx4_1"]), |
| 91 | + ("^=mlx5_0,mlx5_1", ["mlx4_0", "mlx4_1"]), |
| 92 | + ("^=mlx4", ["mlx5_0", "mlx5_1", "mlx4_0", "mlx4_1"]), |
| 93 | + ], |
| 94 | +) |
| 95 | +def test_parse_various_patterns( |
| 96 | + input_value: str, expected: list[str], mock_available_devices: list[str] |
| 97 | +): |
| 98 | + """Test various parsing patterns""" |
| 99 | + result = _parse_NCCL_IB_HCA(input_value, mock_available_devices) |
| 100 | + assert result == expected |
| 101 | + |
| 102 | + |
| 103 | +@pytest.mark.parametrize( |
| 104 | + "input_value,expected_result,expected_warning", |
| 105 | + [ |
| 106 | + ("=mlx5_100", [], "No RDMA device match device_name='mlx5_100' where is_exact_match=True."), |
| 107 | + ("mlx5_100", [], "No RDMA device match device_name='mlx5_100' where is_exact_match=False."), |
| 108 | + ( |
| 109 | + "^mlx5_100", |
| 110 | + ["mlx5_0", "mlx5_1", "mlx4_0", "mlx4_1"], |
| 111 | + "No RDMA device match device_name='mlx5_100' where is_exact_match=False.", |
| 112 | + ), |
| 113 | + ("mlx6", [], "No RDMA device match device_name='mlx6' where is_exact_match=False."), |
| 114 | + ("=mlx6", [], "No RDMA device match device_name='mlx6' where is_exact_match=True."), |
| 115 | + ], |
| 116 | +) |
| 117 | +def test_parse_exact_match_with_nonexistent_device( |
| 118 | + input_value: str, |
| 119 | + expected_result: list[str], |
| 120 | + expected_warning: str, |
| 121 | + mock_available_devices: list[str], |
| 122 | +): |
| 123 | + """Test exact matching with non-existent device""" |
| 124 | + with patch("checkpoint_engine.ps.logger") as mock_logger: |
| 125 | + result = _parse_NCCL_IB_HCA(input_value, mock_available_devices) |
| 126 | + assert result == expected_result |
| 127 | + mock_logger.warning.assert_called_once_with(expected_warning) |
| 128 | + |
| 129 | + |
| 130 | +@pytest.mark.parametrize( |
| 131 | + "env_var_name,env_var_value,expected_devices", |
| 132 | + [ |
| 133 | + ("PS_P2P_STORE_RDMA_DEVICES", "mlx5_0,mlx5_1", ["mlx5_0", "mlx5_1"]), |
| 134 | + ("NCCL_IB_HCA", "mlx5", ["mlx5_0", "mlx5_1"]), |
| 135 | + ("NCCL_IB_HCA", "mlx5_0,mlx5_1", ["mlx5_0", "mlx5_1"]), |
| 136 | + ("NCCL_IB_HCA", "^mlx5_0", ["mlx5_1", "mlx4_0", "mlx4_1"]), |
| 137 | + ("NCCL_IB_HCA", "mlx6", ["mlx5_0", "mlx5_1", "mlx4_0", "mlx4_1"]), |
| 138 | + ("NCCL_IB_HCA", "", ["mlx5_0", "mlx5_1", "mlx4_0", "mlx4_1"]), |
| 139 | + ], |
| 140 | +) |
| 141 | +def test_get_rdma_devices_with_env_vars( |
| 142 | + env_var_name: str, |
| 143 | + env_var_value: str, |
| 144 | + expected_devices: list[str], |
| 145 | + mock_available_devices: list[str], |
| 146 | +): |
| 147 | + """Test _get_rdma_devices with various environment variables""" |
| 148 | + env_dict = {env_var_name: env_var_value} |
| 149 | + with ( |
| 150 | + patch.dict(os.environ, env_dict), |
| 151 | + patch("checkpoint_engine.ps._ibv_get_device_list", return_value=mock_available_devices), |
| 152 | + ): |
| 153 | + devices = _get_rdma_devices() |
| 154 | + assert sorted(devices) == sorted(expected_devices) |
| 155 | + |
| 156 | + |
| 157 | +@pytest.mark.parametrize( |
| 158 | + "local_rank,gpu_count,expected_device", |
| 159 | + [ |
| 160 | + (0, 4, "mlx5_0"), |
| 161 | + (3, 4, "mlx5_3"), |
| 162 | + (4, 8, "mlx5_2"), |
| 163 | + (7, 8, "mlx5_3"), |
| 164 | + ], |
| 165 | +) |
| 166 | +def test_get_my_rdma_device_basic(local_rank: int, gpu_count: int, expected_device: str): |
| 167 | + """Test _get_my_rdma_device with basic allocation""" |
| 168 | + # Use fewer devices to match the GPU count constraint |
| 169 | + devices = ["mlx5_0", "mlx5_1", "mlx5_2", "mlx5_3"] |
| 170 | + device = _get_my_rdma_device(local_rank, gpu_count, devices) |
| 171 | + assert device == expected_device |
| 172 | + |
| 173 | + |
| 174 | +@pytest.mark.parametrize( |
| 175 | + "local_rank,gpu_count,devices,error", |
| 176 | + [ |
| 177 | + ( |
| 178 | + 0, |
| 179 | + 4, |
| 180 | + ["mlx5_0", "mlx5_1", "mlx5_2", "mlx5_3", "mlx5_4"], |
| 181 | + AssertionError, |
| 182 | + ), # Too many devices |
| 183 | + ( |
| 184 | + 0, |
| 185 | + 8, |
| 186 | + ["mlx5_0", "mlx5_1", "mlx5_2"], |
| 187 | + AssertionError, |
| 188 | + ), # GPU count not divisible by device count |
| 189 | + (0, 8, [], RuntimeError), # No devices |
| 190 | + ], |
| 191 | +) |
| 192 | +def test_get_my_rdma_device_invalid_config( |
| 193 | + local_rank: int, gpu_count: int, devices: list[str], error: type |
| 194 | +): |
| 195 | + """Test _get_my_rdma_device with invalid configuration""" |
| 196 | + with pytest.raises(error): |
| 197 | + _get_my_rdma_device(local_rank, gpu_count, devices) |
0 commit comments