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.

MoE: optional router top-k trace (EXL3_ROUTE_TRACE), diagnostic only

YanissAmz53fed509bea9
diff --git a/exllamav3/modules/block_sparse_mlp.py b/exllamav3/modules/block_sparse_mlp.py
index 2fbbbdc..e4a0f7a 100644
--- a/exllamav3/modules/block_sparse_mlp.py
+++ b/exllamav3/modules/block_sparse_mlp.py
@@ -238,6 +238,40 @@ class ExpertsCFG:
# Routed-weighted per-expert Hessians for the quantizer (see Linear.cap_rkey, convert_model.build_rwh_blends)
_RWH = os.environ.get("EXL3_RWH", "0") == "1"
+class _RouteTrace:
+ """Diagnostic (EXL3_ROUTE_TRACE=path): append every MoE layer's router top-k, one record per forward
+ (layer id, rows, k, segment, int16 experts). Read-only hook, outputs unchanged; SIGUSR1 starts a new segment
+ (= a new conversation). Syncs once per layer, run with graphs off."""
+ def __init__(self, path):
+ import signal, threading
+ self.f = open(path, "ab", buffering = 1 << 20)
+ self.keys = {}
+ self.seg = 0
+ self.kf = open(path + ".keys", "a")
+ self.lock = threading.Lock()
+ signal.signal(signal.SIGUSR1, self._seg)
+
+ def _seg(self, *a):
+ self.seg += 1
+ self.f.flush()
+
+ def record(self, key, bsz, sel):
+ import numpy as np
+ if torch.cuda.is_current_stream_capturing():
+ return
+ kid = self.keys.get(key)
+ if kid is None:
+ kid = self.keys[key] = len(self.keys)
+ self.kf.write(f"{kid} {key}\n"); self.kf.flush()
+ a = sel.detach().to(torch.int16).cpu().numpy().reshape(bsz, -1)
+ with self.lock:
+ np.array([kid, bsz, a.shape[1], self.seg], dtype = np.int32).tofile(self.f)
+ a.tofile(self.f)
+
+
+_ROUTE_TRACE = _RouteTrace(os.environ["EXL3_ROUTE_TRACE"]) if os.environ.get("EXL3_ROUTE_TRACE") else None
+
+
class BlockSparseMLP(BlockSparseMLP_CPU, Module):
def __init__(
@@ -2042,6 +2076,9 @@ class BlockSparseMLP(BlockSparseMLP_CPU, Module):
selected_experts = torch.empty((bsz, self.num_experts_per_tok), dtype = torch.long, device = self.device)
routing_weights = torch.empty((bsz, self.num_experts_per_tok), dtype = torch.half, device = self.device)
+ if _ROUTE_TRACE is not None:
+ _ROUTE_TRACE.record(self.key, bsz, selected_experts)
+
# Extra norm (Gemma4)
if self.routed_pre_norm:
y = self.routed_pre_norm.forward(y, params, out_dtype = torch.half)