wgpu-shift 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
gpu_shift/run.py ADDED
@@ -0,0 +1,206 @@
1
+ """Two-phase stateful single-command workflow (`gpu-shift run -- <cmd>`)."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import shlex
6
+ import subprocess
7
+ from dataclasses import dataclass
8
+ from pathlib import Path
9
+
10
+ from gpu_shift.bench import bench_unit
11
+ from gpu_shift.capture import CASES_DIRNAME, INDEX_FILENAME, capture_unit
12
+ from gpu_shift.check import check_unit
13
+ from gpu_shift.diff import diff_unit
14
+ from gpu_shift.manifest import is_source_stale, load_manifest
15
+ from gpu_shift.scan import scan_command
16
+ from gpu_shift.stage import create_stage
17
+ from gpu_shift.switch import enable_unit
18
+
19
+ _STUB_MARKER = "Port not implemented yet."
20
+
21
+
22
+ @dataclass(frozen=True)
23
+ class RunResult:
24
+ """Outcome of one `gpu-shift run` invocation."""
25
+
26
+ stage: str
27
+ unit_dir: Path
28
+ source_file: Path
29
+ function_name: str
30
+ task_path: Path
31
+ message: str
32
+ passed: bool
33
+ peak_speedup: float | None = None
34
+ crossover_elements: int | None = None
35
+
36
+
37
+ def _is_stub_port(unit_dir: Path) -> bool:
38
+ port_path = unit_dir / "port.py"
39
+ if not port_path.is_file():
40
+ return True
41
+ return _STUB_MARKER in port_path.read_text(encoding="utf-8")
42
+
43
+
44
+ def run_workflow(
45
+ cmd: list[str],
46
+ *,
47
+ source_file: Path | None = None,
48
+ function_name: str | None = None,
49
+ unit_dir: Path | None = None,
50
+ agent_cmd: str | None = None,
51
+ auto_enable: bool = True,
52
+ project_root: Path | None = None,
53
+ ) -> RunResult:
54
+ """Run scan -> stage -> capture -> (agent/stop) -> diff -> check -> bench -> enable."""
55
+ cleaned = list(cmd)
56
+ if cleaned and cleaned[0] == "--":
57
+ cleaned = cleaned[1:]
58
+ if not cleaned:
59
+ raise ValueError(
60
+ "Provide a workload command after `--` (e.g. `gpu-shift run -- python demo.py`)."
61
+ )
62
+
63
+ root = (project_root or Path.cwd()).resolve()
64
+
65
+ # 1. Select target function via scanner if not explicitly provided
66
+ if source_file is None or function_name is None:
67
+ scanned = scan_command(cleaned, project_root=root)
68
+ stageable = [c for c in scanned.candidates if c.stageable]
69
+ if not stageable:
70
+ raise RuntimeError(
71
+ "Scanner found no stageable top-level GPU candidate. "
72
+ "Pass `--file <path> --fn <name>` explicitly."
73
+ )
74
+ top = stageable[0]
75
+ target_source = top.source_file
76
+ target_fn = top.function_name
77
+ else:
78
+ target_source = source_file.resolve()
79
+ target_fn = function_name
80
+
81
+ resolved_unit = (
82
+ unit_dir.resolve()
83
+ if unit_dir is not None
84
+ else (root / "kernels" / target_fn).resolve()
85
+ )
86
+ task_path = resolved_unit / "TASK.md"
87
+
88
+ # 2. Stage (idempotent: keep existing port if already created)
89
+ if not (resolved_unit / "manifest.json").is_file():
90
+ create_stage(target_source, target_fn, unit_dir=resolved_unit)
91
+
92
+ manifest = load_manifest(resolved_unit)
93
+
94
+ # 3. Capture (skip if fresh captured cases already exist)
95
+ cases_index = resolved_unit / CASES_DIRNAME / INDEX_FILENAME
96
+ if not cases_index.is_file() or is_source_stale(resolved_unit, manifest):
97
+ capture_unit(resolved_unit, from_cmd=cleaned)
98
+
99
+ # 4. If port is still unimplemented, invoke --agent or pause for human/coding agent
100
+ if _is_stub_port(resolved_unit):
101
+ if agent_cmd:
102
+ prompt_text = task_path.read_text(encoding="utf-8")
103
+ proc = subprocess.run(
104
+ shlex.split(agent_cmd),
105
+ input=prompt_text,
106
+ text=True,
107
+ check=False,
108
+ )
109
+ if proc.returncode != 0:
110
+ return RunResult(
111
+ stage="agent_failed",
112
+ unit_dir=resolved_unit,
113
+ source_file=target_source,
114
+ function_name=target_fn,
115
+ task_path=task_path,
116
+ message=f"Agent command exited with code {proc.returncode}.",
117
+ passed=False,
118
+ )
119
+
120
+ if _is_stub_port(resolved_unit):
121
+ return RunResult(
122
+ stage="needs_port",
123
+ unit_dir=resolved_unit,
124
+ source_file=target_source,
125
+ function_name=target_fn,
126
+ task_path=task_path,
127
+ message=(
128
+ f"Staged and captured `{target_fn}`. "
129
+ f"Implement kernel.wgsl + port.py per {task_path.name}, then re-run `gpu-shift run`."
130
+ ),
131
+ passed=True,
132
+ )
133
+
134
+ # 5. Diff
135
+ diff_res = diff_unit(resolved_unit)
136
+ if not diff_res.passed:
137
+ first_fail = next((c for c in diff_res.cases if not c.passed), None)
138
+ reason = diff_res.error or (first_fail.reason if first_fail else "diff failed")
139
+ return RunResult(
140
+ stage="diff_failed",
141
+ unit_dir=resolved_unit,
142
+ source_file=target_source,
143
+ function_name=target_fn,
144
+ task_path=task_path,
145
+ message=f"Diff failed ({diff_res.passed_count}/{diff_res.total_count}): {reason}",
146
+ passed=False,
147
+ )
148
+
149
+ # 6. Check (freshness, WGSL validation, diff, 15-dispatch VRAM soak)
150
+ check_res = check_unit(resolved_unit)
151
+ if not check_res.passed:
152
+ failed_checks = [f"{c.name}: {c.detail}" for c in check_res.checks if not c.passed]
153
+ return RunResult(
154
+ stage="check_failed",
155
+ unit_dir=resolved_unit,
156
+ source_file=target_source,
157
+ function_name=target_fn,
158
+ task_path=task_path,
159
+ message="Check failed: " + "; ".join(failed_checks),
160
+ passed=False,
161
+ )
162
+
163
+ # 7. Bench + crossover check
164
+ bench_res = bench_unit(resolved_unit, update_manifest=True)
165
+ if bench_res.crossover_elements is None:
166
+ return RunResult(
167
+ stage="slower",
168
+ unit_dir=resolved_unit,
169
+ source_file=target_source,
170
+ function_name=target_fn,
171
+ task_path=task_path,
172
+ message=(
173
+ f"Port is numerically correct and VRAM-clean, but {bench_res.verdict}. "
174
+ "Not enabling automatically."
175
+ ),
176
+ passed=True,
177
+ peak_speedup=bench_res.max_speedup,
178
+ crossover_elements=None,
179
+ )
180
+
181
+ # 8. Enable
182
+ if auto_enable:
183
+ en_res = enable_unit(resolved_unit)
184
+ return RunResult(
185
+ stage="enabled",
186
+ unit_dir=resolved_unit,
187
+ source_file=target_source,
188
+ function_name=target_fn,
189
+ task_path=task_path,
190
+ message=f"{en_res.message} {bench_res.verdict}.",
191
+ passed=True,
192
+ peak_speedup=bench_res.max_speedup,
193
+ crossover_elements=bench_res.crossover_elements,
194
+ )
195
+
196
+ return RunResult(
197
+ stage="ready",
198
+ unit_dir=resolved_unit,
199
+ source_file=target_source,
200
+ function_name=target_fn,
201
+ task_path=task_path,
202
+ message=f"Check and bench passed ({bench_res.verdict}). Run `gpu-shift enable` to activate.",
203
+ passed=True,
204
+ peak_speedup=bench_res.max_speedup,
205
+ crossover_elements=bench_res.crossover_elements,
206
+ )