# Training Guide [](https://atomgit.com/mindspore/docs/blob/master/docs/mindformers/docs/source_en/guide/training.md) ## Overview Pretraining is the core phase of building high-performance LLMs. The essence of pretraining is to enable models to learn general language rules and knowledge from massive amounts of unlabeled data. Many pretrained models (such as Llama, Qwen, and DeepSeek series models) with excellent metrics have been open-sourced in the industry. By learning the probability distributions of languages from massive text data, these models possess general capabilities such as vocabulary, grammar, and semantics, providing a solid foundation for downstream tasks (such as Q&A and writing). In essence, pretraining is performed to optimize model parameters through the backpropagation algorithm, thereby minimizing the loss function and improving the models' ability to predict or generate content based on the input data. MindSpore Transformers provides a unified pretraining training process, and provides easy-to-use solutions based on the ecosystem. In the unified training process, the key steps for starting a training task are as follows.  1. **Preparing for the task**: Determine the configurations of the model to be trained and prepare the training dataset. These two points are critical. 2. **Modifying training configurations**: Set configuration items based on the existing hardware resources, models, and data. The configuration items include basic, advanced, and high-level configurations. Different levels of configuration items allow training tasks to achieve different objectives. 3. **Starting a training task**: Start a training task across different cluster scales based on the existing hardware resources and training configurations. 4. **Monitoring training status**: After the task is executed, monitor the training status using various methods provided by MindSpore Transformers for subsequent debugging and optimization. The following describes the key processes of MindSpore Transformers in LLM pretraining tasks. > **Dynamic graph (PyNative) implementation as the mainline** > > Since **r2.0.0**, MindSpore Transformers has focused on **dynamic graph (PyNative) implementation** as the mainline of evolution. This document is oriented to dynamic graphs by default. Dynamic graphs focus on **pretraining** scenarios. For capabilities that are not covered by dynamic graphs, such as inference, service-oriented deployment, and quantization, see [Static Graph Features](../feature/static_graph_features.md). ## Training Process ### 1. Preparing for the Task #### Specifying the Model Specifications MindSpore Transformers supports different series of pretraining models, such as some typical specifications of DeepSeek and Qwen3 series. Currently, dynamic graphs (PyNative) support two types of model structures: **DeepSeek-V3** (MoE + MLA + MTP) and **Qwen3** (Dense), with the corresponding implementation located in `mindformers/models/*/modeling_*_pynative.py`. Other existing models are implemented as static graphs. For details, see [Model Support Library](../introduction/models.md). Dynamic graphs use a **layered abstraction + modular** design. `GPTModel` (general pretrained model) serves as the unified model interface, which combines modular interfaces such as `TransformerBlock`, `MoELayer`, `Attention`, `Linear`, `Embedding`, and `Norm` and uses the `ModuleSpec` mechanism to flexibly build models. The model structure and hyperparameters are explicitly configured in the `model` section of the YAML configuration file. (`model_type` and `architectures` are fixed fields, and other structure hyperparameters are transparently passed to the model class.) Currently, dynamic graphs do not support automatic combination of Hugging Face `config.json` through `pretrained_model_dir`. Structure parameters must be explicitly specified in the YAML file. For details about the overall structure of dynamic graphs, see [Overall Structure](../introduction/overview.md) #### Preprocessing the Dataset In natural language processing (NLP) tasks, data preprocessing is a key prerequisite for model training. It not only solves problems like noises and inconsistent formats (which contain special characters and garbled characters) in the raw data, but also converts the original text into a numerical form that can be understood by the model through structured conversion (such as tokenization and vectorization). Although general data preprocessing may include full-process operations such as collection, cleaning, and tokenization, the input data in this phase is assumed to have basic quality (that is, "clean" data). Therefore, the focus is on the core objective of **token conversion**. In dynamic graph mode, the dataset configuration is located in the `train_dataset` field of the YAML configuration file. Currently, the following two dataset loading modes are supported, covering common open-source and custom scenarios: - **Megatron dataset**: Datasets in the Megatron-LM format can be loaded, which is applicable to **pretraining** tasks of large-scale language models. - **MindRecord dataset**: MindRecord is an efficient data storage and reading module provided by MindSpore. It can convert different public datasets into the MindRecord format for training. For details about the processing and configuration, see [Datasets](../feature/dataset.md). **Processing Pretraining Data** For the Megatron dataset, MindSpore Transformers provides the data preprocessing script [preprocess_indexed_dataset.py](https://atomgit.com/mindspore/mindformers/blob/master/toolkit/data_preprocess/megatron/preprocess_indexed_dataset.py) to convert the original text corpus in `json` format into `.bin` or `.idx` files. This solution supports multi-source mixing. - **Flexible configuration**: Multiple bin data files can be loaded at the same time, and the sampling ratio parameter can be used to control the hybrid weight of different data sources. - **Efficient training**: The binary storage format greatly improves I/O efficiency, which is especially suitable for large-scale pretraining scenarios. After the preprocessing is complete, you can configure `BlendedMegatronDatasetDataLoader` to load the Megatron dataset for pretraining. For details, see [Datasets > Megatron Datasets](../feature/dataset.md#megatron-datasets). In addition, MindRecord datasets can be efficiently loaded and sampled from multiple sources using `MultiSourceDataLoader`. For details, see [Datasets > MindRecord Datasets](../feature/dataset.md#mindrecord-datasets). ### 2. Preparing Configuration Files Each pretraining task involves a high number of LLM parameters (usually ranging from billions to trillions). Distributed computing resources are required for efficient training and hyperparameter modification to ensure the normal execution of the task and the final performance metrics of the model. Dynamic graph (PyNative) training uses a YAML file to manage all configurable items in a centralized manner. The **dataclass configuration system** (TrainConfig and its sub-configuration classes) of [mindformers/pynative/config/config.py](https://atomgit.com/mindspore/mindformers/blob/master/mindformers/pynative/config/config.py) parses and verifies the YAML file when loading it. A complete configuration consists of the following top-level sections (for details, see [Configuration File Description](../feature/configuration.md)): - **Model configuration** (`model`): Modify the parameters related to the model architecture in the configuration file based on the predefined model specifications, such as the number of layers, number of heads, and hidden layer dimension. - **Data configuration** (`train_dataset`): Specify the dataset obtained after preprocessing, and configure the dataset path, data loading mode, and other necessary information. - **Hyperparameter training** (`training`, `optimizer`, and `lr_scheduler`): Specify the optimizer type, loss function, learning rate, data batch size, and number of training epochs based on the model training strategy. - **Parallelism strategy** (`parallelism`): Configure data parallelism (including FSDP/HSDP), tensor parallelism (TP), pipeline parallelism (PP), context parallelism (CP), and expert parallelism (EP) based on the cluster scale and model parameters to support ultra-large model training or performance optimization. - **Status monitoring** (`monitor` and `profiler`): Set the loss printing interval and configure profiling to collect performance data. In precision debugging tasks, configure the printing/visualization of key values to locate precision problems, such as local norm, local loss, and optimizer status. - **High availability** (`checkpoint`): Set the number of steps for saving weights, resumable training weights, balancing loading, and other HA features to ensure stable training. MindSpore Transformers classifies configurable parameters by configuration type and pre-training scenario, and describes the application scenarios and expected objectives of each layer. The following table describes the details.
| Configuration Type | Description | Configuration Item | Configuration Guide |
|---|---|---|---|
| Basic configurations | You can specify the corresponding configuration items to start a simple training task based on the current model structure. | Dataset | Dataset usage |
| Parallelism configurations |
Parallelism configuration items Distributed parallel training guide |
||
| Hyperparameter training |
Hyperparameters and optimizers for training Other training features (gradient accumulation/gradient clipping/operator fusion/hybrid precision) |
||
| High-level configurations | By configuring this part, you can detect the training status and ensure the continuous execution of multiple training tasks. | Weight saving |
Safetensors weight saving and loading Callbacks configuration > CheckpointMonitor |
| Resumable training | Examples for resumable training after breakpoint | ||
| Online monitoring | Training metrics monitoring and profiling | ||
| Advanced configurations | By specifying these configuration items, you can monitor the status of the training process and optimize performance to ensure stable and high-performance training across different cluster scales. | Performance optimization | Training memory optimization |
| Other training features | Gradient accumulation/gradient clipping/operator fusion/hybrid precision |