MMAP Files: Llama.cpp's Zero-Copy Loading Advantage

Llama.cpp leverages memory-mapped files (mmap) to enable instant model loading and efficient memory access by letting the operating system manage which parts of the model are in RAM. This avoids full model copies, reduces initial memory overhead, and was shown to load LLaMA models 100x faster using half the memory in early development.

The top 3

  1. Llama.cpp's 100x Faster Load Times with MMAP: Switching to MMAP files allowed llama.cpp to load large language models up to 100 times faster compared to traditional C++ standard I/O, by eliminating redundant data copies between kernel and user space and reducing system call overhead.
  2. MMAP's Zero-Copy: Bypassing Kernel Buffers for Speed: Memory mapping (mmap) achieves 'zero-copy' by directly mapping file content into a program's virtual address space, eliminating redundant data transfers. Unlike traditional I/O (read/fread) that involves copying data from disk to kernel buffers then to user buffers, mmap allows direct access, significantly reducing CPU overhead and memory bandwidth consumption.
  3. Beyond LLMs: MMAP's Ubiquitous Role in High-Performance Apps: Memory mapping (mmap) is a critical OS feature widely adopted in diverse high-performance systems for efficient file access and inter-process communication. Key applications include databases (e.g., Apache Kafka, Elasticsearch, Lucene/Solr), web servers (e.g., Apache Spark), multimedia processing, and high-performance computing, where it optimizes handling of large datasets and shared memory.

Sources

Open the full topic