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: checkpoint the recurrent state where a prompt's cached pages end (EXL3_BRANCH_CKPT), on by default for Qwen

YanissAmz0606ae3e82b8
diff --git a/exllamav3/generator/job.py b/exllamav3/generator/job.py
index d09e56f..8bc2eab 100644
--- a/exllamav3/generator/job.py
+++ b/exllamav3/generator/job.py
@@ -1418,6 +1418,20 @@ class Job:
recurrent_last_page = True
prefill_ids = seq.sequence_ids.torch_slice(prefill_start, prefill_end)
+ # EXL3_BRANCH_CKPT=1 (default off, read per chunk; GDN no-tail and KDA big-tail paths): the chunk replays prompt
+ # pages whose K/V were found by hash but whose recurrent state was not (the resume fell back to an older checkpoint,
+ # e.g. a new conversation sharing a long system prompt with a cached one). The kernel also side-writes the state at
+ # the end of that shared run, so the next prompt with the same prefix resumes there instead of replaying up to a chunk
+ branch_row = None
+ if (gdn_nt or big_tail) and self.recurrent_state is not None and prefill_start % PAGE_SIZE == 0 and \
+ os.environ.get("EXL3_BRANCH_CKPT", "0") == "1":
+ q = prefill_start // PAGE_SIZE
+ while q < len(seq.allocated_pages) and (q + 1) * PAGE_SIZE < prefill_end and \
+ seq.allocated_pages[q].kv_position == PAGE_SIZE:
+ q += 1
+ if q * PAGE_SIZE > prefill_start and q * PAGE_SIZE - prefill_start != no_tail_row:
+ branch_row = q * PAGE_SIZE - prefill_start
+
# Exact multimodal chunking (DeepSeek-V4 vision): an image span is prefilled as
# exactly one chunk (non-causal within itself) and never re-fed, since the
# ring / compressor / pool states are streams. Text chunks end at a span start and
@@ -1499,10 +1513,11 @@ class Job:
# EXL3_MIDCHUNK_CKPT=1 (default off): a chunk that crosses a recurrent checkpoint boundary also
# saves the recurrent state at that boundary (KDA layers split conv + core there), stashed below
if self.recurrent_state is not None and not self.embeddings and \
- (os.environ.get("EXL3_MIDCHUNK_CKPT", "0") in ("1", "2") or (gdn_nt and no_tail_row is not None)) and \
+ (os.environ.get("EXL3_MIDCHUNK_CKPT", "0") in ("1", "2") or
+ (gdn_nt and (no_tail_row is not None or branch_row is not None))) and \
not self.generator.model.loaded_tp and prefill_start % PAGE_SIZE == 0:
if gdn_nt:
- rows = [] if no_tail_row is None else [no_tail_row]
+ rows = sorted(r for r in (branch_row, no_tail_row) if r is not None) # the kernel side-writes 2
else:
rows = [
p - prefill_start
@@ -1511,6 +1526,11 @@ class Job:
]
if no_tail_row is not None:
rows = sorted(set(rows + [no_tail_row]))
+ # the branch row only where the kernel has a free slot (a list over the limit drops every row)
+ cap = (4 if os.environ.get("EXL3_NC2_CKPT4", "0") == "1" else
+ 2 if os.environ.get("EXL3_PF_NO_TAIL", "0") == "1" else 1)
+ if branch_row is not None and branch_row not in rows and len(rows) < cap:
+ rows = sorted(rows + [branch_row])
if rows:
params["checkpoint_rows"] = rows
# 1: split conv + KDA core (reference), 2: in-kernel S side-write + async D2H (the only GDN mode)
diff --git a/tools/qwen/serve.py b/tools/qwen/serve.py
index 203e519..70c34be 100644
--- a/tools/qwen/serve.py
+++ b/tools/qwen/serve.py
@@ -590,6 +590,11 @@ SERVE_ENV = (("EXL3_HOST_LEAN", "1"), ("EXL3_HOST_CUTS", "1"), # batched verif
# 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).
# Slot files keep the tail checkpoint, so a reload does not replay the gap. 1024 goes back.
("EXL3_TAIL_MAXGAP", "8192"),
+ # a prompt that shares a cached prefix but not its recurrent checkpoint (a new conversation on the same long system
+ # prompt) side-writes the state at the end of the shared pages, so the next one resumes there: first token after a
+ # shared 9K / 11K / 12.1K / 14K prefix 1.04 -> 0.60, 2.25 -> 0.80, 2.92 -> 0.63, 1.62 -> 0.74 s; the writing prompt is
+ # bitwise unchanged, KLD vs a cold prefill at the level of any resumed turn. 0 = off.
+ ("EXL3_BRANCH_CKPT", "1"),
# bit-identical prefill levers (hyper-connection apply folded into the next norm, MoE glue v2), served
# +2.4 % / +1.6 % / +1.8 % (fuse) and +0.9 / -0.1 / +0.3 % (glue) at 4K / 16K / 32K on bal. 0 = off.
("EXL3_PF_GR_FUSE", "1"), ("EXL3_MPW_GLUE", "1"),