[PR #557] DeepSeek: answer several conversations at once, scheduled on events, so sub-agents don't wait behind each other (first token 4.8x sooner on real agent traffic) #595

Open
opened 2026-10-29 21:27:18 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/mw00/project-maya/pull/557
Author: @mrsirg97-rgb
Created: 10/27/2026
Status: 🔄 Open

Base: main ← Head: ds4-evt


📝 Commits (9)

  • f751046 DeepSeek: several conversations at once (--parallel N) and decode gaps while a prompt is read (#332)
  • cb18af5 DeepSeek several conversations: the serve loop on events, prompts read shortest first
  • 38dccd9 DeepSeek several conversations: shortest remaining work first, measured
  • 4b28097 DeepSeek several conversations: docs, and a short prompt in a long read's gaps on a fresh engine
  • ca1dadc DeepSeek several conversations: the DSpark drafter in parallel, round-robin reads, the scheduling note and benches
  • d01773f DeepSeek several conversations: a piece each in turn by default, a one-piece prompt ahead of the longer reads
  • c3e1cc8 Merge upstream main (v1.1.32) into ds4-evt
  • 0752058 Merge upstream main (v1.1.33) into ds4-evt
  • d94914d DeepSeek several conversations: one flow setup for both prompt reads; the source tests after the merge

📊 Changes

16 files changed (+4352 additions, -248 deletions)

View changed files

📝 CHANGELOG.md (+40 -0)
📝 CMakeLists.txt (+6 -0)
➕ docs/DS4_SCHEDULING.md (+171 -0)
📝 docs/SETTINGS.md (+146 -1)
➕ src/ds4/ds4_evt.hpp (+162 -0)
📝 src/ds4/ds4_main.cpp (+979 -9)
📝 src/ds4/ds4_model.cu (+1064 -236)
📝 src/ds4/ds4_model.hpp (+54 -0)
➕ src/ds4/ds4_prefill_gaps.hpp (+51 -0)
➕ tests/ds4_evt_test.cpp (+148 -0)
➕ tests/ds4_prefill_gaps_test.cpp (+63 -0)
➕ tools/ds4/agent_bench.py (+280 -0)
➕ tools/ds4/parallel_test.py (+436 -0)
➕ tools/ds4/schedule_bench.py (+128 -0)
➕ tools/test_ds4_parallel.py (+622 -0)
📝 tools/test_ds4_prompt_skip.py (+2 -2)

📄 Description

DeepSeek: answer several conversations at once, scheduled on events, so sub-agents don't wait behind each other (first token 4.8x sooner on real agent traffic)

Closes #332 ("every other request waits minutes behind a 100K answer").

With --parallel N (2-8, a layer split with --threads on) the DeepSeek engine answers up to N conversations at
once: each has its own state, its answer's windows are pipelined over the split, a long prompt's read pauses at its
chunk boundaries so the others keep decoding, and a conversation that drafts answers in DSpark rounds exactly as it
does alone. --parallel 1 - the default - is the single path as before, token for token.

It builds on #356's conversation slots: a conversation's state is the pieces slot_save already takes (window rings,
compressor groups, compressed KV and index keys), now one set per lane, and a lane taken by another request sets its
conversation aside and takes it back the same way - so an agent's parent and sub-agents, which #356 stopped from
re-reading each other's prompts as they took turns, can now run at the same time.

Running several at once turned out to be the easy half. The half users feel is what runs next: one read takes
every GPU, so a 250-token question behind a 16,000-token prompt waits for all of it. The serve loop now runs on an
event queue in (priority, id) order, the prompts being read take turns a piece each, and the answers decode in the
read's gaps while the shortest remaining work is theirs - from measured rates, no tuning knobs.

Results

3-GPU split (2x RTX 5090 + RTX 3090), DeepSeek-V4.1-Flash GSQ-RCO IQ2_XXS (~138 GB, about half the experts in VRAM,
the rest on the CPU lane), --parallel 4, greedy.

Real agent traffic. tools/ds4/agent_bench.py --trace replays rig's .served log: four conversations' busiest
10 minutes (73 turns; a turn's new tokens 1-20,653, median 235; one conversation 217K tokens deep) with their real
gaps between turns. Against v1.1.28 as it runs today - one conversation at a time, the drafter on, conversations
kept - and against --parallel without the event loop:

v1.1.28, one at a time --parallel, no events this PR
first token, mean / p50 / p95 (s) 9.19 / 4.80 / 33.7 2.63 / 1.73 / 7.73 1.92 / 1.38 / 6.00
turn done, mean / p50 / p95 (s) 14.2 / 8.24 / 36.3 22.5 / 9.78 / 83.5 13.1 / 6.05 / 48.1
wall (s) 496 874 509

The first token comes 4.8x sooner on the mean, 5.6x at p95; a turn is done 27% sooner at the median - the same
work in about the same wall time, no turn waiting in a line. The cost: the longest turns (p95 done 48.1 s against
36.3), a long answer sharing the GPUs instead of having them.

The final build (with the one-piece rule below) against v1.1.28 again, on the window from 19:50 sharp (69 turns):

v1.1.28, one at a time this PR, final
first token, mean / p50 / p95 (s) 9.14 / 4.66 / 32.1 1.88 / 1.08 / 6.29
turn done, mean / p50 / p95 (s) 14.1 / 9.32 / 37.1 12.9 / 5.74 / 59.4
wall (s) 486 499

The first token 4.9x sooner on the mean, a turn done 38% sooner at the median; the longest turns 60% longer here. The drafter in parallel is most of the gap to
--parallel alone: without it the same build took 882 s.

An agent workload (agent_bench.py: 4 agents x 8 turns at once - a 4-12K-token first prompt, then tool results of
150-2,000 tokens, one in ten 5-20K - answers of 64-400 tokens, 0.5-5 s of tool time between), same build, only the
read order differing:

Read order First token mean / p95 (s) Turn done mean / p95 (s) Wall (s)
first come, first served 17.3 / 38.7 32.1 / 58.3 292
highest response ratio, reads held for answers 20.6 / 58.7 36.9 / 85.5 330
a piece each, in turn (default) 6.5 / 17.2 29.6 / 46.2 266

A short prompt that arrives during a long read - #332's case: a 256-token request sent while a 100,000-token
prompt is read gets all 16 of its answer tokens before the long read is through (4 lanes): a prompt whole in one
piece goes ahead of the longer reads, so it waits for the piece being read, not for the whole read. Before, it waited
for all of it.

The trade, measured (parallel_test.py --lanes 4 --measure: four 2,000-token conversations, N at a time). With
every prompt in one piece, the reads and answers interleave: the four are done sooner (21.9 s at N=4, 23.5 s at
N=1), and an answer that has begun is paused by each read after it (a conversation's own rate at N=4: 7.2 tok/s on the
mean, 34 alone). Agents see the first token sooner and the whole turn about as soon - the tables above.

What's in it

  1. Several conversations (--parallel N): per-conversation state, lane buffers and a mailbox ring each; windows
    pipelined over the split; decode gaps at a read's chunk boundaries (MAYA_DS4_PREFILL_GAPS). A helper GPU's lane
    does not run with N > 1 (the engine says so before the load).
  2. The serve loop on events (src/ds4/ds4_evt.hpp, the evt engine of
    rig, ported - one header, no dependency): the reader's lines, a token done
    (a host callback on the head GPU's stream, Model::multi_on_done) and the choice of the next read are events in
    (priority, id) order, run by one thread. A read, a drafting round or the expert cache's boundary takes the GPUs once
    every issued token is back (want_devices). Requests past the N states wait for a lane.
  3. Scheduling:
    • the prompts being read take turns, a piece each (a chunk per pipeline stage; a prompt whole in one piece goes
      ahead of the longer reads), the answers a token each between
      (MAYA_DS4_READ_ORDER=ratio: the highest response ratio, (wait + read) / read; arrival: first come);
    • at a read's chunk boundaries the answers decode while the soonest would end before the read does - an answer's
      length expected from the answers ended so far, rates measured;
    • a short prompt arriving mid-read is read in windows in a gap only where measured quicker than its turn in chunks.
  4. The DSpark drafter in parallel: a drafting conversation answers in rounds - the single path's round on its lane,
    drafts verified in one window - each round taking the GPUs in turn with the reads.
  5. Docs and benches: docs/SETTINGS.md (the settings), docs/DS4_SCHEDULING.md (how each rule was chosen, what
    was tried and did not work
    , the measurements); tools/ds4/agent_bench.py and tools/ds4/schedule_bench.py
    reproduce the tables.

Fixes that help everyone (N=1 too)

  • A 512-2047-token prompt went through windows at a sixth of the speed on a fresh engine: the choice between
    chunks and windows modelled a window from its experts' bytes and left out its fixed costs (the second prompt after a
    start: 100 tok/s against 600). It now uses what a full prompt window measured.
  • The PCIe lane's pulls were numbered per conversation: the fetch kernel's flags are its GPU's and wait for a
    value equal to the pull's, so two conversations' requests with the same number found the flags set before their
    experts were read (NaN rows, then an illegal address). A pull's number is now seq * conversations + conversation
    • one conversation's seq exactly.

Behaviour to know

  • A request past the N states waits for a lane (was: ERR).
  • With N > 1 the prompts are read a piece each in turn, not in arrival order (MAYA_DS4_READ_ORDER).
  • The decode gaps are adaptive by default (MAYA_DS4_PREFILL_GAPS=<percent> for a fixed share, 0 off) and,
    like GLM's, not bit-exact: a gap's decode can move experts a later chunk then finds elsewhere. The exactness
    checks run with the gaps off.
  • MAYA_DS4_PREFILL_MIN_CHUNKS (cut short reads so they have gap points) is off: on this split a cut read streams
    its routed experts again each chunk (a 4,000-token read ~12 s in four chunks, ~4 s in one).
  • Tried and left out: a drafting conversation's drafts in flight beside the others' windows (the drafter on its own
    streams). Exact, and drafting went from 75 ms a round to 3 - but no faster overall (74.9 s against 72.3 on 4 agents
    x 2 turns): the CPU lane, which every conversation shares, is the ceiling on this split. Where every expert fits in
    VRAM it may pay; docs/DS4_SCHEDULING.md has it.

Tested

  • tools/ds4/parallel_test.py --lanes 2 and --lanes 4, with and without --spec 1: every check passes - each
    conversation's greedy answer is token for token the one it gets alone (2 and 4 at once), a STOP ends one alone, kept
    conversations (slots) work per lane, and the reading check (all 16 of a small answer's tokens before a 100K read is
    through).
  • tests/ds4_evt_test.cpp (the queue's order, update, the engine from 8 threads, pump) and
    tests/ds4_prefill_gaps_test.cpp; ThreadSanitizer clean on the event engine.
  • One machine: a Linux 3-GPU split. Not tested: one GPU, AMD/HIP, Windows (ds4_evt.hpp is standard C++17
    threads; not built there yet), a split with every expert in VRAM.

Reproducing

python tools/ds4/parallel_test.py --pack <pack> --lanes 4 [--spec 1] [--measure]
python tools/ds4/agent_bench.py --pack <pack> --exe build/maya-ds4 [--spec]
python tools/ds4/agent_bench.py --pack <pack> --exe build/maya-ds4 --trace <rig .served> --spec
python tools/ds4/schedule_bench.py --pack <pack>

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/mw00/project-maya/pull/557 **Author:** [@mrsirg97-rgb](https://github.com/mrsirg97-rgb) **Created:** 10/27/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `ds4-evt` --- ### 📝 Commits (9) - [`f751046`](https://github.com/mw00/project-maya/commit/f75104614c2fed768d132e41bcf9361ac04c828b) DeepSeek: several conversations at once (--parallel N) and decode gaps while a prompt is read (#332) - [`cb18af5`](https://github.com/mw00/project-maya/commit/cb18af538a4b14ba748d41b6cc9aba5ffcffa1d7) DeepSeek several conversations: the serve loop on events, prompts read shortest first - [`38dccd9`](https://github.com/mw00/project-maya/commit/38dccd972bd7ab57ddf70226f6ef188d29ebc2a7) DeepSeek several conversations: shortest remaining work first, measured - [`4b28097`](https://github.com/mw00/project-maya/commit/4b280977be0cad533637d582c6bb2c0dd56b823a) DeepSeek several conversations: docs, and a short prompt in a long read's gaps on a fresh engine - [`ca1dadc`](https://github.com/mw00/project-maya/commit/ca1dadc4415d143285346416975fbd93d2e6a74e) DeepSeek several conversations: the DSpark drafter in parallel, round-robin reads, the scheduling note and benches - [`d01773f`](https://github.com/mw00/project-maya/commit/d01773f68906f404bf9a7c6a3cf2c18925e882b1) DeepSeek several conversations: a piece each in turn by default, a one-piece prompt ahead of the longer reads - [`c3e1cc8`](https://github.com/mw00/project-maya/commit/c3e1cc8ca18c3cd26a19947559bf147ea7417126) Merge upstream main (v1.1.32) into ds4-evt - [`0752058`](https://github.com/mw00/project-maya/commit/0752058c157fb43d5234a12ba14f634021d66a90) Merge upstream main (v1.1.33) into ds4-evt - [`d94914d`](https://github.com/mw00/project-maya/commit/d94914d902b6e3567147782129c877e810e33ac0) DeepSeek several conversations: one flow setup for both prompt reads; the source tests after the merge ### 📊 Changes **16 files changed** (+4352 additions, -248 deletions) <details> <summary>View changed files</summary> 📝 `CHANGELOG.md` (+40 -0) 📝 `CMakeLists.txt` (+6 -0) ➕ `docs/DS4_SCHEDULING.md` (+171 -0) 📝 `docs/SETTINGS.md` (+146 -1) ➕ `src/ds4/ds4_evt.hpp` (+162 -0) 📝 `src/ds4/ds4_main.cpp` (+979 -9) 📝 `src/ds4/ds4_model.cu` (+1064 -236) 📝 `src/ds4/ds4_model.hpp` (+54 -0) ➕ `src/ds4/ds4_prefill_gaps.hpp` (+51 -0) ➕ `tests/ds4_evt_test.cpp` (+148 -0) ➕ `tests/ds4_prefill_gaps_test.cpp` (+63 -0) ➕ `tools/ds4/agent_bench.py` (+280 -0) ➕ `tools/ds4/parallel_test.py` (+436 -0) ➕ `tools/ds4/schedule_bench.py` (+128 -0) ➕ `tools/test_ds4_parallel.py` (+622 -0) 📝 `tools/test_ds4_prompt_skip.py` (+2 -2) </details> ### 📄 Description # DeepSeek: answer several conversations at once, scheduled on events, so sub-agents don't wait behind each other (first token 4.8x sooner on real agent traffic) Closes #332 ("every other request waits minutes behind a 100K answer"). With `--parallel N` (2-8, a layer split with `--threads on`) the DeepSeek engine answers up to N conversations at once: each has its own state, its answer's windows are pipelined over the split, a long prompt's read pauses at its chunk boundaries so the others keep decoding, and a conversation that drafts answers in DSpark rounds exactly as it does alone. `--parallel 1` - the default - is the single path as before, token for token. It builds on #356's conversation slots: a conversation's state is the pieces `slot_save` already takes (window rings, compressor groups, compressed KV and index keys), now one set per lane, and a lane taken by another request sets its conversation aside and takes it back the same way - so an agent's parent and sub-agents, which #356 stopped from re-reading each other's prompts as they took turns, can now run at the same time. Running several at once turned out to be the easy half. The half users feel is **what runs next**: one read takes every GPU, so a 250-token question behind a 16,000-token prompt waits for all of it. The serve loop now runs on an event queue in (priority, id) order, the prompts being read take turns a piece each, and the answers decode in the read's gaps while the shortest remaining work is theirs - from measured rates, no tuning knobs. ## Results 3-GPU split (2x RTX 5090 + RTX 3090), DeepSeek-V4.1-Flash GSQ-RCO IQ2_XXS (~138 GB, about half the experts in VRAM, the rest on the CPU lane), `--parallel 4`, greedy. **Real agent traffic.** `tools/ds4/agent_bench.py --trace` replays rig's `.served` log: four conversations' busiest 10 minutes (73 turns; a turn's new tokens 1-20,653, median 235; one conversation 217K tokens deep) with their real gaps between turns. Against v1.1.28 as it runs today - one conversation at a time, the drafter on, conversations kept - and against `--parallel` without the event loop: | | v1.1.28, one at a time | `--parallel`, no events | **this PR** | | --- | ---: | ---: | ---: | | first token, mean / p50 / p95 (s) | 9.19 / 4.80 / 33.7 | 2.63 / 1.73 / 7.73 | **1.92 / 1.38 / 6.00** | | turn done, mean / p50 / p95 (s) | 14.2 / 8.24 / **36.3** | 22.5 / 9.78 / 83.5 | **13.1 / 6.05** / 48.1 | | wall (s) | **496** | 874 | 509 | The first token comes **4.8x sooner on the mean, 5.6x at p95**; a turn is done 27% sooner at the median - the same work in about the same wall time, no turn waiting in a line. **The cost**: the longest turns (p95 done 48.1 s against 36.3), a long answer sharing the GPUs instead of having them. The final build (with the one-piece rule below) against v1.1.28 again, on the window from 19:50 sharp (69 turns): | | v1.1.28, one at a time | **this PR, final** | | --- | ---: | ---: | | first token, mean / p50 / p95 (s) | 9.14 / 4.66 / 32.1 | **1.88 / 1.08 / 6.29** | | turn done, mean / p50 / p95 (s) | 14.1 / 9.32 / **37.1** | **12.9 / 5.74** / 59.4 | | wall (s) | **486** | 499 | The first token 4.9x sooner on the mean, a turn done 38% sooner at the median; the longest turns 60% longer here. The drafter in parallel is most of the gap to `--parallel` alone: without it the same build took 882 s. **An agent workload** (`agent_bench.py`: 4 agents x 8 turns at once - a 4-12K-token first prompt, then tool results of 150-2,000 tokens, one in ten 5-20K - answers of 64-400 tokens, 0.5-5 s of tool time between), same build, only the read order differing: | Read order | First token mean / p95 (s) | Turn done mean / p95 (s) | Wall (s) | | --- | ---: | ---: | ---: | | first come, first served | 17.3 / 38.7 | 32.1 / 58.3 | 292 | | highest response ratio, reads held for answers | 20.6 / 58.7 | 36.9 / 85.5 | 330 | | **a piece each, in turn (default)** | **6.5 / 17.2** | **29.6 / 46.2** | **266** | **A short prompt that arrives during a long read** - #332's case: a 256-token request sent while a 100,000-token prompt is read gets all 16 of its answer tokens before the long read is through (4 lanes): a prompt whole in one piece goes ahead of the longer reads, so it waits for the piece being read, not for the whole read. Before, it waited for all of it. **The trade, measured** (`parallel_test.py --lanes 4 --measure`: four 2,000-token conversations, N at a time). With every prompt in one piece, the reads and answers interleave: the four are done sooner (21.9 s at N=4, 23.5 s at N=1), and an answer that has begun is paused by each read after it (a conversation's own rate at N=4: 7.2 tok/s on the mean, 34 alone). Agents see the first token sooner and the whole turn about as soon - the tables above. ## What's in it 1. **Several conversations** (`--parallel N`): per-conversation state, lane buffers and a mailbox ring each; windows pipelined over the split; decode gaps at a read's chunk boundaries (`MAYA_DS4_PREFILL_GAPS`). A helper GPU's lane does not run with N > 1 (the engine says so before the load). 2. **The serve loop on events** (`src/ds4/ds4_evt.hpp`, the [evt](https://github.com/mrsirg97-rgb/evt) engine of [rig](https://github.com/mrsirg97-rgb/rig), ported - one header, no dependency): the reader's lines, a token done (a host callback on the head GPU's stream, `Model::multi_on_done`) and the choice of the next read are events in (priority, id) order, run by one thread. A read, a drafting round or the expert cache's boundary takes the GPUs once every issued token is back (`want_devices`). Requests past the N states wait for a lane. 3. **Scheduling**: - the prompts being read take turns, a piece each (a chunk per pipeline stage; a prompt whole in one piece goes ahead of the longer reads), the answers a token each between (`MAYA_DS4_READ_ORDER=ratio`: the highest response ratio, `(wait + read) / read`; `arrival`: first come); - at a read's chunk boundaries the answers decode while the soonest would end before the read does - an answer's length expected from the answers ended so far, rates measured; - a short prompt arriving mid-read is read in windows in a gap only where measured quicker than its turn in chunks. 4. **The DSpark drafter in parallel**: a drafting conversation answers in rounds - the single path's round on its lane, drafts verified in one window - each round taking the GPUs in turn with the reads. 5. **Docs and benches**: `docs/SETTINGS.md` (the settings), `docs/DS4_SCHEDULING.md` (how each rule was chosen, **what was tried and did not work**, the measurements); `tools/ds4/agent_bench.py` and `tools/ds4/schedule_bench.py` reproduce the tables. ## Fixes that help everyone (N=1 too) - **A 512-2047-token prompt went through windows at a sixth of the speed** on a fresh engine: the choice between chunks and windows modelled a window from its experts' bytes and left out its fixed costs (the second prompt after a start: 100 tok/s against 600). It now uses what a full prompt window measured. - **The PCIe lane's pulls were numbered per conversation**: the fetch kernel's flags are its GPU's and wait for a value equal to the pull's, so two conversations' requests with the same number found the flags set before their experts were read (NaN rows, then an illegal address). A pull's number is now `seq * conversations + conversation` - one conversation's `seq` exactly. ## Behaviour to know - A request past the N states **waits** for a lane (was: ERR). - With N > 1 the prompts are read **a piece each in turn**, not in arrival order (`MAYA_DS4_READ_ORDER`). - The decode gaps are **adaptive** by default (`MAYA_DS4_PREFILL_GAPS=<percent>` for a fixed share, `0` off) and, like GLM's, **not bit-exact**: a gap's decode can move experts a later chunk then finds elsewhere. The exactness checks run with the gaps off. - `MAYA_DS4_PREFILL_MIN_CHUNKS` (cut short reads so they have gap points) is **off**: on this split a cut read streams its routed experts again each chunk (a 4,000-token read ~12 s in four chunks, ~4 s in one). - Tried and left out: a drafting conversation's drafts in flight beside the others' windows (the drafter on its own streams). Exact, and drafting went from 75 ms a round to 3 - but no faster overall (74.9 s against 72.3 on 4 agents x 2 turns): the CPU lane, which every conversation shares, is the ceiling on this split. Where every expert fits in VRAM it may pay; `docs/DS4_SCHEDULING.md` has it. ## Tested - `tools/ds4/parallel_test.py --lanes 2` and `--lanes 4`, with and without `--spec 1`: every check passes - each conversation's greedy answer is token for token the one it gets alone (2 and 4 at once), a STOP ends one alone, kept conversations (slots) work per lane, and the reading check (all 16 of a small answer's tokens before a 100K read is through). - `tests/ds4_evt_test.cpp` (the queue's order, update, the engine from 8 threads, pump) and `tests/ds4_prefill_gaps_test.cpp`; ThreadSanitizer clean on the event engine. - One machine: a Linux 3-GPU split. **Not tested**: one GPU, AMD/HIP, Windows (`ds4_evt.hpp` is standard C++17 threads; not built there yet), a split with every expert in VRAM. ## Reproducing python tools/ds4/parallel_test.py --pack <pack> --lanes 4 [--spec 1] [--measure] python tools/ds4/agent_bench.py --pack <pack> --exe build/maya-ds4 [--spec] python tools/ds4/agent_bench.py --pack <pack> --exe build/maya-ds4 --trace <rig .served> --spec python tools/ds4/schedule_bench.py --pack <pack> --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No labels
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
mw00/project-maya#595
No description provided.