11import yaml
22import os
33import sys
4- from multiprocessing import Process
54import signal
6- import json
75import ray
8- import torch
96
107os .environ ["OPENBLAS_NUM_THREADS" ] = (
118 "1" # 解决线程不足时,OpenBLAS blas_thread_init报错
2118sys .path .append (root_dir )
2219os .environ ["LOGDIR" ] = os .path .join (root_dir , "logs" )
2320from gpt_server .utils import (
24- start_server ,
2521 start_api_server ,
26- run_cmd ,
22+ start_model_worker ,
2723 stop_server ,
2824 delete_log ,
2925)
@@ -38,74 +34,13 @@ def signal_handler(signum, frame):
3834
3935
4036signal .signal (signal .SIGINT , signal_handler )
37+
4138config_path = os .path .join (root_dir , "gpt_server/script/config.yaml" )
4239with open (config_path , "r" ) as f :
4340 config = yaml .safe_load (f )
4441# print(config)
45- # ----------------------------启动 Controller 和 Openai API 服务----------------------------------------------------
46- start_api_server (config )
47- # ----------------------------启动 Controller 和 Openai API 服务----------------------------------------------------
48- process = []
49- for model_config_ in config ["models" ]:
50- for model_name , model_config in model_config_ .items ():
51- # 启用的模型
52- if model_config ["enable" ]:
53- # pprint(model_config)
54- print ()
55- # 模型地址
56- model_name_or_path = model_config ["model_name_or_path" ]
57- # 模型类型
58- model_type = model_config ["model_type" ]
59- lora = model_config .get ("lora" , None )
60-
61- # model type 校验
62- # py_path = f"{root_dir}/gpt_server/model_worker/{model_type}.py"
63- py_path = f"-m gpt_server.model_worker.{ model_type } "
64-
65- model_names = model_name
66- if model_config ["alias" ]:
67- model_names = model_name + "," + model_config ["alias" ]
68- if lora : # 如果使用lora,将lora的name添加到 model_names 中
69- lora_names = list (lora .keys ())
70- model_names += "," + "," .join (lora_names )
71-
72- # 获取 worker 数目 并获取每个 worker 的资源
73- workers = model_config ["workers" ]
74-
75- # process = []
76- for worker in workers :
77- gpus = worker ["gpus" ]
78- # 将gpus int ---> str
79- gpus = [str (i ) for i in gpus ]
80- gpus_str = "," .join (gpus )
81- num_gpus = len (gpus )
82- run_mode = "python "
83- CUDA_VISIBLE_DEVICES = ""
84- if (
85- torch .cuda .is_available ()
86- and model_config ["device" ].lower () == "gpu"
87- ):
88- CUDA_VISIBLE_DEVICES = f"CUDA_VISIBLE_DEVICES={ gpus_str } "
89- elif model_config ["device" ].lower () == "cpu" :
90- CUDA_VISIBLE_DEVICES = ""
91- else :
92- raise Exception ("目前仅支持 CPU/GPU设备!" )
93- backend = model_config ["work_mode" ]
94-
95- cmd = (
96- CUDA_VISIBLE_DEVICES
97- + run_mode
98- + py_path
99- + f" --num_gpus { num_gpus } "
100- + f" --model_name_or_path { model_name_or_path } "
101- + f" --model_names { model_names } "
102- + f" --backend { backend } "
103- )
104- if lora :
105- cmd += f" --lora '{ json .dumps (lora )} '"
106-
107- p = Process (target = run_cmd , args = (cmd ,))
108- p .start ()
109- process .append (p )
110- for p in process :
111- p .join ()
42+ if __name__ == "__main__" :
43+ # ----------------------------启动 Controller 和 Openai API 服务----------------------------------------
44+ start_api_server (config = config )
45+ # ----------------------------启动 Model Worker 服务----------------------------------------------------
46+ start_model_worker (config = config )
0 commit comments