Skip to content

Commit

Permalink
Uniform code style for utils for better readability (#108)
Browse files Browse the repository at this point in the history
  • Loading branch information
UranusSeven authored Sep 5, 2023
1 parent 2d3cd33 commit ba40f3b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
13 changes: 10 additions & 3 deletions lightllm/utils/infer_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,29 @@ def time_func(*args, **kwargs):
ans = func(*args, **kwargs)
torch.cuda.synchronize()
return ans

return time_func

return inner_func


time_mark = {}


def mark_start(key):
torch.cuda.synchronize()
global time_mark
time_mark[key] = time.time()
return


def mark_end(key, print_min_cost=0.0):
torch.cuda.synchronize()
global time_mark
cost_time = (time.time() - time_mark[key]) * 1000
if cost_time > print_min_cost:
print(f"cost {key}:", cost_time)


def calculate_time(show=False, min_cost_ms=0.0):
def wrapper(func):
Expand All @@ -53,16 +58,18 @@ def inner_func(*args, **kwargs):
if cost_time > min_cost_ms:
print(f"Function {func.__name__} took {cost_time} ms to run.")
return result

return inner_func

return wrapper


def set_random_seed(seed: int) -> None:
import random

random.seed(seed)
import numpy as np

torch.manual_seed(seed)
if torch.cuda.is_available():
torch.cuda.manual_seed_all(seed)


7 changes: 3 additions & 4 deletions lightllm/utils/net_utils.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import socket



def alloc_can_use_network_port(num=3, used_nccl_port=None):
port_list = []
for port in range(10000, 65536):
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
result = s.connect_ex(('localhost', port))
result = s.connect_ex(("localhost", port))
if result != 0 and port != used_nccl_port:
port_list.append(port)

if len(port_list) == num:
return port_list
return None
return None

0 comments on commit ba40f3b

Please sign in to comment.