From 2fd5c9c197145bfb3fe53955ae8389b81c9ecc1c Mon Sep 17 00:00:00 2001 From: jqueguiner Date: Wed, 16 Nov 2022 16:56:57 +0000 Subject: [PATCH 1/7] enable port dynamique assignment --- mii/config.py | 44 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 42 insertions(+), 2 deletions(-) diff --git a/mii/config.py b/mii/config.py index 71ec9686..8c256376 100644 --- a/mii/config.py +++ b/mii/config.py @@ -1,11 +1,11 @@ import torch from typing import Union, List from pydantic import BaseModel, validator - +import socket class MIIConfig(BaseModel): tensor_parallel: int = 1 - port_number: int = 50050 + port_number: int = None dtype: str = "float" enable_cuda_graph: bool = False checkpoint_dict: Union[dict, None] = None @@ -16,6 +16,46 @@ class MIIConfig(BaseModel): profile_model_time: bool = False skip_model_check: bool = False + DEFAULT_PORT = 50050 + + def __is_port_in_use(port_number: int) -> bool: + """ + Checks if a port_number is in use + + Args: + port_number (int): port_number to check + + Returns: + bool: True if port_number is in use else False + """ + with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: + return s.connect_ex(('localhost', port_number)) == 0 + + + @validator('port_number') + def assign_port(port_number: int = None) -> int: + """ + Starts a socket connection to grab a free port (Involves a race + condition but will do for now) + Args: + port_number (int): Port to start the socket connection (default: None) + Returns: + int: Free port number + """ + + # if port is None set the default 50050 and default port is not in use return it + if port is None: + port = MIIConfig.DEFAULT_PORT + + # if the defined port is in use find a free port + if MIIConfig.__is_port_in_use(port_number): + tcp = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + tcp.bind(("", 0)) + _, port = tcp.getsockname() + tcp.close() + + return int(port) + @validator('dtype') def dtype_valid(cls, value): # parse dtype value to determine torch dtype From b5fd46115d58e0954c813faf72a91eb19583d548 Mon Sep 17 00:00:00 2001 From: jqueguiner Date: Wed, 16 Nov 2022 19:08:45 +0000 Subject: [PATCH 2/7] fix(config): variable name --- mii/config.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/mii/config.py b/mii/config.py index 8c256376..dab0c8d6 100644 --- a/mii/config.py +++ b/mii/config.py @@ -16,8 +16,6 @@ class MIIConfig(BaseModel): profile_model_time: bool = False skip_model_check: bool = False - DEFAULT_PORT = 50050 - def __is_port_in_use(port_number: int) -> bool: """ Checks if a port_number is in use @@ -42,19 +40,21 @@ def assign_port(port_number: int = None) -> int: Returns: int: Free port number """ - + DEFAULT_PORT = 50050 # if port is None set the default 50050 and default port is not in use return it - if port is None: - port = MIIConfig.DEFAULT_PORT + if port_number is None: + port_number = DEFAULT_PORT # if the defined port is in use find a free port if MIIConfig.__is_port_in_use(port_number): tcp = socket.socket(socket.AF_INET, socket.SOCK_STREAM) tcp.bind(("", 0)) - _, port = tcp.getsockname() + _, port_number = tcp.getsockname() tcp.close() - return int(port) + MIIConfig.port_number = port_number + + return port_number @validator('dtype') def dtype_valid(cls, value): From cc11fcb13658cf18a439716ec4824e075b753366 Mon Sep 17 00:00:00 2001 From: jqueguiner Date: Sun, 20 Nov 2022 17:57:14 +0000 Subject: [PATCH 3/7] refactor(config): refactor yapf --- mii/config.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mii/config.py b/mii/config.py index dab0c8d6..f589baa8 100644 --- a/mii/config.py +++ b/mii/config.py @@ -3,6 +3,7 @@ from pydantic import BaseModel, validator import socket + class MIIConfig(BaseModel): tensor_parallel: int = 1 port_number: int = None @@ -29,7 +30,6 @@ def __is_port_in_use(port_number: int) -> bool: with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: return s.connect_ex(('localhost', port_number)) == 0 - @validator('port_number') def assign_port(port_number: int = None) -> int: """ From 2ad0e022c4119378e4292bebf4dc907dc9aeee9f Mon Sep 17 00:00:00 2001 From: Michael Wyatt Date: Mon, 28 Aug 2023 14:33:31 -0700 Subject: [PATCH 4/7] Update mii/config.py --- mii/config.py | 6 ------ 1 file changed, 6 deletions(-) diff --git a/mii/config.py b/mii/config.py index 2e1254ca..75a1ac59 100644 --- a/mii/config.py +++ b/mii/config.py @@ -98,12 +98,6 @@ def assign_port(port_number: int = None) -> int: return port_number - @validator('dtype') - def dtype_valid(cls, value): - # parse dtype value to determine torch dtype - MIIConfig._torch_dtype(value) - return value.lower() - @validator("deploy_rank") def deploy_valid(cls, field_value, values): if "tensor_parallel" not in values: From cc4885f5ef429f21f2c681086decb4604caa7fb7 Mon Sep 17 00:00:00 2001 From: Michael Wyatt Date: Mon, 28 Aug 2023 14:44:22 -0700 Subject: [PATCH 5/7] Update mii/config.py --- mii/config.py | 1 + 1 file changed, 1 insertion(+) diff --git a/mii/config.py b/mii/config.py index 75a1ac59..5e29b6d2 100644 --- a/mii/config.py +++ b/mii/config.py @@ -41,6 +41,7 @@ def __repr__(self): class MIIConfig(BaseModel): tensor_parallel: int = 1 port_number: int = None + _DEFAULT_PORT = 50050 dtype: DtypeEnum = torch.float32 meta_tensor: bool = False load_with_sys_mem: bool = False From e10dfcca15a76bb67c61c32aea4dc784d2977014 Mon Sep 17 00:00:00 2001 From: Michael Wyatt Date: Mon, 28 Aug 2023 14:44:32 -0700 Subject: [PATCH 6/7] Update mii/config.py --- mii/config.py | 56 +++++++++++++++++---------------------------------- 1 file changed, 19 insertions(+), 37 deletions(-) diff --git a/mii/config.py b/mii/config.py index 5e29b6d2..b79df4d0 100644 --- a/mii/config.py +++ b/mii/config.py @@ -60,44 +60,26 @@ class MIIConfig(BaseModel): hostfile: str = DLTS_HOSTFILE trust_remote_code: bool = False - def __is_port_in_use(port_number: int) -> bool: - """ - Checks if a port_number is in use - - Args: - port_number (int): port_number to check - - Returns: - bool: True if port_number is in use else False - """ - with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: - return s.connect_ex(('localhost', port_number)) == 0 - @validator('port_number') - def assign_port(port_number: int = None) -> int: - """ - Starts a socket connection to grab a free port (Involves a race - condition but will do for now) - Args: - port_number (int): Port to start the socket connection (default: None) - Returns: - int: Free port number - """ - DEFAULT_PORT = 50050 - # if port is None set the default 50050 and default port is not in use return it - if port_number is None: - port_number = DEFAULT_PORT - - # if the defined port is in use find a free port - if MIIConfig.__is_port_in_use(port_number): - tcp = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - tcp.bind(("", 0)) - _, port_number = tcp.getsockname() - tcp.close() - - MIIConfig.port_number = port_number - - return port_number + def assign_port(cls, field_value, values): + def is_port_in_use(port_number: int) -> bool: + with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: + return s.connect_ex(('localhost', port_number)) == 0 + + # If the user sets a port, make sure we use that port + if field_value is not None: + assert not is_port_in_use(field_value), f"Port number {field_value} already in use." + # Otherwise try the default value + else: + field_value = cls._DEFAULT_PORT + # If the default is in use, select a random port + if is_port_in_use(field_value): + tcp = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + tcp.bind(("", 0)) + field_value = tcp.getsockname()[1] + tcp.close() + + return field_value @validator("deploy_rank") def deploy_valid(cls, field_value, values): From 68539b4e3cb631eac269874e51ad790dbc2892f4 Mon Sep 17 00:00:00 2001 From: Michael Wyatt Date: Mon, 28 Aug 2023 15:30:50 -0700 Subject: [PATCH 7/7] Update config.py --- mii/config.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mii/config.py b/mii/config.py index b79df4d0..a6233fee 100644 --- a/mii/config.py +++ b/mii/config.py @@ -59,7 +59,7 @@ class MIIConfig(BaseModel): replica_num: int = 1 hostfile: str = DLTS_HOSTFILE trust_remote_code: bool = False - + @validator('port_number') def assign_port(cls, field_value, values): def is_port_in_use(port_number: int) -> bool: