In Megatron-LM, a distributed training framework for large-scale language models, the concept of a global rank is used to uniquely identify each process or worker in the distributed training setup. Megatron-LM typically employs model parallelism (Tensor Parallelism and Pipeline Parallelism) along with Data Parallelism to efficiently train extremely large models. Let's break down the formula and explain each component:
Formula:
global_rank = tp_rank + dp_rank * tp_size + pp_rank * tp_size * dp_size
Each rank reflects the position of a process within one of the parallelism groups, where:
-
tp_rank (Tensor Parallel Rank): Represents the position of the process within the Tensor Parallel (TP) group. Tensor parallelism splits model weights (e.g., layers, matrices) across multiple GPUs, and the processes are assigned a rank within this group.
-
dp_rank (Data Parallel Rank): Represents the position of the process within the Data Parallel (DP) group. Data parallelism replicates entire model replicas across multiple GPUs, where each replica processes different samples/minibatches of training data.
-
pp_rank (Pipeline Parallel Rank): Represents the position of the process within the Pipeline Parallel (PP) group. Pipeline parallelism splits the model layers across multiple GPUs, creating stages in the pipeline.
-
tp_size: The total number of ranks (processes) in the Tensor Parallel group.
-
dp_size: The total number of ranks (processes) in the Data Parallel group.
Explanation:
-
tp_rank:
- This is added first to ensure that within a given Data Parallel and Pipeline Parallel group, the Tensor Parallel ranks (processes within the Tensor Parallel dimension) have contiguous ranks.
-
dp_rank * tp_size:
- This adjusts the global rank to account for the process’s position within the Data Parallel group. The offset for a particular Data Parallel rank is determined by the size of the Tensor Parallel group (
tp_size) since there are tp_size ranks per Data Parallel group.
-
pp_rank * tp_size * dp_size:
- This adjusts the global rank further to ensure that processes in different Pipeline Parallel groups have unique ranks. The offset is determined by the size of both the Tensor Parallel group (
tp_size) and the Data Parallel group (dp_size), as the combination of these defines all ranks within a single Pipeline Parallel group.
Intuition:
The formula calculates the global rank of a process as a combination of its positions in the Tensor Parallel, Data Parallel, and Pipeline Parallel groups. It ensures that no two processes have the same global rank, even though they may share the same position within one specific parallelism group. This is critical for coordinating work across all processes in the distributed training setup.
Example:
Assume the following:
tp_size = 4 (4 ranks for Tensor Parallelism).
dp_size = 2 (2 ranks for Data Parallelism).
pp_size = 3 (3 ranks for Pipeline Parallelism).
For a process with:
tp_rank = 1
dp_rank = 0
pp_rank = 2
The global rank would be calculated as:
global_rank = tp_rank + dp_rank * tp_size + pp_rank * tp_size * dp_size
= 1 + 0 * 4 + 2 * 4 * 2
= 1 + 0 + 16
= 17
This means this process would be assigned a unique global rank of 17 in the distributed system.
Summary:
The formula provides a systematic way to assign unique global ranks to processes based on their positions in the Tensor Parallel, Data Parallel, and Pipeline Parallel groups. It reflects the hierarchical structure of parallelism used in Megatron-LM, which enables efficient computation and communication for distributed training of large-scale models.