Install any skill in seconds. Free to start, no credit card required.
Get Started Free →Run PyTorch training across GPUs with minimal changes.
.claude/skills/nousresearch-huggingface-accelerate/SKILL.md| Test case | Without → With | Effect | Δ tokens | Δ turns |
|---|---|---|---|---|
| case-03 | ✓→✗ | ▼ Worse | 143% | 0% |
| case-02 | ✓→✓ | = Same ✓ | 220% | 0% |
| case-04 | ✓→✓ | = Same ✓ | 65% | 0% |
| case-05 | ✓→✓ | = Same ✓ | 170% | 0% |
| case-06 | ✓→✓ | = Same ✓ | 92% | 0% |
Accelerate simplifies distributed training to 4 lines of code.
Installation:
bashpip install accelerate
Convert PyTorch script (4 lines):
pythonimport torch + from accelerate import Accelerator + accelerator = Accelerator() model = torch.nn.Transformer() optimizer = torch.optim.Adam(model.parameters()) dataloader = torch.utils.data.DataLoader(dataset) + model, optimizer, dataloader = accelerator.prepare(model, optimizer, dataloader) for batch in dataloader: optimizer.zero_grad() loss = model(batch) - loss.backward() + accelerator.backward(loss) optimizer.step()
Run (single command):
bashaccelerate launch train.py
Original script:
python# train.py import torch model = torch.nn.Linear(10, 2).to('cuda') optimizer = torch.optim.Adam(model.parameters()) dataloader = torch.utils.data.DataLoader(dataset, batch_size=32) for epoch in range(10): for batch in dataloader: batch = batch.to('cuda') optimizer.zero_grad() loss = model(batch).mean() loss.backward() optimizer.step()
With Accelerate (4 lines added):
python# train.py import torch from accelerate import Accelerator # +1 accelerator = Accelerator() # +2 model = torch.nn.Linear(10, 2) optimizer = torch.optim.Adam(model.parameters()) dataloader = torch.utils.data.DataLoader(dataset, batch_size=32) model, optimizer, dataloader = accelerator.prepare(model, optimizer, dataloader) # +3 for epoch in range(10): for batch in dataloader: # No .to('cuda') needed - automatic! optimizer.zero_grad() loss = model(batch).mean() accelerator.backward(loss) # +4 optimizer.step()
Configure (interactive):
bashaccelerate config
Questions:
Launch (works on any setup):
bash# Single GPU accelerate launch train.py # Multi-GPU (8 GPUs) accelerate launch --multi_gpu --num_processes 8 train.py # Multi-node accelerate launch --multi_gpu --num_processes 16 \ --num_machines 2 --machine_rank 0 \ --main_process_ip $MASTER_ADDR \ train.py
Enable FP16/BF16:
pythonfrom accelerate import Accelerator # FP16 (with gradient scaling) accelerator = Accelerator(mixed_precision='fp16') # BF16 (no scaling, more stable) accelerator = Accelerator(mixed_precision='bf16') # FP8 (H100+) accelerator = Accelerator(mixed_precision='fp8') model, optimizer, dataloader = accelerator.prepare(model, optimizer, dataloader) # Everything else is automatic! for batch in dataloader: with accelerator.autocast(): # Optional, done automatically loss = model(batch) accelerator.backward(loss)
Enable DeepSpeed ZeRO-2 (pass a DeepSpeedPlugin, not a raw dict):
pythonfrom accelerate import Accelerator, DeepSpeedPlugin deepspeed_plugin = DeepSpeedPlugin( zero_stage=2, # ZeRO-2 offload_optimizer_device="none", # or "cpu" to offload gradient_accumulation_steps=4, ) accelerator = Accelerator( mixed_precision='bf16', deepspeed_plugin=deepspeed_plugin, # DeepSpeedPlugin instance (or dict[str, DeepSpeedPlugin]) ) # Same code as before! model, optimizer, dataloader = accelerator.prepare(model, optimizer, dataloader)
Or point at a full DeepSpeed JSON config via the plugin:
pythonfrom accelerate import Accelerator, DeepSpeedPlugin # hf_ds_config accepts a path to a DeepSpeed config JSON (or a dict) deepspeed_plugin = DeepSpeedPlugin(hf_ds_config="ds_config.json") accelerator = Accelerator(mixed_precision='bf16', deepspeed_plugin=deepspeed_plugin)
ds_config.json (a raw DeepSpeed config — passed via the plugin, NOT via --config_file):
json{ "fp16": {"enabled": false}, "bf16": {"enabled": true}, "zero_optimization": { "stage": 2, "offload_optimizer": {"device": "cpu"}, "allgather_bucket_size": 5e8, "reduce_bucket_size": 5e8 } }
Or via interactive config:
bashaccelerate config # Select: DeepSpeed → ZeRO-2 # This writes an accelerate YAML config (default: ~/.cache/huggingface/accelerate/default_config.yaml)
Launch (--config_file expects an accelerate YAML, not a raw DeepSpeed JSON):
bash# Uses the default accelerate config written by `accelerate config` accelerate launch train.py # Or point at a specific accelerate YAML accelerate launch --config_file accelerate_deepspeed.yaml train.py
Enable FSDP:
pythonfrom accelerate import Accelerator, FullyShardedDataParallelPlugin fsdp_plugin = FullyShardedDataParallelPlugin( sharding_strategy="FULL_SHARD", # ZeRO-3 equivalent auto_wrap_policy="transformer_based_wrap", # valid: transformer_based_wrap | size_based_wrap | no_wrap cpu_offload=False ) accelerator = Accelerator( mixed_precision='bf16', fsdp_plugin=fsdp_plugin ) model, optimizer, dataloader = accelerator.prepare(model, optimizer, dataloader)
Or via config:
bashaccelerate config # Select: FSDP → Full Shard → No CPU Offload
Accumulate gradients:
pythonfrom accelerate import Accelerator accelerator = Accelerator(gradient_accumulation_steps=4) model, optimizer, dataloader = accelerator.prepare(model, optimizer, dataloader) for batch in dataloader: with accelerator.accumulate(model): # Handles accumulation optimizer.zero_grad() loss = model(batch) accelerator.backward(loss) optimizer.step()
Effective batch size: batch_size * num_gpus * gradient_accumulation_steps
Use Accelerate when:
Key advantages:
Use alternatives instead:
Issue: Wrong device placement
Don't manually move to device:
python# WRONG batch = batch.to('cuda') # CORRECT # Accelerate handles it automatically after prepare()
Issue: Gradient accumulation not working
Use context manager:
python# CORRECT with accelerator.accumulate(model): optimizer.zero_grad() accelerator.backward(loss) optimizer.step()
Issue: Checkpointing in distributed
Use accelerator methods:
python# Save only on main process if accelerator.is_main_process: accelerator.save_state('checkpoint/') # Load on all processes accelerator.load_state('checkpoint/')
Issue: Different results with FSDP
Ensure same random seed:
pythonfrom accelerate.utils import set_seed set_seed(42)
Megatron integration: See references/megatron-integration.md for tensor parallelism, pipeline parallelism, and sequence parallelism setup.
Custom plugins: See references/custom-plugins.md for creating custom distributed plugins and advanced configuration.
Performance tuning: See references/performance.md for profiling, memory optimization, and best practices.
Launcher requirements:
torch.distributed.run (built-in)deepspeed (pip install deepspeed)| Case | Status | Duration (ms) | Turns | Tokens | Tool calls | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Without | With | Δ | Without | With | Δ | Without | With | Δ | Without | With | Δ | ||
case-01 | fail→fail | 21,092 | 12,720 | -40% | 1 | 1 | 0% | 4,524 | 5,163 | +14% | 0 | 0 | — |
case-02 | pass→pass | 6,472 | 5,810 | -10% | 1 | 1 | 0% | 1,132 | 3,628 | +220% | 0 | 0 | — |
case-03 | pass→fail | 8,220 | 8,441 | +3% | 1 | 1 | 0% | 1,708 | 4,157 | +143% | 0 | 0 | — |
case-04 | pass→pass | 11,004 | 7,708 | -30% | 1 | 1 | 0% | 2,544 | 4,209 | +65% | 0 | 0 | — |
case-05 | pass→pass | 6,960 | 5,786 | -17% | 1 | 1 | 0% | 1,354 | 3,658 | +170% | 0 | 0 | — |
case-06 | pass→pass | 10,712 | 7,680 | -28% | 1 | 1 | 0% | 2,137 | 4,098 | +92% | 0 | 0 | — |
case-07 | pass→pass | 4,698 | 4,148 | -12% | 1 | 1 | 0% | 998 | 3,300 | +231% | 0 | 0 | — |
case-08 | pass→pass | 10,616 | 6,893 | -35% | 1 | 1 | 0% | 1,984 | 3,815 | +92% | 0 | 0 | — |
case-09 | pass→pass | 9,564 | 5,735 | -40% | 1 | 1 | 0% | 1,848 | 3,532 | +91% | 0 | 0 | — |
case-10 | pass→pass | 4,216 | 3,202 | -24% | 1 | 1 | 0% | 789 | 3,057 | +287% | 0 | 0 | — |
case-11 | pass→pass | 4,366 | 2,389 | -45% | 1 | 1 | 0% | 846 | 2,935 | +247% | 0 | 0 | — |
case-12 | pass→pass | 6,296 | 5,542 | -12% | 1 | 1 | 0% | 1,258 | 3,532 | +181% | 0 | 0 | — |
case-13 | pass→pass | 7,533 | 6,537 | -13% | 1 | 1 | 0% | 1,452 | 3,840 | +164% | 0 | 0 | — |
case-14 | pass→pass | 4,486 | 2,299 | -49% | 1 | 1 | 0% | 714 | 2,874 | +303% | 0 | 0 | — |
case-15 | pass→pass | 5,471 | 3,707 | -32% | 1 | 1 | 0% | 1,072 | 3,165 | +195% | 0 | 0 | — |
case-16 | fail→fail | 12,582 | 9,942 | -21% | 1 | 1 | 0% | 2,239 | 4,240 | +89% | 0 | 0 | — |
case-17 | pass→pass | 8,485 | 4,675 | -45% | 1 | 1 | 0% | 1,637 | 3,427 | +109% | 0 | 0 | — |
case-18 | pass→pass | 4,664 | 2,364 | -49% | 1 | 1 | 0% | 843 | 2,836 | +236% | 0 | 0 | — |
case-19 | pass→pass | 3,687 | 2,564 | -30% | 1 | 1 | 0% | 650 | 2,996 | +361% | 0 | 0 | — |
case-20 | pass→pass | 8,625 | 8,364 | -3% | 1 | 1 | 0% | 1,895 | 4,277 | +126% | 0 | 0 | — |
case-21 | pass→pass | 7,283 | 4,968 | -32% | 1 | 1 | 0% | 1,469 | 3,476 | +137% | 0 | 0 | — |
case-22 | pass→pass | 8,431 | 6,783 | -20% | 1 | 1 | 0% | 1,856 | 3,904 | +110% | 0 | 0 | — |
case-23 | pass→pass | 4,649 | 4,534 | -2% | 1 | 1 | 0% | 952 | 3,315 | +248% | 0 | 0 | — |
case-24 | pass→pass | 4,883 | 5,806 | +19% | 1 | 1 | 0% | 979 | 3,612 | +269% | 0 | 0 | — |
DecimalAI ran this skill against gemini-3.6-flash twice over the same eval suite — once with the skill loaded and once without — and compared the two runs case by case. 24 cases were attempted. The headline lift of -4 percentage points is the difference between those two pass rates over the 24 comparable cases. 1 case got worse with the skill loaded, and it is included in that figure.
Other measured skills in the registry, with their headline benchmark lift.