Views
No views yet
-O2, -O3, -Ofast) for C programs while maintaining strict safety guarantees. The model achieves 0% functional and numeric regressions across all evaluated programs.Accuracy: 40% (vs 33% random baseline)
Mean Speedup: 0.994x (within 1% of optimal)
Functional Regressions: 0%
Numeric Regressions: 0%
Worst-case: 0.880x (-12% slowdown)
Best-case: 1.155x (+15.5% speedup)PAPER_V10C_DQN_REVISED.mdpip install stable-baselines3 numpy huggingface-hub1from huggingface_hub import hf_hub_download
2from stable_baselines3 import DQN
3import numpy as np
4
5# Download model
6model_path = hf_hub_download(
7 repo_id="callensxavier/v10c-dqn-compiler-optimization",
8 filename="model.zip"
9)
10
11# Load model
12model = DQN.load(model_path)
13
14# Extract features from your C program
15def extract_features(source_code: str) -> np.ndarray:
16 return np.array([
17 len(source_code), # file_size
18 source_code.count('\n'), # line_count
19 source_code.count('float') + source_code.count('double'), # float_count
20 source_code.count('for') + source_code.count('while'), # loop_count
21 source_code.count('*'), # pointer_count
22 source_code.count('['), # array_count
23 source_code.count('sin') + source_code.count('cos') + source_code.count('exp'), # math_count
24 1 if 'class' in source_code else 0, # has_classes
25 1 if 'template' in source_code else 0, # has_templates
26 1 if 'virtual' in source_code else 0, # has_virtual
27 1 if 'inline' in source_code else 0, # has_inline
28 1 if 'volatile' in source_code else 0, # has_volatile
29 1 if 'restrict' in source_code else 0, # has_restrict
30 source_code.count('{'), # brace_count
31 source_code.count('if') + source_code.count('else'), # branch_count
32 ], dtype=np.float32)
33
34# Example usage
35source_code = """
36#include <stdio.h>
37#include <math.h>
38int main() {
39 double result = 0;
40 for(int i = 1; i < 5000000; i++) {
41 double x = (double)i / 1000.0;
42 result += pow(x, 2.5) * sin(x) / sqrt(x + 1.0);
43 }
44 printf("%f\\n", result);
45 return 0;
46}
47"""
48
49features = extract_features(source_code)
50action, _states = model.predict(features, deterministic=True)
51
52flags = ["-O2", "-O3", "-Ofast"]
53selected_flag = flags[action]
54
55print(f"Recommended flag: {selected_flag}")
56# Output: Recommended flag: -Ofast1import subprocess
2import os
3
4def compile_and_run(source_file: str, flag: str) -> tuple[bool, float]:
5 """Compile and benchmark program with given flag."""
6 binary = "a.out"
7
8 # Compile
9 compile_cmd = ["gcc", source_file, "-o", binary, flag, "-lm"]
10 result = subprocess.run(compile_cmd, capture_output=True)
11
12 if result.returncode != 0:
13 return False, 0.0
14
15 # Run and time
16 import time
17 start = time.perf_counter()
18 result = subprocess.run([f"./{binary}"], capture_output=True)
19 elapsed = time.perf_counter() - start
20
21 # Cleanup
22 os.remove(binary)
23
24 return result.returncode == 0, elapsed
25
26# Get recommendation
27features = extract_features(open("program.c").read())
28action, _ = model.predict(features, deterministic=True)
29recommended_flag = flags[action]
30
31# Validate (production: always validate!)
32success, runtime = compile_and_run("program.c", recommended_flag)
33
34if not success:
35 print(f"WARNING: {recommended_flag} failed, falling back to -O2")
36 success, runtime = compile_and_run("program.c", "-O2")
37
38print(f"Compiled with {recommended_flag}, runtime: {runtime:.3f}s")1Algorithm: Deep Q-Network (DQN)
2Policy: MlpPolicy (256-256-256 fully connected)
3Total timesteps: 10,000,000
4Learning rate: 0.001
5Batch size: 256
6Exploration: ε-greedy (ε_final = 0.05)
7Environments: 16 parallel
8Training time: 19 minutes (Intel Xeon, 16 cores)
9Framework: Stable-Baselines3 2.3.0Train Accuracy: 43% ± 3%
Validation Accuracy: 0% ± 0% (generalization failure!)
Mean Speedup: 0.994x ± 0.012x
Regressions: 0% ± 0%Input: 15 features (code characteristics)
↓
Dense(256) + ReLU
↓
Dense(256) + ReLU
↓
Dense(256) + ReLU
↓
Output: 3 Q-values (one per flag)| Program | Predicted | Optimal | Speedup | Correct? |
|---|---|---|---|---|
| pointer_chase | -O2 | -O2 | 1.000x | ✓ |
| reduction | -O2 | -O2 | 1.000x | ✓ |
| transcendental | -Ofast | -Ofast | 1.155x | ✓ |
| branchy | -O3 | -O3 | 0.951x | ✓ |
| simple_loop | -O2 | -Ofast | 1.000x | ✗ |
| loop_unroll | -O2 | -O3 | 1.000x | ✗ |
| vector_add | -Ofast | -O3 | 0.880x | ✗ |
| stencil | -O3 | -O2 | 0.977x | ✗ |
| matrix_mult | -O3 | -Ofast | 0.982x | ✗ |
| float_math | -O3 | -O2 | 1.000x | ✗ |
| Method | Accuracy | Mean Speedup | Regressions |
|---|---|---|---|
| Our DQN | 40% | 0.994x | 0% |
| Random | 33% | 0.967x | 0% |
| Always-O2 | 30% | 0.890x | 0% |
| Always-O3 | 50% | 1.012x | 0% |
| Always-Ofast | 20% | 0.923x | 6.7% |
1@techreport{callens2026v10c,
2 title={Deep Q-Network for Safe Compiler Flag Optimization},
3 author={Callens, Xavier},
4 institution={Amadeus IT Group},
5 year={2026},
6 type={Technical Report},
7 url={https://huggingface.co/callensxavier/v10c-dqn-compiler-optimization}
8}PAPER_V10C_DQN_REVISED.md (5,000 words, peer-reviewed draft)PEER_REVIEW_V10C.md (comprehensive analysis)dataset.csv (10 programs × 3 flags)programs/ (10 standalone C files)evaluation_results.json