Package: libtorch2.15
Version: 2.15.0+dfsg-1~exp1
Severity: important
Tags: experimental
X-Debbugs-Cc: [email protected]

Dear Maintainer,

libtorch_cpu.so.2.15 links LLVM 23.1.6 statically (Build-Depends:
llvm-23-dev) and exports its symbols in the dynamic symbol table:

  $ nm -DC --defined-only /usr/lib/x86_64-linux-gnu/libtorch_cpu.so.2.15.0 |
grep -c ' llvm::'
  37599

Mesa's Vulkan drivers load their own LLVM (libllvm22 22.1.8 here). In a C++
program linked directly against libtorch, libtorch's exported LLVM symbols
are in the global scope, so the driver's LLVM references bind to libtorch's
copy: two different LLVM versions end up sharing global state. With the
Vulkan backend this crashes:

  - RADV (AMD): correct results, then abort at exit in libtorch_cpu's static
    destructors ("double free or corruption (out)"); the freed pointer is in
    libtorch_cpu's own .bss:
      llvm::UseLEB128Directives + 96 in section .bss of libtorch_cpu.so.2.15
  - llvmpipe (lavapipe): crash during computation ("free(): invalid pointer")
  - NVIDIA's proprietary driver (no LLVM): works correctly

The same operations work from Python (python3-torch 2.15.0+dfsg-1~exp1) on
all three drivers, presumably because Python loads extension modules with
RTLD_LOCAL, keeping libtorch's LLVM symbols out of the global scope.

It reproduces with Debian packages only: forcing Debian's libLLVM.so.22.1
(LD_LIBRARY_PATH=/usr/lib/x86_64-linux-gnu) gives the same crashes, so it
doesn't depend on any third-party driver stack.

Steps to reproduce
------------------

Reproducer (attached as vk-repro.cpp): computes a 256x256 matrix product and
add+relu on the Vulkan device and compares with the CPU.

  $ g++ -std=c++20 -O1 vk-repro.cpp -o vk-repro \
      -I/usr/include/torch/csrc/api/include -ltorch -ltorch_cpu -lc10

  $ VK_DRIVER_FILES=/usr/share/vulkan/icd.d/nvidia_icd.json ./vk-repro
  libtorch 2.15.0, Vulkan available: 1
  256x256 mm on Vulkan: max abs error vs CPU 3.8147e-05; add+relu matches: 1
  (exit status 0)

  $ VK_DRIVER_FILES=/usr/share/vulkan/icd.d/radeon_icd.json ./vk-repro
  double free or corruption (out)
  Aborted (core dumped)

  $ VK_DRIVER_FILES=/usr/share/vulkan/icd.d/lvp_icd.json ./vk-repro
  free(): invalid pointer
  Aborted (core dumped)

With stdout unbuffered and std::quick_exit() at the end (skipping static
destructors), the RADV run prints correct results and exits 0, which
confirms the RADV failure is in exit-time destruction; llvmpipe still
crashes, since it JIT-compiles shaders with LLVM during the computation.

Backtrace (RADV), abridged:

  #6  malloc_printerr (str="double free or corruption (out)")
  #7  _int_free_merge_chunk (p=0x... <llvm::UseLEB128Directives+96>, ...)
  #9  ?? () from /usr/lib/x86_64-linux-gnu/libtorch_cpu.so.2.15
  #10 __cxa_finalize (...)
  #11 ?? () from /usr/lib/x86_64-linux-gnu/libtorch_cpu.so.2.15
  #13 _dl_call_fini (...)
  #14 _dl_fini ()
  #16 __GI_exit (status=<optimized out>)
  #17 __libc_start_call_main (main=<main>, ...)

Expected
--------

A C++ program using libtorch's Vulkan backend works under any Vulkan driver,
as it does from Python; libtorch's private copy of LLVM doesn't interfere with
other libraries' LLVM.

Possible fixes
--------------

  - Don't export the bundled LLVM: link the static LLVM archives with
    -Wl,--exclude-libs,ALL (or name the libLLVM*.a archives), or use a
    linker version script exporting only libtorch's own API.
  - Or link Debian's shared libLLVM-23 instead of the static archives; its
    versioned symbols (LLVM_23) can coexist with Mesa's libLLVM.so.22.1 in
    one process.

Workarounds for users
---------------------

  - Select a Vulkan driver that doesn't use LLVM (e.g. NVIDIA's) with
    VK_DRIVER_FILES.
  - Load libtorch privately (dlopen with RTLD_LOCAL behind a plugin boundary),
    as Python does.

