diff --git a/tests/diffusion/test_omni_diffusion_config_master_port.py b/tests/diffusion/test_omni_diffusion_config_master_port.py new file mode 100644 index 000000000..d65586021 --- /dev/null +++ b/tests/diffusion/test_omni_diffusion_config_master_port.py @@ -0,0 +1,49 @@ +# SPDX-License-Identifier: Apache-2.0 +# SPDX-FileCopyrightText: Copyright contributors to the vLLM project +"""Unit tests for OmniDiffusionConfig master_port resolution (issue #3794).""" + +import socket + +import pytest + +from vllm_omni.diffusion.data import OmniDiffusionConfig + +pytestmark = [pytest.mark.core_model, pytest.mark.cpu] + + +class TestOmniDiffusionConfigMasterPort: + def test_honors_master_port_env(self, monkeypatch: pytest.MonkeyPatch) -> None: + monkeypatch.setenv("MASTER_PORT", "43210") + config = OmniDiffusionConfig(model="test") + assert config.master_port == 43210 + + def test_honors_explicit_master_port(self, monkeypatch: pytest.MonkeyPatch) -> None: + monkeypatch.delenv("MASTER_PORT", raising=False) + config = OmniDiffusionConfig(model="test", master_port=40000) + assert config.master_port == 40000 + + def test_master_port_env_takes_precedence_over_kwarg(self, monkeypatch: pytest.MonkeyPatch) -> None: + monkeypatch.setenv("MASTER_PORT", "43210") + config = OmniDiffusionConfig(model="test", master_port=40000) + assert config.master_port == 43210 + + def test_explicit_master_port_is_stable_without_jitter(self, monkeypatch: pytest.MonkeyPatch) -> None: + monkeypatch.delenv("MASTER_PORT", raising=False) + ports = {OmniDiffusionConfig(model="test", master_port=40123).master_port for _ in range(5)} + assert ports == {40123} + + def test_default_master_port_is_available(self, monkeypatch: pytest.MonkeyPatch) -> None: + monkeypatch.delenv("MASTER_PORT", raising=False) + config = OmniDiffusionConfig(model="test") + assert config.master_port is not None + with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock: + sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) + sock.bind(("", config.master_port)) + + def test_settle_port_steps_when_port_is_busy(self, monkeypatch: pytest.MonkeyPatch) -> None: + monkeypatch.delenv("MASTER_PORT", raising=False) + with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock: + sock.bind(("", 0)) + busy_port = sock.getsockname()[1] + config = OmniDiffusionConfig(model="test", master_port=busy_port) + assert config.master_port == busy_port + 37 diff --git a/vllm_omni/diffusion/data.py b/vllm_omni/diffusion/data.py index 98674ee2d..9841aa2e3 100644 --- a/vllm_omni/diffusion/data.py +++ b/vllm_omni/diffusion/data.py @@ -606,6 +606,25 @@ class OmniDiffusionConfig: return False + def _resolve_master_port(self) -> int: + """Resolve torch.distributed master port without unnecessary random jitter. + + Precedence: + 1. ``MASTER_PORT`` environment variable (set by orchestrators for multi-replica launch). + 2. Explicit ``master_port`` passed at construction time. + 3. An OS-assigned ephemeral port when neither is provided. + """ + from vllm.utils.network_utils import get_open_port + + from vllm_omni.diffusion import envs + + env_port = envs.MASTER_PORT + if env_port is not None: + return self.settle_port(env_port, port_inc=37) + if self.master_port is not None: + return self.settle_port(self.master_port, port_inc=37) + return self.settle_port(get_open_port(), port_inc=37) + def settle_port(self, port: int, port_inc: int = 42, max_attempts: int = 100) -> int: """ Find an available port with retry logic. @@ -642,9 +661,7 @@ class OmniDiffusionConfig: ) def __post_init__(self): - # TODO: remove hard code - initial_master_port = (self.master_port or 30005) + random.randint(0, 100) - self.master_port = self.settle_port(initial_master_port, 37) + self.master_port = self._resolve_master_port() if isinstance(self.profiler_config, dict): from vllm.config import ProfilerConfig