Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
Studio sd.cpp: read the linked CUDA major from DT_NEEDED so a CUDA 13 build gets no cu12 cuDNN (#16789)
Daniel Hanc1cb7db61589
diff --git a/studio/backend/core/inference/sd_cpp_cudnn.py b/studio/backend/core/inference/sd_cpp_cudnn.pyindex 43e07747761..a32064200cf 100644--- a/studio/backend/core/inference/sd_cpp_cudnn.py+++ b/studio/backend/core/inference/sd_cpp_cudnn.py@@ -133,13 +133,67 @@ def _scan_file(path: Path) -> tuple[bool, Optional[int]]:return result+def _elf_needed(path: Path) -> Optional[list[str]]:+ """DT_NEEDED names of a 64-bit little-endian ELF (None if not one, or unreadable). Never raises."""+ import struct+ try:+ with path.open("rb") as fh:+ ident = fh.read(64)+ if len(ident) < 64 or ident[:4] != b"\x7fELF" or ident[4] != 2 or ident[5] != 1:+ return None+ shoff = struct.unpack_from("<Q", ident, 0x28)[0]+ shentsize, shnum = struct.unpack_from("<HH", ident, 0x3A)+ if not shoff or shentsize < 64 or not shnum:+ return None+ fh.seek(shoff)+ table = fh.read(shentsize * shnum)+ sections = [+ struct.unpack_from("<IIQQQQIIQQ", table, i * shentsize) for i in range(shnum)+ ]+ names: list[str] = []+ for _name, sh_type, _fl, _addr, off, size, link, _info, _al, entsize in sections:+ if sh_type != 6 or link >= shnum: # SHT_DYNAMIC; sh_link is its string table+ continue+ _n, _t, _f, _a, str_off, str_size, *_rest = sections[link]+ fh.seek(str_off)+ strtab = fh.read(str_size)+ fh.seek(off)+ dyn = fh.read(size)+ step = entsize or 16+ for j in range(0, len(dyn) - 15, step):+ tag, val = struct.unpack_from("<qQ", dyn, j)+ if tag == 0:+ break+ if tag == 1 and val < len(strtab): # DT_NEEDED+ names.append(+ strtab[val : strtab.index(b"\x00", val)].decode("utf-8", "replace")+ )+ return names+ except (OSError, ValueError, struct.error):+ return None+++def _linked_cudart_major(path: Path) -> Optional[int]:+ """The CUDA major ``path`` actually links (its DT_NEEDED libcudart), not one a fallback string merely names."""+ needed = _elf_needed(path) or []+ for major, marker in _CUDART_MARKERS.items():+ if marker.rstrip(b"\x00").decode() in needed:+ return major+ return None++def binary_cudnn_build(binary: Optional[str]) -> tuple[bool, Optional[int]]:- """(built with cuDNN attention, linked CUDA major); ggml may be static or a ``libggml*.so`` beside the binary."""+ """(built with cuDNN attention, linked CUDA major); ggml may be static or a ``libggml*.so`` beside the binary.++ The major comes from DT_NEEDED first: a CUDA 13 ``libggml-cuda`` also carries "libcudart.so.12" in a dlopen+ fallback list, and the string scan alone read it as a CUDA 12 build (then paired it with the cu12 cuDNN)."""if not binary:return False, Nonepath = Path(binary)carries, major = _scan_file(path)- if carries and major is not None:+ linked = _linked_cudart_major(path)+ major = linked if linked is not None else major+ if carries and linked is not None:return carries, majortry:siblings = sorted(path.resolve().parent.glob("libggml*.so*"))@@ -147,7 +201,10 @@ def binary_cudnn_build(binary: Optional[str]) -> tuple[bool, Optional[int]]:siblings = []for lib in siblings:lib_carries, lib_major = _scan_file(lib)+ lib_linked = _linked_cudart_major(lib)carries = carries or lib_carries+ if lib_linked is not None and linked is None:+ linked = major = lib_linkedmajor = major or lib_majorif major is None:for m in _CUDART_MARKERS:diff --git a/studio/backend/tests/test_sd_cpp_cudnn_linked_major.py b/studio/backend/tests/test_sd_cpp_cudnn_linked_major.pynew file mode 100644index 00000000000..7bced7c8741--- /dev/null+++ b/studio/backend/tests/test_sd_cpp_cudnn_linked_major.py@@ -0,0 +1,76 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++"""The CUDA major of an sd.cpp build is the libcudart it links (DT_NEEDED), not the first one a string names.++The CUDA 13 prebuilt's ``libggml-cuda.so`` names "libcudart.so.12" in a dlopen fallback list too; read by string+alone it looked like a CUDA 12 build and was paired with the cu12 cuDNN, which aborts the process on a CUDA 13-only+host ("Invalid handle. Cannot load symbol cublasLtGetVersion", seen on a Colab L4)."""++from __future__ import annotations++import struct+from pathlib import Path++from core.inference import sd_cpp_cudnn as cd+++def _elf_with_needed(+ path: Path,+ needed: list[str],+ extra: bytes = b"",+) -> str:+ """A minimal ELF64 LE: a .dynstr, a .dynamic with DT_NEEDED entries, and ``extra`` bytes (rodata strings)."""+ dynstr = b"\0"+ offsets = []+ for name in needed:+ offsets.append(len(dynstr))+ dynstr += name.encode() + b"\0"+ dynamic = b"".join(struct.pack("<qQ", 1, off) for off in offsets) + struct.pack("<qQ", 0, 0)+ body_off = 64+ dynstr_off = body_off + len(extra)+ dynamic_off = dynstr_off + len(dynstr)+ shoff = dynamic_off + len(dynamic)+ header = bytearray(64)+ header[:6] = b"\x7fELF\x02\x01"+ struct.pack_into("<Q", header, 0x28, shoff)+ struct.pack_into("<HHH", header, 0x3A, 64, 3, 0)+ null = struct.pack("<IIQQQQIIQQ", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)+ sec_dynstr = struct.pack("<IIQQQQIIQQ", 0, 3, 0, 0, dynstr_off, len(dynstr), 0, 0, 1, 0)+ sec_dynamic = struct.pack("<IIQQQQIIQQ", 0, 6, 0, 0, dynamic_off, len(dynamic), 1, 0, 8, 16)+ path.write_bytes(bytes(header) + extra + dynstr + dynamic + null + sec_dynstr + sec_dynamic)+ return str(path)+++def test_cuda13_build_naming_cudart12_as_fallback_reads_as_13(tmp_path):+ lib = _elf_with_needed(+ tmp_path / "libggml-cuda.so.0",+ ["libcublasLt.so.13", "libcudart.so.13", "libcublas.so.13"],+ extra = b"GGML_CUDA_CUDNN_LIB\0libcudart.so.12\0libcudart.so.13\0",+ )+ assert cd._elf_needed(Path(lib)) == ["libcublasLt.so.13", "libcudart.so.13", "libcublas.so.13"]+ exe = tmp_path / "sd-server"+ exe.write_bytes(b"\x7fELF" + b"\0" * 64)+ assert cd.binary_cudnn_build(str(exe)) == (True, 13)+ plan = cd.plan_cudnn_attention(+ str(exe), (8, 9), allow_install = False, platform = "linux", environ = {}+ )+ assert plan.state == cd.STATE_UNAVAILABLE and "CUDA 13" in plan.reason+++def test_cuda12_build_still_reads_as_12(tmp_path):+ _elf_with_needed(+ tmp_path / "libggml-cuda.so.0",+ ["libcudart.so.12", "libcublas.so.12"],+ extra = b"GGML_CUDA_CUDNN_LIB\0libcudart.so.12\0libcudart.so.13\0",+ )+ exe = tmp_path / "sd-cli"+ exe.write_bytes(b"\x7fELF" + b"\0" * 64)+ assert cd.binary_cudnn_build(str(exe)) == (True, 12)+++def test_not_an_elf_keeps_the_string_scan(tmp_path):+ path = tmp_path / "sd-cli"+ path.write_bytes(b"MZ GGML_CUDA_CUDNN_LIB\0libcudart.so.12\0")+ assert cd._elf_needed(path) is None+ assert cd.binary_cudnn_build(str(path)) == (True, 12)