407 lines
14 KiB
Python
407 lines
14 KiB
Python
"""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,
|
|
)
|