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.

Prompt-end tail checkpoint: snapshot on the device, copy to the host on a side stream

YanissAmz21a480dbe74d
diff --git a/exllamav3/modules/gated_delta_net.py b/exllamav3/modules/gated_delta_net.py
index 4910e88..a03e5c6 100644
--- a/exllamav3/modules/gated_delta_net.py
+++ b/exllamav3/modules/gated_delta_net.py
@@ -240,6 +240,23 @@ class GDNState:
if not self.cache.model.loaded_tp:
layers = self.cache.get_all_recurrent_layers()
if async_ and all(isinstance(l, GDNLayerState) for l in layers.values()):
+ if os.environ.get("EXL3_STASH_SIDE", "0") == "1" and torch.cuda.is_available():
+ # Snapshot every layer's state on the device (one D2D copy each, 153 MB at 27B: ~0.4 ms) and run the D2H
+ # on a side stream: the copies (40 ms over a x4 link) overlap the first decode steps instead of delaying
+ # them, and the live state is free to advance because the copies read the snapshots.
+ snaps = {k: l.snapshot(self.slot) for k, l in layers.items()}
+ side = _stash_stream(self.cache)
+ side.wait_stream(torch.cuda.current_stream())
+ with torch.cuda.stream(side):
+ for k, l in layers.items():
+ stashed[k] = l.stash_from_snapshot(snaps[k], pin_pool)
+ ev = torch.cuda.Event()
+ ev.record(side)
+ for sn in snaps.values():
+ for t in sn:
+ t.record_stream(side)
+ stashed["_ev"] = ev
+ return stashed
for k, l in layers.items():
stashed[k] = l.stash_async(self.slot, pin_pool)
ev = torch.cuda.Event()
@@ -282,6 +299,17 @@ class GDNState:
self.position = 0
+_stash_streams = {}
+
+
+def _stash_stream(cache):
+ dev = torch.cuda.current_device()
+ st = _stash_streams.get(dev)
+ if st is None:
+ st = _stash_streams[dev] = torch.cuda.Stream()
+ return st
+
+
class GDNLayerState:
def __init__(
@@ -432,6 +460,23 @@ class GDNLayerState:
)
+ def snapshot(self, slot):
+ """Device copies of the committed recurrent state and the conv window (what stash() reads)."""
+ cdim = self.module.conv_kernel_size
+ b = _base_value.get((self.cache_id, slot), 0)
+ return (self.recurrent_state[slot, b:b + 1].clone(), self.conv_state[slot, :, :cdim].clone())
+
+
+ def stash_from_snapshot(self, snap, pin_pool: dict = None):
+ out = []
+ for src in snap:
+ free = None if pin_pool is None else pin_pool.get((tuple(src.shape), src.dtype))
+ dst = free.pop() if free else torch.empty(src.shape, dtype = src.dtype, pin_memory = True)
+ dst.copy_(src, non_blocking = True)
+ out.append(dst)
+ return tuple(out)
+
+
def stash_async(self, slot, pin_pool: dict = None):
"""Non-blocking D2H into pinned host tensors, reused from pin_pool ((shape, dtype) -> free list) when given:
a pinned allocation costs ~1 ms of host time, more than the blocking copy it replaces."""
diff --git a/tools/qwen/serve.py b/tools/qwen/serve.py
index f45f429..02abf1a 100644
--- a/tools/qwen/serve.py
+++ b/tools/qwen/serve.py
@@ -608,7 +608,10 @@ HIP_ONLY_SERVE_ENV = frozenset(("EXL3_BRANCH_CKPT", "EXL3_GDN_HF", "EXL3_MOE_WMM
# EXL3_PF_EMB_UPLOAD: a long prefill chunk gathers the CPU-resident embedding rows straight into pinned memory in the table dtype (bf16),
# uploads them asynchronously and widens on the device (bit-identical): 94 MB of pageable fp32 per 4.5K-row chunk plus the 47 MB of the MTP
# block (~37 ms of idle GPU per chunk) become two async 47 MB copies. Served prefill 4K 1712.8 -> 1729.0, 35K 1515.7 -> 1523.8 tok/s, text identical.
-CUDA_SERVE_ENV = (("EXL3_PF_EMB_UPLOAD", "1"), ("EXL3_CUDA_GRAPH", "1"), ("EXL3_DRAFT_VOCAB", "98304"), ("QWSPEC_THF", "0.3"), ("QWSPEC_THV", "0.1"), ("QWSPEC_THFD", "0.6"), ("QWSPEC_THVD", "0.4"))
+# EXL3_NC2_ASYNC_STASH + EXL3_STASH_SIDE: the prompt-end tail checkpoint (96 D2H copies, 153 MB, 40 ms over a x4 PCIe link) was queued on the
+# compute stream right before the first decode step; it is now a device snapshot (~0.4 ms) whose D2H runs on a side stream, settled by the
+# existing stash worker before any reader. First token 35-47 ms earlier on every prompt of 512+ tokens (tail_ckpt_min), text identical.
+CUDA_SERVE_ENV = (("EXL3_NC2_ASYNC_STASH", "1"), ("EXL3_STASH_SIDE", "1"), ("EXL3_PF_EMB_UPLOAD", "1"), ("EXL3_CUDA_GRAPH", "1"), ("EXL3_DRAFT_VOCAB", "98304"), ("QWSPEC_THF", "0.3"), ("QWSPEC_THV", "0.1"), ("QWSPEC_THFD", "0.6"), ("QWSPEC_THVD", "0.4"))
HC_GS_SIDECAR = "hc_gs_sidecar.safetensors"
# The sidecar of the published pack. A file with another hash (wrong pack, damaged download) is not used: plain group rounding, one log line.