kernelvet 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.
Files changed (77) hide show
  1. kernelvet/__init__.py +10 -0
  2. kernelvet/__main__.py +3 -0
  3. kernelvet/benchmark/__init__.py +1 -0
  4. kernelvet/benchmark/engine.py +180 -0
  5. kernelvet/benchmark/workloads.py +153 -0
  6. kernelvet/cli/__init__.py +1 -0
  7. kernelvet/cli/app.py +64 -0
  8. kernelvet/cli/commands/__init__.py +1 -0
  9. kernelvet/cli/commands/diagnose.py +244 -0
  10. kernelvet/cli/commands/migrate.py +334 -0
  11. kernelvet/cli/commands/misc.py +216 -0
  12. kernelvet/cli/output.py +110 -0
  13. kernelvet/compatibility/__init__.py +1 -0
  14. kernelvet/compatibility/data/rocm_matrix.json +123 -0
  15. kernelvet/compatibility/engine.py +24 -0
  16. kernelvet/compatibility/matrix.py +128 -0
  17. kernelvet/compatibility/validators.py +405 -0
  18. kernelvet/config/__init__.py +10 -0
  19. kernelvet/config/settings.py +112 -0
  20. kernelvet/diagnose/__init__.py +1 -0
  21. kernelvet/diagnose/engine.py +296 -0
  22. kernelvet/diagnose/hardware.py +336 -0
  23. kernelvet/diagnose/runtime.py +332 -0
  24. kernelvet/diagnose/software.py +221 -0
  25. kernelvet/diagnose/system.py +67 -0
  26. kernelvet/execution/__init__.py +1 -0
  27. kernelvet/execution/compiler.py +275 -0
  28. kernelvet/execution/runner.py +125 -0
  29. kernelvet/execution/sandbox.py +51 -0
  30. kernelvet/history/__init__.py +3 -0
  31. kernelvet/history/store.py +157 -0
  32. kernelvet/main.py +19 -0
  33. kernelvet/migration/__init__.py +17 -0
  34. kernelvet/migration/analyzer.py +78 -0
  35. kernelvet/migration/auditor.py +89 -0
  36. kernelvet/migration/hipify.py +268 -0
  37. kernelvet/migration/planner.py +128 -0
  38. kernelvet/migration/rules.py +564 -0
  39. kernelvet/migration/scanner.py +234 -0
  40. kernelvet/migration/workflow.py +135 -0
  41. kernelvet/models/__init__.py +35 -0
  42. kernelvet/models/common.py +102 -0
  43. kernelvet/models/migration.py +236 -0
  44. kernelvet/models/report.py +126 -0
  45. kernelvet/models/system.py +89 -0
  46. kernelvet/reporting/__init__.py +80 -0
  47. kernelvet/reporting/common.py +74 -0
  48. kernelvet/reporting/html.py +149 -0
  49. kernelvet/reporting/json.py +20 -0
  50. kernelvet/reporting/loader.py +48 -0
  51. kernelvet/reporting/markdown.py +256 -0
  52. kernelvet/reporting/sarif.py +79 -0
  53. kernelvet/reporting/terminal.py +276 -0
  54. kernelvet/tui/__init__.py +1 -0
  55. kernelvet/tui/app.py +177 -0
  56. kernelvet/tui/screens/__init__.py +1 -0
  57. kernelvet/tui/screens/base.py +34 -0
  58. kernelvet/tui/screens/devices.py +211 -0
  59. kernelvet/tui/screens/diagnose.py +212 -0
  60. kernelvet/tui/screens/help.py +47 -0
  61. kernelvet/tui/screens/migration.py +573 -0
  62. kernelvet/tui/screens/overview.py +195 -0
  63. kernelvet/tui/screens/tools.py +357 -0
  64. kernelvet/tui/state.py +55 -0
  65. kernelvet/tui/widgets/__init__.py +1 -0
  66. kernelvet/tui/widgets/common.py +82 -0
  67. kernelvet/tui/workers/__init__.py +25 -0
  68. kernelvet/utils/__init__.py +1 -0
  69. kernelvet/utils/paths.py +48 -0
  70. kernelvet/utils/proc.py +178 -0
  71. kernelvet/utils/redact.py +47 -0
  72. kernelvet/utils/versions.py +34 -0
  73. kernelvet-0.1.0.dist-info/METADATA +275 -0
  74. kernelvet-0.1.0.dist-info/RECORD +77 -0
  75. kernelvet-0.1.0.dist-info/WHEEL +4 -0
  76. kernelvet-0.1.0.dist-info/entry_points.txt +2 -0
  77. kernelvet-0.1.0.dist-info/licenses/LICENSE +197 -0
