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/__init__.py +19 -0
- gpu_shift/__main__.py +8 -0
- gpu_shift/_capturer.py +191 -0
- gpu_shift/_scanner.py +71 -0
- gpu_shift/bench.py +321 -0
- gpu_shift/capture.py +669 -0
- gpu_shift/check.py +224 -0
- gpu_shift/cli.py +609 -0
- gpu_shift/demo.py +272 -0
- gpu_shift/device.py +170 -0
- gpu_shift/diff.py +510 -0
- gpu_shift/manifest.py +171 -0
- gpu_shift/mcp_server.py +352 -0
- gpu_shift/report.py +218 -0
- gpu_shift/run.py +206 -0
- gpu_shift/runtime.py +592 -0
- gpu_shift/scan.py +380 -0
- gpu_shift/setup.py +245 -0
- gpu_shift/skill_data/SKILL.md +56 -0
- gpu_shift/stage.py +248 -0
- gpu_shift/switch.py +268 -0
- wgpu_shift-0.1.0.dist-info/METADATA +125 -0
- wgpu_shift-0.1.0.dist-info/RECORD +26 -0
- wgpu_shift-0.1.0.dist-info/WHEEL +4 -0
- wgpu_shift-0.1.0.dist-info/entry_points.txt +3 -0
- wgpu_shift-0.1.0.dist-info/licenses/LICENSE +21 -0
gpu_shift/mcp_server.py
ADDED
|
@@ -0,0 +1,352 @@
|
|
|
1
|
+
"""Zero-dependency stdio JSON-RPC 2.0 MCP server for gpu-shift."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import dataclasses
|
|
6
|
+
import json
|
|
7
|
+
import math
|
|
8
|
+
import sys
|
|
9
|
+
from pathlib import Path
|
|
10
|
+
from typing import Any
|
|
11
|
+
|
|
12
|
+
from gpu_shift import __version__
|
|
13
|
+
from gpu_shift.bench import bench_unit
|
|
14
|
+
from gpu_shift.capture import capture_unit
|
|
15
|
+
from gpu_shift.check import check_unit
|
|
16
|
+
from gpu_shift.demo import run_demo
|
|
17
|
+
from gpu_shift.device import list_adapters, run_smoke_test
|
|
18
|
+
from gpu_shift.diff import diff_unit
|
|
19
|
+
from gpu_shift.report import generate_report
|
|
20
|
+
from gpu_shift.scan import scan_command
|
|
21
|
+
from gpu_shift.stage import create_stage
|
|
22
|
+
from gpu_shift.switch import disable_unit, enable_unit
|
|
23
|
+
|
|
24
|
+
MCP_PROTOCOL_VERSION = "2024-11-05"
|
|
25
|
+
|
|
26
|
+
TOOLS: list[dict[str, Any]] = [
|
|
27
|
+
{
|
|
28
|
+
"name": "gpu_doctor",
|
|
29
|
+
"description": "List available WebGPU adapters and run a WGSL compute smoke test.",
|
|
30
|
+
"inputSchema": {
|
|
31
|
+
"type": "object",
|
|
32
|
+
"properties": {
|
|
33
|
+
"power_preference": {
|
|
34
|
+
"type": "string",
|
|
35
|
+
"enum": ["high-performance", "low-power"],
|
|
36
|
+
"default": "high-performance",
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
},
|
|
40
|
+
},
|
|
41
|
+
{
|
|
42
|
+
"name": "gpu_demo",
|
|
43
|
+
"description": "Run the built-in 2D bilateral filter live GPU demo (diff, VRAM soak, crossover).",
|
|
44
|
+
"inputSchema": {
|
|
45
|
+
"type": "object",
|
|
46
|
+
"properties": {
|
|
47
|
+
"power_preference": {
|
|
48
|
+
"type": "string",
|
|
49
|
+
"enum": ["high-performance", "low-power"],
|
|
50
|
+
"default": "high-performance",
|
|
51
|
+
}
|
|
52
|
+
},
|
|
53
|
+
},
|
|
54
|
+
},
|
|
55
|
+
{
|
|
56
|
+
"name": "gpu_scan",
|
|
57
|
+
"description": "Scan a Python command (cProfile + AST) to rank GPU offload candidates.",
|
|
58
|
+
"inputSchema": {
|
|
59
|
+
"type": "object",
|
|
60
|
+
"properties": {
|
|
61
|
+
"cmd": {
|
|
62
|
+
"type": "array",
|
|
63
|
+
"items": {"type": "string"},
|
|
64
|
+
"description": "Python command argv, e.g. ['python', 'examples/denoise/demo.py'].",
|
|
65
|
+
},
|
|
66
|
+
"project_root": {"type": "string"},
|
|
67
|
+
"top_k": {"type": "integer", "default": 8},
|
|
68
|
+
},
|
|
69
|
+
"required": ["cmd"],
|
|
70
|
+
},
|
|
71
|
+
},
|
|
72
|
+
{
|
|
73
|
+
"name": "gpu_stage",
|
|
74
|
+
"description": "Create an isolated kernels/<fn>/ unit (manifest.json, kernel.wgsl, port.py, TASK.md) without editing the source file.",
|
|
75
|
+
"inputSchema": {
|
|
76
|
+
"type": "object",
|
|
77
|
+
"properties": {
|
|
78
|
+
"source_file": {"type": "string"},
|
|
79
|
+
"fn": {"type": "string"},
|
|
80
|
+
"out": {"type": "string"},
|
|
81
|
+
"rtol": {"type": "number", "default": 1e-4},
|
|
82
|
+
"atol": {"type": "number", "default": 1e-5},
|
|
83
|
+
"overwrite": {"type": "boolean", "default": False},
|
|
84
|
+
},
|
|
85
|
+
"required": ["source_file", "fn"],
|
|
86
|
+
},
|
|
87
|
+
},
|
|
88
|
+
{
|
|
89
|
+
"name": "gpu_capture",
|
|
90
|
+
"description": "Capture real CPU inputs/outputs (.npz) from a command plus synthetic GPU edge-case traps.",
|
|
91
|
+
"inputSchema": {
|
|
92
|
+
"type": "object",
|
|
93
|
+
"properties": {
|
|
94
|
+
"unit": {"type": "string"},
|
|
95
|
+
"cmd": {
|
|
96
|
+
"type": "array",
|
|
97
|
+
"items": {"type": "string"},
|
|
98
|
+
},
|
|
99
|
+
"max_calls": {"type": "integer", "default": 16},
|
|
100
|
+
"include_edge_cases": {"type": "boolean", "default": True},
|
|
101
|
+
},
|
|
102
|
+
"required": ["unit", "cmd"],
|
|
103
|
+
},
|
|
104
|
+
},
|
|
105
|
+
{
|
|
106
|
+
"name": "gpu_diff",
|
|
107
|
+
"description": "Replay captured cases on the GPU port and verify numerical accuracy against CPU ground truth.",
|
|
108
|
+
"inputSchema": {
|
|
109
|
+
"type": "object",
|
|
110
|
+
"properties": {
|
|
111
|
+
"unit": {"type": "string"},
|
|
112
|
+
"rtol": {"type": "number"},
|
|
113
|
+
"atol": {"type": "number"},
|
|
114
|
+
"allow_stale": {"type": "boolean", "default": False},
|
|
115
|
+
},
|
|
116
|
+
"required": ["unit"],
|
|
117
|
+
},
|
|
118
|
+
},
|
|
119
|
+
{
|
|
120
|
+
"name": "gpu_bench",
|
|
121
|
+
"description": "Benchmark end-to-end CPU vs GPU latency (including transfers) and compute min_elements crossover.",
|
|
122
|
+
"inputSchema": {
|
|
123
|
+
"type": "object",
|
|
124
|
+
"properties": {
|
|
125
|
+
"unit": {"type": "string"},
|
|
126
|
+
"warmup": {"type": "integer", "default": 2},
|
|
127
|
+
"repeats": {"type": "integer", "default": 5},
|
|
128
|
+
"sweep": {"type": "boolean", "default": True},
|
|
129
|
+
"update_manifest": {"type": "boolean", "default": True},
|
|
130
|
+
},
|
|
131
|
+
"required": ["unit"],
|
|
132
|
+
},
|
|
133
|
+
},
|
|
134
|
+
{
|
|
135
|
+
"name": "gpu_check",
|
|
136
|
+
"description": "Run release verification check (freshness, WGSL compile, diff, 15-dispatch native VRAM soak).",
|
|
137
|
+
"inputSchema": {
|
|
138
|
+
"type": "object",
|
|
139
|
+
"properties": {
|
|
140
|
+
"unit": {"type": "string"},
|
|
141
|
+
"require_speedup": {"type": "boolean", "default": False},
|
|
142
|
+
"min_speedup": {"type": "number", "default": 1.1},
|
|
143
|
+
},
|
|
144
|
+
"required": ["unit"],
|
|
145
|
+
},
|
|
146
|
+
},
|
|
147
|
+
{
|
|
148
|
+
"name": "gpu_enable",
|
|
149
|
+
"description": "Enable a verified GPU port in the original Python file with automatic crossover and CPU fallback.",
|
|
150
|
+
"inputSchema": {
|
|
151
|
+
"type": "object",
|
|
152
|
+
"properties": {
|
|
153
|
+
"unit": {"type": "string"},
|
|
154
|
+
"force": {"type": "boolean", "default": False},
|
|
155
|
+
},
|
|
156
|
+
"required": ["unit"],
|
|
157
|
+
},
|
|
158
|
+
},
|
|
159
|
+
{
|
|
160
|
+
"name": "gpu_disable",
|
|
161
|
+
"description": "Disable the GPU port and restore the original Python file byte-for-byte.",
|
|
162
|
+
"inputSchema": {
|
|
163
|
+
"type": "object",
|
|
164
|
+
"properties": {"unit": {"type": "string"}},
|
|
165
|
+
"required": ["unit"],
|
|
166
|
+
},
|
|
167
|
+
},
|
|
168
|
+
{
|
|
169
|
+
"name": "gpu_report",
|
|
170
|
+
"description": "Generate PR-ready report/REPORT.md and report/card.svg for a unit.",
|
|
171
|
+
"inputSchema": {
|
|
172
|
+
"type": "object",
|
|
173
|
+
"properties": {"unit": {"type": "string"}},
|
|
174
|
+
"required": ["unit"],
|
|
175
|
+
},
|
|
176
|
+
},
|
|
177
|
+
]
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
def _jsonable(obj: Any) -> Any:
|
|
181
|
+
if isinstance(obj, Path):
|
|
182
|
+
return obj.as_posix()
|
|
183
|
+
if dataclasses.is_dataclass(obj) and not isinstance(obj, type):
|
|
184
|
+
return {k: _jsonable(v) for k, v in dataclasses.asdict(obj).items()}
|
|
185
|
+
if isinstance(obj, dict):
|
|
186
|
+
return {str(k): _jsonable(v) for k, v in obj.items()}
|
|
187
|
+
if isinstance(obj, (list, tuple)):
|
|
188
|
+
return [_jsonable(v) for v in obj]
|
|
189
|
+
if isinstance(obj, float):
|
|
190
|
+
if math.isnan(obj):
|
|
191
|
+
return "nan"
|
|
192
|
+
if math.isinf(obj):
|
|
193
|
+
return "inf" if obj > 0 else "-inf"
|
|
194
|
+
return obj
|
|
195
|
+
|
|
196
|
+
|
|
197
|
+
def _dispatch_tool(name: str, arguments: dict[str, Any]) -> Any:
|
|
198
|
+
if name == "gpu_doctor":
|
|
199
|
+
pref = str(arguments.get("power_preference", "high-performance"))
|
|
200
|
+
return {
|
|
201
|
+
"version": __version__,
|
|
202
|
+
"adapters": list_adapters(),
|
|
203
|
+
"smoke": run_smoke_test(power_preference=pref),
|
|
204
|
+
}
|
|
205
|
+
if name == "gpu_demo":
|
|
206
|
+
pref = str(arguments.get("power_preference", "high-performance"))
|
|
207
|
+
return run_demo(power_preference=pref)
|
|
208
|
+
if name == "gpu_scan":
|
|
209
|
+
return scan_command(
|
|
210
|
+
cmd=list(arguments["cmd"]),
|
|
211
|
+
project_root=Path(arguments["project_root"]) if arguments.get("project_root") else None,
|
|
212
|
+
top_k=int(arguments.get("top_k", 8)),
|
|
213
|
+
)
|
|
214
|
+
if name == "gpu_stage":
|
|
215
|
+
return create_stage(
|
|
216
|
+
source_file=Path(arguments["source_file"]),
|
|
217
|
+
function_name=str(arguments["fn"]),
|
|
218
|
+
unit_dir=Path(arguments["out"]) if arguments.get("out") else None,
|
|
219
|
+
rtol=float(arguments.get("rtol", 1e-4)),
|
|
220
|
+
atol=float(arguments.get("atol", 1e-5)),
|
|
221
|
+
overwrite=bool(arguments.get("overwrite", False)),
|
|
222
|
+
)
|
|
223
|
+
if name == "gpu_capture":
|
|
224
|
+
return capture_unit(
|
|
225
|
+
unit_dir=Path(arguments["unit"]),
|
|
226
|
+
from_cmd=list(arguments["cmd"]),
|
|
227
|
+
max_calls=int(arguments.get("max_calls", 16)),
|
|
228
|
+
include_edge_cases=bool(arguments.get("include_edge_cases", True)),
|
|
229
|
+
)
|
|
230
|
+
if name == "gpu_diff":
|
|
231
|
+
return diff_unit(
|
|
232
|
+
unit_dir=Path(arguments["unit"]),
|
|
233
|
+
rtol=float(arguments["rtol"]) if arguments.get("rtol") is not None else None,
|
|
234
|
+
atol=float(arguments["atol"]) if arguments.get("atol") is not None else None,
|
|
235
|
+
allow_stale=bool(arguments.get("allow_stale", False)),
|
|
236
|
+
)
|
|
237
|
+
if name == "gpu_bench":
|
|
238
|
+
return bench_unit(
|
|
239
|
+
unit_dir=Path(arguments["unit"]),
|
|
240
|
+
warmup=int(arguments.get("warmup", 2)),
|
|
241
|
+
repeats=int(arguments.get("repeats", 5)),
|
|
242
|
+
sweep=bool(arguments.get("sweep", True)),
|
|
243
|
+
update_manifest=bool(arguments.get("update_manifest", True)),
|
|
244
|
+
)
|
|
245
|
+
if name == "gpu_check":
|
|
246
|
+
return check_unit(
|
|
247
|
+
unit_dir=Path(arguments["unit"]),
|
|
248
|
+
require_speedup=bool(arguments.get("require_speedup", False)),
|
|
249
|
+
min_speedup=float(arguments.get("min_speedup", 1.1)),
|
|
250
|
+
)
|
|
251
|
+
if name == "gpu_enable":
|
|
252
|
+
return enable_unit(
|
|
253
|
+
unit_dir=Path(arguments["unit"]),
|
|
254
|
+
force=bool(arguments.get("force", False)),
|
|
255
|
+
)
|
|
256
|
+
if name == "gpu_disable":
|
|
257
|
+
return disable_unit(unit_dir=Path(arguments["unit"]))
|
|
258
|
+
if name == "gpu_report":
|
|
259
|
+
return generate_report(unit_dir=Path(arguments["unit"]))
|
|
260
|
+
raise KeyError(f"Unknown MCP tool: {name}")
|
|
261
|
+
|
|
262
|
+
|
|
263
|
+
def handle_jsonrpc_message(msg: dict[str, Any]) -> dict[str, Any] | None:
|
|
264
|
+
"""Process one MCP JSON-RPC 2.0 request and return a response dict (or None for notifications)."""
|
|
265
|
+
method = msg.get("method")
|
|
266
|
+
msg_id = msg.get("id")
|
|
267
|
+
params = msg.get("params") or {}
|
|
268
|
+
|
|
269
|
+
# Notifications have no id and expect no response
|
|
270
|
+
if msg_id is None and method in {"notifications/initialized", "notifications/cancelled"}:
|
|
271
|
+
return None
|
|
272
|
+
|
|
273
|
+
if method == "initialize":
|
|
274
|
+
return {
|
|
275
|
+
"jsonrpc": "2.0",
|
|
276
|
+
"id": msg_id,
|
|
277
|
+
"result": {
|
|
278
|
+
"protocolVersion": MCP_PROTOCOL_VERSION,
|
|
279
|
+
"capabilities": {"tools": {}},
|
|
280
|
+
"serverInfo": {"name": "gpu-shift", "version": __version__},
|
|
281
|
+
},
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
if method == "ping":
|
|
285
|
+
return {"jsonrpc": "2.0", "id": msg_id, "result": {}}
|
|
286
|
+
|
|
287
|
+
if method == "tools/list":
|
|
288
|
+
return {"jsonrpc": "2.0", "id": msg_id, "result": {"tools": TOOLS}}
|
|
289
|
+
|
|
290
|
+
if method == "tools/call":
|
|
291
|
+
tool_name = str(params.get("name", ""))
|
|
292
|
+
arguments = dict(params.get("arguments") or {})
|
|
293
|
+
try:
|
|
294
|
+
payload = _jsonable(_dispatch_tool(tool_name, arguments))
|
|
295
|
+
return {
|
|
296
|
+
"jsonrpc": "2.0",
|
|
297
|
+
"id": msg_id,
|
|
298
|
+
"result": {
|
|
299
|
+
"content": [
|
|
300
|
+
{"type": "text", "text": json.dumps(payload, indent=2)}
|
|
301
|
+
],
|
|
302
|
+
"isError": False,
|
|
303
|
+
},
|
|
304
|
+
}
|
|
305
|
+
except Exception as exc: # noqa: BLE001
|
|
306
|
+
return {
|
|
307
|
+
"jsonrpc": "2.0",
|
|
308
|
+
"id": msg_id,
|
|
309
|
+
"result": {
|
|
310
|
+
"content": [
|
|
311
|
+
{
|
|
312
|
+
"type": "text",
|
|
313
|
+
"text": json.dumps(
|
|
314
|
+
{"error": str(exc), "type": type(exc).__name__},
|
|
315
|
+
indent=2,
|
|
316
|
+
),
|
|
317
|
+
}
|
|
318
|
+
],
|
|
319
|
+
"isError": True,
|
|
320
|
+
},
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
return {
|
|
324
|
+
"jsonrpc": "2.0",
|
|
325
|
+
"id": msg_id,
|
|
326
|
+
"error": {"code": -32601, "message": f"Method not found: {method}"},
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
|
|
330
|
+
def serve_stdio() -> int:
|
|
331
|
+
"""Run the MCP JSON-RPC server over stdin/stdout until EOF."""
|
|
332
|
+
for raw_line in sys.stdin:
|
|
333
|
+
line = raw_line.strip()
|
|
334
|
+
if not line:
|
|
335
|
+
continue
|
|
336
|
+
try:
|
|
337
|
+
msg = json.loads(line)
|
|
338
|
+
except json.JSONDecodeError as exc:
|
|
339
|
+
err = {
|
|
340
|
+
"jsonrpc": "2.0",
|
|
341
|
+
"id": None,
|
|
342
|
+
"error": {"code": -32700, "message": f"Parse error: {exc}"},
|
|
343
|
+
}
|
|
344
|
+
sys.stdout.write(json.dumps(err) + "\n")
|
|
345
|
+
sys.stdout.flush()
|
|
346
|
+
continue
|
|
347
|
+
|
|
348
|
+
resp = handle_jsonrpc_message(msg)
|
|
349
|
+
if resp is not None:
|
|
350
|
+
sys.stdout.write(json.dumps(resp) + "\n")
|
|
351
|
+
sys.stdout.flush()
|
|
352
|
+
return 0
|
gpu_shift/report.py
ADDED
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
"""Generate PR-ready REPORT.md and card.svg summaries for a GPU port unit."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import html
|
|
6
|
+
from dataclasses import dataclass
|
|
7
|
+
from pathlib import Path
|
|
8
|
+
|
|
9
|
+
from gpu_shift.bench import BenchResult, bench_unit
|
|
10
|
+
from gpu_shift.check import CheckResult, check_unit
|
|
11
|
+
from gpu_shift.diff import DiffResult, diff_unit
|
|
12
|
+
from gpu_shift.manifest import load_manifest
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
@dataclass(frozen=True)
|
|
16
|
+
class ReportResult:
|
|
17
|
+
"""Paths and summary metrics for generated report files."""
|
|
18
|
+
|
|
19
|
+
unit_dir: Path
|
|
20
|
+
unit_name: str
|
|
21
|
+
report_md_path: Path
|
|
22
|
+
card_svg_path: Path
|
|
23
|
+
check_passed: bool
|
|
24
|
+
peak_speedup: float
|
|
25
|
+
crossover_elements: int | None
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def _render_report_md(
|
|
29
|
+
unit_name: str,
|
|
30
|
+
source_file: str,
|
|
31
|
+
enabled: bool,
|
|
32
|
+
diff_res: DiffResult,
|
|
33
|
+
bench_res: BenchResult,
|
|
34
|
+
check_res: CheckResult,
|
|
35
|
+
) -> str:
|
|
36
|
+
check_label = "PASS" if check_res.passed else "FAIL"
|
|
37
|
+
vram_check = next((c for c in check_res.checks if c.name == "vram_clean"), None)
|
|
38
|
+
vram_text = vram_check.detail if vram_check is not None else "Not measured"
|
|
39
|
+
crossover_text = (
|
|
40
|
+
f"{bench_res.crossover_elements:,} elements"
|
|
41
|
+
if bench_res.crossover_elements is not None
|
|
42
|
+
else "CPU faster at tested sizes"
|
|
43
|
+
)
|
|
44
|
+
|
|
45
|
+
lines = [
|
|
46
|
+
f"# gpu-shift Report: `{unit_name}`",
|
|
47
|
+
"",
|
|
48
|
+
"## Summary",
|
|
49
|
+
"",
|
|
50
|
+
"| Metric | Value |",
|
|
51
|
+
"| --- | --- |",
|
|
52
|
+
f"| Function | `{unit_name}` (`{source_file}`) |",
|
|
53
|
+
f"| GPU Adapter | {bench_res.adapter.device} ({bench_res.adapter.backend_type}) |",
|
|
54
|
+
f"| Release Check | **{check_label}** |",
|
|
55
|
+
f"| Numerical Diff | {diff_res.passed_count}/{diff_res.total_count} passed (max abs err: `{diff_res.max_abs_err:.2e}`, rtol=`{diff_res.rtol}`, atol=`{diff_res.atol}`) |",
|
|
56
|
+
f"| VRAM Cleanup | {vram_text} |",
|
|
57
|
+
f"| Peak Speedup | **{bench_res.max_speedup:.2f}x** (CPU->GPU->CPU transfers included) |",
|
|
58
|
+
f"| Crossover (`min_elements`) | {crossover_text} |",
|
|
59
|
+
f"| Enabled in Source | `{'yes' if enabled else 'no'}` (automatic CPU fallback when no GPU) |",
|
|
60
|
+
"",
|
|
61
|
+
]
|
|
62
|
+
|
|
63
|
+
points = bench_res.sweep_points or bench_res.recorded_points
|
|
64
|
+
if points:
|
|
65
|
+
lines.extend(
|
|
66
|
+
[
|
|
67
|
+
"## End-to-End Benchmark (Including Transfers)",
|
|
68
|
+
"",
|
|
69
|
+
"| Shape | Elements | CPU (ms) | GPU (ms) | Speedup |",
|
|
70
|
+
"| --- | ---: | ---: | ---: | ---: |",
|
|
71
|
+
]
|
|
72
|
+
)
|
|
73
|
+
for pt in points:
|
|
74
|
+
lines.append(
|
|
75
|
+
f"| `{pt.label}` | {pt.elements:,} | {pt.cpu_ms:.2f} | {pt.gpu_ms:.2f} | **{pt.speedup:.2f}x** |"
|
|
76
|
+
)
|
|
77
|
+
lines.append("")
|
|
78
|
+
|
|
79
|
+
lines.extend(
|
|
80
|
+
[
|
|
81
|
+
"## Checks",
|
|
82
|
+
"",
|
|
83
|
+
]
|
|
84
|
+
)
|
|
85
|
+
for chk in check_res.checks:
|
|
86
|
+
tag = "PASS" if chk.passed else "FAIL"
|
|
87
|
+
lines.append(f"- **[{tag}] `{chk.name}`:** {chk.detail}")
|
|
88
|
+
|
|
89
|
+
lines.append("")
|
|
90
|
+
return "\n".join(lines)
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
def _render_card_svg(
|
|
94
|
+
unit_name: str,
|
|
95
|
+
diff_res: DiffResult,
|
|
96
|
+
bench_res: BenchResult,
|
|
97
|
+
check_res: CheckResult,
|
|
98
|
+
) -> str:
|
|
99
|
+
if not check_res.passed:
|
|
100
|
+
accent = "#f43f5e"
|
|
101
|
+
badge = "CHECK FAIL"
|
|
102
|
+
elif bench_res.crossover_elements is not None:
|
|
103
|
+
accent = "#2563eb"
|
|
104
|
+
badge = "CHECK PASS"
|
|
105
|
+
else:
|
|
106
|
+
accent = "#f59e0b"
|
|
107
|
+
badge = "CPU FASTER"
|
|
108
|
+
|
|
109
|
+
speed_val = (
|
|
110
|
+
f"{bench_res.max_speedup:.1f}x"
|
|
111
|
+
if bench_res.crossover_elements is not None
|
|
112
|
+
else f"{bench_res.max_speedup:.2f}x"
|
|
113
|
+
)
|
|
114
|
+
diff_val = f"{diff_res.passed_count}/{diff_res.total_count} PASS"
|
|
115
|
+
cross_val = (
|
|
116
|
+
f">= {bench_res.crossover_elements:,}"
|
|
117
|
+
if bench_res.crossover_elements is not None
|
|
118
|
+
else "CPU only"
|
|
119
|
+
)
|
|
120
|
+
vram_chk = next((c for c in check_res.checks if c.name == "vram_clean"), None)
|
|
121
|
+
vram_val = "0 B leak" if (vram_chk and vram_chk.passed) else "Check leak"
|
|
122
|
+
|
|
123
|
+
title_esc = html.escape(unit_name)
|
|
124
|
+
gpu_esc = html.escape(f"{bench_res.adapter.device} ({bench_res.adapter.backend_type})")
|
|
125
|
+
err_esc = html.escape(f"max_abs_err {diff_res.max_abs_err:.2e}")
|
|
126
|
+
|
|
127
|
+
return f"""<svg xmlns="http://www.w3.org/2000/svg" width="720" height="230" viewBox="0 0 720 230" role="img" aria-label="gpu-shift report for {title_esc}">
|
|
128
|
+
<rect width="720" height="230" rx="14" fill="#0f172a"/>
|
|
129
|
+
<rect x="1" y="1" width="718" height="228" rx="13" fill="none" stroke="#1e293b" stroke-width="2"/>
|
|
130
|
+
<rect x="24" y="22" width="6" height="36" rx="3" fill="{accent}"/>
|
|
131
|
+
<text x="42" y="38" fill="#94a3b8" font-family="ui-monospace, SFMono-Regular, Menlo, Consolas, monospace" font-size="12" font-weight="600">gpu-shift</text>
|
|
132
|
+
<text x="42" y="58" fill="#f8fafc" font-family="system-ui, -apple-system, Segoe UI, sans-serif" font-size="22" font-weight="700">{title_esc}</text>
|
|
133
|
+
<rect x="572" y="24" width="124" height="28" rx="14" fill="{accent}" fill-opacity="0.16" stroke="{accent}" stroke-width="1.2"/>
|
|
134
|
+
<text x="634" y="42" text-anchor="middle" fill="{accent}" font-family="ui-monospace, SFMono-Regular, Menlo, Consolas, monospace" font-size="12" font-weight="700">{badge}</text>
|
|
135
|
+
|
|
136
|
+
<rect x="24" y="80" width="156" height="92" rx="10" fill="#1e293b"/>
|
|
137
|
+
<text x="38" y="102" fill="#94a3b8" font-family="system-ui, sans-serif" font-size="11" font-weight="600">PEAK SPEEDUP</text>
|
|
138
|
+
<text x="38" y="138" fill="{accent}" font-family="ui-monospace, Menlo, Consolas, monospace" font-size="28" font-weight="700">{speed_val}</text>
|
|
139
|
+
<text x="38" y="158" fill="#64748b" font-family="system-ui, sans-serif" font-size="11">incl. GPU transfers</text>
|
|
140
|
+
|
|
141
|
+
<rect x="196" y="80" width="156" height="92" rx="10" fill="#1e293b"/>
|
|
142
|
+
<text x="210" y="102" fill="#94a3b8" font-family="system-ui, sans-serif" font-size="11" font-weight="600">NUMERICAL DIFF</text>
|
|
143
|
+
<text x="210" y="136" fill="#f8fafc" font-family="ui-monospace, Menlo, Consolas, monospace" font-size="20" font-weight="700">{diff_val}</text>
|
|
144
|
+
<text x="210" y="158" fill="#64748b" font-family="ui-monospace, Menlo, Consolas, monospace" font-size="11">{err_esc}</text>
|
|
145
|
+
|
|
146
|
+
<rect x="368" y="80" width="156" height="92" rx="10" fill="#1e293b"/>
|
|
147
|
+
<text x="382" y="102" fill="#94a3b8" font-family="system-ui, sans-serif" font-size="11" font-weight="600">CROSSOVER</text>
|
|
148
|
+
<text x="382" y="136" fill="#f8fafc" font-family="ui-monospace, Menlo, Consolas, monospace" font-size="20" font-weight="700">{cross_val}</text>
|
|
149
|
+
<text x="382" y="158" fill="#64748b" font-family="system-ui, sans-serif" font-size="11">min_elements (auto CPU)</text>
|
|
150
|
+
|
|
151
|
+
<rect x="540" y="80" width="156" height="92" rx="10" fill="#1e293b"/>
|
|
152
|
+
<text x="554" y="102" fill="#94a3b8" font-family="system-ui, sans-serif" font-size="11" font-weight="600">VRAM SOAK</text>
|
|
153
|
+
<text x="554" y="136" fill="#f8fafc" font-family="ui-monospace, Menlo, Consolas, monospace" font-size="20" font-weight="700">{vram_val}</text>
|
|
154
|
+
<text x="554" y="158" fill="#64748b" font-family="system-ui, sans-serif" font-size="11">15 dispatches verified</text>
|
|
155
|
+
|
|
156
|
+
<text x="24" y="204" fill="#94a3b8" font-family="ui-monospace, SFMono-Regular, Menlo, Consolas, monospace" font-size="12">{gpu_esc}</text>
|
|
157
|
+
<text x="696" y="204" text-anchor="end" fill="#64748b" font-family="system-ui, sans-serif" font-size="11">WebGPU / WGSL - automatic CPU fallback</text>
|
|
158
|
+
</svg>
|
|
159
|
+
"""
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
def generate_report(
|
|
163
|
+
unit_dir: Path,
|
|
164
|
+
*,
|
|
165
|
+
warmup: int = 2,
|
|
166
|
+
repeats: int = 5,
|
|
167
|
+
update_manifest: bool = False,
|
|
168
|
+
) -> ReportResult:
|
|
169
|
+
"""Run verification + benchmark and write report/REPORT.md and report/card.svg."""
|
|
170
|
+
resolved = unit_dir.resolve()
|
|
171
|
+
manifest = load_manifest(resolved)
|
|
172
|
+
|
|
173
|
+
check_res = check_unit(resolved)
|
|
174
|
+
diff_res = diff_unit(resolved)
|
|
175
|
+
bench_res = bench_unit(
|
|
176
|
+
resolved,
|
|
177
|
+
warmup=warmup,
|
|
178
|
+
repeats=repeats,
|
|
179
|
+
sweep=True,
|
|
180
|
+
update_manifest=update_manifest,
|
|
181
|
+
)
|
|
182
|
+
|
|
183
|
+
report_dir = resolved / "report"
|
|
184
|
+
report_dir.mkdir(parents=True, exist_ok=True)
|
|
185
|
+
|
|
186
|
+
md_path = report_dir / "REPORT.md"
|
|
187
|
+
svg_path = report_dir / "card.svg"
|
|
188
|
+
|
|
189
|
+
md_path.write_text(
|
|
190
|
+
_render_report_md(
|
|
191
|
+
unit_name=manifest.name,
|
|
192
|
+
source_file=manifest.source_file,
|
|
193
|
+
enabled=manifest.enabled,
|
|
194
|
+
diff_res=diff_res,
|
|
195
|
+
bench_res=bench_res,
|
|
196
|
+
check_res=check_res,
|
|
197
|
+
),
|
|
198
|
+
encoding="utf-8",
|
|
199
|
+
)
|
|
200
|
+
svg_path.write_text(
|
|
201
|
+
_render_card_svg(
|
|
202
|
+
unit_name=manifest.name,
|
|
203
|
+
diff_res=diff_res,
|
|
204
|
+
bench_res=bench_res,
|
|
205
|
+
check_res=check_res,
|
|
206
|
+
),
|
|
207
|
+
encoding="utf-8",
|
|
208
|
+
)
|
|
209
|
+
|
|
210
|
+
return ReportResult(
|
|
211
|
+
unit_dir=resolved,
|
|
212
|
+
unit_name=manifest.name,
|
|
213
|
+
report_md_path=md_path,
|
|
214
|
+
card_svg_path=svg_path,
|
|
215
|
+
check_passed=check_res.passed,
|
|
216
|
+
peak_speedup=bench_res.max_speedup,
|
|
217
|
+
crossover_elements=bench_res.crossover_elements,
|
|
218
|
+
)
|