Skip to content

Kyojin: open-source engine that runs 100 GB-class open models (GLM-5.3, Qwen3.8, DeepSeek V4.1) on one desktop machine. AMD Strix Halo, NVIDIA RTX and Apple Mac. OpenAI API, built on ExLlamaV3.

Prefill host syncs: sync-free expert histogram in the aligned MoE layout (EXL3_MOE_ALIGN_SYNC_FREE, default on) and EXL3_NC2_ASYNC_STASH=1 in the GLM and Qwen serve envs; logits bitwise equal (first token + tail resume)

YanissAmz6fee2775b3e7
diff --git a/exllamav3/modules/block_sparse_mlp.py b/exllamav3/modules/block_sparse_mlp.py
index 4515f1f..7f2452b 100644
--- a/exllamav3/modules/block_sparse_mlp.py
+++ b/exllamav3/modules/block_sparse_mlp.py
@@ -129,7 +129,8 @@ def _aligned_expert_layout(flat: torch.Tensor, num_tokens: int, top_k: int, num_
"""flat: (num_tokens * top_k) int64 expert ids, slot = token * top_k + k. Returns (order, sel_flat, dummy_tokens):
order lists the assignment slots grouped by expert, each (expert, chunk of `chunk` tokens) segment in slot order
and padded to an even length with dummy slots; sel_flat is the expert id of every slot including the dummy ones
- (slots >= num_tokens * top_k belong to dummy tokens num_tokens .. num_tokens + dummy_tokens - 1). No host sync."""
+ (slots >= num_tokens * top_k belong to dummy tokens num_tokens .. num_tokens + dummy_tokens - 1). No host sync
+ (EXL3_MOE_ALIGN_SYNC_FREE, default on)."""
dev = flat.device
a0 = num_tokens * top_k
nc = (num_tokens + chunk - 1) // chunk
@@ -138,7 +139,9 @@ def _aligned_expert_layout(flat: torch.Tensor, num_tokens: int, top_k: int, num_
total = a0 + dummy_tokens * top_k
key = flat * nc + torch.arange(a0, device = dev) // (top_k * chunk)
srt = key.argsort(stable = True)
- kc = torch.bincount(key, minlength = nseg)
+ # torch.bincount on a device tensor syncs the host (min/max reduction + D2H) once per MoE layer: with big chunks (> 4096 rows)
+ # that drained the queue 36-39 times per forward. Same int64 histogram, device only (EXL3_MOE_ALIGN_SYNC_FREE=0 = bincount, read per call).
+ kc = _scatter_expert_count(key, nseg) if os.environ.get("EXL3_MOE_ALIGN_SYNC_FREE", "1") != "0" else torch.bincount(key, minlength = nseg)
odd = kc & 1
padbefore = torch.cumsum(odd, 0) - odd
pos = torch.arange(a0, device = dev) + padbefore[key[srt]]
diff --git a/tools/glm/serve.py b/tools/glm/serve.py
index 07c0be6..580f5fc 100644
--- a/tools/glm/serve.py
+++ b/tools/glm/serve.py
@@ -127,6 +127,9 @@ SPEED_ENV = {
"EXL3_PF_GLUE": "1",
"EXL3_PF_MSPLIT": "2048",
"EXL3_PF_SKIP": "1",
+ # recurrent-state stashes (mid-prompt checkpoints, prompt-end tail) copy to pinned host memory without a host sync: bitwise
+ # (first-token + tail-resume logits equal, 5 reps x 8K/32K, base vs on); removes ~33 x 0.6 ms of idle at the end of a prefill. 0 = off.
+ "EXL3_NC2_ASYNC_STASH": "1",
# 14/10: verify of 5..8 rows (GLM_NGRAM lookup rounds of 4..7 copied tokens), the Qwen server's switches: MoE row split,
# one-launch 8-row fused MoE, one-launch 6-row dense GEMV. Bit-identical per row, rows <= 4 untouched (--num-draft 2 rounds).
# Harness, one load each: 6-row round +6.9 % (RM6), 8-row round +2.6 %, ids equal. 0 = off.
diff --git a/tools/qwen/serve.py b/tools/qwen/serve.py
index abd79b6..a9389dd 100644
--- a/tools/qwen/serve.py
+++ b/tools/qwen/serve.py
@@ -595,6 +595,10 @@ SERVE_ENV = (("EXL3_HOST_LEAN", "1"), ("EXL3_HOST_CUTS", "1"), # batched verif
# checkpoint the decode state at the end of every answer, so the next chat turn only prefills
# the new message (without it an answer shorter than the 2048-token grid is prefilled again). 0 = off.
("EXL3_ROLL_CKPT", "1"),
+ # recurrent-state stashes (mid-prompt checkpoint, prompt-end tail) copy device -> pinned host without a host sync (a worker
+ # thread settles them before any reader): the 72 per-layer .cpu() syncs of an 8K prompt drained the queue twice (idle 55-64 ms
+ # of a 5.2 s prefill); bitwise (first-token + tail-resume logits equal, base vs on). 0 = the synchronous stash.
+ ("EXL3_NC2_ASYNC_STASH", "1"),
# a turn that resumes mid-page (tail checkpoint) and adds 1024..4095 tokens in one chunk no longer pays a separate
# last-page forward: follow-up after a 12K conversation 1.47 -> 1.26 s (+1536 tokens), 2.37 -> 2.11 s (+3072), first-token
# KLD vs a cold prefill 2.1e-3 / 1.9e-3 (shipped 4.8e-3 / 3.8e-3), top-1 equal; 768 and 6144 appends unchanged (bitwise).