ds4 icon indicating copy to clipboard operation
ds4 copied to clipboard

Feature request: usage-aware expert caching for MoE expert streaming

Open GiorgioOppo opened this issue 5 months ago • 2 comments

Summary

When streaming experts (e.g., from disk/CPU RAM to GPU) in MoE models, the caching policy could be optimized based on the actual usage pattern, instead of a generic LRU-style policy.

Motivation

Expert activation is not uniformly distributed: it strongly depends on the workload. For example, when the model is used in chat mode in a specific language (e.g., Italian), only a limited subset of experts is activated most of the time.

Proposal

  • Track expert activation statistics during inference (per layer, e.g., a frequency counter or sliding window).
  • Use these statistics to drive the cache: pin or prioritize the "hot" experts for the current session/workload, evicting rarely used ones first (LFU-like or frequency-weighted policy instead of plain LRU).
  • Optionally allow saving/loading an activation profile, so a known workload (e.g., "Italian chat") can pre-warm the cache at startup.

Expected benefit

Higher cache hit rate on the expert weights → fewer host-to-device (or disk-to-RAM) transfers → faster token generation in streaming mode, especially on systems where experts don't fit in VRAM.

Possible concerns

  • Small overhead for tracking activations (should be negligible).
  • The profile must adapt if the workload changes mid-session (sliding window / decay handles this).

This is especially relevant on memory-constrained consumer hardware (e.g., Apple Silicon with 16GB unified memory), where experts cannot all stay resident and are repeatedly re-read from disk via mmap. Even a modest improvement in cache hit rate would noticeably improve token generation speed in these setups.

GiorgioOppo avatar Jun 11 '26 15:06 GiorgioOppo

For a simple Italian chat:

LO top8 = 32% expert 238,14, 211, 30,76, 120,18, 207 L1 top8 = 22% expert 83, 205, 254, 153, 238, 162, 246, 150 L2 top8 = 48% expert 66,216,189, 70,60,149, 154, 248 L3 top8 = 27% expert 101,71,237, 69, 232, 241, 235, 3 L4 top8 = 52% expert 97,8,113, 92, 237, 122, 130, 110 L5 top8 = 31% expert 23, 249, 142, 21, 84, 186, 129, 96 L6 top8 = 33% expert 68,16, 155, 64, 1, 93, 207, 219 L7 top8 = 40% expert 141,238, 110, 59, 122, 233, 14, 71 L8 top8 = 27% expert 28,99,200,53, 205, 113, 20, 37 L9 top8 = 34% expert 235, 28, 3, 36, 211, 191, 169, 57 L10 top8 = 46% expert 40,222,14,62,29, 11, 16, 201 L11 top8 = 35% expert 186,34, 70, 46, 47, 30, 94, 5 L12 top8 = 48% expert 250,124, 154, 189, 142, 14, 134, 198 L13 top8 = 47% expert 39,179, 72, 107, 59, 47, 148, 51 L14 top8 = 44% expert 235,200, 70, 61,184, 122, 244, 223 L15 top8 = 41% expert 236, 225, 45, 180, 27, 157, 5, 83 L16 top8 = 48% expert 175,30,25, 76, 178, 176, 155, 203 L17 top8 = 45% expert 33,146, 31,5, 92, 238, 189, 77 L18 top8 = 45% expert 161,116, 211, 90, 139, 183, 34, 89 L19 top8 = 29% expert 92,200,176, 35, 104, 243, 60, 183 L20 top8 = 47% expert 218,219, 131, 149, 204, 184, 70, 176 L21 top8 = 39% expert 184,214, 49, 193, 139, 93, 235, 19 L22 top8 = 53% expert 238,158, 166, 173, 135, 110, 147, 220 L23 top8 = 38% expert 5,71,97, 170, 227, 64, 228, 248 L24 top8 = 50% expert 112,209,185, 128, 135, 27, 123, 105 L25 top8 = 42% expert 67,94,126, 167, 9, 65, 176, 64 L26 top8 = 49% expert 201,191,61,19, 215, 79, 195, 250 L27 top8 = 28% expert 172,180, 2, 166, 47, 204, 84, 72 L28 top8 = 43% expert 137,130, 56, 36, 19, 176, 152, 218 L29 top8 = 39% expert 13,140,96, 230, 178, 66, 180, 226 L30 top8 = 33% expert 121,191, 238, 211, 12, 78, 91, 119 L31 top8 = 36% expert 125, 129, 202, 227, 206, 245, 241, 19 L32 top8 = 41% expert 193,17, 214, 46, 81, 40, 68, 58 L33 top8 = 44% expert 187,58, 41, 25, 212, 5, 158, 55 L34 top8 = 38% expert 201,18, 214, 176, 45, 81, 77, 227 L35 top8 = 23% expert 101,99,160, 84, 34, 0, 235, 203 L36 top8 = 47% expert 63,90,172, 174, 76, 223, 79, 16 L37 top8 = 34% expert 6,1,211, 171, 159, 110, 114, 120 L38 top8 = 46% expert 56,47,23, 10, 33, 253, 13, 110 L39 top8 = 25% expert 32,234, 165, 223, 66, 233, 55, 220 L40 top8 = 35% expert 104, 22, 207, 192, 155, 156, 26, 212 L41 top8 = 38% expert 23,54, 52, 132, 167,160, 7, 133 L42 top8 = 30% expert 240,15,225, 63, 217,97, 244,192

GiorgioOppo avatar Jun 11 '26 16:06 GiorgioOppo

The chart you provided is not proof that successive invocations will benefit from this, aggregates are useless. In my tests LRU worked better, since the model in a given moment is producing, for instance C code, and the latest experts have the best match. You need to experimentally verify that it works better with a different system and provide actual numbers or hard statistical evidence in the generation chain under real world. Please reopen if you have hard evidence.

antirez avatar Jun 16 '26 09:06 antirez