LLM in a flash: Efficient Large Language Model Inference with Limited Memory

Keivan Alizadeh, Iman Mirzadeh, Dmitry Belenko, Karen Khatamifard, Minsik Cho, Carlo C. del Mundo, Mohammad Rastegari, Mehrdad Farajtabar · DOI

TL;DR

This paper introduces a breakthrough method for running Large Language Models (LLMs) on devices with limited RAM by storing model parameters in flash memory and loading them "just-in-time." By leveraging activation sparsity and hardware-aware data bundling, the authors achieve inference speeds up to 25x faster than naive loading, enabling 7B-parameter models to run smoothly on devices with only half the required memory.

What Problem Does This Paper Address?

The "memory wall" is the biggest hurdle for on-device AI. A typical 7-billion parameter model requires about 14GB of DRAM just to load its weights in half-precision. Most consumer smartphones and laptops have significantly less available memory, making it impossible to run state-of-the-art LLMs locally without aggressive quantization.

While flash memory (SSD) is abundant, it is orders of magnitude slower than DRAM. A naive approach—reading weights from flash for every token generated—is painfully slow because the latency of fetching data dwarfs computation time. The challenge is making flash-to-DRAM transfers efficient enough for real-time interaction.

Hardware constraints and throughput characteristics of flash memory.

Methods

The authors propose an inference cost model that treats flash memory as primary storage and DRAM as a dynamic cache. Their approach rests on three pillars:

  1. Reducing Data Transfer via Sparsity: Most LLMs use ReLU activation functions, resulting in "sparse" internal states where over 90% of neurons in Feed-Forward Network (FFN) layers are inactive for a given input. The authors use a tiny low-rank predictor to guess active neurons and only load those specific weights.
  2. Sliding Window Technique: Instead of clearing memory constantly, they maintain a "sliding window" of weights in DRAM. Since consecutive tokens often activate similar neurons, they only incrementally load the difference between current requirements and the cache.
  3. Row-Column Bundling: Flash memory performs best with large, contiguous reads. To avoid throughput degradation from small reads, the authors bundle the $i$-th column of the "up-projection" matrix and the $i$-th row of the "down-projection" matrix together in flash, doubling the chunk size.

Key Insights and Ideas

  • The Throughput Paradox: It is often faster to read more data than needed if it allows for a larger, contiguous block read. This hardware-centric view makes "bundling" effective.
  • Predicting the Future: Using a low-rank model to predict activations saves massive amounts of data transfer with negligible overhead (less than 2.4% of total weights).
  • Sparsity is a Feature: By "relufying" models (replacing other activations with ReLU), the authors force higher sparsity to further accelerate flash-based inference.

Key Results

The results represent a significant leap over baseline "naive" loading methods:

Comparison of inference latency between naive loading and the proposed method across different hardware.
  • Speedups: On an Apple M1 Max, the system achieved a 4-5x speedup on CPU and a 20-25x speedup on GPU compared to naive loading.
  • Memory Efficiency: They successfully ran models twice the size of available DRAM. For example, an OPT 6.7B model ran on a system with only ~3.3GB of DRAM allocated for weights.
  • Latency: For OPT 6.7B on a GPU, I/O latency dropped from over 2000ms per token to just 30ms, enabling interactive use.
  • Accuracy: The use of predictors caused a negligible drop in zero-shot accuracy (often less than 1%), proving that 100% of weights are not needed 100% of the time.

Limitations and Future Work

  • Single-Batch Constraint: The implementation is optimized for single-sequence inference; multi-batch server use might negate sliding window benefits.
  • Power and Thermals: While sparse, the extended duration of inference means total energy per token is currently higher, and continuous high-speed flash reads can lead to thermal throttling.
  • Future Directions: Exploring more optimized weight bundling and specialized data structures to reduce DRAM management overhead.

Summary updated

BibTeX
@inproceedings{alizadeh2024llm,
  author = {Keivan Alizadeh and Iman Mirzadeh and Dmitry Belenko and Karen Khatamifard and Minsik Cho and Carlo C. del Mundo and Mohammad Rastegari and Mehrdad Farajtabar},
  title = {{LLM in a flash: Efficient Large Language Model Inference with Limited Memory}},
  booktitle = {Annual Meeting of the Association for Computational Linguistics},
  year = {2024},
  doi = {10.18653/v1/2024.acl-long.678},
}
Figure