The current Pull Request has been closed by xuyujun.
变更摘要
本 PR 是 stacked PR 644 的第 5 层(验收层),不修改任何生产代码,仅为前四层 TorchFT NPU 功能新增定向单元测试,并将其接入仓库标准 UT 门禁。测试覆盖 torchtitan_npu 中 TorchFT 相关的进程组创建与配置校验、rejoin 恢复/重放/看门狗机制、trainer 初始化顺序、checkpoint 与 RNG 恢复等行为,通过 FaultTolerantTrainerEx、ManagerEx、ProcessGroupHCCLEx、OptimizerWrapperEx、TorchFTCheckpointManagerEx 等扩展类的 mock 构造验证。
主要改动
- CI 门禁接入
torchft-nightlywheel:.ci/unit_test.sh在标准 CPU UT job 中新增pip install "torchft-nightly==2026.7.17",该版本与pyproject.toml中 optional dependency 固定提交一致,使 CPU UT 无需 Rust/maturin 工具链即可 source build。 - 新增进程组契约测试(
tests/unit_tests/torchft/test_process_group.py):验证_hccl_group_id()的 group id 生成、manager_module.create_process_group("hccl", timeout)返回ProcessGroupHCCLEx且 timeout 透传到ProcessGroupWrapper.__init__、backend name 稳定为"torchft-hccl";并以参数化用例覆盖validate_npu_process_group_config对非hcclbackend、非同步 quorum 的semi_sync_method、非法min_replica_size抛ValueError的校验。 - 新增 rejoin 恢复机制测试(
tests/unit_tests/torchft/test_rejoin.py):覆盖ManagerEx的 peer healing snapshot 物化(prepare_healing_snapshot在训练线程调用全部 state dict 并 staging、_manager_state_dict读取 materialized CPU state);_ReplayableStepIterator重放不重读 dataloader、恢复 batch 与 RNG、按 microbatch 数量(gradient accumulation × PP microbatch)缓存完整 step;ProcessGroupHCCLEx的 rejoin workaround(watchdog timer 限定在旧 generation、abort仅在有 active work 时stop_device/restart_device且并发 abort 只恢复一次、重配时未提交 step 必须 replay);OptimizerWrapperEx在zero_grad前完成 initial/recovery quorum,且_StepReplayError阻止旧 quorum 在新 generation 提交。 - 新增 trainer/checkpoint 定向测试(
tests/unit_tests/torchft/test_torchft.py):验证deepseek_v4配置仅在extension.torchft.enable后派生 FT 配置并保留基线字段、TorchFT 关闭时子进程不导入torchft但 CLI 可解析 TorchFT 参数;模型 override 顺序位于 config update 之后、组件 build 之前且_validate_cuda_graphs始终执行;maybe_set_all_reduce_hook在纯 DPReplicateModule路径安装并物化跨 replica AVG all-reduce hook;LossManagedProcessGroupEx仅将浮点 loss 的 SUM 改写为 AVG(int token count 保持 SUM);quorum 被拒时先同步 MoE counts 再回滚expert_bias_E;持久化 checkpoint 仅单 writer 保存且 load 后恢复 LR;data-only DCP 不开启持久化 checkpoint;_TorchRngState.load_state_dict恢复 CPU 与 NPU 的 RNG stream。


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: xuyujun, zhaowei1936, 张德鹏, 徐道鸿, wanghui0) | ❌ (0/1)(You can also ask: hwcaifubi, panchao-gitcode, lrwei0709, 张德鹏, wanghui0) |
💡 Tip:
- Committer can comment
/approveor/lgtm- Commenting
/approveimplies both code review (lgtm) and intent to merge (approve)
CLA Signature Pass
zhangyifan999, thanks for your pull request. All authors of the commits have signed the CLA. 👍


【关闭原因】TorchFT NPU 定向 UT 与门禁已由 #828/#906(已合入 master)取代,故关闭。


