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.

The top 3

  1. mmap's Zero-Copy Advantage for LLM Weights: The POSIX `mmap()` API significantly boosts LLM inference by mapping model weights directly into memory, enabling a 'zero-copy' mechanism that bypasses traditional I/O buffers and allows multiple processes to share read-only weights efficiently, as demonstrated by `llama.cpp`'s 100x faster loading for LLaMA models.
  2. MapViewOfFile: Windows' High-Performance LLM I/O: `MapViewOfFile()` serves as the Windows equivalent to `mmap()`, directly mapping file contents into a process's virtual address space. This enables lazy loading of LLM data on demand through page faults, reducing initial memory consumption and accelerating access by avoiding explicit read/write calls and extra buffer copies.
  3. NtMapViewOfSection: Advanced Windows LLM Memory Control: For advanced scenarios on Windows, `NtMapViewOfSection` provides lower-level memory mapping capabilities, offering more granular control than `MapViewOfFile`. It allows reserving virtual address ranges larger than the current file and dynamically extending file mappings via `NtExtendSection`, critical for managing extremely large or growing LLM models.

Sources

Open the full topic