Streaming Mixture-of-Experts Weights from Disk in Transformers
According to a Hugging Face blog post, Tarek Ziadé implemented an experimental expert-streaming path in Transformers that streams router-selected Mixture-of-Experts weights from an SSD into a bounded cache.
According to a Hugging Face blog post by Tarek Ziadé, an experimental expert-streaming path for Mixture-of-Experts (MoE) models was introduced to Hugging Face Transformers via pull request #50717.
What Changed
Standard inference frameworks often copy entire expert banks or layers across the PCIe bus, causing memory thrashing and overhead. Instead of using Accelerate's module-level offloading, the experimental approach streams specific MoE model weights directly from an SSD based on router selections.
Running massive sparse models locally requires strict memory budgeting. According to the source, executing Qwen3.8-Flash-Next on a 32GB Mac M5 involves quantizing experts down to 3 bits (yielding 49 GB on disk with a 12 GiB device cache), retaining dense layers in 8 bits, and keeping a 51 GB n-gram embedding table on disk.
How It Works
To avoid slow Python loops over individual offloaded experts, the implementation uses a slot pool design:
- Slot Pools: The system establishes a single preallocated device tensor shared across layers, alongside a per-layer mapping table.
- Batching: The runtime remaps router expert IDs to these predefined slots. This layout causes the tensor to mimic a regular expert bank, letting the framework run existing batched matrix multiplication kernels (
grouped_mm) without custom expert matmul implementations. - Optimizations: Performance improvements relied on committing GPU work early, adopting Strata's GGUF weights, and adding targeted Metal kernels for macOS.
Why Developers Might Care
This approach brings local execution of massive language models closer to resource-constrained consumer hardware. After optimizations, the modified Transformers setup reportedly reached 13.9 tokens per second on short decode (compared to Strata's 14.8 tok/s) and 228 tokens per second on prefill, demonstrating that a Python-based implementation using Transformers and runtime-compiled Metal shaders can closely match specialized inference engines.
Limitations
The source highlights several limitations to consider:
- API Stability: The API is still under discussion with Transformers maintainers and requires specific model layouts and conversion scripts.
- Memory Constraints: Memory usage approaches hardware maximums; a 6K prompt can peak at 25.4 GiB, leaving little headroom before macOS triggers swapping and degrades performance.
- Synchronization Overhead: On Apple Silicon (MPS), PyTorch holds execution kernels until synchronization points, leaving the GPU idle while Python queues work layer-by-layer. This adds approximately 10 ms of round-trip latency per token, and PyTorch does not natively expose the fine-grained synchronization primitives needed to resolve this without risking system hangs.
- Throughput: The author notes the setup remains too slow for interactive coding agent workflows requiring higher throughput.