Skip to Content
MS-SWIFT × TWINKLE
训练中间件
COMPONENTSMS-SWIFT × TWINKLE

Padding-Free 训练

Padding-free(打包)把多条序列拼接成一行送入模型,用 position_ids 标记序列边界(每条序列从 0 重新计数),从而不在 padding token 上做无效计算。

填充方式:   [tok tok tok PAD PAD PAD]  [tok tok PAD PAD PAD PAD]
打包方式:   [tok tok tok tok tok ...]   ← 一行内多条序列,无 padding

两种启用方式

模型侧:把 InputProcessor 的 padding_free 打开,只拼接当前 batch 内的样本。

from twinkle.processor import InputProcessor

model.set_processor(InputProcessor, padding_free=True)

数据集侧:用 PackingDataset / IterablePackingDataset 按 max_length 装箱,使各 batch 长度接近。

from twinkle.dataset import PackingDataset, DatasetMeta

dataset = PackingDataset(DatasetMeta(...), packing_num_proc=8)
dataset.set_template('Qwen3_5Template', max_length=2048)   # 装箱目标长度取 template.max_length
dataset.map(...)
dataset.encode()
dataset.pack_dataset()

装箱在 encode 之后进行,并写入 position_ids。InputProcessor 检测到一行内 position_ids 多次归零(_is_packed_position_ids)就按 packed 批次处理,因此数据集侧装箱不需要再显式打开 padding_free。

Megatron 后端下 padding_free=True 要求 variable_seq_lengths=True,否则 set_processor 抛 ValueError:打包后 batch 维为 1,定长 micro batch 切分无法进行。

边界信息如何传到 kernel

InputProcessor 从 position_ids 中 0 的位置推出累积序列长度(_get_packed_seq_params):

  • transformers + GatedDeltaNet:写入 cu_seq_lens_q、cu_seq_lens_k、max_length_q、max_length_k
  • megatron:写入 packed_seq_params(qkv_format='thd')

序列并行、上下文并行或 fp8/fp4 对齐(seq_align_to)需要补齐长度时,按段或按整行补 pad,补出的 token 带 labels=-100、loss_scale=0,不参与损失;补齐发生在推导 cu_seqlens 之前,所以 packed 参数描述的是补齐后的布局。

注意力后端要求

打包行必须使用 FlashAttention 家族后端(attn_implementation 名字含 flash_attention 或 flash_attn,如 flash_attention_2、flash_attention_3)。序列边界只经 cu_seqlens 交给 FA 的 varlen kernel;twinkle 不为 transformers 的打包行构造块对角 4D mask,SDPA / eager 拿到的仍是整行的因果 mask,拼接后的各条序列会互相注意,结果不正确。

注意力后端打包批次
flash_attention_2 / flash_attention_3支持
sdpa不支持
eager不支持

代码中的显式守卫:

  • GatedDeltaNet 模型(未启用序列并行):非 FA 后端直接抛 RuntimeError
  • 序列并行 + 打包输入:要求 FA2/3,否则抛 NotImplementedError;SDPA 后端遇到 padding_free 直接抛 RuntimeError

GatedDeltaNet 补丁(Qwen3.5 / Qwen4Exp)

Qwen3.5 是标准注意力与 GatedDeltaNet 线性注意力的混合架构。原生 torch 版 GatedDeltaNet 不在打包边界重置线性注意力状态,前一条序列的状态会泄漏到后一条。

GatedDeltaNetPaddingFreePatch(twinkle.patch.gdn_padding_free)按层的 linear_attn 类型识别目标,不看模型名,支持 Qwen3_5GatedDeltaNet、Qwen3_5MoeGatedDeltaNet、Qwen4ExpTextGatedDeltaNet。模型 wrap 之前由 _ensure_gdn_padding_free_patch 自动应用,启用序列并行时跳过。

补丁做两件事:

  1. 替换 decoder layer 的 forward:当该层 layer_type == 'linear_attention' 且输入带 cu_seq_lens_q 时,把它传进 linear_attn,其余层走原路径
  2. 替换 GatedDeltaNet 的 forward:调用期间把 causal_conv1d_fn 与 chunk_gated_delta_rule 换成带 cu_seqlens 的 flash-linear-attention kernel 包装,调用结束在 finally 中还原;同时剔除 cu_seq_lens_* / max_length_*,避免 FA 的元数据泄漏给 FLA

Qwen4ExpTextGatedDeltaNet 原生转发 varlen 参数,只 patch GatedDeltaNet 本身,不改 decoder layer。

要求

  • 需安装 flash-linear-attention,缺失时抛 NotImplementedError(提示安装该包或关闭 padding_free/packing)
  • NPU 上改用 twinkle.kernel.ops.fla.npu 的 npu_causal_conv1d_fn(模块带 _twinkle_npu_patched 标记时生效)
  • 启用序列并行时由 Qwen3_5GatedDeltaNetUlyssesPatch 接管;打包的 Qwen3.5 序列并行需要 FLA 或受支持的原生 GDN kernel,否则抛 NotImplementedError