Repository navigation
Studio: fix torch CUDA undefined-symbol errors from a conflicting LD_LIBRARY_PATH - #6905
Conversation
On Linux the dynamic linker reads LD_LIBRARY_PATH before the RUNPATH baked into torch's .so files, so a pre-existing LD_LIBRARY_PATH pointing at a system CUDA (conda, a Docker base image, /usr/local/cuda-*/lib64) shadows torch's bundled nvidia/*/lib libraries and causes undefined-symbol errors when the Studio backend imports torch. Detect torch's lib dirs without importing torch, prepend them to LD_LIBRARY_PATH, and re-exec once (LD_LIBRARY_PATH is only read at process start). Linux-only, sentinel-guarded against re-exec loops, and called only from run.py's __main__ so library/embedder imports (e.g. Colab's `from run import run_server`) are never re-exec'd.
There was a problem hiding this comment.
Code Review
This pull request introduces a mechanism to prepend PyTorch's bundled CUDA libraries to the LD_LIBRARY_PATH on Linux systems, resolving potential conflicts with system CUDA libraries that can cause "undefined symbol" errors. It adds helper functions _fix_torch_cuda_ld_path and _maybe_reexec_for_cuda_ld_path to update the environment and re-execute the process once at startup if necessary. There are no review comments to address, and we have no additional feedback to provide.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
|
@codex review |
|
Codex Review: Didn't find any major issues. Hooray! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
Problem
When the Unsloth Studio backend imports torch on Linux, a pre-existing
LD_LIBRARY_PATHpointing at a system CUDA (a conda env, a Docker base image,/usr/local/cuda-*/lib64) shadows torch's own bundled CUDA runtime insite-packages/nvidia/*/lib, because the dynamic linker readsLD_LIBRARY_PATHbefore the RUNPATH baked into torch's.sofiles. This producesundefined symbol/libcudarterrors at import, and the only workaround today is a manualunset LD_LIBRARY_PATH.main currently corrects
LD_LIBRARY_PATHonly for the llama.cpp / embedding subprocess binaries, not for the backend's ownimport torch.Fix
run.pygains two helpers:_fix_torch_cuda_ld_path()locates torch'slibandnvidia/*/libdirs (viaimportlib.util.find_spec, without importing torch) and prepends them toLD_LIBRARY_PATH. Linux-only; a no-op whenLD_LIBRARY_PATHis unset or torch's libs are already at the front._maybe_reexec_for_cuda_ld_path()applies the fix and re-execs once (needed becauseLD_LIBRARY_PATHis only read at process start), guarded by a_UNSLOTH_STUDIO_LD_FIXEDsentinel to prevent loops.It is called as the first statement in
run.py's__main__block, before torch is imported (torch enters viarun_server). It is not called at import time, so embedders that dofrom run import run_server(e.g. Colab) are never re-exec'd; those keep the manual-unset caveat since a kernel cannot re-exec itself.Testing
python -m py_compileclean.LD_LIBRARY_PATHgets torch's lib dir prepended (existing entries preserved), idempotent on re-run (no loop), no-op whenLD_LIBRARY_PATHis unset, and no path duplication.Supersedes #4402, whose branch was too far behind main to rebase.