Closed
refactor: migrate to TorchTitan 0.4.0 launch-config (Python recipes + opt-in overrides) #1173
hanboyouAdd on Nov 3Closed at 9 days ago
refactor: migrate to TorchTitan 0.4.0 launch-config (Python recipes + opt-in overrides) #1173
Closed
hanboyouAdd on Nov 3Closed at 9 days ago
hanboyou
hanboyouCollaborators
Nov 3

描述

迁移到上游 TorchTitan 0.4.0(dc3985ad)的「配置即 Python」启动方式:一次运行由返回完整 Trainer.Config 的 recipe 函数描述,通过 --module <module> --config <function> 选择,--section.option CLI 仅作为兼容层保留。

本 PR 分两层完成:

  1. Python recipe + thin launcher(refactor: move text and vision training configurations into Python recipes)

    • 新增第二顶层包 torchtitan_npu_recipes/,把原先散落在 examples/**/*.sh 的 --section.option 拼接、环境变量数据注入(DATASET/DATASET_PATH/USE_GOLDEN/USE_TILELANG 等)收敛为具名 recipe。
    • examples/**/*.sh 瘦身为只保留进程数、节点地址、CANN/HCCL 环境、资源路径与 MODULE/CONFIG 选择。
  2. 对齐上游 recipes 布局 + 移出 opt-in override(refactor: adopt torchtitan recipes layout and move opt-in overrides)

    • torchtitan_npu_recipes/ 重构为 models/、graph_trainer/、overrides/、tests/,镜像上游 torchtitan_recipes。
    • 删除全部 19 个 _apply_* 配置改写函数,改为直接构造 typed config 块 + 小步 derive(base, type(base), **typed_blocks) 变体;override.imports 保持显式有序元组。
    • 把 opt-in override 工厂从 torchtitan_npu.override 迁到 torchtitan_npu_recipes.overrides(common 与各模型),可复用实现类留在 torchtitan_npu/models/*、extensions/cpu_offload,核心插件包不再反向依赖 recipes。

类型

Checklist:

如何测试

cd torchtitan-npu-config-migration
TORCH_DEVICE_BACKEND_AUTOLOAD=0 OMP_NUM_THREADS=4 TORCHTITAN_DIR=/tmp/torchtitan \
PYTHONPATH=/tmp/torchtitan:$PWD:$PWD/../.test-deps/attn_gym_016:$PYTHONPATH \
python -m pytest tests/unit_tests -q --tb=line -p no:cacheprovider

结果:1308 passed, 97 skipped, 0 failed, 0 errors(已基于当前 dev_0.4.0 重新 rebase 并全量跑通;迁移前冻结基线在首个失败处停止)。

其他验证:

  • 配置逐字段一致性:70 个 recipe 配置与冻结基线逐字段一致;66 个 launcher 模式解析结果在仅做路径归一化后完全一致。
  • 真实 override 激活:ConfigManager 的 --module/--config + 生产 apply_overrides 覆盖 RoPE/RMSNorm/dispatcher/sparse attention,并传递参数化几何 {"num_heads":16,"index_num_heads":8}。
  • 架构边界 ratchet、wheel 打包(337 项)、wheel 安装后脱离 checkout 的选择均通过。
  • 完整 pre-commit 除 Pyrefly 外全部通过;Pyrefly 保持与冻结基线相同的 49 个既有诊断,无新增。

精度和性能验证结果

纯配置形状/归属重构,不改动数学、dtype、checkpoint key、DTensor/分片或编译语义。以同一 seed checkpoint 做 100 步 loss/grad_norm 逐值对比(未截断)为 bitwise 一致。未在真实 NPU 上重新训练;已存在的 V4 golden 数值差异(12.2756 vs 12.2360)与 NPU 阻塞项(Qwen CUDA-only causal_conv1d、CANN SMLAG tiling、Triton mHC/GDN CI 排除)均不在此变更范围,未声称修复。

其他信息

  • 基线为 dev_0.4.0(93f956c1),固定 TorchTitan dc3985ad4912f67f71d5a125f2fdf40144ec8354。已 rebase 到当前 dev_0.4.0,与既有的 V3.2 varlen-CP 移除(!1150)和 Muon/NovaSwap 适配(!1153)做了归并(主要影响测试用例的 override 路径与 Muon profile 命名)。
  • 本 PR 与已合并的 Grain PR #1147(migration/vision-grain-040)分离,只包含配置迁移。
  • torchtitan_npu/override/ 已删除;torchtitan_npu 不再(直接或惰性)导入 torchtitan_npu_recipes,由子进程契约测试约束。
  • 详细实现与验证记录见 runs/config-layout-migration-20261103/dsh/completion-report.md。
likedislike
The current Pull Request has been closed by hanboyou.
hanboyouhanboyouCollaborators
Nov 3 Created pull request, commit 4eeed278
atomgit-bot
atomgit-botCollaborators
Nov 3 Comments:

🤖 正在生成合并请求摘要,请稍候…

likedislike
Incorrect?
atomgit-bot
atomgit-botCollaborators
Nov 3 Comments:

🤖 AI 代码检视正在进行中,请稍候…

likedislike
Incorrect?
CANN-robotCANN-robotCollaborators
Nov 3 Added label: stat/needs-squash
CANN-robotCANN-robotCollaborators
Nov 3 Added label: cann-cla/yes
CANN-robot
CANN-robotCollaborators
Nov 3 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

⚠️ This PR does not yet meet the following requirements:lgtm (requires ≥ 2 person(s) per module)、approve (requires ≥ 1 person(s) per module)

Module Approval Details

module lgtm status approve status
repo-cann/torchtitan-npu ❌ (0/2)(You can also ask: zhuofanshen, hwcaifubi, xuyujun, zhaowei1936, zhanghz1) ❌ (0/1)(You can also ask: wanghui0, zhuofanshen, 张德鹏, xuyujun, zhaowei1936)

💡 Tip:

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

CLA Signature Pass

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

likedislike
atomgit-bot
atomgit-botCollaborators
Nov 3 Comments:

⚠️ 本次变更过大(217 个可审文件、12750 行,预估需 11 轮分片审查),已超出 AI 代码评审的处理范围,本次跳过。建议拆分为更小的 PR 以获得有效评审。

likedislike
Incorrect?
CANN-robotCANN-robotCollaborators
Nov 3 Set hwcaifubi,lrwei0709,zhaowei1936,zhong_lin,zhanghz1,depeng1994,shengnan666,XDaoHong,xuyujun,zhuofanshen,wanghui0,panchao-gitcode,zzyyjj012 as approver
CANN-robotCANN-robotCollaborators
Nov 3 Set hwcaifubi,lrwei0709,zhaowei1936,zhong_lin,zhanghz1,depeng1994,shengnan666,XDaoHong,xuyujun,zhuofanshen,wanghui0,panchao-gitcode,zzyyjj012 as approver
atomgit-bot
atomgit-botCollaborators
Nov 3 Comments:

⚠️ 本次变更过大(217 个可审文件、12750 行,预估需 11 轮分片审查),已超出 AI 代码评审的处理范围,本次跳过。建议拆分为更小的 PR 以获得有效评审。

likedislike
Incorrect?
titan-npu-reviewer
titan-npu-reviewerNov 3Start code review1
.agents/AGENTS.md
@@ -3,7 +3,7 @@
33`torchtitan-npu` 是 [torchtitan](https://github.com/pytorch/torchtitan) 的 **Ascend NPU 插件仓**。
44本仓不直接修改上游 torchtitan checkout,而是通过包导入 patch、配置级 `@override`、模型模块和 NPU 算子将适配能力叠加到上游之上。
55 
66## 核心原则
titan-npu-reviewer
titan-npu-reviewerNov 3Comments:

当前PR改动代码超过1k行,建议拆分PR后合入。

问题编号:R1173-001

审查版本:c859839d7e5dfb6912721d4f0ad7033770a5112a

likedislike
titan-npu-reviewer
titan-npu-reviewer15 days agoStart code review1
.agents/AGENTS.md
@@ -3,7 +3,7 @@
33`torchtitan-npu` 是 [torchtitan](https://github.com/pytorch/torchtitan) 的 **Ascend NPU 插件仓**。
44本仓不直接修改上游 torchtitan checkout,而是通过包导入 patch、配置级 `@override`、模型模块和 NPU 算子将适配能力叠加到上游之上。
55 
66## 核心原则
titan-npu-reviewer
titan-npu-reviewer15 days agoComments:

当前PR改动代码超过1k行,建议拆分PR后合入。

问题编号:R1173-001

审查版本:c859839d7e5dfb6912721d4f0ad7033770a5112a

likedislike
hanboyouhanboyouCollaborators
9 days ago Close pull request
Can Learn Bot
Can Learn BotCollaborators8 days agoStart code review1
pyproject.toml
@@ -29,1 +29,3 @@
29-include = ["torchtitan_npu*"]
29+# `torchtitan_npu_recipes` is a separate top-level package (Python recipes that
30+# return a complete Trainer.Config), shipped alongside the plugin package.
31+include = ["torchtitan_npu*", "torchtitan_npu_recipes*"]
Can Learn Bot
Can Learn Bot8 days agoComments:

Resolved on dev_0.4.0. The package now declares torchtitan==0.4.0 for the native recipes. The dc3985ad pin is from the earlier packaged-recipe draft.

likedislike
Can Learn Bot
Can Learn BotCollaborators8 days agoStart code review1
examples/deepseek_v4/deepseek_v4_flash_lora_4k_a3.sh
@@ -33,0 +34,4 @@
34+NODE_IPS="${NODE_IPS}" \
35+LOG_PREFIX="${LOG_PREFIX:-${CONFIG}}" \
36+bash scripts/run_train_multinodes.sh \
37+ --hf-assets-path "${HF_ASSETS_PATH}" \
Can Learn Bot
Can Learn Bot8 days agoComments:

Resolved on dev_0.4.0. The supported launcher loads a Python recipe and forwards the native CLI options. The recipe now owns the HF assets and checkpoint settings that this shell wrapper supplied as legacy flags.

likedislike
Can Learn Bot
Can Learn BotCollaborators8 days agoStart code review1
torchtitan_npu_recipes/models/deepseek_v4.py
@@ -0,0 +423,4 @@
423+ seq_len=4096,
424+ text_dataset_path=text_dataset_path,
425+ per_doc_alignment=per_doc_alignment,
426+ converters=[DeepSeekV4LoRAConverter.Config(rank=16, alpha=32, rank_experts=16)],
Can Learn Bot
Can Learn Bot8 days agoComments:

Resolved on dev_0.4.0. The V4 A3 factory now applies the native deepseek_v4_lora_transform(rank=16, alpha=32) and derives the optimizer from the transformed model.

likedislike
Can Learn Bot
Can Learn BotCollaborators8 days agoStart code review1
torchtitan_npu_recipes/models/deepseek_v4_1.py
@@ -0,0 +768,4 @@
768+ spmd_backend="spmd_types",
769+ ),
770+ compile=CompileConfig(enable=False, components=["model"], backend="inductor"),
771+ checkpoint=DeepSeekV41LoRACheckpointManager.Config(
Can Learn Bot
Can Learn Bot8 days agoComments:

Could the V4.1 default schedule create a resumable checkpoint before its final PEFT export? The current public factory still inherits 100 training steps and the 500-step checkpoint interval, while the guide overrides the schedule to produce an intermediate DCP.

likedislike
Can Learn Bot
Can Learn BotCollaborators8 days agoStart code review1
torchtitan_npu_recipes/models/deepseek_v4_1.py
@@ -0,0 +783,4 @@
783+ lr_scheduler=_scheduler(total_steps=100),
784+ override=OverrideConfig(imports=list(LORA_GOLDEN_OVERRIDES if golden else LORA_FUSED_OVERRIDES)),
785+ debug=DebugConfig(print_config=True, moe_force_load_balance=True),
786+ optimizer=_replace(base.optimizer, muon_ns_steps=10, name="Muon"),
Can Learn Bot
Can Learn Bot8 days agoComments:

Could the native replacement keep a first-update check for expert adapter ownership after freezing Engram? The pre-migration recipe builds a derived Muon config here, while the current recipe assembles typed groups after the LoRA transform.

likedislike
Can Learn Bot
Can Learn BotCollaborators8 days agoStart code review1
torchtitan_npu_recipes/models/deepseek_v4_1.py
@@ -0,0 +792,4 @@
792+ return _lora_cpt(deepseek_v4_1_flash_lora, golden=golden)
793+ 
794+ 
795+def deepseek_v4_1_flash_text_lora_4k_a3(
Can Learn Bot
Can Learn Bot8 days agoComments:

Could a supported V4.1 text recipe expose its Grain corpus source? This archived factory accepted a dataset argument, whereas the current text recipe sets the source to the bundled C4 fixture. Making the source explicit would prevent an otherwise successful launch from using sample data.

likedislike
Can Learn Bot
Can Learn BotCollaborators8 days agoStart code review1
torchtitan_npu_recipes/models/deepseek_v4_1.py
@@ -0,0 +781,4 @@
781+ metrics=_replace(base.metrics, log_freq=1),
782+ comm=_comm_timeouts(),
783+ lr_scheduler=_scheduler(total_steps=100),
784+ override=OverrideConfig(imports=list(LORA_GOLDEN_OVERRIDES if golden else LORA_FUSED_OVERRIDES)),
Can Learn Bot
Can Learn Bot8 days agoComments:

Could the supported native recipe test assert its resolved RoPE and optimizer-override list in a fresh process? This archived factory selected a golden or fused list. The current public V4.1 LoRA factory explicitly selects fused_attention=False and swap_optimizer=False, and any opt-in fused variant can be validated separately.

likedislike
Can Learn Bot
Can Learn BotCollaborators8 days agoStart code review1
examples/deepseek_v4/deepseek_v4_flash_cpt_4k_a5.sh
@@ -27,3 +27,1 @@
27- torchtitan_npu.override.deepseek_v4.compressor.asc \
28- torchtitan_npu.override.common.swiglu_group.asc \
29- torchtitan_npu.override.common.swiglu_group.asc_shared_experts}"
27+MODULE="${MODULE:-torchtitan_npu_recipes.models.deepseek_v4}"
Can Learn Bot
Can Learn Bot8 days agoComments:

The golden switch defaults to a CPT factory in either branch when CONFIG is unset. Could any A5 LoRA documentation name the explicit adapter recipe and verify that its first update uses the intended block-FP8 policy for frozen weights and BF16 adapter tensors? The default CPT run covers its own configuration.

likedislike