Skip to main content

MoE Streaming

A mixture-of-experts (MoE) model has many expert weights. For each token, the router uses only a few of them. MoE streaming uses this to run a MoE model that does not fit in the memory of the machine. The experts stay in the model file on the disk. The llama-cpp backend reads only the experts that the router selects, and two caches keep the experts in use close to the GPU.

MoE streaming is part of the llama-cpp backend. It needs a GPU (CUDA, Metal, Vulkan or ROCm). It only reads the model file and never writes to the disk.

The tiers

The experts move through three tiers. The lowest tier is the model file. The highest tier is in GPU memory.

 +--------------------------------------------------------------+
 | GPU memory: device tier of the MoE cache                     |
 |   (moe_cache_mib)  experts that the GPU computes with        |
 +--------------------------------------------------------------+
        ^ upload on a miss                  ^ upload for one prompt batch
        |                                   |
 +-----------------------------+   +------------------------------+
 | RAM: host tier of the       |   | Prompt reader                |
 | MoE cache (pinned memory)   |   |   several threads read each  |
 |   (moe_cache_host_mib)      |   |   expert once per batch into |
 +-----------------------------+   |   two staging buffers        |
        ^ read on a miss           +------------------------------+
        |                                   ^
 +--------------------------------------------------------------+
 | Disk: the GGUF model file. The experts are not loaded at     |
 | start; the backend reads them when the router selects them.  |
 +--------------------------------------------------------------+

Generation of one token at a time (a batch of 32 tokens or fewer) uses the caches:

  1. The router selects the experts of the token.
  2. When an expert is in the device tier, the GPU uses it directly.
  3. When it is not, the backend looks in the host tier.
  4. When the host tier does not have it either, a pool of threads reads it from the model file into the host tier.
  5. The backend uploads the expert from the host tier into the device tier.

Prompt processing (a batch of more than 32 tokens) does not use the caches. The prompt reader reads each expert that the batch needs once, with several threads, and uploads it to the GPU for that batch only. A larger batch reads the experts fewer times, so the backend uses batches of up to 4096 tokens when it streams a model.

The weights that are not experts, for example the attention layers, load as usual. The GPU keeps them in its memory when they fit.

When it helps

  • A MoE model is larger than the RAM of the machine.
  • A MoE model is larger than the memory budget, for example the memory limit of a container.
  • Long prompts on a streamed MoE model. The prompt reader makes prompt processing several times faster than reading the experts from the mapped file one page at a time.

When the model fits in memory, MoE streaming does not run and the model loads as usual.

Turn it on or off

MoE streaming is on by default. It runs only when fit_stream decides that a model does not fit and streams it.

SettingWhereValuesDefaultWhat it does
fit_streammodel option, or LOCALAI_FIT_STREAM for the whole instanceauto, on, offautoauto streams a model that does not fit. on always streams. off refuses to load a model that does not fit.
localai_moe_streammodel option, or LLAMA_LOCALAI_MOE_STREAMtrue, falsetrueThe MoE streaming engine: the prompt reader, and the tier sizes that fit_stream picks. false streams the whole model file through the memory mapping, as before.
localai_moe_prefetchmodel option, or LLAMA_LOCALAI_MOE_PREFETCHtrue, falseautoWhile one layer reads a prompt, read the experts of the next layer that it used most. fit_stream takes the 384 MiB it uses from the device tier. Auto reads ahead when the disk can do it in the time the layer computes; false reads each expert when the layer needs it.
moe_cache_mibmodel optionMiBset by fit_streamSize of the device tier.
moe_cache_host_mibmodel optionMiBset by fit_streamSize of the host tier. When you set it, fit_stream does not size the tiers.

To turn MoE streaming off for one model, add the option to the model YAML:

options:
  - localai_moe_stream:false

To refuse a model that does not fit, instead of streaming it from the disk:

options:
  - fit_stream:off

The other environment variables of the engine (LLAMA_LOCALAI_MOE_STREAM_*) are for tests and tuning. See the patch notes of the backend (backend/cpp/llama-cpp/PATCHES.md).

Memory budget and fit_stream

