Reinforcement Learning
stable-baselines3
ppo
compiler-optimization
gcc
deep-reinforcement-learning
cpugym
Eval Results (legacy)
Instructions to use callensxavier/cpugym-v5-gcc-optimizer with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- stable-baselines3
How to use callensxavier/cpugym-v5-gcc-optimizer with stable-baselines3:
from huggingface_sb3 import load_from_hub checkpoint = load_from_hub( repo_id="callensxavier/cpugym-v5-gcc-optimizer", filename="{MODEL FILENAME}.zip", ) - Notebooks
- Google Colab
- Kaggle
xaviercallens commited on
Commit ·
c3c34a3
1
Parent(s): 0e8b1fc
V5 Run 2 best model — PPO GCC flag optimizer (SB3)
Browse files- Architecture: PPO with Discrete(17) action space, Box(24) observations
- Training: 500k steps, curriculum learning (O0→O1→O2→best_known)
- Eval: 0/30 beat O2, 4/7 O2/O3 classification accuracy (57%)
- Model is a curriculum-in-progress checkpoint (beat_O2 phase)
- Infrastructure: Azure Container Apps, D16 profile, ~13h training
- README.md +220 -0
- best_model.zip +3 -0
- environment_metadata.json +89 -0
README.md
ADDED
|
@@ -0,0 +1,220 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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10 |
+
model-index:
|
| 11 |
+
- name: cpugym-v5-gcc-optimizer
|
| 12 |
+
results:
|
| 13 |
+
- task:
|
| 14 |
+
type: reinforcement-learning
|
| 15 |
+
name: GCC Compiler Flag Selection
|
| 16 |
+
dataset:
|
| 17 |
+
type: polybench
|
| 18 |
+
name: PolyBench/C
|
| 19 |
+
metrics:
|
| 20 |
+
- type: mean_reward
|
| 21 |
+
value: -1.77
|
| 22 |
+
name: Mean Episode Reward (best eval)
|
| 23 |
+
- type: beats_o2_pct
|
| 24 |
+
value: 0
|
| 25 |
+
name: "Programs Beating -O2 (%)"
|
| 26 |
+
- type: o2_o3_classification_accuracy
|
| 27 |
+
value: 57
|
| 28 |
+
name: "O2/O3 Classification Accuracy (%)"
|
| 29 |
+
- type: o2_o3_ground_truth_validation
|
| 30 |
+
value: 85
|
| 31 |
+
name: "O2-vs-O3 Ground Truth Validation (%)"
|
| 32 |
+
---
|
| 33 |
+
|
| 34 |
+
# cpugym-v5-gcc-optimizer
|
| 35 |
+
|
| 36 |
+
A reinforcement learning agent trained with PPO (Stable-Baselines3) to select GCC
|
| 37 |
+
optimization flags for C programs. This is a **research checkpoint** from the V5
|
| 38 |
+
convergence-first training run — the model learns sequential flag composition via
|
| 39 |
+
a curriculum but has not yet surpassed -O2 on PolyBench/C. It serves as a baseline
|
| 40 |
+
for the V6 architecture.
|
| 41 |
+
|
| 42 |
+
## Model Description
|
| 43 |
+
|
| 44 |
+
**CPUGym V5** uses a convergence-first design with:
|
| 45 |
+
- **Reduced action space**: 17 actions (12 individual flags + 4 base optimization levels + STOP)
|
| 46 |
+
- **Potential-based reward shaping** (Ng et al., 1999) for stable intermediate rewards
|
| 47 |
+
- **Curriculum learning**: O0 → O1 → O2 → best-known progressive difficulty
|
| 48 |
+
- **Behavioral cloning cold-start** from 8 expert optimization strategies
|
| 49 |
+
|
| 50 |
+
### Architecture
|
| 51 |
+
|
| 52 |
+
| Component | Details |
|
| 53 |
+
|-----------|---------|
|
| 54 |
+
| Algorithm | PPO (Proximal Policy Optimization) |
|
| 55 |
+
| Policy | MlpPolicy (64×64 hidden layers) |
|
| 56 |
+
| Observation | Box(24): [program_features(8) + flag_state(12) + base_onehot(4)] |
|
| 57 |
+
| Action | Discrete(17): STOP(0) \| toggle_flag(1-12) \| set_base(13-16) |
|
| 58 |
+
| Max steps/episode | 5 |
|
| 59 |
+
| Framework | Stable-Baselines3 |
|
| 60 |
+
|
| 61 |
+
### Optimization Flags (12)
|
| 62 |
+
|
| 63 |
+
| Category | Flags |
|
| 64 |
+
|----------|-------|
|
| 65 |
+
| Vectorization & SIMD | `-march=native`, `-ftree-vectorize` |
|
| 66 |
+
| Math | `-ffast-math` |
|
| 67 |
+
| Loop optimizations | `-funroll-loops`, `-fpeel-loops`, `-ftree-loop-distribution` |
|
| 68 |
+
| Inlining | `-finline-functions` |
|
| 69 |
+
| IPO | `-flto` (auto-adds `-fwhole-program`) |
|
| 70 |
+
| Scheduling & codegen | `-fschedule-insns2`, `-fomit-frame-pointer` |
|
| 71 |
+
| Memory | `-fstrict-aliasing` |
|
| 72 |
+
| Loop vectorization | `-ftree-loop-vectorize` |
|
| 73 |
+
|
| 74 |
+
### Base Optimization Levels (4)
|
| 75 |
+
|
| 76 |
+
`-O1`, `-O2`, `-O3`, `-Ofast`
|
| 77 |
+
|
| 78 |
+
## Training Details
|
| 79 |
+
|
| 80 |
+
### Hyperparameters
|
| 81 |
+
|
| 82 |
+
| Parameter | Value |
|
| 83 |
+
|-----------|-------|
|
| 84 |
+
| Learning rate | 1e-3 |
|
| 85 |
+
| Discount (γ) | 0.95 |
|
| 86 |
+
| GAE (λ) | 0.9 |
|
| 87 |
+
| Clip range | 0.1 |
|
| 88 |
+
| Entropy coefficient | 0.1 |
|
| 89 |
+
| Batch size | 128 |
|
| 90 |
+
| N-steps | 128 |
|
| 91 |
+
| N-epochs | 10 |
|
| 92 |
+
| Parallel environments | 16 |
|
| 93 |
+
| Total timesteps | 500000 |
|
| 94 |
+
|
| 95 |
+
### Curriculum Schedule
|
| 96 |
+
|
| 97 |
+
| Phase | Timesteps | Baseline | Target |
|
| 98 |
+
|-------|-----------|----------|--------|
|
| 99 |
+
| 1. Beat -O0 | 0–20k | `-O0` | Trivial warm-up |
|
| 100 |
+
| 2. Beat -O1 | 20k–80k | `-O1` | Learn specific flags |
|
| 101 |
+
| 3. Beat -O2 | 80k–500k | `-O2` | Core optimization target |
|
| 102 |
+
| 4. Beat best-known | 500k+ | Best found | Research frontier |
|
| 103 |
+
|
| 104 |
+
### Cold Start
|
| 105 |
+
|
| 106 |
+
Pre-trained with behavioral cloning from 8 expert strategies:
|
| 107 |
+
- Vectorization-focused (`-O3 -march=native -ftree-vectorize`)
|
| 108 |
+
- Math-aggressive (`-Ofast -ffast-math`)
|
| 109 |
+
- Loop-focused (`-O3 -funroll-loops -ftree-loop-distribution`)
|
| 110 |
+
- Full pipeline (`-O3 -march=native -flto -funroll-loops`)
|
| 111 |
+
- And 4 more domain-specific combinations
|
| 112 |
+
|
| 113 |
+
### Reward Design
|
| 114 |
+
|
| 115 |
+
- **Terminal reward**: `log(t_baseline / t_agent)` — positive when agent beats baseline
|
| 116 |
+
- **Intermediate reward**: Potential-based shaping (Φ = flag coverage ratio)
|
| 117 |
+
- **Conflict penalty**: -0.1 for selecting flags already implied by the base level
|
| 118 |
+
|
| 119 |
+
|
| 120 |
+
|
| 121 |
+
## Usage
|
| 122 |
+
|
| 123 |
+
```python
|
| 124 |
+
from stable_baselines3 import PPO
|
| 125 |
+
import numpy as np
|
| 126 |
+
|
| 127 |
+
# Load model
|
| 128 |
+
model = PPO.load("path/to/model.zip")
|
| 129 |
+
|
| 130 |
+
# Create observation (24-dim)
|
| 131 |
+
# [program_features(8) + flag_state(12) + base_onehot(4)]
|
| 132 |
+
obs = np.zeros(24, dtype=np.float32)
|
| 133 |
+
# ... set program features from extract_program_features()
|
| 134 |
+
|
| 135 |
+
# Get action
|
| 136 |
+
action, _ = model.predict(obs, deterministic=True)
|
| 137 |
+
# action 0 = STOP, 1-12 = toggle flag, 13-16 = set base level
|
| 138 |
+
```
|
| 139 |
+
|
| 140 |
+
## Intended Use
|
| 141 |
+
|
| 142 |
+
This model is designed for **compiler optimization research**. It demonstrates
|
| 143 |
+
that RL agents can learn to select GCC optimization flags via curriculum learning
|
| 144 |
+
and sequential flag composition.
|
| 145 |
+
|
| 146 |
+
**Not intended for**: Production compiler toolchains without thorough validation.
|
| 147 |
+
|
| 148 |
+
## Evaluation Results (Azure linux/amd64, GCC 10)
|
| 149 |
+
|
| 150 |
+
### Phase 1: Naive PolyBench Evaluation (30 programs × 7 baselines × 7 runs)
|
| 151 |
+
|
| 152 |
+
| Metric | Value |
|
| 153 |
+
|--------|-------|
|
| 154 |
+
| Beat -O2 | 0/30 (0%) |
|
| 155 |
+
| Beat best baseline | 0/30 (0%) |
|
| 156 |
+
| Avg speedup vs -O2 | -265.6% (3.3× slower) |
|
| 157 |
+
| Geomean time ratio vs O2 | 3.32× |
|
| 158 |
+
|
| 159 |
+
The agent at 448k steps (curriculum phase 3: `beat_O2`) selects flags that produce
|
| 160 |
+
slower code than -O2. It tends to choose `-O1` + individual flags or bare `-O2`
|
| 161 |
+
without useful additions.
|
| 162 |
+
|
| 163 |
+
### Phase 2: O2-vs-O3 Classification Test (13 synthetic benchmarks)
|
| 164 |
+
|
| 165 |
+
**11/13 passed (85%)** — validates that the test infrastructure correctly
|
| 166 |
+
differentiates O2-favorable vs O3-favorable programs on the target hardware.
|
| 167 |
+
|
| 168 |
+
| Program | O2 time | O3 time | Speedup | Category |
|
| 169 |
+
|---------|---------|---------|---------|----------|
|
| 170 |
+
| dense_matmul | 0.416s | 0.224s | 1.86× | O3-favorable |
|
| 171 |
+
| simd_vectorize | 0.321s | 0.182s | 1.76× | O3-favorable |
|
| 172 |
+
| stencil_2d | 0.182s | 0.143s | 1.27× | O3-favorable |
|
| 173 |
+
| loop_unroll_target | 0.098s | 0.083s | 1.19× | O3-favorable |
|
| 174 |
+
| branch_heavy | 0.808s | 0.810s | 1.00× | O2-favorable |
|
| 175 |
+
| linked_list_walk | 4.871s | 4.891s | 1.00× | O2-favorable |
|
| 176 |
+
| icache_pressure | 0.065s | 0.065s | 1.00× | O2-favorable |
|
| 177 |
+
|
| 178 |
+
### Phase 3: Agent O2/O3 Flag Selection (7 known-outcome programs)
|
| 179 |
+
|
| 180 |
+
**4/7 correct (57%)** — the agent always defaults to O2 as base level (correct
|
| 181 |
+
for O2-favorable programs, wrong for O3-favorable ones like dense_matmul,
|
| 182 |
+
stencil_2d, vector_reduction). This is expected: the model was still in the
|
| 183 |
+
`beat_O2` curriculum phase and hadn't learned when to escalate to O3.
|
| 184 |
+
|
| 185 |
+
| Program | Expected | Agent chose | Result |
|
| 186 |
+
|---------|----------|-------------|--------|
|
| 187 |
+
| branch_heavy | O2 | O2 | CORRECT |
|
| 188 |
+
| icache_pressure | O2 | O2 | CORRECT |
|
| 189 |
+
| linked_list_walk | O2 | O2 | CORRECT |
|
| 190 |
+
| sort_and_search | O2 | O2 | CORRECT |
|
| 191 |
+
| dense_matmul | O3 | O2 | WRONG |
|
| 192 |
+
| stencil_2d | O3 | O2 | WRONG |
|
| 193 |
+
| vector_reduction | O3 | O2 | WRONG |
|
| 194 |
+
|
| 195 |
+
### Interpretation
|
| 196 |
+
|
| 197 |
+
This checkpoint is a **curriculum-in-progress model**: it learned "O2 is safe"
|
| 198 |
+
but hasn't discovered when O3/Ofast provides measurable benefit. The V6
|
| 199 |
+
architecture addresses this with synthetic data augmentation, LLM-generated
|
| 200 |
+
training programs with known-optimal flags, and extended training (1M+ steps).
|
| 201 |
+
|
| 202 |
+
## Training Infrastructure
|
| 203 |
+
|
| 204 |
+
- Azure Container Apps (D16 workload profile, 16 vCPU, linux/amd64)
|
| 205 |
+
- Training cost: ~$65
|
| 206 |
+
- Training time: ~13 hours (500k timesteps)
|
| 207 |
+
|
| 208 |
+
## Citation
|
| 209 |
+
|
| 210 |
+
```bibtex
|
| 211 |
+
@software{cpugym_v5,
|
| 212 |
+
title={CPUGym V5: Convergence-First GCC Optimization via Reinforcement Learning},
|
| 213 |
+
year={2026},
|
| 214 |
+
url={https://github.com/pznachab_amadeus/CPUGym}
|
| 215 |
+
}
|
| 216 |
+
```
|
| 217 |
+
|
| 218 |
+
## License
|
| 219 |
+
|
| 220 |
+
MIT
|
best_model.zip
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:34f6335bf3fdf29f8b3f1f6e560eacda3ba4114d8675c98e47dd1e7f3f46ce74
|
| 3 |
+
size 185884
|
environment_metadata.json
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"observation_space": {
|
| 3 |
+
"type": "Box",
|
| 4 |
+
"shape": [
|
| 5 |
+
24
|
| 6 |
+
],
|
| 7 |
+
"components": {
|
| 8 |
+
"program_features": {
|
| 9 |
+
"indices": [
|
| 10 |
+
0,
|
| 11 |
+
7
|
| 12 |
+
],
|
| 13 |
+
"dim": 8
|
| 14 |
+
},
|
| 15 |
+
"flag_state": {
|
| 16 |
+
"indices": [
|
| 17 |
+
8,
|
| 18 |
+
19
|
| 19 |
+
],
|
| 20 |
+
"dim": 12
|
| 21 |
+
},
|
| 22 |
+
"base_onehot": {
|
| 23 |
+
"indices": [
|
| 24 |
+
20,
|
| 25 |
+
23
|
| 26 |
+
],
|
| 27 |
+
"dim": 4
|
| 28 |
+
}
|
| 29 |
+
}
|
| 30 |
+
},
|
| 31 |
+
"action_space": {
|
| 32 |
+
"type": "Discrete",
|
| 33 |
+
"n": 17,
|
| 34 |
+
"mapping": {
|
| 35 |
+
"0": "STOP",
|
| 36 |
+
"1": "toggle_march-native",
|
| 37 |
+
"2": "toggle_ftree-vectorize",
|
| 38 |
+
"3": "toggle_ffast-math",
|
| 39 |
+
"4": "toggle_funroll-loops",
|
| 40 |
+
"5": "toggle_fpeel-loops",
|
| 41 |
+
"6": "toggle_ftree-loop-distribution",
|
| 42 |
+
"7": "toggle_finline-functions",
|
| 43 |
+
"8": "toggle_flto",
|
| 44 |
+
"9": "toggle_fschedule-insns2",
|
| 45 |
+
"10": "toggle_fomit-frame-pointer",
|
| 46 |
+
"11": "toggle_fstrict-aliasing",
|
| 47 |
+
"12": "toggle_ftree-loop-vectorize",
|
| 48 |
+
"13": "set_base_O1",
|
| 49 |
+
"14": "set_base_O2",
|
| 50 |
+
"15": "set_base_O3",
|
| 51 |
+
"16": "set_base_Ofast"
|
| 52 |
+
}
|
| 53 |
+
},
|
| 54 |
+
"max_episode_steps": 5,
|
| 55 |
+
"reward_type": "potential_based_shaping_plus_log_ratio",
|
| 56 |
+
"curriculum_phases": [
|
| 57 |
+
{
|
| 58 |
+
"name": "beat_O0",
|
| 59 |
+
"range": [
|
| 60 |
+
0,
|
| 61 |
+
20000
|
| 62 |
+
]
|
| 63 |
+
},
|
| 64 |
+
{
|
| 65 |
+
"name": "beat_O1",
|
| 66 |
+
"range": [
|
| 67 |
+
20000,
|
| 68 |
+
80000
|
| 69 |
+
]
|
| 70 |
+
},
|
| 71 |
+
{
|
| 72 |
+
"name": "beat_O2",
|
| 73 |
+
"range": [
|
| 74 |
+
80000,
|
| 75 |
+
500000
|
| 76 |
+
]
|
| 77 |
+
},
|
| 78 |
+
{
|
| 79 |
+
"name": "beat_best_known",
|
| 80 |
+
"range": [
|
| 81 |
+
500000,
|
| 82 |
+
null
|
| 83 |
+
]
|
| 84 |
+
}
|
| 85 |
+
],
|
| 86 |
+
"framework": "stable-baselines3",
|
| 87 |
+
"algorithm": "PPO",
|
| 88 |
+
"policy": "MlpPolicy"
|
| 89 |
+
}
|