JOUNES // REPORTS
Home  ·  Projects  ·  Essays  ·  GitHub  ·  LinkedIn  ·  Email
// // RESEARCH REPORT

Weight-Context-Switch Primitive for Local LLM Memory Management

·10 citations

Overview

This research analyzes the failure of traditional operating system (OS) process-state management when applied to the gigabyte-scale memory footprints of local LLMs. Traditional OS paging is designed for general-purpose workloads, but LLM inference is dominated by the Key-Value (KV) cache—a dynamic state that grows linearly with sequence length and batch size [C000, C003]. For a model like Llama-3-70B in FP16, the KV cache consumes approximately 160 KB per token [C009]. In long-context deployments (100K to 10M tokens), this cache becomes the primary driver of GPU-hour waste and system instability [C004].

The objective is to implement a "Weight-Context-Switch" primitive. This primitive treats model weights and KV caches as a single, kernel-swappable resource on Unified Memory Architecture (uma) hardware, such as NVIDIA Grace Hopper, which utilizes a 900 GB/s NVLink-C2C interconnect to create a shared address space between CPU and GPU [C008]. This approach addresses the structural failures of current memory orchestration:

Approach Mechanism Primary Failure/Trade-off
Naive Allocation Pre-allocates for max_sequence_length Massive memory fragmentation; locks memory even for short requests [C009].
pagedattention Pages KV blocks on demand (like OS RAM) Introduces a "virtual memory tax" via non-contiguous layouts and pointer-chasing [C007].
vattention Decouples virtual and physical allocation Reduces programming overhead but still relies on CUDA VM APIs [C007].
Traditional Swapping Moves KV cache from HBM to DDR Causes significant context-switching latency and potential process termination by the Low Memory Killer (LMK) on edge devices [C002, C004].

This shift is critical because the "memory wall" has moved from static model weights—which are now mitigated by quantization [C001]—to the dynamic KV cache [C000]. Without a first-principles primitive for switching these resources, the system remains trapped between the binary choice of maximizing "intelligence" (larger context windows) and maintaining system availability (preventing OOM crashes) [C002, C006].

Landscape

Current approaches to LLM memory orchestration focus on mitigating the "memory wall" created by the Key-Value (KV) cache, which grows linearly with sequence length and batch size [C004]. Because of the high memory requirements for models like Llama-3-70B [C008], the industry has split into three primary architectural directions: virtualized paging, unified memory hardware, and stateful system services.

Virtualized Memory Management

The dominant software approach, led by vLLM, utilizes pagedattention to treat GPU memory like operating system RAM [C000]. By paging KV cache blocks on demand, it eliminates the fragmentation caused by pre-allocating contiguous memory for the maximum sequence length [C009]. However, this introduces a "virtual memory tax" because the KV cache becomes non-contiguous in both physical and virtual memory, increasing pointer-chasing overhead [C007]. To resolve this, vAttention decouples virtual and physical allocation using CUDA APIs, maintaining virtual contiguity to allow the use of standard attention kernels while still preventing physical fragmentation [C007].

Unified Memory and hardware Acceleration

hardware-level solutions aim to eliminate the latency of explicit data transfers between CPU and GPU. NVIDIA Grace Hopper and Grace Blackwell architectures use NVLink C2C (900 GB/s) to create a single unified memory address space [C008]. This allows the GPU to access CPU memory directly for KV cache offloading without redundant copies, bypassing the bottleneck of PCIe Gen 5 [C008]. Parallel to this, heterogeneous prototypes like TFLOP utilize CPU-FPGA systems with 4-bit product quantization on both weights and KV caches to reduce the memory footprint during the decoding stage [C001].

Stateful Context Switching

For edge and mobile deployments, the challenge shifts from throughput to system availability. The LLMS framework treats LLMs as a stateful system service, decoupling app and LLM contexts to prevent the Android Low Memory Killer (LMK) from terminating processes [C002]. It implements Tolerance-Aware Compression and IO-Recompute Pipelined Loading to reduce context switching latency by up to two orders of magnitude [C002].

Comparative Memory Orchestration Strategies

Approach Memory Layout Primary Bottleneck Key Benefit Source
pagedattention non-contiguous (Phys/Virt) Pointer-chasing / TLB misses Near-zero memory waste [C000, C009]
vattention non-contiguous (Phys) / Contiguous (Virt) CUDA API overhead Kernel compatibility; 1.23x throughput [C007]
uma (NVLink C2C) Unified Address Space hardware cost/availability Zero-copy offloading [C008]
LLMS (Mobile) Chunk-wise compressed I/O bandwidth Reduced context-switch latency [C002]

The shift toward uma hardware and stateful swapping suggests that treating the KV cache as a kernel-swappable resource—rather than a transient application buffer—is required to support gigabyte-scale local footprints without triggering system-level crashes [C002, C004].

Key Findings

The primary driver of system instability and GPU-hour waste in LLM serving has shifted from model weight fitting to KV-cache dynamics [C000, C004]. At scale, this renders traditional OS process-state management ineffective, as KV cache overflows trigger significant context-switching latency when swapping from HBM to DDR [C004].

