Sequence parallelism is a technique used in high-performance computing, particularly in the context of accelerating large language models and other sequential tasks. It helps to distribute the workload across multiple processing units, such as GPUs or TPUs, to speed up computation. There are several variations of sequence parallelism, each with its own advantages and use cases. Here are the main variations:
1. 1D Sequence Parallelism
Description:
In 1D sequence parallelism, the sequence (e.g., a text input for a language model) is split into contiguous chunks, and each chunk is processed by a different processing unit.
How it works:
- Sequence Splitting: The input sequence is divided into equal-sized or nearly equal-sized chunks.
- Distribution: Each chunk is assigned to a different GPU/TPU.
- Processing: Each processing unit independently processes its assigned chunk.
- Aggregation: The results from each unit are combined to form the final output.
Advantages:
- Simple to implement.
- Reduces memory requirements per device since each device only needs to store and process a portion of the sequence.
Disadvantages:
- Limited scalability because the sequence length is fixed, and only the number of devices can be increased.
- Inter-device communication is required to combine results, which can introduce overhead.
2. 2D Sequence Parallelism
Description:
2D sequence parallelism involves splitting the sequence in two dimensions. This is often used in conjunction with tensor parallelism.
How it works:
- Sequence Splitting: The input sequence is split into chunks, and each chunk is further split into smaller sub-chunks.
- Distribution: The sub-chunks are distributed across a 2D grid of processing units.
- Processing: Each processing unit processes its assigned sub-chunk.
- Aggregation: Results are combined in two stages: first within rows and then within columns of the 2D grid.
Advantages:
- Better scalability than 1D sequence parallelism.
- More efficient use of memory and computational resources.
Disadvantages:
- More complex to implement.
- Increased communication overhead due to the two-stage aggregation process
3. Pipeline Parallelism (also known as Vertical Parallelism)
Description:
Pipeline parallelism divides the model layers rather than the sequence. Each processing unit is responsible for one or more layers of the model. The input sequence is passed through the pipeline, with each unit processing its layers and passing the intermediate results to the next unit.
How it works:
- Layer Splitting: The model's layers are divided among multiple processing units.
- Pipelining: The input sequence is fed into the first unit, which processes its layers and passes the intermediate results to the next unit. This continues until the sequence has passed through all units.
- Processing: Each unit processes its assigned layers and passes the output to the next unit in the pipeline.
- Aggregation: The final output is produced after the sequence has passed through all units.
Advantages:
- Highly scalable, as you can add more devices to handle deeper models.
- Reduces memory requirements per device since each device only needs to store and process a subset of the model’s layers.
Disadvantages:
- Latency can be an issue because each device must wait for the previous one to finish before it can start processing.
- Imbalance in computational load among devices if layers have different complexities.
- Requires careful scheduling to minimise idle time.
4. Tensor Parallelism
Description:
Tensor parallelism involves splitting the weights of the model's tensors across multiple processing units. This is often used in combination with sequence parallelism.
How it works:
- Tensor Splitting: The model's weight tensors are split along one or more dimensions.
- Distribution: Each processing unit gets a portion of the tensor.
- Processing: During forward and backward passes, each unit processes its portion of the tensor.
- Aggregation: Gradients and activations are combined across devices.
Advantages:
- Allows for training and inference of extremely large models that would not fit into the memory of a single device.
- Efficient use of memory and computational resources.
Disadvantages:
- Significant communication overhead, as devices need to frequently exchange parts of tensors.
- Complex to implement, especially when combined with other parallelism techniques.
- Load balancing can be challenging if tensor dimensions are not evenly divisible by the number of devices.
5. Hybrid Parallelism
Description:
Hybrid parallelism combines two or more of the above parallelism techniques to optimise both memory usage and computational efficiency.
How it works:
- Combination: For example combining 1D sequence parallelism with tensor parallelism. The sequence is split across devices (1D sequence parallelism), and within each device the model's tensors are also split (tensor parallelism)
- Distribution: The workload is distributed according to the combined strategies.
- Processing: Each device processes its assigned portion of the sequence and tensors.
- Aggregation: Results are combined according to the rules of each parallelism technique involved.
Advantages:
- Maximises resource utilisation and scalability.
- Can handle extremely large models and sequences.
Disadvantages:
- Very complex to implement and optimise.
- High communication overhead due to the multiple dimensions of parallelism.
- Requires careful planning to balance the load across devices.
Choosing the Right Variation
The choice of sequence parallelism variation depends on several factors:
- Model Size: Larger models may require tensor or hybrid parallelism.
- Sequence Length: Very long sequences might benefit from 1D or 2D sequence parallelism.
- Hardware Availability: The number and type of available processing units will influence the choice.
- Memory Constraints: Techniques that reduce per-device memory requirements, like pipeline and tensor parallelism, are useful when memory is limited.
- Latency Requirements: Pipeline parallelism may introduce latency, so it might not be suitable for applications requiring low-latency responses.
In practice, many modern frameworks and libraries, such as Megatron-LM, DeepSpeed and Transformer Engine, provide built-in support for these parallelism techniques, making it easier to implement and experiment with different variations.