megatron-core 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of megatron-core might be problematic. Click here for more details.

@@ -0,0 +1,12 @@
1
+ import megatron.core.parallel_state
2
+ import megatron.core.tensor_parallel
3
+ import megatron.core.utils
4
+
5
+ # Alias parallel_state as mpu, its legacy name
6
+ mpu = parallel_state
7
+
8
+ __all__ = [
9
+ "parallel_state",
10
+ "tensor_parallel",
11
+ "utils",
12
+ ]
megatron/core/enums.py ADDED
@@ -0,0 +1,7 @@
1
+ # Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
2
+
3
+ import enum
4
+
5
+ class ModelType(enum.Enum):
6
+ encoder_or_decoder = 1
7
+ encoder_and_decoder = 2
@@ -0,0 +1,23 @@
1
+ # Copyright (c) 2023, NVIDIA CORPORATION. All rights reserved.
2
+
3
+
4
+ MAJOR = 0
5
+ MINOR = 1
6
+ PATCH = 0
7
+ PRE_RELEASE = ''
8
+
9
+ # Use the following formatting: (major, minor, patch, pre-release)
10
+ VERSION = (MAJOR, MINOR, PATCH, PRE_RELEASE)
11
+
12
+ __shortversion__ = '.'.join(map(str, VERSION[:3]))
13
+ __version__ = '.'.join(map(str, VERSION[:3])) + ''.join(VERSION[3:])
14
+
15
+ __package_name__ = 'megatron_core'
16
+ __contact_names__ = 'NVIDIA'
17
+ __contact_emails__ = 'nemo-toolkit@nvidia.com' # use NeMo Email
18
+ __homepage__ = 'https://docs.nvidia.com/deeplearning/nemo/user-guide/docs/en/stable/' # use NeMo homepage
19
+ __repository_url__ = 'https://github.com/NVIDIA/Megatron-LM/megatron/core'
20
+ __download_url__ = 'https://github.com/NVIDIA/Megatron-LM/releases'
21
+ __description__ = 'Megatron Core - a library for efficient and scalable training of transformer based models'
22
+ __license__ = 'BSD-3'
23
+ __keywords__ = 'deep learning, machine learning, gpu, NLP, NLU, language, transformer, nvidia, pytorch, torch'
@@ -0,0 +1,570 @@
1
+ # Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
2
+
3
+ """Model and data parallel groups."""
4
+
5
+ import torch
6
+ from typing import Optional
7
+
8
+ from .utils import GlobalMemoryBuffer
9
+
10
+ # Intra-layer model parallel group that the current rank belongs to.
11
+ _TENSOR_MODEL_PARALLEL_GROUP = None
12
+ # Inter-layer model parallel group that the current rank belongs to.
13
+ _PIPELINE_MODEL_PARALLEL_GROUP = None
14
+ # Model parallel group (both intra- and pipeline) that the current rank belongs to.
15
+ _MODEL_PARALLEL_GROUP = None
16
+ # Embedding group.
17
+ _EMBEDDING_GROUP = None
18
+ # Position embedding group.
19
+ _POSITION_EMBEDDING_GROUP = None
20
+ # Data parallel group that the current rank belongs to.
21
+ _DATA_PARALLEL_GROUP = None
22
+
23
+ _VIRTUAL_PIPELINE_MODEL_PARALLEL_RANK = None
24
+ _VIRTUAL_PIPELINE_MODEL_PARALLEL_WORLD_SIZE = None
25
+ _PIPELINE_MODEL_PARALLEL_SPLIT_RANK = None
26
+
27
+ # These values enable us to change the mpu sizes on the fly.
28
+ _MPU_TENSOR_MODEL_PARALLEL_WORLD_SIZE = None
29
+ _MPU_PIPELINE_MODEL_PARALLEL_WORLD_SIZE = None
30
+ _MPU_TENSOR_MODEL_PARALLEL_RANK = None
31
+ _MPU_PIPELINE_MODEL_PARALLEL_RANK = None
32
+
33
+ # A list of ranks that have a copy of the embedding.
34
+ _EMBEDDING_GLOBAL_RANKS = None
35
+
36
+ # A list of ranks that have a copy of the position embedding.
37
+ _POSITION_EMBEDDING_GLOBAL_RANKS = None
38
+
39
+ # A list of global ranks for each pipeline group to ease calculation of the source
40
+ # rank when broadcasting from the first or last pipeline stage.
41
+ _PIPELINE_GLOBAL_RANKS = None
42
+
43
+ # A list of global ranks for each data parallel group to ease calculation of the source
44
+ # rank when broadcasting weights from src to all other data parallel ranks
45
+ _DATA_PARALLEL_GLOBAL_RANKS = None
46
+
47
+ # Memory buffers to avoid dynamic memory allocation
48
+ _GLOBAL_MEMORY_BUFFER = None
49
+
50
+
51
+ def initialize_model_parallel(
52
+ tensor_model_parallel_size: int = 1,
53
+ pipeline_model_parallel_size: int = 1,
54
+ virtual_pipeline_model_parallel_size: Optional[int] = None,
55
+ pipeline_model_parallel_split_rank: Optional[int] = None,
56
+ ) -> None:
57
+ """
58
+ Initialize model data parallel groups.
59
+
60
+ Arguments:
61
+ tensor_model_parallel_size (int, default = 1):
62
+ The number of GPUs to split individual tensors across.
63
+
64
+ pipeline_model_parallel_size (int, default = 1):
65
+ The number of tensor parallel GPU groups to split the
66
+ Transformer layers across. For example, if
67
+ tensor_model_parallel_size is 4 and
68
+ pipeline_model_parallel_size is 2, the model will be split
69
+ into 2 groups of 4 GPUs.
70
+
71
+ virtual_pipeline_model_parallel_size (int, optional):
72
+ The number of stages that each pipeline group will have,
73
+ interleaving as necessary. If None, no interleaving is
74
+ performed. For example, if tensor_model_parallel_size is 1,
75
+ pipeline_model_parallel_size is 4,
76
+ virtual_pipeline_model_parallel_size is 2, and there are
77
+ 16 transformer layers in the model, the model will be
78
+ split into 8 stages with two layers each and each GPU
79
+ would get 2 stages as such (layer number starting with 1):
80
+
81
+ GPU 0: [1, 2] [9, 10]
82
+ GPU 1: [3, 4] [11, 12]
83
+ GPU 2: [5, 6] [13, 14]
84
+ GPU 3: [7, 8] [15, 16]
85
+
86
+ pipeline_model_parallel_split_rank (int, optional):
87
+ For models with both an encoder and decoder, the rank in
88
+ pipeline to switch between encoder and decoder (i.e. the
89
+ first rank of the decoder). This allows the user to set
90
+ the pipeline parallel size of the encoder and decoder
91
+ independently. For example, if
92
+ pipeline_model_parallel_size is 8 and
93
+ pipeline_model_parallel_split_rank is 3, then ranks 0-2
94
+ will be the encoder and ranks 3-7 will be the decoder.
95
+
96
+ Let's say we have a total of 16 GPUs denoted by g0 ... g15 and we
97
+ use 2 GPUs to parallelize the model tensor, and 4 GPUs to parallelize
98
+ the model pipeline. The present function will
99
+ create 8 tensor model-parallel groups, 4 pipeline model-parallel groups
100
+ and 8 data-parallel groups as:
101
+ 8 data_parallel groups:
102
+ [g0, g2], [g1, g3], [g4, g6], [g5, g7], [g8, g10], [g9, g11], [g12, g14], [g13, g15]
103
+ 8 tensor model-parallel groups:
104
+ [g0, g1], [g2, g3], [g4, g5], [g6, g7], [g8, g9], [g10, g11], [g12, g13], [g14, g15]
105
+ 4 pipeline model-parallel groups:
106
+ [g0, g4, g8, g12], [g1, g5, g9, g13], [g2, g6, g10, g14], [g3, g7, g11, g15]
107
+ Note that for efficiency, the caller should make sure adjacent ranks
108
+ are on the same DGX box. For example if we are using 2 DGX-1 boxes
109
+ with a total of 16 GPUs, rank 0 to 7 belong to the first box and
110
+ ranks 8 to 15 belong to the second box.
111
+ """
112
+ # Get world size and rank. Ensure some consistencies.
113
+ assert torch.distributed.is_initialized()
114
+ world_size: int = torch.distributed.get_world_size()
115
+
116
+ if world_size % (tensor_model_parallel_size * pipeline_model_parallel_size) != 0:
117
+ raise RuntimeError(
118
+ f"world_size ({world_size}) is not divisible by tensor_model_parallel_size "
119
+ f"({tensor_model_parallel_size}) x pipeline_model_parallel_size ({pipeline_model_parallel_size})"
120
+ )
121
+
122
+ data_parallel_size: int = world_size // (tensor_model_parallel_size *
123
+ pipeline_model_parallel_size)
124
+
125
+ num_tensor_model_parallel_groups: int = world_size // tensor_model_parallel_size
126
+ num_pipeline_model_parallel_groups: int = world_size // pipeline_model_parallel_size
127
+ num_data_parallel_groups: int = world_size // data_parallel_size
128
+
129
+ if virtual_pipeline_model_parallel_size is not None:
130
+ if not pipeline_model_parallel_size > 2:
131
+ raise RuntimeError("pipeline-model-parallel size should be greater than 2 with "
132
+ "interleaved schedule")
133
+ global _VIRTUAL_PIPELINE_MODEL_PARALLEL_RANK
134
+ global _VIRTUAL_PIPELINE_MODEL_PARALLEL_WORLD_SIZE
135
+ _VIRTUAL_PIPELINE_MODEL_PARALLEL_RANK = 0
136
+ _VIRTUAL_PIPELINE_MODEL_PARALLEL_WORLD_SIZE = virtual_pipeline_model_parallel_size
137
+
138
+ if pipeline_model_parallel_split_rank is not None:
139
+ global _PIPELINE_MODEL_PARALLEL_SPLIT_RANK
140
+ _PIPELINE_MODEL_PARALLEL_SPLIT_RANK = pipeline_model_parallel_split_rank
141
+
142
+ rank = torch.distributed.get_rank()
143
+
144
+ # Build the data-parallel groups.
145
+ global _DATA_PARALLEL_GROUP
146
+ global _DATA_PARALLEL_GLOBAL_RANKS
147
+ assert _DATA_PARALLEL_GROUP is None, 'data parallel group is already initialized'
148
+ all_data_parallel_group_ranks = []
149
+ for i in range(pipeline_model_parallel_size):
150
+ start_rank = i * num_pipeline_model_parallel_groups
151
+ end_rank = (i + 1) * num_pipeline_model_parallel_groups
152
+ for j in range(tensor_model_parallel_size):
153
+ ranks = range(start_rank + j, end_rank, tensor_model_parallel_size)
154
+ all_data_parallel_group_ranks.append(list(ranks))
155
+ group = torch.distributed.new_group(ranks)
156
+ if rank in ranks:
157
+ _DATA_PARALLEL_GROUP = group
158
+ _DATA_PARALLEL_GLOBAL_RANKS = ranks
159
+
160
+ # Build the model-parallel groups.
161
+ global _MODEL_PARALLEL_GROUP
162
+ assert _MODEL_PARALLEL_GROUP is None, 'model parallel group is already initialized'
163
+ for i in range(data_parallel_size):
164
+ ranks = [data_parallel_group_ranks[i]
165
+ for data_parallel_group_ranks in all_data_parallel_group_ranks]
166
+ group = torch.distributed.new_group(ranks)
167
+ if rank in ranks:
168
+ _MODEL_PARALLEL_GROUP = group
169
+
170
+ # Build the tensor model-parallel groups.
171
+ global _TENSOR_MODEL_PARALLEL_GROUP
172
+ assert _TENSOR_MODEL_PARALLEL_GROUP is None, \
173
+ 'tensor model parallel group is already initialized'
174
+ for i in range(num_tensor_model_parallel_groups):
175
+ ranks = range(i * tensor_model_parallel_size,
176
+ (i + 1) * tensor_model_parallel_size)
177
+ group = torch.distributed.new_group(ranks)
178
+ if rank in ranks:
179
+ _TENSOR_MODEL_PARALLEL_GROUP = group
180
+
181
+ # Build the pipeline model-parallel groups and embedding groups
182
+ # (first and last rank in each pipeline model-parallel group).
183
+ global _PIPELINE_MODEL_PARALLEL_GROUP
184
+ global _PIPELINE_GLOBAL_RANKS
185
+ assert _PIPELINE_MODEL_PARALLEL_GROUP is None, \
186
+ 'pipeline model parallel group is already initialized'
187
+ global _EMBEDDING_GROUP
188
+ global _EMBEDDING_GLOBAL_RANKS
189
+ assert _EMBEDDING_GROUP is None, 'embedding group is already initialized'
190
+ global _POSITION_EMBEDDING_GROUP
191
+ global _POSITION_EMBEDDING_GLOBAL_RANKS
192
+ assert _POSITION_EMBEDDING_GROUP is None, \
193
+ 'position embedding group is already initialized'
194
+ for i in range(num_pipeline_model_parallel_groups):
195
+ ranks = range(i, world_size, num_pipeline_model_parallel_groups)
196
+ group = torch.distributed.new_group(ranks)
197
+ if rank in ranks:
198
+ _PIPELINE_MODEL_PARALLEL_GROUP = group
199
+ _PIPELINE_GLOBAL_RANKS = ranks
200
+ # Setup embedding group (to exchange gradients between
201
+ # first and last stages).
202
+ if len(ranks) > 1:
203
+ embedding_ranks = [ranks[0], ranks[-1]]
204
+ position_embedding_ranks = [ranks[0]]
205
+ if pipeline_model_parallel_split_rank is not None:
206
+ if ranks[pipeline_model_parallel_split_rank] not in embedding_ranks:
207
+ embedding_ranks = [ranks[0],
208
+ ranks[pipeline_model_parallel_split_rank],
209
+ ranks[-1]]
210
+ if ranks[pipeline_model_parallel_split_rank] not in position_embedding_ranks:
211
+ position_embedding_ranks = [ranks[0],
212
+ ranks[pipeline_model_parallel_split_rank]]
213
+ else:
214
+ embedding_ranks = ranks
215
+ position_embedding_ranks = ranks
216
+
217
+ group = torch.distributed.new_group(embedding_ranks)
218
+ if rank in embedding_ranks:
219
+ _EMBEDDING_GROUP = group
220
+ if rank in ranks:
221
+ _EMBEDDING_GLOBAL_RANKS = embedding_ranks
222
+
223
+ group = torch.distributed.new_group(position_embedding_ranks)
224
+ if rank in position_embedding_ranks:
225
+ _POSITION_EMBEDDING_GROUP = group
226
+ if rank in ranks:
227
+ _POSITION_EMBEDDING_GLOBAL_RANKS = position_embedding_ranks
228
+
229
+ # Initialize global memory buffer
230
+ # This isn't really "parallel state" but there isn't another good place to
231
+ # put this. If we end up with a more generic initialization of megatron-core
232
+ # we could stick it there
233
+ _set_global_memory_buffer()
234
+
235
+
236
+ def is_unitialized():
237
+ """Useful for code segments that may be accessed with or without mpu initialization"""
238
+ return _DATA_PARALLEL_GROUP is None
239
+
240
+
241
+ def model_parallel_is_initialized():
242
+ """Check if model and data parallel groups are initialized."""
243
+ if _TENSOR_MODEL_PARALLEL_GROUP is None or \
244
+ _PIPELINE_MODEL_PARALLEL_GROUP is None or \
245
+ _DATA_PARALLEL_GROUP is None:
246
+ return False
247
+ return True
248
+
249
+
250
+ def get_model_parallel_group():
251
+ """Get the model parallel group the caller rank belongs to."""
252
+ assert _MODEL_PARALLEL_GROUP is not None, \
253
+ 'model parallel group is not initialized'
254
+ return _MODEL_PARALLEL_GROUP
255
+
256
+
257
+ def get_tensor_model_parallel_group():
258
+ """Get the tensor model parallel group the caller rank belongs to."""
259
+ assert _TENSOR_MODEL_PARALLEL_GROUP is not None, \
260
+ 'intra_layer_model parallel group is not initialized'
261
+ return _TENSOR_MODEL_PARALLEL_GROUP
262
+
263
+
264
+ def get_pipeline_model_parallel_group():
265
+ """Get the pipeline model parallel group the caller rank belongs to."""
266
+ assert _PIPELINE_MODEL_PARALLEL_GROUP is not None, \
267
+ 'pipeline_model parallel group is not initialized'
268
+ return _PIPELINE_MODEL_PARALLEL_GROUP
269
+
270
+
271
+ def get_data_parallel_group():
272
+ """Get the data parallel group the caller rank belongs to."""
273
+ assert _DATA_PARALLEL_GROUP is not None, \
274
+ 'data parallel group is not initialized'
275
+ return _DATA_PARALLEL_GROUP
276
+
277
+
278
+ def get_embedding_group():
279
+ """Get the embedding group the caller rank belongs to."""
280
+ assert _EMBEDDING_GROUP is not None, \
281
+ 'embedding group is not initialized'
282
+ return _EMBEDDING_GROUP
283
+
284
+
285
+ def get_position_embedding_group():
286
+ """Get the position embedding group the caller rank belongs to."""
287
+ assert _POSITION_EMBEDDING_GROUP is not None, \
288
+ 'position embedding group is not initialized'
289
+ return _POSITION_EMBEDDING_GROUP
290
+
291
+
292
+ def set_tensor_model_parallel_world_size(world_size):
293
+ """Set the tensor model parallel size"""
294
+ global _MPU_TENSOR_MODEL_PARALLEL_WORLD_SIZE
295
+ _MPU_TENSOR_MODEL_PARALLEL_WORLD_SIZE = world_size
296
+
297
+
298
+ def set_pipeline_model_parallel_world_size(world_size):
299
+ """Set the pipeline model parallel size"""
300
+ global _MPU_PIPELINE_MODEL_PARALLEL_WORLD_SIZE
301
+ _MPU_PIPELINE_MODEL_PARALLEL_WORLD_SIZE = world_size
302
+
303
+ def set_virtual_pipeline_model_parallel_world_size(world_size):
304
+ """Set the pipeline model parallel size"""
305
+ global _VIRTUAL_PIPELINE_MODEL_PARALLEL_WORLD_SIZE
306
+ _VIRTUAL_PIPELINE_MODEL_PARALLEL_WORLD_SIZE = world_size
307
+
308
+ def get_tensor_model_parallel_world_size():
309
+ """Return world size for the tensor model parallel group."""
310
+ global _MPU_TENSOR_MODEL_PARALLEL_WORLD_SIZE
311
+ if _MPU_TENSOR_MODEL_PARALLEL_WORLD_SIZE is not None:
312
+ return _MPU_TENSOR_MODEL_PARALLEL_WORLD_SIZE
313
+ return torch.distributed.get_world_size(group=get_tensor_model_parallel_group())
314
+
315
+
316
+ def get_pipeline_model_parallel_world_size():
317
+ """Return world size for the pipeline model parallel group."""
318
+ global _MPU_PIPELINE_MODEL_PARALLEL_WORLD_SIZE
319
+ if _MPU_PIPELINE_MODEL_PARALLEL_WORLD_SIZE is not None:
320
+ return _MPU_PIPELINE_MODEL_PARALLEL_WORLD_SIZE
321
+ return torch.distributed.get_world_size(group=get_pipeline_model_parallel_group())
322
+
323
+
324
+ def set_tensor_model_parallel_rank(rank):
325
+ """Set tensor model parallel rank."""
326
+ global _MPU_TENSOR_MODEL_PARALLEL_RANK
327
+ _MPU_TENSOR_MODEL_PARALLEL_RANK = rank
328
+
329
+
330
+ def set_pipeline_model_parallel_rank(rank):
331
+ """Set pipeline model parallel rank."""
332
+ global _MPU_PIPELINE_MODEL_PARALLEL_RANK
333
+ _MPU_PIPELINE_MODEL_PARALLEL_RANK = rank
334
+
335
+
336
+ def set_pipeline_model_parallel_split_rank(rank):
337
+ """Set pipeline model parallel split rank."""
338
+ global _PIPELINE_MODEL_PARALLEL_SPLIT_RANK
339
+ _PIPELINE_MODEL_PARALLEL_SPLIT_RANK = rank
340
+
341
+
342
+ def get_tensor_model_parallel_rank():
343
+ """Return my rank for the tensor model parallel group."""
344
+ global _MPU_TENSOR_MODEL_PARALLEL_RANK
345
+ if _MPU_TENSOR_MODEL_PARALLEL_RANK is not None:
346
+ return _MPU_TENSOR_MODEL_PARALLEL_RANK
347
+ return torch.distributed.get_rank(group=get_tensor_model_parallel_group())
348
+
349
+
350
+ def get_pipeline_model_parallel_rank():
351
+ """Return my rank for the pipeline model parallel group."""
352
+ global _MPU_PIPELINE_MODEL_PARALLEL_RANK
353
+ if _MPU_PIPELINE_MODEL_PARALLEL_RANK is not None:
354
+ return _MPU_PIPELINE_MODEL_PARALLEL_RANK
355
+ return torch.distributed.get_rank(group=get_pipeline_model_parallel_group())
356
+
357
+
358
+ def get_pipeline_model_parallel_split_rank():
359
+ """Return pipeline model parallel split rank."""
360
+ global _PIPELINE_MODEL_PARALLEL_SPLIT_RANK
361
+ return _PIPELINE_MODEL_PARALLEL_SPLIT_RANK
362
+
363
+
364
+ def is_pipeline_first_stage(ignore_virtual=False):
365
+ """Return True if in the first pipeline model-parallel stage, False otherwise."""
366
+ if not ignore_virtual:
367
+ if get_virtual_pipeline_model_parallel_world_size() is not None and \
368
+ get_virtual_pipeline_model_parallel_rank() != 0:
369
+ return False
370
+ return get_pipeline_model_parallel_rank() == 0
371
+
372
+
373
+ def is_pipeline_last_stage(ignore_virtual=False):
374
+ """Return True if in the last pipeline model-parallel stage, False otherwise."""
375
+ if not ignore_virtual:
376
+ virtual_pipeline_model_parallel_world_size = \
377
+ get_virtual_pipeline_model_parallel_world_size()
378
+ if virtual_pipeline_model_parallel_world_size is not None and \
379
+ get_virtual_pipeline_model_parallel_rank() != (
380
+ virtual_pipeline_model_parallel_world_size - 1):
381
+ return False
382
+ return get_pipeline_model_parallel_rank() == (
383
+ get_pipeline_model_parallel_world_size() - 1)
384
+
385
+
386
+ def is_rank_in_embedding_group(ignore_virtual=False):
387
+ """Return true if current rank is in embedding group, False otherwise."""
388
+ rank = torch.distributed.get_rank()
389
+ global _EMBEDDING_GLOBAL_RANKS
390
+ if ignore_virtual:
391
+ return rank in _EMBEDDING_GLOBAL_RANKS
392
+ if rank in _EMBEDDING_GLOBAL_RANKS:
393
+ if rank == _EMBEDDING_GLOBAL_RANKS[0]:
394
+ return is_pipeline_first_stage(ignore_virtual=False)
395
+ elif rank == _EMBEDDING_GLOBAL_RANKS[-1]:
396
+ return is_pipeline_last_stage(ignore_virtual=False)
397
+ else:
398
+ return True
399
+ return False
400
+
401
+
402
+ def is_rank_in_position_embedding_group():
403
+ """Return true if current rank is in position embedding group, False otherwise."""
404
+ rank = torch.distributed.get_rank()
405
+ global _POSITION_EMBEDDING_GLOBAL_RANKS
406
+ return rank in _POSITION_EMBEDDING_GLOBAL_RANKS
407
+
408
+
409
+ def is_pipeline_stage_before_split(rank=None):
410
+ """Return True if pipeline stage executes encoder block for a model
411
+ with both encoder and decoder."""
412
+ if get_pipeline_model_parallel_world_size() == 1:
413
+ return True
414
+ if rank is None:
415
+ rank = get_pipeline_model_parallel_rank()
416
+ global _PIPELINE_MODEL_PARALLEL_SPLIT_RANK
417
+ if _PIPELINE_MODEL_PARALLEL_SPLIT_RANK is None:
418
+ return True
419
+ if rank < _PIPELINE_MODEL_PARALLEL_SPLIT_RANK:
420
+ return True
421
+ return False
422
+
423
+
424
+ def is_pipeline_stage_after_split(rank=None):
425
+ """Return True if pipeline stage executes decoder block for a model
426
+ with both encoder and decoder."""
427
+ if get_pipeline_model_parallel_world_size() == 1:
428
+ return True
429
+ if rank is None:
430
+ rank = get_pipeline_model_parallel_rank()
431
+ global _PIPELINE_MODEL_PARALLEL_SPLIT_RANK
432
+ if _PIPELINE_MODEL_PARALLEL_SPLIT_RANK is None:
433
+ return True
434
+ if rank >= _PIPELINE_MODEL_PARALLEL_SPLIT_RANK:
435
+ return True
436
+ return False
437
+
438
+
439
+ def is_pipeline_stage_at_split():
440
+ """Return true if pipeline stage executes decoder block and next
441
+ stage executes encoder block for a model with both encoder and
442
+ decoder."""
443
+ rank = get_pipeline_model_parallel_rank()
444
+ return is_pipeline_stage_before_split(rank) and \
445
+ is_pipeline_stage_after_split(rank+1)
446
+
447
+
448
+ def get_virtual_pipeline_model_parallel_rank():
449
+ """Return the virtual pipeline-parallel rank."""
450
+ global _VIRTUAL_PIPELINE_MODEL_PARALLEL_RANK
451
+ return _VIRTUAL_PIPELINE_MODEL_PARALLEL_RANK
452
+
453
+
454
+ def set_virtual_pipeline_model_parallel_rank(rank):
455
+ """Set the virtual pipeline-parallel rank."""
456
+ global _VIRTUAL_PIPELINE_MODEL_PARALLEL_RANK
457
+ _VIRTUAL_PIPELINE_MODEL_PARALLEL_RANK = rank
458
+
459
+
460
+ def get_virtual_pipeline_model_parallel_world_size():
461
+ """Return the virtual pipeline-parallel world size."""
462
+ global _VIRTUAL_PIPELINE_MODEL_PARALLEL_WORLD_SIZE
463
+ return _VIRTUAL_PIPELINE_MODEL_PARALLEL_WORLD_SIZE
464
+
465
+
466
+ def set_virtual_pipeline_model_parallel_world_size(world_size):
467
+ """Set the virtual pipeline-parallel world size"""
468
+ global _VIRTUAL_PIPELINE_MODEL_PARALLEL_WORLD_SIZE
469
+ _VIRTUAL_PIPELINE_MODEL_PARALLEL_WORLD_SIZE = world_size
470
+
471
+
472
+ def get_tensor_model_parallel_src_rank():
473
+ """Calculate the global rank corresponding to the first local rank
474
+ in the tensor model parallel group."""
475
+ global_rank = torch.distributed.get_rank()
476
+ local_world_size = get_tensor_model_parallel_world_size()
477
+ return (global_rank // local_world_size) * local_world_size
478
+
479
+
480
+ def get_data_parallel_src_rank():
481
+ """Calculate the global rank corresponding to the first local rank
482
+ in the data parallel group."""
483
+ assert _DATA_PARALLEL_GLOBAL_RANKS is not None, \
484
+ "Data parallel group is not initialized"
485
+ return _DATA_PARALLEL_GLOBAL_RANKS[0]
486
+
487
+
488
+ def get_pipeline_model_parallel_first_rank():
489
+ """Return the global rank of the first process in the pipeline for the
490
+ current tensor parallel group"""
491
+ assert _PIPELINE_GLOBAL_RANKS is not None, \
492
+ "Pipeline parallel group is not initialized"
493
+ return _PIPELINE_GLOBAL_RANKS[0]
494
+
495
+
496
+ def get_pipeline_model_parallel_last_rank():
497
+ """Return the global rank of the last process in the pipeline for the
498
+ current tensor parallel group"""
499
+ assert _PIPELINE_GLOBAL_RANKS is not None, \
500
+ "Pipeline parallel group is not initialized"
501
+ last_rank_local = get_pipeline_model_parallel_world_size() - 1
502
+ return _PIPELINE_GLOBAL_RANKS[last_rank_local]
503
+
504
+ def get_pipeline_model_parallel_next_rank():
505
+ """Return the global rank that follows the caller in the pipeline"""
506
+ assert _PIPELINE_GLOBAL_RANKS is not None, \
507
+ "Pipeline parallel group is not initialized"
508
+ rank_in_pipeline = get_pipeline_model_parallel_rank()
509
+ world_size = get_pipeline_model_parallel_world_size()
510
+ return _PIPELINE_GLOBAL_RANKS[(rank_in_pipeline + 1) % world_size]
511
+
512
+
513
+ def get_pipeline_model_parallel_prev_rank():
514
+ """Return the global rank that preceeds the caller in the pipeline"""
515
+ assert _PIPELINE_GLOBAL_RANKS is not None, \
516
+ "Pipeline parallel group is not initialized"
517
+ rank_in_pipeline = get_pipeline_model_parallel_rank()
518
+ world_size = get_pipeline_model_parallel_world_size()
519
+ return _PIPELINE_GLOBAL_RANKS[(rank_in_pipeline - 1) % world_size]
520
+
521
+
522
+ def get_data_parallel_world_size():
523
+ """Return world size for the data parallel group."""
524
+ return torch.distributed.get_world_size(group=get_data_parallel_group())
525
+
526
+
527
+ def get_data_parallel_rank():
528
+ """Return my rank for the data parallel group."""
529
+ return torch.distributed.get_rank(group=get_data_parallel_group())
530
+
531
+ def _set_global_memory_buffer():
532
+ """Initialize global buffer"""
533
+ global _GLOBAL_MEMORY_BUFFER
534
+ assert _GLOBAL_MEMORY_BUFFER is None, 'global memory buffer is already initialized'
535
+ _GLOBAL_MEMORY_BUFFER = GlobalMemoryBuffer()
536
+
537
+ def get_global_memory_buffer():
538
+ """Return the global GlobalMemoryBuffer object"""
539
+ assert _GLOBAL_MEMORY_BUFFER is not None, 'global memory buffer is not initialized'
540
+ return _GLOBAL_MEMORY_BUFFER
541
+
542
+
543
+ def destroy_model_parallel():
544
+ """Set the groups to none."""
545
+ global _MODEL_PARALLEL_GROUP
546
+ _MODEL_PARALLEL_GROUP = None
547
+ global _TENSOR_MODEL_PARALLEL_GROUP
548
+ _TENSOR_MODEL_PARALLEL_GROUP = None
549
+ global _PIPELINE_MODEL_PARALLEL_GROUP
550
+ _PIPELINE_MODEL_PARALLEL_GROUP = None
551
+ global _DATA_PARALLEL_GROUP
552
+ _DATA_PARALLEL_GROUP = None
553
+ global _EMBEDDING_GROUP
554
+ _EMBEDDING_GROUP = None
555
+ global _POSITION_EMBEDDING_GROUP
556
+ _POSITION_EMBEDDING_GROUP = None
557
+ global _VIRTUAL_PIPELINE_MODEL_PARALLEL_RANK
558
+ _VIRTUAL_PIPELINE_MODEL_PARALLEL_RANK = None
559
+ global _VIRTUAL_PIPELINE_MODEL_PARALLEL_WORLD_SIZE
560
+ _VIRTUAL_PIPELINE_MODEL_PARALLEL_WORLD_SIZE = None
561
+ global _MPU_TENSOR_MODEL_PARALLEL_WORLD_SIZE
562
+ _MPU_TENSOR_MODEL_PARALLEL_WORLD_SIZE = None
563
+ global _MPU_PIPELINE_MODEL_PARALLEL_WORLD_SIZE
564
+ _MPU_PIPELINE_MODEL_PARALLEL_WORLD_SIZE = None
565
+ global _MPU_TENSOR_MODEL_PARALLEL_RANK
566
+ _MPU_TENSOR_MODEL_PARALLEL_RANK = None
567
+ global _MPU_PIPELINE_MODEL_PARALLEL_RANK
568
+ _MPU_PIPELINE_MODEL_PARALLEL_RANK = None
569
+ global _GLOBAL_MEMORY_BUFFER
570
+ _GLOBAL_MEMORY_BUFFER = None
@@ -0,0 +1 @@
1
+ from .schedules import get_forward_backward_func