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.

The top 3

  1. On-Demand Paging: The Lazy Loader's Secret: Memory mapping's primary RAM-saving trick is loading model weights into physical memory only when specific pages are accessed, not the entire file at once. This significantly reduces initial RAM footprint for large LLMs.
  2. Zero-Copy Loading: Bypassing RAM Duplication: By mapping files directly into a process's address space, memory mapping eliminates the need to copy model data from disk to an intermediate buffer in RAM. This 'zero-copy' approach saves significant memory and CPU cycles.
  3. OS Page Cache Leveraging: Smart Memory Reuse: Memory mapping allows the operating system to efficiently manage model data via its page cache. If physical RAM is needed elsewhere, unmodified model pages can be discarded without writing back to disk, as the master copy remains on disk, ensuring optimal RAM utilization.

Sources

Open the full topic