Inferensys

Glossary

Demand Paging

Demand paging is a virtual memory technique where data is transferred from a slower backing store into faster GPU memory only when the GPU attempts to access it, as signaled by a page fault.
Data scientist building training data pipeline on laptop, data preprocessing visible, technical workspace.
GPU MEMORY OPTIMIZATION

What is Demand Paging?

Demand paging is a core virtual memory technique for managing GPU memory capacity, directly impacting inference latency and cost.

Demand paging is a virtual memory management technique where data pages are transferred from a slower backing store—such as system RAM or NVMe storage—into faster GPU device memory only when a GPU kernel attempts to access them, as signaled by a page fault. This mechanism enables applications to operate on datasets larger than the physical GPU memory capacity by treating host memory and storage as an extension, swapping pages on-demand. It is a foundational component for memory overcommit and unified virtual memory (UVM) systems in accelerator computing.

The performance of demand paging hinges on minimizing page fault latency. When a fault occurs, execution stalls while the required page is migrated, directly increasing inference latency. Optimizations include prefetching algorithms that predict future accesses and huge pages to reduce fault frequency. In AI workloads, demand paging is critical for large model inference, enabling GPU memory oversubscription by keeping only active attention key-value (KV) cache pages resident while others reside in host memory, a trade-off between throughput and per-request latency.

GPU MEMORY OPTIMIZATION

Key Characteristics of Demand Paging

Demand paging is a foundational virtual memory technique that defers data transfer into GPU memory until the moment it is required for computation. This section details its core operational mechanisms and system-level impacts.

01

On-Demand Page Migration

The core mechanism where data pages are transferred from a backing store (system RAM or SSD) into GPU device memory only when a kernel attempts to access an address that is not currently resident. This access attempt triggers a GPU page fault, which halts the executing thread and initiates a page migration operation to fetch the required data before resuming execution. This lazy loading principle is what defines 'demand' paging.

02

Transparency to the Programmer

A key benefit is that demand paging operates transparently beneath the CUDA Unified Memory programming model. Developers can write code using single pointers (e.g., allocated with cudaMallocManaged) that are accessible from both CPU and GPU. The memory management unit (MMU) and driver handle all migration logic, freeing the programmer from manually staging data via cudaMemcpy. This simplifies code but requires an understanding of the performance implications of page faults.

03

Enables Memory Overcommit

Demand paging is the essential enabler for memory overcommit (oversubscription). It allows an application to allocate a total working set larger than the physical GPU memory (VRAM) capacity. The system relies on host memory and even storage (via technologies like GPU Direct Storage) as a slower, larger backing tier. Only the actively accessed 'hot' pages reside in fast GPU memory, while 'cold' pages remain in slower tiers, effectively creating a large virtual address space.

04

Performance Trade-off: Latency vs. Utilization

The technique introduces a fundamental trade-off:

  • Benefit: Maximizes GPU memory utilization by keeping only needed data resident, allowing more or larger models to run concurrently.
  • Cost: Page fault latency. Handling a fault involves significant overhead: interrupting execution, migrating data over the PCIe bus (or NVLink), and resuming. Excessive faulting can devastate throughput, turning a compute-bound kernel into a memory-bound or I/O-bound operation. Performance optimization focuses on minimizing faults through access pattern optimization and prefetching hints.
05

Integration with Unified Virtual Memory (UVM)

Demand paging is a runtime behavior of a Unified Virtual Memory (UVM) system. UVM establishes a single virtual address space across CPU and GPU. Demand paging is the dynamic data movement policy within that space. Other UVM features, like CPU-initiated page faults and memory prefetching APIs (cudaMemPrefetchAsync), work in concert with demand paging to optimize data locality and hide migration latency.

06

Critical for Large Model Inference

This characteristic is pivotal for deploying large language models (LLMs) where the model weights exceed GPU memory. With demand paging, weights can be staged in CPU RAM or NVMe storage. During inference, the attention mechanism generates a sequential access pattern across layers and the KV cache. While naive demand paging would cause severe stuttering, it is combined with predictive prefetching algorithms that proactively load the weights for the next layer or sequence position, overlapping compute with data migration to sustain high throughput.