Research reveals a critical tension between memory flexibility and kernel throughput. While PagedAttention eliminates fragmentation, it forces a non-contiguous virtual memory layout that introduces a "virtual memory tax" through pointer-chasing and TLB bottlenecks [C005, C007]. In contrast, vAttention leverages CUDA virtual memory management APIs to maintain virtual memory contiguity, resulting in up to a 1.23x throughput increase over PagedAttention-based kernels [C007].

On mobile devices, where the Android Low Memory Killer (LMK) presents a binary failure mode, LLMS mitigates switching overhead by treating the KV cache as a stateful system service [C002]. It employs Tolerance-Aware Compression and an LCTRU (Least Compression-Tolerable and Recently-Used) queue to prioritize which cache chunks to evict or compress, reducing latency by two orders of magnitude compared to baseline solutions [C002].

At the hardware level, the "Weight-Context-Switch" primitive is most viable on Unified Memory Architecture (uma) hardware. The NVIDIA Grace Hopper and Blackwell architectures utilize NVLink C2C to provide a 900 GB/s memory-coherent interconnect [C008]. This allows the GPU to access CPU memory without explicit data transfers or redundant copies, effectively treating the system's total memory as a single pool for both weights and KV caches [C008].

Finally, evidence suggests that "worst-case" provisioning for context length leads to 4-8$\times$ throughput waste because 80-95% of production requests are short [C006]. Implementing dual-pool token-budget routing—partitioning fleets into specialized short-context and long-context pools—reduces GPU-hour consumption by 31-42% [C006].

Tensions and Tradeoffs

Practitioners implementing local LLM memory orchestration face a primary conflict between maximizing the available memory budget for the KV-cache and maintaining raw kernel execution throughput. This creates several critical architectural tensions:

Memory Flexibility vs. Kernel Throughput
The industry standard, PagedAttention, increases batch sizes by treating GPU memory like OS RAM [C000], but introduces the aforementioned "virtual memory tax" [C007]. Alternative approaches like vAttention attempt to retain virtual contiguity to support standard attention kernels out-of-the-box, improving throughput [C007].

Context Intelligence vs. System Availability
On mobile devices, there is a binary tradeoff between the "intelligence" of a model (determined by its context window) and system stability [C002]. Increasing the KV cache to support longer reasoning chains raises the probability that the Android Low Memory Killer (LMK) will terminate the process [C002]. This necessitates fine-grained, chunk-wise compression and swapping to reduce context-switching latency [C002].

Offloading Latency vs. Memory Capacity
When KV cache requirements exceed GPU HBM, practitioners must offload data to DDR or SSDs [C004]. While this prevents out-of-Memory (OOM) crashes, it introduces significant context-switching latency [C004]. Unified Memory Architecture (uma) hardware, such as NVIDIA Grace Hopper, attempts to resolve this via NVLink C2C, providing 900 GB/s of memory-coherent bandwidth to treat CPU memory as a seamless extension of GPU memory [C008].

Worst-Case Provisioning vs. Resource Efficiency
Provisioning fleets for worst-case context lengths often leads to 4-8$\times$ throughput waste because the majority of actual requests are short [C006]. This mismatch triggers reliability issues, including preemption and request rejections, forcing a tradeoff between a simplified "one-size-fits-all" configuration and the complexity of dual-pool routing based on estimated token budgets [C006].

Opportunities

Priority Build Targets

To implement a first-principles 'Weight-Context-Switch' primitive, development should focus on three architectural components:

  1. uma-Aware state Orchestrator: Build a kernel-level manager for NVIDIA Grace Hopper or Blackwell architectures that leverages the 900 GB/s NVLink C2C memory-coherent interconnect to treat CPU and GPU memory as a single address space [C008]. This eliminates explicit data transfers and redundant copies when swapping KV caches between HBM and DDR [C008].
  2. Hybrid Virtual Memory Layer: Integrate vAttention's approach of decoupling virtual and physical memory allocation using CUDA APIs [C007] with TLB subregion contiguity [C005]. This would allow the system to retain virtual contiguity for the KV cache—improving throughput [C007]—while reducing TLB miss stalls by coalescing up to 512 pages into a single entry [C005].
  3. Tolerance-Aware Eviction Engine: Implement a kernel-level LCTRU (Least Compression-Tolerable and Recently-Used) queue [C002]. This engine should prioritize swapping or compressing KV cache chunks based on measured accuracy tolerance, reducing context-switching latency by up to two orders of magnitude on memory-constrained devices [C002].

Critical Research Questions

The transition to a unified swappable resource requires empirical answers to the following:

Memory Management Trade-offs

Approach Primary Benefit System Cost Key Bottleneck
pagedattention [C000] Near-zero memory waste Virtual memory tax Pointer-chasing/TLB misses
vattention [C007] Virtual contiguity CUDA API complexity Physical fragmentation
uma Offloading [C008] Massive capacity (DDR) hardware lock-in (C2C) Interconnect bandwidth
Dual-Pool Routing [C006] 31-42% lower GPU-hours Dispatch overhead Configuration-traffic mismatch

References

Provenance: Published 2026-05-11 · 10 inline citations · 10 references
// GENERATED FROM A LIVE OBSIDIAN VAULT · CLOUDFLARE PAGES · DRAFTED WITH AGENTS
← back to Reports