Find optimized kernels from the workspace directory. Checks orchestration_state.json first, then scans for *_optimized.py files.
()
| 320 | |
| 321 | |
| 322 | def discover_optimized_kernels() -> List[KernelReplacement]: |
| 323 | """ |
| 324 | Find optimized kernels from the workspace directory. |
| 325 | Checks orchestration_state.json first, then scans for *_optimized.py files. |
| 326 | """ |
| 327 | replacements: List[KernelReplacement] = [] |
| 328 | |
| 329 | # Strategy 1: Read orchestration state |
| 330 | state = load_orchestration_state() |
| 331 | if state and "kernels" in state: |
| 332 | for k in state["kernels"]: |
| 333 | ktype = k.get("op_type", k.get("type", "unknown")) |
| 334 | rank = k.get("rank", 0) |
| 335 | speedup = k.get("speedup", k.get("best_speedup", 1.0)) |
| 336 | # optimized_path is not written by orchestrate.py, so derive it |
| 337 | # from the kernel file path if available |
| 338 | opt_path = k.get("optimized_path", "") |
| 339 | |
| 340 | if not opt_path: |
| 341 | # Try to derive from the "file" key that orchestrate.py writes |
| 342 | base_file = k.get("file", "") |
| 343 | if base_file: |
| 344 | stem = Path(base_file).stem |
| 345 | opt_path = os.path.join( |
| 346 | WORKSPACE_DIR, f"{stem}_optimized.py" |
| 347 | ) |
| 348 | else: |
| 349 | # Fallback convention: workspace/kernel_{type}_{rank}_optimized.py |
| 350 | opt_path = os.path.join( |
| 351 | WORKSPACE_DIR, f"kernel_{ktype}_{rank}_optimized.py" |
| 352 | ) |
| 353 | |
| 354 | if os.path.exists(opt_path) and speedup > 1.0: |
| 355 | replacements.append(KernelReplacement( |
| 356 | kernel_type=ktype, |
| 357 | rank=rank, |
| 358 | speedup=speedup, |
| 359 | optimized_path=opt_path, |
| 360 | )) |
| 361 | return replacements |
| 362 | |
| 363 | # Strategy 2: Scan workspace directory for optimized kernel files |
| 364 | if not os.path.isdir(WORKSPACE_DIR): |
| 365 | return replacements |
| 366 | |
| 367 | for fname in sorted(os.listdir(WORKSPACE_DIR)): |
| 368 | if fname.endswith("_optimized.py"): |
| 369 | # Parse filename: kernel_{type}_{rank}_optimized.py |
| 370 | # Type can be multi-word (e.g. flash_attention), so the rank |
| 371 | # is always the last numeric segment before "_optimized.py". |
| 372 | stem = fname.replace("_optimized.py", "") # e.g. "kernel_flash_attention_1" |
| 373 | parts = stem.split("_") |
| 374 | if len(parts) >= 3 and parts[0] == "kernel": |
| 375 | # Find the rank: last part that is purely numeric |
| 376 | rank = 0 |
| 377 | rank_idx = len(parts) |
| 378 | for i in range(len(parts) - 1, 0, -1): |
| 379 | if parts[i].isdigit(): |
no test coverage detected