Initial commit

This commit is contained in:
2026-11-16 16:29:32 +01:00
commit 97e5c9ef4d
37 changed files with 5932 additions and 0 deletions
+233
View File
@@ -0,0 +1,233 @@
"""Systemdiagnose und verstaendlicher Systembericht."""
from __future__ import annotations
import os
import shutil
import sqlite3
from dataclasses import dataclass, field
from pathlib import Path
from rich.console import Console
from rich.panel import Panel
from rich.table import Table
from ..config import Config
from ..utils.formatting import format_size
from ..utils.logging import get_logger
from .gpu import (
GpuInfo,
ResolvedExecution,
RuntimeInfo,
collect_runtime_info,
detect_gpu,
is_pascal_or_older,
resolve_execution,
)
logger = get_logger("system.diagnostics")
CHECK = "[green]OK[/green]"
WARN = "[yellow]Warnung[/yellow]"
FAIL = "[red]nicht verfuegbar[/red]"
@dataclass
class SystemReport:
"""Gesammelte Systeminformationen."""
gpu: GpuInfo | None
runtime: RuntimeInfo
execution: ResolvedExecution
db_path: Path
db_exists: bool
config_path: str | None
model_dir: Path
model_dir_writable: bool
ffmpeg: str | None
ffplay: str | None
notes: list[str] = field(default_factory=list)
open_tasks: list[str] = field(default_factory=list)
@property
def gpu_ready(self) -> bool:
return self.runtime.cuda_runtime_usable and self.execution.device == "cuda"
@property
def verdict(self) -> str:
if self.gpu_ready:
return (
f"GPU-Transkription moeglich mit compute_type='{self.execution.compute_type}'."
)
return "Keine GPU-Transkription moeglich - CPU-Fallback wird verwendet."
def _is_writable(path: Path) -> bool:
target = path
while not target.exists() and target != target.parent:
target = target.parent
return target.is_dir() and os.access(target, os.W_OK)
def collect_report(config: Config, *, requested_device: str | None = None,
requested_compute_type: str | None = None) -> SystemReport:
"""Erstellt einen vollstaendigen Systembericht."""
gpu = detect_gpu()
runtime = collect_runtime_info(check_fts=True)
device = requested_device or config.transcription.device
compute_type = requested_compute_type or config.transcription.compute_type
execution = resolve_execution(device, compute_type, gpu, runtime)
db_path = config.database_path
model_dir = config.model_path
notes: list[str] = []
open_tasks: list[str] = []
if gpu is not None and is_pascal_or_older(gpu):
notes.append(
f"{gpu.name} (Compute Capability {gpu.compute_capability}) ist eine Pascal-Karte: "
"kein FP16/BF16, kein Tensor-Core-Betrieb. int8_float32 ist der passende Modus."
)
if runtime.cudnn_libraries:
notes.append(
f"cuDNN gefunden ({len(runtime.cudnn_libraries)} Bibliothek(en)); "
"SjAudioSearch laedt die pip-nvidia-Bibliotheken beim Start automatisch vor."
)
else:
notes.append(
"Kein cuDNN gefunden. Fuer GPU-Betrieb die CUDA-12-Wheels installieren: "
"pip install -e \".[cuda]\" (nvidia-cublas-cu12, nvidia-cudnn-cu12)."
)
if not runtime.fts5_available:
open_tasks.append(
"SQLite ohne FTS5 - bitte Python mit FTS5-Unterstuetzung installieren."
)
if not runtime.trigram_available:
open_tasks.append(
"Trigramm-Tokenizer nicht verfuegbar - die Kandidatensuche arbeitet nur ueber FTS5."
)
return SystemReport(
gpu=gpu,
runtime=runtime,
execution=execution,
db_path=db_path,
db_exists=db_path.exists(),
config_path=str(config.source_path) if config.source_path else None,
model_dir=model_dir,
model_dir_writable=_is_writable(model_dir.parent if not model_dir.exists() else model_dir),
ffmpeg=shutil.which("ffmpeg"),
ffplay=shutil.which("ffplay"),
notes=notes,
open_tasks=open_tasks,
)
def render_report(report: SystemReport, console: Console) -> None:
"""Gibt den Systembericht uebersichtlich im Terminal aus."""
console.print(Panel.fit("[bold]SjAudioSearch - Systemdiagnose[/bold]", border_style="cyan"))
system = Table(show_header=False, box=None, padding=(0, 1))
system.add_column(style="bold cyan", no_wrap=True)
system.add_column()
system.add_row("Python", report.runtime.python_version)
system.add_row("System", report.runtime.platform)
system.add_row("SQLite", report.runtime.sqlite_version)
system.add_row("FTS5", CHECK if report.runtime.fts5_available else FAIL)
system.add_row("FTS5-Trigramme", CHECK if report.runtime.trigram_available else WARN)
system.add_row("CTranslate2", report.runtime.ctranslate2_version or FAIL)
system.add_row("FFmpeg", report.ffmpeg or WARN)
system.add_row("ffplay", report.ffplay or WARN)
system.add_row("Konfiguration", report.config_path or "eingebaute Defaults")
system.add_row("Datenbank", f"{report.db_path} ({'vorhanden' if report.db_exists else 'wird angelegt'})")
system.add_row("Modellordner", f"{report.model_dir} ({'beschreibbar' if report.model_dir_writable else 'NICHT beschreibbar'})")
console.print(system)
console.print()
gpu_table = Table(title="GPU", show_header=False, box=None, padding=(0, 1))
gpu_table.add_column(style="bold cyan", no_wrap=True)
gpu_table.add_column()
if report.gpu is None:
gpu_table.add_row("Status", FAIL + " (nvidia-smi lieferte keine Daten)")
else:
gpu_table.add_row("Modell", report.gpu.name)
gpu_table.add_row("Treiber", report.gpu.driver_version or "unbekannt")
gpu_table.add_row("CUDA (Treiber)", report.gpu.cuda_driver_version or "unbekannt")
gpu_table.add_row(
"VRAM", f"{report.gpu.memory_total_mb} MB" if report.gpu.memory_total_mb else "unbekannt"
)
gpu_table.add_row("Compute Capability", report.gpu.compute_capability or "unbekannt")
gpu_table.add_row("Architektur", "Pascal oder aelter" if is_pascal_or_older(report.gpu) else "neuere Architektur")
gpu_table.add_row(
"CUDA-Geraete (CTranslate2)", str(report.runtime.cuda_device_count)
)
gpu_table.add_row(
"Compute Types (CUDA)",
", ".join(report.runtime.cuda_compute_types) or "keine",
)
gpu_table.add_row(
"Compute Types (CPU)",
", ".join(report.runtime.cpu_compute_types) or "keine",
)
gpu_table.add_row(
"CUDA-Bibliotheken",
", ".join(Path(lib).name for lib in report.runtime.cuda_libraries) or "keine im System",
)
gpu_table.add_row(
"cuDNN-Bibliotheken",
", ".join(Path(lib).name for lib in report.runtime.cudnn_libraries) or "keine im System",
)
console.print(gpu_table)
console.print()
plan = Table(title="Empfohlener Ausfuehrungsmodus", show_header=False, box=None, padding=(0, 1))
plan.add_column(style="bold cyan", no_wrap=True)
plan.add_column()
plan.add_row("Angefragt", f"{report.execution.requested_device} / {report.execution.requested_compute_type}")
plan.add_row("Verwendet", f"{report.execution.device} / {report.execution.compute_type}")
plan.add_row("FP16/BF16", "nicht verwendet" if report.execution.device == "cuda" else "irrelevant (CPU)")
console.print(plan)
for note in report.execution.notes:
console.print(f"[yellow]-[/yellow] {note}")
for note in report.notes:
console.print(f"[cyan]-[/cyan] {note}")
console.print()
color = "green" if report.gpu_ready else "yellow"
console.print(Panel(report.verdict, border_style=color, title="Ergebnis"))
if report.open_tasks:
console.print()
console.print("[bold red]Offene Punkte:[/bold red]")
for task in report.open_tasks:
console.print(f" [red]*[/red] {task}")
def database_summary(db_path: Path) -> dict[str, object] | None:
"""Kurze Zusammenfassung einer vorhandenen Datenbank (fuer die Diagnose)."""
if not db_path.exists():
return None
try:
conn = sqlite3.connect(str(db_path))
try:
tables = {
row[0]
for row in conn.execute(
"SELECT name FROM sqlite_master WHERE type IN ('table','view')"
)
}
audio_count = 0
if "audio_files" in tables:
audio_count = int(conn.execute("SELECT COUNT(*) FROM audio_files").fetchone()[0])
return {
"tables": sorted(tables),
"audio_files": audio_count,
"size": format_size(db_path.stat().st_size),
}
finally:
conn.close()
except sqlite3.Error as exc:
logger.warning("Datenbank konnte nicht gelesen werden: %s", exc)
return None
+406
View File
@@ -0,0 +1,406 @@
"""GPU-, CUDA- und CTranslate2-Diagnose.
Die GTX 1070 Ti ist eine Pascal-Karte (Compute Capability 6.1). Sie besitzt
keine FP16-Tensor-Cores. Deshalb wird FP16/BF16 hier grundsaetzlich nicht als
GPU-Rechenmodus vorausgesetzt; bevorzugt wird ``int8_float32``.
"""
from __future__ import annotations
import ctypes.util
import glob
import os
import re
import subprocess
from dataclasses import dataclass, field
from pathlib import Path
from ..utils.logging import get_logger
logger = get_logger("system.gpu")
# Pascal und aelter: kein FP16-Tensor-Betrieb auf Consumer-Karten.
PASCAL_CONSUMER_MAX_CC = 6.2
PREFERRED_COMPUTE_TYPE = "int8_float32"
FORBIDDEN_GPU_COMPUTE_TYPES = ("float16", "bfloat16", "int8_bfloat16", "int8_float16")
@dataclass
class GpuInfo:
"""Angaben aus ``nvidia-smi``."""
name: str
driver_version: str | None = None
cuda_driver_version: str | None = None
memory_total_mb: int | None = None
compute_capability: str | None = None
@dataclass
class RuntimeInfo:
"""Laufzeitinformationen zu CTranslate2, CUDA und SQLite."""
ctranslate2_version: str | None = None
cuda_device_count: int = 0
supported_compute_types: dict[str, list[str]] = field(default_factory=dict)
cuda_libraries: list[str] = field(default_factory=list)
cudnn_libraries: list[str] = field(default_factory=list)
python_version: str = ""
platform: str = ""
sqlite_version: str = ""
fts5_available: bool = False
trigram_available: bool = False
@property
def cuda_runtime_usable(self) -> bool:
return self.cuda_device_count > 0 and bool(self.supported_compute_types.get("cuda"))
@property
def cuda_compute_types(self) -> list[str]:
return sorted(self.supported_compute_types.get("cuda", []))
@property
def cpu_compute_types(self) -> list[str]:
return sorted(self.supported_compute_types.get("cpu", []))
@dataclass
class ResolvedExecution:
"""Tatsaechlich verwendeter Ausfuehrungsmodus."""
device: str
compute_type: str
requested_device: str
requested_compute_type: str
notes: list[str] = field(default_factory=list)
@property
def used_fallback(self) -> bool:
return self.device != self.requested_device or self.compute_type != self.requested_compute_type
def _run_nvidia_smi(args: list[str]) -> str | None:
try:
completed = subprocess.run(
["nvidia-smi", *args],
capture_output=True,
text=True,
timeout=15,
check=False,
)
except (OSError, subprocess.SubprocessError) as exc:
logger.debug("nvidia-smi nicht ausfuehrbar: %s", exc)
return None
if completed.returncode != 0:
return None
return completed.stdout
def detect_gpu() -> GpuInfo | None:
"""Ermittelt GPU-Modell, Treiber und Compute Capability."""
output = _run_nvidia_smi(
[
"--query-gpu=name,driver_version,memory.total,compute_cap",
"--format=csv,noheader",
]
)
gpu: GpuInfo | None = None
if output:
first_line = output.strip().splitlines()[0] if output.strip() else ""
parts = [part.strip() for part in first_line.split(",")]
if len(parts) >= 4:
memory_mb = None
match = re.search(r"(\d+)", parts[2])
if match:
memory_mb = int(match.group(1))
gpu = GpuInfo(
name=parts[0],
driver_version=parts[1] or None,
memory_total_mb=memory_mb,
compute_capability=parts[3] or None,
)
if gpu is None:
return None
header = _run_nvidia_smi([]) or ""
match = re.search(r"CUDA Version:\s*([0-9.]+)", header)
if match:
gpu.cuda_driver_version = match.group(1)
return gpu
_PRELOADED_LIBRARIES: list[str] = []
def ensure_cuda_libraries() -> list[str]:
"""Laedt CUDA-/cuDNN-Bibliotheken aus den pip-``nvidia``-Paketen.
CTranslate2 linkt dynamisch gegen ``libcublas.so.12`` und ``libcudnn.so.9``.
Die PyPI-Wheels legen diese Bibliotheken unter ``site-packages/nvidia``
ab, wo der dynamische Linker sie nicht von allein findet. Ein Preload mit
``RTLD_GLOBAL`` macht sie verfuegbar, ohne systemweite Aenderungen an
``/etc/ld.so.conf`` oder installierte CUDA-Versionen.
Muss vor dem ersten CUDA-Aufruf von CTranslate2 erfolgen.
"""
global _PRELOADED_LIBRARIES
if _PRELOADED_LIBRARIES or os.name != "posix":
return _PRELOADED_LIBRARIES
cuda_libs, cudnn_libs = detect_cuda_libraries()
if not cuda_libs and not cudnn_libs:
return _PRELOADED_LIBRARIES
order = ("libcudart", "libcublasLt", "libcublas", "libcudnn")
def sort_key(path: str) -> int:
name = Path(path).name
for index, prefix in enumerate(order):
if name.startswith(prefix):
return index
return len(order)
import ctypes
loaded: list[str] = []
for path in sorted(cuda_libs + cudnn_libs, key=sort_key):
# cuDNN-Teilbibliotheken werden von libcudnn.so.9 selbst nachgeladen.
name = Path(path).name
if name.startswith("libcudnn") and not name.startswith("libcudnn.so"):
continue
try:
ctypes.CDLL(path, mode=ctypes.RTLD_GLOBAL)
loaded.append(path)
except OSError as exc:
logger.debug("Bibliothek konnte nicht vorgeladen werden (%s): %s", path, exc)
if loaded:
directories = sorted({str(Path(path).parent) for path in loaded})
existing = os.environ.get("LD_LIBRARY_PATH", "")
os.environ["LD_LIBRARY_PATH"] = os.pathsep.join(
directories + ([existing] if existing else [])
)
logger.debug("CUDA-Bibliotheken vorgeladen: %s", ", ".join(Path(p).name for p in loaded))
_PRELOADED_LIBRARIES = loaded
return loaded
def _find_libraries(patterns: list[str], directories: list[str]) -> list[str]:
found: list[str] = []
for pattern in patterns:
for directory in directories:
try:
found.extend(glob.glob(os.path.join(directory, pattern)))
except OSError:
continue
return sorted(set(found))
def detect_cuda_libraries() -> tuple[list[str], list[str]]:
"""Sucht CUDA- und cuDNN-Bibliotheken im System und in der venv."""
directories = [
"/usr/lib",
"/usr/lib64",
"/usr/local/cuda/lib64",
"/usr/local/lib",
"/opt/cuda/lib64",
str(Path.cwd() / ".venv" / "lib"),
]
# nvidia-Wheels (falls installiert) liegen unter site-packages/nvidia/<paket>/lib
import site
import sys
site_roots: list[Path] = []
for base in (os.environ.get("VIRTUAL_ENV"), os.environ.get("CONDA_PREFIX")):
if base:
site_roots.append(Path(base))
site_roots.extend(Path(p) for p in site.getsitepackages())
site_roots.append(Path(sys.prefix))
for base in site_roots:
directories.extend([str(base / "lib"), str(base / "lib64")])
nvidia_root = base / "lib" / f"python{sys.version_info.major}.{sys.version_info.minor}" / "site-packages" / "nvidia"
directories.extend(str(path) for path in nvidia_root.glob("*/lib"))
directories.extend(str(path) for path in (base / "Lib" / "site-packages" / "nvidia").glob("*/bin"))
cuda_patterns = ["libcudart.so*", "libcublas.so*", "libcublasLt.so*"]
cudnn_patterns = ["libcudnn*.so*"]
cuda_libs = _find_libraries(cuda_patterns, directories)
cudnn_libs = _find_libraries(cudnn_patterns, directories)
# Zusaetzlich ueber den dynamischen Linker suchen.
for name in ("cudart", "cublas", "cudnn"):
located = ctypes.util.find_library(name)
if located and located not in cuda_libs and name != "cudnn":
cuda_libs.append(located)
if located and name == "cudnn" and located not in cudnn_libs:
cudnn_libs.append(located)
return sorted(set(cuda_libs)), sorted(set(cudnn_libs))
def probe_ctranslate2() -> tuple[str | None, int, dict[str, list[str]], str | None]:
"""Fragt CTranslate2 nach Geraeten und unterstuetzten Compute Types."""
try:
import ctranslate2
except ImportError as exc:
return None, 0, {}, f"CTranslate2 nicht installiert: {exc}"
version = getattr(ctranslate2, "__version__", None)
error: str | None = None
try:
device_count = int(ctranslate2.get_cuda_device_count())
except Exception as exc: # pragma: no cover - hardwareabhaengig
device_count = 0
error = f"CUDA-Geraete konnten nicht abgefragt werden: {exc}"
supported: dict[str, list[str]] = {}
for device in ("cpu", "cuda"):
try:
supported[device] = sorted(ctranslate2.get_supported_compute_types(device))
except Exception as exc:
logger.debug("Compute Types fuer %s nicht verfuegbar: %s", device, exc)
supported[device] = []
return version, device_count, supported, error
def collect_runtime_info(*, check_fts: bool = True) -> RuntimeInfo:
"""Sammelt alle Laufzeitinformationen fuer den Systembericht."""
import platform
import sqlite3
import sys
version, device_count, supported, error = probe_ctranslate2()
cuda_libs, cudnn_libs = detect_cuda_libraries()
info = RuntimeInfo(
ctranslate2_version=version,
cuda_device_count=device_count,
supported_compute_types=supported,
cuda_libraries=cuda_libs,
cudnn_libraries=cudnn_libs,
python_version=sys.version.split()[0],
platform=f"{platform.system()} {platform.release()}",
sqlite_version=sqlite3.sqlite_version,
)
if error:
logger.debug(error)
if check_fts:
from ..database.connection import check_fts5, connect
conn = connect(":memory:")
try:
info.fts5_available, info.trigram_available = check_fts5(conn)
finally:
conn.close()
return info
def _cc_tuple(compute_capability: str | None) -> tuple[int, int] | None:
if not compute_capability:
return None
match = re.match(r"^\s*(\d+)\.(\d+)", compute_capability)
if not match:
return None
return int(match.group(1)), int(match.group(2))
def is_pascal_or_older(gpu: GpuInfo | None) -> bool:
"""True fuer Pascal und aeltere Architekturen (Compute Capability <= 6.x)."""
if gpu is None:
return False
cc = _cc_tuple(gpu.compute_capability)
if cc is None:
return False
return cc[0] <= 6
def resolve_execution(
requested_device: str,
requested_compute_type: str,
gpu: GpuInfo | None,
runtime: RuntimeInfo,
) -> ResolvedExecution:
"""Waehlt einen tatsaechlich unterstuetzten Ausfuehrungsmodus.
Regeln:
* Nie einen Compute Type verwenden, den CTranslate2 fuer das Geraet nicht meldet.
* Auf Pascal kein FP16/BF16.
* Wenn CUDA nicht nutzbar ist, automatisch CPU mit einer verstaendlichen Notiz.
"""
device = (requested_device or "auto").strip().lower()
requested_compute = (requested_compute_type or "auto").strip().lower()
notes: list[str] = []
if device in {"auto", ""}:
device = "cuda" if runtime.cuda_runtime_usable else "cpu"
notes.append(f"device=auto auf '{device}' aufgeloest.")
if device == "cuda" and not runtime.cuda_runtime_usable:
notes.append(
"CUDA ist nicht nutzbar (keine CUDA-Geraete oder keine CUDA-Compute-Types) - "
"es wird die CPU verwendet."
)
device = "cpu"
if device == "cuda" and gpu is None:
notes.append("nvidia-smi lieferte keine GPU-Informationen; CUDA wird trotzdem versucht.")
cc = _cc_tuple(gpu.compute_capability) if gpu else None
if device == "cuda" and cc is not None and cc[0] <= 6:
if requested_compute in FORBIDDEN_GPU_COMPUTE_TYPES:
notes.append(
f"'{requested_compute}' ist auf Pascal (CC {gpu.compute_capability}) nicht sinnvoll - "
f"stattdessen {PREFERRED_COMPUTE_TYPE}."
)
requested_compute = PREFERRED_COMPUTE_TYPE
if device == "cpu":
supported = runtime.cpu_compute_types or ["int8", "float32"]
preference = [requested_compute, "int8", "int8_float32", "float32"]
notes.append("CPU-Fallback aktiv: Transkription laeuft ohne GPU und ist deutlich langsamer.")
else:
supported = runtime.cuda_compute_types or []
if gpu is not None and gpu.compute_capability:
supported = [item for item in supported]
# Pascal: FP16/BF16 nie automatisch waehlen.
if cc is not None and cc[0] <= 6:
supported = [item for item in supported if item not in FORBIDDEN_GPU_COMPUTE_TYPES]
preference = [requested_compute, PREFERRED_COMPUTE_TYPE, "float32", "int8"]
chosen = ""
for candidate in preference:
if candidate in {"", "auto"}:
continue
if candidate in supported:
chosen = candidate
break
if not chosen:
# Letzter Ausweg: erster gemeldeter Typ.
chosen = supported[0] if supported else "float32"
notes.append(
f"Kein bevorzugter Compute Type verfuegbar; verwende '{chosen}'."
)
elif chosen != requested_compute and requested_compute not in {"", "auto"}:
notes.append(
f"'{requested_compute}' ist nicht verfuegbar - verwende '{chosen}'."
)
if device == "cuda" and chosen in FORBIDDEN_GPU_COMPUTE_TYPES and cc is not None and cc[0] <= 6:
chosen = PREFERRED_COMPUTE_TYPE
notes.append("FP16/BF16 auf Pascal verhindert, int8_float32 erzwungen.")
return ResolvedExecution(
device=device,
compute_type=chosen,
requested_device=requested_device,
requested_compute_type=requested_compute_type,
notes=notes,
)