# Lightx2v Parameter Offloading Mechanism Documentation ## πŸ“– Overview Lightx2v implements an advanced parameter offloading mechanism designed for large model inference under limited hardware resources. This system provides excellent speed-memory balance through intelligent management of model weights across different memory hierarchies. **Core Features:** - **Block/Phase Offloading**: Efficiently manages model weights in block/phase units for optimal memory usage - **Block**: Basic computational unit of Transformer models, containing complete Transformer layers (self-attention, cross-attention, feed-forward networks, etc.), serving as larger memory management units - **Phase**: Finer-grained computational stages within blocks, containing individual computational components (such as self-attention, cross-attention, feed-forward networks, etc.), providing more precise memory control - **Multi-level Storage Support**: GPU β†’ CPU β†’ Disk hierarchy with intelligent caching - **Asynchronous Operations**: Uses CUDA streams to overlap computation and data transfer - **Disk/NVMe Serialization**: Supports secondary storage when memory is insufficient ## 🎯 Offloading Strategies ### Strategy 1: GPU-CPU Block/Phase Offloading **Applicable Scenarios**: GPU VRAM insufficient but system memory adequate **Working Principle**: Manages model weights in block or phase units between GPU and CPU memory, utilizing CUDA streams to overlap computation and data transfer. Blocks contain complete Transformer layers, while phases are individual computational components within blocks. **Block vs Phase Explanation**: - **Block Granularity**: Larger memory management units containing complete Transformer layers (self-attention, cross-attention, feed-forward networks, etc.), suitable for memory-sufficient scenarios, reducing management overhead - **Phase Granularity**: Finer-grained memory management containing individual computational components (such as self-attention, cross-attention, feed-forward networks, etc.), suitable for memory-constrained scenarios, providing more flexible memory control ``` GPU-CPU Block/Phase Offloading Workflow: ╔═════════════════════════════════════════════════════════════════╗ β•‘ 🎯 GPU Memory β•‘ ╠═════════════════════════════════════════════════════════════════╣ β•‘ β•‘ β•‘ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β•‘ β•‘ β”‚ πŸ”„ Current β”‚ β”‚ ⏳ Prefetch β”‚ β”‚ πŸ“€ To Offload β”‚ β•‘ β•‘ β”‚ block/phase N │◄──►│ block/phase N+1 │◄──►│ block/phase N-1 β”‚ β•‘ β•‘ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β•‘ β•‘ β”‚ β”‚ β”‚ β•‘ β•‘ β–Ό β–Ό β–Ό β•‘ β•‘ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β•‘ β•‘ β”‚ Compute β”‚ β”‚ GPU Load β”‚ β”‚ CPU Load β”‚ β•‘ β•‘ β”‚ Stream β”‚ β”‚ Stream β”‚ β”‚ Stream β”‚ β•‘ β•‘ β”‚(priority=-1)β”‚ β”‚ (priority=0) β”‚ β”‚ (priority=0) β”‚ β•‘ β•‘ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β•‘ β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β• ↕ ╔═════════════════════════════════════════════════════════════════╗ β•‘ πŸ’Ύ CPU Memory β•‘ ╠═════════════════════════════════════════════════════════════════╣ β•‘ β•‘ β•‘ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β•‘ β•‘ β”‚ πŸ“₯ Cache β”‚ β”‚ πŸ“₯ Cache β”‚ β”‚ πŸ“₯ Cache β”‚ β”‚ πŸ“₯ Cache β”‚ β•‘ β•‘ β”‚ block/phase β”‚ β”‚ block/phase β”‚ β”‚ block/phase β”‚ β”‚ block/phase β”‚ β•‘ β•‘ β”‚ N-2 β”‚ β”‚ N-1 β”‚ β”‚ N β”‚ β”‚ N+1 β”‚ β•‘ β•‘ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β•‘ β•‘ β–² β–² β–² β–² β•‘ β•‘ β”‚ β”‚ β”‚ β”‚ β•‘ β•‘ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β•‘ β•‘ β”‚ CPU Load β”‚ β”‚ CPU Load β”‚ β”‚ CPU Load β”‚ β”‚ CPU Load β”‚ β•‘ β•‘ β”‚ Stream β”‚ β”‚ Stream β”‚ β”‚ Stream β”‚ β”‚ Stream β”‚ β•‘ β•‘ β”‚(priority=0) β”‚ β”‚(priority=0) β”‚ β”‚(priority=0) β”‚ β”‚(priority=0) β”‚ β•‘ β•‘ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β•‘ β•‘ β•‘ β•‘ πŸ’‘ CPU memory stores multiple blocks/phases, forming cache pool β•‘ β•‘ πŸ”„ GPU load stream prefetches from CPU cache, CPU load stream β•‘ β•‘ offloads to CPU cache β•‘ β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β• ╔═════════════════════════════════════════════════════════════════╗ β•‘ πŸ”„ Swap Operation Flow β•‘ ╠═════════════════════════════════════════════════════════════════╣ β•‘ β•‘ β•‘ Step 1: Parallel Execution Phase β•‘ β•‘ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β•‘ β•‘ β”‚ πŸ”„ Compute β”‚ β”‚ ⏳ Prefetch β”‚ β”‚ πŸ“€ Offload β”‚ β•‘ β•‘ β”‚ block/phase N β”‚ β”‚ block/phase N+1 β”‚ β”‚ block/phase N-1 β”‚ β•‘ β•‘ β”‚ (Compute Stream)β”‚ β”‚ (GPU Load Stream)β”‚ β”‚ (CPU Load Stream)β”‚ β•‘ β•‘ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β•‘ β•‘ β•‘ β•‘ Step 2: Swap Rotation Phase β•‘ β•‘ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β•‘ β•‘ β”‚ πŸ”„ Compute β”‚ β”‚ ⏳ Prefetch β”‚ β”‚ πŸ“€ Offload β”‚ β•‘ β•‘ β”‚ block/phase N+1 β”‚ β”‚ block/phase N+2 β”‚ β”‚ block/phase N β”‚ β•‘ β•‘ β”‚ (Compute Stream)β”‚ β”‚ (GPU Load Stream)β”‚ β”‚ (CPU Load Stream)β”‚ β•‘ β•‘ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β•‘ β•‘ β•‘ β•‘ Swap Concept: Achieves continuous computation through position β•‘ β•‘ rotation, avoiding repeated loading/unloading β•‘ β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β• ╔═════════════════════════════════════════════════════════════════╗ β•‘ πŸ’‘ Swap Core Concept β•‘ ╠═════════════════════════════════════════════════════════════════╣ β•‘ β•‘ β•‘ πŸ”„ Traditional vs Swap Method Comparison: β•‘ β•‘ β•‘ β•‘ Traditional Method: β•‘ β•‘ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β” β•‘ β•‘ β”‚ Compute N │───►│ Offload N│───►│ Load N+1│───►│Compute β”‚ β•‘ β•‘ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚N+1 β”‚ β•‘ β•‘ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β•‘ β•‘ ❌ Serial execution, waiting time, low efficiency β•‘ β•‘ β•‘ β•‘ Swap Method: β•‘ β•‘ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β•‘ β•‘ β”‚ Compute N β”‚ β”‚ Prefetch β”‚ β”‚ Offload β”‚ β•‘ β•‘ β”‚(Compute β”‚ β”‚N+1 β”‚ β”‚N-1 β”‚ β•‘ β•‘ β”‚ Stream) β”‚ β”‚(GPU Load β”‚ β”‚(CPU Load β”‚ β•‘ β•‘ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ Stream) β”‚ β”‚ Stream) β”‚ β•‘ β•‘ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β•‘ β•‘ βœ… Parallel execution, no waiting time, high efficiency β•‘ β•‘ β•‘ β•‘ 🎯 Swap Advantages: β•‘ β•‘ β€’ Avoids repeated loading/unloading of same data β•‘ β•‘ β€’ Achieves continuous computation through position rotation β•‘ β•‘ β€’ Maximizes GPU utilization β•‘ β•‘ β€’ Reduces memory fragmentation β•‘ β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β• ``` **Key Features:** - **Asynchronous Transfer**: Uses three CUDA streams with different priorities to parallelize computation and transfer - Compute Stream (priority=-1): High priority, responsible for current computation - GPU Load Stream (priority=0): Medium priority, responsible for prefetching from CPU to GPU - CPU Load Stream (priority=0): Medium priority, responsible for offloading from GPU to CPU - **Prefetch Mechanism**: Preloads the next block/phase to GPU - **Intelligent Caching**: Maintains weight cache in CPU memory - **Stream Synchronization**: Ensures correctness of data transfer and computation - **Swap Operation**: Rotates block/phase positions after computation completion for continuous processing ### Strategy 2: Disk-CPU-GPU Block/Phase Offloading (Lazy Loading) **Applicable Scenarios**: Both GPU VRAM and system memory insufficient **Working Principle**: Introduces disk storage on top of Strategy 1, implementing a three-level storage hierarchy (Disk β†’ CPU β†’ GPU). CPU continues as a cache pool but with configurable size, suitable for CPU memory-constrained devices. ``` Disk-CPU-GPU Block/Phase Offloading Workflow: ╔═════════════════════════════════════════════════════════════════╗ β•‘ πŸ’Ώ SSD/NVMe Storage β•‘ ╠═════════════════════════════════════════════════════════════════╣ β•‘ β•‘ β•‘ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β•‘ β•‘ β”‚ πŸ“ block_0 β”‚ β”‚ πŸ“ block_1 β”‚ β”‚ πŸ“ block_2 β”‚ β”‚ πŸ“ block_N β”‚ β•‘ β•‘ β”‚ .safetensorsβ”‚ β”‚ .safetensorsβ”‚ β”‚ .safetensorsβ”‚ β”‚ .safetensorsβ”‚ β•‘ β•‘ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β•‘ β•‘ β”‚ β”‚ β”‚ β”‚ β•‘ β•‘ β–Ό β–Ό β–Ό β–Ό β•‘ β•‘ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β•‘ β•‘ β”‚ 🎯 Disk Worker Thread Pool β”‚ β•‘ β•‘ β”‚ β”‚ β•‘ β•‘ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ β•‘ β•‘ β”‚ β”‚ Disk Thread β”‚ β”‚ Disk Thread β”‚ β”‚ Disk Thread β”‚ β”‚ β•‘ β•‘ β”‚ β”‚ 1 β”‚ β”‚ 2 β”‚ β”‚ N β”‚ β”‚ β•‘ β•‘ β”‚ β”‚(Async Load) β”‚ β”‚(Async Load) β”‚ β”‚(Async Load) β”‚ β”‚ β•‘ β•‘ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β•‘ β•‘ β”‚ β”‚ β”‚ β”‚ β”‚ β•‘ β•‘ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β•‘ β•‘ β”‚ β–Ό β”‚ β•‘ β•‘ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ β•‘ β•‘ β”‚ β”‚ πŸ“‹ Priority Task Queue β”‚ β”‚ β•‘ β•‘ β”‚ β”‚ (Manages disk loading task scheduling) β”‚ β”‚ β•‘ β•‘ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β•‘ β•‘ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β•‘ β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β• ↓ ╔═════════════════════════════════════════════════════════════════╗ β•‘ πŸ’Ύ CPU Memory Buffer β•‘ ╠═════════════════════════════════════════════════════════════════╣ β•‘ β•‘ β•‘ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β•‘ β•‘ β”‚ 🎯 FIFO Intelligent Cache β”‚ β•‘ β•‘ β”‚ β”‚ β•‘ β•‘ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β•‘ β•‘ β”‚ β”‚ πŸ“₯ Cache β”‚ β”‚ πŸ“₯ Cache β”‚ β”‚ πŸ“₯ Cache β”‚ β”‚ πŸ“₯ Cache β”‚ β•‘ β•‘ β”‚ β”‚ block/phase β”‚ β”‚ block/phase β”‚ β”‚ block/phase β”‚ β”‚ block/phase β”‚ β•‘ β•‘ β”‚ β”‚ N-2 β”‚ β”‚ N-1 β”‚ β”‚ N β”‚ β”‚ N+1 β”‚ β•‘ β•‘ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β•‘ β•‘ β”‚ β–² β–² β–² β–² β•‘ β•‘ β”‚ β”‚ β”‚ β”‚ β”‚ β•‘ β•‘ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β•‘ β•‘ β”‚ β”‚ CPU Load β”‚ β”‚ CPU Load β”‚ β”‚ CPU Load β”‚ β”‚ CPU Load β”‚ β•‘ β•‘ β”‚ β”‚ Stream β”‚ β”‚ Stream β”‚ β”‚ Stream β”‚ β”‚ Stream β”‚ β•‘ β•‘ β”‚ β”‚(priority=0) β”‚ β”‚(priority=0) β”‚ β”‚(priority=0) β”‚ β”‚(priority=0) β”‚ β•‘ β•‘ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β•‘ β•‘ β”‚ β”‚ β•‘ β•‘ β”‚ πŸ’‘ Configurable Size 🎯 FIFO Eviction πŸ”„ Cache Hit/Miss β”‚ β•‘ β•‘ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β•‘ β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β• ↕ ╔═════════════════════════════════════════════════════════════════╗ β•‘ 🎯 GPU Memory β•‘ ╠═════════════════════════════════════════════════════════════════╣ β•‘ β•‘ β•‘ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β•‘ β•‘ β”‚ πŸ”„ Current β”‚ β”‚ ⏳ Prefetch β”‚ β”‚ πŸ“€ To Offload β”‚ β•‘ β•‘ β”‚ block/phase N │◄──►│ block/phase N+1 │◄──►│ block/phase N-1 β”‚ β•‘ β•‘ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β•‘ β•‘ β”‚ β”‚ β”‚ β•‘ β•‘ β–Ό β–Ό β–Ό β•‘ β•‘ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β•‘ β•‘ β”‚ Compute β”‚ β”‚ GPU Load β”‚ β”‚ CPU Load β”‚ β•‘ β•‘ β”‚ Stream β”‚ β”‚ Stream β”‚ β”‚ Stream β”‚ β•‘ β•‘ β”‚(priority=-1)β”‚ β”‚ (priority=0) β”‚ β”‚ (priority=0) β”‚ β•‘ β•‘ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β•‘ β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β• ╔═════════════════════════════════════════════════════════════════╗ β•‘ πŸ”„ Complete Workflow β•‘ ╠═════════════════════════════════════════════════════════════════╣ β•‘ β•‘ β•‘ Step 1: Cache Miss Handling β•‘ β•‘ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β•‘ β•‘ β”‚ πŸ’Ώ Disk │───►│ πŸ’Ύ CPU Cache│───►│ 🎯 GPU β”‚ β•‘ β•‘ β”‚ (On-demand β”‚ β”‚ (FIFO β”‚ β”‚ Memory β”‚ β•‘ β•‘ β”‚ loading) β”‚ β”‚ Management)β”‚ β”‚ (Compute β”‚ β•‘ β•‘ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ Execution) β”‚ β•‘ β•‘ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β•‘ β•‘ β•‘ β•‘ Step 2: Cache Hit Handling β•‘ β•‘ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β•‘ β•‘ β”‚ πŸ’Ώ Disk β”‚ β”‚ πŸ’Ύ CPU Cache│───►│ 🎯 GPU β”‚ β•‘ β•‘ β”‚ (Skip β”‚ β”‚ (Direct β”‚ β”‚ Memory β”‚ β•‘ β•‘ β”‚ loading) β”‚ β”‚ Access) β”‚ β”‚ (Compute β”‚ β•‘ β•‘ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ Execution) β”‚ β•‘ β•‘ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β•‘ β•‘ β•‘ β•‘ Step 3: Memory Management β•‘ β•‘ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β•‘ β•‘ β”‚ πŸ’Ώ Disk β”‚ β”‚ πŸ’Ύ CPU Cacheβ”‚ β”‚ 🎯 GPU β”‚ β•‘ β•‘ β”‚ (Persistent β”‚ β”‚ (FIFO β”‚ β”‚ Memory β”‚ β•‘ β•‘ β”‚ Storage) β”‚ β”‚ Eviction) β”‚ β”‚ (Swap β”‚ β•‘ β•‘ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ Rotation) β”‚ β•‘ β•‘ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β•‘ β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β• Work Steps: 1. Disk Storage: Model weights stored by block on SSD/NVMe, one .safetensors file per block 2. Task Scheduling: When a block/phase is needed, priority task queue assigns disk worker threads 3. Async Loading: Multiple disk threads parallelly read weight files from disk to CPU memory buffer 4. Intelligent Caching: CPU memory buffer uses FIFO strategy for cache management with configurable size 5. Cache Hit: If weights are already in cache, directly transfer to GPU without disk reading 6. Prefetch Transfer: Weights in cache asynchronously transfer to GPU memory (using GPU load stream) 7. Compute Execution: Weights on GPU perform computation (using compute stream), while background continues prefetching next block/phase 8. Swap Rotation: After computation completion, rotate block/phase positions for continuous computation 9. Memory Management: When CPU cache is full, automatically evict earliest used weight blocks/phases ``` **Key Features:** - **Lazy Loading**: Model weights loaded from disk on-demand, avoiding loading entire model at once - **Intelligent Caching**: CPU memory buffer uses FIFO strategy with configurable size - **Multi-threaded Prefetching**: Uses multiple disk worker threads for parallel loading - **Asynchronous Transfer**: Uses CUDA streams to overlap computation and data transfer - **Swap Rotation**: Achieves continuous computation through position rotation, avoiding repeated loading/unloading ## βš™οΈ Configuration Parameters ### GPU-CPU Offloading Configuration ```python config = { "cpu_offload": True, "offload_ratio": 1.0, # Offload ratio (0.0-1.0) "offload_granularity": "block", # Offload granularity: "block" or "phase" "lazy_load": False, # Disable lazy loading } ``` ### Disk-CPU-GPU Offloading Configuration ```python config = { "cpu_offload": True, "lazy_load": True, # Enable lazy loading "offload_ratio": 1.0, # Offload ratio "offload_granularity": "phase", # Recommended to use phase granularity "num_disk_workers": 2, # Number of disk worker threads "offload_to_disk": True, # Enable disk offloading "offload_path": ".", # Disk offload path } ``` **Intelligent Cache Key Parameters:** - `max_memory`: Controls CPU cache size, affects cache hit rate and memory usage - `num_disk_workers`: Controls number of disk loading threads, affects prefetch speed - `offload_granularity`: Controls cache granularity (block or phase), affects cache efficiency - `"block"`: Cache management in units of complete Transformer layers - `"phase"`: Cache management in units of individual computational components Detailed configuration files can be referenced at [config](https://github.com/ModelTC/lightx2v/tree/main/configs/offload) ## 🎯 Usage Recommendations ``` ╔═════════════════════════════════════════════════════════════════╗ β•‘ πŸ“‹ Configuration Guide β•‘ ╠═════════════════════════════════════════════════════════════════╣ β•‘ β•‘ β•‘ πŸ”„ GPU-CPU Block/Phase Offloading: β•‘ β•‘ Suitable for insufficient GPU VRAM (RTX 3090/4090 24G) β•‘ β•‘ but adequate system memory (>64/128G) β•‘ β•‘ πŸ’Ύ Disk-CPU-GPU Block/Phase Offloading: β•‘ β•‘ Suitable for insufficient GPU VRAM (RTX 3060/4090 8G) β•‘ β•‘ and system memory (16/32G) β•‘ β•‘ 🚫 No Offload: Suitable for high-end hardware configurations, β•‘ β•‘ pursuing optimal performance β•‘ β•‘ β•‘ β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β• ``` ## πŸ” Troubleshooting ### Common Issues and Solutions 1. **Disk I/O Bottleneck** ``` Solution: Use NVMe SSD, increase num_disk_workers ``` 2. **Memory Buffer Overflow** ``` Solution: Increase max_memory or decrease num_disk_workers ``` 3. **Loading Timeout** ``` Solution: Check disk performance, optimize file system ``` **Note**: This offloading mechanism is specifically designed for Lightx2v, fully utilizing modern hardware's asynchronous computing capabilities, significantly reducing the hardware threshold for large model inference.