Files
meeting-lab/scripts/build_whisper_vulkan.py
T

108 lines
3.6 KiB
Python
Executable File

#!/usr/bin/env python3
"""Build the pinned Vulkan CLI separately, preserving the CPU installation."""
from __future__ import annotations
import argparse
import hashlib
import json
import subprocess
from pathlib import Path
PINNED_REVISION = "978113305b2ead22249b881deafa131dc8884911"
FLAGS = [
"-DCMAKE_BUILD_TYPE=Release",
"-DGGML_VULKAN=ON",
"-DGGML_CPU=ON",
"-DGGML_HIP=OFF",
"-DGGML_CUDA=OFF",
"-DWHISPER_BUILD_TESTS=OFF",
"-DWHISPER_BUILD_SERVER=OFF",
]
def sha256(path: Path) -> str:
digest = hashlib.sha256()
with path.open("rb") as handle:
for block in iter(lambda: handle.read(1024 * 1024), b""):
digest.update(block)
return digest.hexdigest()
def capture(*command: str) -> str:
return subprocess.check_output(command, text=True)
def main() -> None:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--source", type=Path, default=Path("/opt/whisper.cpp"))
parser.add_argument("--jobs", type=int, default=4)
args = parser.parse_args()
source = args.source.resolve()
if args.jobs < 1:
parser.error("--jobs must be positive")
revision = capture("git", "-C", str(source), "rev-parse", "HEAD").strip()
changes = capture("git", "-C", str(source), "diff", "HEAD", "--binary")
status = capture("git", "-C", str(source), "status", "--porcelain=v1")
if revision != PINNED_REVISION or changes or status:
parser.error(
"Source must be the clean pinned revision; refusing an unrecorded build."
)
build = source / "build-vulkan"
model = source / "models/ggml-large-v3-turbo.bin"
protected = [
model,
source / "build/bin/whisper-cli",
source / "build/CMakeCache.txt",
]
before = {str(path): sha256(path) for path in protected}
build.mkdir(exist_ok=False)
provenance = build / "provenance"
provenance.mkdir()
configure = ["cmake", "-S", str(source), "-B", str(build), *FLAGS]
compile_command = [
"cmake",
"--build",
str(build),
"--target",
"whisper-cli",
"-j",
str(args.jobs),
]
manifest = {
"source_revision": revision,
"source_status": status,
"protected_sha256": before,
"configure": configure,
"build": compile_command,
"compiler": capture("c++", "--version"),
"cmake": capture("cmake", "--version"),
}
(provenance / "baseline.json").write_text(json.dumps(manifest, indent=2) + "\n")
(provenance / "source.patch").write_text(changes)
(provenance / "cpu-CMakeCache.txt").write_bytes(protected[2].read_bytes())
try:
for command, name in [
(configure, "configure.log"),
(compile_command, "build.log"),
]:
with (provenance / name).open("w") as log:
subprocess.run(
command, stdout=log, stderr=subprocess.STDOUT, check=True
)
(provenance / "vulkan-CMakeCache.txt").write_bytes(
(build / "CMakeCache.txt").read_bytes()
)
manifest["vulkan_binary_sha256"] = sha256(build / "bin/whisper-cli")
finally:
after = {str(path): sha256(path) for path in protected}
manifest["protected_sha256_after"] = after
(provenance / "baseline.json").write_text(json.dumps(manifest, indent=2) + "\n")
if before != after:
raise RuntimeError("Protected CPU installation/model changed during build.")
print(f"Built {build / 'bin/whisper-cli'}; GPU validation is still required.")
if __name__ == "__main__":
main()