When n_gpu_layers=-1, the entire model file stays memory-mapped in RAM
(via mmap) even after all weights are copied to VRAM. This causes
unexpectedly high host RAM usage that is not released until the process
exits.
This fix automatically disables mmap when all layers are offloaded to
GPU and GPU offload is supported. With mmap disabled, llama.cpp uses a
temporary read buffer that is freed after GPU upload, significantly
reducing host RAM consumption.
The behavior can be overridden by explicitly passing use_mmap=True.
Summary
Fixes #1964 — When n_gpu_layers=-1, host RAM used for model loading is not released.
Problem
When use_mmap=True (the default), the entire model file is memory-mapped into the process address space. Even after all layer weights are copied to VRAM via n_gpu_layers=-1, the mmap'd pages remain resident in the OS page cache and show up as consumed RAM in Task Manager / htop. This memory is not released until the Python process exits.
Fix
In Llama.__init__, when all three conditions are met:
…mmap is automatically disabled. With mmap off, llama.cpp reads weights via a temporary buffer that is freed after GPU upload, so host RAM drops back down after loading.
A verbose stderr message is printed when this auto-disable kicks in. Users can override with use_mmap=True explicitly.
Changes
Testing
All 6 tests pass. The tests use mocked native libraries so they don't require a compiled llama.cpp binary.