Skip to content

Studio: fix torch CUDA undefined-symbol errors from a conflicting LD_LIBRARY_PATH - #6905

Merged
danielhanchen merged 2 commits into
mainfrom
fix/studio-torch-cuda-ld-reexec
Jul 7, 2026
Merged

danielhanchen merged 2 commits into
mainfrom
fix/studio-torch-cuda-ld-reexec

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Problem

When the Unsloth Studio backend imports torch on Linux, a pre-existing LD_LIBRARY_PATH pointing at a system CUDA (a conda env, a Docker base image, /usr/local/cuda-*/lib64) shadows torch's own bundled CUDA runtime in site-packages/nvidia/*/lib, because the dynamic linker reads LD_LIBRARY_PATH before the RUNPATH baked into torch's .so files. This produces undefined symbol / libcudart errors at import, and the only workaround today is a manual unset LD_LIBRARY_PATH.

main currently corrects LD_LIBRARY_PATH only for the llama.cpp / embedding subprocess binaries, not for the backend's own import torch.

Fix

run.py gains two helpers:

  • _fix_torch_cuda_ld_path() locates torch's lib and nvidia/*/lib dirs (via importlib.util.find_spec, without importing torch) and prepends them to LD_LIBRARY_PATH. Linux-only; a no-op when LD_LIBRARY_PATH is unset or torch's libs are already at the front.
  • _maybe_reexec_for_cuda_ld_path() applies the fix and re-execs once (needed because LD_LIBRARY_PATH is only read at process start), guarded by a _UNSLOTH_STUDIO_LD_FIXED sentinel to prevent loops.

It is called as the first statement in run.py's __main__ block, before torch is imported (torch enters via run_server). It is not called at import time, so embedders that do from 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_compile clean.
  • Unit test of the helper: a conflicting system CUDA on LD_LIBRARY_PATH gets torch's lib dir prepended (existing entries preserved), idempotent on re-run (no loop), no-op when LD_LIBRARY_PATH is unset, and no path duplication.

Supersedes #4402, whose branch was too far behind main to rebase.

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.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Hooray!

Reviewed commit: 457e96272d

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

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".

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Jul 6, 2026
@danielhanchen
danielhanchen merged commit 08226c2 into main Jul 7, 2026
54 of 55 checks passed
@danielhanchen
danielhanchen deleted the fix/studio-torch-cuda-ld-reexec branch July 7, 2026 06:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant