[Bug Report] Severe VRAM Allocation Instability in PyTorch after llama-cpp-python is Imported
Les mainteneurs répondent en général sous 1 jour
Personne n'a encore pris cette issue.
Évaluation
- Difficulté
- 5/5
- Temps estimé
- Plus d'une semaine
- Accessibilité débutants
- 30/100
- Type d'issue
- Bug
- Clarté
- Plutôt claire
- Activité
- À l'abandon
- Domaine
- ai-infra-agents
Piste de recherche
L’issue décrit un conflit de bas niveau entre les contextes CUDA de llama-cpp-python et de PyTorch. Commencez par examiner le code d’initialisation CUDA dans les bindings de llama-cpp-python, en particulier autour du moment de l’importation. Recherchez toute création de contexte CUDA ou tout paramétrage de l’allocateur de mémoire. Reproduisez le problème dans un environnement minimal qui importe llama-cpp-python puis charge un grand modèle PyTorch, en surveillant les schémas d’allocation de VRAM. « Done » consisterait à identifier l’appel ou la configuration spécifique à l’origine du conflit et à proposer un correctif.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Description
Prerequisites
- I am running the latest code.
- I carefully followed the README.md.
- I searched using keywords relevant to my issue to make sure that I am creating a new issue that is not already open (or closed).
- I reviewed the Discussions, and have a new bug to share.
Expected Behavior
When loading a large PyTorch-based model (e.g., an SDXL checkpoint >10GB) within a host application (ComfyUI), I expect the GPU's dedicated memory (VRAM) usage to increase smoothly and stabilize at the required level for the model. This process should be fast and efficient.
Current Behavior
The VRAM allocation is extremely erratic and inefficient. The memory usage graph shows a severe sawtooth/spiky pattern—it spikes up, drops slightly, spikes again, and repeats this cycle until the model is finally loaded. This process is significantly slower than normal and suggests a fundamental conflict in memory management at the CUDA level.
This behavior only occurs when a component that imports llama-cpp-python is loaded by the host application at startup. The issue manifests before any llama-cpp-python functions are actively called; the mere act of importing the library is enough to trigger this conflict with subsequent PyTorch operations.
Environment and Context
-
Hardware:
- GPU: NVIDIA RTX PRO 6000 Blackwell Workstation Edition (94.5 GB VRAM)
-
Operating System:
- Windows 11
-
SDK and Software Versions:
- Python Version:
3.12.11 - Compiler: MSVC / Visual Studio
2022used to compile for CUDA - CUDA Toolkit: 12.8 (locally compiled)
- NVIDIA Driver Version:
580.97 - Host Application: ComfyUI (Release
2025-08-27or newer)
- Python Version:
-
Key Python Packages:
- llama-cpp-python:
0.3.16 - torch:
2.8.0+cu128 - diskcache: 5.6.3
- llama-cpp-python:
Failure Information (for bugs)
This report details a deep-seated conflict between llama-cpp-python and torch regarding CUDA context and/or VRAM management.
Investigation Summary & Key Findings
We conducted an extensive, multi-day investigation to isolate this issue, and the results were conclusive:
-
The problem is triggered by the
importofllama-cpp-python. Any host application that loads this library at startup will cause subsequent, unrelatedtorchVRAM allocations to become unstable. Disabling the components that importllama-cpp-pythoncompletely resolves the issue. -
The
diskcachedependency is a Red Herring. We initially suspected thediskcachelibrary was the cause. To test this, we performed a definitive experiment: we hid the realdiskcachelibrary and replaced it with a completely inert, non-functional fake Python package that only contained an emptyCacheclass.- Result: The VRAM instability problem persisted even with the fake library.
- Log Confirmation: The application log confirmed our fake library was being used:
--- 警告:正在使用一個偽造的 (FAKE) diskcache 函式庫 --- Set up system temp directory: C:\Users\Win\AppData\Local\Temp\latentsync_16880d62 - Conclusion: This proves that
diskcache's code is not the active cause. The problem isllama-cpp-pythonitself. The initial correlation was due todiskcachebeing a mandatory dependency; uninstalling it simply preventedllama-cpp-pythonfrom being imported successfully.
Final Hypothesis
The evidence overwhelmingly suggests a low-level conflict between how llama-cpp-python (especially a version compiled against a modern CUDA 12.8 toolkit) initializes its CUDA context and how PyTorch expects to manage the VRAM. The initialization of llama-cpp-python appears to "pollute" or "fragment" the VRAM state, making subsequent large-scale allocations by PyTorch highly inefficient.
Steps to Reproduce
- Set up a ComfyUI environment with PyTorch and the system components listed above.
- Compile and install
llama-cpp-pythonwith CUDA 12.8 support. - Install any ComfyUI custom node that imports
llama-cpp-pythonat startup (e.g.,ComfyUI-JoyCaptionorComfyUI-Searge-LLM-Node). - Launch ComfyUI. The server will start, and
llama-cpp-pythonwill be imported into the process. - Load a standard workflow designed to load a large PyTorch-based checkpoint model (e.g., an SDXL checkpoint, >10GB).
- Observe the "Dedicated GPU Memory" graph in Windows Task Manager. You will see the erratic, spiky allocation pattern instead of a smooth ramp-up.
Note on llama.cpp comparison: This issue is not about the performance or functionality of llama-cpp-python's inference, but rather a resource conflict it creates with another major library (torch). Therefore, comparing behavior against llama.cpp's ./main executable is not applicable, as the issue requires both runtimes to be active in the same Python process.
- Langage dominant
- Python
- Étoiles
- 10.7k
- Forks
- 1.5k
- Merge moyen
- 1 h 40 min
- PR mergées (30 j)
- 5
Préparer son environnement
- Aucun Dockerfile ni fichier Docker Compose
- Aucun modèle de pull request
- Lire le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Autres issues de abetlen/llama-cpp-python
-
Difficulté 2/5 1-3 heures Accessibilité débutants 78/100
abetlen/llama-cpp-python#2424 ·
Les mainteneurs répondent en général sous 1 jour
-
Seven llama_sampler_init_* bindings admit keyword arguments that the ctypes function object silently dropsPeut-être pris @Belal0066 l’a pris il y a 59 jours. Ouverte
Difficulté 2/5 1-3 heures Accessibilité débutants 88/100
abetlen/llama-cpp-python#2371 ·
Les mainteneurs répondent en général sous 1 jour
-
uv add llama-cpp-python wheels fails for versions above 0.3.30Peut-être pris Une pull request liée à cette issue est ouverte ou déjà fusionnée. Ouverte
Difficulté 2/5 1-3 heures Accessibilité débutants 65/100
abetlen/llama-cpp-python#2352 · 1 commentaire · 3 réactions ·
Les mainteneurs répondent en général sous 1 jour
-
Difficulté 2/5 1-3 heures Accessibilité débutants 75/100
abetlen/llama-cpp-python#2314 ·
Les mainteneurs répondent en général sous 1 jour
-
Llama.embed() calls LlamaBatch.add_sequence with old 3-arg signature; missing logits_arrayPeut-être à nouveau libre @lxcxjxhx l’a pris il y a 136 jours, et aucune pull request n’est ouverte. Ouverte
Difficulté 2/5 1-3 heures Accessibilité débutants 65/100
abetlen/llama-cpp-python#2211 · 2 commentaires ·
Les mainteneurs répondent en général sous 1 jour
Toutes les issues de abetlen/llama-cpp-python
Issues similaires
-
Bug IronPython Python 3
Difficulté 2/5 1-3 heures Accessibilité débutants 72/100
pyrevitlabs/pyRevit#4184 ·
Les mainteneurs répondent en général sous 1 jour
-
CLAW (Agents & Orchestration)Ouverte
Difficulté 1/5 Moins d'une heure Accessibilité débutants 74/100
jqueryscript/awesome-claude-code#1009 ·
-
tests
Difficulté 2/5 1-3 heures Accessibilité débutants 66/100
Les mainteneurs répondent en général sous 3 jours
-
Difficulté 2/5 1-3 heures Accessibilité débutants 72/100
Les mainteneurs répondent en général sous 1 jour
-
[Plugin] 群聊记忆 · 人格养成Ouverte
Difficulté 1/5 1-3 heures Accessibilité débutants 74/100