Skip to content

Commit 9e32dfd

Browse files
committed
Adding find free port logic to test_infer.py
Signed-off-by: John St. John <[email protected]>
1 parent f2eb0b6 commit 9e32dfd

File tree

1 file changed

+18
-13
lines changed
  • bionemo-recipes/recipes/evo2_megatron/tests/bionemo/evo2/run

1 file changed

+18
-13
lines changed

bionemo-recipes/recipes/evo2_megatron/tests/bionemo/evo2/run/test_infer.py

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,23 @@
2828

2929
import os
3030
import subprocess
31-
from pathlib import Path
3231

3332
import pytest
3433
import torch
3534

3635
from bionemo.evo2.models.evo2_provider import HyenaInferenceContext
3736

3837

38+
def find_free_network_port(address: str = "localhost") -> int:
39+
"""Find a free port on localhost for distributed testing."""
40+
import socket
41+
42+
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
43+
s.bind((address, 0))
44+
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
45+
return s.getsockname()[1]
46+
47+
3948
@pytest.fixture(scope="module")
4049
def mbridge_checkpoint_path(tmp_path_factory):
4150
"""Create or use an MBridge checkpoint for testing.
@@ -101,16 +110,15 @@ def test_infer_runs(mbridge_checkpoint_path, tmp_path):
101110

102111
env = os.environ.copy()
103112
env["MASTER_ADDR"] = "localhost"
104-
env["MASTER_PORT"] = "29501"
113+
env["MASTER_PORT"] = str(find_free_network_port())
105114

106115
result = subprocess.run(
107116
cmd,
108117
check=False,
109118
capture_output=True,
110119
text=True,
111-
timeout=300,
120+
timeout=300, # 5 minutes
112121
env=env,
113-
cwd=str(Path(__file__).parent.parent.parent.parent.parent),
114122
)
115123

116124
assert result.returncode == 0, f"infer command failed:\nstdout: {result.stdout}\nstderr: {result.stderr}"
@@ -148,16 +156,15 @@ def test_infer_temperature(mbridge_checkpoint_path, tmp_path, temperature):
148156

149157
env = os.environ.copy()
150158
env["MASTER_ADDR"] = "localhost"
151-
env["MASTER_PORT"] = "29502"
159+
env["MASTER_PORT"] = str(find_free_network_port())
152160

153161
result = subprocess.run(
154162
cmd,
155163
check=False,
156164
capture_output=True,
157165
text=True,
158-
timeout=300,
166+
timeout=300, # 5 minutes
159167
env=env,
160-
cwd=str(Path(__file__).parent.parent.parent.parent.parent),
161168
)
162169

163170
assert result.returncode == 0, f"infer command failed:\nstdout: {result.stdout}\nstderr: {result.stderr}"
@@ -189,16 +196,15 @@ def test_infer_top_k(mbridge_checkpoint_path, tmp_path):
189196

190197
env = os.environ.copy()
191198
env["MASTER_ADDR"] = "localhost"
192-
env["MASTER_PORT"] = "29503"
199+
env["MASTER_PORT"] = str(find_free_network_port())
193200

194201
result = subprocess.run(
195202
cmd,
196203
check=False,
197204
capture_output=True,
198205
text=True,
199-
timeout=300,
206+
timeout=300, # 5 minutes
200207
env=env,
201-
cwd=str(Path(__file__).parent.parent.parent.parent.parent),
202208
)
203209

204210
assert result.returncode == 0, f"infer command failed:\nstdout: {result.stdout}\nstderr: {result.stderr}"
@@ -245,16 +251,15 @@ def test_infer_phylogenetic_prompt(mbridge_checkpoint_path, tmp_path):
245251

246252
env = os.environ.copy()
247253
env["MASTER_ADDR"] = "localhost"
248-
env["MASTER_PORT"] = "29504"
254+
env["MASTER_PORT"] = str(find_free_network_port())
249255

250256
result = subprocess.run(
251257
cmd,
252258
check=False,
253259
capture_output=True,
254260
text=True,
255-
timeout=300,
261+
timeout=300, # 5 minutes
256262
env=env,
257-
cwd=str(Path(__file__).parent.parent.parent.parent.parent),
258263
)
259264

260265
assert result.returncode == 0, f"infer command failed:\nstdout: {result.stdout}\nstderr: {result.stderr}"

0 commit comments

Comments
 (0)