@@ -0,0 +1,334 @@
1
+ """`kernelvet migrate ...` — CUDA-to-HIP migration workflow."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from pathlib import Path
6
+
7
+ import typer
8
+
9
+ from kernelvet.cli.output import (
10
+ FormatOpt,
11
+ OutputOpt,
12
+ emit,
13
+ err,
14
+ progress,
15
+ resolve_format,
16
+ settings_or_exit,
17
+ )
18
+ from kernelvet.diagnose.hardware import discover_gpus
19
+ from kernelvet.diagnose.software import detect_libraries
20
+ from kernelvet.execution.compiler import build_project
21
+ from kernelvet.execution.runner import run_tests
22
+ from kernelvet.execution.sandbox import SandboxConfig
23
+ from kernelvet.history import HistoryError, HistoryStore
24
+ from kernelvet.migration import (
25
+ HipifyUnavailable,
26
+ analyze_project,
27
+ audit_project,
28
+ build_plan,
29
+ convert_project,
30
+ )
31
+ from kernelvet.migration.hipify import Translator
32
+ from kernelvet.migration.workflow import (
33
+ assemble_report,
34
+ compute_stages,
35
+ load_state,
36
+ recommendations,
37
+ save_state,
38
+ )
39
+ from kernelvet.models.common import ExitCode
40
+ from kernelvet.models.migration import (
41
+ AuditResult,
42
+ BuildResult,
43
+ ConversionResult,
44
+ FindingCategory,
45
+ StageStatus,
46
+ )
47
+ from kernelvet.models.report import MigrationReport
48
+ from kernelvet.reporting import OutputFormat
49
+ from kernelvet.reporting.terminal import render_migration
50
+ from kernelvet.utils.paths import PathError, existing_dir
51
+ from kernelvet.utils.proc import HostContext
52
+
53
+ app = typer.Typer(
54
+ help="Analyze, plan, convert (via AMD HIPIFY), audit, build and test CUDA→HIP migrations.",
55
+ no_args_is_help=True,
56
+ )
57
+
58
+ ProjectArg = typer.Argument(..., help="Project directory", show_default=False)
59
+ TranslatorOpt = typer.Option(None, "--translator", help="Which HIPIFY to use (default: config value)")
60
+ IsolationOpt = typer.Option(None, "--isolation", help="none or docker (default from config)")
61
+
62
+
63
+ def _dir(path: Path) -> Path:
64
+ try:
65
+ return existing_dir(path)
66
+ except PathError as exc:
67
+ err.print(f"[red]{exc}[/red]")
68
+ raise typer.Exit(ExitCode.USAGE) from exc
69
+
70
+
71
+ def _record(project: Path, stage: str, summary: str, report: MigrationReport | None = None) -> None:
72
+ s = settings_or_exit()
73
+ if not s.history:
74
+ return
75
+ try:
76
+ HistoryStore().record_project(str(project), stage, summary, report)
77
+ except HistoryError as exc: # history is best-effort: never lose the result over it
78
+ err.print(f"[yellow]could not record history: {exc}[/yellow]")
79
+
80
+
81
+ def _targets(ctx: HostContext, explicit: list[str] | None) -> list[str]:
82
+ s = settings_or_exit()
83
+ if explicit:
84
+ return explicit
85
+ if s.offload_arch:
86
+ return [a.strip() for a in s.offload_arch.split(",") if a.strip()]
87
+ return sorted(
88
+ {g.gfx_target.split(":")[0] for g in discover_gpus(ctx).gpus if g.vendor_is_amd and g.gfx_target}
89
+ )
90
+
91
+
92
+ @app.command()
93
+ def analyze(
94
+ project: Path = ProjectArg, fmt: OutputFormat | None = FormatOpt, output: Path | None = OutputOpt
95
+ ) -> None:
96
+ """Scan a CUDA project and classify every CUDA construct (read-only)."""
97
+ root = _dir(project)
98
+ fmt = resolve_format(fmt, output)
99
+ with progress() as cb:
100
+ cb("Scanning project", 0.3)
101
+ analysis = analyze_project(root)
102
+ report = MigrationReport(project=str(root), analysis=analysis)
103
+ _record(root, "analyzed", f"readiness {analysis.readiness.score}/100, {len(analysis.findings)} findings")
104
+ emit(report, fmt, output, lambda: render_migration(report))
105
+
106
+
107
+ @app.command()
108
+ def plan(
109
+ project: Path = ProjectArg,
110
+ fmt: OutputFormat | None = FormatOpt,
111
+ output: Path | None = OutputOpt,
112
+ translator: Translator | None = TranslatorOpt,
113
+ offload_arch: list[str] | None = typer.Option(None, "--offload-arch", help="Target gfx architecture(s)"),
114
+ ) -> None:
115
+ """Produce a migration plan before anything is modified (a dry run of the whole migration)."""
116
+ s = settings_or_exit()
117
+ root = _dir(project)
118
+ fmt = resolve_format(fmt, output)
119
+ ctx = HostContext(timeout=s.command_timeout)
120
+ with progress() as cb:
121
+ cb("Analyzing project", 0.3)
122
+ analysis = analyze_project(root)
123
+ cb("Planning", 0.8)
124
+ p = build_plan(
125
+ analysis,
126
+ ctx,
127
+ preference=translator or s.hipify_preference,
128
+ offload_arch=_targets(ctx, offload_arch),
129
+ )
130
+ report = MigrationReport(project=str(root), analysis=analysis, plan=p)
131
+ report.recommendations = recommendations(report)
132
+ _record(root, "planned", f"{len(p.steps)} steps, {len(p.blockers)} blockers")
133
+ emit(report, fmt, output, lambda: render_migration(report))
134
+ raise typer.Exit(ExitCode.REVIEW if p.blockers or p.translator is None else ExitCode.OK)
135
+
136
+
137
+ @app.command()
138
+ def convert(
139
+ project: Path = ProjectArg,
140
+ output_dir: Path = typer.Option(
141
+ ..., "--output", "-o", help="Directory for the migrated HIP project", file_okay=False
142
+ ),
143
+ dry_run: bool = typer.Option(False, "--dry-run", help="Show what would be converted; write nothing"),
144
+ force: bool = typer.Option(False, "--force", help="Allow writing into a non-empty output directory"),
145
+ translator: Translator | None = TranslatorOpt,
146
+ fmt: OutputFormat | None = FormatOpt,
147
+ report_file: Path | None = typer.Option(None, "--report", help="Also write a report file"),
148
+ ) -> None:
149
+ """Convert CUDA sources to HIP with AMD HIPIFY into a separate directory (sources are never modified)."""
150
+ s = settings_or_exit()
151
+ root = _dir(project)
152
+ ctx = HostContext(timeout=s.command_timeout)
153
+ analysis = analyze_project(root)
154
+ try:
155
+ with progress() as cb:
156
+ result = convert_project(
157
+ ctx,
158
+ analysis.inventory,
159
+ output_dir,
160
+ preference=translator or s.hipify_preference,
161
+ dry_run=dry_run,
162
+ force=force,
163
+ timeout=s.build_timeout,
164
+ progress=cb,
165
+ )
166
+ except HipifyUnavailable as exc:
167
+ err.print(f"[red]HIPIFY unavailable:[/red] {exc}")
168
+ err.print(
169
+ "KernelVet does not translate CUDA itself; install hipify-clang/hipify-perl "
170
+ "(ROCm package `hipify-clang`) and retry."
171
+ )
172
+ raise typer.Exit(ExitCode.UNAVAILABLE) from exc
173
+ except PathError as exc:
174
+ err.print(f"[red]{exc}[/red]")
175
+ raise typer.Exit(ExitCode.USAGE) from exc
176
+ if not dry_run:
177
+ save_state(result.output, "conversion", result)
178
+ report = MigrationReport(
179
+ project=result.output,
180
+ analysis=analysis,
181
+ conversion=result,
182
+ stages=compute_stages(result, None, None, None),
183
+ )
184
+ report.recommendations = recommendations(report)
185
+ if not dry_run:
186
+ _record(
187
+ Path(result.output),
188
+ "converted",
189
+ f"{len(result.files)} files, {len(result.failed)} failed ({result.translator})",
190
+ report,
191
+ )
192
+ fmt = resolve_format(fmt, report_file) if (fmt or report_file) else OutputFormat.TERMINAL
193
+ emit(report, fmt, report_file, lambda: render_migration(report))
194
+ if not dry_run:
195
+ err.print(f"Migrated project: {result.output} · log: {result.log_path}")
196
+ err.print("Next: kernelvet migrate audit " + result.output)
197
+ raise typer.Exit(ExitCode.FAILURE if result.failed else ExitCode.OK)
198
+
199
+
200
+ @app.command()
201
+ def audit(
202
+ project: Path = ProjectArg, fmt: OutputFormat | None = FormatOpt, output: Path | None = OutputOpt
203
+ ) -> None:
204
+ """Statically audit a migrated HIP project for leftover CUDA and risky patterns."""
205
+ s = settings_or_exit()
206
+ root = _dir(project)
207
+ fmt = resolve_format(fmt, output)
208
+ ctx = HostContext(timeout=s.command_timeout)
209
+ rocm = ctx.rocm_path()
210
+ libs = detect_libraries(ctx, rocm) if ctx.exists(rocm) else None
211
+ with progress() as cb:
212
+ cb("Auditing", 0.5)
213
+ result = audit_project(root, installed_libraries=libs)
214
+ if libs is None:
215
+ err.print("[yellow]ROCm not found: missing-library checks were skipped[/yellow]")
216
+ if (root / ".kernelvet").is_dir():
217
+ save_state(root, "audit", result)
218
+ report = MigrationReport(
219
+ project=str(root),
220
+ audit=result,
221
+ stages=compute_stages(load_state(root, "conversion", ConversionResult), result, None, None),
222
+ )
223
+ report.recommendations = recommendations(report)
224
+ _record(root, "audited", f"{len(result.findings)} findings")
225
+ emit(report, fmt, output, lambda: render_migration(report))
226
+ cats = {f.category for f in result.findings}
227
+ if cats & {FindingCategory.MANUAL_INTERVENTION, FindingCategory.UNSUPPORTED}:
228
+ raise typer.Exit(ExitCode.FAILURE)
229
+ raise typer.Exit(ExitCode.REVIEW if FindingCategory.REQUIRES_VERIFICATION in cats else ExitCode.OK)
230
+
231
+
232
+ @app.command()
233
+ def build(
234
+ project: Path = ProjectArg,
235
+ offload_arch: list[str] | None = typer.Option(None, "--offload-arch", help="gfx target(s), e.g. gfx942"),
236
+ isolation: str | None = IsolationOpt,
237
+ fmt: OutputFormat | None = FormatOpt,
238
+ output: Path | None = OutputOpt,
239
+ ) -> None:
240
+ """Compile a migrated project with hipcc (or its HIP CMake build)."""
241
+ s = settings_or_exit()
242
+ root = _dir(project)
243
+ fmt = resolve_format(fmt, output)
244
+ ctx = HostContext(timeout=s.command_timeout)
245
+ iso = isolation or s.isolation
246
+ with progress() as cb:
247
+ cb("Building", 0.3)
248
+ result = build_project(
249
+ ctx,
250
+ root,
251
+ gfx_targets=_targets(ctx, offload_arch),
252
+ isolation=iso,
253
+ sandbox=SandboxConfig(image=s.docker_image),
254
+ timeout=s.build_timeout,
255
+ )
256
+ save_state(root, "build", result)
257
+ report = MigrationReport(
258
+ project=str(root),
259
+ build=result,
260
+ stages=compute_stages(
261
+ load_state(root, "conversion", ConversionResult),
262
+ load_state(root, "audit", AuditResult),
263
+ result,
264
+ None,
265
+ ),
266
+ )
267
+ report.recommendations = recommendations(report)
268
+ _record(root, "built", f"build {result.status}")
269
+ emit(report, fmt, output, lambda: render_migration(report))
270
+ raise typer.Exit(
271
+ {StageStatus.PASSED: ExitCode.OK, StageStatus.UNAVAILABLE: ExitCode.UNAVAILABLE}.get(
272
+ result.status, ExitCode.FAILURE
273
+ )
274
+ )
275
+
276
+
277
+ @app.command()
278
+ def test(
279
+ project: Path = ProjectArg,
280
+ run_all: bool = typer.Option(False, "--all", help="Run every built executable, not just tests"),
281
+ isolation: str | None = IsolationOpt,
282
+ fmt: OutputFormat | None = FormatOpt,
283
+ output: Path | None = OutputOpt,
284
+ ) -> None:
285
+ """Run the migrated project's tests and record whether they ran on an AMD GPU."""
286
+ s = settings_or_exit()
287
+ root = _dir(project)
288
+ fmt = resolve_format(fmt, output)
289
+ ctx = HostContext(timeout=s.command_timeout)
290
+ build_result = load_state(root, "build", BuildResult)
291
+ with progress() as cb:
292
+ cb("Running tests", 0.3)
293
+ result = run_tests(
294
+ ctx,
295
+ root,
296
+ build_result,
297
+ run_all=run_all,
298
+ isolation=isolation or s.isolation,
299
+ sandbox=SandboxConfig(image=s.docker_image),
300
+ timeout=s.test_timeout,
301
+ )
302
+ save_state(root, "tests", result)
303
+ stages = compute_stages(
304
+ load_state(root, "conversion", ConversionResult),
305
+ load_state(root, "audit", AuditResult),
306
+ build_result,
307
+ result,
308
+ )
309
+ report = MigrationReport(project=str(root), build=build_result, tests=result, stages=stages)
310
+ report.recommendations = recommendations(report)
311
+ _record(root, "tested", f"tests {result.status}, GPU validation {stages.gpu_runtime_validation}")
312
+ emit(report, fmt, output, lambda: render_migration(report))
313
+ if result.status is StageStatus.FAILED:
314
+ raise typer.Exit(ExitCode.FAILURE)
315
+ if result.status is StageStatus.NOT_RUN:
316
+ raise typer.Exit(ExitCode.UNAVAILABLE)
317
+ raise typer.Exit(ExitCode.OK if stages.gpu_runtime_validation is StageStatus.PASSED else ExitCode.REVIEW)
318
+
319
+
320
+ @app.command()
321
+ def report(
322
+ project: Path = ProjectArg, fmt: OutputFormat | None = FormatOpt, output: Path | None = OutputOpt
323
+ ) -> None:
324
+ """Assemble the full migration report from every stage run so far."""
325
+ root = _dir(project)
326
+ fmt = resolve_format(fmt, output)
327
+ with progress() as cb:
328
+ cb("Assembling report", 0.5)
329
+ r = assemble_report(root)
330
+ emit(r, fmt, output, lambda: render_migration(r))
331
+ st = r.stages
332
+ if any(getattr(st, k) is StageStatus.FAILED for k in type(st).model_fields):
333
+ raise typer.Exit(ExitCode.FAILURE)
334
+ raise typer.Exit(ExitCode.OK if st.gpu_runtime_validation is StageStatus.PASSED else ExitCode.REVIEW)
@@ -0,0 +1,216 @@
1
+ """`kernelvet config`, `benchmark`, `schema`, `history`."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import typing
6
+ from pathlib import Path
7
+
8
+ import typer
9
+ from pydantic import BaseModel, ValidationError
10
+ from rich.table import Table
11
+
12
+ from kernelvet.benchmark.engine import ITERATIONS_MAX, ITERATIONS_MIN, BenchmarkOptions, run_benchmarks
13
+ from kernelvet.cli.output import (
14
+ FormatOpt,
15
+ OutputOpt,
16
+ emit,
17
+ err,
18
+ out,
19
+ progress,
20
+ resolve_format,
21
+ settings_or_exit,
22
+ write_json,
23
+ )
24
+ from kernelvet.config import Settings, config_path, data_dir, save_settings
25
+ from kernelvet.history import HistoryError, HistoryStore
26
+ from kernelvet.models.common import ExitCode
27
+ from kernelvet.models.report import (
28
+ BenchmarkReport,
29
+ CompatibilityReport,
30
+ DiagnosticReport,
31
+ MigrationReport,
32
+ )
33
+ from kernelvet.reporting import OutputFormat
34
+ from kernelvet.reporting.loader import ReportFileError, load_report_file
35
+ from kernelvet.reporting.terminal import kv_table, render_benchmarks
36
+ from kernelvet.utils.proc import HostContext
37
+
38
+ config_app = typer.Typer(help="Show and edit configuration.", no_args_is_help=True)
39
+
40
+
41
+ @config_app.command("show")
42
+ def config_show(json_out: bool = typer.Option(False, "--json", help="Emit JSON")) -> None:
43
+ """Show the effective configuration (defaults + config file + KERNELVET_* env vars)."""
44
+ s = settings_or_exit()
45
+ if json_out:
46
+ write_json({"path": str(config_path()), "exists": config_path().exists(), "settings": s.model_dump()})
47
+ return
48
+ rows = [(k, "" if v is None else str(v)) for k, v in s.model_dump().items()]
49
+ out.print(
50
+ kv_table(
51
+ f"Configuration ({config_path()}{'' if config_path().exists() else ', not created yet'})", rows
52
+ )
53
+ )
54
+ out.print(f"[dim]Data directory: {data_dir()}[/dim]")
55
+
56
+
57
+ @config_app.command("path")
58
+ def config_path_cmd() -> None:
59
+ """Print the config file path."""
60
+ print(config_path())
61
+
62
+
63
+ def _config_value(key: str, value: str) -> object:
64
+ """What `config set KEY VALUE` stores.
65
+
66
+ An empty VALUE resets the setting to its default. "none"/"null" clear a setting
67
+ that can be unset; everywhere else the text is validated as typed, so
68
+ `isolation none` selects the "none" choice.
69
+ """
70
+ field = Settings.model_fields[key]
71
+ if value == "":
72
+ return field.get_default(call_default_factory=True)
73
+ if value.lower() in ("none", "null") and type(None) in typing.get_args(field.annotation):
74
+ return None
75
+ return value
76
+
77
+
78
+ @config_app.command("set")
79
+ def config_set(
80
+ key: str = typer.Argument(..., help="Setting name (see `kernelvet config show`)"),
81
+ value: str = typer.Argument(..., help='New value; "" resets the setting to its default'),
82
+ ) -> None:
83
+ """Set a configuration value and save it to the config file."""
84
+ s = settings_or_exit()
85
+ if key not in Settings.model_fields:
86
+ err.print(f"[red]unknown setting {key!r}[/red]; valid: {', '.join(Settings.model_fields)}")
87
+ raise typer.Exit(ExitCode.USAGE)
88
+ data = s.model_dump()
89
+ data[key] = _config_value(key, value)
90
+ try:
91
+ new = Settings(**data)
92
+ except ValidationError as exc:
93
+ err.print(f"[red]invalid value for {key}:[/red] {exc.errors()[0]['msg']}")
94
+ raise typer.Exit(ExitCode.USAGE) from exc
95
+ path = save_settings(new)
96
+ err.print(f"[green]saved[/green] {key} = {getattr(new, key)!r} to {path}")
97
+
98
+
99
+ def benchmark(
100
+ hip: bool = typer.Option(True, "--hip/--no-hip", help="HIP kernel, memory and SGEMM benchmarks"),
101
+ torch: bool = typer.Option(True, "--torch/--no-torch", help="PyTorch tensor benchmarks"),
102
+ inference: bool = typer.Option(False, "--inference", help="Also run a synthetic MLP inference benchmark"),
103
+ iterations: int = typer.Option(
104
+ 20, min=ITERATIONS_MIN, max=ITERATIONS_MAX, help="Timed iterations per measurement"
105
+ ),
106
+ profile: bool = typer.Option(False, "--profile", help="Run the HIP benchmark under rocprofv3"),
107
+ profile_dir: Path | None = typer.Option(None, help="Where rocprofv3 writes its output"),
108
+ python: str | None = typer.Option(None, help="Python interpreter for PyTorch benchmarks"),
109
+ fmt: OutputFormat | None = FormatOpt,
110
+ output: Path | None = OutputOpt,
111
+ ) -> None:
112
+ """Measure HIP and PyTorch performance on this host (real runs only; never simulated)."""
113
+ s = settings_or_exit()
114
+ fmt = resolve_format(fmt, output)
115
+ opts = BenchmarkOptions(
116
+ hip=hip,
117
+ torch=torch,
118
+ inference=inference,
119
+ iterations=iterations,
120
+ profile=profile,
121
+ profile_dir=str(profile_dir) if profile_dir else None,
122
+ python=python or s.python_executable,
123
+ )
124
+ if problem := opts.problem():
125
+ err.print(f"[red]{problem}[/red]")
126
+ raise typer.Exit(ExitCode.USAGE)
127
+ with progress() as cb:
128
+ report = run_benchmarks(HostContext(timeout=s.command_timeout), opts, cb)
129
+ if s.history:
130
+ try:
131
+ HistoryStore().record_run(
132
+ "benchmark",
133
+ "/".join(r.status for r in report.results) or "none",
134
+ ", ".join(f"{r.id}: {r.status}" for r in report.results),
135
+ report,
136
+ )
137
+ except HistoryError as exc: # history is best-effort: never lose the results over it
138
+ err.print(f"[yellow]could not record history: {exc}[/yellow]")
139
+ emit(report, fmt, output, lambda: render_benchmarks(report))
140
+ statuses = {r.status for r in report.results}
141
+ if "FAIL" in statuses:
142
+ raise typer.Exit(ExitCode.FAILURE)
143
+ raise typer.Exit(ExitCode.OK if statuses == {"PASS"} else ExitCode.REVIEW)
144
+
145
+
146
+ SCHEMAS: dict[str, type[BaseModel]] = {
147
+ "diagnosis": DiagnosticReport,
148
+ "compatibility": CompatibilityReport,
149
+ "migration": MigrationReport,
150
+ "benchmark": BenchmarkReport,
151
+ }
152
+
153
+
154
+ def schema(kind: str = typer.Argument(..., help="diagnosis, compatibility, migration or benchmark")) -> None:
155
+ """Print the JSON Schema of a report type."""
156
+ model = SCHEMAS.get(kind)
157
+ if model is None:
158
+ err.print(f"[red]unknown report type {kind!r}[/red]; choose from {', '.join(SCHEMAS)}")
159
+ raise typer.Exit(ExitCode.USAGE)
160
+ write_json(model.model_json_schema(mode="serialization")) # describes the JSON we emit
161
+
162
+
163
+ def history(
164
+ limit: int = typer.Option(10, min=1, max=500, help="How many runs and projects to show"),
165
+ json_out: bool = typer.Option(False, "--json", help="Emit JSON"),
166
+ ) -> None:
167
+ """Show recent diagnostic runs and migration projects."""
168
+ store = HistoryStore()
169
+ try:
170
+ runs = store.recent_runs(limit)
171
+ projects = store.recent_projects(limit)
172
+ except HistoryError as exc:
173
+ err.print(f"[red]{exc}[/red]")
174
+ raise typer.Exit(ExitCode.USAGE) from exc
175
+ if json_out:
176
+ write_json({"runs": [r.__dict__ for r in runs], "projects": [p.__dict__ for p in projects]})
177
+ return
178
+ t = Table(title="Recent runs", title_justify="left", title_style="bold")
179
+ for col in ("When", "Kind", "Status", "Summary", "Report"):
180
+ t.add_column(col)
181
+ for r in runs:
182
+ t.add_row(r.created_at, r.kind, r.status, r.summary, r.report_path or "")
183
+ out.print(t)
184
+ p = Table(title="Migration projects", title_justify="left", title_style="bold")
185
+ for col in ("Updated", "Project", "Stage", "Summary"):
186
+ p.add_column(col)
187
+ for pr in projects:
188
+ p.add_row(pr.updated_at, pr.path, pr.stage, pr.summary)
189
+ out.print(p)
190
+
191
+
192
+ def export(
193
+ source: Path = typer.Argument(..., exists=True, dir_okay=False, help="A saved JSON report"),
194
+ fmt: OutputFormat | None = FormatOpt,
195
+ output: Path | None = OutputOpt,
196
+ ) -> None:
197
+ """Re-render a saved JSON report as Markdown, HTML, SARIF or terminal output."""
198
+ from kernelvet.reporting.terminal import render_compatibility, render_diagnosis, render_migration
199
+
200
+ fmt = resolve_format(fmt, output)
201
+ try:
202
+ report = load_report_file(source)
203
+ except ReportFileError as exc:
204
+ err.print(f"[red]{exc}[/red]")
205
+ raise typer.Exit(ExitCode.USAGE) from exc
206
+
207
+ def term() -> object:
208
+ if isinstance(report, DiagnosticReport):
209
+ return render_diagnosis(report)
210
+ if isinstance(report, CompatibilityReport):
211
+ return render_compatibility(report)
212
+ if isinstance(report, MigrationReport):
213
+ return render_migration(report)
214
+ return render_benchmarks(report)
215
+
216
+ emit(report, fmt, output, term) # type: ignore[arg-type]
@@ -0,0 +1,110 @@
1
+ """CLI output plumbing.
2
+
3
+ * Machine-readable output (JSON/Markdown/HTML/SARIF) goes to stdout, or to a
4
+ file with ``--output``; nothing else is ever written to stdout in that mode.
5
+ * Progress, warnings and confirmations always go to stderr.
6
+ """
7
+
8
+ from __future__ import annotations
9
+
10
+ import contextlib
11
+ import json
12
+ import sys
13
+ from collections.abc import Callable, Iterator
14
+ from pathlib import Path
15
+
16
+ import typer
17
+ from pydantic import BaseModel
18
+ from rich.console import Console, RenderableType
19
+
20
+ from kernelvet.config import ConfigError, Settings, load_settings
21
+ from kernelvet.models.common import ExitCode
22
+ from kernelvet.reporting import OutputFormat, UnsupportedFormat, format_from_path, render_text
23
+ from kernelvet.utils.proc import ProgressCallback
24
+
25
+ out = Console(soft_wrap=False)
26
+ err = Console(stderr=True)
27
+
28
+
29
+ def settings_or_exit() -> Settings:
30
+ try:
31
+ return load_settings()
32
+ except ConfigError as exc:
33
+ err.print(f"[red]config error:[/red] {exc}")
34
+ raise typer.Exit(ExitCode.USAGE) from exc
35
+
36
+
37
+ def resolve_format(fmt: OutputFormat | None, output: Path | None) -> OutputFormat:
38
+ if fmt is not None:
39
+ return fmt
40
+ if output is not None:
41
+ guessed = format_from_path(str(output))
42
+ if guessed is None:
43
+ err.print(f"[red]cannot infer a format from {output}; pass --format[/red]")
44
+ raise typer.Exit(ExitCode.USAGE)
45
+ return guessed
46
+ return OutputFormat.TERMINAL
47
+
48
+
49
+ def emit(
50
+ report: BaseModel, fmt: OutputFormat, output: Path | None, terminal: Callable[[], RenderableType]
51
+ ) -> None:
52
+ if fmt is OutputFormat.TERMINAL:
53
+ if output is not None:
54
+ err.print("[red]--output needs a file format (json, markdown, html, sarif)[/red]")
55
+ raise typer.Exit(ExitCode.USAGE)
56
+ out.print(terminal())
57
+ return
58
+ try:
59
+ text = render_text(report, fmt)
60
+ except UnsupportedFormat as exc:
61
+ err.print(f"[red]{exc}[/red]")
62
+ raise typer.Exit(ExitCode.USAGE) from exc
63
+ if output is None:
64
+ sys.stdout.write(text if text.endswith("\n") else text + "\n")
65
+ sys.stdout.flush()
66
+ else:
67
+ try:
68
+ output.parent.mkdir(parents=True, exist_ok=True)
69
+ output.write_text(text)
70
+ except OSError as exc:
71
+ err.print(f"[red]could not write {output}: {exc}[/red]")
72
+ raise typer.Exit(ExitCode.FAILURE) from exc
73
+ err.print(f"[green]wrote[/green] {output} ({fmt.value})")
74
+
75
+
76
+ def write_json(data: object) -> None:
77
+ """Plain JSON on stdout: no colour codes, no wrapping, safe to pipe."""
78
+ sys.stdout.write(json.dumps(data, indent=2, default=str) + "\n")
79
+ sys.stdout.flush()
80
+
81
+
82
+ @contextlib.contextmanager
83
+ def progress(enabled: bool = True) -> Iterator[ProgressCallback]:
84
+ """A stderr spinner; disabled automatically when stderr is not a terminal."""
85
+ if not enabled or not err.is_terminal:
86
+ yield lambda _m, _f: None
87
+ return
88
+ with err.status("Starting…", spinner="dots") as status:
89
+
90
+ def update(message: str, fraction: float) -> None:
91
+ status.update(f"[cyan]{message}[/cyan] [dim]{int(fraction * 100)}%[/dim]")
92
+
93
+ yield update
94
+
95
+
96
+ FormatOpt = typer.Option(
97
+ None,
98
+ "--format",
99
+ "-f",
100
+ help="terminal, json, markdown, html (sarif for migration findings)",
101
+ case_sensitive=False,
102
+ )
103
+ OutputOpt = typer.Option(
104
+ None,
105
+ "--output",
106
+ "-o",
107
+ help="Write the report to a file (format inferred from extension)",
108
+ dir_okay=False,
109
+ writable=True,
110
+ )
@@ -0,0 +1 @@
1
+