How rk289g/grpo-control works
1. This repository is an RL experiment suite centered on a single training/evaluation pipeline. It compares PPO, GRPO v1/v2/v3, SAC, and TD3 across classic-control and simple-vision environments, then aggregates training logs into comparison plots.
2. The top-level entrypoint is main.py, which dispatches into four operational modes: single training run, multi-seed sweep, full grid sweep, and plotting only. That makes the system operate like an experiment runner rather than a serving application.
3. Configuration is split between utils/config.py and pipeline/config.py, with adjust_config()-style logic in the experiment layer to specialize defaults per environment. The key architectural role of config is to determine env type, algorithm, seeds, rollout length, evaluation cadence, device, and result directory.
4. Environment integration lives under envs/ and abstracts two backends: Gymnasium for CarRacing-v3 and DeepMind Control for cartpole_swingup and acrobot_swingup. The Gym path uses flattened grayscale image observations, while the DMC path handles low-dimensional state control tasks. These wrappers are the boundary between algorithm code and external simulators.
5. The core learning logic is split by algorithm family in algorithms/. On-policy methods (ppo.py, grpo.py, grpo_v2.py, grpo_v3.py) share a rollout/evaluate/update style, while off-policy methods (sac.py, td3.py) rely on replay-based updates. GRPO variants are versioned as separate implementations, which is reflected in separate result directories to avoid overwriting earlier experiments.
6. Model definitions are factored into reusable network components under models/: actor, critic, policy, Q-network, and value network modules. These are the shared function approximators used by the algorithms, and they represent the main trainable parameter boundary in the system.
7. Experience storage is separated by learning paradigm in buffers/. on_policy_buffer.py supports trajectory-style collection for PPO/GRPO, while replay_buffer.py supports SAC/TD3 off-policy learning. Buffer choice determines whether the trainer consumes fresh rollouts or sampled past transitions.
8. Training orchestration sits in trainers/, with distinct trainers for on-policy, SAC, and TD3 flows. The trainers connect environment interaction, buffer population, optimization steps, periodic evaluation, and logging. This is the main execution layer where algorithm logic and environment stepping are coordinated.
9. Evaluation is isolated in evaluation/evaluator.py, which periodically runs the current policy and emits metrics such as evaluation return. Those metrics are written through my_logging/ as JSON logs, with an optional Weights & Biases logger present but not central to the repo’s primary workflow. The architecture’s durable output is therefore local JSON rather than a database or remote service.
10. The experiment layer in experiments/ defines the batch execution patterns: run_single.py, run_multiseed.py, and run_grid.py. This is where the repo encodes reproducibility policies such as seed sweeps, skipping already-completed runs, and versioned output directories for GRPO v2/v3. It is the boundary between configuration and actual training jobs.
11. Plotting is a separate post-processing pipeline under plots/. It reads raw JSON logs from results/raw, results_v2/raw, and results_v3/raw, aggregates them, and writes a single processed figure to results/processed/final_plot.png. The plotting layer is intentionally decoupled from training so visualization can be rerun without recomputing experiments.
12. The results/, results_v2/, and results_v3/ trees are part of the architecture, not just output dumps: they encode the experiment lineage by algorithm version. The report/ directory is a downstream artifact for the accompanying paper, pulling from the same experimental results but not participating in runtime training.
13. The overall data flow is: CLI command → config selection → environment wrapper creation → trainer loop → algorithm/model/buffer updates → evaluator metrics → JSON logging → plotting/aggregation. The main architectural boundary is between online interaction/training and offline analysis/reporting, with environment simulators on one side and result-processing/report-generation on the other.
Main entry points: trainer_onpolicy.py On-policy trainer · logger.py Logger · main.py Main CLI · ppo.py PPO
Components 33
Experiment orchestration
Command-line entrypoints and batch run modes.
- Main CLI · Dispatches into single run, multiseed, grid, or plot-only modes.
main.py - Single run · Runs one training job with a selected config.
experiments/run_single.py - Multi-seed · Executes repeated runs across seeds for reproducibility.
experiments/run_multiseed.py - Grid sweep · Launches full cross-product sweeps over settings.
experiments/run_grid.py - Base config · Shared config helpers and defaults for runs.
utils/config.py - Pipeline config · Experiment-layer config adjustment and specialization.
pipeline/config.py
Learning core
Algorithms, models, buffers, trainers, and evaluation.
- On-policy trainer · Coordinates rollout collection, updates, evaluation, and logging for PPO/GRPO.
trainers/trainer_onpolicy.py - SAC trainer · Runs replay-based SAC training and evaluation.
trainers/trainer_sac.py - TD3 trainer · Runs replay-based TD3 training and evaluation.
trainers/trainer_td3.py - PPO · Proximal policy optimization implementation.
algorithms/ppo.py - GRPO · Baseline GRPO implementation with shared utilities.
algorithms/grpo.py - GRPO v2 · Versioned GRPO variant with separate experiment lineage.
algorithms/grpo_v2.py - GRPO v3 · Latest versioned GRPO variant with separate experiment lineage.
algorithms/grpo_v3.py - SAC · Soft actor-critic implementation using replay updates.
algorithms/sac.py - TD3 · Twin delayed deep deterministic policy gradient implementation.
algorithms/td3.py - Buffers · Holds trajectory and replay storage used by different learning paradigms.
- On-policy buffer · Stores fresh rollouts for PPO/GRPO-style updates.
buffers/on_policy_buffer.py - Replay buffer · Stores transitions for SAC/TD3 off-policy sampling.
buffers/replay_buffer.py - Models · Shared function approximators for policy and value learning.
- Actor · Action-producing network used by policy-gradient methods and actor-critic methods.
models/actor.py - Critic · Value estimator used for advantage and policy learning.
models/critic.py - Policy · Policy module shared across algorithms.
models/policy.py - Q network · Action-value network for off-policy learning.
models/q_network.py - Value net · State-value network used in learning and evaluation support.
models/value.py - Evaluator · Periodically runs the current policy and emits metrics.
evaluation/evaluator.py
Environment layer
Wrappers around external simulators and task backends.
Offline analysis
Logging, aggregation, and plotting of experiment results.
- Logger · Writes training and evaluation metrics to local JSON logs.
my_logging/logger.py - W&B logger · Optional experiment tracking integration.
my_logging/wandb_logger.py - Aggregate · Combines raw result logs into summary data.
plots/aggregate.py - Plot curves · Generates comparison figures from aggregated results.
plots/plot_curves.py
Reporting
Paper source and exported report artifacts.
- Report source · Primary LaTeX source for the accompanying paper.
report/main.tex - Final report · Compiled paper artifact produced from the report sources.
report/GRPO_Report.pdf
Connections 44
- Main CLI → Base config: load defaults · Entry point starts by reading base configuration.
- Main CLI → Pipeline config: specialize · Experiment layer adjusts defaults per task and algorithm.
- Main CLI → Single run: dispatch · CLI can invoke a single training job.
- Main CLI → Multi-seed: dispatch · CLI can invoke a seed sweep.
- Main CLI → Grid sweep: dispatch · CLI can invoke a full grid sweep.
- Single run → On-policy trainer: launch · Single-run mode starts the selected trainer.
- Multi-seed → On-policy trainer: launch · Multi-seed mode repeats on-policy runs.
- Grid sweep → On-policy trainer: launch · Grid mode schedules training jobs across settings.
- On-policy trainer → PPO: uses · On-policy trainer can execute PPO.
- On-policy trainer → GRPO: uses · On-policy trainer can execute baseline GRPO.
- On-policy trainer → GRPO v2: uses · On-policy trainer can execute GRPO v2.
- On-policy trainer → GRPO v3: uses · On-policy trainer can execute GRPO v3.
- SAC trainer → SAC: uses · SAC trainer drives SAC updates.
- TD3 trainer → TD3: uses · TD3 trainer drives TD3 updates.
- PPO → On-policy buffer: collects · PPO consumes fresh trajectories.
- GRPO → On-policy buffer: collects · GRPO consumes trajectory batches.
- GRPO v2 → On-policy buffer: collects · GRPO v2 consumes trajectory batches.
- GRPO v3 → On-policy buffer: collects · GRPO v3 consumes trajectory batches.
- SAC → Replay buffer: samples · SAC learns from replayed transitions.
- TD3 → Replay buffer: samples · TD3 learns from replayed transitions.
- PPO → Models: updates · PPO trains shared network components.
- GRPO → Models: updates · GRPO trains shared network components.
- GRPO v2 → Models: updates · GRPO v2 trains shared network components.
- GRPO v3 → Models: updates · GRPO v3 trains shared network components.
- SAC → Models: updates · SAC trains shared network components.
- TD3 → Models: updates · TD3 trains shared network components.
- On-policy trainer → Evaluator: periodic eval · Trainer invokes evaluation during training.
- SAC trainer → Evaluator: periodic eval · SAC trainer invokes evaluation during training.
- TD3 trainer → Evaluator: periodic eval · TD3 trainer invokes evaluation during training.
- Evaluator → Logger: write metrics · Evaluation outputs are recorded as local JSON logs.
- Logger → Aggregate: feed logs · Raw JSON logs are consumed by the aggregation pipeline.
- Aggregate → Plot curves: summarize · Aggregated metrics are turned into plots.
- PPO → Gym env: runs in · PPO can interact with the Gymnasium CarRacing wrapper.
- GRPO → Gym env: runs in · GRPO can interact with the Gymnasium CarRacing wrapper.
- GRPO v2 → Gym env: runs in · GRPO v2 can interact with the Gymnasium CarRacing wrapper.
- GRPO v3 → Gym env: runs in · GRPO v3 can interact with the Gymnasium CarRacing wrapper.
- SAC → DMC env: runs in · SAC can interact with DMC swingup tasks.
- TD3 → DMC env: runs in · TD3 can interact with DMC swingup tasks.
- On-policy trainer → Logger: log · Trainer records progress and metrics.
- SAC trainer → Logger: log · Trainer records progress and metrics.
- TD3 trainer → Logger: log · Trainer records progress and metrics.
- Logger → W&B logger: optional · Local logging can be mirrored to W&B.
- Plot curves → Report source: inform · Plots and summaries feed the paper source.
- Report source → Final report: build · LaTeX source compiles into the final PDF artifact.