mirror of
https://github.com/hpcaitech/ColossalAI.git
synced 2025-09-10 21:40:02 +00:00
[doc] migrate the markdown files (#2652)
This commit is contained in:
36
docs/source/en/concepts/colossalai_overview.md
Normal file
36
docs/source/en/concepts/colossalai_overview.md
Normal file
@@ -0,0 +1,36 @@
|
||||
# Colossal-AI Overview
|
||||
|
||||
Author: Shenggui Li, Siqi Mai
|
||||
|
||||
## About Colossal-AI
|
||||
|
||||
With the development of deep learning model size, it is important to shift to a new training paradigm. The traditional training method with no parallelism and optimization became a thing of the past and new training methods are the key to make training large-scale models efficient and cost-effective.
|
||||
|
||||
Colossal-AI is designed to be a unfied system to provide an integrated set of training skills and utilities to the user. You can find the common training utilities such as mixed precision training and gradient accumulation. Besides, we provide an array of parallelism including data, tensor and pipeline parallelism. We optimize tensor parallelism with different multi-dimensional distributed matrix-matrix multiplication algorithm. We also provided different pipeline parallelism methods to allow the user to scale their model across nodes efficiently. More advanced features such as offloading can be found in this tutorial documentation in detail as well.
|
||||
|
||||
## General Usage
|
||||
|
||||
We aim to make Colossal-AI easy to use and non-instrusive to user code. There is a simple general workflow if you want to use Colossal-AI.
|
||||
|
||||
<figure style={{textAlign: "center"}}>
|
||||
<img src="https://s2.loli.net/2022/01/28/ZK7ICWzbMsVuJof.png"/>
|
||||
<figcaption>Workflow</figcaption>
|
||||
</figure>
|
||||
|
||||
1. Prepare a configiguration file where specifies the features you want to use and your parameters.
|
||||
2. Initialize distributed backend with `colossalai.launch`
|
||||
3. Inject the training features into your training components (e.g. model, optimizer) with `colossalai.initialize`.
|
||||
4. Run training and testing
|
||||
|
||||
We will cover the whole workflow in the `basic tutorials` section.
|
||||
|
||||
## Future Development
|
||||
|
||||
The Colossal-AI system will be expanded to include more training skills, these new developments may include but are not limited to:
|
||||
|
||||
1. optimization of distributed operations
|
||||
2. optimization of training on heterogenous system
|
||||
3. implementation of training utilities to reduce model size and speed up training while preserving model performance
|
||||
4. expansion of existing parallelism methods
|
||||
|
||||
We welcome ideas and contribution from the community and you can post your idea for future development in our forum.
|
120
docs/source/en/concepts/distributed_training.md
Normal file
120
docs/source/en/concepts/distributed_training.md
Normal file
@@ -0,0 +1,120 @@
|
||||
# Distributed Training
|
||||
|
||||
Author: Shenggui Li, Siqi Mai
|
||||
|
||||
## What is a distributed system?
|
||||
|
||||
<figure style={{textAlign: "center"}}>
|
||||
<img src="https://s2.loli.net/2022/01/28/sE5daHf2ohIy9wX.png"/>
|
||||
<figcaption>Image source: <a href="https://towardsdatascience.com/distributed-training-in-the-cloud-cloud-machine-learning-engine-9e264ddde27f">Towards Data Science</a></figcaption>
|
||||
</figure>
|
||||
|
||||
A distributed system consists of multiple software components which run on multiple machines. For example, the traditional
|
||||
database runs on a single machine. As the amount of data gets incredibly large, a single machine can no longer deliver desirable
|
||||
performance to the business, especially in situations such as Black Friday where network traffic can be unexpectedly high.
|
||||
To handle such pressure, modern high-performance database is designed to run on multiple machines, and they work together to provide
|
||||
high throughput and low latency to the user.
|
||||
|
||||
One important evaluation metric for distributed system is scalability. For example, when we run an application on 4 machines,
|
||||
we naturally expect that the application can run 4 times faster. However, due to communication overhead and difference in
|
||||
hardware performance, it is difficult to achieve linear speedup. Thus, it is important to consider how to make the application
|
||||
faster when we implement it. Algorithms of good design and system optimization can help to deliver good performance. Sometimes,
|
||||
it is even possible to achieve linear and super-linear speedup.
|
||||
|
||||
|
||||
## Why we need distributed training for machine learning?
|
||||
|
||||
Back in 2012, [AlexNet](https://arxiv.org/abs/1404.5997) won the champion of the ImageNet competition, and it was trained
|
||||
on two GTX 580 3GB GPUs.
|
||||
Today, most models that appear in the top AI conferences are trained on multiple GPUs. Distributed training is definitely
|
||||
a common practice when researchers and engineers develop AI models. There are several reasons behind this trend.
|
||||
|
||||
1. Model size increases rapidly. [ResNet50](https://arxiv.org/abs/1512.03385) has 20 million parameters in 2015,
|
||||
[BERT-Large](https://arxiv.org/abs/1810.04805) has 345 million parameters in 2018,
|
||||
[GPT-2](https://d4mucfpksywv.cloudfront.net/better-language-models/language_models_are_unsupervised_multitask_learners.pdf)
|
||||
has 1.5 billion parameters in 2018, and [GPT-3](https://arxiv.org/abs/2005.14165) has 175 billion parameters in 2020.
|
||||
It is obvious that the model size grows exponentially with time. The current largest model has exceeded more than 1000
|
||||
billion parameters. Super large models generally deliver more superior performance compared to their smaller counterparts.
|
||||
<figure style={{textAlign: "center"}}>
|
||||
<img src="https://s2.loli.net/2022/01/28/sCyreJ9PF1EdZYf.jpg"/>
|
||||
<figcaption>Image source: <a href="https://huggingface.co/blog/large-language-models">HuggingFace</a></figcaption>
|
||||
</figure>
|
||||
|
||||
|
||||
2. Dataset size increases rapidly. For most machine learning developers, MNIST and CIFAR10 datasets are often the first few
|
||||
datasets on which they train their models. However, these datasets are very small compared to well-known ImageNet datasets.
|
||||
Google even has its own (unpublished) JFT-300M dataset which has around 300 million images, and this is close to 300 times
|
||||
larger than the ImageNet-1k dataset.
|
||||
|
||||
|
||||
3. Computing power gets stronger. With the advancement in the semiconductor industry, graphics cards become more and more
|
||||
powerful. Due to its larger number of cores, GPU is the most common compute platform for deep learning.
|
||||
From K10 GPU in 2012 to A100 GPU in 2020, the computing power has increased several hundred times. This allows us to performance
|
||||
compute-intensive tasks faster and deep learning is exactly such a task.
|
||||
|
||||
Nowadays, the model can be too large to fit into a single GPU, and the dataset can be large enough to train for a hundred
|
||||
days on a single GPU. Only by training our models on multiple GPUs with different parallelization techniques, we are able
|
||||
to speed up the training process and obtain results in a reasonable amount of time.
|
||||
|
||||
|
||||
## Basic Concepts in Distributed Training
|
||||
|
||||
Distributed training requires multiple machines/GPUs. During training, there will be communication among these devices.
|
||||
To understand distributed training better, there are several important terms to be made clear.
|
||||
|
||||
- host: host is the main device in the communication network. It is often required as an argument when initializing the
|
||||
distributed environment.
|
||||
- port: port here mainly refers to master port on the host for communication.
|
||||
- rank: the unique ID given to a device in the network.
|
||||
- world size: the number of devices in the network.
|
||||
- process group: a process group is a communication network which include a subset of the devices. There is always a default
|
||||
process group which contains all the devices. A subset devices can form a process group so that they only communicate among
|
||||
the devices within the group.
|
||||
|
||||
<figure style={{textAlign: "center"}}>
|
||||
<img src="https://s2.loli.net/2022/01/28/qnNBKh8AjzgM5sY.png"/>
|
||||
<figcaption>A distributed system example</figcaption>
|
||||
</figure>
|
||||
|
||||
To illustrate these concepts, let's assume we have 2 machines (also called nodes), and each machine has 4 GPUs. When we
|
||||
initialize distributed environment over these two machines, we essentially launch 8 processes (4 processes on each machine)
|
||||
and each process is bound to a GPU.
|
||||
|
||||
Before initializing the distributed environment, we need to specify the host (master address) and port (master port). In
|
||||
this example, we can let host be node 0 and port be a number such as 29500. All the 8 processes will then look for the
|
||||
address and port and connect to one another.
|
||||
The default process group will then be created. The default process group has a world size of 8 and details are as follows:
|
||||
|
||||
| process ID | rank | Node index | GPU index |
|
||||
| ---------- | ---- | ---------- | --------- |
|
||||
| 0 | 0 | 0 | 0 |
|
||||
| 1 | 1 | 0 | 1 |
|
||||
| 2 | 2 | 0 | 2 |
|
||||
| 3 | 3 | 0 | 3 |
|
||||
| 4 | 4 | 1 | 0 |
|
||||
| 5 | 5 | 1 | 1 |
|
||||
| 6 | 6 | 1 | 2 |
|
||||
| 7 | 7 | 1 | 3 |
|
||||
|
||||
|
||||
We can also create a new process group. This new process group can contain any subset of the processes.
|
||||
For example, we can create one containing only even-number processes, and the details of this new group will be:
|
||||
|
||||
| process ID | rank | Node index | GPU index |
|
||||
| ---------- | ---- | ---------- | --------- |
|
||||
| 0 | 0 | 0 | 0 |
|
||||
| 2 | 1 | 0 | 2 |
|
||||
| 4 | 2 | 1 | 0 |
|
||||
| 6 | 3 | 1 | 2 |
|
||||
|
||||
**Please note that rank is relative to the process group and one process can have a different rank in different process
|
||||
groups. The max rank is always `world size of the process group - 1`.**
|
||||
|
||||
In the process group, the processes can communicate in two ways:
|
||||
1. peer-to-peer: one process send data to another process
|
||||
2. collective: a group of process perform operations such as scatter, gather, all-reduce, broadcast together.
|
||||
|
||||
<figure style={{textAlign: "center"}}>
|
||||
<img src="https://s2.loli.net/2022/01/28/zTmlxgc3oeAdn97.png"/>
|
||||
<figcaption>Collective communication, source: <a href="https://pytorch.org/tutorials/intermediate/dist_tuto.html">PyTorch distributed tutorial</a></figcaption>
|
||||
</figure>
|
123
docs/source/en/concepts/paradigms_of_parallelism.md
Normal file
123
docs/source/en/concepts/paradigms_of_parallelism.md
Normal file
@@ -0,0 +1,123 @@
|
||||
# Paradigms of Parallelism
|
||||
|
||||
Author: Shenggui Li, Siqi Mai
|
||||
|
||||
## Introduction
|
||||
|
||||
With the development of deep learning, there is an increasing demand for parallel training. This is because that model
|
||||
and datasets are getting larger and larger and training time becomes a nightmare if we stick to single-GPU training. In
|
||||
this section, we will provide a brief overview of existing methods to parallelize training. If you wish to add on to this
|
||||
post, you may create a discussion in the [GitHub forum](https://github.com/hpcaitech/ColossalAI/discussions).
|
||||
|
||||
## Data Parallel
|
||||
|
||||
Data parallel is the most common form of parallelism due to its simplicity. In data parallel training, the dataset is split
|
||||
into several shards, each shard is allocated to a device. This is equivalent to parallelize the training process along the
|
||||
batch dimension. Each device will hold a full copy of the model replica and trains on the dataset shard allocated. After
|
||||
back-propagation, the gradients of the model will be all-reduced so that the model parameters on different devices can stay
|
||||
synchronized.
|
||||
|
||||
<figure style={{textAlign: "center"}}>
|
||||
<img src="https://s2.loli.net/2022/01/28/WSAensMqjwHdOlR.png"/>
|
||||
<figcaption>Data parallel illustration</figcaption>
|
||||
</figure>
|
||||
|
||||
## Model Parallel
|
||||
|
||||
In data parallel training, one prominent feature is that each GPU holds a copy of the whole model weights. This brings
|
||||
redundancy issue. Another paradigm of parallelism is model parallelism, where model is split and distributed over an array
|
||||
of devices. There are generally two types of parallelism: tensor parallelism and pipeline parallelism. Tensor parallelism is
|
||||
to parallelize computation within an operation such as matrix-matrix multiplication. Pipeline parallelism is to parallelize
|
||||
computation between layers. Thus, from another point of view, tensor parallelism can be seen as intra-layer parallelism and
|
||||
pipeline parallelism can be seen as inter-layer parallelism.
|
||||
|
||||
### Tensor Parallel
|
||||
|
||||
Tensor parallel training is to split a tensor into `N` chunks along a specific dimension and each device only holds `1/N`
|
||||
of the whole tensor while not affecting the correctness of the computation graph. This requires additional communication
|
||||
to make sure that the result is correct.
|
||||
|
||||
Taking a general matrix multiplication as an example, let's say we have C = AB. We can split B along the column dimension
|
||||
into `[B0 B1 B2 ... Bn]` and each device holds a column. We then multiply `A` with each column in `B` on each device, we
|
||||
will get `[AB0 AB1 AB2 ... ABn]`. At this moment, each device still holds partial results, e.g. device rank 0 holds `AB0`.
|
||||
To make sure the result is correct, we need to all-gather the partial result and concatenate the tensor along the column
|
||||
dimension. In this way, we are able to distribute the tensor over devices while making sure the computation flow remains
|
||||
correct.
|
||||
|
||||
<figure style={{textAlign: "center"}}>
|
||||
<img src="https://s2.loli.net/2022/01/28/2ZwyPDvXANW4tMG.png"/>
|
||||
<figcaption>Tensor parallel illustration</figcaption>
|
||||
</figure>
|
||||
|
||||
In Colossal-AI, we provide an array of tensor parallelism methods, namely 1D, 2D, 2.5D and 3D tensor parallelism. We will
|
||||
talk about them in detail in `advanced tutorials`.
|
||||
|
||||
|
||||
Related paper:
|
||||
- [GShard: Scaling Giant Models with Conditional Computation and Automatic Sharding](https://arxiv.org/abs/2006.16668)
|
||||
- [Megatron-LM: Training Multi-Billion Parameter Language Models Using Model Parallelism](https://arxiv.org/abs/1909.08053)
|
||||
- [An Efficient 2D Method for Training Super-Large Deep Learning Models](https://arxiv.org/abs/2104.05343)
|
||||
- [2.5-dimensional distributed model training](https://arxiv.org/abs/2105.14500)
|
||||
- [Maximizing Parallelism in Distributed Training for Huge Neural Networks](https://arxiv.org/abs/2105.14450)
|
||||
|
||||
### Pipeline Parallel
|
||||
|
||||
Pipeline parallelism is generally easy to understand. If you recall your computer architecture course, this indeed exists
|
||||
in the CPU design.
|
||||
|
||||
<figure style={{textAlign: "center"}}>
|
||||
<img src="https://s2.loli.net/2022/01/28/at3eDv7kKBusxbd.png"/>
|
||||
<figcaption>Pipeline parallel illustration</figcaption>
|
||||
</figure>
|
||||
|
||||
The core idea of pipeline parallelism is that the model is split by layer into several chunks, each chunk is
|
||||
given to a device. During the forward pass, each device passes the intermediate activation to the next stage. During the backward pass,
|
||||
each device passes the gradient of the input tensor back to the previous pipeline stage. This allows devices to compute simultaneously,
|
||||
and increases the training throughput. One drawback of pipeline parallel training is that there will be some bubble time where
|
||||
some devices are engaged in computation, leading to waste of computational resources.
|
||||
|
||||
<figure style={{textAlign: "center"}}>
|
||||
<img src="https://s2.loli.net/2022/01/28/sDNq51PS3Gxbw7F.png"/>
|
||||
<figcaption>Source: <a href="https://arxiv.org/abs/1811.06965">GPipe</a></figcaption>
|
||||
</figure>
|
||||
|
||||
Related paper:
|
||||
- [PipeDream: Fast and Efficient Pipeline Parallel DNN Training](https://arxiv.org/abs/1806.03377)
|
||||
- [GPipe: Efficient Training of Giant Neural Networks using Pipeline Parallelism](https://arxiv.org/abs/1811.06965)
|
||||
- [Megatron-LM: Training Multi-Billion Parameter Language Models Using Model Parallelism](https://arxiv.org/abs/1909.08053)
|
||||
- [Chimera: Efficiently Training Large-Scale Neural Networks with Bidirectional Pipelines](https://arxiv.org/abs/2107.06925)
|
||||
|
||||
|
||||
## Optimizer-Level Parallel
|
||||
|
||||
Another paradigm works at the optimizer level, and the current most famous method of this paradigm is ZeRO which stands
|
||||
for [zero redundancy optimizer](https://arxiv.org/abs/1910.02054). ZeRO works at three levels to remove memory redundancy
|
||||
(fp16 training is required for ZeRO):
|
||||
|
||||
- Level 1: The optimizer states are partitioned across the processes
|
||||
- Level 2: The reduced 32-bit gradients for updating the model weights are also partitioned such that each process
|
||||
only stores the gradients corresponding to its partition of the optimizer states.
|
||||
- Level 3: The 16-bit model parameters are partitioned across the processes
|
||||
|
||||
Related paper:
|
||||
- [ZeRO: Memory Optimizations Toward Training Trillion Parameter Models](https://arxiv.org/abs/1910.02054)
|
||||
|
||||
|
||||
## Parallelism on Heterogeneous System
|
||||
|
||||
The methods mentioned above generally require a large number of GPU to train a large model. However, it is often neglected
|
||||
that CPU has a much larger memory compared to GPU. On a typical server, CPU can easily have several hundred GB RAM while each GPU
|
||||
typically only has 16 or 32 GB RAM. This prompts the community to think why CPU memory is not utilized for distributed training.
|
||||
|
||||
Recent advances rely on CPU and even NVMe disk to train large models. The main idea is to offload tensors back to CPU memory
|
||||
or NVMe disk when they are not used. By using the heterogeneous system architecture, it is possible to accommodate a huge
|
||||
model on a single machine.
|
||||
|
||||
<figure style={{textAlign: "center"}}>
|
||||
<img src="https://s2.loli.net/2022/01/28/qLHD5lk97hXQdbv.png"/>
|
||||
<figcaption>Heterogenous system illustration</figcaption>
|
||||
</figure>
|
||||
|
||||
Related paper:
|
||||
- [ZeRO-Infinity: Breaking the GPU Memory Wall for Extreme Scale Deep Learning](https://arxiv.org/abs/2104.07857)
|
||||
- [PatrickStar: Parallel Training of Pre-trained Models via Chunk-based Memory Management](https://arxiv.org/abs/2108.05818)
|
Reference in New Issue
Block a user