fit_stream compares the memory that the model needs with the memory budget. The budget is the available memory of the system and the memory limit of the container, less 2 GiB. When the model needs more, fit_stream streams it. With MoE streaming on, it then does these steps:

  1. It keeps the experts in the model file.
  2. It sizes the host tier and the device tier from the budget.
  3. It raises the prompt batch to 4096 tokens, when the context allows it.
  4. When the tiers do not leave room for that batch, it makes the device tier smaller, and then the batch.

On a GPU that shares the memory with the CPU (NVIDIA DGX Spark, NVIDIA Jetson Thor, Apple silicon), both tiers use the same memory. The host tier then gets the smallest size that works, and the device tier gets the rest of the budget. On a GPU with its own memory, the device tier uses the GPU memory and the host tier uses the RAM.

When fit_stream streams a model, the prompt reader reads with direct I/O, so it does not fill the page cache.

After a load, GET /system shows the decision of the fit. moe_cache_host_mib and n_ubatch show the host tier and the prompt batch that it picked.

Measured results

The model is a 47 GB MoE model (IQ3_XXS). On the Mac mini it is a 66 GB version of the same model (Q2_0). The table shows prompt processing speed in tokens per second for a 6000-token document and a 4000-token prompt, as the median of 3 runs. The tiers are set by hand: a 12 GiB device tier and a 1 GiB host tier (4 GiB device tier on the Mac mini). The page cache holds the model file, except in the direct I/O column.

HardwareExperts on the CPU, from the mapped fileStreaming, engine offStreaming, engine onEngine on, direct I/O
NVIDIA DGX Spark (GB10)495 / 476159 / 333805 / 819450 / 478
NVIDIA Jetson AGX Thor227 / 218261 / 344519 / 528333 / 346
Apple Mac mini, M4, 16 GB-6.2 / 6.274.7 / 67.3-

The streaming columns use a prompt batch of 4096 tokens. On the Mac mini, the “engine off” figure is the earlier streaming setup with a batch of 512 tokens.

  • With the engine on and off, the model gives the same text.
  • The engine does not change the speed of generation: 21.6 to 23.5 tokens per second on the DGX Spark.
  • The direct I/O column reads past the page cache. It shows the speed from a disk that does not have the model in the page cache.

When fit_stream picks the tiers by itself under a small budget, the tiers can be smaller than in this table, and the speed is lower.

The read-ahead of the next layer (localai_moe_prefetch) adds to this. On the Mac mini, with the same memory and a prompt batch of 1024 tokens, prompts ran at 71.0 / 70.6 tokens per second with it (a 3712 MiB device tier and 384 MiB for the read-ahead) and at 61.8 / 58.6 without it (a 4096 MiB device tier). With a prompt batch of 4096 tokens: 83.8 / 83.9 against 76.0 / 74.2. With direct I/O it was 1.09x to 1.10x faster on the DGX Spark and 1.14x to 1.19x on the Jetson AGX Thor, and 1.11x to 1.16x under fit_stream with a 24 GiB budget on both. With the model in the page cache it was 0.99x to 1.06x. Generation ran within 2% of the speed without it, and the text was the same.

Under fit_stream the read-ahead does not add memory: the 384 MiB come out of the device tier. On the DGX Spark and the Thor they are pinned host memory, so the process RSS grows by 0.4 GiB while the GPU memory drops by the same amount. With tiers set by hand (moe_cache_mib), the 384 MiB come on top.

Limits

  • MoE streaming needs a GPU. On a host without a GPU, fit_stream streams the whole model file through the memory mapping.
  • fit_stream does not turn on the host tier for a model with an RPC device, a draft model or MTP. Such a model streams through the memory mapping.
  • Generation reads the missing experts when it needs them, and waits for the read. Only prompts read the experts of the next layer ahead, and only the ones that the layer used most in earlier prompt batches: the router of the next layer has not run yet.
  • An expert can be in the host tier and the device tier at the same time.
  • The Metal backend uses the same code as the other GPUs. It has no special read path.
  • The speed depends on the disk. Use a fast local NVMe disk. A network disk is much slower.