Repository navigation
Description
Prerequisites
Please answer the following questions for yourself before submitting an issue.
- I am running the latest code. Development is very rapid so there are no tagged versions as of now.
- 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 or useful enhancement to share.
Expected Behavior
Please provide a detailed written description of what you were trying to do, and what you expected llama-cpp-python to do.
def generate_text(model_id:str, prompt:str) -> str:
llm = LlamaCpp(model_path=f'./weights/{model_id}.bin', n_gpu_layers=40)
output = llm(prompt)
return outputAfter calling this function, the llm object still occupies memory on the GPU.
Current Behavior
Please provide a detailed written description of what llama-cpp-python did, instead.
The llm object should clean up after itself and clear GPU memory. The GPU memory is only released after terminating the python process.
Environment and Context
Please provide detailed information about your computer setup. This is important in case the issue is not reproducible except for under certain specific conditions.
- Physical (or virtual) hardware you are using, e.g. for Linux:
Architecture: x86_64
CPU op-mode(s): 32-bit, 64-bit
Address sizes: 48 bits physical, 48 bits virtual
Byte Order: Little Endian
CPU(s): 16
On-line CPU(s) list: 0-15
Vendor ID: AuthenticAMD
Model name: AMD Ryzen 7 5800X 8-Core Processor
CPU family: 25
Model: 33
Thread(s) per core: 2
Core(s) per socket: 8
Socket(s): 1
Stepping: 2
Frequency boost: enabled
CPU(s) scaling MHz: 58%
CPU max MHz: 4850.1948
CPU min MHz: 2200.0000
BogoMIPS: 7602.85
Flags: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx mmxext fxsr_opt pdpe1gb rdtscp lm
constant_tsc rep_good nopl nonstop_tsc cpuid extd_apicid aperfmperf rapl pni pclmulqdq monitor ssse3 fma cx16 sse4_1 sse4_2 x2apic movbe popcnt ae
s xsave avx f16c rdrand lahf_lm cmp_legacy svm extapic cr8_legacy abm sse4a misalignsse 3dnowprefetch osvw ibs skinit wdt tce topoext perfctr_core
perfctr_nb bpext perfctr_llc mwaitx cpb cat_l3 cdp_l3 hw_pstate ssbd mba ibrs ibpb stibp vmmcall fsgsbase bmi1 avx2 smep bmi2 erms invpcid cqm rd
t_a rdseed adx smap clflushopt clwb sha_ni xsaveopt xsavec xgetbv1 xsaves cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local clzero irperf xsaveerp
tr rdpru wbnoinvd arat npt lbrv svm_lock nrip_save tsc_scale vmcb_clean flushbyasid decodeassists pausefilter pfthreshold avic v_vmsave_vmload vgi
f v_spec_ctrl umip pku ospke vaes vpclmulqdq rdpid overflow_recov succor smca fsrm
Virtualization features:
Virtualization: AMD-V
Caches (sum of all):
L1d: 256 KiB (8 instances)
L1i: 256 KiB (8 instances)
L2: 4 MiB (8 instances)
L3: 32 MiB (1 instance)
NUMA:
NUMA node(s): 1
NUMA node0 CPU(s): 0-15
Vulnerabilities:
Itlb multihit: Not affected
L1tf: Not affected
Mds: Not affected
Meltdown: Not affected
Mmio stale data: Not affected
Retbleed: Not affected
Spec store bypass: Mitigation; Speculative Store Bypass disabled via prctl
Spectre v1: Mitigation; usercopy/swapgs barriers and __user pointer sanitization
Spectre v2: Mitigation; Retpolines, IBPB conditional, IBRS_FW, STIBP always-on, RSB filling, PBRSB-eIBRS Not affected
Srbds: Not affected
Tsx async abort: Not affected
- Operating System, e.g. for Linux:
Linux name 6.2.13-arch1-1 #1 SMP PREEMPT_DYNAMIC Wed, 26 Apr 2023 20:50:14 +0000 x86_64 GNU/Linux
Activity
iactix commentedon May 21, 2023
I am encountering this too. Windows, CuBLAS, AMD CPU, RTX 1080. When the llm model is destroyed you get the RAM back but the VRAM stays occupied until the whole python script using it quits. That means you get out of memory doing a second inference this way. Which makes GPU acceleration unusable for me, currently. Kind of a big deal I think.
gjmulder commentedon May 21, 2023
Is this a bug then in how
llama-cpp-pythonis managing the shared liblibllamaor a bug withinlibllama?I suspect the latter, which means a bug needs to be logged with llama.cpp that reproduces the issue as simply as possible.
Does someone have a few lines of
pythonto reproduce the problem? Ideally with informative output ofnvidia-smibefore, during and after the bug occurrence.EDIT: Sorry, I see that the OP did provide some code. What happens if you do a:
del llmbefore returning? My python-fu isn't that strong, but I suspect you need to explicitly destroy the object to destroy the reference to the code running on the GPU. Of course this looks horribly inefficient as every time the method is being called the model needs to be reloaded on the GPU.
nidhishs commentedon May 21, 2023
I’ll make a minimal example and update you.
iactix commentedon May 21, 2023
By now I am doing
Changes nothing. RAM goes down, VRAM keeps up.
ggml-org/llama.cpp#1456
nidhishs commentedon May 21, 2023
This snippet should do the job:
Output:
Interestingly RAM usage also doesn't go down until process is terminated? Perhaps I'm missing something.
gjmulder commentedon May 21, 2023
Good repro! I patched it to use
$MODEL.You probably want to append to the bug llama.cpp/issues/1456, but they may ask which
llama.cppcommit, which is non-obvious from the:$ pip install --force-reinstall --ignore-installed --no-cache-dir --verbose llama-cpp-pythonnidhishs commentedon May 25, 2023
oobabooga/text-generation-webui/#2087 was able to fix the RAM not being released. Have we already integrated this change?
iactix commentedon May 26, 2023
It seems to me they only fixed the RAM not VRAM? RAM was already freed when I tried this. Anyway it clearly seems to be a llama.cpp problem and I don't know how this can be open for a week or so. The fix must be a single line or something. Over there. As a workaround, I could imagine wrapping the model useage entirely into a thread and killing that after use could work to force it to free everything like it does when the pyhton script exits, but I have not tested it. I doubt it would be a fix llama-cpp-python could actually implement though.
nidhishs commentedon May 26, 2023
Yeah they fixed the RAM issue. I have been following the thread on llama.cpp and seems like the author of the GPU implementation was able to fix it after being able to reproduce with my snippet. Not sure when the fix will be pushed though.
iactix commentedon May 26, 2023
But it is a VRAM issue.
nidhishs commentedon May 26, 2023
Yes, the author was able to clean up VRAM. Check the thread issue #1456.
iactix commentedon May 26, 2023
Is the last thing I see there
Edit: It is possible I misinterpreted the most recent comment there, I don't know what they tested. The guy above indeed says the issue is fixed in his branch. Hopefully there will just be a fix in llama.cpp soon?
9 remaining items
JohannesGaessler commentedon Jun 11, 2023
It doesn't have anything to do with what NVIDIA did, it's a consequence of the llama.cpp code. There is a global memory buffer pool and a global scratch buffer that are not tied to a specific model.
iactix commentedon Jun 11, 2023
That's great, makes it solveable. May I suggest a "cleanup" call in the API or something.
JohannesGaessler commentedon Jun 11, 2023
Right now using multiple models in the same process won't work correctly anyways. I'll include a fix that just frees the buffers upon model deletion the next time I make a PR.
iactix commentedon Jun 11, 2023
Thank you, sounds great! <3
iactix commentedon Jun 30, 2023
Still basically a memory leak issue for 1 1/2 months now.
eugen-ajechiloae-clearml commentedon Jul 12, 2023
Hey guys, in case you have CPU memory issues, check out this issue ggml-org/llama.cpp#2145.
As a temporary workaround until this is fixed officially, you could use this fork https://github.com/eugen-ajechiloae-clearml/llama.cpp:
iactix commentedon Jul 19, 2023
It is a GPU memory issue. VRAM rises just importing llama-cpp-python. It is not a lot but in my book that's a no-go already. Then when I load a model with BLAS (cuda) and a few layers and do inference, VRAM goes to 5GB. Fine. Then I delete/unload the model, goes down to 2.5GB VRAM usage. Terminate the python process, goes down to 1.1 GB VRAM usage. When that should not make anything go down after the model was already deleted. Does it really take 2 months to add some function that frees some kind of sloppy shared resources? By now I am getting the impression this is deemed perfectly cool behavior by llama.cpp and llama-cpp-python can't do anything about it due to how some bindings-magic works probably.
JohannesGaessler commentedon Jul 19, 2023
That is 100% a llama-cpp-python issue, most likely from the eager initialization of the CUDA backend.
That is very likely due to the buffer pool for prompt processing, see ggml-org/llama.cpp#1935 , will be fixed by ggml-org/llama.cpp#2160 .
I'm doing this as a hobby and I don't particularly care about the use cases of other people. I personally only use llama.cpp from the command line or the native server. So I'm not going to spend my time on a temporary fix that manages the deallocation of the buffer pool when the proper fix would be to implement kernels that don't need temporary buffers in the first place. If someone else does care they can make a PR for it.
iactix commentedon Jul 19, 2023
I mean if there is a global buffer pool, is it even sloppy to give it like a "flush" function that llama-cpp-python could call?
JohannesGaessler commentedon Jul 19, 2023
Feel free to implement it. As I said, I'm not going to spend my time on a temporary solution.
iactix commentedon Jul 19, 2023
Yeah I understand that completely. I didn't mean to sound ungrateful either. And I know I can't demand anything if I'm not going to do it myself and all that. But there's also people being in better positions to do it with less effort, and I guess I just don't understand why such a well managed project doesn't just kind of priority fix something like that. I mean I know I can't technically call it a leak on llama's end. But apparently the bindings can't fix this either and in combination it's just pretty much a gpu memory leak. Idk. Also thanks for your gpu inference, it's pretty cool.
JohannesGaessler commentedon Jul 19, 2023
It's just a matter of manpower. I just work on the things that I want for myself when I feel like it and the only significant CUDA infrastructure contributor other than me is slaren.
iactix commentedon Jul 19, 2023
I see. I mean I have never made a pull request in my life but maybe I will actually look into it.
JohannesGaessler commentedon Jul 19, 2023
If you do you should invoke clearing the buffer pool at the same time that the VRAM scratch buffer gets deallocated.
iactix commentedon Jul 22, 2023
I am sorry to report that I did in fact opt to not go for a temporary fix, since who knows what the next tool I use decides to keep in global buffer. So I wrapped all my llama-cpp-python stuff in a process wrapper. Here is the code in case a second person wants to use the vram for stable diffusion or something:
I guess it should work for streaming updates too once that works correctly.