Skip to content

Commit d6f0c90

Browse files
committed
refactor: move find_free_port to utils_network.py to resolve circular import
1 parent e02e466 commit d6f0c90

File tree

2 files changed

+13
-14
lines changed

2 files changed

+13
-14
lines changed

src/nanotron/utils.py

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import functools
22
import inspect
33
import os
4-
import random
5-
import socket
64
from contextlib import ExitStack, contextmanager
75
from typing import ContextManager, List, Optional
86

@@ -148,15 +146,3 @@ def tensor_from_untyped_storage(untyped_storage: torch.UntypedStorage, dtype: to
148146
tensor = torch.empty([], dtype=dtype, device=device)
149147
tensor.set_(source=untyped_storage)
150148
return tensor
151-
152-
153-
def find_free_port(min_port: int = 2000, max_port: int = 65000) -> int:
154-
while True:
155-
port = random.randint(min_port, max_port)
156-
try:
157-
with socket.socket() as sock:
158-
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
159-
sock.bind(("localhost", port))
160-
return port
161-
except OSError:
162-
continue

src/nanotron/utils_network.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import random
2+
import socket
3+
4+
def find_free_port(min_port: int = 2000, max_port: int = 65000) -> int:
5+
while True:
6+
port = random.randint(min_port, max_port)
7+
try:
8+
with socket.socket() as sock:
9+
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
10+
sock.bind(("localhost", port))
11+
return port
12+
except OSError:
13+
continue

0 commit comments

Comments
 (0)