Repository navigation
Add swarp.rl, a CUDA-graph MAPPO trainer, with TorchRL, BenchMARL and rsl_rl baselines - #13
Merged
Merged
Conversation
…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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
12 commits; the main threads:
swarp.rl, a torch-only MAPPO trainer with the rollout and the update CUDA-graph captured. Same algorithm, recipes andmetrics.csvcolumns asexamples/train_mappo.py, which stays as the reference. Non-finite minibatches are skipped on device through Adam'sfound_inf, and each iteration has one host sync.python -m swarp.rl --scenario <name>, with--compile --tf32for the fastest configuration. The torchrl-free recipe code (TrainSpec,SPECS,SolveRateWindow,score) moved toswarp.rl.recipes.examples/train_mappo_tuned.py(TorchRL with its own speed features),examples/train_benchmarl.py(BenchMARL's MAPPO, in its own venv because BenchMARL pinstorchrl < 0.12) andexamples/train_rsl_rl.py(rsl_rl'sOnPolicyRunner, one env per agent). Each docstring lists how its algorithm differs fromswarp.rl.train_mappo.pywas split into importable pieces, with the same behaviour, so the tuned trainer reuses it.python -m swarp.benchmark.train_throughputtimes every trainer end to end through its real CLI, with--python CONFIG=PATHto run a trainer from another venv.Environment.step_countandswarp.rl.check_action_box.cudagraph_policybug 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:
--compile --tf32swarp.rlswarp.rl --compile --tf32swarp.rlis 1.1–1.4× faster than tuned TorchRL and 2.1–3.4× faster than TorchRL as usually written over 512–16k envs.swarp.rlgets 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 newdocs/training.md.Checks
pytest: 1152 passed, 9 skipped, on CUDA with TorchRL 0.14.1. The BenchMARL tests pass in.venv-benchmarl.ruff checkis clean, and the docs build with-W.🤖 Generated with Claude Code