Merged
feat: DeepSeek SFT on dev_0.4.0 #1248
feat: DeepSeek SFT on dev_0.4.0 #1248
Merged
Can Learn BotAdd on 23 days ago
Can Learn Bot
Can Learn BotCollaborators
23 days ago

描述

DeepSeek V4.1 在 TorchTitan 0.4.0 上复用 CPT 配方,通过 --override 接入图文 SFT。数据层调用模型目录中的官方 encoding/encoding.py,支持纯文本对话及同一条消息内多图与文字交错,使用 assistant 目标 token 计算 SFT loss。

类型

Checklist:

如何测试

从 DOCCI 训练集选取 4 条图文样本。输入为图片和指令 Describe the image in detail.,参考答案为人工标注的图片描述。
在开发阶段用这组样本进行 64 步图文 SFT。微调前和重载微调权重后,在同一组样本上计算回答 token 的交叉熵。

精度和性能验证结果

指标 起点 64 步结果
训练 loss 12.22532(第 1 步) 0.02046(第 64 步)
同组样本的回答交叉熵 12.20929(SFT 前) 0.01706(重载权重后)

DOCCI 图文 SFT 64 步训练 loss

当前提交验证

用 8 份 DeepSeek 官方发布 encoding.py 文件完成 4,000 组对照。Prompt 和 token ID 与官方输出逐项相同,assistant 监督和 EOS 检查通过。在 CPU 上比较屏蔽标签的交叉熵与逐有效位置计算,最大绝对误差为 4.44e-16,低于 1e-12。

likedislike
Pull Request has been successfully merged by CANN-robot.
(Thank you for Can Learn Bot's contribution.)
Can Learn BotCan Learn BotCollaborators
23 days ago Created pull request, commit f216c3bc
atomgit-bot
atomgit-botCollaborators
23 days ago Comments:

变更摘要

本 PR 将 DeepSeek V4 与 V4.1 的 SFT 数据处理从单体文件拆分为分层模块:把“会话监督策略”“图像/媒体解析”“Grain 数据图装配”“兼容导出”分离到各自的 sft 子包中,原模块(deepseek_v4/chat_processor.py、deepseek_v4_1/vision/language_dataset.py)收缩为兼容转发层,并同步更新配置注册表与单元测试的导入路径与类名。整体属于数据管道结构化重构,同时新增了对 .jsonl 数据集源的支持。

主要改动

  • V4 监督逻辑拆层:新增 deepseek_v4/sft/policy.py,把 messages_from_sample、validate_messages、encode_sample 从 DeepSeekV4ChatProcessor.__call__ 抽出,encode_sample 以 (input_ids, label_ids) 元组返回并自带 IGNORE_INDEX = -100,不再依赖 TextSequence/torchtitan.components.loss.IGNORE_INDEX。
  • V4 Grain 装配独立:新增 deepseek_v4/sft/data.py,其中保留 DeepSeekV4ChatProcessor 并新增 is_valid_sample 与 sft_dataloader_config(...);config_registry.py 中 deepseek_v4_flash_sft 改为调用该函数,移除注册表内的 FirstFitPackingConfig、SingleDatasetConfig 等内联装配代码。
  • .jsonl 数据源分支:V4 的 sft_dataloader_config 在路径后缀为 .jsonl 时使用 IndexedJsonlSource.Config,否则回退到 HuggingFaceRandomAccessSource.Config;V4.1 的 vision/sft/data.py 同样为 .jsonl 引入 IndexedJsonlSource.Config 分支。
  • V4.1 视觉 SFT 四分法:vision/language_dataset.py 中的代码被拆到 vision/sft/policy.py(normalize_messages、normalize_context)、vision/sft/media.py(load_image)、vision/sft/processor.py(DeepSeekV41VisionLanguageProcessor,保留 _message_parts = staticmethod(normalize_messages) 别名)与 vision/sft/data.py;_GrainSFTProcessor 更名为 DeepSeekV41SFTProcessor,_SFTCollator 更名为 VisionLanguageCollator,sft_dataloader_config 签名与行为保持不变。
  • 调用方与测试适配:deepseek_v4_1/config_registry.py 的延迟导入改为 from .vision.sft.data import sft_dataloader_config,tests/unit_tests/models/deepseek_v4_1/test_vision_language_sft.py 同步改用新模块路径并断言 VisionLanguageCollator.Config 与 DeepSeekV41SFTProcessor.Config。
likedislike
Incorrect?
atomgit-bot
atomgit-botCollaborators
23 days ago Comments:

代码审查

✅ 未发现问题

likedislike
Incorrect?
CANN-robotCANN-robotCollaborators
23 days ago Added label: cann-cla/yes
CANN-robot
CANN-robotCollaborators
23 days ago Comments:

Thanks for your pull-request.
The full list of commands accepted by me can be found at here.
You can get sig-info at here.
You can self-configure the PR merge rules for this repository. For more details, please refer to Here.
For more, you also can visit HICANN.


PR Approval Progress

✅ Congratulations! All modules have met the lgtm and approve requirements.

Module Approval Details

module lgtm status approve status
repo-cann/torchtitan-npu ✅ zhaowei1936, xuyujun (2/2) ✅ zhaowei1936 (1/1)

💡 Tip:

  • Committer can comment /approve or /lgtm
  • Commenting /approve implies both code review (lgtm) and intent to merge (approve)

CLA Signature Pass

botcanlearn, thanks for your pull request. All authors of the commits have signed the CLA. 👍

likedislike
242 messages are collapsed here。 See more
CANN-robotCANN-robotCollaborators
14 days ago Added label: approved
xuyujun
xuyujunCollaborators
14 days ago Comments:

/lgtm

likedislike
CANN-robotCANN-robotCollaborators
14 days ago Added label: lgtm
CANN-robotCANN-robotCollaborators
14 days ago Merged pull request
CANN-robot
CANN-robotCollaborators
14 days ago Comments:

Pull Request 已合并或已关闭。

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

likedislike