Not tested: whether libtorch2.14 (built with llvm-22, the same major version
as Mesa's LLVM here) shows the same problem.

System
------

Debian GNU/Linux forky/sid, amd64; kernel 7.4.0-rc3
libtorch2.15, libtorch-dev, python3-torch  2.15.0+dfsg-1~exp1
mesa-vulkan-drivers                         26.3.1-1
libllvm22                                   1:22.1.8-4
libvulkan1                                  1.4.363.0-1
g++                                         4:16.2.0-1 (GCC 16.2.0)
GPUs: AMD Radeon RX 7600 XT (RADV), NVIDIA GeForce RTX 3060 Ti
(proprietary driver), plus llvmpipe


-- System Information:
Debian Release: forky/sid
  APT prefers unstable
  APT policy: (500, 'unstable'), (500, 'stable'), (1, 'experimental')
Architecture: amd64 (x86_64)
Foreign Architectures: i386

Kernel: Linux 7.4.0-rc3-wrx90+ (SMP w/64 CPU threads; PREEMPT)
Kernel taint flags: TAINT_OOT_MODULE, TAINT_UNSIGNED_MODULE
Locale: LANG=C.UTF-8, LC_CTYPE=C.UTF-8 (charmap=UTF-8), LANGUAGE not set
Shell: /bin/sh linked to /usr/bin/dash
Init: systemd (via /run/systemd/system)

Versions of packages libtorch2.15 depends on:
ii  libblas3 [libblas.so.3]                3.12.1-9
ii  libc6                                  2.44-3
ii  libcpp-httplib0.41                     0.41.0+ds-3
ii  libcpuinfo0                            0.0~git20250905.877328f-1+b1
ii  libdnnl3.6                             3.14.2+ds-1
ii  libfmt12                               12.2.1+ds1-1
ii  libgcc-s1                              16.2.0-5
ii  libgloo0                               0.0~git20250912.d97133a-2
ii  libgomp1                               16.2.0-5
ii  liblapack3 [liblapack.so.3]            3.12.1-9
ii  libnuma1                               2.0.19-1+b2
ii  libonnx1l                              1.21.0-1
ii  libopenblas0-openmp [liblapack.so.3]   0.3.34+ds-2
ii  libopenblas0-pthread [liblapack.so.3]  0.3.34+ds-2
ii  libopenmpi40                           6.0.0~rc2-2
ii  libprotobuf32t64                       3.21.12-17
ii  libpthreadpool0                        0.0~git20251020.0e6ca13-1.1
ii  libsleef3                              3.9.0-1
ii  libstdc++6                             16.2.0-5
ii  libvulkan1                             1.4.363.0-1
ii  libxnnpack0.20241108                   0.0~git20241108.4ea82e5-2+b1
ii  libz3-4                                4.13.3-1.1
ii  libzstd1                               1.5.7+dfsg-4
ii  zlib1g                                 1:1.3.dfsg+really1.3.2-4

Versions of packages libtorch2.15 recommends:
ii  libblas3      3.12.1-9
ii  libopenblas0  0.3.34+ds-2

libtorch2.15 suggests no packages.

-- no debconf information
// Minimal reproducer: libtorch Vulkan backend from C++ under a Mesa Vulkan 
driver.
// Build: g++ -std=c++20 -O1 vk-repro.cpp -o vk-repro 
-I/usr/include/torch/csrc/api/include -ltorch -ltorch_cpu -lc10
// Does the system libtorch (C++) compute on a Vulkan device?
#include <torch/torch.h>
#include <iostream>

int main() {
    std::cout << "libtorch " << TORCH_VERSION << ", Vulkan available: " << 
at::is_vulkan_available() << "\n";
    if (!at::is_vulkan_available()) return 1;
    torch::manual_seed(0);
    auto a = torch::randn({256, 256}), b = torch::randn({256, 256});
    auto on_gpu = torch::mm(a.vulkan(), b.vulkan()).cpu();
    auto on_cpu = torch::mm(a, b);
    double err = (on_gpu - on_cpu).abs().max().item<double>();
    auto r = torch::relu(a.vulkan() + b.vulkan()).cpu();
    bool relu_ok = torch::allclose(r, torch::relu(a + b));
    std::cout << "256x256 mm on Vulkan: max abs error vs CPU " << err << "; 
add+relu matches: " << relu_ok << "\n";
    return err < 1e-3 && relu_ok ? 0 : 1;
}

Reply via email to