HideIron commited on
Commit
707394c
·
verified ·
1 Parent(s): 9f135d5

Upload folder using huggingface_hub

Browse files
Files changed (3) hide show
  1. README.md +30 -30
  2. inference.py +5 -5
  3. server/compiler_env_environment.py +39 -15
README.md CHANGED
@@ -13,7 +13,7 @@ tags:
13
 
14
  # IRis Compiler Optimization Environment
15
 
16
- An OpenEnv environment where an **LLM agent selects LLVM optimization passes** to minimize execution time of C programs compiled for **RISC-V architecture**. The agent builds a sequence of compiler passes, then compiles and measures performance against standard optimization level baselines (O0–O3).
17
 
18
  ## Quick Start
19
 
@@ -21,7 +21,7 @@ An OpenEnv environment where an **LLM agent selects LLVM optimization passes** t
21
  from compiler_env import CompilerAction, CompilerEnv
22
 
23
  with CompilerEnv.from_docker_image("compiler_env-env:latest") as env:
24
- # Start episode — picks a C program and computes baselines
25
  result = env.reset()
26
  print(f"Program: {result.observation.data['program']}")
27
  print(f"O3 baseline: {result.observation.data['baselines']['O3']}s")
@@ -40,9 +40,9 @@ with CompilerEnv.from_docker_image("compiler_env-env:latest") as env:
40
  ## How It Works
41
 
42
  1. **reset()** picks a C program (random from 200 training programs, or a fixed eval task)
43
- 2. The environment compiles it with `-O0`, `-O1`, `-O2`, `-O3` and measures each on QEMU → **baselines**
44
  3. The agent calls tools to build an optimization pass sequence
45
- 4. **compile_and_measure** runs the full pipeline: `clang → opt (agent's passes) → llc → gcc → qemu`
46
  5. Reward is based on how the agent's execution time compares to baselines
47
 
48
  ## Action Space
@@ -58,11 +58,11 @@ with CompilerEnv.from_docker_image("compiler_env-env:latest") as env:
58
  ## Observation
59
 
60
  **CompilerObservation** fields:
61
- - **data** (dict) — Action-specific data (baselines, sequences, execution times, etc.)
62
- - **status** (str) — `"success"`, `"error"`, `"invalid_pass"`, `"failed"`
63
- - **message** (str) — Human-readable description
64
- - **reward** (float) — Cumulative episode reward
65
- - **done** (bool) — Whether the episode has ended
66
 
67
  ## Reward Structure (Partial Progress)
68
 
@@ -88,9 +88,9 @@ with CompilerEnv.from_docker_image("compiler_env-env:latest") as env:
88
  ## System Requirements
89
 
90
  The Docker image includes all needed tools:
91
- - **clang** + **llvm** (opt, llc) — LLVM compilation toolchain
92
- - **gcc-riscv64-linux-gnu** — RISC-V cross-compiler
93
- - **qemu-user** (qemu-riscv64) — RISC-V binary emulation
94
 
95
  ## Building & Deploying
96
 
@@ -113,22 +113,22 @@ uvicorn server.app:app --reload --host 0.0.0.0 --port 8000
113
 
114
  ```
115
  compiler_env/
116
- ├── __init__.py # Module exports
117
- ├── README.md # This file
118
- ├── openenv.yaml # OpenEnv manifest
119
- ├── pyproject.toml # Dependencies
120
- ├── client.py # CompilerEnv WebSocket client
121
- ├── models.py # CompilerAction & CompilerObservation
122
- ├── baseline_inference.py # Baseline script (OpenAI API)
123
- ├── training_programs/ # 200 C programs
124
- │ ├── 01_insertion_sort.c
125
- │ ├── 08_strassen_matrix.c
126
- │ ├── 114_polynomial_multiply_fft.c
127
- │ └── ... (197 more)
128
- └── server/
129
- ├── __init__.py
130
- ├── compiler_env_environment.py # Core environment logic
131
- ├── app.py # FastAPI app (HTTP + WebSocket)
132
- ├── requirements.txt
133
- └── Dockerfile
134
  ```
 
13
 
14
  # IRis Compiler Optimization Environment
15
 
16
+ An OpenEnv environment where an **LLM agent selects LLVM optimization passes** to minimize execution time of C programs compiled for **RISC-V architecture**. The agent builds a sequence of compiler passes, then compiles and measures performance against standard optimization level baselines (O0-O3).
17
 
18
  ## Quick Start
19
 
 
21
  from compiler_env import CompilerAction, CompilerEnv
22
 
23
  with CompilerEnv.from_docker_image("compiler_env-env:latest") as env:
24
+ # Start episode - picks a C program and computes baselines
25
  result = env.reset()
