TL;DR
FlexInfer is an optimized offloading framework designed to run Large Language Models (LLMs) efficiently on resource-constrained edge devices. By combining asynchronous prefetching, balanced memory locking, and a heuristic tensor preservation strategy, it mitigates I/O bottlenecks and dynamically adapts to varying memory budgets. The framework achieves up to a 12.5× inference throughput speedup compared to standard offloading methods like mmap.
What Problem Does This Paper Address?
Deploying LLMs directly on edge devices (smartphones, laptops, embedded systems) is highly desirable for privacy, lower latency, and offline customization. However, the massive memory footprint of these models frequently exceeds local hardware capacities.
While techniques like quantization, pruning, and distillation help reduce model size, they often degrade model accuracy and lack the flexibility to adapt to changing memory constraints (e.g., when a user opens another memory-heavy application). Alternatively, offloading frameworks store model weights on slower external storage (like SSDs) and fetch them on demand. Unfortunately, standard offloading mechanisms—such as the memory-mapped file (mmap) approach used by llama.cpp—suffer from severe I/O bottlenecks. They fail to fully utilize available memory and cause massive pipeline stalls because the CPU constantly waits for weights to load from storage.
Methods
FlexInfer introduces a highly coordinated three-part architecture to maximize memory efficiency and parallelize computation with I/O operations:
- Asynchronous Prefetching: Instead of loading weights synchronously right before computation, FlexInfer uses a tensor-based multi-threaded prefetching strategy. Multiple I/O threads load upcoming tensors into a sliding "prefetch window" while compute threads process the current layer. Memory is immediately freed after a tensor is used, keeping the active memory footprint minimal.
- Balanced Memory Locking: A naive way to use extra available memory is to lock the first few layers of a model into RAM. However, this creates an asymmetric workload: the CPU breezes through the locked layers but stalls on the offloaded ones, causing the I/O and compute threads to constantly wait for each other. FlexInfer solves this by splitting every layer uniformly into a fixed part (locked in memory) and a prefetched part, ensuring a stable, predictable I/O workload across the entire inference cycle.
- Flexible Tensor Preservation: LLM layers typically consist of Attention tensors and Feed-Forward Network (FFN) tensors, usually maintaining a predictable 1:3 size ratio. FlexInfer utilizes a heuristic algorithm to decide exactly which types of tensors to lock based on the user's specific memory budget. When memory is tight, it prioritizes locking smaller attention tensors to maximize the number of cached parameters; when memory is abundant, it prioritizes FFN tensors to minimize memory fragmentation.
Datasets
As this paper focuses purely on inference optimization rather than training, no new text datasets were introduced. Instead, the authors evaluated FlexInfer using the widely adopted Llama2 series models (including Llama2-7B, Llama2-13B, Codellama-34B, and Llama2-70B), all quantized to 4-bit precision.
Key Insights and Ideas
- The Pitfall of Asymmetry: The authors discovered that traditional layer-based caching strategies actually break pipeline parallelism. Uniformly distributing the memory budget across all layers rather than completely caching a subset of layers is the key to keeping I/O and compute threads perfectly synchronized.
- Exploiting Structural Predictability: Rather than relying on complex, heavy runtime profiling to determine which weights to cache, FlexInfer leverages the highly static and predictable structural ratio (1:3) between Attention and FFN blocks inherent to the Transformer architecture. This allows a lightweight heuristic algorithm to make optimal caching decisions instantaneously.
Key Results
The authors evaluated FlexInfer on an Ubuntu server configured to simulate highly resource-constrained hardware environments by restricting available memory and CPU cores.
- Throughput Speedups: FlexInfer consistently outperformed the standard
mmapbaseline across all tested models and memory limits, delivering 5.2× to 12.5× speedups for Llama2-7B and 5× to 11× speedups for the massive Llama2-70B model. - Ablation Performance: Replacing standard
mmapwith multi-threaded large reads alone improved performance by 2.6× to 3×. Adding asynchronous prefetching boosted throughput by an additional 34.8% to 59.4% in tight memory scenarios, and up to 118.8% as available memory scaled up. - Balanced Locking Gains: Compared to an unbalanced layer-locking strategy, FlexInfer's balanced memory locking yielded up to an 83.3% performance improvement in higher-memory environments.
Limitations and Future Work
The primary limitation acknowledged in the paper is its strict focus on CPU-based inference. While this is highly relevant for low-power edge devices and smartphones that lack dedicated, high-capacity GPUs, many modern consumer devices feature heterogeneous compute architectures (CPUs, GPUs, and NPUs). The authors note that orthogonal optimization methods like speculative decoding or alternative model compression schemes could be combined with FlexInfer in the future. Extending this balanced offloading paradigm to hybrid CPU-GPU-NPU setups remains an open and promising direction.
Summary updated