r/deeplearning • u/Emergency-Tutor-7 • 2h ago
Offloading activations to pinned host memory that the GPU kernel can address directly
Activation offloading usually means copying tensors out of VRAM and back, and paying for the copy twice. The alternative most people try first is cudaMallocManaged, where the driver migrates pages on demand and residency is decided at fault time; that works until the access pattern makes it thrash and the GPU sits idle waiting on migrations you never scheduled.
vram-stream takes a third route. The workspace holds two regions, one in device memory and one in system RAM, and both are addressable from the same kernel: the RAM region is allocated with cuMemHostAlloc using CU_MEMHOSTALLOC_DEVICEMAP and mapped with cuMemHostGetDevicePointer_v2, after checking the device reports CAN_MAP_HOST_MEMORY. A kernel store to that region goes over PCIe or NVLink with link latency, with no page fault and no migration.
The trade is pinned memory instead of elastic memory. The driver and OS cap how much can be pinned, so this is a budget you set rather than an oversubscription. That constraint shapes the design: open_cuda_cache reads a contiguous buffer in fixed-size blocks and keeps one pinned buffer plus one VRAM buffer per slot, with copies and kernels in separate streams and CUDA events protecting the pinned buffer, so a block is resident in VRAM before the kernel that needs it starts and cache.prefetch(n) keeps the pipeline fed.
For training, the same mechanism serves the dataloader: prefetch_dataloader supplies cached batches, and optional integrations move model state and saved activations to RAM using FSDP2 or the Hugging Face Trainer with DeepSpeed. That matters for the write-once-read-once tensors that dominate activation memory, where a copy you can overlap is cheaper than residency you cannot afford.
Measurement is a command, not a claim: vram-stream benchmark --block-mib 16 --blocks 2 --slots 2 --passes 4 measures both CUDA memory paths on your card. The README does not quote throughput because PCIe generation changes the ratio more than any number would survive as a general statement.
Correctness check: vram-stream memory --device 0 --vram-mib 16 --ram-mib 16 allocates both regions, runs one kernel that combines values from VRAM and system RAM and writes results back to both, then verifies the first and last aligned 32-bit words of each region, with the PTX kernel compiled by the driver at run time.
Install is pip install vram-stream, or npm i -g vram-stream. What it does not do: it is not a managed-memory replacement, pinning fails past the lockable limit instead of degrading, remote traffic is bounded by the interconnect, and the workspace expects one CUDA context on the thread that opened it. MIT, 0.4.0: https://github.com/Furox-Art/vram-stream