[PD] Fix IB device validation for JSON mappings (#26114)
This commit is contained in:
@@ -1,3 +1,6 @@
|
||||
import json
|
||||
import os
|
||||
import tempfile
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
|
||||
@@ -34,5 +37,63 @@ class TestServerArgsCPUBackend(unittest.TestCase):
|
||||
self.assertEqual(server_args.sampling_backend, "pytorch")
|
||||
|
||||
|
||||
class TestServerArgsIBDeviceValidation(unittest.TestCase):
|
||||
def _validate_ib_devices(self, device_str, available_devices=None):
|
||||
server_args = ServerArgs.__new__(ServerArgs)
|
||||
available_devices = available_devices or [
|
||||
"mlx5_0",
|
||||
"mlx5_1",
|
||||
"mlx5_2",
|
||||
"mlx5_3",
|
||||
]
|
||||
real_isdir = os.path.isdir
|
||||
real_listdir = os.listdir
|
||||
|
||||
with patch(
|
||||
"sglang.srt.server_args.os.path.isdir",
|
||||
side_effect=lambda path: (
|
||||
True if path == "/sys/class/infiniband" else real_isdir(path)
|
||||
),
|
||||
), patch(
|
||||
"sglang.srt.server_args.os.listdir",
|
||||
side_effect=lambda path: (
|
||||
available_devices
|
||||
if path == "/sys/class/infiniband"
|
||||
else real_listdir(path)
|
||||
),
|
||||
):
|
||||
return ServerArgs._validate_ib_devices(server_args, device_str)
|
||||
|
||||
def test_validate_ib_devices_accepts_comma_separated(self):
|
||||
self.assertEqual(
|
||||
self._validate_ib_devices("mlx5_0, mlx5_1"),
|
||||
"mlx5_0,mlx5_1",
|
||||
)
|
||||
|
||||
def test_validate_ib_devices_accepts_json_object(self):
|
||||
result = self._validate_ib_devices(
|
||||
'{"0": "mlx5_0, mlx5_1", "1": "mlx5_2, mlx5_3"}'
|
||||
)
|
||||
self.assertEqual(
|
||||
json.loads(result),
|
||||
{"0": "mlx5_0,mlx5_1", "1": "mlx5_2,mlx5_3"},
|
||||
)
|
||||
|
||||
def test_validate_ib_devices_accepts_json_file(self):
|
||||
with tempfile.NamedTemporaryFile("w", suffix=".json", delete=False) as file:
|
||||
json.dump({"0": "mlx5_0, mlx5_1", "1": "mlx5_2"}, file)
|
||||
json_file = file.name
|
||||
|
||||
try:
|
||||
result = self._validate_ib_devices(json_file)
|
||||
finally:
|
||||
os.unlink(json_file)
|
||||
|
||||
self.assertEqual(
|
||||
json.loads(result),
|
||||
{"0": "mlx5_0,mlx5_1", "1": "mlx5_2"},
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
||||
Reference in New Issue
Block a user