MEMORY MANAGEMENT COMPARISON

Demand Paging vs. Traditional Swapping

A technical comparison of two virtual memory techniques for managing GPU memory capacity, highlighting their operational mechanisms, performance characteristics, and suitability for AI inference workloads.

Feature / MetricDemand PagingTraditional Swapping

Unit of Transfer

Memory Page (e.g., 4KB, 2MB, 1GB)

Entire Process Context

Transfer Trigger

GPU Page Fault on first access

System-wide memory pressure

Granularity

Fine-grained (page-level)

Coarse-grained (process-level)

Latency Impact

Per-access fault penalty (< 1 ms from host RAM, ~10-100 ms from SSD)

Process stall during full context swap (100s of ms to seconds)

GPU Utilization

High; compute continues on other warps/threads during page fetch

Low to zero; GPU is idle during swap-in/swap-out

Primary Use Case

Transparently oversubscribe GPU memory for large models

Relieve severe host memory pressure for multiple jobs

Implementation Complexity

High; requires OS, driver, and GPU MMU support (e.g., NVIDIA UVM)

Low; standard OS scheduler function

Optimal For

Inference with large, sparse memory access patterns

Multi-tenancy with independent, smaller workloads

GPU MEMORY OPTIMIZATION

Implementations and Frameworks

Demand paging is implemented through a combination of hardware support, operating system drivers, and runtime libraries. These frameworks manage the virtual address space, handle page faults, and orchestrate data movement between memory tiers.

04

Page-Locked Host Memory & DMA

A foundational technique that enables efficient demand paging by ensuring host memory is ready for rapid transfer.

  • Pinned (Page-Locked) Memory: Host memory allocated with cudaHostAlloc or hipHostMalloc is prevented from being paged to disk by the OS, guaranteeing a fixed physical address.
  • Direct Memory Access (DMA): The GPU's DMA engine can directly read from/write to this pinned memory without CPU intervention, which is essential for fast page migration during a fault.
  • Zero-Copy Fallback: In some implementations, the GPU can directly access pinned host memory (a 'zero-copy' transfer) as a temporary measure while the target page is being migrated to device memory.
06

User-Space Drivers & Runtime (ROCm, CUDA)

The software stack that orchestrates demand paging, sitting between the application and the kernel-mode driver.

  • Virtual Address Management: The runtime (e.g., libcuda.so, libhip.so) manages the GPU's page tables, mapping virtual addresses to physical pages in GPU memory, system memory, or indicating 'not present'.
  • Fault Service Routine: When the GPU MMU raises a page fault, the user-space driver's fault handler is invoked. It identifies the required page, schedules a DMA transfer via the kernel driver, and updates the page table.
  • Concurrency & Streams: Fault handling is designed to be asynchronous and non-blocking for other work on the GPU, allowing computation to continue on different streams while a fault is serviced.
GPU MEMORY OPTIMIZATION

Frequently Asked Questions

Demand paging is a core virtual memory technique for managing GPU memory capacity. These questions address its mechanisms, benefits, and role in modern AI infrastructure.

Demand paging is a virtual memory management technique where data is transferred from a slower backing store (like system RAM or SSD) into faster GPU device memory only when a GPU compute kernel attempts to access it, as triggered by a page fault. It works by extending the GPU's virtual address space beyond its physical memory limits. When a GPU thread accesses a virtual address whose corresponding physical page is not resident in GPU memory, a GPU page fault occurs. This fault is handled by the driver and hardware, which pauses the relevant work, migrates the required page from the host or storage, and then resumes execution transparently. This mechanism enables memory overcommit, allowing workloads to allocate more memory than the GPU physically has, with pages swapped in on-demand.

Prasad Kumkar

About the author

Prasad Kumkar

CEO & MD, Inference Systems

Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.

His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.