Weight Saving and Loading
For MindSpore Transformers dynamic graph (PyNative) training, weights are saved and loaded in Safetensors format. The framework centrally configures the saving and loading behavior in the checkpoint section (CheckpointConfig): The saving is triggered by CheckpointCallback of mindformers/pynative/callback/checkpoint_callback.py step by step during training, and the loading is completed by Trainer during the training startup phase. The underlying layers of the two modes call mindformers.checkpoint.save_checkpoint and mindformers.checkpoint.load_checkpoint, and use the common.json file in the weight directory to connect the resumable training.
Dynamic graph using only Safetensors
The dynamic graph path uses Safetensors as the only weight format. Format conversion between checkpoint and Safetensors is not involved, and no separate conversion script is required.
Terms
This document involves the following terms:
Weight loading
Only model parameters are loaded, and the optimizer state and training progress are not restored. It is commonly used in scenarios such as fine-tuning, distillation, and transfer learning.
Resumable training
It has the same meaning as resumable training, but emphasizes the resumption of the training process from a specific checkpoint.
Checkpoint
It is a model state file saved during training and is used to resume training or export the model.
Global step
It is the number of optimization steps that have been executed during training, which is used for learning rate scheduling, training resumption, and training statistics.
How to Choose Checkpoint Configurations
Select the saving and loading modes based on your actual requirements:
Do you need to resume training from the interruption point?
│
├── Yes
│ └── Use the entire segment for resumable training.
│ ├── Save the optimizer state.
│ ├── Save the training progress.
│ └── Use the resume configuration during loading.
│
└── No
│
├── Perform only for model inference.
│ └── Save only the weights.
│
└── Perform for fine-tuning a new task.
└── Load the model weights, but not the optimizer state.
Common scenario mapping
Scenario |
Recommendation Mode |
|---|---|
Recovery from training interruption |
Resuming training of the entire section |
Cluster fault recovery |
Resuming training of the entire section |
Model fine-tuning |
Weight loading |
Model inference and deployment |
Weight loading |
Model conversion and export |
Weight loading |
Selecting Fields by Scenario
The following table helps you quickly locate the fields that need to be paid attention to in each scenario. For details about the semantics, see the corresponding sections below.
Scenario |
Key Field |
Description |
|---|---|---|
Disabling saving (Only the loss is displayed and the weight is not saved.) |
|
Retains only the loss/monitor. No weight is saved. |
Basic periodic saving |
|
Saves by step and retains the latest several copies. |
Asynchronous saving |
|
Overlaps between flushing and computation, reducing saving blocking. |
Weight-only saving (without saving optimizer weights) |
|
Occupies smaller size. However, the optimizer state cannot be saved. |
Redundancy-free saving |
|
Performs multi-device segmentation and deduplication, reducing occupation. |
Multi-device layout cache |
|
Reuses shard metadata to avoid recomputing each time. |
Full resumable training |
|
Restores the weight, optimizer, step, and data cursor. |
Fine-tuning loading only weights |
|
Only the weight is loaded, and the optimizer starts from the beginning. |
Multi-device balanced loading |
|
Performs shard balancing + parameter broadcast, eliminating redundant parameter loading. This is valid only in multi-device sharding scenarios. |
Field Ownership
The fields for saving are consumed by
CheckpointCallback, and the fields for loading are consumed byTrainer._load_checkpoint. Both types of fields are written under the samecheckpointfield and do not affect each other.
Saving
Triggering Mechanism
The saving logic is all in CheckpointCallback. The key behaviors are as follows:
Whether to mount the callback:
Trainer._create_built_in_callbackschecksenable_save. Whenenable_saveis set toFalse,CheckpointCallbackis not constructed, and onlyLossCallbackandMonitorCallbackare retained. In this case, all other saving fields are invalid.Save by step:
on_step_endtriggers the save operation once atstate.global_step % save_interleaved_steps == 0.Supplementary save upon training completion:
on_train_endsaves an additional copy of the final weight when the training is complete to ensure that the final training result is not lost.Deduplication:
_last_triggered_stepis maintained internally. If the current step has the same name as the previously saved step, the previous step will be overwritten.Excess clearance:
save_maxspecifies the maximum number of copies that can be retained. If the number of copies exceeds the limit, the earliest directory is deleted by time. However, only the weights saved in the current training round are deleted.Path verification: If
save_pathis empty,ValueError("save_path must be provided for CheckpointCallback.")is thrown directly during callback construction. Therefore,save_pathmust be configured when saving is enabled.
Application scenarios: The saving function is used to persist model weights and optimizer states during training. Enable saving when resumable training, model deployment, or fine-tuning downstream tasks is required. If you only need to view training metrics (loss) and do not need to save weights, you can disable saving (enable_save=False) to skip the overhead of flushing weights to disk.
Output Directory Structure
Each time data is saved, a subdirectory named by step is generated in
save_path, containing:
Safetensors weight shards (model shards;
no_save_optimspecifies whether optimizer shards are included).
common.json: metadata of the fine-tuning (see Loading).Shard layout metadata
metadata.json.
Fields
Parameter |
Data Type |
Required/Optional |
Default Value |
Description |
|---|---|---|---|---|
|
Boolean |
Optional |
|
Specifies whether to enable weight saving. If this parameter is set to |
|
String |
Optional |
|
Specifies the save directory. This parameter is required when saving is enabled. If this parameter is left empty, |
|
int |
Optional |
|
Specifies the maximum number of weights to be saved. If the number of weights exceeds the maximum, the earliest weights are deleted by time. Only the weights saved for the current training are deleted each time. (Special scenario: If a checkpoint has been saved for the current step, the directory corresponding to the step is overwritten.) |
|
int |
Optional |
|
Specifies the number of steps between each save. The save is triggered when the number of steps is an integer multiple of |
|
Boolean |
Optional |
|
If this parameter is set to |
|
Boolean |
Optional |
|
If this parameter is set to |
|
String |
Optional |
|
Specifies the prefix of the saved file name. |
|
Boolean |
Optional |
|
If this parameter is set to |
|
Boolean |
Optional |
|
If this parameter is set to |
Scenario-Based Configuration
Scenario 1: Basic Periodic Saving
Most commonly used configuration: Save a replica of data at a fixed step interval and retain the latest several replicas. This configuration is suitable for most training tasks.
checkpoint:
enable_save: True
save_path: "./output/ckpt"
save_interleaved_steps: 1000 # Save every 1000 steps.
save_max: 5 # Retain a maximum of 5 replicas. If the number of replicas exceeds 5, the earliest replicas are deleted.
prefix: "checkpoint" # Prefix of the saved weight name.
no_save_optim: False # Save the optimizer state to facilitate resumable training (restoring the weight, optimizer state, and training progress).
async_save: False
remove_redundancy: False
save_global_layout_cache: True
Application scenarios: single-data-source and single-/multi-device common training. Cost: Each save operation blocks training until the data is flushed to the disk. If the number of steps is large and the save operation is frequent, you can use asynchronous saving.
Scenario 2: Asynchronous Saving (Reducing Saving Blocking)
When the weight is large and synchronous flushing significantly slows down the training, enable async_save. Before saving, the framework calls AsyncSaveManager.prepare_before_save to overlap the disk write operation with subsequent training computation.
checkpoint:
enable_save: True
save_path: "./output/ckpt"
save_interleaved_steps: 1000
save_max: 5
async_save: True # Enable asynchronous saving.
Applicable scenarios: LLMs, frequent saving, and slow disks. Cost: Saving is performed in the background, which consumes additional memory/thread resources. If the process exits unexpectedly, the latest asynchronous saving operation may not be completed.
Scenario 3: Weight-Only Saving (Without Saving Optimizer Weights)
When no_save_optim is set to True, only model weights are saved, significantly reducing the size. This is suitable for scenarios where only the weight product is required (for example, only fine-tuning is performed later).
checkpoint:
enable_save: True
save_path: "./output/ckpt"
save_interleaved_steps: 1000
save_max: 5
no_save_optim: True # Do not save optimizer weights.
⚠️ Impact on resumable training
If
no_save_optimis set toTrue, the saved weights do not contain optimizer weights and cannot be used for strict restoration of optimizer momentum and second-order moment during the entire resumable training process. If you want to resume training from this weight, useno_load_optim: Trueon the loading side.
Scenario 4: Redundancy-Free Saving (Reducing Memory Usage)
During multi-device training, duplicate weight shards exist between different ranks. If remove_redundancy is set to True, the redundant data is removed during saving to reduce disk usage.
checkpoint:
enable_save: True
save_path: "./output/ckpt"
save_interleaved_steps: 1000
save_max: 5
remove_redundancy: True # Remove redundancy between shards.
Application scenarios: multi-device sharding and insufficient disk space. Note that redundancy removal depends on the multi-device sharding metadata (
sharded_tensor_metas, which is always empty for a single device). This option does not take effect (is silently ignored) in single-device scenarios.
Scenario 5: Multi-device Layout Cache
In multi-device scenarios, the sharding metadata (sharded_tensor_metas) of each rank needs to be collected before saving. If save_global_layout_cache is set to True (default), the metadata is cached and reused in subsequent saving operations. If this parameter is set to False, the cache is cleared after each saving operation and recomputed next time.
checkpoint:
enable_save: True
save_path: "./output/ckpt"
save_interleaved_steps: 1000
save_max: 5
save_global_layout_cache: True # Cache the shard layout to accelerate subsequent saving.
Application scenarios: common multi-device training where the shard layout remains unchanged during training. Remaining
Truecan eliminate the overhead of recomputing each time. Set this parameter toFalseonly when the shard layout may change during training and needs to be re-collected each time. In single-device scenarios, sharding is not involved, and this field is not applicable.
Loading
Loading Process
Loading is performed during the Trainer.train startup phase. The weight loading directory is obtained from load_path. The core process is Trainer._load_checkpoint.
Reading metadata: Read
CommonInfofromload_path/common.json(see the following table).Restoring the step and data cursor: When the training is resumed for the entire segment (
no_load_optim=False), useglobal_stepincommon.jsonas the start point for resumable training. If the currentglobal_batch_sizeis different from the saved one, scaleglobal_step = global_step * (Original global_batch_size/new global_batch_size)by proportion, calltrain_dataset.set_init_step(global_step)to align the dataset cursor to the resumable training position, and write back the result tostate.global_step.Weight loading (automatic resharding): Call
load_checkpoint. Internally, UseReshardLoaderto process distributed resharding. Therefore, the parallel layout for saving can be different from that for loading. The framework automatically reshards to the current layout.(Optional) Balanced loading: When
load_balancedis set toTrue,apply_balance_shard_strategyis used to compute the redundant parameter mapping between ranks, and thensingle_parameter_broadcastis used to broadcast parameters between ranks. In this way, repetitive reading of redundant parameters is eliminated.(Optional) Optimizer master weight update: The optimizer is not loaded when
no_load_optimis set toTrue. After the loading is complete,optimizer.reload_main_params_from_model()is called to update the FP32 master weight using the newly loaded model parameters, ensuring that the master weight is aligned with the model parameters.
Application scenarios: The loading function is used to resume training from existing weights or initialize model parameters. This function is used when you need to resume training from the point where training was interrupted (resumable training for the entire section), fine-tune a new task based on pre-trained weights, or load weights for inference and deployment. The loading mode is specified by fields such as no_load_optim and load_balanced. You can configure the fields based on the scenario.
If
load_pathis empty andcheckpoint_pathis not passed, no weight is loaded, and training starts from random initialization.
Fields Recorded in common.json
Example of common.json
The typical content in common.json is as follows:
{
"epoch_num": 1,
"step_num": 100,
"global_step": 100,
"loss_scale": "1.0",
"global_batch_size": 2,
"ckpt_status": null
}
common.json is written by CommonInfo on the saving side (mindformers/checkpoint/checkpoint.py) and is used to restore the step and data cursor during resumable training.
Field |
Description |
Function in Resumable Training |
|---|---|---|
|
Current epoch of training. |
Metadata records. |
|
Step number in the current epoch. |
Metadata records. |
|
Total number of global training steps across epochs. |
Start point of resumable training; |
|
Gradient amplification coefficient. |
Metadata records. |
|
Global batch size for multi-device training. |
Specifies whether to scale |
|
Weight health status flag. |
Specifies whether the weight is healthy. The default value is |
Fields
Parameter |
Data Type |
Required/Optional |
Default Value |
Description |
|---|---|---|---|---|
|
String |
Optional |
|
Loading directory. If this parameter is left empty, no data is loaded and training starts from random initialization. |
|
Boolean |
Optional |
|
If this parameter is set to |
|
Boolean |
Optional |
|
If this parameter is set to |
load_balanced applies to the following scenarios:
The tensor parallelism (TP) scale is large.
The pipeline parallelism (PP) scale is large.
The data parallelism (DP) scale is large.
There are a large number of checkpoint files.
An obvious I/O bottleneck occurs in the loading phase.
Typical scenarios:
TP=8
PP=8
DP=16
In large-scale distributed training, checkpoint files are usually distributed on multiple storage nodes. After load_balanced is enabled, the loading pressure of some ranks can be reduced, improving the overall recovery efficiency.
⚠️ load_worker_number does not take effect in dynamic graphs.
Although
load_worker_number(default value:1) is declared inCheckpointConfig, the dynamic graph loading path does not consume this field. WhenTrainer._load_checkpointcallsload_checkpoint,reshard_worker_numis not passed, and the default value1is retained.Therefore, the current configuration
load_worker_numberdoes not change the loading parallelism. Do not rely on it to accelerate reading.
Scenario-Based Configuration
Scenario 1: Full Resumable Training
The weights, optimizer status, global_step, and dataset cursors are restored to seamlessly continue from the interruption. This is the default form of resumable training.
checkpoint:
load_path: "./output/ckpt/checkpoint_5000" # Specify the step directory where the model is saved.
no_load_optim: False # Load the optimizer status.
load_balanced: False
Application scenarios: resuming training in place after an interruption. Prerequisites:
no_save_optimhas been set toFalse(including the optimizer status) when the loaded weight is saved. For details about the overall resumable training behavior, see Resumable Training. For details about data cursor recovery, see Datasets.
Scenario 2: Fine-Tuning Weight-Only Loading
Only the model weight is loaded, and the optimizer is initialized from the beginning. This method is suitable for downstream fine-tuning starting from the pre-trained weight, without inheriting the optimizer momentum from the pre-training phase.
checkpoint:
load_path: "./pretrained/ckpt"
no_load_optim: True # Do not load the optimizer status.
When
no_load_optimis set toTrue,global_stepis not scaled or resumed based oncommon.json, and the training step starts from the configured start point. After the loading is complete, the framework automatically callsreload_main_params_from_model()to update the FP32 master weight to avoid misalignment between the master weight and model parameters.
Scenario 3: Multi-device Balanced Loading
When load_balanced is set to True, the framework uses the shard balancing strategy to compute the mapping of redundant parameters between ranks, and then uses parameter broadcast to ensure that each redundant parameter is read only once. This eliminates repeated loading of redundant parameters and reduces the graphics memory and I/O required for distributed loading.
checkpoint:
load_path: "./output/ckpt/checkpoint_5000"
no_load_optim: False
load_balanced: True # Cross-device balanced loading.
Applicable conditions
load_balancedusesapply_balance_shard_strategyfor shard redistribution, which is meaningful only in multi-device sharding scenarios. It does not bring benefits in single-device or non-sharding scenarios.For details about the relationship between the parallel dimension and sharding, see Distributed Parallel Training.
Complete Example: Resuming Training After an Interruption
Step 1: Configuration Saving
checkpoint:
enable_save: True
save_path: "./checkpoints"
save_interleaved_steps: 1000
no_save_optim: False
The following content is generated during training:
checkpoints/
├── iteration_00001000/
├── iteration_00002000/
└── latest_checkpointed_iteration.txt
Step 2: Training Interruption
Assume that the training exits due to a node fault at step 2300.
The latest checkpoint that is successfully saved is:
iteration_00002000
Step 3: Configuration Loading
checkpoint:
load_path: "./checkpoints"
Step 4: Restoration Process
Restart training. The system automatically performs the following operations:
Load the model weight.
Load the optimizer state.
Restore the random number state.
Restore global_step.
Restore the dataset reading position.
After the restoration is complete:
global_step = 2000
The training will continue from the position corresponding to the checkpoint, instead of starting from scratch.
Step 5: Training Continuation
2000 -> 2001 -> 2002 -> ...
The learning rate scheduling, optimizer momentum, and dataset cursor remain continuous.