Skip to content

Commit c8bc461

Browse files
authored
[megatron] support fp8 (#4730)
1 parent 1f8d908 commit c8bc461

File tree

7 files changed

+118
-21
lines changed

7 files changed

+118
-21
lines changed

docs/source/Instruction/Megatron-SWIFT训练.md

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,13 @@ I am a language model developed by swift, you can call me swift-robot. How can I
167167
- calculate_per_token_loss: 根据全局批次中的非填充token数量来对交叉熵损失进行缩放。默认为True。
168168
- 🔥attention_backend: 使用的注意力后端 (flash、fused、unfused、local、auto)。默认为 auto。
169169
- optimizer: 优化器类型,可选为'adam'、'sgd'。默认为adam。
170-
- optimizer_cpu_offload: 将优化器状态卸载到 CPU。默认为False。
170+
- 🔥optimizer_cpu_offload: 将优化器状态卸载到 CPU。默认为False。
171171
- optimizer_offload_fraction: 卸载到 CPU 的优化器状态所占比例。默认为1.。
172172
- use_precision_aware_optimizer: 使用 TransformerEngine 中的精度感知优化器,该优化器允许将主参数和优化器状态设置为较低精度,例如 fp16 和 fp8。
173+
- main_grads_dtype: 启用 use_precision_aware_optimizer 时主梯度的 dtype。可选为'fp32', 'bf16'。默认为'fp32'。
174+
- main_params_dtype: 启用 use_precision_aware_optimizer 时主参数的 dtype。可选为'fp32', 'fp16'。默认为'fp32'。
175+
- exp_avg_dtype: 启用 use_precision_aware_optimizer 时,adam 优化器中 exp_avg(即一阶矩)的 dtype。该 dtype 用于在训练过程中将优化器状态存储在内存中,但不会影响内核计算时的精度。可选为'fp32', 'fp16', 'bf16', 'fp8'。默认为'fp32'。
176+
- exp_avg_sq_dtype: 启用 use_precision_aware_optimizer 时,adam 优化器中 exp_avg_sq(即二阶矩)的 dtype。该 dtype 用于在训练过程中将优化器状态存储在内存中,但不会影响内核计算的精度。可选为'fp32', 'fp16', 'bf16', 'fp8'。默认为'fp32'。
173177
- dataloader_type: 默认为'cyclic',可选为'single', 'cyclic', 'external'。若开启`--streaming`,则设置为`external`
174178
- manual_gc: 禁用默认垃圾回收器,手动触发垃圾回收。默认为False。
175179
- manual_gc_interval: 触发垃圾回收的间隔。默认为0。
@@ -247,6 +251,13 @@ I am a language model developed by swift, you can call me swift-robot. How can I
247251
- 注意:若使用流式数据集,该值需要手动设置。
248252
- 🔥eval_interval: 评估的间隔(steps),默认为None,即设置为save_interval。
249253

254+
**fp8参数**:
255+
- fp8_format: 用于前向和反向传播中FP8张量的FP8格式方案。可选为'e4m3','hybrid'。默认为None。
256+
- fp8_recipe: 用于前向和反向传播中 FP8 张量的 FP8 算法方案。可选为'tensorwise', 'delayed', 'mxfp8', 'blockwise'。默认为'delayed'。
257+
- fp8_amax_history_len: 每个张量记录 amax 历史的步数。默认为1024。
258+
- fp8_amax_compute_algo: 用于根据历史记录计算 amax 的算法。可选为'most_recent','max'。默认为'max'。
259+
- fp8_param_gather: 保持计算参数为 fp8(不使用任何其他中间数据类型),并在 fp8 格式下执行参数的 all-gather 操作。默认为False。
260+
250261
**混合精度参数**:
251262
- fp16: fp16模式。默认为None,会根据模型的torch_dtype进行设置。torch_dtype默认读取config.json。
252263
- bf16: bf16模式。默认为None,会根据模型的torch_dtype进行设置。
@@ -281,21 +292,34 @@ I am a language model developed by swift, you can call me swift-robot. How can I
281292

282293
**MoE参数**:
283294
- num_experts: MoE的专家数,默认为None。自动从config.json读取。
284-
- moe_ffn_hidden_siz: 每个专家的前馈网络(ffn)的隐藏层大小。默认为None,设置为ffn_hidden_size。自动从config.json读取。
295+
- moe_layer_freq: MoE 层与 Dense 层之间的分布频率。默认为None。从config.json中读取。
296+
- moe_ffn_hidden_siz: 每个专家的前馈网络(ffn)的隐藏层大小。默认为None,自动从config.json读取。若未读取到且`num_experts`不为None,则设置为ffn_hidden_size。
285297
- moe_shared_expert_intermediate_size: 共享专家的总FFN隐藏层大小。如果有多个共享专家,它应等于 `num_shared_experts * ffn_size_of_each_shared_expert`。 默认为None。自动从config.json读取。
286298
- moe_router_topk: 每个token路由到的专家数量。默认为None。自动从config.json读取。
287299
- moe_router_pre_softmax: 为MoE启用预softmax路由,这意味着softmax会在top-k选择之前进行。默认为None。自动从config.json读取。
288-
- 🔥moe_aux_loss_coeff: 辅助损失的缩放系数:建议的初始值为 1e-2。默认为None。自动从config.json读取。
289-
- moe_router_dtype: 用于路由计算和专家输出加权平均的数据类型。可选为'none', 'fp32'、'fp64',这增强了数值稳定性,尤其是在专家数量较多时。与`moe_permute_fusion`一起使用时,性能影响可以忽略不计。默认为'fp32'。'none'代表不改变数据类型。
290-
- moe_permute_fusion: 在令牌分发过程中融合令牌重排操作。默认为False。
300+
- 🔥moe_router_dtype: 用于路由计算和专家输出加权平均的数据类型。可选为'none', 'fp32'、'fp64',这增强了数值稳定性,尤其是在专家数量较多时。与`moe_permute_fusion`一起使用时,性能影响可以忽略不计。默认为'fp32'。'none'代表不改变数据类型。
301+
- moe_router_score_function: MoE TopK 路由的评分函数。可以为 "softmax" 或 "sigmoid"。默认为None,从config.json中读取。
302+
- moe_router_bias_update_rate: 在无辅助损失负载均衡策略中,专家偏置的更新速率。专家偏置根据每个专家在全局批次中被分配的 token 数量进行更新,对于分配到的 token 较少的专家,偏置会增加;对于分配到的 token 较多的专家,偏置会减少。默认值 1e-3,与 DeepSeekV3 中使用的值相同。
303+
- moe_router_enable_expert_bias: 在无辅助损失负载均衡策略中,带有动态专家偏置的 TopK 路由。路由决策基于路由分数与专家偏置之和。详情请参见:https://arxiv.org/abs/2408.15664。默认为None,自动从config.json读取。
304+
- moe_router_topk_scaling_factor: 默认为None。从config.json中读取。
305+
- moe_router_load_balancing_type: 确定路由器的负载均衡策略。可选项为"aux_loss"、"seq_aux_loss"、"sinkhorn"、"none"。默认值为 None。从config.json中读取。
291306
- 🔥expert_model_parallel_size: 专家并行数,默认为1。
292307
- moe_token_dispatcher_type: 要使用的token分发器类型。可选选项包括 'allgather'、'alltoall'、'flex'和'alltoall_seq'。默认值为'alltoall'。
293308
- moe_enable_deepep: 实验性功能,启用DeepSeek/DeepEP以实现 MoE 模型中的高效令牌分发与组合。仅在设置`--moe_token_dispatcher_type flex`使用灵活令牌分发器时生效。
294-
- moe_grouped_gemm: 当每个rank包含多个专家时,通过在多个流中启动多个本地 GEMM 内核,利用 TransformerEngine中的GroupedLinear提高利用率和性能。默认为False。
295-
- moe_router_load_balancing_type: 确定路由器的负载均衡策略。可选项为"aux_loss"、"seq_aux_loss"、"sinkhorn"、"none"。默认值为 "aux_loss"。
309+
- 🔥moe_grouped_gemm: 当每个rank包含多个专家时,通过在多个流中启动多个本地 GEMM 内核,利用 TransformerEngine中的GroupedLinear提高利用率和性能。默认为False。
310+
- 🔥moe_permute_fusion: 在令牌分发过程中融合令牌重排操作。默认为False。
311+
- 🔥moe_aux_loss_coeff: 辅助损失的缩放系数:建议的初始值为 1e-2。默认为None。自动从config.json读取。
296312
- moe_z_loss_coeff: z-loss 的缩放系数。默认为None。
297-
- moe_expert_capacity_factor: 每个专家的容量因子,None表示不会丢弃任何token。默认为None。
298-
- moe_shared_expert_overlap: 启用共享专家计算与调度器通信之间的重叠。如果不启用此选项,共享专家将在路由专家之后执行。仅在设置了`moe_shared_expert_intermediate_size`时有效。默认为False。
313+
- moe_expert_capacity_factor: 每个专家的容量因子,None表示不会丢弃任何token。默认为None。自动从config.json读取。
314+
- 🔥moe_shared_expert_overlap: 启用共享专家计算与调度器通信之间的重叠。如果不启用此选项,共享专家将在路由专家之后执行。仅在设置了`moe_shared_expert_intermediate_size`时有效。默认为False。
315+
- moe_token_drop_policy: 可选为'probs', 'position'。默认为'probs'。
316+
317+
**mla参数**
318+
- multi_latent_attention: 是否使用MLA。默认为False。
319+
- q_lora_rank: Query 张量低秩表示的rank值。默认为None,自动从config.json读取。
320+
- kv_lora_rank: Key 和 Value 张量低秩表示的秩(rank)值。默认为None,自动从config.json读取。
321+
- qk_head_dim: QK 投影中 head 的维度。 `q_head_dim = qk_head_dim + qk_pos_emb_head_dim`。默认为None,自动从config.json读取。
322+
- qk_pos_emb_head_dim: QK 投影中位置嵌入的维度。默认为None,自动从config.json读取。
299323

300324
**DPO参数**:
301325
- ref_load: ref_model的加载路径。默认为None,即设置为`load`

docs/source_en/Instruction/Megatron-SWIFT-Training.md

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -169,9 +169,13 @@ The speed comparison of full-parameter training for Dense/MoE models using `mega
169169
- calculate_per_token_loss: Scales the cross-entropy loss according to the number of non-padded tokens in the global batch. Default is True.
170170
- 🔥attention_backend: The attention backend to use (flash, fused, unfused, local, auto). Defaults to auto.
171171
- optimizer: Optimizer type, options are 'adam', 'sgd'. Default is adam.
172-
- optimizer_cpu_offload: Offloads the optimizer state to CPU. Default is `False`.
172+
- 🔥optimizer_cpu_offload: Offloads the optimizer state to CPU. Default is `False`.
173173
- optimizer_offload_fraction: The fraction of the optimizer state to offload to CPU. Default is `1.0`.
174174
- use_precision_aware_optimizer: Use the precision-aware optimizer in TransformerEngine, which allows setting the main parameters and optimizer states to lower precision, such as fp16 and fp8.
175+
- main_grads_dtype: The dtype of main gradients when use_precision_aware_optimizer is enabled. Options are 'fp32' and 'bf16'. Default is 'fp32'.
176+
- main_params_dtype: The dtype of main parameters when use_precision_aware_optimizer is enabled. Options are 'fp32' and 'fp16'. Default is 'fp32'.
177+
- exp_avg_dtype: The dtype of exp_avg (i.e., the first moment in the Adam optimizer) when use_precision_aware_optimizer is enabled. This dtype is used for storing the optimizer state in memory during training, but does not affect the precision in kernel computation. Options are 'fp32', 'fp16', 'bf16', and 'fp8'. Default is 'fp32'.
178+
- exp_avg_sq_dtype: The dtype of exp_avg_sq (i.e., the second moment in the Adam optimizer) when use_precision_aware_optimizer is enabled. This dtype is used for storing the optimizer state in memory during training, but does not affect the precision in kernel computation. Options are 'fp32', 'fp16', 'bf16', and 'fp8'. Default is 'fp32'.
175179
- dataloader_type: Default is 'cyclic', options are 'single', 'cyclic', 'external'. If `--streaming` is enabled, set it to external.
176180
- manual_gc: Disables the default garbage collector and manually triggers garbage collection. Default is False.
177181
- manual_gc_interval: Interval at which garbage collection is triggered. Default is 0.
@@ -255,6 +259,15 @@ seq_length: Defaults to None, meaning it is set to `max_length`. To restrict the
255259
- Note: If using a streaming dataset, this value needs to be set manually.
256260
- 🔥eval_interval: Evaluation interval (steps), default is None, meaning it will be set to save_interval.
257261

262+
263+
**FP8 Parameters**:
264+
- fp8_format: The FP8 format scheme used for FP8 tensors in the forward and backward pass. Options are 'e4m3' and 'hybrid'. Default is None.
265+
- fp8_recipe: The FP8 recipe (algorithm scheme) used for FP8 tensors in the forward and backward pass. Options are 'tensorwise', 'delayed', 'mxfp8', and 'blockwise'. Default is 'delayed'.
266+
- fp8_amax_history_len: Number of steps for which amax history is recorded per tensor. Default is 1024.
267+
- fp8_amax_compute_algo: Algorithm for computing amax from history. Options are 'most_recent' and 'max'. Default is 'max'.
268+
- fp8_param_gather: Keep the compute parameter in FP8 (do not use any other intermediate dtype) and perform the parameter all-gather in FP8 format. Default is False.
269+
270+
258271
**Mixed Precision Parameters**:
259272

260273
- fp16: FP16 mode. The default is None, and it will be set according to the model's torch_dtype. The torch_dtype is read from the config.json by default.
@@ -292,21 +305,36 @@ seq_length: Defaults to None, meaning it is set to `max_length`. To restrict the
292305
**MoE Parameters**:
293306

294307
- num_experts: The number of experts in MoE, default is None. Automatically read from config.json.
295-
- moe_ffn_hidden_size: The hidden layer size of the feed-forward network (ffn) for each expert. Default is None, set to ffn_hidden_size. Automatically read from config.json.
308+
- moe_layer_freq: Frequency distribution between MoE layers and Dense layers. Default is None. This parameter is read from config.json.
309+
- moe_ffn_hidden_size: Hidden layer size of the feedforward network (ffn) for each expert. Default is None and will be automatically read from config.json. If not found and `num_experts` is not None, it will be set to ffn_hidden_size.
296310
- moe_shared_expert_intermediate_size: The total FFN hidden layer size for shared experts. If there are multiple shared experts, it should equal `num_shared_experts * ffn_size_of_each_shared_expert`. Default is None. Automatically read from config.json.
297311
- moe_router_topk: The number of experts each token is routed to. Default is None. Automatically read from config.json.
298312
- moe_router_pre_softmax: Enable pre-softmax routing for MoE, meaning that softmax will be applied before top-k selection. Default is None. Automatically read from config.json.
299-
- 🔥moe_aux_loss_coeff: Scaling coefficient for the auxiliary loss: the recommended initial value is 1e-2. Default is None. Automatically read from config.json.
300-
- moe_router_dtype: Data type for routing computation and expert output weighted averaging. Options include 'none', 'fp32' and 'fp64', which enhance numerical stability, particularly with a large number of experts. When used with `moe_permute_fusion`, the performance impact is negligible. The default is 'fp32'. 'none' means no change to the data type.
301-
- moe_permute_fusion: Fuses token rearrangement operations during token dispatching. Defaults to False.
313+
- 🔥moe_router_dtype: Data type used for routing computation and expert output weighted averaging. Options are 'none', 'fp32', and 'fp64', which enhances numerical stability, especially when the number of experts is large. When used together with `moe_permute_fusion`, the performance impact is negligible. Default is 'fp32'. 'none' means no change to data type.
314+
- moe_router_score_function: Scoring function for MoE TopK routing. Can be "softmax" or "sigmoid". Default is None and is read from config.json.
315+
- moe_router_bias_update_rate: Update rate of expert bias in the auxiliary-loss-free load balancing strategy. Expert bias is updated based on the number of tokens each expert is assigned in the global batch: bias increases for experts assigned fewer tokens, and decreases for those assigned more tokens. Default is 1e-3, same as used in DeepSeekV3.
316+
- moe_router_enable_expert_bias: TopK routing with dynamic expert bias in the auxiliary-loss-free load balancing strategy. Routing decisions are based on the sum of routing scores and expert bias. See details at: https://arxiv.org/abs/2408.15664. Default is None and is automatically read from config.json.
317+
- moe_router_topk_scaling_factor: Default is None. This parameter is read from config.json.
318+
- moe_router_load_balancing_type: Determines the router’s load balancing strategy. Options are "aux_loss", "seq_aux_loss", "sinkhorn", and "none". Default is None and is read from config.json.
302319
- 🔥expert_model_parallel_size: The degree of expert parallelism, default is 1.
303320
- moe_token_dispatcher_type: The type of token dispatcher to use. Options include 'allgather', 'alltoall', 'flex', and 'alltoall_seq'. Default is 'alltoall'.
304321
- moe_enable_deepep: Experimental feature, Enables DeepSeek/DeepEP for efficient token dispatching and combination in MoE models. Only works when using the flexible token dispatcher by setting `--moe_token_dispatcher_type flex`.
305-
- moe_grouped_gemm: When each rank contains multiple experts, improve utilization and performance by launching multiple local GEMM kernels across multiple streams using GroupedLinear in TransformerEngine. Default is False.
306-
- moe_router_load_balancing_type: Determines the load balancing strategy for the router. Options are "aux_loss", "seq_aux_loss", "sinkhorn", "none". Default is "aux_loss".
322+
- 🔥moe_grouped_gemm: When each rank contains multiple experts, multiple local GEMM kernels can be launched in parallel streams to improve utilization and performance by using GroupedLinear from TransformerEngine. Default is False.
323+
- 🔥moe_permute_fusion: Fuses token permutation operations during token dispatch. Default is False.
324+
- 🔥moe_aux_loss_coeff: Scaling coefficient for the auxiliary loss; a recommended initial value is 1e-2. Default is None and is automatically read from config.json.
307325
- moe_z_loss_coeff: Scaling coefficient for z-loss. Default is None.
308-
- moe_expert_capacity_factor: Capacity factor for each expert, None means no tokens will be dropped. Default is None.
309-
- moe_shared_expert_overlap: Enable overlapping of shared expert computation with scheduler communication. If this option is not enabled, shared experts will execute after the routing experts. Only effective when `moe_shared_expert_intermediate_size` is set. Default is False.
326+
- moe_expert_capacity_factor: Capacity factor for each expert. None means no token will be dropped. Default is None and will be automatically read from config.json.
327+
- 🔥moe_shared_expert_overlap: Enables overlap between shared expert computation and the dispatcher. If not enabled, shared expert computation will be performed after routing experts. Only effective when `moe_shared_expert_intermediate_size` is set. Default is False.
328+
- moe_token_drop_policy: Options are 'probs' and 'position'. Default is 'probs'.
329+
330+
**MLA Parameters**
331+
332+
- multi_latent_attention: Whether to use MLA. Default is False.
333+
- q_lora_rank: Low-rank representation rank value of the Query tensor. Default is None and will be automatically read from config.json.
334+
- kv_lora_rank: Low-rank representation rank value of the Key and Value tensors. Default is None and will be automatically read from config.json.
335+
- qk_head_dim: Dimension of the head in the QK projection. `q_head_dim = qk_head_dim + qk_pos_emb_head_dim`. Default is None and will be automatically read from config.json.
336+
- qk_pos_emb_head_dim: Dimension of the position embedding in the QK projection. Default is None and will be automatically read from config.json.
337+
310338

311339
**DPO Parameters**
312340
- ref_load: The path to load the reference model. Defaults to `None`, which means it will be set to `load`.

examples/train/megatron/dense/72b.sh

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# 8 * 65GiB. 80s/it
2+
PYTORCH_CUDA_ALLOC_CONF='expandable_segments:True' \
3+
NPROC_PER_NODE=8 \
4+
CUDA_VISIBLE_DEVICES=0,1,2,3,4,5,6,7 \
5+
megatron sft \
6+
--load Qwen2.5-72B-Instruct-mcore \
7+
--dataset 'liucong/Chinese-DeepSeek-R1-Distill-data-110k-SFT' \
8+
--tensor_model_parallel_size 8 \
9+
--micro_batch_size 1 \
10+
--global_batch_size 16 \
11+
--packing true \
12+
--recompute_granularity full \
13+
--recompute_method uniform \
14+
--recompute_num_layers 1 \
15+
--train_iters 10000 \
16+
--max_epochs 5 \
17+
--eval_iters 50 \
18+
--finetune true \
19+
--cross_entropy_loss_fusion true \
20+
--lr 1e-5 \
21+
--lr_warmup_iters 100 \
22+
--min_lr 1e-6 \
23+
--save megatron_output/Qwen2.5-72B-Instruct \
24+
--eval_interval 500 \
25+
--save_interval 500 \
26+
--max_length 8192 \
27+
--num_workers 8 \
28+
--dataset_num_proc 8 \
29+
--no_save_optim true \
30+
--no_save_rng true \
31+
--sequence_parallel true \
32+
--optimizer_cpu_offload true \
33+
--use_precision_aware_optimizer true \
34+
--attention_backend flash

0 commit comments

Comments
 (0)