26
  print(f"Program: {result.observation.data['program']}")
27
  print(f"O3 baseline: {result.observation.data['baselines']['O3']}s")
 
40
  ## How It Works
41
 
42
  1. **reset()** picks a C program (random from 200 training programs, or a fixed eval task)
43
+ 2. The environment compiles it with `-O0`, `-O1`, `-O2`, `-O3` and measures each on QEMU -> **baselines**
44
  3. The agent calls tools to build an optimization pass sequence
45
+ 4. **compile_and_measure** runs the full pipeline: `clang -> opt (agent's passes) -> llc -> gcc -> qemu`
46
  5. Reward is based on how the agent's execution time compares to baselines
47
 
48
  ## Action Space
 
58
  ## Observation
59
 
60
  **CompilerObservation** fields:
61
+ - **data** (dict) - Action-specific data (baselines, sequences, execution times, etc.)
62
+ - **status** (str) - `"success"`, `"error"`, `"invalid_pass"`, `"failed"`
63
+ - **message** (str) - Human-readable description
64
+ - **reward** (float) - Cumulative episode reward
65
+ - **done** (bool) - Whether the episode has ended
66
 
67
  ## Reward Structure (Partial Progress)
68
 
 
88
  ## System Requirements
89
 
90
  The Docker image includes all needed tools:
91
+ - **clang** + **llvm** (opt, llc) - LLVM compilation toolchain
92
+ - **gcc-riscv64-linux-gnu** - RISC-V cross-compiler
93
+ - **qemu-user** (qemu-riscv64) - RISC-V binary emulation
94
 
95
  ## Building & Deploying
96
 
 
113
 
114
  ```
115
  compiler_env/
116
+ |-- __init__.py # Module exports
117
+ |-- README.md # This file
118
+ |-- openenv.yaml # OpenEnv manifest
119
+ |-- pyproject.toml # Dependencies
120
+ |-- client.py # CompilerEnv WebSocket client
121
+ |-- models.py # CompilerAction & CompilerObservation
122
+ |-- inference.py # Baseline script (OpenAI API)
123
+ |-- training_programs/ # 200 C programs
124
+ | |-- 01_insertion_sort.c
125
+ | |-- 08_strassen_matrix.c
126
+ | |-- 114_polynomial_multiply_fft.c
127
+ | +-- ... (197 more)
128
+ +-- server/
129
+ |-- __init__.py
130
+ |-- compiler_env_environment.py # Core environment logic
131
+ |-- app.py # FastAPI app (HTTP + WebSocket)
132
+ |-- requirements.txt
133
+ +-- Dockerfile
134
  ```
inference.py CHANGED
@@ -45,8 +45,8 @@ from openai import OpenAI
45
 
46
  from compiler_env import CompilerAction, CompilerEnv
47
 
48
- IMAGE_NAME = os.getenv("IMAGE_NAME")
49
- API_KEY = os.getenv("HF_TOKEN") or os.getenv("API_KEY")
50
 
51
  API_BASE_URL = os.getenv("API_BASE_URL") or "https://router.huggingface.co/v1"
52
  MODEL_NAME = os.getenv("MODEL_NAME") or "Qwen/Qwen2.5-72B-Instruct"
@@ -189,9 +189,9 @@ def get_model_tool_call(client: OpenAI, messages: list) -> tuple:
189
 
190
 
191
  async def main() -> None:
192
- client = OpenAI(base_url=API_BASE_URL, api_key=API_KEY)
193
 
194
- env = await CompilerEnv.from_docker_image(IMAGE_NAME)
195
 
196
  history_messages: list = []
197
  rewards: List[float] = []
@@ -233,7 +233,7 @@ async def main() -> None:
233
  fn_name, fn_args, raw_tc = get_model_tool_call(client, history_messages)
234
 
235
  if fn_name is None:
236
- # No tool call — agent stopped; force compile_and_measure
237
  action_str = "compile_and_measure"
238
  else:
239
  action_str = tool_call_to_action(fn_name, fn_args)
 
45
 
46
  from compiler_env import CompilerAction, CompilerEnv
47
 
48
+ LOCAL_IMAGE_NAME = os.getenv("LOCAL_IMAGE_NAME")
49
+ HF_TOKEN = os.getenv("HF_TOKEN")
50
 
51
  API_BASE_URL = os.getenv("API_BASE_URL") or "https://router.huggingface.co/v1"
52
  MODEL_NAME = os.getenv("MODEL_NAME") or "Qwen/Qwen2.5-72B-Instruct"
 
189
 
190
 
191
  async def main() -> None:
192
+ client = OpenAI(base_url=API_BASE_URL, api_key=HF_TOKEN)
193
 
