Taotian Group and Aicheng Technology open source large model training framework Megatron-LLaMA

On September 12, Taotian Group and Aicheng Technology officially open sourced the large model training framework - Megatron-LLaMA, aiming to allow technology developers to more easily improve the training performance of large language models, reduce training costs, and maintain peace of mind. LLaMA community compatibility. Tests show that in 32-card training, Megatron-LLaMA can achieve 176% acceleration compared to the code version directly obtained from HuggingFace; in large-scale training, Megatron-LLaMA has almost linear scalability compared to 32 cards. And show high tolerance for network instability.

At present, Megatron-LLaMA has been launched in the open source community.

Open source address: https://github.com/alibaba/Megatron-LLaMA

1. Preface

The superior performance of large language models has exceeded people's imagination time and time again.

In the past few months, LLaMA and LLaMA2 have been fully opened to the open source community, giving people who want to train their own large language models a good choice. In the open source community, there are many models based on LLaMA output, including continuous training/SFT (such as Alpaca, Vicuna, WizardLM, Platypus, StableBegula, Orca, OpenBuddy, Linly, Ziya, etc.) and training from scratch ( Baichuan, QWen, InternLM, OpenLLaMA).

These works not only perform well on the current objective evaluation lists of various large model capabilities, but also demonstrate excellent performance in practical scenarios such as long text understanding, long text generation, code writing, and mathematical solving. Furthermore, many interesting products have appeared in the industry, such as LLaMA combined with Whisper's voice chat robot, LLaMA combined with Stable Diffusion's painting software, auxiliary consultation robots in the medical/legal field, etc.

Although you can get the LLaMA model code from HuggingFace, training an LLaMA model with your own data is not a low-cost and simple task for individual users or small and medium-sized organizations. The volume of large models and the scale of data make it impossible to complete effective training on ordinary computing resources, and computing power and cost have become serious bottlenecks. Users in the Megatron-LM community have very urgent demands in this regard.

picture

Taotian Group and Aicheng Technology have broad application scenarios for large models, and have invested heavily in efficient training of large models. The advent of LLaMA has given the industry a lot of inspiration in terms of data processing, model design, fine-tuning and reinforcement learning feedback adjustment, and has helped many businesses achieve new breakthroughs. Therefore, in order to give back to the entire LLaMA open source community, promote the development of the Chinese pre-trained large model open source community, and allow developers to more conveniently improve the training performance of large language models and reduce training costs, Taotian Group and Aicheng Technology will jointly transfer some internal The optimization technology is open sourced and Megatron-LLaMA is released, hoping to build the Megatron and LLaMA ecosystem with every collaborator and enthusiast.

Megatron-LLaMA provides a standard set of LLaMA implemented by Megatron-LM , and provides tools for free switching with the HuggingFace format to facilitate compatibility with community ecological tools. Megatron-LLaMA has redesigned the reverse process of Megatron-LM, so that it can be achieved no matter where the number of nodes is small and large gradient aggregation (GA) needs to be turned on, or when the number of nodes is large and small GA must be used. Excellent training performance. Test shows:

  • In 32-card training, compared to the code version obtained directly from HuggingFace, Megatron-LLaMA can achieve a 176% acceleration ; even with the version optimized by DeepSpeed ​​and FlashAttention, Megatron-LLaMA can still reduce the training time by at least 19%.

  • In large-scale training, Megatron-LLaMA has almost linear scalability compared to 32 cards . For example, using 512 A100 images to reproduce the training of LLaMA-13B, the reverse mechanism of Megatron-LLaMA can save at least two days compared to the DistributedOptimizer of the native Megatron-LM without any loss of accuracy.

  • Megatron-LLaMA exhibits high tolerance to network instability . Even on the current cost-effective 8xA100-80GB training cluster with 4x200Gbps communication bandwidth (this environment is usually a mixed-deployment environment, the network can only use half of the bandwidth, network bandwidth is a serious bottleneck, but the rental price is relatively low), Megatron-LLaMA can still achieve a linear expansion capability of 0.85, but Megatron-LM can only achieve less than 0.7 on this indicator.

2. High-performance LLaMA training opportunities brought by Megatron-LM technology

LLaMA is an important work in the current large language model open source community. LLaMA introduces optimization technologies such as BPE character encoding, RoPE position encoding, SwiGLU activation function, RMSNorm regularization and Untied Embedding into the structure of LLM, and has achieved excellent results in many objective and subjective evaluations. LLaMA provides 7B, 13B, 30B, 65B/70B versions, which are suitable for various large model demand scenarios and are also favored by the majority of developers. Like many open source large models, the official only provides the inference version of the code. There is no standard paradigm for how to carry out efficient training at the lowest cost.

Megatron-LM is an elegant high-performance training solution. Megatron-LM provides tensor parallelism ( Tensor Parallel, TP, which allocates large multiplications to multiple cards for parallel calculation ), pipeline parallelism ( Pipeline Parallel, PP, which allocates different layers of the model to different cards for processing ), and sequence parallelism ( Sequence Parallel, SP, different parts of the sequence are processed by different cards, saving video memory ), DistributedOptimizer optimization ( similar to DeepSpeed ​​Zero Stage-2, splitting gradients and optimizer parameters to all computing nodes ) and other technologies can significantly reduce video memory usage and improve GPU utilization. Megatron-LM operates an active open source community, and new optimization technologies and functional designs continue to be incorporated into the framework.

