Initialize model data parallel groups. Arguments: model_parallel_size: number of GPUs used to parallelize model. Let's say we have a total of 8 GPUs denoted by g0 ... g7 and we use 2 GPUs to parallelize the model. The present function will create 4 model parallel group
(model_parallel_size_)
| 28 | |
| 29 | |
| 30 | def initialize_model_parallel(model_parallel_size_): |
| 31 | """ |
| 32 | Initialize model data parallel groups. |
| 33 | |
| 34 | Arguments: |
| 35 | model_parallel_size: number of GPUs used to parallelize model. |
| 36 | |
| 37 | Let's say we have a total of 8 GPUs denoted by g0 ... g7 and we |
| 38 | use 2 GPUs to parallelize the model. The present function will |
| 39 | create 4 model parallel groups and 2 data parallel grous as: |
| 40 | 4 model parallel groups: |
| 41 | [g0, g1], [g2, g3], [g4, g5], [g6, g7] |
| 42 | 2 data parallel groups: |
| 43 | [g0, g2, g4, g6], [g1, g3, g5, g7] |
| 44 | Note that for efficiency, the caller should make sure adjacent ranks |
| 45 | are on the same DGX box. For example if we are using 2 DGX-1 boxes |
| 46 | with a total of 16 GPUs, rank 0 to 7 belong to the first box and |
| 47 | ranks 8 to 15 belong to the second box. |
| 48 | """ |
| 49 | if torch.distributed.get_rank() == 0: |
| 50 | print('> initializing model parallel with size {}'.format( |
| 51 | model_parallel_size_)) |
| 52 | # Get world size and rank. Ensure some consistencies. |
| 53 | assert torch.distributed.is_initialized() |
| 54 | world_size = torch.distributed.get_world_size() |
| 55 | model_parallel_size = min(model_parallel_size_, world_size) |
| 56 | ensure_divisibility(world_size, model_parallel_size) |
| 57 | rank = torch.distributed.get_rank() |
| 58 | |
| 59 | # Build the data parallel groups. |
| 60 | global _DATA_PARALLEL_GROUP |
| 61 | assert _DATA_PARALLEL_GROUP is None, \ |
| 62 | 'data parallel group is already initialized' |
| 63 | for i in range(model_parallel_size): |
| 64 | ranks = range(i, world_size, model_parallel_size) |
| 65 | group = torch.distributed.new_group(ranks) |
| 66 | if i == (rank % model_parallel_size): |
| 67 | _DATA_PARALLEL_GROUP = group |
| 68 | |
| 69 | # Build the model parallel groups. |
| 70 | global _MODEL_PARALLEL_GROUP |
| 71 | assert _MODEL_PARALLEL_GROUP is None, \ |
| 72 | 'model parallel group is already initialized' |
| 73 | for i in range(world_size // model_parallel_size): |
| 74 | ranks = range(i * model_parallel_size, |
| 75 | (i + 1) * model_parallel_size) |
| 76 | group = torch.distributed.new_group(ranks) |
| 77 | if i == (rank // model_parallel_size): |
| 78 | _MODEL_PARALLEL_GROUP = group |
| 79 | |
| 80 | |
| 81 | def model_parallel_is_initialized(): |
nothing calls this directly
no test coverage detected