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.

Scheduler: a prefill round ends at the first job that completes its prefill

YanissAmzf5e8b0132af2
diff --git a/exllamav3/generator/generator.py b/exllamav3/generator/generator.py
index 84cea04..cf31750 100644
--- a/exllamav3/generator/generator.py
+++ b/exllamav3/generator/generator.py
@@ -739,6 +739,29 @@ class Generator:
for job in self.active_jobs:
job._cil_hold = False
+ def _prefill_round(self, parked, results: list):
+ """One prefill chunk for each active job that is not parked. The first token of a job is sampled in the generation round
+ after this one, so jobs that finish their prefill here all wait for the longest prefill of the step: four sub-agents of one
+ parent got their first token together, after the sum of the four prefills (4.7 s) instead of one by one (1.4 s for the first).
+ The round therefore ends at the first job that completes its prefill while other jobs still have chunks to run; the
+ rest run in the next step (EXL3_PF_FIRST_OUT=0: the whole round every step)."""
+ stop_early = os.environ.get("EXL3_PF_FIRST_OUT", "1") != "0"
+ jobs = list(self.active_jobs)
+ for i, job in enumerate(jobs):
+ if job in parked:
+ continue
+ was_done = job.is_prefill_done()
+ try:
+ job.prefill(results)
+ except Exception as e:
+ if os.environ.get("KYO_TURN_TRACE"):
+ import traceback; traceback.print_exc()
+ self.reap_failed_job(job, e, results)
+ continue
+ if stop_early and not was_done and job.is_prefill_done() and \
+ any(j in self.active_jobs and j not in parked and not j.is_prefill_done() for j in jobs[i + 1:]):
+ break
+
def _iterate_inner(self) -> list[dict]:
"""
Performs inference on available jobs.
@@ -827,15 +850,7 @@ class Generator:
# Perform one round of prefill
_t0 = _ttrace_begin()
parked = self._cil_parked() if chunk_il.chunk_il_enabled() else ()
- for job in list(self.active_jobs):
- if job in parked:
- continue
- try:
- job.prefill(results)
- except Exception as e:
- if os.environ.get("KYO_TURN_TRACE"):
- import traceback; traceback.print_exc()
- self.reap_failed_job(job, e, results)
+ self._prefill_round(parked, results)
_ttrace_end("prefill total", _t0)
# Recurrent checkpoints
diff --git a/tools/qwen/test_prefill_share.py b/tools/qwen/test_prefill_share.py
index 83be0ab..1904b6c 100644
--- a/tools/qwen/test_prefill_share.py
+++ b/tools/qwen/test_prefill_share.py
@@ -27,9 +27,9 @@ def glue():
src = (ROOT / "exllamav3" / "generator" / "generator.py").read_text()
ns = {"os": os, "prefill_share": prefill_share, "PAGE_SIZE": PAGE}
for n in ast.walk(ast.parse(src)):
- if isinstance(n, ast.FunctionDef) and n.name in ("_prefill_leader", "iterate_start_jobs"):
+ if isinstance(n, ast.FunctionDef) and n.name in ("_prefill_leader", "iterate_start_jobs", "_prefill_round"):
exec(textwrap.dedent(ast.get_source_segment(src, n)), ns)
- return ns["_prefill_leader"], ns["iterate_start_jobs"]
+ return ns["_prefill_leader"], ns["iterate_start_jobs"], ns["_prefill_round"]
class Seq:
@@ -66,13 +66,32 @@ class FakeGen:
self.pending_jobs, self.active_jobs = list(pending), []
self.pagetable = types.SimpleNamespace(num_unreferenced_pages=lambda: 100)
- _prefill_leader, iterate_start_jobs = glue()
+ _prefill_leader, iterate_start_jobs, _prefill_round = glue()
+
+ def reap_failed_job(self, job, e, results):
+ self.active_jobs.remove(job)
def chain(n, tag="p"):
return [f"{tag}{i}" for i in range(n)]
+class PJob(FakeJob):
+ """prefill() advances `rows` of `need`; done once it got them all."""
+ def __init__(self, need, rows=1, fail=False):
+ super().__init__([])
+ self.need, self.have, self.rows, self.fail, self.ran = need, 0, rows, fail, 0
+
+ def is_prefill_done(self):
+ return self.have >= self.need
+
+ def prefill(self, results):
+ if self.fail:
+ raise RuntimeError("boom")
+ self.ran += 1
+ self.have += self.rows
+
+
class T(unittest.TestCase):
def setUp(self):
os.environ.pop("EXL3_PF_SHARE", None)
@@ -124,6 +143,44 @@ class T(unittest.TestCase):
self.assertEqual(len(g.active_jobs), 2)
self.assertEqual(g.active_jobs[1].sequences[0].page_hashes[0], "q0")
+ def _round(self, jobs, parked=()):
+ g = FakeGen([])
+ g.active_jobs = list(jobs)
+ g._prefill_round(parked, [])
+ return g
+
+ def test_round_ends_at_first_job_that_finishes_its_prefill(self):
+ a, b, c = PJob(1), PJob(1), PJob(1)
+ self._round([a, b, c])
+ self.assertEqual((a.ran, b.ran, c.ran), (1, 0, 0))
+
+ def test_round_runs_every_job_that_does_not_finish(self):
+ a, b, c = PJob(5), PJob(5), PJob(5)
+ self._round([a, b, c])
+ self.assertEqual((a.ran, b.ran, c.ran), (1, 1, 1))
+
+ def test_round_last_job_finishing_ends_nothing_early(self):
+ a, b = PJob(5), PJob(1)
+ self._round([a, b])
+ self.assertEqual((a.ran, b.ran), (1, 1))
+
+ def test_round_skips_parked_and_failed(self):
+ a, b, c = PJob(1), PJob(1), PJob(1)
+ g = self._round([a, b, c], parked=(b,))
+ self.assertEqual((a.ran, b.ran, c.ran), (1, 0, 0))
+ f, d = PJob(2, fail=True), PJob(2)
+ g = self._round([f, d])
+ self.assertEqual((d.ran, len(g.active_jobs)), (1, 1))
+
+ def test_round_off_switch(self):
+ os.environ["EXL3_PF_FIRST_OUT"] = "0"
+ try:
+ a, b, c = PJob(1), PJob(1), PJob(1)
+ self._round([a, b, c])
+ self.assertEqual((a.ran, b.ran, c.ran), (1, 1, 1))
+ finally:
+ del os.environ["EXL3_PF_FIRST_OUT"]
+
if __name__ == "__main__":
unittest.main()