描述
本 PR 是原 PR 644 的第 5 层,为前四层功能增加定向单元测试并接入仓库标准 UT 门禁,同时提供 4 卡单机 TorchFT ST。该层不修改生产代码。
目标分支为
master。合入依赖:#675、#676、#677 和 #678。调用流程
.ci/setup_torchtitan.sh读取requirements.txt中固定的 Torchtitan commit 并准备源码。.ci/unit_test.sh安装开发依赖和与固定 TorchFT commit 对应的 nightly wheel。pytest收集tests/unit_tests/torchft下三个测试文件。test_process_group.py验证 #675 factory 与 #676 HCCL PG 的注册边界。test_rejoin.py直接驱动 #676 Work tracker、device recovery、state staging 和 replay 状态机。test_torchft.py从 #678 DeepSeek recipe/CLI 进入 #677 Manager/Trainer/Optimizer/Checkpoint,覆盖完整集成链。.ci/smoke_test.sh通过--test_suite torchft进入 integration runner;torchft.py在 4 张 NPU 上编排 2 replicas x 2 ranks,分别验证正常训练和 rejoin。IntegrationTestRunner 扩展点
仓库原有
run_single_test()每个 case 只启动一次scripts/run_train.sh,适合普通单训练任务。TorchFT ST 需要同时管理 Lighthouse、两个独立torchrun、故障 replica 的退出与 replacement,因此为OverrideDefinitions增加可选的runner回调,并用IntegrationTestRunnerProtocol 约束其签名。只有显式设置
runner的 TorchFT case 才绕过默认单进程路径;现有模型 case 的命令拼装、golden loss 和 TensorBoard 检查保持不变。TorchFT 的具体编排由_TorchFTScenarioRunner实现,调用关系为:测试调用链与断言边界
37 passed in 39.80s。文件与函数
.ci/unit_test.shtorchft-nightly==2026.7.17wheel,避免 source build 依赖 Rust/maturin。.ci/smoke_test.shtorchft-nightly,先通过--test_suite torchft运行 TorchFT ST,再执行既有 models suite。tests/integration_tests/__init__.pyIntegrationTestRunner:约束需要自定义进程编排的 integration case 回调签名。OverrideDefinitions.runner:可选回调;未设置时既有 case 的执行行为不变。tests/integration_tests/run_tests.py_TEST_SUITES_FUNCTION["torchft"]:注册 TorchFT suite。run_single_test():case 设置runner时调用自定义回调并返回,否则继续使用原来的单次scripts/run_train.sh和 loss 检查路径。tests/integration_tests/torchft.py_Settings.from_environment():读取模型、数据、设备、端口、步数和超时设置。_TorchFTScenarioRunner:管理 Lighthouse、两个 torchrun 进程树、日志、超时和清理。_run_training():验证 2 replicas x 2 ranks 正常同步训练。_run_rejoin():在 step 2 后终止 replica 1,再以同一 replica ID 拉起 replacement;要求 survivor 完成 stop/restart 和 step replay,replacement 完成 peer healing。run_torchft_case():把通用 runner 回调转换为具体 TorchFT scenario。build_torchft_test_list():注册torchft_training和torchft_rejoin两个 4 卡 case。tests/unit_tests/torchft/test_process_group.py_config():构造最小 NPU FT manager 配置。test_registered_hccl_process_group_contract():验证 registry 能创建 HCCL wrapper、透传 timeout 并暴露稳定 backend name。capture_timeout():截获 ProcessGroupWrapper 初始化 timeout。test_npu_process_group_config_contract():覆盖 HCCL/同步 quorum 合法配置和 backend、半同步、replica 数非法配置。tests/unit_tests/torchft/test_rejoin.py_configured_rejoin_process_group():构造已启用 rejoin 的最小 HCCL PG。_tracked_work_fixture():构造带 timer 的 tracked Work 测试夹具。TestNpuRecoverySnapshot.test_peer_healing_state_becomes_the_local_committed_snapshot():验证 peer state 应用后成为本地 committed snapshot。test_prepare_healing_snapshot_materializes_all_state_on_training_thread():验证 FSDP/NPU state 在训练线程 staging。test_manager_state_dict_materializes_deferred_state_for_peer_healing():验证 recovery worker 读取的是 materialized CPU state。_make_replay_trainer():构造最小 replay trainer。test_replay_restores_batches_and_rng_without_refetching():验证重放不重新读 dataloader,并恢复 batch 与 RNG。test_replay_waits_for_device_restart_before_next_attempt():验证下一次 attempt 前等待 restart、清理失败状态并恢复 committed state。run_attempt():第一次 attempt 注入 replay error,第二次验证 restart 后从缓存继续。test_trainer_replay_caches_every_pipeline_microbatch():验证 PP/gradient accumulation 会缓存完整 step 的所有 microbatch。test_work_watchdog_is_scoped_to_transaction_generation():验证旧 generation timer 不会 abort 新 quorum。test_work_remains_active_until_completion_event_finishes():验证 Python Work 完成后仍等待 NPU event。test_incomplete_work_remains_active():验证未完成 event 保持 active。test_failed_work_remains_active():验证 query 失败按 active 处理,避免误判安全。test_abort_restarts_device_only_for_active_work():验证无 active Work 只重建 PG,有 active Work 才 stop/restart device。test_reconfigure_replays_completed_but_uncommitted_step():验证 communicator 重配时未提交 step 必须 replay。test_concurrent_abort_runs_device_recovery_once():验证并发 abort 只执行一次整设备恢复。stop_device():用事件阻塞模拟慢速整设备停止,制造两个 abort 并发窗口。test_container_wraps_the_nonrecursive_inner_adapter():验证 optimizer container 使用非递归 adapter。test_initial_and_recovery_quorum_finish_before_zero_grad():验证 initial/recovery quorum 在下一次 forward 前完成。test_aborted_quorum_cannot_commit_after_process_group_reconfigure():验证旧 quorum future 不能在新 generation 提交 optimizer step。tests/unit_tests/torchft/test_torchft.py_initialize_fault_tolerant_trainer():用 mock 上游构造函数测试 NPU trainer 初始化顺序。test_deepseek_v4_config_late_binds_torchft_after_enable():验证普通 recipe 只在 enable 后派生 FT 配置且保留基线字段。test_deepseek_v4_config_does_not_eagerly_import_torchft():验证 TorchFT 关闭时进程不导入torchft。test_deepseek_v4_config_exposes_torchft_cli_without_preimport():验证 CLI 可解析 TorchFT 参数且保持延迟导入。test_deepseek_v4_torchft_recipe_defers_implementation_imports():在独立子进程加载显式 FT recipe,验证只预置 extension/checkpoint,配置解析期不导入 NPU TorchFT 实现。test_npu_ft_trainer_initializes_configured_spmd_backend():验证 FT trainer 使用 NPU 配置的 distributed backend。test_model_overrides_run_after_update_and_before_build():验证模型 override 顺序位于 config update 后、组件 build 前。apply_override():记录 override 执行顺序。initialize_upstream():模拟并记录上游 trainer 初始化。update_from_config():记录模型配置更新发生在 override/build 之前。test_cuda_graph_validation_runs_without_model_overrides():验证无 override 时仍执行 graph 配置校验。initialize_upstream():模拟上游初始化并检查校验后的配置。update_from_config():记录无 override 场景的 config update。test_replicate_dp_hook_uses_stream_ordered_cross_replica_all_reduce():验证纯 DP replicate 路径安装跨 replica hook,并以目标 ProcessGroup 和ReduceOp.AVG调用一次 stream-ordered AllReduce;不再断言 bucket 前后的.item()。_run_abort_path()、test_abort_restarts_device_only_for_active_work()、test_abort_without_active_work_uses_process_group_abort():分别复用 abort 执行夹具并独立验证 active Work 的整设备恢复路径与无 active Work 的普通 PG abort 路径。FakeReplicateModule.__init__():初始化测试用 hook 槽位。FakeReplicateModule._get_fsdp_state():提供最小单 parameter-group FSDP state。FakeReplicateModule.set_all_reduce_hook():记录被安装的跨 replica hook。test_loss_process_group_averages_float_sum_only():验证 loss SUM 转 AVG,而非浮点或其他 op 不被改写。test_rejected_quorum_syncs_moe_counts_then_rolls_back_bias():验证 MoE counts 先同步,quorum 拒绝后 bias 回滚。initialize_base():模拟普通 OptimizersContainer 初始化。update_expert_bias():记录并模拟 MoE bias 更新。test_persistent_checkpoint_has_single_writer_and_restores_live_lr():验证完整 DCP 单 writer 及 load 后 LR 恢复。test_peer_healing_without_persistent_checkpoint_does_not_require_states_attribute():验证关闭持久化 DCP 时 peer healing 仍注册完整训练状态。test_dataloader_checkpoint_storage_does_not_enable_persistent_checkpoint():验证 data-only DCP 不会误开启完整 checkpoint。initialize_storage():截获 data-only storage 配置。test_torch_rng_state_restores_loaded_cpu_and_npu_streams():验证 checkpoint load 后恢复 CPU/NPU RNG。类型
Checklist:
如何测试
4 卡单机 ST:
python -m tests.integration_tests.run_tests \ test_reports/torchft \ --test_suite torchft \ --ngpu 4 # 单独运行 rejoin python -m tests.integration_tests.run_tests \ test_reports/torchft_rejoin \ --test_suite torchft \ --test_name torchft_rejoin \ --ngpu 4同时验证:
拆分组合验证结果(固定
masterrequirements 环境):4 卡真实 NPU ST(2026-09-02,
NPU_DEVICES=2,3,4,5):组合分支与原 PR 644
aa41bfb的git diff --exit-code返回 0。其他信息
该 PR 作为 stacked PR 的验收层。前三个代码 PR 的 CI 可运行仓库原门禁;新增 TorchFT 定向用例在本层进入标准 UT job。