Absolutely, let's break down and explain the formula used in Megatron-LM:
Global Rank Calculation in Megatron-LM:
[
\text{global_rank} = \text{tp_rank} + \text{dp_rank} \times \text{tp_size} + \text{pp_rank} \times \text{tp_size} \times \text{dp_size}
]
Background:
Megatron-LM is designed to train large-scale language models efficiently by leveraging parallelism across multiple GPUs and nodes. To handle the immense computational load, Megatron-LM combines three types of parallelism:
- Tensor Parallelism (TP): Splits computations within layers across multiple GPUs.
- Data Parallelism (DP): Replicates the model across GPUs, each processing different mini-batches.
- Pipeline Parallelism (PP): Splits the model layers across different GPUs in a pipeline fashion.
Understanding how these parallelisms interact and how processes (or GPUs) are assigned roles is crucial. Each GPU participates in all three parallelism strategies and therefore has three ranks:
- tp_rank: Rank within the tensor parallel group (ranging from (0) to (\text{tp_size} - 1)).
- dp_rank: Rank within the data parallel group (ranging from (0) to (\text{dp_size} - 1)).
- pp_rank: Rank within the pipeline parallel group (ranging from (0) to (\text{pp_size} - 1)).
The Need for a Global Rank:
In distributed computing frameworks like MPI (Message Passing Interface), each process (or GPU) needs a unique global rank ranging from (0) to (N - 1), where (N) is the total number of processes.
Megatron-LM organizes GPUs in a 3D grid based on the three parallelism types. The formula provides a way to map the 3D grid coordinates ((\text{tp_rank}, \text{dp_rank}, \text{pp_rank})) to a unique global rank.
Breaking Down the Formula:
-
(\text{tp_rank}):
- Role: Identifies the GPU's position within the tensor parallel group.
- Contribution to (\text{global_rank}): It's the fastest changing index (like the innermost loop in nested loops).
- Range: (0 \leq \text{tp_rank} < \text{tp_size}).
-
(\text{dp_rank} \times \text{tp_size}):
- Role: Accounts for all the tensor parallel ranks within the current data parallel rank.
- Contribution: Each increment in (\text{dp_rank}) skips over one full set of tensor parallel ranks.
- Range: (0 \leq \text{dp_rank} < \text{dp_size}), multiplied by (\text{tp_size}).
-
(\text{pp_rank} \times \text{tp_size} \times \text{dp_size}):
- Role: Accounts for all combinations of tensor and data parallel ranks within the current pipeline parallel rank.
- Contribution: Each increment in (\text{pp_rank}) skips over all combinations of tensor and data parallel ranks.
- Range: (0 \leq \text{pp_rank} < \text{pp_size}), multiplied by (\text{tp_size} \times \text{dp_size}).
Visualization:
Imagine a 3D grid where:
- The X-axis represents (\text{tp_rank}) (tensor parallel ranks).
- The Y-axis represents (\text{dp_rank}) (data parallel ranks).
- The Z-axis represents (\text{pp_rank}) (pipeline parallel ranks).
Each point in this grid corresponds to a GPU/process.
Calculating the Global Rank:
- First, within each pipeline stage ((\text{pp_rank})), you have a group of data parallel groups.
- Within each data parallel group ((\text{dp_rank})), you have several tensor parallel ranks ((\text{tp_rank})).
- The formula linearizes this 3D arrangement into a single global rank.
Example:
Suppose we have:
- (\text{tp_size} = 2) (tensor parallel group size)
- (\text{dp_size} = 3) (data parallel group size)
- (\text{pp_size} = 4) (pipeline parallel group size)
- Total processes (N = \text{tp_size} \times \text{dp_size} \times \text{pp_size} = 2 \times 3 \times 4 = 24)
Let's calculate the global rank for a process with:
- (\text{tp_rank} = 1)
- (\text{dp_rank} = 2)
- (\text{pp_rank} = 3)
Using the formula:
[
\text{global_rank} = 1 + 2 \times 2 + 3 \times 2 \times 3 = 1 + 4 + 18 = 23
]
So, the process at position ((1, 2, 3)) in the (tp, dp, pp) grid has a global rank of 23.
Why This Mapping Works:
- Uniqueness: Each combination of ((\text{tp_rank}, \text{dp_rank}, \text{pp_rank})) maps to a unique (\text{global_rank}).
- Consistency: Processes with the same (\text{pp_rank}) and (\text{dp_rank}) but different (\text{tp_rank}) have consecutive global ranks.
- Grouping: Allows easy reconstruction of groupings for collective operations within TP, DP, and PP groups.
Implications in Megatron-LM:
- Efficient Communication: Knowing the global rank allows processes to perform collective communications within their respective parallel groups.
- Scalability: The formula ensures process ranks are assigned systematically, facilitating scaling to larger numbers of GPUs.
- Simplicity: Linearizing the 3D grid simplifies the management of processes in the distributed training environment.
Summary:
The formula in Megatron-LM for computing the global rank from the tensor, data, and pipeline parallel ranks provides a systematic way to assign unique identifiers to each process in a multi-dimensional parallelism setup. This mapping is essential for coordinating processes during training, especially for collective operations and data exchanges that occur within and across the different parallel groups.