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.

Chunk interleave: decode priority (EXL3_CIL_DECODE_PRIO=1), a running stream waits one short chunk, not the whole big prefill

YanissAmz1196cfeefaee
diff --git a/exllamav3/generator/chunk_il.py b/exllamav3/generator/chunk_il.py
index f954a24..7b9846e 100644
--- a/exllamav3/generator/chunk_il.py
+++ b/exllamav3/generator/chunk_il.py
@@ -9,6 +9,12 @@ alternates with the queued short work of other jobs:
leader chunk : exactly one leader chunk; every other job is parked / held, so the chunk does not share its step with them
... repeat until the leader's deferral budget is used, then leader chunks only (= FIFO order) until its prefill is done.
+Decode priority (EXL3_CIL_DECODE_PRIO=1, for GPUs that decode several users in one batch): while another job is decode-ready the
+deferral budget does not apply and a leader chunk is not exclusive. The leader takes EXL3_CIL_DPRIO_CHUNK rows (default 768, about
+0.75 s on a 3090) per step and shares the step with the decode round of every ready job; between two leader chunks the ready jobs
+decode, all of them in one batch, for up to EXL3_CIL_SLICE_S. A running stream waits one chunk, not the whole big prefill; the
+leader keeps about two thirds of the GPU time. Without a decode-ready job the leader runs at full chunk size as before.
+
Hard bound: the leader's deferral (wall time of steps in which it did not run) is capped per leader by
min(EXL3_CIL_MAX_DEFER_S, EXL3_CIL_DEFER_FRAC x its estimated prefill time), so a smaller big prefill pays a proportionally
smaller price (default 8 s and 18 %: a 45K prefill of 36 s defers at most 6.5 s, an 8K one of 6.5 s at most 1.2 s). A slice step
@@ -45,6 +51,7 @@ class Plan(NamedTuple):
parked: set # keys that skip prefill this step
mode: str | None # None (no interleave), 'shorts', 'chunk'
leader: Any
+ chunk_rows: int = 0 # decode priority: prefill rows per job this step (0 = the generator's own chunk size)
class _Leader:
@@ -60,6 +67,7 @@ class _Leader:
self.rows_chunked = 0
self.t_start = now
self.rows = 0 # prompt rows left when it took the lead (budget base)
+ self.dprio_on = False # the last plan ran under decode priority (its slice time is not charged to the deferral budget)
class ChunkPlanner:
@@ -73,7 +81,9 @@ class ChunkPlanner:
self.slice_s = float(e("EXL3_CIL_SLICE_S", "0.6")) if slice_s is None else slice_s
self.row_s = float(e("EXL3_CIL_ROW_S", "0.0007")) if row_s is None else row_s # 2.9 s per 4096-row chunk until measured
self.decode_s = float(e("EXL3_CIL_DECODE_S", "0.05")) if decode_s is None else decode_s
- self.single_decode = (e("EXL3_CIL_SINGLE_DECODE", "1") != "0") if single_decode is None else single_decode
+ self.dprio = e("EXL3_CIL_DECODE_PRIO", "0") == "1"
+ self.dprio_chunk = max(256, int(e("EXL3_CIL_DPRIO_CHUNK", "768")) // 256 * 256)
+ self.single_decode = (e("EXL3_CIL_SINGLE_DECODE", "1") != "0" and not self.dprio) if single_decode is None else single_decode
self._clock = clock
self._lead: _Leader | None = None
self._resync = False
@@ -139,7 +149,8 @@ class ChunkPlanner:
ld.t_last, ld.kv_last, ld.mode = now, lv.kv, None
dt = max(now - ld.t_last, 0.0)
if ld.mode == "shorts":
- ld.used += dt
+ if not ld.dprio_on:
+ ld.used += dt
ld.slice_used += dt
elif ld.mode == "chunk" and lv.kv > ld.kv_last and dt > 0:
rows = lv.kv - ld.kv_last # the leader's own chunk(s) since the last step: measured s/row
@@ -150,8 +161,11 @@ class ChunkPlanner:
bigs_other = [v for v in others if not v.done and v.remaining > self.short_tok]
shorts = [v for v in others if v not in bigs_other]
budget = self._budget(ld)
+ # decode priority: with a decode-ready job beside the leader the shorts slice is not bounded by the deferral budget
+ dprio = self.dprio and any(v.done for v in others)
+ ld.dprio_on = dprio
# next mode
- run = self._pick(shorts, budget - ld.used)
+ run = self._pick(shorts, float("inf") if dprio else budget - ld.used)
fits = bool(run)
if ld.mode in (None, "chunk"):
if fits:
@@ -162,8 +176,8 @@ class ChunkPlanner:
elif not fits or ld.slice_used >= self.slice_s: # 'shorts' continues until its slice / the budget is spent
ld.mode = "chunk"
if ld.mode == "shorts":
- return Plan({ld.key} | {v.key for v in others if not v.done and v.key not in run}, "shorts", ld.key)
- return Plan({v.key for v in others if not v.done}, ld.mode, ld.key)
+ return Plan({ld.key} | {v.key for v in others if not v.done and v.key not in run}, "shorts", ld.key, self.dprio_chunk if dprio else 0)
+ return Plan({v.key for v in others if not v.done}, ld.mode, ld.key, self.dprio_chunk if dprio else 0)
# ---- generation round (after the prefill round: jobs that just finished their prefill are decode-ready)
def plan_decode(self, views: list[View], plan: Plan) -> set:
@@ -172,7 +186,8 @@ class ChunkPlanner:
return set()
ready = [v for v in views if v.done and v.key != plan.leader] # the leader (just finished) decodes at once
if plan.mode == "chunk":
- return {v.key for v in ready} # the leader chunk does not share its step with a decode
+ return set() if plan.chunk_rows else {v.key for v in ready} # decode priority: the decode round shares the leader's chunk step
+ # (otherwise the leader chunk does not share its step with a decode)
if not self.single_decode or len(ready) <= 1:
return set()
run = min(ready, key=lambda v: (self._last_decoded.get(v.key, -1), v.order)) # one batch-1 decode, least recently served
diff --git a/exllamav3/generator/generator.py b/exllamav3/generator/generator.py
index f3f22ca..8bf021c 100644
--- a/exllamav3/generator/generator.py
+++ b/exllamav3/generator/generator.py
@@ -691,8 +691,10 @@ class Generator:
for job in self.active_jobs:
job._cil_skip = False
job._cil_hold = False
+ base = getattr(self, "base_chunk_size", self.max_chunk_size)
if getattr(self, "_cil_broken", False) or len(self.active_jobs) < 2:
self._cil_plan = None
+ self.max_chunk_size = base # decode priority lowered it only beside a decode-ready job
if getattr(self, "_cil", None) is not None:
self._cil.solo() # these steps are not interleave time
return []
@@ -702,9 +704,12 @@ class Generator:
views = self._cil_views()
self._cil_plan = self._cil.plan_prefill(views)
parked = [j for j in self.active_jobs if id(j) in self._cil_plan.parked and not j.is_prefill_done()]
+ rows = getattr(self._cil_plan, "chunk_rows", 0)
+ self.max_chunk_size = min(rows, base) if rows else base # decode priority: a prefill step is short while others decode
except Exception as e: # noqa: BLE001
logging.warning("EXL3_SCHED_CHUNK_IL disabled (%r); serving without it", e)
self._cil_broken, self._cil_plan = True, None
+ self.max_chunk_size = base
return []
for job in parked:
job._cil_skip = True
diff --git a/tools/qwen/test_chunk_il.py b/tools/qwen/test_chunk_il.py
index f6e85bf..5e186d4 100644
--- a/tools/qwen/test_chunk_il.py
+++ b/tools/qwen/test_chunk_il.py
@@ -65,7 +65,8 @@ class FakeJob:
if seq.kv_position == self.n - 1:
self.t_prefill_done = self.sim.clk.t; return
rem = self.n - 1 - seq.kv_position
- end = self.n - 1 if rem <= CHUNK + MERGE else (seq.kv_position + CHUNK) // PAGE * PAGE # Job.prefill's chunk rule (shape)
+ chunk = self.sim.g.max_chunk_size # lowered at run time under decode priority
+ end = self.n - 1 if rem <= chunk + MERGE else (seq.kv_position + chunk) // PAGE * PAGE # Job.prefill's chunk rule (shape)
self.ranges.append((seq.kv_position, end))
self.sim.clk.t += (end - seq.kv_position) * ROW_S
seq.kv_position = end
@@ -78,7 +79,7 @@ class Sim:
def __init__(self, flag, **planner_kw):
self.clk = Clock()
self.flag = flag
- g = types.SimpleNamespace(active_jobs=[], max_chunk_size=CHUNK)
+ g = types.SimpleNamespace(active_jobs=[], max_chunk_size=CHUNK, base_chunk_size=CHUNK)
for k, f in load_glue().items():
setattr(g, k, types.MethodType(f, g))
g._cil = chunk_il.ChunkPlanner(clock=self.clk, **planner_kw) if flag else None
@@ -154,6 +155,56 @@ def solo_big_ttft(n=45000):
return a.t_prefill_done
+class DecodePriorityTests(unittest.TestCase):
+ """EXL3_CIL_DECODE_PRIO=1: a running stream waits one short chunk, not the whole big prefill (3 users on one 3090)."""
+
+ def tearDown(self):
+ os.environ.pop("EXL3_SCHED_CHUNK_IL", None)
+
+ def run_case(self, flag, **kw):
+ s = Sim(flag, **kw)
+ s.add("u1", 900, out=4000); s.add("u2", 900, out=4000)
+ big = []
+
+ def refill(sim):
+ if not big and sim.clk.t >= 3.0:
+ big.append(sim.add("A", 100000, 1))
+ if big and big[0].t_prefill_done is not None and sim.clk.t > big[0].t_prefill_done + 5:
+ sim.g.active_jobs.clear()
+ s.run(600, refill=refill)
+ a = big[0]
+ ends = [t1 for (t0, t1, pre, dec) in s.log if "u1" in dec and a.t_arrive <= t0 <= a.t_prefill_done]
+ gaps = [b - a_ for a_, b in zip(ends, ends[1:])]
+ toks = sum(1 for (t0, t1, pre, dec) in s.log if "u1" in dec and a.t_arrive <= t0 <= a.t_prefill_done)
+ return max(gaps), a.t_prefill_done - a.t_arrive, toks / (a.t_prefill_done - a.t_arrive), s
+
+ def test_stream_gap_bounded_and_big_prefill_not_starved(self):
+ solo = solo_big_ttft(100000)
+ gap0, t0_, _, _ = self.run_case(True) # decode priority off: leader chunks are exclusive
+ self.assertGreater(gap0, 3.0)
+ os.environ["EXL3_CIL_DECODE_PRIO"] = "1"
+ try:
+ gap1, t1_, rate1, s = self.run_case(True, slice_s=0.35)
+ finally:
+ os.environ.pop("EXL3_CIL_DECODE_PRIO", None)
+ self.assertLess(gap1, 1.5) # one 768-row chunk (the last one merges the tail, 1792 rows), never a 4096-row chunk
+ self.assertLess(t1_, 1.7 * solo) # the big prefill keeps about 60 % of the GPU
+ self.assertGreater(rate1, 10) # rounds per second of a stream during the big prefill (full speed 29: about 40 %)
+
+ def test_chunk_size_restored_when_nobody_decodes(self):
+ os.environ["EXL3_CIL_DECODE_PRIO"] = "1"
+ try:
+ s = Sim(True)
+ s.add("A", 30000, out=1)
+ s.add("u1", 900, out=3)
+ s.run(60)
+ s.add("B", 5000, out=1) # alone: the first step of a lone job restores the generator's chunk size before its prefill
+ s.step()
+ self.assertEqual(s.g.max_chunk_size, CHUNK)
+ finally:
+ os.environ.pop("EXL3_CIL_DECODE_PRIO", None)
+
+
class InterleaveTests(unittest.TestCase):
def tearDown(self):
os.environ.pop("EXL3_SCHED_CHUNK_IL", None)