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.

Grouped KQ MoE: return rows in the caller's [jobs, q, K] shape; add multi-session graph test

YanissAmz85fd82c4e0f8
diff --git a/exllamav3/modules/block_sparse_mlp.py b/exllamav3/modules/block_sparse_mlp.py
index 9f4f5f8..f6fdaa5 100644
--- a/exllamav3/modules/block_sparse_mlp.py
+++ b/exllamav3/modules/block_sparse_mlp.py
@@ -1839,7 +1839,7 @@ class BlockSparseMLP(BlockSparseMLP_CPU, Module):
ext.fq_moe_kq_grouped_mixed_q8(h, sel, wts, gate_ptrs, up_ptrs, down_ptrs,
gate_stride, up_stride, down_stride, _FID[fmt], _FID[fmt_d],
partial_v, routed_v)
- final_hidden_states = routed_v
+ final_hidden_states = routed_v.view(x.shape) if R > 1 else routed_v # rows -> [jobs, q, K] like the per-expert path
if self.shared_experts is not None:
se_params = {**params, "moe_valu": True} if _MOE_VALU else params
se = self.shared_experts.forward(x, se_params)
diff --git a/tests/test_gguf_multisession_graph.py b/tests/test_gguf_multisession_graph.py
new file mode 100644
index 0000000..e7824fb
--- /dev/null
+++ b/tests/test_gguf_multisession_graph.py
@@ -0,0 +1,58 @@
+"""Multi-session decode on a foreign GGUF: eager vs CUDA graph (one job per graph) vs CUDA graph MULTI (several jobs per graph).
+Aggregate tok/s with U concurrent sessions, and token agreement of every session against eager.
+
+ FMOE_GGUF=<file.gguf> [FMOE_USERS=4] python tests/test_gguf_multisession_graph.py
+"""
+import os, sys, time
+import torch
+
+ENGINE = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
+sys.path.insert(0, ENGINE)
+from exllamav3 import Model, Config, Cache, Generator, Job, GreedySampler, Tokenizer
+from exllamav3.loader.gguf_config import write_config_dir
+from exllamav3.model import cuda_graph as cg
+
+src = os.environ["FMOE_GGUF"]
+U = int(os.environ.get("FMOE_USERS", "4"))
+d = write_config_dir(src, os.path.join(os.environ.get("FMOE_CFG", "/tmp"), "qwen35moe_cfg"))
+config = Config.from_directory(d)
+model = Model.from_config(config)
+cache = Cache(model, max_num_tokens=8192)
+model.load()
+tok = Tokenizer.from_config(config)
+gen = Generator(model=model, cache=cache, tokenizer=tok)
+prompts = ["The capital of France is a city that", "Water boils at a temperature of", "In 1969 the first humans",
+ "The quick brown fox jumps over", "Photosynthesis is the process by which", "The largest planet in the solar system",
+ "A binary search tree is a structure", "Mount Everest is located in"][:U]
+N = 48
+
+
+def run():
+ jobs = [Job(input_ids=tok.encode(p), max_new_tokens=N, sampler=GreedySampler(), stop_conditions=[], identifier=i)
+ for i, p in enumerate(prompts)]
+ for j in jobs:
+ gen.enqueue(j)
+ out = {i: [] for i in range(len(jobs))}
+ torch.cuda.synchronize(); t = time.time()
+ while gen.num_remaining_jobs():
+ for r in gen.iterate():
+ if r["stage"] == "streaming" and "token_ids" in r:
+ out[r["identifier"]] += r["token_ids"].flatten().tolist()
+ torch.cuda.synchronize()
+ return out, time.time() - t
+
+
+arms = [("eager", False, False), ("graph-single", True, False), ("graph-multi", True, True)]
+ref = None
+for name, on, multi in arms:
+ cg.CUDA_GRAPH_ENABLED = on
+ cg.set_multi(multi)
+ cg.purge(model)
+ run() # warm / capture
+ out, dt = run()
+ n = sum(len(v) for v in out.values())
+ if ref is None:
+ ref = out
+ agree = sum(a == b for i in out for a, b in zip(out[i], ref[i]))
+ print(f"{name:13s} users={U} {n} tok in {dt:.2f}s = {n / dt:.1f} tok/s aggregate, agreement vs eager {agree}/{n}")
+print("PASS")