# rk289g/grpo-control architecture

> Architecture diagram of the GitHub repository rk289g/grpo-control, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-06-01). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/rk289g/grpo-control
- Repository: https://github.com/rk289g/grpo-control

## Overview

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: [`trainers/trainer_onpolicy.py`](https://github.com/rk289g/grpo-control/blob/main/trainers/trainer_onpolicy.py) (On-policy trainer), [`my_logging/logger.py`](https://github.com/rk289g/grpo-control/blob/main/my_logging/logger.py) (Logger), [`main.py`](https://github.com/rk289g/grpo-control/blob/main/main.py) (Main CLI), [`algorithms/ppo.py`](https://github.com/rk289g/grpo-control/blob/main/algorithms/ppo.py) (PPO)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_grp_experiment["Experiment orchestration"]
  node_main["Main CLI<br/>entrypoint<br/>[main.py]"]
  node_run_single["Single run<br/>experiment mode<br/>[run_single.py]"]
  node_run_multiseed["Multi-seed<br/>experiment mode<br/>[run_multiseed.py]"]
  node_run_grid["Grid sweep<br/>experiment mode<br/>[run_grid.py]"]
  node_utils_config["Base config<br/>config defaults<br/>[config.py]"]
  node_pipeline_config["Pipeline config<br/>config specialization<br/>[config.py]"]
end

subgraph group_grp_core["Learning core"]
  node_trainer_onpolicy["On-policy trainer"]
  node_trainer_sac["SAC trainer<br/>[trainer_sac.py]"]
  node_trainer_td3["TD3 trainer<br/>[trainer_td3.py]"]
  node_ppo["PPO<br/>on-policy algo<br/>[ppo.py]"]
  node_grpo["GRPO<br/>on-policy algo<br/>[grpo.py]"]
  node_grpo_v2["GRPO v2<br/>on-policy algo<br/>[grpo_v2.py]"]
  node_grpo_v3["GRPO v3<br/>on-policy algo<br/>[grpo_v3.py]"]
  node_sac["SAC<br/>off-policy algo<br/>[sac.py]"]
  node_td3["TD3<br/>off-policy algo<br/>[td3.py]"]
  node_buffers[("Buffers<br/>storage layer")]
  node_on_policy_buffer["On-policy buffer<br/>trajectory buffer"]
  node_replay_buffer["Replay buffer<br/>experience buffer<br/>[replay_buffer.py]"]
  node_models{{"Models<br/>networks"}}
  node_actor["Actor<br/>network<br/>[actor.py]"]
  node_critic["Critic<br/>network<br/>[critic.py]"]
  node_policy["Policy<br/>network<br/>[policy.py]"]
  node_q_network["Q network<br/>[q_network.py]"]
  node_value["Value net<br/>network<br/>[value.py]"]
  node_evaluator["Evaluator<br/>evaluation<br/>[evaluator.py]"]
end

subgraph group_grp_env["Environment layer"]
  node_gym_env["Gym env<br/>env wrapper<br/>[gym_env.py]"]
  node_dmc_env["DMC env<br/>env wrapper<br/>[dmc_env.py]"]
end

subgraph group_grp_analysis["Offline analysis"]
  node_logger["Logger<br/>[logger.py]"]
  node_wandb_logger["W&B logger<br/>optional logger<br/>[wandb_logger.py]"]
  node_aggregate["Aggregate<br/>postprocess<br/>[aggregate.py]"]
  node_plot_curves["Plot curves<br/>plotting<br/>[plot_curves.py]"]
end

subgraph group_grp_docs["Reporting"]
  node_report_main["Report source<br/>paper source<br/>[main.tex]"]
  node_report_pdf["Final report<br/>artifact<br/>[GRPO_Report.pdf]"]
end

node_main -->|"load defaults"| node_utils_config
node_main -->|"specialize"| node_pipeline_config
node_main -->|"dispatch"| node_run_single
node_main -->|"dispatch"| node_run_multiseed
node_main -->|"dispatch"| node_run_grid
node_run_single -->|"launch"| node_trainer_onpolicy
node_run_multiseed -->|"launch"| node_trainer_onpolicy
node_run_grid -->|"launch"| node_trainer_onpolicy
node_trainer_onpolicy -->|"uses"| node_ppo
node_trainer_onpolicy -->|"uses"| node_grpo
node_trainer_onpolicy -->|"uses"| node_grpo_v2
node_trainer_onpolicy -->|"uses"| node_grpo_v3
node_trainer_sac -->|"uses"| node_sac
node_trainer_td3 -->|"uses"| node_td3
node_ppo -->|"collects"| node_on_policy_buffer
node_grpo -->|"collects"| node_on_policy_buffer
node_grpo_v2 -->|"collects"| node_on_policy_buffer
node_grpo_v3 -->|"collects"| node_on_policy_buffer
node_sac -->|"samples"| node_replay_buffer
node_td3 -->|"samples"| node_replay_buffer
node_ppo -->|"updates"| node_models
node_grpo -->|"updates"| node_models
node_grpo_v2 -->|"updates"| node_models
node_grpo_v3 -->|"updates"| node_models
node_sac -->|"updates"| node_models
node_td3 -->|"updates"| node_models
node_trainer_onpolicy -->|"periodic eval"| node_evaluator
node_trainer_sac -->|"periodic eval"| node_evaluator
node_trainer_td3 -->|"periodic eval"| node_evaluator
node_evaluator -->|"write metrics"| node_logger
node_logger -->|"feed logs"| node_aggregate
node_aggregate -->|"summarize"| node_plot_curves
node_ppo -->|"runs in"| node_gym_env
node_grpo -->|"runs in"| node_gym_env
node_grpo_v2 -->|"runs in"| node_gym_env
node_grpo_v3 -->|"runs in"| node_gym_env
node_sac -->|"runs in"| node_dmc_env
node_td3 -->|"runs in"| node_dmc_env
node_trainer_onpolicy -->|"log"| node_logger
node_trainer_sac -->|"log"| node_logger
node_trainer_td3 -->|"log"| node_logger
node_logger -.->|"optional"| node_wandb_logger
node_plot_curves -->|"inform"| node_report_main
node_report_main -->|"build"| node_report_pdf

click node_main "https://github.com/rk289g/grpo-control/blob/main/main.py"
click node_run_single "https://github.com/rk289g/grpo-control/blob/main/experiments/run_single.py"
click node_run_multiseed "https://github.com/rk289g/grpo-control/blob/main/experiments/run_multiseed.py"
click node_run_grid "https://github.com/rk289g/grpo-control/blob/main/experiments/run_grid.py"
click node_utils_config "https://github.com/rk289g/grpo-control/blob/main/utils/config.py"
click node_pipeline_config "https://github.com/rk289g/grpo-control/blob/main/pipeline/config.py"
click node_trainer_onpolicy "https://github.com/rk289g/grpo-control/blob/main/trainers/trainer_onpolicy.py"
click node_trainer_sac "https://github.com/rk289g/grpo-control/blob/main/trainers/trainer_sac.py"
click node_trainer_td3 "https://github.com/rk289g/grpo-control/blob/main/trainers/trainer_td3.py"
click node_ppo "https://github.com/rk289g/grpo-control/blob/main/algorithms/ppo.py"
click node_grpo "https://github.com/rk289g/grpo-control/blob/main/algorithms/grpo.py"
click node_grpo_v2 "https://github.com/rk289g/grpo-control/blob/main/algorithms/grpo_v2.py"
click node_grpo_v3 "https://github.com/rk289g/grpo-control/blob/main/algorithms/grpo_v3.py"
click node_sac "https://github.com/rk289g/grpo-control/blob/main/algorithms/sac.py"
click node_td3 "https://github.com/rk289g/grpo-control/blob/main/algorithms/td3.py"
click node_on_policy_buffer "https://github.com/rk289g/grpo-control/blob/main/buffers/on_policy_buffer.py"
click node_replay_buffer "https://github.com/rk289g/grpo-control/blob/main/buffers/replay_buffer.py"
click node_actor "https://github.com/rk289g/grpo-control/blob/main/models/actor.py"
click node_critic "https://github.com/rk289g/grpo-control/blob/main/models/critic.py"
click node_policy "https://github.com/rk289g/grpo-control/blob/main/models/policy.py"
click node_q_network "https://github.com/rk289g/grpo-control/blob/main/models/q_network.py"
click node_value "https://github.com/rk289g/grpo-control/blob/main/models/value.py"
click node_evaluator "https://github.com/rk289g/grpo-control/blob/main/evaluation/evaluator.py"
click node_gym_env "https://github.com/rk289g/grpo-control/blob/main/envs/gym_env.py"
click node_dmc_env "https://github.com/rk289g/grpo-control/blob/main/envs/dmc_env.py"
click node_logger "https://github.com/rk289g/grpo-control/blob/main/my_logging/logger.py"
click node_wandb_logger "https://github.com/rk289g/grpo-control/blob/main/my_logging/wandb_logger.py"
click node_aggregate "https://github.com/rk289g/grpo-control/blob/main/plots/aggregate.py"
click node_plot_curves "https://github.com/rk289g/grpo-control/blob/main/plots/plot_curves.py"
click node_report_main "https://github.com/rk289g/grpo-control/blob/main/report/main.tex"
click node_report_pdf "https://github.com/rk289g/grpo-control/blob/main/report/GRPO_Report.pdf"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_main,node_run_single,node_run_multiseed,node_run_grid,node_utils_config,node_pipeline_config toneBlue
class node_trainer_onpolicy,node_trainer_sac,node_trainer_td3,node_ppo,node_grpo,node_grpo_v2,node_grpo_v3,node_sac,node_td3,node_buffers,node_on_policy_buffer,node_replay_buffer,node_models,node_actor,node_critic,node_policy,node_q_network,node_value,node_evaluator toneAmber
class node_gym_env,node_dmc_env toneMint
class node_logger,node_wandb_logger,node_aggregate,node_plot_curves toneRose
class node_report_main,node_report_pdf toneIndigo
```

## Components

### Experiment orchestration

Command-line entrypoints and batch run modes.

- [Main CLI](https://github.com/rk289g/grpo-control/blob/main/main.py): entrypoint · Dispatches into single run, multiseed, grid, or plot-only modes. · `main.py`
- [Single run](https://github.com/rk289g/grpo-control/blob/main/experiments/run_single.py): experiment mode · Runs one training job with a selected config. · `experiments/run_single.py`
- [Multi-seed](https://github.com/rk289g/grpo-control/blob/main/experiments/run_multiseed.py): experiment mode · Executes repeated runs across seeds for reproducibility. · `experiments/run_multiseed.py`
- [Grid sweep](https://github.com/rk289g/grpo-control/blob/main/experiments/run_grid.py): experiment mode · Launches full cross-product sweeps over settings. · `experiments/run_grid.py`
- [Base config](https://github.com/rk289g/grpo-control/blob/main/utils/config.py): config defaults · Shared config helpers and defaults for runs. · `utils/config.py`
- [Pipeline config](https://github.com/rk289g/grpo-control/blob/main/pipeline/config.py): config specialization · Experiment-layer config adjustment and specialization. · `pipeline/config.py`

### Learning core

Algorithms, models, buffers, trainers, and evaluation.

- [On-policy trainer](https://github.com/rk289g/grpo-control/blob/main/trainers/trainer_onpolicy.py): trainer · Coordinates rollout collection, updates, evaluation, and logging for PPO/GRPO. · `trainers/trainer_onpolicy.py`
- [SAC trainer](https://github.com/rk289g/grpo-control/blob/main/trainers/trainer_sac.py): trainer · Runs replay-based SAC training and evaluation. · `trainers/trainer_sac.py`
- [TD3 trainer](https://github.com/rk289g/grpo-control/blob/main/trainers/trainer_td3.py): trainer · Runs replay-based TD3 training and evaluation. · `trainers/trainer_td3.py`
- [PPO](https://github.com/rk289g/grpo-control/blob/main/algorithms/ppo.py): on-policy algo · Proximal policy optimization implementation. · `algorithms/ppo.py`
- [GRPO](https://github.com/rk289g/grpo-control/blob/main/algorithms/grpo.py): on-policy algo · Baseline GRPO implementation with shared utilities. · `algorithms/grpo.py`
- [GRPO v2](https://github.com/rk289g/grpo-control/blob/main/algorithms/grpo_v2.py): on-policy algo · Versioned GRPO variant with separate experiment lineage. · `algorithms/grpo_v2.py`
- [GRPO v3](https://github.com/rk289g/grpo-control/blob/main/algorithms/grpo_v3.py): on-policy algo · Latest versioned GRPO variant with separate experiment lineage. · `algorithms/grpo_v3.py`
- [SAC](https://github.com/rk289g/grpo-control/blob/main/algorithms/sac.py): off-policy algo · Soft actor-critic implementation using replay updates. · `algorithms/sac.py`
- [TD3](https://github.com/rk289g/grpo-control/blob/main/algorithms/td3.py): off-policy algo · Twin delayed deep deterministic policy gradient implementation. · `algorithms/td3.py`
- **Buffers**: storage layer · Holds trajectory and replay storage used by different learning paradigms.
- [On-policy buffer](https://github.com/rk289g/grpo-control/blob/main/buffers/on_policy_buffer.py): trajectory buffer · Stores fresh rollouts for PPO/GRPO-style updates. · `buffers/on_policy_buffer.py`
- [Replay buffer](https://github.com/rk289g/grpo-control/blob/main/buffers/replay_buffer.py): experience buffer · Stores transitions for SAC/TD3 off-policy sampling. · `buffers/replay_buffer.py`
- **Models**: networks · Shared function approximators for policy and value learning.
- [Actor](https://github.com/rk289g/grpo-control/blob/main/models/actor.py): network · Action-producing network used by policy-gradient methods and actor-critic methods. · `models/actor.py`
- [Critic](https://github.com/rk289g/grpo-control/blob/main/models/critic.py): network · Value estimator used for advantage and policy learning. · `models/critic.py`
- [Policy](https://github.com/rk289g/grpo-control/blob/main/models/policy.py): network · Policy module shared across algorithms. · `models/policy.py`
- [Q network](https://github.com/rk289g/grpo-control/blob/main/models/q_network.py): network · Action-value network for off-policy learning. · `models/q_network.py`
- [Value net](https://github.com/rk289g/grpo-control/blob/main/models/value.py): network · State-value network used in learning and evaluation support. · `models/value.py`
- [Evaluator](https://github.com/rk289g/grpo-control/blob/main/evaluation/evaluator.py): evaluation · Periodically runs the current policy and emits metrics. · `evaluation/evaluator.py`

### Environment layer

Wrappers around external simulators and task backends.

- [Gym env](https://github.com/rk289g/grpo-control/blob/main/envs/gym_env.py): env wrapper · Gymnasium wrapper for CarRacing-v3 with flattened grayscale observations. · `envs/gym_env.py`
- [DMC env](https://github.com/rk289g/grpo-control/blob/main/envs/dmc_env.py): env wrapper · DeepMind Control wrapper for low-dimensional swingup tasks. · `envs/dmc_env.py`

### Offline analysis

Logging, aggregation, and plotting of experiment results.

- [Logger](https://github.com/rk289g/grpo-control/blob/main/my_logging/logger.py): json logger · Writes training and evaluation metrics to local JSON logs. · `my_logging/logger.py`
- [W&B logger](https://github.com/rk289g/grpo-control/blob/main/my_logging/wandb_logger.py): optional logger · Optional experiment tracking integration. · `my_logging/wandb_logger.py`
- [Aggregate](https://github.com/rk289g/grpo-control/blob/main/plots/aggregate.py): postprocess · Combines raw result logs into summary data. · `plots/aggregate.py`
- [Plot curves](https://github.com/rk289g/grpo-control/blob/main/plots/plot_curves.py): plotting · Generates comparison figures from aggregated results. · `plots/plot_curves.py`

### Reporting

Paper source and exported report artifacts.

- [Report source](https://github.com/rk289g/grpo-control/blob/main/report/main.tex): paper source · Primary LaTeX source for the accompanying paper. · `report/main.tex`
- [Final report](https://github.com/rk289g/grpo-control/blob/main/report/GRPO_Report.pdf): artifact · Compiled paper artifact produced from the report sources. · `report/GRPO_Report.pdf`

## Connections

- 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.
