Refactor DSPy-driven optimization flow with coordinator-based orchestration - #48
Conversation
There was a problem hiding this comment.
Pull request overview
This PR refactors the DSPy-driven kernel optimization flow to be template-driven and adds a new coordinator-based orchestration strategy, while consolidating shared agent utilities and updating pipeline wiring/config to support the new strategy.
Changes:
- Introduces
CoordinatorAgent+ coordinator tools to drive dynamic analyze/apply/benchmark/profile loops viadspy.ReActV2. - Moves signature instruction content into reusable Jinja2 templates and injects them via
append_instructions()across analyzer/planner/optimizer agents. - Updates pipeline strategy wiring, shared utilities, and result metadata (token usage), and bumps the project version/dependency.
Reviewed changes
Copilot reviewed 26 out of 26 changed files in this pull request and generated 6 comments.
Show a summary per file
| File | Description |
|---|---|
| src/xe_forge/prompts/templates/sycl_optimization_signature.md.j2 | Adds SYCL/CUTLASS optimization instruction template. |
| src/xe_forge/prompts/templates/sycl_algorithmic_signature.md.j2 | Adds SYCL algorithmic optimization instruction template. |
| src/xe_forge/prompts/templates/planning_signature.md.j2 | Adds planning/stage-ordering instruction template. |
| src/xe_forge/prompts/templates/optimization_signature.md.j2 | Adds templated optimizer-stage guidance (device-conditional). |
| src/xe_forge/prompts/templates/optimization_react_signature.md.j2 | Adds templated ReAct optimizer guidance (device-conditional). |
| src/xe_forge/prompts/templates/coordinator_signature.md.j2 | Adds coordinator orchestration instruction template. |
| src/xe_forge/prompts/templates/autotune_signature.md.j2 | Adds autotune instruction template (device-conditional). |
| src/xe_forge/prompts/templates/analysis_signature.md.j2 | Adds analysis instruction template including injected issue-category blocks. |
| src/xe_forge/prompts/templates/algorithmic_signature.md.j2 | Adds algorithmic optimization instruction template. |
| src/xe_forge/prompts/device_prompts.py | Adds template rendering + device addendum helpers for signatures. |
| src/xe_forge/prompts/init.py | Adds Jinja2 environment and render_signature_instructions() export. |
| src/xe_forge/planner.py | Refactors planner to inject template-based planning instructions. |
| src/xe_forge/pipeline.py | Wires coordinator strategy, template-based instructions, token usage tracking, and helper refactors. |
| src/xe_forge/models.py | Adds token_usage field to OptimizationResult. |
| src/xe_forge/config.py | Expands agent strategy comment to include coordinator. |
| src/xe_forge/cli.py | Updates LiteLLM HTTP clients configuration for DSPy setup. |
| src/xe_forge/agents/utils.py | Introduces shared utilities (extract_gemm_dims, verify_sycl, SUCCESS_MESSAGE). |
| src/xe_forge/agents/react_agent.py | Refactors ReAct agent to use shared utilities, template instructions, and ReActV2-style history handling. |
| src/xe_forge/agents/optimizer_agent.py | Refactors optimizer agent to use shared utilities and injected template instructions. |
| src/xe_forge/agents/cover.py | Adjusts CoVeR fallback signature and extraction predictor behavior. |
| src/xe_forge/agents/coordinator.py | Adds CoordinatorAgent implementation using dspy.ReActV2 and coordinator tools. |
| src/xe_forge/agents/coordinator_tools.py | Adds CoordinatorState and tool closures for analyze/retrieve/apply/benchmark/profile/status. |
| src/xe_forge/agents/analyzer_agent.py | Injects template-based analysis guidance and device/DSL naming helpers. |
| src/xe_forge/agents/init.py | Exports CoordinatorAgent from the agents package. |
| src/xe_forge/init.py | Switches to lazy import for XeForgePipeline. |
| pyproject.toml | Bumps version and updates DSPy dependency/source configuration. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
The performance speedup we achieved was up to 13.3x, as described in the paper, this must be a typo.
| candidates.append(result) | ||
|
|
||
| for attempt in range(best_k): | ||
| # Fixed stage loop runs only when coordinator strategy is not active. |
There was a problem hiding this comment.
should be under if-else: missing else for the block of code that does optimization according to a fixed stages should only be active if there is no coordinator (also fits the comment here)- else includes: l379-638 (up to: candidates.append(result))
| if not analysis.detected_issues: | ||
| result.success, result.optimized_code = True, kernel_code | ||
| candidates.append(result) | ||
| continue |
There was a problem hiding this comment.
this should be replaced by an else, so the pipeline wouldnt continue to run for an empty issues kernel, ends at: result = max(candidates, key=lambda..
| if not stages_to_apply: | ||
| result.success, result.optimized_code = True, kernel_code | ||
| candidates.append(result) | ||
| continue |
There was a problem hiding this comment.
this should be replaced by an else, so the pipeline wouldnt continue to run for an empty stage kernel, ends at: result = max(candidates, key=lambda..
| result.total_speedup = speedup | ||
| candidates.append(result) | ||
|
|
||
| for attempt in range(best_k): |
There was a problem hiding this comment.
should update README that top-k was removed
|
|
||
| for attempt in range(best_k): | ||
| # Fixed stage loop runs only when coordinator strategy is not active. | ||
| best_k = max(1, self.config.optimization.best_k) if self.coordinator is None else 0 |
There was a problem hiding this comment.
these two lines should be deleted- best-k not used for optimization anymore.
| from xe_forge.models import OptimizationResult, OptimizationStage | ||
| from xe_forge.pipeline import XeForgePipeline | ||
|
|
||
| __version__ = "0.2.0" |
There was a problem hiding this comment.
change to 0.3.0 to match pyproject.toml
This PR refactors the DSPy integration to make optimization flow more modular, template-driven, and agentic.