However, developing based on Megatron-LM is not simple, and debugging and functional verification on multiple cards is very expensive. Megatron-LLaMA launched by Taotian Group and Aicheng Technology first provides a set of LLaMA training code based on the Megatron-LM framework, supports model versions of various sizes, and can be easily adapted to various variants of LLaMA. Includes direct support for the HuggingFace format Tokenizer. Therefore, Megatron-LLaMA can be easily applied to existing offline training links without excessive adaptation. In the scenario of small and medium-scale training/fine-tuning of LLaMA-7b and LLaMA-13b, Megatron-LLaMA can easily achieve the industry-leading hardware utilization (MFU) of 54% and above.

3. Reverse process optimization of Megatron-LLaMA

3.1 DeepSpeed ZeRO Stage-2

picture

DeepSpeed ​​ZeRO is a distributed training framework launched by Microsoft, which has a very far-reaching technical impact. DeepSpeed ​​ZeRO Stage-2 ( hereinafter referred to as ZeRO-2 ) is one of the important technologies, which can save video memory usage without adding additional calculation and communication workload.

As shown in the figure above, due to calculation requirements, each Rank needs to have all parameters. But for the optimizer state, each Rank only needs to be responsible for a part of it, and it is not necessary for all Ranks to perform completely repeated operations at the same time. Therefore, ZeRO-2 proposes to evenly divide the optimizer state into each Rank ( note that there is no need to ensure that each variable is evenly divided or completely retained in a certain Rank ). Each Rank only needs to be divided during the training process. Responsible for updating the optimizer status and model parameters of the corresponding part. In this setting, gradients can also be split in this way.

By default, ZeRO-2 uses the Reduce method to aggregate gradients among all Ranks in the reverse direction, and then each Rank only needs to retain the part of the parameters it is responsible for, which not only eliminates redundant repeated calculations, but also reduces the memory usage.

3.2 Megatron-LM DistributedOptimizer

picture

The native Megatron-LM implements ZeRO-2-like gradient and optimizer state segmentation through DistributedOptimizer to reduce video memory usage during training.

As shown in the figure above, DistributedOptimizer uses the ReduceScatter operator to distribute all the previously accumulated gradients to different Ranks after obtaining all the gradients aggregated by the preset gradient. Each Rank only obtains part of the gradient that it needs to process, and then updates the optimizer status and corresponding parameters. Finally, each Rank obtains updated parameters from other nodes through AllGather, and finally obtains all parameters.

The actual training results show that the gradient and parameter communication of Megatron-LM are performed in series with other calculations. For large-scale pre-training tasks, in order to ensure that the total batch data size remains unchanged, it is usually impossible to open a larger GA. Therefore, the proportion of communication will increase with the increase of machines. At this time, the characteristics of serial communication lead to very weak scalability. Within the community, the need is also acute.

picture

3.3 Megatron-LLaMA OverlappedDistributedOptimizer

In order to solve the above problems, Megatron-LLaMA has improved the DistributedOptimizer of the native Megatron-LM so that its gradient communication operator can be parallelized with the calculation. In particular, compared to the implementation of ZeRO, Megatron-LLaMA uses a more scalable collective communication method to improve scalability through clever optimization of the optimizer partitioning strategy under the premise of parallelism.

The main design of OverlappedDistributedOptimizer ensures the following points:

  • The data volume of a single set communication operator is large enough to fully utilize the communication bandwidth;

  • The amount of communication data required by the new segmentation method should be equal to the minimum amount of communication data required for data parallelism;

  • During the conversion process of complete parameters or gradients and segmented parameters or gradients, too many video memory copies cannot be introduced.

picture

Specifically, Megatron-LLaMA improves the mechanism of DistributedOptimizer and proposes OverlappedDistributedOptimizer , which is used to optimize the reverse process in training in combination with the new segmentation method. As shown in the figure above, when OverlappedDistributedOptimizer is initialized, all parameters will be pre-allocated to the Bucket to which they belong. The parameters in the Bucket are complete. A parameter only belongs to one Bucket. There may be multiple parameters in a Bucket.

Logically, each Bucket will be continuously divided into P ( P is the number of data parallel groups ) equal parts, and each Rank in the data parallel group is responsible for one of them. Buckets are placed in a local queue ( Local grad bucket queue ) to ensure communication order. During training and calculation, the data parallel groups exchange the gradients they need through collective communication in Bucket units. The implementation of Bucket in Megatron-LLaMA uses address indexing as much as possible, and only newly allocates space when the required value changes, avoiding waste of video memory.

The above design, combined with a large number of engineering optimizations, allows Megatron-LLaMA to make full use of hardware during large-scale training, achieving better acceleration than the native Megatron-LM. When training from 32 A100 cards to 512 A100 cards, Megatron-LLaMA can still achieve an expansion ratio of 0.85 in a commonly used mixed network environment.

4. Future plans of Megatron-LLaMA

At present, Megatron-LLaMA has been widely used within Taotian Group and Aicheng Technology, and the team will continue to maintain and promote it after it is open sourced. As more and more developers flock to the open source community to exchange contributions, I believe there will be more challenges and opportunities at the training framework level in the future. Megatron-LLaMA will pay close attention to the development of the community and explore the following directions with developers:

  • Adaptive optimal configuration selection

  • More support for model structure or local design changes

  • The ultimate performance training solution in more different types of hardware environments

Welcome to try Megatron-LLaMA and start the exciting journey of efficiently domesticating LLaMA together!

project address:

https://github.com/alibaba/Megatron-LLaMA

Guess you like

Origin blog.csdn.net/AlibabaTech1024/article/details/132897352