55import threading
66import sys
77import time
8- from typing import Any
8+ from typing import Any , Callable , Dict
99
1010from mmengine .config import Config , ConfigDict
1111from mmengine .utils import mkdir_or_exist
1212
1313from ais_bench .benchmark .registry import (ICL_INFERENCERS , ICL_RETRIEVERS , TASKS )
1414from ais_bench .benchmark .tasks .base import BaseTask
15- from ais_bench .benchmark .utils .config import build_dataset_from_cfg
15+ from ais_bench .benchmark .utils .config import build_dataset_from_cfg , get_model_cls_from_cfg
1616from ais_bench .benchmark .utils .core .abbr import task_abbr_from_cfg , model_abbr_from_cfg
1717from ais_bench .benchmark .tasks .base import TaskStateManager
1818from ais_bench .benchmark .utils .logging import AISLogger
2222from ais_bench .benchmark .openicl .icl_inferencer .icl_base_local_inferencer import BaseLocalInferencer
2323
2424
25+ def _build_msrun_cmd (ctx : Dict [str , Any ]) -> str :
26+ """Build msrun command from context dict."""
27+ parts = [
28+ "msrun" ,
29+ f"--worker_num={ ctx ['worker_num' ]} " ,
30+ f"--local_worker_num={ ctx ['local_worker_num' ]} " ,
31+ f"--master_port={ ctx ['port' ]} " ,
32+ ]
33+ if ctx .get ("master_addr" ) is not None and ctx .get ("node_rank" ) is not None :
34+ parts .extend ([
35+ f"--master_addr={ ctx ['master_addr' ]} " ,
36+ f"--node_rank={ ctx ['node_rank' ]} " ,
37+ ])
38+ parts .extend ([
39+ "--log_dir='output/msrun_log'" ,
40+ "--join=True" ,
41+ "--cluster_time_out=7200" ,
42+ ctx ["script_path" ],
43+ ctx ["cfg_path" ],
44+ ])
45+ return " " .join (parts )
46+
47+
48+ def _build_torchrun_cmd (ctx : Dict [str , Any ]) -> str :
49+ """Build torchrun command from context dict."""
50+ parts = [
51+ "torchrun" ,
52+ f"--master_port={ ctx ['port' ]} " ,
53+ f"--nproc_per_node { ctx ['nproc_per_node' ]} " ,
54+ ]
55+ if ctx .get ("nnodes" , 1 ) > 1 :
56+ parts .extend ([
57+ f"--nnodes { ctx ['nnodes' ]} " ,
58+ f"--node_rank { ctx ['node_rank' ]} " ,
59+ f"--master_addr { ctx ['master_addr' ]} " ,
60+ ])
61+ parts .extend ([ctx ["script_path" ], ctx ["cfg_path" ]])
62+ return " " .join (parts )
63+
64+
65+ LAUNCHER_CMD_BUILDERS : Dict [str , Callable [[Dict [str , Any ]], str ]] = {
66+ "msrun" : _build_msrun_cmd ,
67+ "torchrun" : _build_torchrun_cmd ,
68+ }
69+
2570
2671@TASKS .register_module ()
2772class OpenICLInferTask (BaseTask ):
@@ -43,7 +88,6 @@ def __init__(self, cfg: ConfigDict):
4388 self .node_rank = run_cfg .get ('node_rank' , 0 )
4489 self .master_addr = run_cfg .get ('master_addr' , "localhost" )
4590 self .logger .debug (f"Local infer task config: { run_cfg } " )
46- self .abbr = self .model_cfg .get ('abbr' , '' )
4791
4892 def get_command (self , cfg_path , template ):
4993 """Get the command template for the task.
@@ -60,45 +104,36 @@ def get_command(self, cfg_path, template):
60104 key in str (self .model_cfg .get ('type' , '' ))
61105 or key in str (self .model_cfg .get ('llm' , {}).get ('type' , '' ))
62106 for key in backend_keys )
107+ model_cls = get_model_cls_from_cfg (self .model_cfg )
108+ launcher = getattr (model_cls , 'launcher' , 'torchrun' ) if model_cls else 'torchrun'
63109 if self .num_gpus > 1 and not use_backend and self .nnodes == 1 :
64- port = random .randint (12000 , 32000 )
65- if self .abbr == 'mindformer-model' :
66- command = (
67- f"msrun "
68- f"--worker_num={ self .num_gpus } "
69- f"--local_worker_num={ self .num_gpus } "
70- f"--master_port={ port } "
71- f"--log_dir='output/msrun_log' "
72- f"--join=True "
73- f"--cluster_time_out=7200 "
74- f'{ script_path } { cfg_path } '
75- )
76- else :
77- command = (f'torchrun --master_port={ port } '
78- f'--nproc_per_node { self .num_procs } '
79- f'{ script_path } { cfg_path } ' )
110+ ctx = dict (
111+ port = random .randint (12000 , 32000 ),
112+ script_path = script_path ,
113+ cfg_path = cfg_path ,
114+ worker_num = self .num_gpus ,
115+ local_worker_num = self .num_gpus ,
116+ nproc_per_node = self .num_procs ,
117+ nnodes = 1 ,
118+ master_addr = None ,
119+ node_rank = None ,
120+ )
121+ builder = LAUNCHER_CMD_BUILDERS .get (launcher , LAUNCHER_CMD_BUILDERS ["torchrun" ])
122+ command = builder (ctx )
80123 elif self .nnodes > 1 :
81- port = 12345
82- if self .abbr == "mindformer-model" :
83- command = (
84- f"msrun "
85- f"--worker_num={ self .nnodes * self .num_procs } "
86- f"--local_worker_num={ self .num_procs } "
87- f"--master_port={ port } "
88- f"--master_addr={ self .master_addr } "
89- f"--node_rank={ self .node_rank } "
90- f"--log_dir='output/msrun_log' "
91- f"--join=True "
92- f"--cluster_time_out=7200 "
93- f'{ script_path } { cfg_path } '
94- )
95- else :
96- command = (f'torchrun --master_port={ port } '
97- f'--nproc_per_node { self .num_procs } '
98- f'--nnodes { self .nnodes } '
99- f'--node_rank { self .node_rank } '
100- f'--master_addr { self .master_addr } '
101- f'{ script_path } { cfg_path } ' )
124+ ctx = dict (
125+ port = 12345 ,
126+ script_path = script_path ,
127+ cfg_path = cfg_path ,
128+ worker_num = self .nnodes * self .num_procs ,
129+ local_worker_num = self .num_procs ,
130+ nproc_per_node = self .num_procs ,
131+ nnodes = self .nnodes ,
132+ master_addr = self .master_addr ,
133+ node_rank = self .node_rank ,
134+ )
135+ builder = LAUNCHER_CMD_BUILDERS .get (launcher , LAUNCHER_CMD_BUILDERS ["torchrun" ])
136+ command = builder (ctx )
102137 else :
103138 python = sys .executable
104139 command = f'{ python } { script_path } { cfg_path } '
0 commit comments