Skip to content

Add swarp.rl, a CUDA-graph MAPPO trainer, with TorchRL, BenchMARL and rsl_rl baselines - #13

Merged
ddebenedittis merged 12 commits into
mainfrom
feat/fast-trainer
Oct 17, 2026
Merged

ddebenedittis merged 12 commits into
mainfrom
feat/fast-trainer

Conversation

@ddebenedittis

Copy link
Copy Markdown
Owner

12 commits; the main threads:

  • swarp.rl, a torch-only MAPPO trainer with the rollout and the update CUDA-graph captured. Same algorithm, recipes and metrics.csv columns as examples/train_mappo.py, which stays as the reference. Non-finite minibatches are skipped on device through Adam's found_inf, and each iteration has one host sync. python -m swarp.rl --scenario <name>, with --compile --tf32 for the fastest configuration. The torchrl-free recipe code (TrainSpec, SPECS, SolveRateWindow, score) moved to swarp.rl.recipes.
  • Training baselines. examples/train_mappo_tuned.py (TorchRL with its own speed features), examples/train_benchmarl.py (BenchMARL's MAPPO, in its own venv because BenchMARL pins torchrl < 0.12) and examples/train_rsl_rl.py (rsl_rl's OnPolicyRunner, one env per agent). Each docstring lists how its algorithm differs from swarp.rl. train_mappo.py was split into importable pieces, with the same behaviour, so the tuned trainer reuses it.
  • python -m swarp.benchmark.train_throughput times every trainer end to end through its real CLI, with --python CONFIG=PATH to run a trainer from another venv.
  • Public API: Environment.step_count and swarp.rl.check_action_box.
  • The lockfile moves to TorchRL 0.14.1 / tensordict 0.14.3. TorchRL 0.14.0 fixed the cudagraph_policy bug the tuned trainer works around (every step of a batch got the last step's action; [BUG] CUDAgraph policy changes the learning process pytorch/rl#3002, #4143).

Results (RTX 3070 Laptop)

ms per iteration at the recipes' sizes:

trainer navigation, 512 envs giveway, 1024 envs
TorchRL as usually written 232 464
BenchMARL 252 480
rsl_rl 162 316
TorchRL, tuned 121 242
TorchRL, tuned, --compile --tf32 101 190
swarp.rl 97 189
swarp.rl --compile --tf32 72 145
  • The simulator is at most 2.7% of any trainer's iteration.
  • swarp.rl is 1.1–1.4× faster than tuned TorchRL and 2.1–3.4× faster than TorchRL as usually written over 512–16k envs.
  • Giveway, full recipe, 3 seeds: every trainer reaches full difficulty at iteration 413–417 and solves all 2048 evaluation envs. swarp.rl gets there in 79 s, tuned TorchRL in 102 s, rsl_rl in 133 s, TorchRL in 187 s and BenchMARL in 208 s. The curriculum sets that iteration, so this measures speed, not sample efficiency.

The details are in docs/benchmarks.md (Training throughput) and the new docs/training.md.

Checks

  • pytest: 1152 passed, 9 skipped, on CUDA with TorchRL 0.14.1. The BenchMARL tests pass in .venv-benchmarl.
  • ruff check is clean, and the docs build with -W.
  • No change to the simulator step, so the step throughput is unaffected.

🤖 Generated with Claude Code

…captured

The torchrl trainer spends most of an iteration on framework overhead:
TensorDict construction and clones per step, host syncs in the reset
check and in every TanhNormal constructor, and an eager update with an
.item() per minibatch. The simulator is a few percent of the iteration.

swarp.rl keeps the algorithm and the recipes and removes the overhead.
Per env step it replays an act graph, calls Environment.step and replays
a store graph; per iteration it replays a prepare graph (critic, GAE,
permutations) and one minibatch graph per minibatch, with one host sync
for every logged statistic. Non-finite minibatches are skipped on device
through fused Adam's found_inf. Torch only, no torchrl.

End to end it is 2.4-2.5x faster than examples/train_mappo.py at the
recipes' batch sizes, 3.1-3.4x with --compile --tf32, and the ratio holds
up to 16384 envs. On giveway both trainers reach full difficulty at the
same iteration (414 vs 415), in 83 s vs 208 s, and both final policies
solve 1.000 of 2048 eval envs. Timeouts bootstrap from V(s_t), since the
env auto-resets and has no terminal observation.

The torchrl-free half of examples/mappo.py moves to swarp.rl.recipes and
is re-exported. examples/train_mappo.py now times collection as well as
the update, so its env_sps is end to end. eval_mappo.py reads both
checkpoint formats. python -m swarp.benchmark.train_throughput measures
both trainers through their CLIs.

No change to the simulator step: swarp/core, swarp/scenarios and
swarp/interop are untouched.
Each config is a Trainer entry (command, required module), and --python CONFIG=PATH runs a config under another venv, as compare_sims does, so baselines whose libraries cannot share the main venv plug in with one line.
…hmark

examples/train_rsl_rl.py drives rsl_rl's PPO and OnPolicyRunner.learn unmodified
over a swarp Environment, one rsl_rl env per agent: the actor sees each agent's
obs, the critic the env's joint obs repeated per agent, and per-env dones and
time-outs are broadcast to the env's agents. The runner's Logger is replaced by
device-side counters that write the shared metrics.csv (wall_s = rsl_rl's own
collect_time + learn_time), run the TrainSpec curriculum gate and entropy
schedule, and save rsl_rl/1 checkpoints that eval_mappo.py evaluates.

Registered as the rsl_rl config of swarp.benchmark.train_throughput, with an
rsl-rl optional extra.
examples/train_benchmarl.py runs MAPPO on a swarp scenario through BenchMARL's own
Experiment: a VMAS-style task class over SwarpEnv with the per-agent entries nested
under an "agents" group, MappoConfig/MlpConfig/ExperimentConfig set to the TrainSpec
recipes as far as BenchMARL allows, and a callback that writes metrics.csv, saves
benchmarl/1 checkpoints, runs the curriculum gate and the entropy schedule. Deviations
from swarp.rl's MAPPO are listed in the module docstring.

eval_mappo.py evaluates benchmarl/1 checkpoints (actor stored in swarp.rl's layout, no
benchmarl import), train_throughput registers the benchmarl config, and the benchmarl
extra is declared conflicting with torchrl in the uv lock since BenchMARL 1.5.2 pins
torchrl < 0.12.
Keeps both the rsl_rl and benchmarl entries in train_throughput and eval_mappo, and relocks so uv.lock carries the rsl-rl extra alongside benchmarl's.
Split parse_args into build_parser + config_from_args, the loss construction into
build_loss, and GAE plus the PPO epochs into an EagerUpdate class that train() takes
as an argument, together with Collector overrides. A derived trainer can now swap the
update and the collector settings while sharing the rest of the loop.

Behaviour is unchanged: a seeded CPU run on navigation and giveway gives byte-identical
metrics (wall time aside) and final weights before and after.
examples/train_mappo_tuned.py runs train_mappo's algorithm with a CUDA-graphed
collector policy (cudagraph_policy), the update step in tensordict's CudaGraphModule,
fused capturable Adam with an on-device non-finite skip through found_inf, one host
read per iteration, and optional --compile/--tf32. Two TorchRL 0.13 issues are worked
around: TanhNormal's float bounds are an H2D copy that cannot be captured, and
cudagraph_policy stored the graph's static output buffers in every step of a batch, so
the whole batch carried the last step's action and nothing learnt.

Registered in train_throughput as torchrl-tuned (graphs, fp32) and torchrl-tuned-fast
(+ --compile --tf32). Navigation, 512 envs, RTX 3070 Laptop: 113 and 94 ms/iter against
241 for train_mappo and 99.5/75 for swarp.rl graphs/fast. A GPU test checks the
captured update against EagerUpdate on fresh batches through warm-up, capture and
replay, and that a non-finite batch is skipped without touching the weights.
… them

docs/benchmarks.md's training-throughput tables now cover TorchRL as usually written, TorchRL with its own speed features, BenchMARL and rsl_rl at the recipes' sizes and over n_envs. Against tuned TorchRL swarp.rl is 1.1-1.4x faster rather than 2-3x, so the README and training page state both comparisons.
Environment.step_count returns the live per-env counter that max_steps truncates on, so the rsl_rl adapter no longer reads env._step_count; swarp.rl.check_action_box replaces the private helper BenchMARL's script imported.
A read-only property off the step path: no change to the step, so no throughput run.
…e seeds

The lockfile now resolves torchrl 0.14.1 and tensordict 0.14.3. TorchRL 0.14.0 fixed the cudagraph_policy aliasing bug (pytorch/rl#4143); the tuned trainer learns on 0.14.1 with or without FreshOutputs, at the same ms/iteration, so it stays for 0.13. Both TorchRL trainers time within 4% of their 0.13.3 numbers, as does swarp.rl alongside them.

Giveway, full recipe, seeds 0-2: every trainer reaches full difficulty at iteration 413-417 and solves all 2048 evaluation envs, so time-to-solve follows iteration time; the curriculum, not learning, sets that iteration.
@ddebenedittis
ddebenedittis merged commit 667bcaf into main Oct 17, 2026
6 checks passed
@ddebenedittis
ddebenedittis deleted the feat/fast-trainer branch October 17, 2026 00:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant