Skip to content

moe-stream: routed experts streamed from the model file (opt-in) - #19

Merged
bong-water-water-bong merged 7 commits into
1bit/vulkan-upstreamfrom
1bit/moe-stream
Sep 26, 2026
Merged

bong-water-water-bong merged 7 commits into
1bit/vulkan-upstreamfrom
1bit/moe-stream

Conversation

@bong-water-water-bong

Copy link
Copy Markdown

Streams a MoE model's routed experts from the GGUF into a RAM cache during decode, so the experts need not all fit in memory (1bit engine, docs/moe-streaming.md).

Off by default. It is enabled with ONEBIT_MOE_FILE (the model's GGUF) and ONEBIT_MOE_SLOTS (experts held), and the expert tensors are loaded mmap-ed on the CPU (-ot exps=CPU). With the variables unset, build_moe_ffn is unchanged apart from one pointer check.

How it works

  • GPU mode (default on a UMA GPU):
    • each MoE layer's slots are a Vulkan buffer the host maps, filled by O_DIRECT reads (several in flight, pinned bounce buffers);
    • a CPU op pins the batch's experts and turns their ids into slot indices;
    • the regular MUL_MAT_ID then runs on Vulkan over the slots.
  • CPU mode (ONEBIT_MOE_DEVICE=cpu): three custom ops on the CPU's dot kernels.
  • Gate-ahead prefetch of the next layer's experts (ONEBIT_MOE_PREFETCH, on by default, see below).
  • Batches whose experts cannot all fit one layer's slots, such as prompts, keep the mmap-ed path.

Files. New: src/llama-moe-stream.*, and src/moe-{expert-cache,gguf-index}.* (the engine's moe/), Linux only. Changed:

  • llama-graph.cpp: the hook in build_moe_ffn;
  • src/CMakeLists.txt;
  • ggml-vulkan.cpp: ggml_backend_vk_buffer_get_host_ptr through the registry's get_proc_address, which was NULL.

Correctness (Qwen3-Coder-30B Q4_K_M, perplexity over 512 tokens):

  • GPU mode matches all-Vulkan exactly: 21.1029 at batch 1 and 20.9475 at batch 4, whatever the slot count or prefetch.
  • CPU mode matches the CPU expert path: 21.160 vs 21.153 over 1024 tokens.

Speed (Strix Halo, shared box with load around 5, warm repetitions):

setup decode tok/s
all on Vulkan0 79-91
experts on the CPU, mmap 25-43
GPU streaming, 4,608 slots (75%) 28-38 (cold: 12)
GPU streaming, 3,072 slots (50%) 17-22
GPU streaming, 1,536 slots (25%) 8-10

Open

  • Prefetch lowers the rate in these runs: it wastes reads when the drive is the limit.
  • 6,144 slots, where everything fits, was erratic under the box's memory pressure.

Streaming off, all-Vulkan decode is unchanged (78.8 tok/s on this build under load).

🤖 Generated with Claude Code

bong-water-water-bong and others added 7 commits September 26, 2026 13:41
…lots (1bit engine)

A MoE layer's expert FFN (SwiGLU, separate gate/up, no biases or scales) runs on the CPU out
of an expert cache: pinned slots filled by O_DIRECT reads, several in flight
(moe-expert-cache, from the 1bit engine's moe/). Three custom ops per layer: pin the
batch's experts and convert its inputs, gate/up/SwiGLU, down; the routing weights and the sum
stay in the graph. Batches larger than ONEBIT_MOE_MAX_BATCH (prompts) keep the regular path.

Enabled by ONEBIT_MOE_FILE (the GGUF) and ONEBIT_MOE_SLOTS; Linux only.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Each layer's pinned slots are imported as a device buffer (buffer_from_host_ptr; Strix Halo's
GPU shares the memory) and viewed as strided gate/up/down tensors. A CPU op pins the batch's
experts and maps their ids to slot indices; the regular MUL_MAT_ID path then runs on the GPU
over the slots. The expert cache now keeps each part at a fixed offset in its slot.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…nary expert tensors

Vulkan's MUL_MAT_ID takes the expert stride as ne00*ne01, not nb[2], so strided slots read
garbage. The cache now keeps one packed array per part per list (part k of slot i at
part_off[k] + i * part_bytes[k]); O_DIRECT reads go through each reader's aligned bounce
buffer and are copied into place.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The next MoE layer's router (F32, read from the file once) is applied to this layer's FFN
input while this layer computes; its top-k experts are queued for reading. Single-token
steps; ONEBIT_MOE_PREFETCH=0 turns it off.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ost memory

Vulkan MUL_MAT_ID on imported host memory ran at about 1 GB/s (6-14 ms per expert matmul on
Strix Halo). On UMA, ggml-vulkan's device buffers are host-visible; the expert cache now takes
a region per list from the caller, and GPU mode allocates each layer's slots as a Vulkan
buffer and fills it through the host mapping. ggml-vulkan exposes the mapping through its
registry's get_proc_address (ggml_backend_vk_buffer_get_host_ptr).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@bong-water-water-bong
bong-water-water-bong merged commit 57c22a3 into 1bit/vulkan-upstream Sep 26, 2026
6 of 15 checks passed
@bong-water-water-bong
bong-water-water-bong deleted the 1bit/moe-stream branch November 12, 2026 21:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant