[Technical Proposal] Engine-Level Direct Host Mapping to Prevent OS Swapping
- Dominant language
- Python
- Stars
- 133k
- Forks
- 15.7k
- Avg merge
- 1d 7h
- Merged PRs (30d)
- 158
Description
### Feature Idea
[Technical Proposal] Engine-Level Direct Host Mapping to Prevent OS Swapping
Summary: This proposal outlines a method to optimize memory handling by implementing direct CUDA host memory registration. The goal is to prevent the Operating System (specifically Windows) from offloading data to the pagefile (swap), ensuring that inference remains purely within the RAM/VRAM domain regardless of workload type or resolution.
The Problem:
Standard memory allocation in deep learning frameworks often fails to prevent the OS from utilizing the disk-based pagefile when video memory limits are approached. This introduces significant I/O Latency and system instability, as disk access speeds are orders of magnitude slower than PCIe/RAM access. This bottleneck affects all modalities, including image, video, and audio generation.
Technical Implementation:
The proposed optimization utilizes cudaHostRegister with the cudaHostRegisterMapped flag to establish a zero-copy access protocol.
Direct Mapping: By registering host memory directly for the GPU, data is accessed via the PCIe bus, eliminating unnecessary CPU-side memory copies.
Swap Prevention: Using flags = 3 during memory registration forces the OS to keep the allocated pages in physical RAM, effectively bypassing the system swap mechanism.
Architecture Agnostic: This is an engine-level implementation that functions independently of specific models or output dimensions. It provides a stable memory foundation for any inference task.
Key Benefits:
Elimination of Disk I/O: Ensures 0% disk activity related to memory swapping during active inference.
Consistent Performance: Moves system latency from the millisecond range (Disk) to the nanosecond range (PCIe), providing a fluid experience regardless of task complexity.
Resource Efficiency: Maximizes the utility of available system RAM by ensuring it acts as a high-speed extension of the GPU's memory space.
Author/Developer: https://github.com/orakulstorm-hue/ComfyUI-Viking-Engine
[_device_limits.py](https://github.com/user-attachments/files/26894178/_device_limits.py)
[_pin_memory_utils.py](https://github.com/user-attachments/files/26894175/_pin_memory_utils.py)
[_utils.py](https://github.com/user-attachments/files/26894177/_utils.py)
[graphs.py](https://github.com/user-attachments/files/26894179/graphs.py)
[streams.py](https://github.com/user-attachments/files/26894176/streams.py)
### Existing Solutions
_No response_
### Other
_No response_
Contributor guide
Assessment
This issue has not been assessed yet.