Memory Mapping Magic: Loading Gigantic LLMs Faster
By using `mmap()` instead of standard C++ I/O, llama.cpp achieved 100x faster model loading and halved memory usage, allowing larger models to run directly from disk and enabling multiple LLM processes simultaneously.
The top 3
- Top 3 OS Memory Mapping APIs for LLMs: Operating systems employ specific APIs like `mmap()` on Unix-like systems (Linux, macOS) and `CreateFileMapping()` / `MapViewOfFile()` on Windows to map LLM weights directly into virtual memory. This bypasses traditional file I/O buffers and system calls, enabling significantly faster model loading by providing direct memory access to disk-resident data.
- Memory Mapping's Top 3 RAM-Saving Tricks: Memory mapping significantly boosts RAM efficiency for large LLMs through: 1) Lazy loading, where only the actively used parts of a model are loaded into physical RAM, reducing initial memory footprint. 2) Shared memory (e.g., `MAP_SHARED`), allowing multiple processes to access the same model data from a single copy in RAM, crucial for running multiple LLM instances. 3) On-demand paging, where the operating system can seamlessly move model data between disk and RAM, enabling models larger than physical memory to run.
- Memory Mapping's Top 3 Speed Advantages: Memory mapping dramatically accelerates LLM inference by: 1) Eliminating redundant data copies between kernel and user space, which reduces processing overhead. 2) Providing significantly faster model loading, with projects like `llama.cpp` reporting up to 100x speed improvements compared to traditional C++ I/O. 3) Reducing context switching and enabling more efficient CPU utilization by allowing applications to interact with file contents directly in memory.
Sources
Open the full topic