Add Vulkan Whisper backend and GPU concurrency tooling
This commit is contained in:
1 parent
405fa6cdf4
commit
b3f8baa7ca
18 files changed
+1326
-12
No files matched your search
@@ -0,0 +1,220 @@
|
||||
"""Regression checks for execution evidence and explicit fallback policy."""
|
||||
|
||||
import json
|
||||
import subprocess
|
||||
import tempfile
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
|
||||
from src.meeting_lab.transcription.whisper import TranscriptionError, transcribe_audio
|
||||
|
||||
GPU_LOG = (
|
||||
"ggml_vulkan: 0 = AMD Radeon AI PRO R9700 (RADV) | uma: 0\n"
|
||||
"whisper_backend_init_gpu: using Vulkan0 backend\n"
|
||||
)
|
||||
RAW = {"transcription": [{"offsets": {"from": 0, "to": 1000}, "text": "Hello"}]}
|
||||
|
||||
|
||||
class BackendTests(unittest.TestCase):
|
||||
def run_case(self, backend, logs):
|
||||
temporary = tempfile.TemporaryDirectory()
|
||||
self.addCleanup(temporary.cleanup)
|
||||
root = Path(temporary.name)
|
||||
audio, model = root / "audio.wav", root / "model.bin"
|
||||
audio.write_bytes(b"audio")
|
||||
model.write_bytes(b"model")
|
||||
commands = []
|
||||
|
||||
def runner(command, **kwargs):
|
||||
commands.append(command)
|
||||
code, log = logs[len(commands) - 1]
|
||||
Path(command[command.index("-of") + 1]).with_suffix(".json").write_text(
|
||||
json.dumps(RAW), encoding="utf-8"
|
||||
)
|
||||
return subprocess.CompletedProcess(command, code, stdout="", stderr=log)
|
||||
|
||||
return (
|
||||
root,
|
||||
commands,
|
||||
lambda: transcribe_audio(
|
||||
audio,
|
||||
model,
|
||||
root / "out",
|
||||
"en",
|
||||
backend=backend,
|
||||
runner=runner,
|
||||
thread_detector=lambda: 2,
|
||||
),
|
||||
)
|
||||
|
||||
def test_cpu_disables_gpu_and_preserves_parameters(self):
|
||||
_root, commands, run = self.run_case("cpu", [(0, "")])
|
||||
result = run()
|
||||
self.assertIn("--no-gpu", commands[0])
|
||||
self.assertIn("-fa", commands[0])
|
||||
self.assertEqual(commands[0][commands[0].index("-l") + 1], "en")
|
||||
metadata = json.loads(result.runtime_metadata.read_text())
|
||||
self.assertEqual(metadata["actual_backend"], "cpu")
|
||||
self.assertIsNone(metadata["fallback_reason"])
|
||||
self.assertEqual(
|
||||
json.loads(result.transcript_json.read_text())["text"], "Hello"
|
||||
)
|
||||
|
||||
def test_verified_vulkan_and_provenance(self):
|
||||
root, commands, run = self.run_case("vulkan", [(0, GPU_LOG)])
|
||||
metadata = json.loads(run().runtime_metadata.read_text())
|
||||
self.assertEqual(metadata["actual_backend"], "vulkan")
|
||||
self.assertEqual(metadata["device_name"], "AMD Radeon AI PRO R9700")
|
||||
self.assertEqual(len(metadata["model_sha256"]), 64)
|
||||
self.assertNotIn("--no-gpu", commands[0])
|
||||
self.assertTrue((root / "out/whisper_attempt_1.log").is_file())
|
||||
|
||||
def test_enumeration_without_selection_fails(self):
|
||||
root, commands, run = self.run_case("vulkan", [(0, GPU_LOG.splitlines()[0])])
|
||||
with self.assertRaisesRegex(
|
||||
TranscriptionError, "Vulkan acceleration unavailable"
|
||||
):
|
||||
run()
|
||||
self.assertEqual(len(commands), 1)
|
||||
self.assertIsNone(
|
||||
json.loads((root / "out/backend_diagnostics.json").read_text())[
|
||||
"actual_backend"
|
||||
]
|
||||
)
|
||||
|
||||
def test_software_vulkan_is_rejected(self):
|
||||
_root, _commands, run = self.run_case(
|
||||
"vulkan", [(0, GPU_LOG.replace("AMD Radeon AI PRO R9700", "llvmpipe"))]
|
||||
)
|
||||
with self.assertRaises(TranscriptionError):
|
||||
run()
|
||||
|
||||
def test_failed_initialization_is_rejected(self):
|
||||
_root, _commands, run = self.run_case(
|
||||
"vulkan", [(0, GPU_LOG + "failed to initialize Vulkan0 backend")]
|
||||
)
|
||||
with self.assertRaises(TranscriptionError):
|
||||
run()
|
||||
|
||||
def test_auto_fallback_is_reported_and_forces_cpu(self):
|
||||
_root, commands, run = self.run_case("auto", [(0, "no GPU found"), (0, "")])
|
||||
with self.assertWarnsRegex(RuntimeWarning, "fallback to CPU"):
|
||||
metadata = json.loads(run().runtime_metadata.read_text())
|
||||
self.assertEqual(metadata["actual_backend"], "cpu")
|
||||
self.assertTrue(metadata["fallback_reason"])
|
||||
self.assertIn("--no-gpu", commands[1])
|
||||
|
||||
def test_auto_vulkan_error_retries_cpu(self):
|
||||
_root, commands, run = self.run_case(
|
||||
"auto", [(1, GPU_LOG + "VK_ERROR_DEVICE_LOST"), (0, "")]
|
||||
)
|
||||
with self.assertWarns(RuntimeWarning):
|
||||
run()
|
||||
self.assertEqual(len(commands), 2)
|
||||
|
||||
def test_auto_does_not_retry_unrelated_failure(self):
|
||||
_root, commands, run = self.run_case("auto", [(7, "invalid model")])
|
||||
with self.assertRaisesRegex(TranscriptionError, "exit code 7"):
|
||||
run()
|
||||
self.assertEqual(len(commands), 1)
|
||||
|
||||
def test_cpu_fallback_failure_is_preserved(self):
|
||||
_root, commands, run = self.run_case(
|
||||
"auto", [(0, "no GPU found"), (9, "CPU failure")]
|
||||
)
|
||||
with (
|
||||
self.assertWarns(RuntimeWarning),
|
||||
self.assertRaisesRegex(TranscriptionError, "exit code 9"),
|
||||
):
|
||||
run()
|
||||
self.assertEqual(len(commands), 2)
|
||||
|
||||
def test_auto_successful_vulkan_does_not_retry(self):
|
||||
_root, commands, run = self.run_case("auto", [(0, GPU_LOG)])
|
||||
self.assertEqual(
|
||||
json.loads(run().runtime_metadata.read_text())["actual_backend"], "vulkan"
|
||||
)
|
||||
self.assertEqual(len(commands), 1)
|
||||
|
||||
def test_invalid_backend_fails_before_execution(self):
|
||||
_root, commands, run = self.run_case("hip", [])
|
||||
with self.assertRaisesRegex(TranscriptionError, "Unsupported Whisper backend"):
|
||||
run()
|
||||
self.assertEqual(commands, [])
|
||||
|
||||
|
||||
class BackendConfigurationTests(unittest.TestCase):
|
||||
def test_cli_and_mvp_backend_selection(self):
|
||||
from scripts.run_mvp_meeting import config_from_args, parse_args
|
||||
from scripts.transcribe_meeting import parse_args as parse_transcription
|
||||
|
||||
for backend in ("cpu", "vulkan", "auto"):
|
||||
args = parse_args(
|
||||
[
|
||||
"audio.wav",
|
||||
"--whisper-model",
|
||||
"model.bin",
|
||||
"--whisper-backend",
|
||||
backend,
|
||||
]
|
||||
)
|
||||
self.assertEqual(config_from_args(args).whisper_backend, backend)
|
||||
transcription = parse_transcription(
|
||||
[
|
||||
"audio.wav",
|
||||
"--model",
|
||||
"model.bin",
|
||||
"--output-dir",
|
||||
"out",
|
||||
"--whisper-backend",
|
||||
backend,
|
||||
]
|
||||
)
|
||||
self.assertEqual(transcription.whisper_backend, backend)
|
||||
|
||||
|
||||
class BackendFailureArtifactTests(unittest.TestCase):
|
||||
def test_auto_cannot_reuse_json_from_failed_gpu_attempt(self):
|
||||
with tempfile.TemporaryDirectory() as temporary:
|
||||
root = Path(temporary)
|
||||
audio, model = root / "audio.wav", root / "model.bin"
|
||||
audio.write_bytes(b"audio")
|
||||
model.write_bytes(b"model")
|
||||
calls = []
|
||||
|
||||
def runner(command, **kwargs):
|
||||
calls.append(command)
|
||||
raw = Path(command[command.index("-of") + 1]).with_suffix(".json")
|
||||
if len(calls) == 1:
|
||||
raw.write_text(json.dumps(RAW))
|
||||
return subprocess.CompletedProcess(
|
||||
command, 1, "", "VK_ERROR_DEVICE_LOST"
|
||||
)
|
||||
return subprocess.CompletedProcess(command, 0, "", "")
|
||||
|
||||
with (
|
||||
self.assertWarns(RuntimeWarning),
|
||||
self.assertRaisesRegex(TranscriptionError, "without producing JSON"),
|
||||
):
|
||||
transcribe_audio(
|
||||
audio, model, root / "out", backend="auto", runner=runner
|
||||
)
|
||||
self.assertFalse((root / "out/whisper_raw.json").exists())
|
||||
|
||||
def test_start_error_persists_diagnostics_without_retry(self):
|
||||
with tempfile.TemporaryDirectory() as temporary:
|
||||
root = Path(temporary)
|
||||
audio, model = root / "audio.wav", root / "model.bin"
|
||||
audio.write_bytes(b"audio")
|
||||
model.write_bytes(b"model")
|
||||
|
||||
def runner(command, **kwargs):
|
||||
raise FileNotFoundError("executable missing")
|
||||
|
||||
with self.assertRaisesRegex(
|
||||
TranscriptionError, "Could not start whisper.cpp"
|
||||
):
|
||||
transcribe_audio(audio, model, root / "out", runner=runner)
|
||||
diagnostic = json.loads((root / "out/backend_diagnostics.json").read_text())
|
||||
self.assertIsNone(diagnostic["actual_backend"])
|
||||
self.assertEqual(diagnostic["error"], "executable missing")
|
||||
Reference in new issue
Block a user