training-loop-integration
Migrate raw PyTorch training and evaluation loops to Hugging Face Accelerate using Accelerator, prepare(), backward(), gradient accumulation, dataloader behavior, gather/reduce, mixed precision, DDP kwargs, local SGD, communication hooks, and basic distributed logging.
- License 许可
- Apache-2.0
- 最近更新
- 2026-09-03
- Stars
- ★ 351
- Forks
- 27
01 前言 Front Matter
- name
- training-loop-integration
- license
- Apache 2.0
- metadata
- { "disco-role": "operating" }
- description
- Migrate raw PyTorch training and evaluation loops to Hugging Face Accelerate using Accelerator, prepare(), backward(), gradient accumulation, dataloader behavior, gather/reduce, mixed precision, DDP kwargs, local SGD, communication hooks, and basic distributed logging.
- disable-model-invocation
- true
02 资源文件 4
- 参考文档references/api-reference.md11.6 KB
- 参考文档references/troubleshooting.md8.4 KB
- 参考文档references/workflows.md10.7 KB
- 脚本scripts/accelerator_loop_smoke.py3.7 KB
03 SKILL.md 正文
Training Loop Integration
Use this sub-skill when adapting an existing PyTorch model, optimizer, dataloader, scheduler, or evaluation loop to Accelerate while keeping the training code mostly framework-neutral.
Route first
- For
accelerate config,accelerate launch, launch flags, config files, or process-count setup, use../configuration-and-cli/. - For DeepSpeed, FSDP, TPU, Megatron-LM, FP8 backend recipes, or backend-specific plugin tuning, use
../distributed-training-backends/. - For
save_state,load_state, model checkpoint saving, trackers, profiler, or experiment dashboards, use../checkpointing-and-tracking/. - For large-model loading, device maps, offload, or inference dispatch, use the root skill routing to the big-model sub-skill.
Start here
- Read
references/workflows.mdfor migration recipes and loop patterns. - Read
references/api-reference.mdfor constructor options, wrappers, dataloader semantics, gather/reduce, kwargs handlers, logging, and scheduler behavior. - Read
references/troubleshooting.mdwhen a distributed loop hangs, metrics are wrong, gradients do not sync, or device placement behaves unexpectedly. - Run
scripts/accelerator_loop_smoke.pyto verify a minimal CPU training loop in the current environment.
Core migration checklist
- Create one
Acceleratorearly, before distributed-sensitive objects need its state. - Remove hard-coded
.cuda()and direct.to("cuda"); prefer automatic device placement throughaccelerator.prepare(). - Pass related PyTorch objects to
accelerator.prepare(...)and unpack them in the same order. - Replace
loss.backward()withaccelerator.backward(loss). - For accumulation, create
Accelerator(gradient_accumulation_steps=N)or aGradientAccumulationPlugin, then wrap each minibatch body inwith accelerator.accumulate(model):. - Use
accelerator.gather_for_metrics(...)for evaluation metrics andaccelerator.pad_across_processes(...)before gathering variable-size tensors. - Use
accelerator.print(...)oraccelerate.logging.get_logger(...)instead of unsynchronized process-wideprint()spam.
Smoke check
python skills/accelerate/sub-skills/training-loop-integration/scripts/accelerator_loop_smoke.py
python skills/accelerate/sub-skills/training-loop-integration/scripts/accelerator_loop_smoke.py --help
The smoke script is intentionally CPU-safe and tiny. It validates importability, Accelerator.prepare, Accelerator.accumulate, Accelerator.backward, scheduler wrapping, and metric gathering without requiring a distributed launcher.
04 同仓库更多 Skills
passnet-triton-opt
Make a single PassNet Triton kernel fast AND numerically faithful: performance model (when a replacement can win at all), block/grid/warp tuning, autotune policy, launch overhead, and per-op numeric recipes to pass the dtype baseline tolerances. Use when a pass matches and is correct but speedup below expected, or when correctness fails by small numeric margins.
passnet-solve
END-TO-END playbook for solving one PassNet sample: analyze the computation graphs, decide the optimization strategy (what to fuse, what to replace, what to leave alone), drive the iteration loop, and maximize the sample score. This is the ENTRY skill — invoke it first for any PassNet optimization task; it tells you when to use passnet-pattern-fusion, passnet-triton-opt and passnet-feedback.
passnet-skill
PassNet GPU kernel optimization via compiler passes. Design and implement Triton-based optimization passes, create pass files under ./pass_dir/, self-evaluate with pass_evaluator, and iterate to maximize GPU speedup.
passnet-pattern-fusion
Author PassNet pass files: write patterns that actually MATCH the FX graph, pick fusion regions (which ops to absorb into one Triton kernel), and structure multi-pass submissions with the mandatory shared-dispatch architecture. Use when creating or fixing pattern()/replacement_args()/replacement_func() files, when a pass "failed to match", or when deciding how to fuse multiple kernels/ops.
passnet-orchestrate
Round/batch planning and decision support for PassNet graph optimization. Use for multi-sample triage, worker allocation, eval-budget policy, and post-eval keep/revert decisions. For solving one concrete sample end to end, use passnet-solve as the entry skill; consult this skill only when you need broader planning or a specific decision gate.
passnet-feedback
Fast, deterministic feedback for PassNet work: pre-flight pattern/match verification WITHOUT burning a GPU evaluation, per-node bottleneck analysis of a sample's graphs, and eval-log parsing into per-variant status + estimated score + failure classification. Use BEFORE every GPU evaluation (check_pattern), at the START of a sample (analyze_graph), and AFTER every evaluation (parse_eval_log).