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.
- kernelvet/__init__.py +10 -0
- kernelvet/__main__.py +3 -0
- kernelvet/benchmark/__init__.py +1 -0
- kernelvet/benchmark/engine.py +180 -0
- kernelvet/benchmark/workloads.py +153 -0
- kernelvet/cli/__init__.py +1 -0
- kernelvet/cli/app.py +64 -0
- kernelvet/cli/commands/__init__.py +1 -0
- kernelvet/cli/commands/diagnose.py +244 -0
- kernelvet/cli/commands/migrate.py +334 -0
- kernelvet/cli/commands/misc.py +216 -0
- kernelvet/cli/output.py +110 -0
- kernelvet/compatibility/__init__.py +1 -0
- kernelvet/compatibility/data/rocm_matrix.json +123 -0
- kernelvet/compatibility/engine.py +24 -0
- kernelvet/compatibility/matrix.py +128 -0
- kernelvet/compatibility/validators.py +405 -0
- kernelvet/config/__init__.py +10 -0
- kernelvet/config/settings.py +112 -0
- kernelvet/diagnose/__init__.py +1 -0
- kernelvet/diagnose/engine.py +296 -0
- kernelvet/diagnose/hardware.py +336 -0
- kernelvet/diagnose/runtime.py +332 -0
- kernelvet/diagnose/software.py +221 -0
- kernelvet/diagnose/system.py +67 -0
- kernelvet/execution/__init__.py +1 -0
- kernelvet/execution/compiler.py +275 -0
- kernelvet/execution/runner.py +125 -0
- kernelvet/execution/sandbox.py +51 -0
- kernelvet/history/__init__.py +3 -0
- kernelvet/history/store.py +157 -0
- kernelvet/main.py +19 -0
- kernelvet/migration/__init__.py +17 -0
- kernelvet/migration/analyzer.py +78 -0
- kernelvet/migration/auditor.py +89 -0
- kernelvet/migration/hipify.py +268 -0
- kernelvet/migration/planner.py +128 -0
- kernelvet/migration/rules.py +564 -0
- kernelvet/migration/scanner.py +234 -0
- kernelvet/migration/workflow.py +135 -0
- kernelvet/models/__init__.py +35 -0
- kernelvet/models/common.py +102 -0
- kernelvet/models/migration.py +236 -0
- kernelvet/models/report.py +126 -0
- kernelvet/models/system.py +89 -0
- kernelvet/reporting/__init__.py +80 -0
- kernelvet/reporting/common.py +74 -0
- kernelvet/reporting/html.py +149 -0
- kernelvet/reporting/json.py +20 -0
- kernelvet/reporting/loader.py +48 -0
- kernelvet/reporting/markdown.py +256 -0
- kernelvet/reporting/sarif.py +79 -0
- kernelvet/reporting/terminal.py +276 -0
- kernelvet/tui/__init__.py +1 -0
- kernelvet/tui/app.py +177 -0
- kernelvet/tui/screens/__init__.py +1 -0
- kernelvet/tui/screens/base.py +34 -0
- kernelvet/tui/screens/devices.py +211 -0
- kernelvet/tui/screens/diagnose.py +212 -0
- kernelvet/tui/screens/help.py +47 -0
- kernelvet/tui/screens/migration.py +573 -0
- kernelvet/tui/screens/overview.py +195 -0
- kernelvet/tui/screens/tools.py +357 -0
- kernelvet/tui/state.py +55 -0
- kernelvet/tui/widgets/__init__.py +1 -0
- kernelvet/tui/widgets/common.py +82 -0
- kernelvet/tui/workers/__init__.py +25 -0
- kernelvet/utils/__init__.py +1 -0
- kernelvet/utils/paths.py +48 -0
- kernelvet/utils/proc.py +178 -0
- kernelvet/utils/redact.py +47 -0
- kernelvet/utils/versions.py +34 -0
- kernelvet-0.1.0.dist-info/METADATA +275 -0
- kernelvet-0.1.0.dist-info/RECORD +77 -0
- kernelvet-0.1.0.dist-info/WHEEL +4 -0
- kernelvet-0.1.0.dist-info/entry_points.txt +2 -0
- kernelvet-0.1.0.dist-info/licenses/LICENSE +197 -0
|
@@ -0,0 +1,275 @@
|
|
|
1
|
+
"""Compile a migrated HIP project.
|
|
2
|
+
|
|
3
|
+
Strategy:
|
|
4
|
+
|
|
5
|
+
1. If the project's CMake already targets HIP (``enable_language(HIP)`` or
|
|
6
|
+
``LANGUAGES ... HIP``) and ``cmake`` is available, configure and build it.
|
|
7
|
+
2. Otherwise build directly with ``hipcc``: every translation unit containing
|
|
8
|
+
``main`` becomes an executable linked with the shared (non-main) units and
|
|
9
|
+
with the ROCm libraries its includes require. A HIP ``CMakeLists`` that
|
|
10
|
+
reproduces this build is generated under ``.kernelvet/`` for the user.
|
|
11
|
+
|
|
12
|
+
All output lives in ``<project>/.kernelvet/build``; sources are never edited.
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
from __future__ import annotations
|
|
16
|
+
|
|
17
|
+
import re
|
|
18
|
+
import time
|
|
19
|
+
from dataclasses import dataclass, field
|
|
20
|
+
from pathlib import Path
|
|
21
|
+
|
|
22
|
+
from kernelvet.execution.sandbox import SandboxConfig, docker_argv
|
|
23
|
+
from kernelvet.migration.scanner import HDR_EXT, read_text, scan_project
|
|
24
|
+
from kernelvet.models.migration import BuildResult, FileKind, StageStatus
|
|
25
|
+
from kernelvet.utils.proc import CommandResult, HostContext
|
|
26
|
+
|
|
27
|
+
STATE_DIR = ".kernelvet"
|
|
28
|
+
BUILD_SUBDIR = f"{STATE_DIR}/build"
|
|
29
|
+
|
|
30
|
+
COMPILE_KINDS = {FileKind.CUDA_SOURCE, FileKind.HIP_SOURCE, FileKind.CUDA_RELATED_CPP, FileKind.CPP}
|
|
31
|
+
_MAIN = re.compile(r"\bint\s+main\s*\(")
|
|
32
|
+
_HIP_CMAKE = re.compile(r"(enable_language\s*\(\s*HIP\b|project\s*\([^)]*\bHIP\b)", re.IGNORECASE)
|
|
33
|
+
_CUDA_CMAKE = re.compile(r"(enable_language\s*\(\s*CUDA\b|project\s*\([^)]*\bCUDA\b)", re.IGNORECASE)
|
|
34
|
+
|
|
35
|
+
LIB_FLAGS: dict[str, str] = {
|
|
36
|
+
"hipblas": "-lhipblas",
|
|
37
|
+
"hipblaslt": "-lhipblaslt",
|
|
38
|
+
"rocblas": "-lrocblas",
|
|
39
|
+
"hipfft": "-lhipfft",
|
|
40
|
+
"hiprand": "-lhiprand",
|
|
41
|
+
"rocrand": "-lrocrand",
|
|
42
|
+
"hipsparse": "-lhipsparse",
|
|
43
|
+
"hipsolver": "-lhipsolver",
|
|
44
|
+
"rccl": "-lrccl",
|
|
45
|
+
"miopen": "-lMIOpen",
|
|
46
|
+
"roctracer": "-lroctx64",
|
|
47
|
+
}
|
|
48
|
+
_INC = re.compile(r"#\s*include\s*[<\"](\w+)/")
|
|
49
|
+
_ERROR_LINE = re.compile(r"(error:|undefined reference|ld(?:\.lld)?: error|fatal error)", re.IGNORECASE)
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def _errors(text: str, limit: int = 60) -> list[str]:
|
|
53
|
+
return [line.strip() for line in text.splitlines() if _ERROR_LINE.search(line)][:limit]
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def _arch_flags(targets: list[str]) -> list[str]:
|
|
57
|
+
return [f"--offload-arch={t}" for t in sorted({t.split(":")[0] for t in targets if t.startswith("gfx")})]
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
@dataclass
|
|
61
|
+
class DirectBuild:
|
|
62
|
+
units: list[str] = field(default_factory=list) # shared translation units (no main)
|
|
63
|
+
mains: list[str] = field(default_factory=list) # translation units defining main()
|
|
64
|
+
libs: list[str] = field(default_factory=list) # -l flags inferred from includes
|
|
65
|
+
commands: list[list[str]] = field(default_factory=list)
|
|
66
|
+
executables: list[str] = field(default_factory=list)
|
|
67
|
+
notes: list[str] = field(default_factory=list)
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def plan_direct_build(project: Path, targets: list[str]) -> DirectBuild:
|
|
71
|
+
"""Plan a direct hipcc build. All paths are project-relative."""
|
|
72
|
+
inv = scan_project(project)
|
|
73
|
+
plan = DirectBuild()
|
|
74
|
+
libs: set[str] = set()
|
|
75
|
+
for f in inv.files:
|
|
76
|
+
if f.kind not in COMPILE_KINDS or Path(f.path).suffix.lower() in HDR_EXT:
|
|
77
|
+
continue
|
|
78
|
+
text = read_text(project / f.path) or ""
|
|
79
|
+
for m in _INC.finditer(text):
|
|
80
|
+
if m.group(1).lower() in LIB_FLAGS:
|
|
81
|
+
libs.add(LIB_FLAGS[m.group(1).lower()])
|
|
82
|
+
(plan.mains if _MAIN.search(text) else plan.units).append(f.path)
|
|
83
|
+
plan.libs = sorted(libs)
|
|
84
|
+
incdirs = sorted(
|
|
85
|
+
{str(Path(f.path).parent) for f in inv.files if Path(f.path).suffix in (".h", ".hpp", ".cuh", ".hh")}
|
|
86
|
+
)
|
|
87
|
+
inc_flags = ["-I."] + [f"-I{d}" for d in incdirs if d != "."]
|
|
88
|
+
arch = _arch_flags(targets)
|
|
89
|
+
if not plan.mains:
|
|
90
|
+
plan.notes.append("no translation unit defines main(); compiled objects only")
|
|
91
|
+
for u in plan.units:
|
|
92
|
+
obj = f"{BUILD_SUBDIR}/{Path(u).with_suffix('.o').as_posix().replace('/', '_')}"
|
|
93
|
+
plan.commands.append(["hipcc", "-O2", *arch, *inc_flags, "-c", "-x", "hip", u, "-o", obj])
|
|
94
|
+
return plan
|
|
95
|
+
for main_src in plan.mains:
|
|
96
|
+
exe = f"{BUILD_SUBDIR}/{Path(main_src).stem}"
|
|
97
|
+
plan.commands.append(
|
|
98
|
+
["hipcc", "-O2", *arch, *inc_flags, "-x", "hip", main_src, *plan.units, "-o", exe, *plan.libs]
|
|
99
|
+
)
|
|
100
|
+
plan.executables.append(exe)
|
|
101
|
+
return plan
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
def generated_cmake(
|
|
105
|
+
project_name: str, units: list[str], mains: list[str], libs: list[str], targets: list[str]
|
|
106
|
+
) -> str:
|
|
107
|
+
arch = ";".join(sorted({t.split(":")[0] for t in targets})) or "gfx942"
|
|
108
|
+
lines = [
|
|
109
|
+
"# Generated by KernelVet. Review before use.",
|
|
110
|
+
"cmake_minimum_required(VERSION 3.21)",
|
|
111
|
+
f"project({project_name} LANGUAGES CXX HIP)",
|
|
112
|
+
f'set(CMAKE_HIP_ARCHITECTURES "{arch}" CACHE STRING "AMD GPU targets")',
|
|
113
|
+
"find_package(hip REQUIRED)",
|
|
114
|
+
]
|
|
115
|
+
lib_pkgs = {
|
|
116
|
+
"-lhipblas": "hipblas",
|
|
117
|
+
"-lhipfft": "hipfft",
|
|
118
|
+
"-lhiprand": "hiprand",
|
|
119
|
+
"-lhipsparse": "hipsparse",
|
|
120
|
+
"-lhipsolver": "hipsolver",
|
|
121
|
+
"-lrccl": "rccl",
|
|
122
|
+
"-lMIOpen": "miopen",
|
|
123
|
+
"-lrocblas": "rocblas",
|
|
124
|
+
}
|
|
125
|
+
for flag in libs:
|
|
126
|
+
if flag in lib_pkgs:
|
|
127
|
+
lines.append(f"find_package({lib_pkgs[flag]} REQUIRED)")
|
|
128
|
+
srcs = " ".join(units)
|
|
129
|
+
lines += [
|
|
130
|
+
f"set_source_files_properties({' '.join(units + mains)} PROPERTIES LANGUAGE HIP)"
|
|
131
|
+
if units or mains
|
|
132
|
+
else ""
|
|
133
|
+
]
|
|
134
|
+
if units:
|
|
135
|
+
lines += [
|
|
136
|
+
f"add_library(kernelvet_common STATIC {srcs})",
|
|
137
|
+
"target_include_directories(kernelvet_common PUBLIC . include)",
|
|
138
|
+
"target_link_libraries(kernelvet_common PUBLIC hip::device)",
|
|
139
|
+
]
|
|
140
|
+
for m in mains:
|
|
141
|
+
name = Path(m).stem
|
|
142
|
+
lines.append(f"add_executable({name} {m})")
|
|
143
|
+
deps = ["kernelvet_common"] if units else ["hip::device"]
|
|
144
|
+
deps += [f"roc::{lib_pkgs[f]}" for f in libs if f in lib_pkgs]
|
|
145
|
+
lines.append(f"target_link_libraries({name} PRIVATE {' '.join(deps)})")
|
|
146
|
+
return "\n".join(line for line in lines if line) + "\n"
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
def build_project(
|
|
150
|
+
ctx: HostContext,
|
|
151
|
+
path: str | Path,
|
|
152
|
+
*,
|
|
153
|
+
gfx_targets: list[str] | None = None,
|
|
154
|
+
isolation: str = "none",
|
|
155
|
+
sandbox: SandboxConfig | None = None,
|
|
156
|
+
timeout: float = 900.0,
|
|
157
|
+
) -> BuildResult:
|
|
158
|
+
project = Path(path).resolve()
|
|
159
|
+
targets = gfx_targets or []
|
|
160
|
+
docker = isolation == "docker"
|
|
161
|
+
if docker and not ctx.which("docker"):
|
|
162
|
+
return BuildResult(
|
|
163
|
+
project=str(project),
|
|
164
|
+
status=StageStatus.UNAVAILABLE,
|
|
165
|
+
method="none",
|
|
166
|
+
reason="isolation=docker but docker is not installed",
|
|
167
|
+
)
|
|
168
|
+
if not docker and not ctx.which("hipcc"):
|
|
169
|
+
return BuildResult(
|
|
170
|
+
project=str(project),
|
|
171
|
+
status=StageStatus.UNAVAILABLE,
|
|
172
|
+
method="none",
|
|
173
|
+
reason="hipcc not found; install the ROCm HIP SDK or use isolation=docker",
|
|
174
|
+
)
|
|
175
|
+
(project / BUILD_SUBDIR).mkdir(parents=True, exist_ok=True)
|
|
176
|
+
cfg = sandbox or SandboxConfig()
|
|
177
|
+
|
|
178
|
+
def run(cmd: list[str]) -> CommandResult:
|
|
179
|
+
argv = docker_argv(project, cmd, cfg=cfg) if docker else cmd
|
|
180
|
+
return ctx.run(argv, timeout=timeout, cwd=project)
|
|
181
|
+
|
|
182
|
+
cmake_text = read_text(project / "CMakeLists.txt") if (project / "CMakeLists.txt").exists() else None
|
|
183
|
+
t0 = time.monotonic()
|
|
184
|
+
if cmake_text and _HIP_CMAKE.search(cmake_text) and (docker or ctx.which("cmake")):
|
|
185
|
+
bdir = BUILD_SUBDIR + "/cmake"
|
|
186
|
+
configure = ["cmake", "-S", ".", "-B", bdir, "-DCMAKE_BUILD_TYPE=Release"]
|
|
187
|
+
if targets:
|
|
188
|
+
configure.append(
|
|
189
|
+
"-DCMAKE_HIP_ARCHITECTURES=" + ";".join(sorted({t.split(":")[0] for t in targets}))
|
|
190
|
+
)
|
|
191
|
+
r1 = run(configure)
|
|
192
|
+
if not r1.ok:
|
|
193
|
+
return BuildResult(
|
|
194
|
+
project=str(project),
|
|
195
|
+
status=StageStatus.FAILED,
|
|
196
|
+
method="cmake",
|
|
197
|
+
command=configure,
|
|
198
|
+
returncode=r1.returncode,
|
|
199
|
+
stdout=r1.stdout[-20000:],
|
|
200
|
+
stderr=r1.stderr[-20000:],
|
|
201
|
+
errors=_errors(r1.stdout + r1.stderr) or [r1.describe_failure()],
|
|
202
|
+
duration_s=time.monotonic() - t0,
|
|
203
|
+
isolated=docker,
|
|
204
|
+
)
|
|
205
|
+
build = ["cmake", "--build", bdir, "--parallel"]
|
|
206
|
+
r2 = run(build)
|
|
207
|
+
artifacts = [
|
|
208
|
+
str(p.relative_to(project))
|
|
209
|
+
for p in (project / bdir).rglob("*")
|
|
210
|
+
if p.is_file() and p.stat().st_mode & 0o111 and "CMakeFiles" not in p.parts
|
|
211
|
+
]
|
|
212
|
+
return BuildResult(
|
|
213
|
+
project=str(project),
|
|
214
|
+
status=StageStatus.PASSED if r2.ok else StageStatus.FAILED,
|
|
215
|
+
method="cmake",
|
|
216
|
+
command=build,
|
|
217
|
+
returncode=r2.returncode,
|
|
218
|
+
stdout=r2.stdout[-20000:],
|
|
219
|
+
stderr=r2.stderr[-20000:],
|
|
220
|
+
errors=[] if r2.ok else (_errors(r2.stdout + r2.stderr) or [r2.describe_failure()]),
|
|
221
|
+
artifacts=artifacts,
|
|
222
|
+
duration_s=time.monotonic() - t0,
|
|
223
|
+
isolated=docker,
|
|
224
|
+
)
|
|
225
|
+
|
|
226
|
+
plan = plan_direct_build(project, targets)
|
|
227
|
+
reason_parts = []
|
|
228
|
+
if cmake_text and _CUDA_CMAKE.search(cmake_text):
|
|
229
|
+
reason_parts.append(
|
|
230
|
+
"CMakeLists.txt still declares the CUDA language, so it was not used; built directly with hipcc"
|
|
231
|
+
)
|
|
232
|
+
if not targets:
|
|
233
|
+
reason_parts.append("no --offload-arch given and no GPU detected: hipcc uses its default targets")
|
|
234
|
+
reason_parts += plan.notes
|
|
235
|
+
if not plan.commands:
|
|
236
|
+
return BuildResult(
|
|
237
|
+
project=str(project),
|
|
238
|
+
status=StageStatus.FAILED,
|
|
239
|
+
method="hipcc",
|
|
240
|
+
reason="no HIP/C++ translation units found to compile",
|
|
241
|
+
duration_s=0.0,
|
|
242
|
+
isolated=docker,
|
|
243
|
+
)
|
|
244
|
+
try:
|
|
245
|
+
gen = generated_cmake(project.name, plan.units, plan.mains, plan.libs, targets)
|
|
246
|
+
(project / STATE_DIR / "CMakeLists.generated.txt").write_text(gen)
|
|
247
|
+
reason_parts.append(f"generated a HIP CMakeLists at {STATE_DIR}/CMakeLists.generated.txt")
|
|
248
|
+
except OSError:
|
|
249
|
+
pass
|
|
250
|
+
commands, exes = plan.commands, plan.executables
|
|
251
|
+
stdout, stderr, errors = [], [], []
|
|
252
|
+
rc: int | None = 0
|
|
253
|
+
for cmd in commands:
|
|
254
|
+
r = run(cmd)
|
|
255
|
+
stdout.append(f"$ {' '.join(cmd)}\n{r.stdout}")
|
|
256
|
+
stderr.append(r.stderr)
|
|
257
|
+
if not r.ok:
|
|
258
|
+
rc = r.returncode
|
|
259
|
+
errors += _errors(r.stdout + r.stderr) or [r.describe_failure()]
|
|
260
|
+
built = [e for e in exes if (project / e).exists()]
|
|
261
|
+
ok = not errors
|
|
262
|
+
return BuildResult(
|
|
263
|
+
project=str(project),
|
|
264
|
+
status=StageStatus.PASSED if ok else StageStatus.FAILED,
|
|
265
|
+
method="hipcc",
|
|
266
|
+
command=commands[0] if len(commands) == 1 else ["hipcc", f"({len(commands)} commands)"],
|
|
267
|
+
returncode=rc,
|
|
268
|
+
stdout="\n".join(stdout)[-20000:],
|
|
269
|
+
stderr="\n".join(stderr)[-20000:],
|
|
270
|
+
artifacts=built,
|
|
271
|
+
errors=errors,
|
|
272
|
+
duration_s=time.monotonic() - t0,
|
|
273
|
+
isolated=docker,
|
|
274
|
+
reason="; ".join(reason_parts) or None,
|
|
275
|
+
)
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
"""Run a migrated project's tests and record what actually happened.
|
|
2
|
+
|
|
3
|
+
* CMake builds with ``enable_testing()`` run through ``ctest``.
|
|
4
|
+
* Direct hipcc builds run each built executable whose source lives in a test
|
|
5
|
+
directory (or every executable with ``--all``).
|
|
6
|
+
* If ``<name>.expected`` / ``tests/expected/<name>.out`` exists, stdout is
|
|
7
|
+
compared against it (reference output).
|
|
8
|
+
|
|
9
|
+
``ran_on_gpu`` is only set when an AMD GPU was detected on the host; a passing
|
|
10
|
+
test on a GPU-less host is never reported as GPU validation.
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
from __future__ import annotations
|
|
14
|
+
|
|
15
|
+
import time
|
|
16
|
+
from pathlib import Path
|
|
17
|
+
|
|
18
|
+
from kernelvet.diagnose.hardware import discover_gpus
|
|
19
|
+
from kernelvet.execution.compiler import BUILD_SUBDIR
|
|
20
|
+
from kernelvet.execution.sandbox import SandboxConfig, docker_argv
|
|
21
|
+
from kernelvet.migration.scanner import TEST_PATH, scan_project
|
|
22
|
+
from kernelvet.models.migration import BuildResult, StageStatus, TestCaseResult, TestRunResult
|
|
23
|
+
from kernelvet.utils.proc import HostContext
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def _reference_for(project: Path, name: str, source: str | None) -> Path | None:
|
|
27
|
+
candidates = [project / "tests" / "expected" / f"{name}.out", project / f"{name}.expected"]
|
|
28
|
+
if source:
|
|
29
|
+
candidates.append(project / Path(source).with_suffix(".expected"))
|
|
30
|
+
return next((c for c in candidates if c.is_file()), None)
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def _normalise(text: str) -> str:
|
|
34
|
+
return "\n".join(line.rstrip() for line in text.strip().splitlines())
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def run_tests(
|
|
38
|
+
ctx: HostContext,
|
|
39
|
+
path: str | Path,
|
|
40
|
+
build: BuildResult | None,
|
|
41
|
+
*,
|
|
42
|
+
run_all: bool = False,
|
|
43
|
+
isolation: str = "none",
|
|
44
|
+
sandbox: SandboxConfig | None = None,
|
|
45
|
+
timeout: float = 300.0,
|
|
46
|
+
) -> TestRunResult:
|
|
47
|
+
project = Path(path).resolve()
|
|
48
|
+
if build is None or build.status is not StageStatus.PASSED:
|
|
49
|
+
return TestRunResult(
|
|
50
|
+
project=str(project),
|
|
51
|
+
status=StageStatus.NOT_RUN,
|
|
52
|
+
reason="the project has not been built successfully; run `kernelvet migrate build` first",
|
|
53
|
+
)
|
|
54
|
+
gpus = [g for g in discover_gpus(ctx).gpus if g.vendor_is_amd]
|
|
55
|
+
gpu_present = bool(gpus)
|
|
56
|
+
docker = isolation == "docker"
|
|
57
|
+
cfg = sandbox or SandboxConfig()
|
|
58
|
+
|
|
59
|
+
def argv_for(cmd: list[str]) -> list[str]:
|
|
60
|
+
return docker_argv(project, cmd, cfg=cfg, gpu=True) if docker else cmd
|
|
61
|
+
|
|
62
|
+
cases: list[TestCaseResult] = []
|
|
63
|
+
if build.method == "cmake" and (project / BUILD_SUBDIR / "cmake" / "CTestTestfile.cmake").exists():
|
|
64
|
+
cmd = ["ctest", "--test-dir", f"{BUILD_SUBDIR}/cmake", "--output-on-failure"]
|
|
65
|
+
t0 = time.monotonic()
|
|
66
|
+
r = ctx.run(argv_for(cmd), timeout=timeout, cwd=project)
|
|
67
|
+
cases.append(
|
|
68
|
+
TestCaseResult(
|
|
69
|
+
name="ctest",
|
|
70
|
+
command=cmd,
|
|
71
|
+
status=StageStatus.PASSED if r.ok else StageStatus.FAILED,
|
|
72
|
+
returncode=r.returncode,
|
|
73
|
+
stdout=r.stdout[-20000:],
|
|
74
|
+
stderr=r.stderr[-20000:],
|
|
75
|
+
duration_s=time.monotonic() - t0,
|
|
76
|
+
ran_on_gpu=gpu_present and r.ok,
|
|
77
|
+
)
|
|
78
|
+
)
|
|
79
|
+
else:
|
|
80
|
+
inv = scan_project(project)
|
|
81
|
+
sources = {Path(f.path).stem: f.path for f in inv.files}
|
|
82
|
+
for exe in build.artifacts:
|
|
83
|
+
name = Path(exe).name
|
|
84
|
+
src = sources.get(name)
|
|
85
|
+
is_test = bool(src and TEST_PATH.search(src)) or name.startswith("test")
|
|
86
|
+
if not (is_test or run_all):
|
|
87
|
+
continue
|
|
88
|
+
cmd = [f"./{exe}"]
|
|
89
|
+
t0 = time.monotonic()
|
|
90
|
+
r = ctx.run(argv_for(cmd), timeout=timeout, cwd=project)
|
|
91
|
+
ref = _reference_for(project, name, src)
|
|
92
|
+
match = None
|
|
93
|
+
if ref is not None:
|
|
94
|
+
match = _normalise(r.stdout) == _normalise(ref.read_text(errors="replace"))
|
|
95
|
+
ok = r.ok and match is not False
|
|
96
|
+
cases.append(
|
|
97
|
+
TestCaseResult(
|
|
98
|
+
name=name,
|
|
99
|
+
command=cmd,
|
|
100
|
+
status=StageStatus.PASSED if ok else StageStatus.FAILED,
|
|
101
|
+
returncode=r.returncode,
|
|
102
|
+
stdout=r.stdout[-20000:],
|
|
103
|
+
stderr=r.stderr[-20000:],
|
|
104
|
+
duration_s=time.monotonic() - t0,
|
|
105
|
+
expected_output_match=match,
|
|
106
|
+
ran_on_gpu=gpu_present and r.ok,
|
|
107
|
+
)
|
|
108
|
+
)
|
|
109
|
+
if not cases:
|
|
110
|
+
return TestRunResult(
|
|
111
|
+
project=str(project),
|
|
112
|
+
status=StageStatus.NOT_RUN,
|
|
113
|
+
gpu_present=gpu_present,
|
|
114
|
+
reason="no test executables found (sources under tests/ or named test_*); "
|
|
115
|
+
"use --all to run every built executable",
|
|
116
|
+
)
|
|
117
|
+
failed = any(c.status is StageStatus.FAILED for c in cases)
|
|
118
|
+
reason = None if gpu_present else ("no AMD GPU detected: results do not validate GPU execution")
|
|
119
|
+
return TestRunResult(
|
|
120
|
+
project=str(project),
|
|
121
|
+
status=StageStatus.FAILED if failed else StageStatus.PASSED,
|
|
122
|
+
cases=cases,
|
|
123
|
+
gpu_present=gpu_present,
|
|
124
|
+
reason=reason,
|
|
125
|
+
)
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"""Optional Docker isolation for building and running migrated projects.
|
|
2
|
+
|
|
3
|
+
Migrated projects contain arbitrary user code and build scripts. With
|
|
4
|
+
``isolation = "docker"`` every build/test command runs in a throwaway container:
|
|
5
|
+
no network, all capabilities dropped, no privilege escalation, bounded
|
|
6
|
+
processes and memory, and only the project directory mounted. GPU device nodes
|
|
7
|
+
are passed through only for test execution.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from __future__ import annotations
|
|
11
|
+
|
|
12
|
+
from dataclasses import dataclass
|
|
13
|
+
from pathlib import Path
|
|
14
|
+
|
|
15
|
+
CONTAINER_WORKDIR = "/work"
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
@dataclass
|
|
19
|
+
class SandboxConfig:
|
|
20
|
+
image: str = "rocm/dev-ubuntu-24.04:latest"
|
|
21
|
+
network: str = "none"
|
|
22
|
+
memory: str = "8g"
|
|
23
|
+
pids_limit: int = 1024
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def docker_argv(
|
|
27
|
+
project: Path,
|
|
28
|
+
command: list[str],
|
|
29
|
+
*,
|
|
30
|
+
cfg: SandboxConfig,
|
|
31
|
+
gpu: bool = False,
|
|
32
|
+
workdir: str = CONTAINER_WORKDIR,
|
|
33
|
+
) -> list[str]:
|
|
34
|
+
"""Wrap ``command`` (relative to the project root) in ``docker run``."""
|
|
35
|
+
argv = [
|
|
36
|
+
"docker",
|
|
37
|
+
"run",
|
|
38
|
+
"--rm",
|
|
39
|
+
f"--network={cfg.network}",
|
|
40
|
+
"--cap-drop=ALL",
|
|
41
|
+
"--security-opt=no-new-privileges",
|
|
42
|
+
f"--pids-limit={cfg.pids_limit}",
|
|
43
|
+
f"--memory={cfg.memory}",
|
|
44
|
+
"-v",
|
|
45
|
+
f"{project.resolve()}:{CONTAINER_WORKDIR}",
|
|
46
|
+
"-w",
|
|
47
|
+
workdir,
|
|
48
|
+
]
|
|
49
|
+
if gpu:
|
|
50
|
+
argv += ["--device=/dev/kfd", "--device=/dev/dri", "--group-add=video", "--group-add=render"]
|
|
51
|
+
return [*argv, cfg.image, *command]
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
"""Local diagnostic and migration history (SQLite, stdlib only).
|
|
2
|
+
|
|
3
|
+
Lives in ``$XDG_DATA_HOME/kernelvet/history.db``; full JSON reports are saved
|
|
4
|
+
next to it under ``reports/``. Disabled with ``history = false`` in the config.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
import functools
|
|
10
|
+
import sqlite3
|
|
11
|
+
from collections.abc import Callable
|
|
12
|
+
from contextlib import closing
|
|
13
|
+
from dataclasses import dataclass
|
|
14
|
+
from datetime import UTC, datetime
|
|
15
|
+
from pathlib import Path
|
|
16
|
+
from typing import ParamSpec, TypeVar
|
|
17
|
+
|
|
18
|
+
from pydantic import BaseModel
|
|
19
|
+
|
|
20
|
+
from kernelvet.config.settings import data_dir
|
|
21
|
+
|
|
22
|
+
SCHEMA = """
|
|
23
|
+
CREATE TABLE IF NOT EXISTS runs (
|
|
24
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
25
|
+
kind TEXT NOT NULL,
|
|
26
|
+
created_at TEXT NOT NULL,
|
|
27
|
+
status TEXT NOT NULL,
|
|
28
|
+
summary TEXT NOT NULL,
|
|
29
|
+
report_path TEXT
|
|
30
|
+
);
|
|
31
|
+
CREATE TABLE IF NOT EXISTS projects (
|
|
32
|
+
path TEXT PRIMARY KEY,
|
|
33
|
+
updated_at TEXT NOT NULL,
|
|
34
|
+
stage TEXT NOT NULL,
|
|
35
|
+
summary TEXT NOT NULL,
|
|
36
|
+
report_path TEXT
|
|
37
|
+
);
|
|
38
|
+
"""
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
P = ParamSpec("P")
|
|
42
|
+
T = TypeVar("T")
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
class HistoryError(Exception):
|
|
46
|
+
"""The history database or report folder cannot be used (unreadable, corrupt, read-only, ...)."""
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def _guarded(method: Callable[P, T]) -> Callable[P, T]:
|
|
50
|
+
"""Turn SQLite and filesystem errors into one HistoryError that says which file is at fault."""
|
|
51
|
+
|
|
52
|
+
@functools.wraps(method)
|
|
53
|
+
def wrapper(*args: P.args, **kwargs: P.kwargs) -> T:
|
|
54
|
+
try:
|
|
55
|
+
return method(*args, **kwargs)
|
|
56
|
+
except (sqlite3.Error, OSError) as exc:
|
|
57
|
+
store = args[0]
|
|
58
|
+
assert isinstance(store, HistoryStore)
|
|
59
|
+
raise HistoryError(
|
|
60
|
+
f"history in {store.root} is not usable ({exc}). "
|
|
61
|
+
f"Move or delete {store.db_path.name} to start a new history, or set history = false."
|
|
62
|
+
) from exc
|
|
63
|
+
|
|
64
|
+
return wrapper
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
@dataclass
|
|
68
|
+
class RunRecord:
|
|
69
|
+
id: int
|
|
70
|
+
kind: str
|
|
71
|
+
created_at: str
|
|
72
|
+
status: str
|
|
73
|
+
summary: str
|
|
74
|
+
report_path: str | None
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
@dataclass
|
|
78
|
+
class ProjectRecord:
|
|
79
|
+
path: str
|
|
80
|
+
updated_at: str
|
|
81
|
+
stage: str
|
|
82
|
+
summary: str
|
|
83
|
+
report_path: str | None
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
class HistoryStore:
|
|
87
|
+
def __init__(self, root: Path | None = None) -> None:
|
|
88
|
+
self.root = root or data_dir()
|
|
89
|
+
self.db_path = self.root / "history.db"
|
|
90
|
+
self.reports_dir = self.root / "reports"
|
|
91
|
+
|
|
92
|
+
def _connect(self) -> sqlite3.Connection:
|
|
93
|
+
self.root.mkdir(parents=True, exist_ok=True)
|
|
94
|
+
conn = sqlite3.connect(self.db_path)
|
|
95
|
+
conn.executescript(SCHEMA)
|
|
96
|
+
return conn
|
|
97
|
+
|
|
98
|
+
def save_report(self, kind: str, report: BaseModel) -> Path:
|
|
99
|
+
self.reports_dir.mkdir(parents=True, exist_ok=True)
|
|
100
|
+
stamp = datetime.now(UTC).strftime("%Y%m%dT%H%M%S%fZ")
|
|
101
|
+
path = self.reports_dir / f"{stamp}-{kind}.json"
|
|
102
|
+
path.write_text(report.model_dump_json(indent=2))
|
|
103
|
+
return path
|
|
104
|
+
|
|
105
|
+
@_guarded
|
|
106
|
+
def record_run(self, kind: str, status: str, summary: str, report: BaseModel | None = None) -> RunRecord:
|
|
107
|
+
path = str(self.save_report(kind, report)) if report is not None else None
|
|
108
|
+
now = datetime.now(UTC).isoformat(timespec="seconds")
|
|
109
|
+
with closing(self._connect()) as conn, conn:
|
|
110
|
+
cur = conn.execute(
|
|
111
|
+
"INSERT INTO runs(kind, created_at, status, summary, report_path) VALUES (?,?,?,?,?)",
|
|
112
|
+
(kind, now, status, summary, path),
|
|
113
|
+
)
|
|
114
|
+
rid = cur.lastrowid or 0
|
|
115
|
+
return RunRecord(rid, kind, now, status, summary, path)
|
|
116
|
+
|
|
117
|
+
@_guarded
|
|
118
|
+
def record_project(self, path: str, stage: str, summary: str, report: BaseModel | None = None) -> None:
|
|
119
|
+
rpath = str(self.save_report("migration", report)) if report is not None else None
|
|
120
|
+
now = datetime.now(UTC).isoformat(timespec="seconds")
|
|
121
|
+
with closing(self._connect()) as conn, conn:
|
|
122
|
+
conn.execute(
|
|
123
|
+
"INSERT INTO projects(path, updated_at, stage, summary, report_path) VALUES (?,?,?,?,?) "
|
|
124
|
+
"ON CONFLICT(path) DO UPDATE SET updated_at=excluded.updated_at, stage=excluded.stage, "
|
|
125
|
+
"summary=excluded.summary, report_path=COALESCE(excluded.report_path, projects.report_path)",
|
|
126
|
+
(path, now, stage, summary, rpath),
|
|
127
|
+
)
|
|
128
|
+
|
|
129
|
+
@_guarded
|
|
130
|
+
def recent_runs(self, limit: int = 10, kind: str | None = None) -> list[RunRecord]:
|
|
131
|
+
if not self.db_path.exists():
|
|
132
|
+
return []
|
|
133
|
+
q = "SELECT id, kind, created_at, status, summary, report_path FROM runs"
|
|
134
|
+
args: tuple[object, ...] = ()
|
|
135
|
+
if kind:
|
|
136
|
+
q += " WHERE kind = ?"
|
|
137
|
+
args = (kind,)
|
|
138
|
+
q += " ORDER BY id DESC LIMIT ?"
|
|
139
|
+
with closing(self._connect()) as conn:
|
|
140
|
+
return [RunRecord(*row) for row in conn.execute(q, (*args, limit))]
|
|
141
|
+
|
|
142
|
+
@_guarded
|
|
143
|
+
def recent_projects(self, limit: int = 10) -> list[ProjectRecord]:
|
|
144
|
+
if not self.db_path.exists():
|
|
145
|
+
return []
|
|
146
|
+
with closing(self._connect()) as conn:
|
|
147
|
+
rows = conn.execute(
|
|
148
|
+
"SELECT path, updated_at, stage, summary, report_path FROM projects "
|
|
149
|
+
"ORDER BY updated_at DESC LIMIT ?",
|
|
150
|
+
(limit,),
|
|
151
|
+
)
|
|
152
|
+
return [ProjectRecord(*row) for row in rows]
|
|
153
|
+
|
|
154
|
+
def saved_reports(self) -> list[Path]:
|
|
155
|
+
if not self.reports_dir.exists():
|
|
156
|
+
return []
|
|
157
|
+
return sorted(self.reports_dir.glob("*.json"), reverse=True)
|
kernelvet/main.py
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"""Console entry point (``kernelvet``)."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import sys
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def main() -> None:
|
|
9
|
+
from kernelvet.cli.app import app
|
|
10
|
+
|
|
11
|
+
try:
|
|
12
|
+
app()
|
|
13
|
+
except KeyboardInterrupt:
|
|
14
|
+
print("\ninterrupted", file=sys.stderr)
|
|
15
|
+
sys.exit(130)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
if __name__ == "__main__": # pragma: no cover
|
|
19
|
+
main()
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"""CUDA-to-HIP migration: scan, analyze, plan, convert (via AMD HIPIFY), audit."""
|
|
2
|
+
|
|
3
|
+
from kernelvet.migration.analyzer import analyze_project
|
|
4
|
+
from kernelvet.migration.auditor import audit_project
|
|
5
|
+
from kernelvet.migration.hipify import HipifyUnavailable, choose_translator, convert_project
|
|
6
|
+
from kernelvet.migration.planner import build_plan
|
|
7
|
+
from kernelvet.migration.scanner import scan_project
|
|
8
|
+
|
|
9
|
+
__all__ = [
|
|
10
|
+
"HipifyUnavailable",
|
|
11
|
+
"analyze_project",
|
|
12
|
+
"audit_project",
|
|
13
|
+
"build_plan",
|
|
14
|
+
"choose_translator",
|
|
15
|
+
"convert_project",
|
|
16
|
+
"scan_project",
|
|
17
|
+
]
|