已合并
feat: support configurable LoRA weight saving #3109
feat: support configurable LoRA weight saving #3109
已合并
LKONE创建于 27 天前
LKONE
LKONE成员
27 天前

What this PR does / why we need it?

  1. 新增 lora_save_only 参数用于控制LoRA场景下的权重保存行为,开启时仅保存LoRA部分的权重和配置文件,用于单独将 LoRA 权重与基础权重进行合并;关闭时根据 save_format 存储全参权重;
  2. 关闭 lora_save_only 时,若 save_format 为 dcp 格式,则分开存储基础权重和 LoRA 权重,主要用于断点续训场景;若 save_format 为 hf 格式,则将基础权重与 LoRA 权重进行融合后保存,跳过离线转换过程,主要用于导出进行推理的场景(注:仅支持已适配 hf 权重转换的模型);
  3. 调整 hf 权重保存中保存精度 save_ckpt_dtype 参数生效的时间点,改由 LoRA 权重合并之后进行,使融合过程中 LoRA 部分的权重精度更高;
  4. save_ckpt_dtype 参数支持对 lora_save_only 场景下保存的 LoRA 权重生效;
  5. 调整 LoRA 权重保存路径,仿照基础权重保存方式,在原先根目录保存基础上添加步数(iter_0000xx)文件夹进行隔离,并同步调整 UT 用例;
  6. 同步调整文档内容,新增 lora_save_only 接口介绍,新增三种保存方式的使用方式和推荐场景介绍,修改 LoRA 权重融合与断点续训的说明;

Does this PR introduce any user-facing change?

无

How was this patch tested?

测试lora训练场景下,单独保存lora权重,导出为HF权重和DCP权重的正确性

likedislike
Pull Request已成功合入, 合并人@ascend-robot
(感谢 LKONE 的贡献)
LKONELKONE成员
27 天前 创建了 pull request,commit 124d9a43
atomgit-bot
atomgit-bot
27 天前 评论:

变更摘要

本 PR 主要调整了 LoRA 的保存模式:在默认的全量 checkpoint 保存流程中支持将 LoRA 权重(lora_A / lora_B)合并回基础权重后再导出,并新增 lora_save_only 开关,仅在显式开启时走"只导出 LoRA adapter"的路径;同时将 LoRA-only 保存改为写入按迭代号组织的 checkpoint 子目录,便于 PEFT/vLLM/SGLang 直接加载。

主要改动

  • LoRA 权重合并导出:在 hf_utils.py 中新增 merge_lora_weights 函数,遍历 .lora_A.default.weight / .lora_B.default.weight 键,通过 delta = matmul(lora_b, lora_a) 计算增量并加到对应基础权重(base + scaling * delta),随后从状态字典中删除 LoRA 键;遇到 DTensor 时会先 redistribute 为 Replicate 再合并。

  • Checkpointer 参数与预处理扩展:HuggingFaceCheckpointer 新增 lora_alpha、lora_rank 参数,enable_lora 时以 lora_alpha / lora_rank 作为缩放系数调用 merge_lora_weights,并在保存前将浮点权重统一转换为 save_ckpt_dtype。

  • 新增 lora_save_only 配置项:LoraArguments 新增 lora_save_only(默认 False)字段,用于控制是否仅导出 adapter 的 safetensors 及其配置。

  • 保存逻辑分流:train_engine.py 的 checkpoint 保存逻辑改为仅当 args.training.lora.enable 且 args.training.lora.lora_save_only 时调用 save_lora_only,随后执行 torch.distributed.barrier() 并提前返回;否则走默认全量模型保存流程,并将 lora_alpha、lora_rank 传入 checkpointer。

  • LoRA-only 保存目录调整:LoraWeightManager.save_lora_only 改用 get_checkpoint_name(save_path, iteration, release=False) 生成带迭代号的输出目录,lora_adapter.safetensors 与 adapter_config.json 均写入该目录而非原 save_path 下。

likedislike
不准确?
atomgit-bot
atomgit-bot
27 天前 评论:

代码审查

✅ 未发现问题

likedislike
不准确?
ascend-robotascend-robot成员
27 天前 添加了label:ascend-cla/yes
此处折叠了225条消息 查看更多
ascend-robotascend-robot成员
18 天前 合入了pull request
ascend-robot
ascend-robot成员
18 天前 评论:

Pull Request 已合并或已关闭。

If you want to solve this problem, you can click here to do it in the FAQs.

likedislike
ascend-robot
ascend-robot成员
18 天前 评论:

Pull Request 已合并或已关闭。

If you want to solve this problem, you can click here to do it in the FAQs.

likedislike
LKONELKONE成员
18 天前 关联了issue:[Usage]: Qwen3.5-27B LoRA微调save_mode无实际作用
ascend-robot
ascend-robot成员
18 天前 评论:

Pull Request 已合并或已关闭。

If you want to solve this problem, you can click here to do it in the FAQs.

likedislike