194
+ env = await CompilerEnv.from_docker_image(LOCAL_IMAGE_NAME)
195
 
196
  history_messages: list = []
197
  rewards: List[float] = []
 
233
  fn_name, fn_args, raw_tc = get_model_tool_call(client, history_messages)
234
 
235
  if fn_name is None:
236
+ # No tool call -- agent stopped; force compile_and_measure
237
  action_str = "compile_and_measure"
238
  else:
239
  action_str = tool_call_to_action(fn_name, fn_args)
server/compiler_env_environment.py CHANGED
@@ -144,19 +144,37 @@ class CompilerEnvironment(Environment):
144
  raise ValueError(
145
  f"Task ID '{task_id}' not found. Valid: {list(self.eval_programs.keys())}"
146
  )
 
147
  else:
148
- self.current_program = random.choice(self.train_programs)
149
-
150
- # Reset episode state
151
- self.current_sequence = []
152
- self.episode_reward = 0.0
153
- self.done = False
154
-
155
- # Compute baselines
156
- self.baseline_O0 = self._compute_baseline(self.current_program, "O0")
157
- self.baseline_O1 = self._compute_baseline(self.current_program, "O1")
158
- self.baseline_O2 = self._compute_baseline(self.current_program, "O2")
159
- self.baseline_O3 = self._compute_baseline(self.current_program, "O3")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
160
 
161
  return CompilerObservation(
162
  data={
@@ -310,9 +328,9 @@ class CompilerEnvironment(Environment):
310
  f"Sequence has {len(self.current_sequence)} passes.",
311
  )
312
 
313
- # ──────────────────────────────────────────────
314
  # Compilation & Measurement
315
- # ──────────────────────────────────────────────
316
 
317
  def _compute_baseline(self, program_path, flag: str) -> float:
318
  """
@@ -326,7 +344,13 @@ class CompilerEnvironment(Environment):
326
  "--gcc-toolchain=/usr",
327
  f"-{flag}", program_path, "-o", exe_file, "-static",
328
  ]
329
- subprocess.run(clang_cmd, check=True, capture_output=True, timeout=30)
 
 
 
 
 
 
330
 
331
  start = time.perf_counter()
332
  subprocess.run(
 
144
  raise ValueError(
145
  f"Task ID '{task_id}' not found. Valid: {list(self.eval_programs.keys())}"
146
  )
147
+ programs_to_try = [self.current_program]
148
  else:
149
+ programs_to_try = [random.choice(self.train_programs)]
150
+ # Build a retry list of up to 5 different programs
151
+ tried = {programs_to_try[0]}
152
+ while len(programs_to_try) < 5:
153
+ candidate = random.choice(self.train_programs)
154
+ if candidate not in tried:
155
+ programs_to_try.append(candidate)
156
+ tried.add(candidate)
157
+
158
+ # Try programs until one compiles successfully
159
+ last_error = None
160
+ for program in programs_to_try:
161
+ self.current_program = program
162
+ self.current_sequence = []
163
+ self.episode_reward = 0.0
164
+ self.done = False
165
+ try:
166
+ self.baseline_O0 = self._compute_baseline(self.current_program, "O0")
167
+ self.baseline_O1 = self._compute_baseline(self.current_program, "O1")
168
+ self.baseline_O2 = self._compute_baseline(self.current_program, "O2")
169
+ self.baseline_O3 = self._compute_baseline(self.current_program, "O3")
170
+ last_error = None
171
+ break
172
+ except (RuntimeError, subprocess.CalledProcessError, subprocess.TimeoutExpired) as e:
173
+ last_error = e
174
+ continue
175
+
176
+ if last_error is not None:
177
+ raise RuntimeError(f"All programs failed to compile: {last_error}")
178
 
179
  return CompilerObservation(
180
  data={
 
328
  f"Sequence has {len(self.current_sequence)} passes.",
329
  )
330
 
331
+ # ----------------------------------------------
332
  # Compilation & Measurement
333
+ # ----------------------------------------------
334
 
335
  def _compute_baseline(self, program_path, flag: str) -> float:
336
  """
 
344
  "--gcc-toolchain=/usr",
345
  f"-{flag}", program_path, "-o", exe_file, "-static",
346
  ]
347
+ result = subprocess.run(clang_cmd, capture_output=True, timeout=30)
348
+ if result.returncode != 0:
349
+ stderr = result.stderr.decode(errors="replace")
350
+ raise RuntimeError(
351
+ f"Baseline compilation failed for {os.path.basename(program_path)} "
352
+ f"with -{flag}: {stderr}"
353
+ )
354
 
355
  start = time.perf_counter()
356
  subprocess.run(