MCPcopy Create free account
hub / github.com/YvanYin/DrivingWorld / initialize

Function initialize

utils/dist.py:20–49  ·  view source on GitHub ↗
(fork=False, backend='nccl', gpu_id_if_not_distibuted=0, timeout=30)

Source from the content-addressed store, hash-verified

18
19
20def initialize(fork=False, backend='nccl', gpu_id_if_not_distibuted=0, timeout=30):
21 global __device
22 if not torch.cuda.is_available():
23 print(f'[dist initialize] cuda is not available, use cpu instead', file=sys.stderr)
24 return
25 elif 'RANK' not in os.environ:
26 torch.cuda.set_device(gpu_id_if_not_distibuted)
27 __device = torch.empty(1).cuda().device
28 print(f'[dist initialize] env variable "RANK" is not set, use {__device} as the device', file=sys.stderr)
29 return
30 # then 'RANK' must exist
31 global_rank, num_gpus = int(os.environ['RANK']), torch.cuda.device_count()
32 local_rank = global_rank % num_gpus
33 torch.cuda.set_device(local_rank)
34
35 # ref: https://github.com/open-mmlab/mmcv/blob/master/mmcv/runner/dist_utils.py#L29
36 if mp.get_start_method(allow_none=True) is None:
37 method = 'fork' if fork else 'spawn'
38 print(f'[dist initialize] mp method={method}')
39 mp.set_start_method(method)
40 tdist.init_process_group(backend=backend, timeout=datetime.timedelta(seconds=timeout*60))
41
42 global __rank, __local_rank, __world_size, __initialized
43 __local_rank = local_rank
44 __rank, __world_size = tdist.get_rank(), tdist.get_world_size()
45 __device = torch.empty(1).cuda().device
46 __initialized = True
47
48 assert tdist.is_initialized(), 'torch.distributed is not initialized!'
49 print(f'[lrk={get_local_rank()}, rk={get_rank()}]')
50
51
52def get_rank():

Callers

nothing calls this directly

Calls 2

get_local_rankFunction · 0.85
get_rankFunction · 0.85

Tested by

no test coverage detected