Streaming Massive MoE Models: Running DeepSeek-V4 in RAM

Learn how to run 284B parameter MoE models like DeepSeek-V4 on consumer hardware by streaming weights from NVMe storage using pure C.

hardware, code, memory

Most developers hit a hard wall when attempting to run frontier-scale models. If your model is larger than your available VRAM or RAM, the standard answer is usually “out of memory,” but for systems engineers, that is just a resource management problem.

The DeepSeek-V4 implementation in C99 solves this by treating your NVMe drive as an extension of memory. It is not a speed play; it is a feasibility play.

The Engineering Trade-off: Speed vs. Feasibility

This project allows you to run a massive Mixture-of-Experts (MoE) model—specifically the DeepSeek-V4 Flash checkpoint with 284B parameters (~160 GB on disk)—on a laptop with as little as 3.2 GB of RAM. You are trading inference speed for the ability to actually execute a model that would otherwise be impossible to load.

ScenarioRecommended ToolWhy?
Model fits in RAM/VRAMllama.cppStreaming adds unnecessary overhead.
Model fits in VRAM + RAMKTransformersOptimized hybrid CPU/GPU execution.
Model exceeds all memory (Exact Output)deepseek-v4-in-cIdentical output to reference via streaming.
Model exceeds all memory (High Speed)colibriAdvanced I/O path and async prefetch.

How the Streaming Architecture Works

hardware, code, memory

The core design is a port of Fareed Khan’s kimi-k3-in-c, specifically handling the heavy lifting of memory management. While 40% of the logic—the safetensors reader, trunk streamer, and expert cache—is ported, the remaining 60% is entirely custom. Because DeepSeek-V4 uses different math than Kimi, every kernel was rewritten from the original reference implementation.

The engine operates with a strict focus on read-only I/O to ensure data integrity and predictable behavior. It avoids common pitfalls like mmap or swap-backed temporary files that can lead to unpredictable latency.

  • Direct I/O: Uses O_RDONLY | O_DIRECT for efficient, unbuffered reads.
  • Zero Writes: The inference path never writes to the disk; it only reads checkpoint shards via pread.
  • Memory Planning: A custom memory planner manages the expert cache to keep the footprint minimal.

Performance Benchmarks

On a standard laptop setup, you can expect roughly 1.6 – 1.7 s/token when provided with 18 GB of RAM and GPU acceleration. For pure CPU execution on DeepSeek-V4-Flash, the speed settles around:

DeepSeek-V4-Flash (CPU): 2.14 – 2.21 s/token

Addressing the SSD Endurance Question

hardware, code, memory

A common concern is whether this constant streaming will wear out your NVMe drive. The short answer is no, because the inference path never writes to the disk. Unlike traditional swap files that can cause heavy write amplification, this engine only performs reads.

However, there is a nuance regarding read disturb. Repeatedly reading the same NAND cells (specifically the “hot” experts in the ~137 GB region) can theoretically perturb neighboring cells, causing the controller to perform background relocation writes. While this technically contributes to TBW (Total Bytes Written), it is a negligible factor compared to standard OS operations.

Choosing Your Path: Exactness vs. Speed

If you are building production-grade agentic workflows, you need to decide if your priority is mathematical exactness or raw throughput. If you want the fastest possible execution for massive models, Colibri is a significant step ahead in the I/O path, featuring async prefetch and multi-drive striping.

For those working on Multi-Model Orchestration or complex agentic reasoning where the model must be bit-for-bit identical to the reference, this C implementation is your primary tool.

Ready to experiment? Clone the repository and start testing the limits of your local hardware. Just remember: this is a learning tool for understanding low-level memory management, not a replacement for high-performance inference engines.

hardware, code, memory