Repository navigation
[Engine] Inkling 2 Mini: several requests at once (batch slots, parallel) - #226
Draft
okada-shogo wants to merge 7 commits into
Draft
okada-shogo wants to merge 7 commits into
okada-shogo wants to merge 7 commits into
Conversation
Inkling 2 no longer refuses --batch: up to 2 sequences beside the first (not with --draft-model, whose caches and block are one sequence's, and not on Metal yet). Its step() rows go through one head (batch_heads()): the products, the depth mixes, the loop's messages, the norms and the router run once for all rows, so a step reads the dense weights once. What reads or writes a sequence's state goes a row at a time with the launches one token alone has (ink_qkv(), ink_attend() sized by the row's own room, ink_conv_add()), each with its own sequence's slot (run_slot(): seq_buf(), seq_state()) and its position from at_. The --loop-bf16 controllers' gates and the FFN controller also go a row at a time with a token's launches, since their few-token launches are other kernels. Engine::step() now takes the layers in next_layer()'s order, as run() does, so a looped core runs its passes in a step too. A new hook, tail_by_rows(), keeps step()'s rows through layer_tail() and records a tail by its rows' sequences (Layer::rows_tails), for Inkling 2's convolution of the experts' sum, whose state is a sequence's own; such a step is not split into halves. The other families do not set it: their paths are unchanged. Inkling 2's tail recordings in run() are kept a sequence apiece (tail_key()), since Layer::tail is shared by the sequences and holds the bound one's state; with one sequence the keys are the ones before. Class: same bits for one request (--batch 0 runs the launches it ran before). With slots, each row's per-sequence steps are a token's own launches, so they give it the bits it gets alone; the shared products take the few-token kernels main documents as giving each row its single-row bits (lin()/mmvq, gemv_set(), the norms and mixes). The batch-slot design follows our reference (fork branch inkling2-batch, d525c9a) on VecSzn's Inkling 2 engine and his step() machinery (batch_heads(), rows_heads, next_layer()). Built for CUDA (sm_75). HIP (inkling2.cpp and engine.cpp are in the HIP build) and Metal not built here. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
With --check-batch K, sequence j's prompt is the first n >> 3 (K - 1 - j) of the --tokens' n (16 at least), read when the others have taken j * max_new / (2K) steps, and it writes max_new - j * max_new / K tokens: prompts of different lengths (so rows of a step at different positions and attention sizes), sequences that join between steps, and steps of fewer rows as they leave. Without it --check-batch is as before. A test of the engine's step() for every family; ported from our reference branch (inkling2-batch, d525c9a). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Inkling 2 takes --batch up to the engine's 16 (kInkRows, 32, is the most rows a launch takes). The steps of step()'s rows that read or write a sequence's state were a launch a row; they are now one launch for all rows (the _rows kernels), each row with its own state and cache pointers: - ink_qkv_rows(): the K and V convolutions, the clips and the cache stores, a row a blockIdx.y. - ink_attend_rows(): scores, softmax, values and join over each row's own caches. The launch is sized for the largest room among the rows; each row's join adds only the value parts it has alone (InkAttnRows::parts, from its own room, or its position and itself when not recorded). A row's scores and parts do not depend on the launch's size, so its bits are its own launch's. - ink_conv_add_rows(): the attention's and the experts' output convolutions. The kernels' bodies moved, unchanged, into __device__ functions that the one-token launches and the _rows launches both call. The rows read their positions from at_, which step() writes every step. Not on Metal, which has no _rows kernels and refuses --batch for Inkling 2. The loop controllers stay a row at a time with a token's launches. Class: same bits. One request runs the same kernel code; each slot's row gets the bits its own launch gave it. Built for CUDA (sm_75). HIP (inkling2.cu is HIP-compiled) not built here. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Inkling 2 Mini's family is "one_request": "mac": setup.py no longer writes "parallel": 1 for it, so --parallel N works on Linux and Windows as for the other strata-poly families (written when asked, with poly_parallel_note()). setup_mac.py still writes 1 (the engine refuses --batch for this model on Metal). test_setup_inkling.py checks both. docs/BATCHING.md lists the model; docs/models/inkling.md says requests at once are opt-in on NVIDIA, not with the draft file, and one at a time on a Mac. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Engine::step() with one row (a batch slot alone in a step: the first request of a server with "parallel" set, or --check-stagger's steps before the others join and after they leave) routed it on the card, since card_moe(1) says yes for the doorbell (bell_on(): Inkling 2 with experts outside VRAM), but step() never set bell_. moe_card() then took the mirror's branch (far_plan(), far_pull()) with the far table that card_moe() leaves unfilled when it counts on the doorbell, and read expert pointers that are not there: "step: an illegal memory access was encountered" (--check-batch 4 --check-stagger, --no-graphs too, and the server's first request at parallel 2 and 4, which answered HTTP 400). With --batch 0 step() is not used, so one request was not hit. step() now sets bell_ = bell_on(R) as run() does, keys its whole recording by it, and answers the rings with bell_serve(): after the whole recording's launch, or layer by layer after the card's experts, as run() does. bell_on() is false for every family but Inkling 2 (bells()), and for R > 1: other families' steps, and Inkling 2's steps of several rows, launch what they launched before. Class: same bits. A one-row step now takes the experts the way the same token alone takes them (the doorbell, the card's picks). The doorbell is VecSzn's (202c1ad, after Strata v0.2.19's Inkling hybrid mode). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
With the doorbell (bell_on(): Inkling 2 with experts outside VRAM) a token alone is routed on the card: route_card(), whose sigmoid is the card's expf. A step of several rows went to moe(), which routed each row on the host: route(), the same formula with the host's std::exp. The two need not round alike in the last bit, so a row's expert weights (and, at a near tie, an expert) could differ from the ones it got alone, and greedy answers parted after some tokens: --check-batch 4 and 6 gave "DIFF" (cb4-iq4 sequence 4 at token 20 of 64, cb6 3 sequences), and the small-card arguments and UD-Q2_K_XL more often. At 2 slots no token happened to change. Where a token alone routes on the card, step() now routes its rows there too before the router's event (route_card() of R rows: a row a warp, a row's bits its own), copies the picks and weights to pinned memory (h_ids_, h_wts_, the latter only for bells() with --batch), and moe() takes them in place of route() for those rows (step_ids_). The rest of moe() is as before. Not with --cache-bias, whose host route() biases the picks, and not on HIP or Metal (no doorbell there, so a token alone routes on the host as the rows do). Class: same bits as a token alone; one request (--batch 0) and the other families do not reach the new code (h_wts_ is null for them). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
step() records Inkling 2's heads and tails (Layer::rows_heads,
rows_tails) and whole steps (step_passes_) by their rows' sequences and
rooms. With slots that come and go, every new set of rows made new
recordings, and nothing dropped them: on the RTX 2070 Super, four chats
at "parallel" 4 with a 32K context filled the card until
cudaGraphInstantiate failed ("out of memory"), and the engine ended.
Where the tails take the rows (tail_by_rows(): Inkling 2), step() now
goes through step_replay():
- each recording's VRAM is what cudaMemGetInfo() lost across its
cudaGraphInstantiate; together they are held to a budget, a 64th of
the card from 64 to 256 MiB (128 on an 8 GB card), or
STRATA_POLY_STEP_GRAPH_MIB. Past it the recordings launched longest
ago go (never the step's own), down to three quarters of it;
- an instantiation that fails for memory drops every other step
recording and tries once more; failing still, the step's work runs
unrecorded, and no step records for the next 256 steps;
- a line "step recordings: N held, X MiB (Y KiB each)" each time the
count doubles from 256, for sizing the budget.
The other families' step() records and replays as before (the same
maps, record() and cudaGraphLaunch()).
Class: same bits. A recording replays the launches it records, and an
unrecorded run makes the same launches with positions by value.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem and result
Inkling 2 Mini refuses
--batchtoday (inkling2.cpp: "runs one sequence"), so the server answers one request at atime and setup writes
"parallel": 1. This PR lets it take batch slots like the other strata-poly models: up to 16slots in all (the server uses up to 8, docs/BATCHING.md), a token of each slot in one
step().--batch 0,"parallel": 1) runs the same launches as main: same tokens and top-5 probabilities.--check-batch, every token, 2 to 6 slots).--draft-model; not on Metal yet (refused there; setup keeps"parallel": 1on a Mac).Throughput (tok/s, all chats together, higher is better). RTX 2070 Super 8 GB, IQ4_NL, fp16 cache, 32K context,
the server (
terminal/serve), 300 tokens a chat, greedy. Each cell is the mean of 3 rounds. main answers one request ata time, so the 2 and 4 chat rows are compared with main's 1-chat rate.
"parallel": 1"parallel": 2"parallel": 4"parallel": 4The last row is slower because 4 slots' caches leave fewer experts in VRAM (649 of 3,072, against 921).
Each run (tok/s)
"parallel": 1"parallel": 1"parallel": 2"parallel": 4"parallel": 4Experts in VRAM: 921 (main) and 922 (this PR) at
"parallel": 1, 795 at 2, 649 at 4. Per-chat rates at 4 chats:13.0 to 14.3 tok/s.
Identity.
--batch 0: tokens + top-5 of 64 steps, IQ4_NL and UD-Q2_K_XL, expert cache frozen, all experts on the CPU,--no-graphs, small-card args--check-batch2, 3, 4, 6 slots;--check-stagger2 and 4 slots (with and without graphs); small-card args; UD-Q2_K_XL--check-batch4 and 6 slots,--check-stagger4 slots, small-card args, UD-Q2_K_XL; also with a 1 MiB recording budget--check-batch 4 --tail-skip 4Related discussion
Split out of #200, as the maintainer asked there:
#200 (comment)
Independent of the other split PRs (based on main, not stacked).
Approach
step()takes Inkling 2's rows in one head (batch_heads()): the dense products, mixes, norms and router run oncefor all rows. What reads or writes a sequence's own state (K/V convolutions, cache stores, attention, the output
convolutions) uses the one-token kernel code with each row's own slot, in one launch for all rows (
_rowskernels).step()never rang it, somoe_card()read anunfilled table ("illegal memory access").
step()now rings and answers the doorbell asrun()does.expf), a step's rows on the host (std::exp), which can differ in the lastbit. The rows now take the card's picks too.
cudaGraphInstantiateran outof memory (4 chats on 8 GB). They are now held to a VRAM budget (least recently used dropped first; measured
224-248 KiB each), and a recording the card has no memory for runs unrecorded.
Commit by commit
step()walks the layers innext_layer()order, asrun()does (other families:il + 1). Newhook
tail_by_rows()(default false, only Inkling 2 sets it).tail_key()includes the bound sequence.--check-stagger(test tool, every family): with--check-batch K, prompts and answers of different lengths,sequences that join between steps. Changes no engine path.
ink_qkv_rows(),ink_attend_rows(),ink_conv_add_rows()call theone-token kernel bodies with a row per
blockIdx.yand each row's own pointers."one_request": "mac", so--parallel Nworks on Linux/Windows; docs/BATCHING.md anddocs/models/inkling.md.
bell_on()is false for other families and for more rows).route_card(),h_wts_; only with the doorbell).STRATA_POLY_STEP_GRAPH_MIB);instantiate out of memory: drop the others, retry once, else run unrecorded. Only where
tail_by_rows().Validation
Passed:
cd terminal && ../.venv/bin/python -m unittest tools.test_setup_inkling tools.test_setup_mac tools.test_setup_menu tools.test_setup_choices tools.test_setup_amd serve.test_parallel: 282 tests OK (at commit 4; commits 5-7 touch only engine.cpp/engine.hpp);test_setup_inkling.pyagain at the head: OK.--check-batch(table above):strata-poly --tokens @ids302.txt --max-new 64 --top-probs 5 --cpu-share 0 --kv fp16withSTRATA_FREEZE_EXPERTS=1(A/B), and--batch K --check-batch K [--check-stagger](slots), RTX 2080 Ti 11 GB and RTX 2070 8 GB, sm_75 binaries.terminal/serve/server.pywith"parallel"1/2/4, 1/2/4 concurrent chats, 3 rounds after a warm-up request.Failed (first run, fixed by commits 5-7 and passed above):
--check-batch4 and 6 differed after some tokens;--check-staggerand the server at"parallel"2 and 4 stopped with "illegal memory access"; 4 chats at"parallel"4 stopped withcudaGraphInstantiate: out of memory.Not run:
--check-staggercase: queued (a first, larger run timed out).inkling2.cualready fails (10__ldcs/__ldcgerrors in code this PR does not touch; fixed by [Engine] build Inkling 2's DSpark screen on HIP #206).engine.cppwith this PR builds for HIP; a full HIP build waits for [Engine] build Inkling 2's DSpark screen on HIP #206. No AMD run.--batchfor this model); FP8 caches (Ampere+); cards other than sm_75._rowskernels against the one-token launch, bit for bit; the end-to-end--check-batchcovers them.Limits
--draft-model; not on Metal.--tail-skipcan make a slot differ from the same request alone (the skip is decided over all rows of a step)."parallel"4 is about 6% slower than"parallel"1 on an 8 GB card: the slots' cachestake VRAM from the expert cache.
--layer-spliton Inkling 2): both PRs edit the sameneed(opt_.layer_split.empty())line. Whicheverlands second should keep
--batchand--layer-splitapart on Inkling 2 until that pair is tested.Credits: design ported from our own engine's batch slots (#200), onto VecSzn's engine and his
step()machinery(
batch_heads(),rows_heads,next_layer()) and his doorbell.🤖 Generated with Claude Code