MCPcopy Create free account
hub / github.com/modelscope/modelscope / _init_dist_slurm

Function _init_dist_slurm

modelscope/utils/torch_utils.py:85–121  ·  view source on GitHub ↗

Initialize slurm distributed training environment. If argument ``port`` is not specified, then the master port will be system environment variable ``MASTER_PORT``. If ``MASTER_PORT`` is not in system environment variable, then a default port ``29500`` will be used. Args: ba

(backend: str, port: Optional[int] = None)

Source from the content-addressed store, hash-verified

83
84
85def _init_dist_slurm(backend: str, port: Optional[int] = None) -> None:
86 """Initialize slurm distributed training environment.
87
88 If argument ``port`` is not specified, then the master port will be system
89 environment variable ``MASTER_PORT``. If ``MASTER_PORT`` is not in system
90 environment variable, then a default port ``29500`` will be used.
91
92 Args:
93 backend (str): Backend of torch.distributed.
94 port (int, optional): Master port. Defaults to None.
95 """
96 proc_id = int(os.environ['SLURM_PROCID'])
97 ntasks = int(os.environ['SLURM_NTASKS'])
98 node_list = os.environ['SLURM_NODELIST']
99 num_gpus = torch.cuda.device_count()
100 torch.cuda.set_device(proc_id % num_gpus)
101 addr = subprocess.getoutput(
102 f'scontrol show hostname {node_list} | head -n1')
103 # specify master port
104 if port is not None:
105 os.environ['MASTER_PORT'] = str(port)
106 elif 'MASTER_PORT' in os.environ:
107 pass # use MASTER_PORT in the environment variable
108 else:
109 # if torch.distributed default port(29500) is available
110 # then use it, else find a free port
111 if _is_free_port(29500):
112 os.environ['MASTER_PORT'] = '29500'
113 else:
114 os.environ['MASTER_PORT'] = str(_find_free_port())
115 # use MASTER_ADDR in the environment variable if it already exists
116 if 'MASTER_ADDR' not in os.environ:
117 os.environ['MASTER_ADDR'] = addr
118 os.environ['WORLD_SIZE'] = str(ntasks)
119 os.environ['LOCAL_RANK'] = str(proc_id % num_gpus)
120 os.environ['RANK'] = str(proc_id)
121 dist.init_process_group(backend=backend)
122
123
124def get_dist_info(group=None) -> Tuple[int, int]:

Callers 1

init_distFunction · 0.85

Calls 3

_is_free_portFunction · 0.85
_find_free_portFunction · 0.85
set_deviceMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…