// src/program/generate.cpp - P2.S6: `strata generate`. // // THE DRIVER, and the first program in this project that answers a question. Everything below it is a // component; this is the thing that composes them into a token: // // embed_row(token) -> 48 captured layer graphs (the CPU expert pool behind the doorbell) -> // lm_head(R) -> sample -> embed_row(next) -> ... // // WHAT IT IS NOT. There is no tokenizer here. `pack/full/tokenizer/` and `tools/strata_tokenizer.py` exist, // and a C++ BPE is Phase 1's deliverable rather than this program's, so the prompt arrives as IDS via // `--tokens`. That is not a placeholder: it is exactly what Gate C1 needs, because C1 compares logits against // llama.cpp on the SAME ids, and a tokenizer on only one side of that comparison is a second variable. // // AND IT IS PHASE 2, so hit rate is `h = 0` and the number it prints is slow on purpose // (`phase-2-correct-engine.md:5-9`). What it is FOR is the honest tok/s figure and the logit dump. #include "strata/core/expert_cache.hpp" #include "strata/core/expert_source.hpp" #include "strata/core/layer.hpp" #include "strata/core/layout.hpp" #include "strata/core/session.hpp" #include "strata/core/weights.hpp" #include "strata/kernels/cpu/expert.hpp" #include "strata/kernels/cpu/pool.hpp" #include "strata/kernels/cpu/expert_layout.hpp" #include "strata/kernels/ngram.hpp" #include "strata/kernels/s2_expert_grouped.hpp" #include "strata/kernels/sampler.hpp" #include "strata/kernels/shared_expert.hpp" #include "strata/kernels/native_moe.hpp" #include "strata/kernels/native_gdn.hpp" #include "strata/kernels/native_router.hpp" #include "strata/kernels/native_qsa.hpp" #include "strata/kernels/native_qsa_indexer.hpp" #include "strata/kernels/native_rope.hpp" #include "strata/kernels/mrope.hpp" #include "strata/kernels/kv_q4.hpp" #include "strata/kernels/qsa.hpp" #include "strata/core/native_head.hpp" #include "strata/core/verify.hpp" #include "strata/core/mtp.hpp" #include "strata/prefill/prefill.hpp" #include "strata/core/native_dense.hpp" #include "strata/program/logits_selection.hpp" #include "strata/program/conv_cache.hpp" #include "strata/spec/draft_policy.hpp" #include "strata/spec/suffix_drafter.hpp" #include "strata/kernels/cvec.hpp" #include "strata/core/progress.hpp" #ifndef NOMINMAX #define NOMINMAX // gguf_reader.hpp includes windows.h #endif #include "strata/artifact/gguf_reader.hpp" #if defined(_WIN32) #include #include #include #else #include #include #endif #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace { using Clock = std::chrono::steady_clock; struct Options { std::string pack = "pack/full"; std::vector tokens; // the prompt, PRE-TOKENIZED int64_t max_new = 16; int64_t max_context = 4096; bool greedy = true; uint64_t seed = 0; int top_k = 20; float top_p = 0.95f; float temperature = 1.0f; std::string dump_logits; // one line of logits per generated position int64_t logits_stride = 1; // storage selection; all prompt tokens remain conditioned /// **THE RESIDUAL, SO THE HEAD CAN BE CHECKED WITHOUT THE LAYERS.** /// /// C1 fails (LEDGER L116) and the pipeline is `embed -> 48 layers -> head`. Dumping `R` splits it in half: /// the head is one norm, two bf16 projections and one 794 MB GEMV, all of which can be recomputed in Python /// from the manifest. If Python agrees with the engine on the same `R`, the head is right and the layers /// are wrong; if it disagrees, the head is wrong. Nothing else in the engine can be split that cheaply. std::string ple_gguf; // the ORIGINAL second GGUF shard: the PLE table is not in the pack bool no_ple = false; // explicit diagnostic ablation; never a normal inference default bool stream_token = false; // R2.6 experiment: ordered work on the session stream bool check_logits = false; // optional full-vocabulary finite scan bool gr_fp32_activations = false; // pinned CUDA single-token BF16 activation contract bool gr_native_mmvf = false; // pinned projection reduction tree as well as FP32 inputs bool native_bf16 = false; // SSM gates, router and indexer projections only bool native_bf16_extra = false; // PLE value and shared expert scalar gate bool native_ple_key = false; // unchanged Q2_0 key and CUDA Q8_1 activations bool native_moe_combine = false; // pinned fused CUDA weighted reduction bool native_gdn = false; // pinned CUDA recurrence and preprocessing bool native_flash_attn_short = false; // diagnostic pinned attention, context <=256 bool native_qsa_indexer = false; // pinned F16 key cache and F32 pooling bool native_qsa = false; // pinned F32 QSA norms and gate bool native_rope = false; // pinned text-only CUDA rotary arithmetic bool native_ple_postops = false; // pinned PLE postprojection arithmetic bool native_router = false; // pinned fused 512-expert top-10 router bool cpu_oracle_q8_0 = false; // pinned x86 activation scales/codes at both expert stages std::string native_head_gguf; // native output.weight experiment; same model shard as the pack std::vector native_dense_gguf; // repeat for native GDN/QSA projection shards /// Plan v0.3 P1: the whole native arithmetic set as ONE switch (model shard 1). It enables exactly the /// combination recorded in bench/results/2026-09-23-attention-ple plus the native indexer, and never the /// <=256-token attention adapter. It becomes the default once P0 shows it is not slower. std::string native_preset; /// Plan v0.3 P2: how the n-gram table is read. Direct (default) = unbuffered SSD reads, table never in RAM. std::string ple_io = "direct"; int64_t ple_row_cache = 1 << 20; ///< bounded row cache (rows of 90 B); 0 disables int ple_inflight = 64; double ple_delay_us = 0; ///< fault injection: every row read completes no earlier than this bool ple_sync_submit = false; ///< A/B arm: submit reads on the token thread, no I/O worker std::string kv = "fp16"; ///< plan v0.3 P7: KV storage, fp16 (default) or int8 (half the VRAM) int64_t kv_resident = 0; ///< KV streaming: resident cells per QSA layer (0: all in VRAM) std::string dump_residual; /// The head input, `bb.mixed`. It exists so the head can be SPLIT: steps 1-4 (the per-stream norm, the two /// bf16 projections and the stream mean) recompute cheaply in Python, and only the 794 MB GEMV does not. std::string dump_mixed; /// One residual snapshot per layer per position: `n_layers * hc * n_embd` floats per position, appended in /// position order. This is the C1 BISECTION LADDER - it is what `llama-debug --tensor-filter l_last` prints /// for the reference, so the first layer whose `sum` diverges is the layer that holds the bug. It needs the /// captured path (the expert pool only exists there), so it is refused with `--no-capture`. std::string dump_layers; /// `2 * n_embd + 2 * hc` floats per layer per position: the attention half's block output, the MoE half's, /// and the two injection vectors. It separates `linear_attn_out-` from `ffn_out-`, which the residual /// ladder cannot. **CAPTURED INTO THE LAYER GRAPHS**, so it must be armed before `session_capture`. std::string dump_halves; /// P0.S8's routing trace, and a prerequisite the Phase 3 plan names explicitly. One record per layer per /// position: `int32 layer, int32 k, k int32 ids, k float weights`. It is what a hit-rate curve for a /// candidate VRAM expert cache is computed from, and it needs no new kernels - the doorbell already /// publishes exactly this much to pinned memory. std::string dump_routing; bool no_capture = false; // run the layers directly instead of replaying graphs bool no_pool = false; // skip the CPU expert pool: the GPU-only floor bool sync_every_layer = false; /// Per-stage CUDA-event timings inside the layer halves. `--no-capture` only: an event recorded inside a /// stream capture is silently dropped, so the captured path cannot carry this. bool stage_timing = false; /// Launch the 48 captured `pre` graphs back to back with no host work between them and report the pure GPU /// time per token. This is the only measurement that separates host-bound from GPU-bound, because the /// stage events include every gap where the GPU waited for the host. bool graph_only = false; bool gpu_only_full = false; ///< R0.3: pre + post + head, the true per-token GPU floor int pool_workers = 0; ///< R2.2: 0 = "all physical cores minus the host's"; >0 overrides /// R2.2's first half, as an A/B arm. **ON by default**, because the measurement that justifies it is the /// pool's own drain: 33.7 GB/s against 5/6 x 44.14 = 36.8 for five workers, on a machine whose sixth core /// is reserved for a host thread that has nothing to do while the drain runs. bool no_host_worker = false; bool mmap_experts = false; ///< R2.1: opt OUT of the resident arena, back to MapViewOfFile /// R4: slots of VRAM-resident experts. **0 = off, and off is the default.** /// **THE COMMENT THAT USED TO BE HERE WAS FALSE AND ROUND 328 MEASURED IT.** It said "the cache has no /// consumer yet - `moe_hit_grouped_s2` does not exist - so switching it on costs the fill traffic and /// saves nothing". The kernel exists (`src/kernels/cuda/s2_expert_grouped.cu`), it is wired at line ~660 /// via `expert_hit_run`, and switching the cache on **does** move work off the CPU pool: the drain fell /// **19.076 -> 10.312 ms/token** at 4096 per-layer slots, for **-2.7 ms/token** end to end. What was /// true is that the ADMISSION POLICY gave every slot to the first position, which is why the earlier /// measurement found nothing - see `expert_cache_per_layer`. int expert_cache = 0; bool expert_cache_cpu_order = false; /// **R4.2g. ROUND 328 MEASURED THAT THE GLOBAL ADMISSION POLICY CANNOT WORK, AND THIS IS THE FIX.** /// The default policy hands out slots in arrival order from one counter shared by all 48 layers, so the /// first `n_slots` distinct pairs - about 26 LAYERS OF POSITION 0 - take every slot and hits are confined /// to them. Measured at 256 slots: **1781 of 60000 = 2.97%**, against **21.4%** for 8 slots per layer and /// **70.4%** for 64, from `Memory/cache_allocation.py` on the same run's routing. Off by default. bool expert_cache_per_layer = false; /// The PLE gather's prefetch, as an A/B arm. The gather measured 2.10-2.61 ms/token because its sixteen /// row reads are sixteen SEPARATE page faults into a 26.8 GB mapping; see `ple_prefetch_enable`. bool no_ple_prefetch = false; /// R4.2e: a `profile.bin` from `tools/make_profile.py`. **When given, it decides residency instead of the /// compulsory-miss policy**, which is the whole point: a profile ranked by routing frequency over a whole /// trace is what the plan's `h = 0.6447` refers to, and compulsory-miss measured 0.4864 because it fills /// with whatever the prompt touched FIRST. Empty means no profile. std::string expert_profile; /// R4.2d: **ON by default**, because the measurement is unambiguous and the alternative is known-broken. /// Without it, 17 of 10,562 layers had the hit work done when the pool returned; with it, 9,190. The /// A/B arm is `--no-hit-poke`. bool no_hit_poke = false; /// R0.9: capture each layer as THREE graphs and time them from outside the capture, which is the only /// valid way to get a per-stage table on the real graph. Prints and exits; it is a measurement, not a run. bool gpu_stages = false; bool stats = false; bool shared_late = false; ///< plan v0.3 P3 A/B: shared expert inside post[l] (old order) bool keep_canonical = false; ///< plan v0.3 P1 A/B: load canonical copies of natively served tensors bool no_token_graph = false; ///< plan v0.3 P3 A/B: two graphs per layer instead of one per token bool no_fused_gr = false; ///< plan v0.3 P3 A/B: the six-kernel native gr_read + separate gr_write bool no_fast_attn = false; ///< plan v0.3 P3 A/B: gather + one-block-per-head QSA attention bool no_publish_kernel = false; ///< plan v0.3 P3 A/B: memcpy nodes for the doorbell and QSA step bool no_fused_gdn = false; ///< plan v0.3 P3 A/B: llama.cpp-layout GDN step + separate out norm bool no_fast_select = false; ///< plan v0.3 P7 A/B: FP64 row scores + bit-serial cell top-k /// Plan v0.3 P4: `--expert-cache auto` sizes the VRAM tier from what is free after the weights, the session /// and the KV state, minus this reserve for the graphs, the hit scratch and the head. int vram_reserve_mib = 700; /// Plan v0.3 P5: batched prompt processing in chunks of this many tokens (0 = the token path). int64_t prefill_chunk = 0; /// `--prefill auto`: the largest chunk (up to 8192) whose buffers the expert cache can lend. Every expert a chunk /// routes to is streamed once per chunk, so a bigger chunk streams fewer bytes per token (the "ubatch" effect). bool prefill_auto = false; bool no_split_rows = false; ///< plan v0.3 P4 A/B: one whole expert per pool thread /// Plan v0.3 P5: the prompt path borrows the top expert-cache slots for its buffers and refills them after /// the prompt (default); `--no-prefill-borrow` reserves the buffers' VRAM for the whole session instead. bool no_prefill_borrow = false; /// Plan v0.3 P5 validation: batch only positions [0, P) and run the rest of the prompt through the token path /// (teacher-forced), so the logits of positions >= P - which depend on the batched state - can be scored /// against the oracle at many positions. 0 = the whole prompt but the last position. int64_t prefill_until = 0; /// Plan v0.3 P6: after every processed position, append the residual after the last layer (hc x n_embd /// floats, the MTP draft head's input) to this file. Token path only. std::string dump_final_r; /// Plan v0.3 P6: speculative decoding with a verify window of this many tokens (the last accepted token and /// spec-1 drafts); 0 = plain decode. `spec_oracle` drafts from a token file (the expected continuation, for /// the exactness test); `spec_corrupt` N > 0 replaces every Nth draft with a wrong token. int spec = 0; std::string spec_oracle; int spec_corrupt = 0; /// Plan v0.3 P6: the MTP draft layer's runtime directory (tools/mtp_rt.py); drafts come from it. std::string mtp; int64_t mtp_window = 32768; ///< the draft layer attends to the last N cells (0 = every cell) /// Plan v0.3 P6: the share (0..1) of each layer's distinct missed experts the GPU reads over PCIe from the /// pinned arena while the CPU computes the rest (verify windows). double pcie_frac = -1.0; ///< < 0: the model's default (0.2 direct for the Q2_0 pack, 0.55 DMA for native packs) std::string pcie_mode = "auto"; ///< auto | dma | kernel | direct /// Plan v0.3 P6: every `adapt_every` rounds, swap up to `adapt_swaps` of the most-routed missing experts into /// the VRAM tier in place of the least-routed resident ones (decayed counts). 0 = static residency. int adapt_every = 4; /// Plan v0.3 P6: a draft enters the verify window only while every draft before it (and itself) has at least /// this probability under the draft layer; 0 = always --spec-1 drafts. double spec_min_p = 0.0; /// Stop when the model emits an end-of-turn token (<|endoftext|> 248044, <|im_end|> 248046, or --eos-ids). bool stop_eos = false; std::vector eos_ids = {248044, 248046}; bool spec_split = false; ///< opt-in split verify window (the overlap study: exact, ~7% slower) /// Plan v0.3 P8: stay resident and take requests on stdin (see the --serve block in main). bool serve = false; /// The vision path: keep a per-cell (t, h, w) rotary position table so --serve can take GENI requests. bool vision = false; int adapt_swaps = 96; /// --serve: how many conversation checkpoints to keep between requests (0 = every request reads its whole /// prompt again, the v0.1.2 behaviour). One is the GDN recurrence of the 36 layers, the QSA indexer tails and /// the PLE history (~118 MB of host RAM); the KV cache itself is positional and stays where it is. int prompt_cache = 6; /// --serve: also keep a checkpoint every N freshly read prompt tokens (0 = only at the last turn boundary) int64_t prompt_cache_every = 16384; /// --serve: the token that opens a chat turn (<|im_start|>). The last one in a prompt is where the chat's /// history ends and the new assistant turn begins, which is the checkpoint the next request can reuse. int64_t turn_token = 248045; /// --serve: a text part of the prompt of at most N tokens (a chat message, the assistant header, a short tool /// result) goes through the verify windows, S tokens at a time, instead of the batched prompt path (0 = always /// the batched path) int64_t short_read = 64; /// The suffix drafter (prompt lookup): when the text being written repeats an earlier stretch of the context (code /// edits, quoted input, tool-call JSON) by at least this many tokens, the window may be filled with what followed /// it there instead of the MTP's drafts, where the MTP's own first guess agrees and the draft policy expects it to /// pay (strata/spec/draft_policy.hpp). On by default; 0 = MTP only. int suffix_draft = 3; /// The MTP's own window cap (0 = --spec): with --spec 6 --mtp-max-t 4 the long windows come from suffix matches. int mtp_max_t = 0; /// A control vector on the residual stream (strata/kernels/cvec.hpp), with llama.cpp's flags: the /// `experimental-speed-projection` profile passes `--control-vector-scaled FILE:1.0 --control-vector-layer-range /// 4 44 --cvec-mode project --cvec-dir per-layer`. None by default; --serve switches a loaded one per request. std::vector> cvec_files; int cvec_first = -1, cvec_last = -1; ///< llama.cpp's defaults: 1 .. the last layer int cvec_mode = 1; ///< 0 = project, 1 = add (llama.cpp's default) int cvec_single = -1; ///< --cvec-dir single:L (project mode): layer L's direction everywhere }; void usage() { std::fprintf(stderr, "strata generate --pack DIR --tokens \"1,2,3\" [options]\n" "\n" " --pack DIR the pack directory (default pack/full)\n" " --tokens LIST the prompt as comma-separated token IDS (required)\n" " --tokens-file PATH pretokenized prompt, commas or whitespace (alternative to --tokens)\n" " --ple-gguf PATH required PLE table (original second GGUF shard)\n" " --no-ple explicit diagnostic ablation of the PLE layer\n" " --ple-io direct|mmap n-gram table reads (plan v0.3 P2). direct (default): unbuffered SSD\n" " reads, the table never enters RAM or the file cache; mmap: A/B arm\n" " --ple-row-cache N bounded cache of fetched rows, 90 B each (default 1048576; 0 = off)\n" " --ple-inflight N outstanding SSD reads (default 64)\n" " --ple-delay-us U fault injection: each row read completes no earlier than U us\n" " --ple-sync-submit A/B arm: submit table reads on the token thread (default: an I/O thread)\n" " --kv fp16|int8 KV storage (plan v0.3 P7): int8 codes + fp16 scale per 64 values, half the\n" " VRAM; default fp16 until gate G-C accepts int8\n" " --kv q4_0 4-bit K/V after a Hadamard rotation (PR #21): half of int8's memory,\n" " slightly lower precision (see bench/results/2026-09-27-kv-q4)\n" " --kv-resident N KV streaming: keep N cells of each QSA layer in VRAM (min 20480) and the\n" " whole K/V in pinned RAM; the freed VRAM goes to expert slots. 0 (default):\n" " all of it in VRAM. A context of N cells or fewer is not streamed\n" " --stream-token enqueue token work on the session stream (experimental)\n" " --check-logits copy and check all logits in the stream-token path\n" " --gr-fp32-activations experimental CUDA-oracle GR activation precision\n" " --gr-native-mmvf experimental pinned GR norm/projections; implies FP32 activations\n" " --native-bf16 experimental CUDA-oracle SSM/router/indexer BF16 projections\n" " --native-bf16-extra experimental CUDA-oracle PLE/shared gate BF16 projections\n" " --native-ple-key experimental native PLE key; requires --native-dense-gguf\n" " --native-moe-combine experimental pinned CUDA routed/shared combination\n" " --native-gdn experimental pinned CUDA GDN norms/gates/recurrence\n" " --native-flash-attn-short diagnostic pinned vector attention; --max-context <=256\n" " --native-qsa-indexer experimental pinned indexer key cache and pooling\n" " --native-qsa experimental pinned QSA normalization and output gate\n" " --native-rope experimental pinned text-only CUDA rotary arithmetic\n" " --native-ple-postops experimental pinned PLE postprojection arithmetic\n" " --native-router experimental pinned CUDA 512-expert top-10 routing\n" " --cpu-oracle-q8-0 experimental pinned CPU expert quantization and dot reduction\n" " --native SHARD1 every full-context native path at once (plan v0.3 P1): stream-token,\n" " GR MMVF, BF16, head, dense + PLE key, MoE combine, GDN, router, QSA,\n" " indexer, RoPE, PLE postops, and the CPU q8_0 contract unless the\n" " expert cache is on. Individual --native-* flags stay for A/B.\n" " --native-head-gguf PATH native Q5_K head from model shard 1; requires --stream-token\n" " --native-dense-gguf PATH native GDN/QSA/shared projections; repeat for each source model shard\n" " --expert-cache-cpu-order experimental GPU expert reduction matching CPU order\n" " --max-new N tokens to generate (default 16)\n" " --max-context N KV/state capacity (default 4096)\n" " --greedy argmax (the default)\n" " --seed S enable sampling with this Philox seed\n" " --top-k N --top-p F --temperature F\n" " --dump-logits PATH write one line of raw logits per position\n" " --logits-stride N store every Nth row plus final input (default 1); N>1 requires --max-new 1\n" " --dump-residual PATH write the final R (hc x n_embd, f32) for head bisection\n" " --dump-layers PATH write R after EVERY layer, per position: the C1 bisection ladder\n" " --dump-halves PATH write both halves' block_out and inject per layer: the half bisection\n" " --dump-routing PATH write the routed expert ids and weights per layer per position (P0.S8)\n" " --no-capture run the layers directly instead of replaying graphs\n" " --shared-late A/B: shared expert after the CPU pool (default: overlapped with it)\n" " --keep-canonical A/B: also load canonical copies of natively served tensors (more VRAM)\n" " --vision --serve takes images too (GENI requests; embeddings from strata-vision)\n" " --prompt-cache N --serve: keep N conversation checkpoints between requests (default 6, ~118 MB\n" " of RAM each; 0 = read every prompt from the start)\n" " --prompt-cache-every N --serve: also checkpoint every N fresh prompt tokens (default 16384, 0 = off)\n" " --turn-token ID --serve: the token that opens a chat turn (default 248045, <|im_start|>)\n" " --short-read N --serve: read at most N fresh text tokens through the decode windows instead\n" " of the batched prompt path (default 64, 0 = off)\n" " --suffix-draft N prompt lookup: draft from an earlier repeat of the last N+ tokens of context\n" " when it pays (default 3; 0 = MTP only)\n" " --mtp-max-t M cap the MTP's windows at M tokens (0 = --spec; longer ones come from suffixes)\n" " --control-vector-scaled FILE:SCALE[,...] a control vector GGUF on the residual stream (llama.cpp's\n" " format; --control-vector FILE = scale 1). --serve: requests switch it (cvec=0|1)\n" " --control-vector-layer-range A B the layers it follows (inclusive; default 1 .. the last)\n" " --cvec-mode add|project h += s v (default) or h -= s (h.v) v with v unit\n" " --cvec-dir per-layer|single:L each layer's own direction (default) or layer L's everywhere (project)\n" " --no-token-graph A/B: two graphs per layer (the host launches each) instead of one per token\n" " --no-fused-gr A/B: the six-kernel hyper-connection read and a separate write (native)\n" " --prefill CHUNK batched prompt processing in chunks of CHUNK tokens (needs --native); auto =\n" " the largest chunk up to 8192 whose buffers the expert cache can lend\n" " --no-pool skip the CPU expert pool (the GPU-only floor)\n" " --sync-every-layer debug: synchronise after every layer\n" " --ple-gguf PATH the n-gram/PLE shard. WITHOUT IT LAYER 1's PLE IS SILENTLY SKIPPED,\n" " which changes every number downstream - pass it for any real run\n" " --dump-mixed PATH write the post-attention residual (n_embd, f32)\n" " --stage-timing per-stage KERNEL-COUNT shares. NOT a time profile: an uncaptured\n" " event interval includes host gaps, so run with --gpu-only-full first\n" " --graph-only MEASURE: replay the 48 `pre` graphs only. OMITS the 48 `post` graphs\n" " and the LM head, so it is NOT the GPU floor (R0.3, Memory/ERRORS.md A4)\n" " --gpu-only-full MEASURE: replay pre+post for all 48 layers plus the LM head, no pool.\n" " THE TRUE PER-TOKEN GPU FLOOR. Quote this one, not --graph-only.\n" " --stats print the per-stage breakdown\n" " --gpu-stages R0.9: capture the layer as three graphs (mixer / ffn+router / post)\n" " and time them from OUTSIDE the capture. The per-stage table on the\n" " real graph that --stage-timing cannot give. Prints and exits.\n" " --expert-profile P R4.2e: pre-load the VRAM tier from a `profile.bin` (see\n" " tools/make_profile.py) instead of admitting on first use.\n" " --no-hit-poke R4.2d's A/B arm. The hit path pokes the driver once right after its\n" " launch so the GPU starts while the CPU pool runs; without it the work\n" " waits for the next driver entry and does not overlap at all.\n" " --no-ple-prefetch A/B arm: read the PLE table's sixteen rows one at a time, instead of\n" " issuing them in one PrefetchVirtualMemory call.\n" " --expert-cache N R4: keep N expert blobs resident in VRAM and compute their rows on the\n" " GPU via `moe_hit_grouped_s2`. DEFAULT 0. Measured at 4096 slots\n" " with --expert-cache-per-layer: 54.4%% hits, CPU pool drain 19.1 -> 10.3\n" " ms/token, -2.7 ms/token end to end.\n" " --expert-cache-per-layer R4.2g: give each layer its OWN slots instead of letting the first\n" " position take all of them. The default policy fills in arrival order\n" " from one shared counter, so 256 slots went to ~26 layers of position 0\n" " and measured **2.97%%**. Per-layer, the same routing gives 21.4%% at 8\n" " slots/layer and 70.4%% at 64.\n" " --no-host-worker R2.2: the A/B arm. By default the HOST THREAD joins the drain, so the\n" " pool is six threads on six cores instead of five plus an idle core;\n" " this flag restores the five-worker form for comparison on `pool phases`.\n" " --pool-workers N R2.2: CPU expert pool worker count. Default 0 = every physical core\n" " except the one the host loop spins on. A sweep is how the pool's\n" " deviation from `cpu_s2` is attributed.\n" " --mmap-experts R2.1: opt OUT of the resident expert arena, back to MapViewOfFile.\n" " The A/B arm: the mmap's rate depends on the OS page cache holding\n" " 34 GB, and measured 71.97 vs 34.78 ms/token cold vs warm.\n"); } /// All shards of a split GGUF, from shard 1's path ("...-00001-of-00002.gguf"); just the path when it is not split. std::vector model_shards(const std::string& first) { const std::string tag = "-00001-of-"; const size_t at = first.rfind(tag); if (at == std::string::npos || first.size() < at + tag.size() + 10) return {first}; const int total = std::atoi(first.substr(at + tag.size(), 5).c_str()); std::vector out; for (int i = 1; i <= total && i <= 99; ++i) { char num[8]; std::snprintf(num, sizeof num, "%05d", i); std::string p = first; p.replace(at + 1, 5, num); if (std::ifstream(p, std::ios::binary)) out.push_back(p); } return out.empty() ? std::vector{first} : out; } bool parse_i64_list(const char* s, std::vector& out, std::string& err) { out.clear(); std::string text(s); for (char& c : text) if (c == ',') c = ' '; std::istringstream input(text); std::string token; while (input >> token) { int32_t id = 0; const auto parsed = std::from_chars(token.data(), token.data() + token.size(), id); if (parsed.ec != std::errc{} || parsed.ptr != token.data() + token.size() || id < 0) { err = "invalid token id: expected an integer in [0, 2147483647]"; out.clear(); return false; } out.push_back(id); } if (out.empty()) { err = "token list was empty"; return false; } return true; } /// The pool's adapter plus the wall-clock it spent, so the report can say how much of the token was the CPU. struct Drive { strata::core::ExpertDispatch d; double cpu_ms = 0; int64_t calls = 0; /// THE ROUTING TRACE, which is P0.S8 and a stated prerequisite of Phase 3. `drive_pool` is called once /// per layer from the main loop - the workers live inside `expert_pool_dispatch` - so a single FILE* here /// needs no locking. `d.layers` is the CURRENT layer on entry (the adapter increments it as it walks the /// blob), which is why the layer index comes from there rather than from a counter of our own. std::FILE* routing = nullptr; }; void drive_pool(void* user, const float* x_f, const int32_t* ids, const float* weights, int64_t n_embd, int64_t k, float* out) { Drive* t = (Drive*) user; const Clock::time_point a = Clock::now(); strata::core::expert_pool_dispatch(&t->d, x_f, ids, weights, n_embd, k, out); t->cpu_ms += std::chrono::duration(Clock::now() - a).count(); ++t->calls; // THE ROUTING TRACE. Written AFTER the dispatch so the layer index is still this layer's: `d.layers` is // advanced by the adapter as it consumes the blob, and reading it after the call is the same value the // dispatch used. Record = int32 layer, int32 k, k int32 ids, k float weights. if (t->routing != nullptr) { // **`d.layers` HAS ALREADY BEEN ADVANCED BY THE TIME THIS RUNS, AND THE FIRST TRACE WAS OFF BY ONE // BECAUSE OF IT.** The adapter walks the blob by incrementing `d.layers` as it consumes each layer's // experts, so after the dispatch it holds the NEXT layer's index. `tools/make_profile.py` caught it // with a bounds check when the trace turned out to span 1..48 instead of 0..47. The hit-rate CURVE was // unaffected - it is a per-layer split, and shifting every layer by one preserves both metrics - but // anything keyed on the layer index, which is exactly what a cache profile is, would have been wrong. const int32_t layer_idx = (int32_t) (t->d.layers - 1); if (layer_idx < 0 || layer_idx >= 48) { std::fprintf(stderr, "strata generate: the routing trace saw layer %d, outside 0..47\n", layer_idx); return; } const int32_t rec[2] = {layer_idx, (int32_t) k}; std::fwrite(rec, sizeof rec, 1, t->routing); std::fwrite(ids, sizeof(int32_t), (size_t) k, t->routing); std::fwrite(weights, sizeof(float), (size_t) k, t->routing); } } /// Plan v0.3 P6: the pool for a verify window. void drive_pool_multi(void* user, const float* x_f, const int32_t* ids, int64_t n_tok, int64_t k, float* out, int64_t layer) { Drive* t = (Drive*) user; t->d.layers = layer; const Clock::time_point a = Clock::now(); strata::core::expert_pool_dispatch_multi(t->d, x_f, ids, n_tok, k, out); t->cpu_ms += std::chrono::duration(Clock::now() - a).count(); ++t->calls; } // ---- issue #31: what the watchdog prints before it stops a stalled engine struct MemSample { unsigned long long faults = 0, rss_mib = 0, avail_mib = 0, commit_mib = 0; // faults: hard (Linux) / all (Windows) }; MemSample mem_sample() { MemSample m; #if defined(_WIN32) PROCESS_MEMORY_COUNTERS pmc{}; if (K32GetProcessMemoryInfo(GetCurrentProcess(), &pmc, sizeof pmc)) { m.faults = pmc.PageFaultCount; m.rss_mib = pmc.WorkingSetSize >> 20; m.commit_mib = pmc.PagefileUsage >> 20; } MEMORYSTATUSEX ms{}; ms.dwLength = sizeof ms; if (GlobalMemoryStatusEx(&ms)) m.avail_mib = ms.ullAvailPhys >> 20; #else if (std::FILE* f = std::fopen("/proc/self/stat", "r")) { char buf[4096]; const size_t n = std::fread(buf, 1, sizeof buf - 1, f); buf[n] = 0; std::fclose(f); const char* s = std::strrchr(buf, ')'); // fields after the command name: 3 state ... 12 majflt for (int field = 2; s && field < 12; ++field) s = std::strchr(s + 1, ' '); if (s) m.faults = std::strtoull(s + 1, nullptr, 10); } auto kb = [](const char* path, const char* key) -> unsigned long long { unsigned long long v = 0; if (std::FILE* f = std::fopen(path, "r")) { char line[256]; const size_t kl = std::strlen(key); while (std::fgets(line, sizeof line, f)) if (std::strncmp(line, key, kl) == 0) { v = std::strtoull(line + kl, nullptr, 10); break; } std::fclose(f); } return v; }; m.rss_mib = kb("/proc/self/status", "VmRSS:") >> 10; m.commit_mib = kb("/proc/self/status", "VmSwap:") >> 10; m.avail_mib = kb("/proc/meminfo", "MemAvailable:") >> 10; #endif return m; } void stall_report(std::FILE* f, uint64_t layers_during) { strata::core::Progress& p = strata::core::progress(); std::fprintf(f, "strata serve: stall report (engine %s): stage \"%s %lld\" for %lld s; %llu layers served since the " "last finished step (0 = stopped, more = slow)\n", STRATA_VERSION, p.where.load(), (long long) p.detail.load(), (long long) ((strata::core::progress_now_ms() - p.since_ms.load()) / 1000), (unsigned long long) layers_during); for (int pass = 0; pass < 2; ++pass) { if (pass == 1) { std::this_thread::sleep_for(std::chrono::seconds(2)); std::fprintf(f, " 2 s later:\n"); } if (auto fn = strata::core::diag_pool_fn().load()) fn(f); if (auto fn = strata::core::diag_verify_fn().load()) fn(f); const MemSample m = mem_sample(); std::fprintf(f, " memory: %llu MiB resident, %llu MiB %s, %llu MiB RAM available; %llu %s\n", m.rss_mib, m.commit_mib, #if defined(_WIN32) "committed", m.avail_mib, m.faults, "page faults so far" #else "in swap", m.avail_mib, m.faults, "major page faults so far" #endif ); std::fflush(f); } #if defined(_WIN32) // every thread's stack, to read against this build's PDB: a few MB beside the engine's working directory if (HMODULE dbg = LoadLibraryA("dbghelp.dll")) { using Fn = BOOL(WINAPI*)(HANDLE, DWORD, HANDLE, int, void*, void*, void*); if (auto write = (Fn) GetProcAddress(dbg, "MiniDumpWriteDump")) { char path[64]; std::snprintf(path, sizeof path, "strata-stall-%lu.dmp", (unsigned long) GetCurrentProcessId()); HANDLE h = CreateFileA(path, GENERIC_WRITE, 0, nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr); if (h != INVALID_HANDLE_VALUE) { const int kThreadInfo = 0x1000; // MiniDumpWithThreadInfo; MiniDumpNormal = 0 const BOOL ok = write(GetCurrentProcess(), GetCurrentProcessId(), h, kThreadInfo, nullptr, nullptr, nullptr); CloseHandle(h); char full[MAX_PATH]; if (!GetFullPathNameA(path, MAX_PATH, full, nullptr)) std::snprintf(full, sizeof full, "%s", path); std::fprintf(f, " %s the thread stacks to %s (attach it to the issue)\n", ok ? "wrote" : "could not write", full); } } } #endif } /// STRATA_TRACE=1: the VRAM left at a step of the startup (finds what fills the card after the cache is sized) void mem_mark(const char* where) { static const bool on = std::getenv("STRATA_TRACE") != nullptr; if (!on) return; size_t free_b = 0, total_b = 0; cudaMemGetInfo(&free_b, &total_b); std::fprintf(stderr, "strata trace: %lld MiB free after %s\n", (long long) (free_b >> 20), where); } int argmax(const std::vector& v) { int best = 0; for (size_t i = 1; i < v.size(); ++i) if (v[i] > v[best]) best = (int) i; return best; } // ---- --serve's conversation cache. A chat or an agent sends the whole conversation again with every request, and // reading it again is what made a long session wait minutes for every turn. What a sequence leaves behind splits in // two, and only one half needs copying: // * POSITIONAL state - the KV cache of the 12 QSA layers and their pooled indexer keys, the draft layer's KV. A // cell is written once for its position and read only by later positions (the block scores take `dead` for the // block being filled, never its pooled row), so rewinding to a position just means writing from there again. // * RUNNING state - the 36 GDN recurrences and conv histories, each QSA layer's indexer tail (the unfinished // block's raw keys) and the PLE's normalized history. Each describes "everything so far" and cannot be // rewound, so a checkpoint is a copy of exactly these: ~118 MB, the same set the verifier snapshots to roll // back rejected drafts. // A checkpoint is only valid while the positional cells below it still hold ITS tokens, so the serve loop keeps // just the checkpoints that are prefixes of the tokens the session holds now. struct ImgKey { int64_t start = 0; ///< the image's first <|image_pad|> position uint64_t hash = 0; ///< its embeddings and grid: the pad tokens alone are the same for every picture bool operator==(const ImgKey& o) const { return start == o.start && hash == o.hash; } }; struct ConvCheckpoint { std::vector ids; ///< the tokens this state has consumed std::vector imgs; ///< the images among them std::vector gdn, ple, tails; uint64_t used = 0; ///< last-use stamp for the retention policy (conv_cache.hpp) }; uint64_t fnv1a(const void* data, size_t n, uint64_t h = 1469598103934665603ull) { const uint8_t* p = (const uint8_t*) data; for (size_t i = 0; i < n; ++i) { h ^= p[i]; h *= 1099511628211ull; } return h; } struct ConvStateSizes { size_t gdn = 0, ple = 0, tail = 0; }; ConvStateSizes conv_state_sizes(const strata::core::ModelGeometry& g) { ConvStateSizes z; z.gdn = (size_t) g.n_gdn_layers() * ((size_t) g.ssm_state_size * (size_t) g.ssm_v_heads * (size_t) g.ssm_state_size + (size_t) g.ssm_conv_channels * (size_t) (g.ssm_d_conv - 1)) * sizeof(float); z.ple = (size_t) strata::kernels::NG_HIST * (size_t) strata::kernels::NG_HC_DIM * sizeof(float); z.tail = (size_t) (strata::kernels::qsa_real_shapes().idx_block - 1) * (size_t) g.idx_key_dim * sizeof(float); return z; } /// Copies the running state out. The caller has synchronized the device. bool checkpoint_save(ConvCheckpoint& c, const strata::core::SessionState& ss, const strata::core::ModelGeometry& g) { const ConvStateSizes z = conv_state_sizes(g); c.gdn.resize(z.gdn); c.ple.resize(ss.ple_hist != nullptr ? z.ple : 0); c.tails.resize(z.tail * (size_t) g.n_qsa_layers()); if (cudaMemcpy(c.gdn.data(), ss.gdn_state, z.gdn, cudaMemcpyDeviceToHost) != cudaSuccess) return false; if (!c.ple.empty() && cudaMemcpy(c.ple.data(), ss.ple_hist, z.ple, cudaMemcpyDeviceToHost) != cudaSuccess) return false; for (int64_t i = 0; i < g.n_qsa_layers(); ++i) if (cudaMemcpy(c.tails.data() + (size_t) i * z.tail, ss.qsa_states[i].idx_tail, z.tail, cudaMemcpyDeviceToHost) != cudaSuccess) return false; return true; } /// Puts a checkpoint's running state back; the positional cells below it are the caller's to guarantee. bool checkpoint_restore(const ConvCheckpoint& c, strata::core::SessionState& ss, const strata::core::ModelGeometry& g) { const ConvStateSizes z = conv_state_sizes(g); if (c.gdn.size() != z.gdn || c.tails.size() != z.tail * (size_t) g.n_qsa_layers()) return false; if (cudaMemcpy(ss.gdn_state, c.gdn.data(), z.gdn, cudaMemcpyHostToDevice) != cudaSuccess) return false; if (!c.ple.empty() && cudaMemcpy(ss.ple_hist, c.ple.data(), z.ple, cudaMemcpyHostToDevice) != cudaSuccess) return false; for (int64_t i = 0; i < g.n_qsa_layers(); ++i) if (cudaMemcpy(ss.qsa_states[i].idx_tail, c.tails.data() + (size_t) i * z.tail, z.tail, cudaMemcpyHostToDevice) != cudaSuccess) return false; // the PLE's token window is the last two tokens, OLDEST FIRST, -1 where there is none (as session_zero leaves it) const size_t L = c.ids.size(); ss.ple_prev[0] = L >= 2 ? c.ids[L - 2] : -1; ss.ple_prev[1] = L >= 1 ? c.ids[L - 1] : -1; return cudaDeviceSynchronize() == cudaSuccess; } // --control-vector-scaled: llama.cpp's `common_control_vector_load` (every file's `direction.` times its scale, // summed; layer 0 has none) and `llama_adapter_cvec::apply` with the projection-mode patch (project: the unit // direction and its norm as the scale), into the tables `cvec_upload` takes. `summary` is what INFO reports. bool load_control_vectors(const Options& o, const strata::core::ModelGeometry& g, std::string& summary, std::string& err) { const int64_t L = g.n_layers, N = g.n_embd; std::vector data((size_t) (L * N), 0.0f); std::vector have((size_t) L, false); for (const auto& [path, scale] : o.cvec_files) { try { strata::GgufFile f(path); const strata::MetaValue* arch = f.get("general.architecture"); if (arch == nullptr || arch->s != "controlvector") { err = path + ": not a control vector GGUF (general.architecture is not 'controlvector')"; return false; } const strata::MetaValue* hint = f.get("controlvector.model_hint"); if (hint != nullptr && hint->s != "qwen4exp") std::fprintf(stderr, "strata generate: %s was made for '%s', not qwen4exp\n", path.c_str(), hint->s.c_str()); int found = 0; for (const strata::TensorInfo& t : f.tensors()) { if (t.name.rfind("direction.", 0) != 0) continue; const long l = std::strtol(t.name.c_str() + 10, nullptr, 10); if (l < 1 || l >= L) continue; // layer 0 has no vector; past the model is ignored, as in llama.cpp if (t.type != 0 || t.elements() != (uint64_t) N) { err = path + ": " + t.name + " must be " + std::to_string((long long) N) + " f32"; return false; } const float* src = reinterpret_cast(f.tensor_data(t)); for (int64_t j = 0; j < N; ++j) data[(size_t) (l * N + j)] += scale * src[j]; have[(size_t) l] = true; ++found; } if (found == 0) { err = path + ": no direction. tensors"; return false; } } catch (const std::exception& e) { err = e.what(); return false; } } const int first = o.cvec_first <= 0 ? 1 : o.cvec_first; const int last = (o.cvec_last <= 0 || o.cvec_last >= L) ? (int) L - 1 : o.cvec_last; const int single = o.cvec_mode == 0 ? o.cvec_single : -1; if (single >= 0 && (single >= L || !have[(size_t) single])) { err = "--cvec-dir single:" + std::to_string(single) + ": the vector has no direction for that layer"; return false; } std::vector dir((size_t) (L * N), 0.0f), s((size_t) L, 0.0f); int steered = 0; for (int64_t l = first; l <= last; ++l) { const int64_t src = single >= 0 ? single : l; if (!have[(size_t) src]) continue; const float* d = data.data() + (size_t) (src * N); if (o.cvec_mode == 0) { double nrm = 0.0; for (int64_t j = 0; j < N; ++j) nrm += (double) d[j] * d[j]; nrm = std::sqrt(nrm); if (nrm <= 0.0) continue; s[(size_t) l] = (float) nrm; for (int64_t j = 0; j < N; ++j) dir[(size_t) (l * N + j)] = (float) (d[j] / nrm); } else { s[(size_t) l] = 1.0f; std::copy(d, d + N, dir.begin() + (size_t) (l * N)); } ++steered; } if (steered == 0) { err = "the control vector has no direction in layers " + std::to_string(first) + ".." + std::to_string(last); return false; } if (!strata::kernels::cvec_upload(dir, s, o.cvec_mode, first, last, N, g.hc, err)) return false; summary = std::string(o.cvec_mode == 0 ? "project" : "add") + ":" + std::to_string(first) + "-" + std::to_string(last) + (single >= 0 ? ":single" + std::to_string(single) : ""); // the line llama.cpp's patched build prints, so a log shows the same thing std::fprintf(stderr, "strata generate: control vector mode = %s, dir = %s, layers %d..%d (%d steered)\n", o.cvec_mode == 0 ? "project" : "add", single >= 0 ? "single" : "per-layer", first, last, steered); return true; } } // namespace int main(int argc, char** argv) { // **UNBUFFERED, BECAUSE THE INTERESTING OUTPUT IS THE OUTPUT BEFORE A CRASH.** `stdout` redirected to a // pipe or a file is block-buffered, so a program that dies loses every line it had already printed - which // turns "it crashed at step 7" into "it crashed somewhere", and the difference is a debugging session. std::setvbuf(stdout, nullptr, _IONBF, 0); // Load every CUDA kernel when the context is created, before the expert cache takes the free VRAM. With the // default lazy loading, a kernel first used mid-prompt (MMQ for IQ3_XXS at 64K+ on a 12 GB card) found no VRAM // left for its code and the engine ended ("out of memory: cudaFuncSetAttribute"). Costs ~30 MB of VRAM. if (std::getenv("CUDA_MODULE_LOADING") == nullptr) { #if defined(_WIN32) _putenv_s("CUDA_MODULE_LOADING", "EAGER"); #else setenv("CUDA_MODULE_LOADING", "EAGER", 0); #endif } Options o; bool have_tokens = false; bool have_logits_stride = false; for (int i = 1; i < argc; ++i) { const std::string a = argv[i]; auto next = [&](const char* what) -> const char* { if (i + 1 >= argc) { std::fprintf(stderr, "%s needs a value\n", what); std::exit(2); } return argv[++i]; }; if (a == "--help" || a == "-h") { usage(); return 0; } else if (a == "--pack") o.pack = next("--pack"); else if (a == "--tokens") { if (have_tokens) { std::fprintf(stderr, "supply one token input only\n"); return 2; } std::string e; if (!parse_i64_list(next("--tokens"), o.tokens, e)) { std::fprintf(stderr, "%s\n", e.c_str()); return 2; } have_tokens = true; } else if (a == "--tokens-file") { if (have_tokens) { std::fprintf(stderr, "supply one token input only\n"); return 2; } std::ifstream input(next("--tokens-file")); if (!input) { std::fprintf(stderr, "cannot open token file\n"); return 2; } std::string text((std::istreambuf_iterator(input)), std::istreambuf_iterator()); if (input.bad()) { std::fprintf(stderr, "cannot read token file\n"); return 2; } std::string error; if (text.find('\0') != std::string::npos || !parse_i64_list(text.c_str(), o.tokens, error)) { std::fprintf(stderr, "malformed token file: %s\n", error.c_str()); return 2; } have_tokens = true; } else if (a == "--max-new") o.max_new = std::atoll(next("--max-new")); else if (a == "--max-context") o.max_context = std::atoll(next("--max-context")); else if (a == "--greedy") o.greedy = true; else if (a == "--seed") { o.seed = (uint64_t) std::atoll(next("--seed")); o.greedy = false; } else if (a == "--top-k") o.top_k = std::atoi(next("--top-k")); else if (a == "--top-p") o.top_p = (float) std::atof(next("--top-p")); else if (a == "--temperature") o.temperature = (float) std::atof(next("--temperature")); else if (a == "--dump-logits") o.dump_logits = next("--dump-logits"); else if (a == "--logits-stride") { if (have_logits_stride) { std::fprintf(stderr, "--logits-stride must be supplied only once\n"); return 2; } if (!strata::program::logits_selection::parse_stride(next("--logits-stride"), o.logits_stride)) { std::fprintf(stderr, "--logits-stride requires a positive decimal int64\n"); return 2; } have_logits_stride = true; } else if (a == "--dump-residual") o.dump_residual = next("--dump-residual"); else if (a == "--dump-mixed") o.dump_mixed = next("--dump-mixed"); else if (a == "--dump-layers") o.dump_layers = next("--dump-layers"); else if (a == "--dump-halves") o.dump_halves = next("--dump-halves"); else if (a == "--dump-routing") o.dump_routing = next("--dump-routing"); else if (a == "--ple-gguf") o.ple_gguf = next("--ple-gguf"); else if (a == "--no-ple") o.no_ple = true; else if (a == "--ple-io") o.ple_io = next("--ple-io"); else if (a == "--ple-row-cache") o.ple_row_cache = std::atoll(next("--ple-row-cache")); else if (a == "--ple-inflight") o.ple_inflight = std::atoi(next("--ple-inflight")); else if (a == "--ple-delay-us") o.ple_delay_us = std::atof(next("--ple-delay-us")); else if (a == "--ple-sync-submit") o.ple_sync_submit = true; else if (a == "--kv") o.kv = next("--kv"); else if (a == "--kv-resident") o.kv_resident = std::atoll(next("--kv-resident")); else if (a == "--stream-token") o.stream_token = true; else if (a == "--check-logits") o.check_logits = true; else if (a == "--gr-fp32-activations") o.gr_fp32_activations = true; else if (a == "--gr-native-mmvf") o.gr_native_mmvf = true; else if (a == "--native-bf16") o.native_bf16 = true; else if (a == "--native-bf16-extra") o.native_bf16_extra = true; else if (a == "--native-ple-key") o.native_ple_key = true; else if (a == "--native-moe-combine") o.native_moe_combine = true; else if (a == "--native-gdn") o.native_gdn = true; else if (a == "--native-flash-attn-short") o.native_flash_attn_short = true; else if (a == "--native-qsa-indexer") o.native_qsa_indexer = true; else if (a == "--native-qsa") o.native_qsa = true; else if (a == "--native-rope") o.native_rope = true; else if (a == "--native-ple-postops") o.native_ple_postops = true; else if (a == "--native-router") o.native_router = true; else if (a == "--cpu-oracle-q8-0") o.cpu_oracle_q8_0 = true; else if (a == "--native") o.native_preset = next("--native"); else if (a == "--native-head-gguf") o.native_head_gguf = next("--native-head-gguf"); else if (a == "--native-dense-gguf") o.native_dense_gguf.push_back(next("--native-dense-gguf")); else if (a == "--no-capture") o.no_capture = true; else if (a == "--no-pool") o.no_pool = true; else if (a == "--sync-every-layer") o.sync_every_layer = true; else if (a == "--stage-timing") o.stage_timing = true; else if (a == "--graph-only") o.graph_only = true; else if (a == "--gpu-only-full") o.gpu_only_full = true; else if (a == "--pool-workers") o.pool_workers = std::atoi(next("--pool-workers")); else if (a == "--no-host-worker") o.no_host_worker = true; else if (a == "--no-ple-prefetch") o.no_ple_prefetch = true; else if (a == "--expert-cache") { const std::string v = next("--expert-cache"); o.expert_cache = (v == "auto") ? -1 : std::atoi(v.c_str()); } else if (a == "--vram-reserve-mib") o.vram_reserve_mib = std::atoi(next("--vram-reserve-mib")); else if (a == "--prefill") { const std::string v = next("--prefill"); o.prefill_auto = v == "auto"; o.prefill_chunk = o.prefill_auto ? 8192 : std::atoll(v.c_str()); } else if (a == "--no-split-rows") o.no_split_rows = true; else if (a == "--no-prefill-borrow") o.no_prefill_borrow = true; else if (a == "--prefill-until") o.prefill_until = std::atoll(next("--prefill-until")); else if (a == "--dump-final-r") o.dump_final_r = next("--dump-final-r"); else if (a == "--spec") o.spec = std::atoi(next("--spec")); else if (a == "--spec-oracle") o.spec_oracle = next("--spec-oracle"); else if (a == "--spec-corrupt") o.spec_corrupt = std::atoi(next("--spec-corrupt")); else if (a == "--mtp") o.mtp = next("--mtp"); else if (a == "--mtp-window") o.mtp_window = std::atoll(next("--mtp-window")); else if (a == "--pcie-frac") o.pcie_frac = std::atof(next("--pcie-frac")); else if (a == "--adapt-every") o.adapt_every = std::atoi(next("--adapt-every")); else if (a == "--spec-min-p") o.spec_min_p = std::atof(next("--spec-min-p")); else if (a == "--stop-eos") o.stop_eos = true; else if (a == "--spec-split") o.spec_split = true; else if (a == "--pcie-mode") o.pcie_mode = next("--pcie-mode"); else if (a == "--serve") o.serve = true; else if (a == "--vision") o.vision = true; else if (a == "--prompt-cache") o.prompt_cache = std::max(0, std::atoi(next("--prompt-cache"))); else if (a == "--prompt-cache-every") o.prompt_cache_every = std::max(0LL, std::atoll(next("--prompt-cache-every"))); else if (a == "--turn-token") o.turn_token = std::atoll(next("--turn-token")); else if (a == "--short-read") o.short_read = std::max(0LL, std::atoll(next("--short-read"))); else if (a == "--suffix-draft") o.suffix_draft = std::max(0, std::atoi(next("--suffix-draft"))); else if (a == "--mtp-max-t") o.mtp_max_t = std::max(0, std::atoi(next("--mtp-max-t"))); else if (a == "--control-vector") o.cvec_files.push_back({next("--control-vector"), 1.0f}); else if (a == "--control-vector-scaled") { // FILE:SCALE, comma-separated; the LAST colon splits, so a Windows path (C:\...) keeps its drive std::stringstream list(next("--control-vector-scaled")); std::string item; while (std::getline(list, item, ',')) { const size_t colon = item.rfind(':'); char* end = nullptr; const float sc = colon == std::string::npos ? 0.0f : std::strtof(item.c_str() + colon + 1, &end); if (colon == std::string::npos || colon == 0 || end == item.c_str() + colon + 1 || *end != '\0') { std::fprintf(stderr, "--control-vector-scaled: expected FILE:SCALE, got '%s'\n", item.c_str()); return 2; } o.cvec_files.push_back({item.substr(0, colon), sc}); } } else if (a == "--control-vector-layer-range") { o.cvec_first = std::atoi(next("--control-vector-layer-range")); o.cvec_last = std::atoi(next("--control-vector-layer-range")); } else if (a == "--cvec-mode") { const std::string m = next("--cvec-mode"); if (m == "project") o.cvec_mode = 0; else if (m == "add") o.cvec_mode = 1; else { std::fprintf(stderr, "--cvec-mode: add or project, got '%s'\n", m.c_str()); return 2; } } else if (a == "--cvec-dir") { const std::string d = next("--cvec-dir"); if (d == "per-layer") o.cvec_single = -1; else if (d.rfind("single:", 0) == 0) o.cvec_single = std::atoi(d.c_str() + 7); else { std::fprintf(stderr, "--cvec-dir: per-layer or single:L, got '%s'\n", d.c_str()); return 2; } } else if (a == "--no-spec-split") o.spec_split = false; else if (a == "--eos-ids") { std::string e; if (!parse_i64_list(next("--eos-ids"), o.eos_ids, e)) { std::fprintf(stderr, "--eos-ids: %s\n", e.c_str()); return 2; } o.stop_eos = true; } else if (a == "--adapt-swaps") o.adapt_swaps = std::atoi(next("--adapt-swaps")); else if (a == "--expert-cache-cpu-order") o.expert_cache_cpu_order = true; else if (a == "--expert-cache-per-layer") o.expert_cache_per_layer = true; else if (a == "--no-hit-poke") o.no_hit_poke = true; else if (a == "--expert-profile") o.expert_profile = next("--expert-profile"); else if (a == "--gpu-stages") o.gpu_stages = true; else if (a == "--mmap-experts") o.mmap_experts = true; else if (a == "--stats") o.stats = true; else if (a == "--shared-late") o.shared_late = true; else if (a == "--keep-canonical") o.keep_canonical = true; else if (a == "--no-token-graph") o.no_token_graph = true; else if (a == "--no-fused-gr") o.no_fused_gr = true; else if (a == "--no-fast-attn") o.no_fast_attn = true; else if (a == "--no-publish-kernel") o.no_publish_kernel = true; else if (a == "--no-fused-gdn") o.no_fused_gdn = true; else if (a == "--no-fast-select") o.no_fast_select = true; else { // An unknown flag is an ERROR and not a warning: a typo'd `--max-neww` that silently generated 16 // tokens would look like a working run. std::fprintf(stderr, "unknown argument: %s\n", a.c_str()); usage(); return 2; } } if (o.prefill_auto && (o.no_prefill_borrow || o.expert_profile.empty())) { o.prefill_auto = false; // nothing to lend from: the buffers are reserved for the session, so keep them small o.prefill_chunk = 2048; } if (!have_tokens && o.serve) { // plan v0.3 P8: requests bring their own tokens o.tokens = {248045}; o.max_new = 1; have_tokens = true; o.stop_eos = true; } if (!have_tokens) { std::fprintf(stderr, "strata generate: --tokens is required (this build has no tokenizer; see the " "header of src/program/generate.cpp)\n"); usage(); return 2; } if ((o.ple_io != "direct" && o.ple_io != "mmap") || o.ple_row_cache < 0 || o.ple_inflight < 1 || o.ple_inflight > 1024 || !(o.ple_delay_us >= 0)) { std::fprintf(stderr, "strata generate: invalid --ple-io/--ple-row-cache/--ple-inflight/--ple-delay-us\n"); return 2; } if (o.kv == "q4") o.kv = "q4_0"; if (o.kv != "fp16" && o.kv != "int8" && o.kv != "q4_0") { std::fprintf(stderr, "strata generate: --kv must be fp16, int8 or q4_0\n"); return 2; } strata::core::qsa_set_kv_int8(o.kv == "int8"); strata::core::qsa_set_kv_q4(o.kv == "q4_0"); // PR #21: 4-bit codes after a Hadamard rotation (kv_q4.hpp) if (o.kv_resident < 0) { std::fprintf(stderr, "strata generate: --kv-resident must be >= 0\n"); return 2; } strata::core::qsa_set_kv_resident(o.kv_resident); // Prompt lookup (the suffix drafter, on by default): the MTP keeps its --spec windows and a lookup window may be // up to 2 tokens longer; the draft policy (strata/spec/draft_policy.hpp) takes one only where it pays. Code // edits +6-11%, ordinary text unchanged (bench/results/2026-09-27-spec). --suffix-draft 0 turns it off. if (o.suffix_draft > 0 && o.spec >= 2 && o.mtp_max_t == 0) { o.mtp_max_t = o.spec; o.spec = std::min(o.spec + 2, 8); // kVerifyMaxT } strata::core::layer_set_shared_early(!o.shared_late); if (!o.native_preset.empty()) { if (o.no_ple || o.ple_gguf.empty()) { std::fprintf(stderr, "strata generate: --native requires --ple-gguf (the PLE key is native too)\n"); return 2; } o.stream_token = true; o.gr_native_mmvf = true; o.native_bf16 = o.native_bf16_extra = true; o.native_ple_key = o.native_moe_combine = o.native_gdn = o.native_router = true; o.native_qsa = o.native_qsa_indexer = o.native_rope = o.native_ple_postops = true; if (o.native_head_gguf.empty()) o.native_head_gguf = o.native_preset; if (o.native_dense_gguf.empty()) { // every shard of the model (-0000N-of-0000M.gguf beside --native), then the PLE shard: a split // may put any layer in any shard (Swift's GGUFs: layers 13-47 in shard 2, the PLE table in shard 1) o.native_dense_gguf = model_shards(o.native_preset); if (std::find(o.native_dense_gguf.begin(), o.native_dense_gguf.end(), o.ple_gguf) == o.native_dense_gguf.end()) o.native_dense_gguf.push_back(o.ple_gguf); } // Plan v0.3 (24 Sep): the CPU experts stay on the VNNI kernel. The llama.cpp-CPU-exact q8_0 contract // cost 27.0 vs 17.2 ms/token of pool time and G-C does not need it; `--cpu-oracle-q8-0` still selects it. } if (o.logits_stride > 1 && (o.max_new != 1 || o.dump_logits.empty())) { std::fprintf(stderr, "strata generate: --logits-stride > 1 requires --max-new 1 and --dump-logits\n"); return 2; } if (o.no_ple && !o.ple_gguf.empty()) { std::fprintf(stderr, "strata generate: --no-ple and --ple-gguf are mutually exclusive\n"); return 2; } if (o.native_ple_postops && o.no_ple) { std::fprintf(stderr, "strata generate: --native-ple-postops requires PLE enabled\n"); return 2; } if (!o.no_ple && o.ple_gguf.empty()) { std::fprintf(stderr, "strata generate: --ple-gguf is required; --no-ple explicitly enables a diagnostic ablation\n"); return 2; } // P7 audit: positions, cells and pooled-block indices are cast to int32 on the device path. if (o.max_context > 2147483647LL - 8) { std::fprintf(stderr, "strata generate: --max-context must be below 2^31\n"); return 2; } if (o.max_new <= 0 || o.max_context <= 0 || o.max_new > o.max_context || o.tokens.size() > (size_t) (o.max_context - o.max_new)) { std::fprintf(stderr, "strata generate: positive --max-new and --max-context must fit the prompt and generation\n"); return 2; } if (!std::isfinite(o.temperature) || o.temperature < 0 || !std::isfinite(o.top_p) || o.top_p <= 0 || o.top_p > 1 || o.top_k < 0 || o.expert_cache < -1 || o.pool_workers < 0) { std::fprintf(stderr, "strata generate: invalid sampling or resource parameter\n"); return 2; } if (o.native_flash_attn_short && o.max_context > 256) { std::fprintf(stderr, "strata generate: --native-flash-attn-short requires --max-context <=256\n"); return 2; } if (o.native_flash_attn_short && (o.gpu_only_full || o.graph_only || o.gpu_stages)) { std::fprintf(stderr, "strata generate: --native-flash-attn-short requires the normal decode loop for status validation\n"); return 2; } if (o.native_ple_key && (o.native_dense_gguf.empty() || o.no_ple)) { std::fprintf(stderr, "strata generate: --native-ple-key requires PLE and --native-dense-gguf\n"); return 2; } if (o.cpu_oracle_q8_0 && (o.expert_cache != 0 || !o.expert_profile.empty())) { std::fprintf(stderr, "strata generate: --cpu-oracle-q8-0 cannot be combined with --expert-cache or --expert-profile until the GPU expert contract matches\n"); return 2; } // **BEFORE ANYTHING ELSE.** The CPU expert kernel is AVX-512 (VNNI + VBMI) and its translation unit is // compiled `/arch:AVX512`, so on a CPU without those features it does not fail - it executes an illegal // instruction at some unpredictable token. Refusing at second zero is the whole point of P2.S3's check. strata::kernels::cpu::expert_set_oracle_q8_0(o.cpu_oracle_q8_0); std::string err; if (!o.native_head_gguf.empty() && !o.stream_token) { std::fprintf(stderr, "--native-head-gguf requires --stream-token\n"); return 2; } // Plan v0.3 P6: where the experts live. A native pack (tools/iq_pack.py: the IQ2_XS / IQ3_XXS files) keeps // every quantized tensor in its GGUF form, so it needs --native (the dense projections, head and embedding // come from the model file) and runs its experts in verify windows only (--spec). { const strata::core::ModelGeometry g0; if (!strata::kernels::cpu::expert_layout_load(o.pack, g0.n_layers, g0.n_expert, err)) { std::fprintf(stderr, "strata generate: %s\n", err.c_str()); return 1; } } const bool native_pack = strata::kernels::cpu::expert_layout().native; // plan v0.3 P6: the PCIe share of the missed experts, measured per kind of pack (the paper, finding on PCIe) if (o.pcie_frac < 0.0) o.pcie_frac = native_pack ? 0.55 : 0.2; // the canonical Q2_0 pack's CPU kernels are AVX-512 only; a native pack runs on AVX2 CPUs as well if (!native_pack) strata::kernels::cpu::cpu_require_expert_support(); else if (!strata::kernels::cpu::cpu_avx512_ok()) std::fprintf(stderr, "strata generate: this CPU has no AVX-512: the expert kernels run on %s " "(multi-token for the i-quant gate/up rows)\n", std::getenv("STRATA_NO_IQ256") == nullptr ? "AVX-2" : "ggml-cpu vec_dot (STRATA_NO_IQ256 set)"); strata::core::NativeEmbed native_embed; if (native_pack) { if (o.native_preset.empty() || o.spec < 2 || o.keep_canonical || (o.prefill_chunk <= 0 && o.tokens.size() > 1)) { std::fprintf(stderr, "strata generate: %s is a native (IQ) pack: it needs --native SHARD1, --spec T (T >= 2) " "and --prefill CHUNK\n", o.pack.c_str()); return 2; } const strata::core::ModelGeometry g0; if (!native_embed.load(o.native_preset, g0.n_embd, 248320, err)) { std::fprintf(stderr, "strata generate: %s\n", err.c_str()); return 1; } strata::core::set_native_embed(&native_embed); std::fprintf(stderr, "strata generate: native pack: %s experts (largest blob %.2f MB), token embedding " "type %d in mapped host memory (%.0f MiB)\n", o.pack.c_str(), (double) strata::kernels::cpu::expert_layout().max_blob / 1e6, native_embed.type(), (double) native_embed.bytes() / 1048576.0); } // Plan v0.3 P1: tensors served in native form are not also loaded in canonical form (~2.7 GB of VRAM back // to the expert cache with --native). `--keep-canonical` loads both, as before. std::set skip; if (!o.keep_canonical) { if (!o.native_dense_gguf.empty() && !strata::core::NativeDense::served_names(o.native_dense_gguf, o.native_ple_key, skip, err)) { std::fprintf(stderr, "strata generate: %s\n", err.c_str()); return 1; } if (!o.native_head_gguf.empty()) skip.insert("output.weight"); // the PLE module validates its canonical key at construction (8 MB); a native pack has none to load if (!native_pack) skip.erase("blk.1.ple_key.weight"); if (native_pack) skip.insert("token_embd.weight"); } uint64_t pool_bytes = 0; if (!strata::core::WeightTable::pool_bytes(o.pack, pool_bytes, err, skip.empty() ? nullptr : &skip)) { std::fprintf(stderr, "strata generate: %s\n", err.c_str()); return 1; } void* arena = nullptr; if (cudaMalloc(&arena, pool_bytes) != cudaSuccess) { std::fprintf(stderr, "strata generate: cudaMalloc(%llu) for the weight arena failed\n", (unsigned long long) pool_bytes); return 1; } strata::core::WeightTable wt; if (!wt.load(o.pack, arena, pool_bytes, err, skip.empty() ? nullptr : &skip)) { std::fprintf(stderr, "strata generate: %s\n", err.c_str()); return 1; } std::fprintf(stderr, "strata generate: %llu MiB of weights loaded from %s (%zu canonical tensors skipped: " "served natively)\n", (unsigned long long) (pool_bytes >> 20), o.pack.c_str(), skip.size()); strata::core::NativeDense native_dense; if (!o.native_dense_gguf.empty()) { if (!native_dense.load(o.native_dense_gguf, wt, err, o.native_ple_key)) { std::fprintf(stderr, "strata generate: native dense projections: %s\n", err.c_str()); return 1; } std::fprintf(stderr, "strata generate: %zu native projection matrices, %.2f MiB of weights\n", native_dense.tensor_count(), (double) native_dense.weight_bytes() / (1024.0 * 1024.0)); } strata::kernels::gr_set_fp32_activations(o.gr_fp32_activations); strata::kernels::gr_set_native_mmvf(o.gr_native_mmvf); // Plan v0.3 P3: the fused hyper-connection read rides the native (FP32-activation) contract; the per-stage // and dump measurements need the unfused layout of R, so they keep the old kernels. strata::core::layer_set_fast_attn(!o.no_fast_attn); strata::core::layer_set_publish_kernel(!o.no_publish_kernel); strata::core::layer_set_fused_gdn(!o.no_fused_gdn); strata::core::layer_set_fast_select(!o.no_fast_select); strata::core::layer_set_fused_gr(o.gr_native_mmvf && !o.no_fused_gr && !o.gpu_stages && o.dump_layers.empty() && o.dump_halves.empty() && !o.stage_timing); strata::core::layer_set_native_bf16(o.native_bf16); strata::core::layer_set_native_flash_attn_short(o.native_flash_attn_short); strata::kernels::ple_set_native_bf16(o.native_bf16_extra); strata::kernels::shared_expert_set_native_bf16(o.native_bf16_extra); strata::kernels::native_moe_combine_set_enabled(o.native_moe_combine); strata::kernels::native_gdn_set_enabled(o.native_gdn); strata::kernels::native_router_set_enabled(o.native_router); strata::kernels::native_qsa_set_enabled(o.native_qsa); strata::kernels::native_qsa_indexer_set_enabled(o.native_qsa_indexer); strata::kernels::native_rope_set_enabled(o.native_rope); // The vision path: every rope kernel reads a cell's (t, h, w) from this table (strata/kernels/mrope.hpp). It is // the identity until an image request, and it is set here, before any CUDA graph captures a rope kernel. int32_t* d_mrope = nullptr; std::vector mrope_host; if (o.vision) { const int64_t cells = o.max_context + 64; mrope_host.resize((size_t) cells * 3); for (int64_t c = 0; c < cells; ++c) mrope_host[(size_t) c * 3] = mrope_host[(size_t) c * 3 + 1] = mrope_host[(size_t) c * 3 + 2] = (int32_t) c; if (cudaMalloc(&d_mrope, mrope_host.size() * sizeof(int32_t)) != cudaSuccess || cudaMemcpy(d_mrope, mrope_host.data(), mrope_host.size() * sizeof(int32_t), cudaMemcpyHostToDevice) != cudaSuccess) { std::fprintf(stderr, "strata generate: cannot allocate the image position table\n"); return 1; } strata::kernels::mrope_table_set(d_mrope); } strata::kernels::ple_set_native_postops(o.native_ple_postops); strata::core::ModelGeometry g; // canonical defaults; the model file overrides the MoE shape below int64_t K = 10; if (!o.native_preset.empty()) { // a pruned variant (GSQ-RCO Coder) ships fewer experts than the canonical 512x10; the model file // is the authority on its own MoE shape - everything else in the geometry is unchanged try { strata::GgufFile model_gguf(o.native_preset); if (const strata::MetaValue* v = model_gguf.get("qwen4exp.expert_count")) g.n_expert = (int64_t) v->u; if (const strata::MetaValue* v = model_gguf.get("qwen4exp.expert_used_count")) K = (int64_t) v->u; } catch (const std::exception& e) { std::fprintf(stderr, "strata generate: reading the model's expert shape from %s: %s\n", o.native_preset.c_str(), e.what()); return 1; } } // before session_init: every graph captured from here on has the vector's kernels where it applies std::string cvec_summary = "0"; if (!o.cvec_files.empty()) { std::string ce; if (!load_control_vectors(o, g, cvec_summary, ce)) { std::fprintf(stderr, "strata generate: control vector: %s\n", ce.c_str()); return 2; } } if (o.max_context < (int64_t) o.tokens.size() + o.max_new) { std::fprintf(stderr, "strata generate: --max-context %lld cannot hold %zu prompt + %lld new tokens\n", (long long) o.max_context, o.tokens.size(), (long long) o.max_new); return 2; } void* sbuf = nullptr; if (cudaMalloc(&sbuf, strata::core::session_bytes(g, o.max_context, K)) != cudaSuccess) { std::fprintf(stderr, "strata generate: session state allocation failed\n"); return 1; } strata::core::SessionState ss; // **THE ENGINE RAN ON THE LEGACY DEFAULT STREAM, WHICH ON WDDM IS THE SLOW PATH.** All four session // calls - `session_capture`, `session_replay`, `session_token` and `session_loop` - were handed `nullptr`, // i.e. stream 0. `bench/micro/kernel_costs.cu` measures what that costs: EVERY kernel it launches through // a wrapper comes back at 28-31 us REGARDLESS OF SIZE, `scale_inplace` on 2,048 floats and `silu_inplace` // on 10,240 floats being indistinguishable, which is a fixed per-launch cost and not execution. // `bench/micro/graph_node_cost.cu` measures the same kernels on a real stream at 3.63 us ungrapped and // 0.805 us inside a graph. **That is an ~8x penalty on every launch in the engine.** cudaStream_t main_stream = nullptr; if (cudaStreamCreateWithFlags(&main_stream, cudaStreamNonBlocking) != cudaSuccess) { std::fprintf(stderr, "strata generate: cannot create the main stream\n"); return 1; } void* const main_cs = (void*) main_stream; if (strata::core::session_init(g, o.max_context, K, sbuf, ss) == 0) { std::fprintf(stderr, "strata generate: session_init failed\n"); return 1; } if (g.n_qsa_layers() > 0 && ss.qsa_states[0].kv_mode == 1) std::fprintf(stderr, "strata generate: KV streaming: %lld of %lld cells per QSA layer in VRAM, the K/V in " "%.2f GiB of pinned RAM\n", (long long) (ss.qsa_states[0].n_slots * 4), (long long) o.max_context, (double) strata::core::qsa_kv_host_bytes() / 1073741824.0); // ---- **THE HALF-LEVEL DUMP HAS TO BE ARMED BEFORE `session_capture`, AND THE LADDER MUST NOT BE.** The // half copies are issued from inside `block_layer_pre`/`block_layer_post`, so they are only ever enqueued // while a graph is being CAPTURED - arming `ss.block.dump` afterwards would produce a file of zeros that // reads exactly like a wrong answer. The ladder is the opposite: `session_loop` enqueues it per token on // the replay stream, so it must be armed after capture to stay out of the graph. const uint64_t half_stride = (uint64_t) 2 * g.n_embd + (uint64_t) 2 * g.hc + (uint64_t) g.n_head * g.head_dim + (uint64_t) 5 * g.n_head_kv * g.head_dim + 8; std::FILE* half_dump = nullptr; float* half_stage = nullptr; if (!o.dump_halves.empty()) { half_dump = std::fopen(o.dump_halves.c_str(), "wb"); if (half_dump == nullptr) { std::fprintf(stderr, "strata generate: cannot write %s\n", o.dump_halves.c_str()); return 1; } const size_t n = (size_t) g.n_layers * (size_t) half_stride; if (cudaHostAlloc((void**) &half_stage, n * sizeof(float), cudaHostAllocDefault) != cudaSuccess) { std::fprintf(stderr, "strata generate: cannot pin the half-dump staging buffer\n"); return 1; } ss.block.dump = half_stage; } strata::core::Doorbell db; if (strata::core::doorbell_init(g, K, db) == 0) { std::fprintf(stderr, "strata generate: doorbell_init failed\n"); return 1; } ss.db = &db; // ================================ THE PLE ================================ // // **ITS ABSENCE IS WHY GATE C1 FAILED** (LEDGER L123): layer 1 carries six `blk.1.ple_*` tensors, the whole // module was built and parity-tested, and nothing called it. Everything below is construction - the table // is a mapping of the ORIGINAL second GGUF shard, the six weights are already loaded in the arena, and the // three buffers are the only allocation. strata::kernels::PleTable ple_table; std::vector ple_emb_host((size_t) strata::kernels::NG_N_EMBD); float* ple_emb_dev = nullptr; float* ple_scratch = nullptr; if (!o.ple_gguf.empty()) { strata::kernels::PleIoOptions pio; pio.mode = o.ple_io == "mmap" ? strata::kernels::PleIo::Mmap : strata::kernels::PleIo::Direct; pio.max_inflight = (uint32_t) o.ple_inflight; pio.cache_rows = (uint64_t) o.ple_row_cache; pio.io_thread = !o.ple_sync_submit; if (!ple_table.open(o.ple_gguf, err, pio)) { std::fprintf(stderr, "strata generate: %s\n", err.c_str()); return 1; } const strata::core::WeightRef* wk = wt.find("blk.1.ple_key.weight"); const strata::core::WeightRef* wv = wt.find("blk.1.ple_value.weight"); const strata::core::WeightRef* wnk = wt.find("blk.1.ple_norm_key.weight"); const strata::core::WeightRef* wnq = wt.find("blk.1.ple_norm_query.weight"); const strata::core::WeightRef* wnc = wt.find("blk.1.ple_norm_conv.weight"); const strata::core::WeightRef* wc = wt.find("blk.1.ple_conv1d.weight"); if (!wk || !wv || !wnk || !wnq || !wnc || !wc) { std::fprintf(stderr, "strata generate: the pack has no blk.1.ple_* tensors, so the PLE cannot be " "wired - and running without it is a DIFFERENT MODEL (LEDGER L123)\n"); return 1; } // `ple_key` is S2 and the loader has already widened its scales to f32, so the two planes are located // by the sizes the `WeightRef` records rather than re-derived - the same rule `plane_ptrs` follows. if (!wk->quantized()) { // plan v0.3 P6: the IQ model files' BF16 key (the pack's extra.bin, raw BF16) ss.ple.w.key_bf16 = (const uint16_t*) wk->data; } else if (wk->data != nullptr) { ss.ple.w.key_codes = (const uint8_t*) wk->data; ss.ple.w.key_scales = (const float*) ((const uint8_t*) wk->data + wk->codes_bytes); } if (o.native_ple_key && wk->quantized()) { if (!wk->native_data || wk->native_type != 42 || !wk->native_q8_1) { std::fprintf(stderr, "strata generate: native PLE key is absent or incompatible\n"); return 1; } ss.ple.w.key_native_data = wk->native_data; ss.ple.w.key_native_type = wk->native_type; ss.ple.w.key_native_q8_1 = wk->native_q8_1; } ss.ple.w.value_bf16 = (const uint16_t*) wv->data; ss.ple.w.norm_key = (const float*) wnk->data; ss.ple.w.norm_query = (const float*) wnq->data; ss.ple.w.norm_conv = (const float*) wnc->data; ss.ple.w.conv1d_f16 = (const uint16_t*) wc->data; ss.ple.consts = strata::kernels::ple_artifact_consts(); if (o.ple_delay_us > 0) ple_table.set_injected_delay_us(o.ple_delay_us); ss.ple.table = &ple_table; ss.ple.token = &ss.ple_token; ss.ple.prev = ss.ple_prev; ss.ple.hist = ss.ple_hist; ss.ple.emb_host = ple_emb_host.data(); if (cudaMalloc((void**) &ple_emb_dev, (size_t) strata::kernels::NG_N_EMBD * 4) != cudaSuccess || cudaMalloc((void**) &ple_scratch, strata::core::ple_run_scratch_bytes()) != cudaSuccess) { std::fprintf(stderr, "strata generate: the PLE buffers failed\n"); return 1; } ss.ple.emb_dev = ple_emb_dev; ss.ple.scratch = ple_scratch; if (!ss.ple.ready()) { std::fprintf(stderr, "strata generate: the PLE run is not ready after construction\n"); return 1; } std::fprintf(stderr, "strata generate: PLE on, table %llu rows of %s\n", (unsigned long long) ple_table.rows(), o.ple_gguf.c_str()); } else { std::fprintf(stderr, "strata generate: PLE OFF by explicit --no-ple diagnostic request.\n" " The tokens below are NOT this model's; this is only useful for A/B measurement.\n"); } float* d_parts = nullptr; if (cudaMalloc(&d_parts, (size_t) K * g.n_embd * 4) != cudaSuccess || cudaMemset(d_parts, 0, (size_t) K * g.n_embd * 4) != cudaSuccess) { std::fprintf(stderr, "strata generate: the parts buffer failed\n"); return 1; } // ---- the CPU expert pool // // R2.1: the experts are loaded into a RESIDENT ARENA by default. The mmap path is kept behind // `--mmap-experts` because it is the A/B arm, not because it is competitive. // // The reasoning is the review's C1 and it is now measured on both sides. `FileExpertSource` maps the 34 GB // file, and mapped file pages are the first thing the OS reclaims; the engine's rate then depends on whether // the standby list happens to hold `experts.bin`, which is why two consecutive runs of the SAME BINARY with // the SAME FLAGS measured 71.97 and 34.78 ms/token in the pool (7.54 vs 12.18 tok/s). The arena is // anonymous memory the engine owns, and the pool runs at 19.41 ms/token - 1.79x better than the warm mmap // and 3.7x better than the cold one. // // IT IS NOT PINNED, and that is reported rather than hidden: `cudaHostRegister` on 31.64 GiB fails with // "out of memory" (you cannot pin 34 of 63 GB) and the arena falls back to 4 KB anonymous pages. That is // fine for the CPU pool - which is all that exists today - and NOT fine for Phase 3, whose cache fills and // CPU/PCIe miss split need the GPU to DMA out of this arena. Read `note()` when that lands. // // The earlier "the arena does not fit" conclusion was WRONG and is worth recording: the failure was a stale // CUDA error left set by the failed `cudaHostRegister` and read later by `gr_read`'s launch check. See the // note in `pinned.cu`. strata::core::FileExpertSource src; strata::core::ArenaExpertSource arena_src; strata::core::ExpertSource* srcp = nullptr; if (o.mmap_experts) { if (!src.open(o.pack, g.n_layers, g.n_expert, err)) { std::fprintf(stderr, "strata generate: %s\n", err.c_str()); return 1; } std::fprintf(stderr, "strata generate: experts via mmap (--mmap-experts; the A/B arm of R2.1)\n"); srcp = &src; } else { arena_src.set_gguf(o.native_preset); // plan v0.3 P6: a native pack may take its experts from shard 1 if (!arena_src.open(o.pack, g.n_layers, g.n_expert, /*threads=*/6, err)) { std::fprintf(stderr, "strata generate: %s\n", err.c_str()); return 1; } std::fprintf(stderr, "strata generate: expert arena: %s\n", arena_src.note().c_str()); std::fprintf(stderr, "strata generate: loaded %.2f GiB at %.2f GiB/s\n", (double) strata::kernels::cpu::expert_layout().total / (1024.0 * 1024 * 1024), arena_src.load_gib_per_second()); srcp = &arena_src; } // Plan v0.3 P6: the MTP draft layer, loaded before the VRAM expert tier is sized from what is left. strata::core::MtpDrafter mtp; if (!o.mtp.empty()) { if (o.spec < 2) { std::fprintf(stderr, "strata generate: --mtp is ignored without --spec T (T >= 2)\n"); o.mtp.clear(); } if (!o.mtp.empty()) mtp.set_prompt_len((int64_t) o.tokens.size()); // the draft layer is the canonical model's MTP head (512 experts) even when the target is pruned, // so it always sees the canonical geometry; `static` because MtpDrafter keeps a reference static const strata::core::ModelGeometry draft_geometry{}; if (!o.mtp.empty() && !mtp.load(o.mtp, draft_geometry, ss, o.spec, err, o.mtp_window)) { std::fprintf(stderr, "strata generate: %s\n", err.c_str()); return 1; } } strata::kernels::cpu::ExpertPool pool(o.pool_workers, /*pin=*/true, /*host_works=*/!o.no_host_worker); if (o.no_ple_prefetch) strata::kernels::ple_prefetch_enable(false); // ---- R4's slot storage. Allocated AFTER the weights and the session, so `cudaMemGetInfo` inside `open` // sees the memory this process actually has left rather than the card's idle figure - and refuses with both // numbers if the slots do not fit, instead of handing back a cache smaller than it was asked for. mem_mark("the weights, the session and the drafter"); strata::core::ExpertCache xcache; std::vector> profile; if (!o.expert_profile.empty()) { int64_t pslots = 0; if (!strata::core::read_expert_profile(o.expert_profile, g.n_layers, g.n_expert, profile, pslots, err)) { std::fprintf(stderr, "strata generate: %s\n", err.c_str()); return 1; } // An explicit number truncates the ranked list ("what would 2,000 slots give" without rebuilding the // file). `--expert-cache 0` used to take the count the profile was built for; the profile now ranks // every pair (issue #46: a card that holds more than the old 8,000 used to stop there), so it means auto. if (o.expert_cache == 0) o.expert_cache = -1; std::fprintf(stderr, "strata generate: profile %s: %zu ranked pairs, built for %lld slots\n", o.expert_profile.c_str(), profile.size(), (long long) pslots); } // THE HEAD BEFORE THE CACHE. The expert cache takes what is free minus the reserve, so everything allocated // after it comes out of the reserve. The native head (~0.5 GB with IQ3_S) was loaded after it and ate most of // the 700 MiB: 128K IQ3_S ended with 30 MiB free, the driver paged, and a request stalled for good at its first // verify window. Loaded first, the cache is sized around it. const strata::core::WeightRef* wo = wt.find("output.weight"); if (wo == nullptr) { std::fprintf(stderr, "strata generate: output.weight is missing\n"); return 1; } const int64_t n_vocab = wo->ne1; strata::core::NativeHead native_head; if (!o.native_head_gguf.empty()) { if (!native_head.load(o.native_head_gguf, g.n_embd, n_vocab, err)) { std::fprintf(stderr, "strata generate: %s\n", err.c_str()); return 1; } std::fprintf(stderr, "strata generate: experimental native Q5_K head, %llu bytes\n", (unsigned long long) native_head.weight_bytes()); } std::vector logits((size_t) n_vocab); float* d_logits = nullptr; if (cudaMalloc(&d_logits, (size_t) n_vocab * 4) != cudaSuccess) { std::fprintf(stderr, "strata generate: the logits buffer failed\n"); return 1; } const bool auto_cache = o.expert_cache < 0; if (o.expert_cache < 0) { size_t free_b = 0, total_b = 0; cudaMemGetInfo(&free_b, &total_b); // Plan v0.3 P5: the batched prompt path's chunk buffers are allocated later, so they are reserved here - // under WDDM an over-subscribed allocation does not fail, it pages to system memory and crawls. // (with borrowing - the default with a profile - the prompt path lends cache slots instead) const bool borrow = !o.no_prefill_borrow && !o.expert_profile.empty(); const int64_t prefill_mib = (o.prefill_chunk > 0 && !borrow) ? 160 + (o.prefill_chunk * 680) / 1024 : 0; const int64_t reserve = ((int64_t) o.vram_reserve_mib + prefill_mib) << 20; int64_t slots = ((int64_t) free_b - reserve) / (int64_t) strata::kernels::cpu::expert_layout().max_blob; if (!profile.empty()) slots = std::min(slots, (int64_t) profile.size()); o.expert_cache = (int) std::max(slots, 0); std::fprintf(stderr, "strata generate: expert cache auto: %.2f GiB free, %d MiB reserved -> %d slots\n", (double) free_b / 1073741824.0, o.vram_reserve_mib, o.expert_cache); } // plan v0.3 P6: a native pack's blobs differ per layer, so with a profile its slots are sized per pair: the // same VRAM holds ~30% more IQ3_XXS experts than slots of the largest blob would std::vector sized_slots; if (native_pack && o.expert_cache > 0 && !profile.empty()) { size_t free_b = 0, total_b = 0; cudaMemGetInfo(&free_b, &total_b); const auto& lay = strata::kernels::cpu::expert_layout(); const uint64_t budget = (uint64_t) o.expert_cache * lay.max_blob; // what the uniform sizing granted uint64_t used = 0; size_t free_room = free_b > ((size_t) o.vram_reserve_mib << 20) ? free_b - ((size_t) o.vram_reserve_mib << 20) : 0; const uint64_t cap = std::min(budget, (uint64_t) free_room); for (const auto& pr : profile) { const uint64_t b = (lay.blob_bytes(pr.first) + 255) / 256 * 256; if (used + b > cap) break; used += b; sized_slots.push_back((int64_t) lay.blob_bytes(pr.first)); } o.expert_cache = (int) sized_slots.size(); } if (o.expert_cache > 0) { // With `--expert-cache auto` the reserve must still be free once the slots are WRITTEN: under WDDM an // allocation is not resident until it is touched, and the free figure read before it can be ~1 GB too // high. A cache sized from it filled the card to 0 MiB, the driver then paged, and a request that needed a // page back while the verify graph spun on a host flag never finished. So the slots are zeroed and the // free figure read again; while it is short of the reserve the cache is reopened smaller. for (int attempt = 0;; ++attempt) { const bool ok = sized_slots.empty() ? xcache.open(o.expert_cache, g.n_layers, g.n_expert, (int64_t) strata::kernels::cpu::expert_layout().max_blob, err) : xcache.open_sized(sized_slots, g.n_layers, g.n_expert, err); if (!ok) { std::fprintf(stderr, "strata generate: %s\n", err.c_str()); return 1; } if (!auto_cache || attempt >= 6) break; cudaMemset(xcache.device_slot(0), 0, (size_t) xcache.bytes()); cudaDeviceSynchronize(); size_t free_b = 0, total_b = 0; cudaMemGetInfo(&free_b, &total_b); const int64_t want = (int64_t) o.vram_reserve_mib << 20; if ((int64_t) free_b >= want - (64ll << 20)) break; // short by (want - free); a figure of 0 only says "at least", so then give back a quarter as well int64_t give = want - (int64_t) free_b + (64ll << 20); if (free_b < ((size_t) 16 << 20)) give = std::max(give, xcache.bytes() / 4); const int64_t keep_bytes = xcache.bytes() - give; std::fprintf(stderr, "strata generate: only %lld MiB free once the slots are written (reserve %d MiB); " "shrinking the expert cache\n", (long long) (free_b >> 20), o.vram_reserve_mib); xcache.close(); if (keep_bytes <= 0) { o.expert_cache = 0; sized_slots.clear(); break; } if (!sized_slots.empty()) { int64_t used = 0; size_t keep = 0; while (keep < sized_slots.size() && used + (sized_slots[keep] + 255) / 256 * 256 <= keep_bytes) used += (sized_slots[keep++] + 255) / 256 * 256; sized_slots.resize(keep); o.expert_cache = (int) keep; } else { o.expert_cache = (int) (keep_bytes / (int64_t) strata::kernels::cpu::expert_layout().max_blob); } if (o.expert_cache <= 0) { o.expert_cache = 0; sized_slots.clear(); break; } } } if (o.expert_cache > 0) { std::fprintf(stderr, "strata generate: expert cache %lld slots, %.2f GiB of VRAM; policy is\n", (long long) xcache.slots(), xcache.gib()); mem_mark("opening the expert cache"); xcache.set_per_layer_admission(o.expert_cache_per_layer); // Round 328 warned here that the GPU hit path was wrong (tokens diverged from a cache-off run from // token 0). That fault was fixed long since (native_expert_parity, expert_parity, the grouped kernels' // tests), and the warning outlived it (issue #23). What remains is rounding: a GPU expert and the CPU's // compute the same quantized expert with different float order, so a near-tie can flip. Measured teacher- // forced on 2,557 tokens (bench/results/2026-09-27-cache-parity): 95-98% same top-1, and perplexity equal // (on - off = -0.005 +- 0.005 nats). Neither output is more correct than the other. std::fprintf(stderr, "strata generate: the GPU computes the experts in the cache; it rounds differently from the CPU,\n" " so a reply can differ slightly from a run without the cache (same quality:\n" " bench/results/2026-09-27-cache-parity).\n"); if (o.expert_cache_per_layer) { int64_t lo = 0, hi = 0; xcache.layer_slot_range(0, lo, hi); std::fprintf(stderr, " R4.2g PER-LAYER: each layer owns %lld slots (%lld..%lld).\n", (long long) (hi - lo), (long long) lo, (long long) (hi - 1)); } else if (profile.empty()) { std::fprintf(stderr, " compulsory-miss (fills with whatever the run routes first).\n"); } else { std::fprintf(stderr, " PROFILE, ranked by routing frequency, no eviction.\n"); } } // ---- R4.2e: fill the tier from the profile. This is the only place the plan is applied, and it runs // ONCE: with `slots` pairs and `slots` slots the cache is full when this returns, so the decode-time // admission finds no room and every non-profiled expert stays a CPU miss. That is what makes the profile // the policy rather than a hint. int64_t prefilled = 0; if (!profile.empty() && srcp != nullptr) { const int64_t want = std::min((int64_t) profile.size(), xcache.slots()); for (int64_t i = 0; i < want; ++i) { const int32_t slot = xcache.admit(profile[(size_t) i].first, profile[(size_t) i].second); if (slot == strata::core::kNotResident) break; const uint8_t* b = srcp->blob(profile[(size_t) i].first, profile[(size_t) i].second); if (b == nullptr || !xcache.fill_slot_blocking(slot, b, err, (int64_t) strata::kernels::cpu::expert_layout().blob_bytes(profile[(size_t) i].first))) { std::fprintf(stderr, "strata generate: the profile fill failed at pair %lld: %s\n", (long long) i, err.c_str()); return 1; } ++prefilled; } // **AND ONE SLOT IS READ BACK AND COMPARED.** A residency table that is right about indices and wrong // about bytes produces a plausible token, which is this project's most expensive failure mode; the // cache's own `verify_slot` is the check and it costs one 1.38 MB D2H at startup. if (prefilled > 0 && !xcache.verify_slot(xcache.slot_of(profile[0].first, profile[0].second), srcp->blob(profile[0].first, profile[0].second), err, (int64_t) strata::kernels::cpu::expert_layout().blob_bytes(profile[0].first))) { std::fprintf(stderr, "strata generate: %s\n", err.c_str()); return 1; } mem_mark("the profile fill"); std::fprintf(stderr, "strata generate: pre-filled %lld of %lld slots from the profile; slot 0 verified\n", (long long) prefilled, (long long) want); } Drive drive; drive.d.hit_cpu_order = o.expert_cache_cpu_order; drive.d.split_rows = !o.no_split_rows; drive.d.pool = &pool; drive.d.src = srcp; drive.d.n_expert = g.n_expert; drive.d.jobs.resize((size_t) K); // ---- R4.2c: THE HIT PATH. Every one of these is required for `hits_ready()`, which is all-or-nothing on // purpose: a half-configured hit path would compute some experts twice and others not at all, and a token // built on that is wrong rather than refused. void* hit_scratch = nullptr; int32_t* d_hit_slot = nullptr; int32_t* d_hit_dst = nullptr; uint8_t* d_hit_q8 = nullptr; float* d_hit_q8_scale = nullptr; ///< R4.2h: the fp32 activation scales the CPU path also uses float* d_hit_out = nullptr; if (o.expert_cache > 0 && !o.no_pool) { const uint64_t sb = strata::kernels::moe_hit_grouped_scratch_bytes(K, g.n_embd, strata::kernels::cpu::FF); if (cudaMalloc(&hit_scratch, (size_t) sb) != cudaSuccess || cudaMalloc((void**) &d_hit_slot, (size_t) K * sizeof(int32_t)) != cudaSuccess || cudaMalloc((void**) &d_hit_dst, (size_t) K * sizeof(int32_t)) != cudaSuccess || cudaMalloc((void**) &d_hit_q8, (size_t) (g.n_embd / 32) * 34) != cudaSuccess || // R4.2h: the fp32 activation scales. Without this the GPU's hits use the block's fp16 `d` // while the CPU's misses use `ActQ::scale`, which is fp32 - a 4.761e-04 relative disagreement on // every chunk, and the reason enabling the cache changed the tokens. cudaMalloc((void**) &d_hit_q8_scale, (size_t) (g.n_embd / 32) * sizeof(float)) != cudaSuccess || cudaMalloc((void**) &d_hit_out, (size_t) K * g.n_embd * 4) != cudaSuccess) { std::fprintf(stderr, "strata generate: the R4 hit path could not allocate its device buffers\n"); return 1; } drive.d.cache = &xcache; drive.d.cache_stream = main_cs; drive.d.cache_base = (const uint8_t*) xcache.device_slot(0); drive.d.cache_blob = (int64_t) strata::kernels::cpu::expert_layout().max_blob; drive.d.cache_slot_off = xcache.slot_offsets(); drive.d.hit_scratch = hit_scratch; drive.d.parts_out = d_parts; drive.d.hit_out = d_hit_out; drive.d.parts_elems = K * g.n_embd; drive.d.mixed = ss.block.mixed; drive.d.x_q8_0_hit = d_hit_q8; drive.d.x_q8_0_hit_scale = d_hit_q8_scale; drive.d.d_slot = d_hit_slot; drive.d.d_dst = d_hit_dst; drive.d.h_slot.resize((size_t) K); cudaEvent_t hit_done = nullptr; if (cudaEventCreate(&hit_done) != cudaSuccess) { std::fprintf(stderr, "strata generate: the hit path could not create its probe event\n"); return 1; } drive.d.hit_done = (void*) hit_done; drive.d.hit_poke = !o.no_hit_poke; drive.d.h_dst.resize((size_t) K); mem_mark("the R4 hit path"); std::fprintf(stderr, "strata generate: R4 hit path ON - resident experts are computed on the GPU\n"); } // ---- P0.S8: the routing trace. Only meaningful with the pool running, because the ids arrive through // the doorbell that the pool consumes - so `--no-pool` is refused rather than silently producing an empty // file that would read as "the router selected nothing". // ---- PER-STAGE TIMING. `--no-capture` only: an event recorded inside a stream capture is silently // dropped, so a captured graph cannot carry these events and the numbers would be zeros that read as // "every stage is free". Refusing is the fix. if (o.stage_timing) { if (!o.no_capture) { std::fprintf(stderr, "strata generate: --stage-timing records CUDA events inside the layer path, " "and an event record inside a stream capture is silently dropped. Pass " "--no-capture as well.\n"); return 2; } if (!strata::core::stage_timing_enable()) { std::fprintf(stderr, "strata generate: stage_timing_enable failed\n"); return 1; } strata::core::stage_timing_name(0, "gr_read (attn)"); strata::core::stage_timing_name(1, "attention block"); strata::core::stage_timing_name(2, "gr_write (attn)"); strata::core::stage_timing_name(3, "gr_read (ffn)"); strata::core::stage_timing_name(4, "moe_route"); strata::core::stage_timing_name(5, "moe_finish"); strata::core::stage_timing_name(6, "gr_write (ffn)"); // The GDN block's internals. It is 36 of the 48 layers, 0.96 ms each, and its entire weight traffic // is ~26 MB - so ~0.11 ms at the measured read rate. ~13 tiny latency-bound launches live in it and // a single "attention block" number cannot say which one costs anything. strata::core::stage_timing_name(8, " gdn: quantize x"); strata::core::stage_timing_name(9, " gdn: qkv gemv"); strata::core::stage_timing_name(10, " gdn: conv+silu"); strata::core::stage_timing_name(11, " gdn: l2 norms"); strata::core::stage_timing_name(12, " gdn: alpha/beta/gate"); strata::core::stage_timing_name(13, " gdn: gdn_step"); strata::core::stage_timing_name(14, " gdn: z + out_norm"); strata::core::stage_timing_name(15, " gdn: out gemv"); } std::FILE* routing = nullptr; if (!o.dump_routing.empty()) { if (o.no_pool) { std::fprintf(stderr, "strata generate: --dump-routing needs the expert pool; the routed ids reach " "the host through the doorbell the pool reads. Drop --no-pool.\n"); return 2; } routing = std::fopen(o.dump_routing.c_str(), "wb"); if (routing == nullptr) { std::fprintf(stderr, "strata generate: cannot write %s\n", o.dump_routing.c_str()); return 1; } drive.routing = routing; } strata::core::PoolFn pool_fn = o.no_pool ? nullptr : &drive_pool; // The hit hook rides the same switch as the pool: with no pool there is no `parts` staging to // write into, and a hit path with nowhere to write is a wrong token rather than an error. strata::core::HitFn hit_fn = (o.no_pool || o.expert_cache <= 0) ? nullptr : &strata::core::expert_hit_run; void* pool_user = o.no_pool ? nullptr : (void*) &drive; std::fprintf(stderr, "strata generate: %d expert-pool workers%s%s\n", pool.workers(), pool.host_works() ? " + the host thread" : "", o.no_pool ? " (UNUSED: --no-pool)" : ""); // **THE MISALIGNMENT WARNING THAT STOOD HERE IS GONE, BECAUSE THE MISALIGNMENT IS FIXED.** // // It said the tokens were not the model's, and it was true: `session_loop` handed layer `l`'s expert // outputs to layer `l+1`, which multiplied them by layer `l+1`'s router weights (LEDGER L100). The loop // now runs a captured PAIR per layer - `pre[l]` ending with the router and the doorbell, then the CPU // pool, then `post[l]` which combines those experts with THAT layer's weights - so layer `l`'s experts meet // layer `l`'s routing. The generated ids changed the moment it landed, which is what a correctness fix // looks like from the outside. // // The cost is real and is recorded rather than hidden: the window for the CPU pool is now whatever GPU // work follows the ring inside `pre[l]`, which is the shared expert and nothing else - 0.038 ms against // 0.514 ms of CPU work per layer. A per-layer CPU expert pool cannot be hidden behind a strictly serial // residual chain; the CPU term is answered by Phase 3's VRAM expert cache, not by this pipeline. // ---- the graphs strata::core::SessionGraphs gr; if (!o.no_capture && !native_pack) { // plan v0.3 P6: a native pack runs verify windows only if (!strata::core::session_capture(wt, g, ss, d_parts, gr, err, /*split=*/o.gpu_stages)) { std::fprintf(stderr, "strata generate: session_capture: %s\n", err.c_str()); return 1; } } // **`--no-capture` AND THE EXPERTS ARE MUTUALLY EXCLUSIVE, AND SILENTLY SO.** // // The CPU expert pool is wired into `session_loop` - the host loop around the captured graphs - and // `session_token` has no pool hook at all. So `--no-capture` did not merely change HOW the layers were // launched: it ran the whole model with `parts` left at whatever the buffer held, which is ZERO, and the // only symptom was `expert blobs 0` in a stats line nobody had to read. A run that silently omits the // routed experts is not a slow measurement of this model, it is a measurement of a different model. // // Refusing is the fix. `--no-pool` is the explicit way to say "I want the GPU-only floor". if (o.no_capture && !o.no_pool) { std::fprintf(stderr, "strata generate: --no-capture runs `session_token`, which has NO CPU expert pool hook, so " "the routed experts would silently contribute nothing. Pass --no-pool as well if the " "GPU-only floor is what you want.\n"); return 2; } // The ladder is written by `session_loop`, and `session_token` does not touch the staging buffer at all - so // accepting the flag there would produce a file of uninitialised memory, which reads as a wrong answer rather // than as a mistake. `--no-capture` without `--no-pool` is already refused above, so this catches the pair. if (o.no_capture && !o.dump_layers.empty()) { std::fprintf(stderr, "strata generate: --dump-layers is written by `session_loop`; `--no-capture` runs " "`session_token` instead, which never fills the staging buffer. Drop one of the two.\n"); return 2; } if (o.no_capture && !o.dump_halves.empty()) { std::fprintf(stderr, "strata generate: --dump-halves is CAPTURED into the layer graphs, so it needs the " "captured path; `--no-capture` never records it. Drop one of the two.\n"); return 2; } mem_mark("the expert cache and the graphs"); std::fprintf(stderr, "strata generate: session is up (engine %s)\n", STRATA_VERSION); auto run_head = [&](void* stream) -> bool { if (!native_head.loaded()) return strata::core::lm_head(wt, g, ss.block, d_logits, stream, err); return strata::core::lm_head_mix(wt, g, ss.block, stream, err) && native_head.run(ss.block.mixed, d_logits, stream, err); }; float* d_emb = nullptr; if (cudaMalloc(&d_emb, (size_t) g.n_embd * 4) != cudaSuccess) { std::fprintf(stderr, "strata generate: the embedding buffer failed\n"); return 1; } // **`sample_tokens` TAKES DEVICE POINTERS.** It is a kernel launch; `logits` and `out` are both read and // written on the device. Passing `logits.data()` - the host vector - faults inside the kernel and the // error surfaces at the NEXT synchronising call, which here was the next token's `embed_row`, reporting an // illegal access on a weight plane. Nothing in the parameter names said device. int* d_next = nullptr; if (cudaMalloc(&d_next, sizeof(int)) != cudaSuccess) { std::fprintf(stderr, "strata generate: the sampler output buffer failed\n"); return 1; } // **`R` IS BOTH THE INPUT AND THE OUTPUT, SO THE NEW TOKEN'S EMBEDDING HAS TO REPLACE THE OLD RESIDUAL.** // At `pos == 0` that is `session_zero`, which is the reference's own initial condition - the embedding // broadcast to all `hc` streams. After that `session_zero` would also wipe the recurrence, so the // broadcast is done directly. Getting this wrong is invisible for exactly one token. void* token_stream = o.stream_token ? main_cs : nullptr; auto put_input = [&](int64_t tok, int64_t pos) -> bool { if (!strata::core::embed_row(wt, g, tok, d_emb, token_stream, err)) { std::fprintf(stderr, "strata generate: %s\n", err.c_str()); return false; } if (pos == 0) { strata::core::session_zero(ss, g, d_emb, token_stream); } else { for (int64_t c = 0; c < g.hc; ++c) if (cudaMemcpyAsync(ss.R + (size_t) c * g.n_embd, d_emb, (size_t) g.n_embd * 4, cudaMemcpyDeviceToDevice, (cudaStream_t) token_stream) != cudaSuccess) { std::fprintf(stderr, "strata generate: the residual broadcast failed\n"); return false; } } return o.stream_token || cudaDeviceSynchronize() == cudaSuccess; }; strata::kernels::SamplerParams sp; sp.greedy = o.greedy; sp.seed = o.seed; sp.top_k = o.top_k; sp.top_p = o.top_p; sp.temperature = o.temperature; std::FILE* dump = nullptr; const int64_t dump_positions = (int64_t) o.tokens.size() - 1 + o.max_new; if (!o.dump_logits.empty()) { if (dump_positions > INT32_MAX || n_vocab > INT32_MAX) { std::fprintf(stderr, "strata generate: logits dump dimensions exceed int32\n"); return 2; } dump = std::fopen(o.dump_logits.c_str(), "wb"); if (dump == nullptr) { std::fprintf(stderr, "strata generate: cannot write %s\n", o.dump_logits.c_str()); return 1; } // **THE COUNT IS `n_prompt - 1 + max_new`, NOT `n_prompt + max_new`.** The loop writes one row per // position from 0, and it stops once `produced` holds `max_new` tokens - and `produced` only starts // receiving at position `n_prompt - 1`. So a 5-token prompt with `--max-new 6` writes 10 rows, and the // header used to claim 11. A header that describes a different file from the one written is the same // class of defect as a self-check that verifies the wrong invariant: anything reading the count instead // of the size gets a wrong answer that looks authoritative. `tools/logits_identical.py` caught it by // parsing the header and refusing the file. const int32_t n_rows = (int32_t) strata::program::logits_selection::row_count(dump_positions, o.logits_stride); const int32_t hdr[2] = {(int32_t) n_vocab, n_rows}; if (std::fwrite(hdr, sizeof hdr, 1, dump) != 1) { std::fprintf(stderr, "strata generate: cannot write logits header\n"); std::fclose(dump); return 1; } } // ---- THE C1 ORACLE: ONE RESIDUAL SNAPSHOT PER LAYER PER POSITION, so the engine can be bisected against // `llama-debug`'s `l_last-` node instead of against a single end-to-end perplexity. The buffer is // PINNED because `session_loop` enqueues a device-to-host copy into it after every layer and the transfer // would otherwise be staged through a pageable bounce buffer on the critical path. std::FILE* layer_dump = nullptr; float* layer_stage = nullptr; const size_t layer_floats = (size_t) (g.n_layers + 1) * (size_t) g.hc * (size_t) g.n_embd; if (!o.dump_layers.empty()) { layer_dump = std::fopen(o.dump_layers.c_str(), "wb"); if (layer_dump == nullptr) { std::fprintf(stderr, "strata generate: cannot write %s\n", o.dump_layers.c_str()); return 1; } if (cudaHostAlloc((void**) &layer_stage, layer_floats * sizeof(float), cudaHostAllocDefault) != cudaSuccess) { std::fprintf(stderr, "strata generate: cannot pin the layer-dump staging buffer\n"); return 1; } } // ---- prefill is the DECODE PATH ONE TOKEN AT A TIME, which `phase-2-correct-engine.md:12-13` says is // fine here: "process the prompt through the decode-style graphs in small batches; a 19K-token prompt will // take minutes". A real batched prefill is P2.S6's other half and is not this. // // **THE LOOP IS `feed -> 48 layers -> head -> sample -> feed`, AND THE FIRST GENERATED TOKEN COMES FROM THE // LAST *PROMPT* POSITION.** The first version sampled only on the decode positions, so `produced` was // still empty when the first generated position asked for `produced.back()` - an out-of-bounds read on an // empty vector. Teacher forcing below is what makes the distinction unnecessary to special-case: for every // position before the last prompt one, the next input is the PROMPT's next token, and after that it is the // sampled one. std::vector produced; double total_ms = 0; double prefill_ms = 0; // positions 0 .. n_prompt-2: prompt tokens that only condition const Clock::time_point t_start = Clock::now(); double ttft_ms = 0; const int64_t n_prompt = (int64_t) o.tokens.size(); int64_t tok = o.tokens[0]; // ---- THE PURE-GPU MEASUREMENT. `session_replay` launches all 48 `pre` graphs back to back on one stream // with NO host work between them - no doorbell poll, no pool, no parts copy - so what it times is the GPU // executing the layer sequence and nothing else. It had been declared, defined and never called since the // day it was written. // // **THIS IS THE MEASUREMENT THAT SAYS WHETHER THE ENGINE IS HOST-BOUND OR GPU-BOUND**, and the stage table // cannot answer it: those events measure the interval between two marks on a stream, which includes every // gap where the GPU sat idle waiting for the host to enqueue the next kernel. In `--no-capture` those gaps // are the host's launch latency and they are proportional to the KERNEL COUNT rather than to any work, so // the no-capture stage shares are shares of kernel count - which is why the attention block, with the most // kernels, looks like 55% of the token there. // ---- R0.9: THE PER-STAGE TABLE ON THE CAPTURED GRAPH. if (o.gpu_stages) { strata::core::doorbell_reset(db); double mix = 0, ffn = 0, post = 0; if (!strata::core::session_replay_stages(g, 0, 0, ss, gr, main_cs, mix, ffn, post, err)) { std::fprintf(stderr, "strata generate: %s\n", err.c_str()); return 1; } const int reps = 20; double t_mix = 0, t_ffn = 0, t_post = 0; for (int r = 0; r < reps; ++r) { if (!strata::core::session_replay_stages(g, 0, 0, ss, gr, main_cs, mix, ffn, post, err)) { std::fprintf(stderr, "strata generate: %s\n", err.c_str()); return 1; } t_mix += mix; t_ffn += ffn; t_post += post; } const double tot = t_mix + t_ffn + t_post; std::printf("\nper-stage GPU time on the CAPTURED graph, one token over %lld layers\n", (long long) g.n_layers); std::printf(" %-22s %9.3f ms/token %8.3f ms/layer %6.1f%%\n", "mixer (gr_read+attn+gr_write)", t_mix / reps, t_mix / reps / (double) g.n_layers, 100.0 * t_mix / tot); std::printf(" %-22s %9.3f ms/token %8.3f ms/layer %6.1f%%\n", "ffn front + router", t_ffn / reps, t_ffn / reps / (double) g.n_layers, 100.0 * t_ffn / tot); std::printf(" %-22s %9.3f ms/token %8.3f ms/layer %6.1f%%\n", "post (moe_finish+gr_write)", t_post / reps, t_post / reps / (double) g.n_layers, 100.0 * t_post / tot); std::printf(" %-22s %9.3f ms/token\n", "sum of the three", tot / reps); // ---- AND THE MIXER BY LAYER KIND, because 36 of the 48 are GDN and 12 are QSA and a total cannot // separate them. Round 309's uncaptured table put GDN at 10.88 ms for 36 layers against QSA's 4.56 for // 12, which would make the recurrence the largest single R3 target - and that table had `moe_finish` // wrong by 4x, so the ratio is re-derived here from the captured graph rather than inherited. { // **ACCUMULATED OVER `reps`, NOT MEASURED ONCE AND THEN DIVIDED.** The first version called the // per-layer replay a single time and printed `gdn / reps`, which reported GDN at 0.480 ms/token // against a mixer total of 14.039 - a factor of exactly `reps`, and the tell was that // 0.480 + 0.220 = 0.700 = 14.039 / 20. std::vector acc((size_t) g.n_layers, 0.0), per; double f2 = 0, p2 = 0; for (int r = 0; r < reps; ++r) { if (!strata::core::session_replay_stages_per_layer(g, 0, 0, ss, gr, main_cs, per, f2, p2, err)) { std::fprintf(stderr, "strata generate: %s\n", err.c_str()); return 1; } for (int64_t l = 0; l < g.n_layers; ++l) acc[(size_t) l] += per[(size_t) l]; } double gdn = 0, qsa = 0, worst = 0; int64_t ng = 0, nq = 0, worst_l = 0; for (int64_t l = 0; l < g.n_layers; ++l) { const double v = acc[(size_t) l] / reps; if (strata::core::is_qsa_layer(g, l)) { qsa += v; ++nq; } else { gdn += v; ++ng; } if (v > worst) { worst = v; worst_l = l; } } std::printf("\n the mixer by layer kind, averaged over %d runs\n", reps); std::printf(" %-22s %9.3f ms/token %8.3f ms/layer (%lld layers)\n", "GDN layers", gdn, ng ? gdn / (double) ng : 0.0, (long long) ng); std::printf(" %-22s %9.3f ms/token %8.3f ms/layer (%lld layers)\n", "QSA layers", qsa, nq ? qsa / (double) nq : 0.0, (long long) nq); std::printf(" %-22s layer %lld at %.3f ms\n", "worst mixer layer", (long long) worst_l, worst); std::printf(" %-22s %9.3f ms/token (must equal the mixer above)\n", "GDN + QSA", gdn + qsa); } // ================================ R0.11: THE FIVE STAGES SEPARATELY ================================ // // Prefixes 1..5 are replayed per layer with the residual restored between them, and consecutive // differences are the per-stage times. **THE CHECK IS THAT THE FIVE SUM TO THE THREE-GRAPH TOTAL** - // the same independent-restatement test that caught round 320's divide-by-reps bug, and it is the only // reason to believe a table built out of differences. { std::vector acc5(5, 0.0), per, s5; for (int r = 0; r < reps; ++r) { if (!strata::core::session_replay_stage_prefixes(g, 0, 0, ss, gr, main_cs, s5, per, err)) { std::fprintf(stderr, "strata generate: %s\n", err.c_str()); return 1; } for (int k = 0; k < 5; ++k) acc5[(size_t) k] += s5[(size_t) k]; } static const char* sn[5] = {"0 gr_read (attn)", "1 attention", "2 gr_write (attn)", "3 gr_read (ffn)", "4 moe_route (router)"}; const char* kind[5] = {"GR", "ATTN", "GR", "GR", "ROUTER"}; double tot5 = 0, gr_ms = 0; std::printf("\n the five stages separately, by differencing prefixes\n"); std::printf(" %-24s %-8s %10s %10s %8s\n", "stage", "kind", "ms/token", "ms/layer", "share"); for (int k = 0; k < 5; ++k) { const double v = acc5[(size_t) k] / reps; tot5 += v; if (k != 1 && k != 4) gr_ms += v; std::printf(" %-24s %-8s %10.3f %10.4f %7.1f%%\n", sn[k], kind[k], v, v / (double) g.n_layers, 0.0); } for (int k = 0; k < 5; ++k) { const double v = acc5[(size_t) k] / reps; (void) v; } std::printf(" %-24s %-8s %10.3f\n", "sum of the five", "", tot5); std::printf(" %-24s %-8s %10.3f <- R3.3's target is <= 3 ms for all four passes\n", "GR passes (0,2,3)", "GR", gr_ms); std::printf("\n the three-graph total above was %.3f ms/token; the five must account for it.\n", tot / reps); // ================================ R3.5c: THE SAME TABLE, A DIFFERENT WAY ================================ // // The differencing table above mixes five graphs per layer, so stage 4's interval carries the launch // of the FULL five-stage graph while stage 3's carries a four-stage one. This sweep launches ONE // graph type per layer and nothing else, so that bias cannot exist. **If the two disagree, the // difference IS the bias and this one is right** - and stage 4 is the router, so it is exactly the // number that must not be wrong. { double sweep[6] = {0, 0, 0, 0, 0, 0}; for (int k = 1; k <= 5; ++k) { double acc = 0, one = 0; for (int r = 0; r < reps; ++r) { if (!strata::core::session_replay_stage_sweep(g, 0, 0, ss, gr, main_cs, k, one, err)) { std::fprintf(stderr, "strata generate: %s\n", err.c_str()); return 1; } acc += one; } sweep[k] = acc / reps; } std::printf("\n the same five stages, by SWEEPING each prefix back to back (no graph switching)\n"); std::printf(" %-24s %10s %10s %12s\n", "stage", "prefix sweep", "difference", "bias"); static const char* sn2[5] = {"0 gr_read (attn)", "1 attention", "2 gr_write (attn)", "3 gr_read (ffn)", "4 moe_route (router)"}; for (int k = 0; k < 5; ++k) { const double sw = sweep[k + 1] - sweep[k]; const double df = acc5[(size_t) k] / reps; std::printf(" %-24s %10.3f %10.3f %11.1f%%\n", sn2[k], sw, df, sw != 0.0 ? 100.0 * (df / sw - 1.0) : 0.0); } std::printf(" %-24s %10.3f (full pre, 48 layers)\n", "prefix 5 total", sweep[5]); } } std::printf("\n compare `--gpu-only-full`, which replays the same work as TWO graphs per layer. The\n"); std::printf(" three sum slightly above it because each launch carries the driver's gap.\n"); strata::core::session_graphs_free(gr); strata::core::doorbell_free(db); cudaFree(d_next); return 0; } if (o.graph_only) { strata::core::doorbell_reset(db); // one warm pass so the first launch does not pay for page mapping if (!strata::core::session_replay(g, 0, 0, ss, gr, main_cs, err)) { std::fprintf(stderr, "strata generate: session_replay warm: %s\n", err.c_str()); return 1; } if (cudaDeviceSynchronize() != cudaSuccess) { std::fprintf(stderr, "strata generate: session_replay warm faulted\n"); return 1; } const int reps = 20; const Clock::time_point t0 = Clock::now(); for (int r = 0; r < reps; ++r) { if (!strata::core::session_replay(g, 0, 0, ss, gr, main_cs, err)) { std::fprintf(stderr, "strata generate: session_replay: %s\n", err.c_str()); return 1; } } if (cudaDeviceSynchronize() != cudaSuccess) { std::fprintf(stderr, "strata generate: session_replay faulted: %s\n", cudaGetErrorString(cudaGetLastError())); return 1; } const double ms = std::chrono::duration(Clock::now() - t0).count() / (double) reps; std::printf("pre graphs only %8.2f ms per token over %lld layers -> %.2f tok/s of GPU work\n", ms, (long long) g.n_layers, ms > 0 ? 1000.0 / ms : 0.0); std::printf(" %8.3f ms per layer\n", ms / (double) g.n_layers); return 0; } // ---- R0.3: THE TRUE PER-TOKEN GPU FLOOR. // // `--graph-only` above launches ONLY `gr.execs[l]`, the `pre` graphs. It omits the 48 `post` graphs - the // shared expert, the combine and the second `gr_write` - and the LM head. Everything this project published // as "39.8 ms pure GPU" came from that loop while being described as the whole GPU, which also made the // "host's share = 49.4 - 39.8 = 9.6 ms" figure wrong by however much the missing work costs. See // Memory/ERRORS.md A4/A5. // // This flag is what "pure GPU" has to mean, and it replaces that number everywhere. No pool runs, so // `parts` keeps whatever the buffer holds and the timing is GPU work alone. if (o.gpu_only_full) { strata::core::doorbell_reset(db); const int reps = 20; double ms_layers = 0, ms_head = 0; for (int r = -1; r < reps; ++r) { // r == -1 is the warm pass, not counted const Clock::time_point t0 = Clock::now(); if (!strata::core::session_replay_full(g, 0, 0, ss, gr, main_cs, err)) { std::fprintf(stderr, "strata generate: session_replay_full: %s\n", err.c_str()); return 1; } // The sync is INSIDE the interval on purpose: it is the wait for the GPU, so t1 - t0 is GPU time. if (cudaStreamSynchronize((cudaStream_t) main_cs) != cudaSuccess) { std::fprintf(stderr, "strata generate: gpu-only-full layers faulted: %s\n", cudaGetErrorString(cudaGetLastError())); return 1; } const Clock::time_point t1 = Clock::now(); if (!run_head(main_cs)) { std::fprintf(stderr, "strata generate: gpu-only-full lm_head: %s\n", err.c_str()); return 1; } if (cudaStreamSynchronize((cudaStream_t) main_cs) != cudaSuccess) { std::fprintf(stderr, "strata generate: gpu-only-full head faulted: %s\n", cudaGetErrorString(cudaGetLastError())); return 1; } const Clock::time_point t2 = Clock::now(); if (r < 0) continue; ms_layers += std::chrono::duration(t1 - t0).count(); ms_head += std::chrono::duration(t2 - t1).count(); } ms_layers /= (double) reps; ms_head /= (double) reps; const double ms = ms_layers + ms_head; std::printf("GPU floor pre+post+head %7.2f ms per token -> %.2f tok/s of GPU work\n", ms, ms > 0 ? 1000.0 / ms : 0.0); std::printf(" %8.3f ms layers (%lld x pre+post)\n", ms_layers, (long long) g.n_layers); std::printf(" %8.3f ms per layer\n", ms_layers / (double) g.n_layers); std::printf(" %8.3f ms LM head\n", ms_head); return 0; } // ---- **THE TOKEN PATH ALLOCATES NOTHING (P2.T10, review finding H3).** // // `session_loop` used to allocate its pinned staging buffer, its probe event and its host pin ON EVERY // TOKEN, and `cudaFreeHost` at the end of each call implicitly synchronises the device - so every token // finished with a device-wide sync nobody asked for. The scratch is created once here and reused; it also // owns the host pin for the whole session rather than taking and releasing it per token. strata::core::SessionLoopScratch loop_scratch; struct ScratchFree { strata::core::SessionLoopScratch* p; ~ScratchFree() { if (p != nullptr) p->free(); } } scratch_free{&loop_scratch}; // Initialised unconditionally, including under --no-pool: the loop validates the scratch it is handed, so // passing a default-constructed one is an error rather than a fallback. (It was, and the guard caught it - // which is the point of the guard.) One allocation at setup either way. if (!loop_scratch.init((size_t) K * g.n_embd * 4, err)) { std::fprintf(stderr, "strata generate: %s\n", err.c_str()); return 1; } // Plan v0.3 P3: the whole token as ONE graph whenever nothing needs a host step between the ring and post[l] // (the VRAM expert tier and the per-layer dumps do). `--no-token-graph` keeps two graphs per layer. strata::core::TokenGraph tgraph; struct TokenGraphFree { strata::core::TokenGraph* p; ~TokenGraphFree() { strata::core::token_graph_free(*p); } } tgraph_free{&tgraph}; // Plan v0.3 P4: with a PROFILE-filled cache the residency is static, so the hit decision moves onto the // device and the token graph keeps it. (A cache filled on demand still needs the per-layer host path.) std::vector host_res; int32_t* d_res = nullptr; int32_t* d_hit_count = nullptr; strata::core::TokenHits thits; const bool graph_hits = hit_fn != nullptr && !profile.empty() && !o.no_pool; if (graph_hits && !o.no_capture && !o.no_token_graph && layer_dump == nullptr && half_dump == nullptr) { host_res.assign((size_t) (g.n_layers * g.n_expert), strata::core::kNotResident); int64_t resident = 0; for (int64_t l = 0; l < g.n_layers; ++l) for (int64_t e = 0; e < g.n_expert; ++e) { const int32_t slot = xcache.slot_of(l, e); host_res[(size_t) (l * g.n_expert + e)] = slot; if (slot != strata::core::kNotResident) ++resident; } if (cudaMalloc((void**) &d_res, host_res.size() * sizeof(int32_t)) != cudaSuccess || cudaMalloc((void**) &d_hit_count, sizeof(int32_t)) != cudaSuccess || cudaMemcpy(d_res, host_res.data(), host_res.size() * sizeof(int32_t), cudaMemcpyHostToDevice) != cudaSuccess) { std::fprintf(stderr, "strata generate: the device residency table could not be staged\n"); return 1; } thits.d_res = d_res; thits.n_expert = g.n_expert; thits.cache_base = drive.d.cache_base; thits.blob = drive.d.cache_blob; thits.d_slot = drive.d.d_slot; thits.d_dst = drive.d.d_dst; thits.d_count = d_hit_count; thits.x_q8 = drive.d.x_q8_0_hit; thits.x_scale = drive.d.x_q8_0_hit_scale; thits.scratch = drive.d.hit_scratch; thits.hit_out = drive.d.hit_out; drive.d.host_res = host_res.data(); std::fprintf(stderr, "strata generate: token graph hit path: %lld resident experts, decided on the device\n", (long long) resident); } if (!o.no_capture && !o.no_token_graph && layer_dump == nullptr && half_dump == nullptr && (hit_fn == nullptr || thits.on()) && !native_pack) { if (!strata::core::session_capture_token(wt, g, ss, d_parts, loop_scratch.y_miss, loop_scratch.parts_bytes, tgraph, err, thits.on() ? &thits : nullptr)) { std::fprintf(stderr, "strata generate: %s\n", err.c_str()); return 1; } std::fprintf(stderr, "strata generate: token graph captured (48 layers, one launch per token)\n"); } // ================================ WHERE THE HOST TERM GOES, PER TOKEN ================================ // // **`--gpu-only-full` MEASURES THE 48 LAYER GRAPHS AND THE LM HEAD AND NOTHING ELSE.** It never enters // this loop, so it does not run `ple_stage_token`, `embed_row`, the whole-vocabulary logits readback, the // NaN scan or the sampler - and `--no-pool --stats` against that floor was being read as "the per-layer // round trip costs 12.4 ms" when an unknown part of it is per TOKEN, not per layer. That is the same error // the review catalogued as A4/A5, one level down: a difference between two measurements attributed to a // mechanism that neither of them isolates. // // Six accumulators, because the six have different fixes. Reported in `--stats` as ms/token. **The // boundary after the layer loop is the one that matters**: without it the head's interval swallows all 48 // layers and the report reads as "head = 50 ms", which is not a thing that can happen to 1.4 ms of GPU // work. That is not hypothetical - it is what the first version of this printed. double ms_ple = 0, ms_embed = 0, ms_layers = 0, ms_head = 0, ms_readback = 0, ms_sample = 0; int64_t phase_tokens = 0; // ================================ plan v0.3 P8: THE PERSISTENT ENGINE (--serve) ================================ // // The weights, the expert arena and the VRAM tier load once; then requests arrive on stdin, one per line, // // GEN // // and each generated token is written to stdout as `T ` as soon as its verify window is done, followed by // // DONE // // // Before that, `RESUME ` (n prompt tokens are not read again), `PP ` // after every prompt chunk, and `REUSED ` once the prompt is read. (`ERR ` instead when a request // cannot run; `STOP` ends the running request at its next step; `QUIT` ends the process.) A request continues // from the live session or the longest conversation checkpoint its prompt starts with (see ConvCheckpoint), // otherwise from an empty sequence (`session_zero`); the rest of the prompt goes through the batched prompt path // and its last token through the first verify window - the path all three model files share. Decoding is greedy. // The expert-cache slots (from the end of the cache) that hold the prompt path's buffers for a chunk, and the // bytes from the first of them to the end. // the share of expert bytes the arena could pin (sizes the prompt path's streamed ring and its lend cap) if (srcp != nullptr && o.prefill_chunk > 0) { uint64_t pinned = 0, total = 0; const auto& lay = strata::kernels::cpu::expert_layout(); for (int64_t l = 0; l < g.n_layers; ++l) for (int64_t e = 0; e < g.n_expert; ++e) { const uint64_t b = lay.blob_bytes(l); total += b; if (srcp->pinned(l, e)) pinned += b; } strata::prefill::Prefill::set_pinned_share(total ? (double) pinned / (double) total : 1.0); } auto lend_slots = [&](int64_t c) -> int64_t { const uint64_t need = strata::prefill::Prefill::bytes_needed(g, ss, c); const int64_t blob = (int64_t) strata::kernels::cpu::expert_layout().max_blob; int64_t k = (int64_t) ((need + (uint64_t) blob - 1) / (uint64_t) blob); if (xcache.slot_offsets() != nullptr) { // sized slots: take slots from the end until they hold `need` k = 0; while (k < xcache.slots() && (uint64_t) (xcache.bytes() - (int64_t) xcache.slot_offsets()[xcache.slots() - k]) < need) ++k; } return k; }; auto lend_bytes = [&](int32_t first) -> uint64_t { return xcache.slot_offsets() ? (uint64_t) (xcache.bytes() - (int64_t) xcache.slot_offsets()[first]) : (uint64_t) (xcache.slots() - first) * (uint64_t) strata::kernels::cpu::expert_layout().max_blob; }; // The prompt path's chunk and the slots it borrows for its buffers: the requested chunk halved until it fits, // or with --prefill auto the largest of kAutoChunks whose buffers take at most kAutoLendPct % of the slots (a // lent slot's expert is streamed during the prompt and refilled after it; measured on a 12 GB card, 32K Q2_0 // prompt: 4096 791 tok/s, 6144 878, 8192 973 with 69% of the slots lent). A request lends only what its own // prompt needs (Prefill::relayout), so a big chunk costs short prompts nothing. 0 = none fits. auto plan_lend = [&](int64_t& chunk) -> int64_t { static constexpr int64_t kAutoChunks[] = {8192, 6144, 4096, 3072, 2048, 1024, 512, 256}; // at 8192-token chunks nearly every expert streams anyway, so a lent slot costs little: 90% when the // copies are DMA from pinned RAM (Q2_0 8192 + a 384-slot ring: 1283 tok/s), 85% when host copies are the // limit (lending more only streams more through them). STRATA_PREFILL_LEND_PCT overrides (tuning). const int64_t kAutoLendPct = [] { const char* v = std::getenv("STRATA_PREFILL_LEND_PCT"); return v ? (int64_t) std::atoi(v) : (int64_t) (strata::prefill::Prefill::pinned_share() >= 0.9 ? 90 : 85); }(); auto slots_for = lend_slots; if (o.prefill_auto) { for (const int64_t c : kAutoChunks) { const int64_t k = slots_for(c); if (k + 128 <= xcache.slots() && k * 100 <= kAutoLendPct * xcache.slots()) { chunk = c; return k; } } return 0; } for (int64_t c = chunk; c >= 256; c /= 2) { const int64_t k = slots_for(c); if (k + 128 <= xcache.slots()) { chunk = c; return k; } } return 0; }; if (o.serve) { if (o.spec < 2 || o.mtp.empty() || o.prefill_chunk <= 0 || (graph_hits && (thits.d_res == nullptr || host_res.empty()))) { std::fprintf(stderr, "strata serve: needs --spec T, --mtp DIR and --prefill CHUNK (and a fillable " "--expert-cache; the graphed hit path additionally needs --expert-profile P)\n"); return 2; } strata::prefill::Prefill sp; void* borrow = nullptr; uint64_t borrow_bytes = 0; int32_t lend_first = -1; // the first slot the prompt path may borrow (its largest chunk) int32_t lend_first_now = -1; // where its buffers are laid out now // a cache too small to lend the prompt path its buffers would make it allocate them on top - on a card the // cache already filled to its reserve, that is the over-subscription the auto sizing avoids - so the // prompt chunk is halved until its buffers fit in the lendable slots (a smaller chunk only reads slower) if (!o.no_prefill_borrow && d_res != nullptr) { int64_t chunk = o.prefill_chunk; if (const int64_t k = plan_lend(chunk); k > 0) { if (o.prefill_auto) std::fprintf(stderr, "strata serve: prompt chunk auto: %lld tokens\n", (long long) chunk); else if (chunk != o.prefill_chunk) std::fprintf(stderr, "strata serve: prompt chunk %lld -> %lld tokens so its buffers fit in the " "expert cache\n", (long long) o.prefill_chunk, (long long) chunk); o.prefill_chunk = chunk; lend_first = (int32_t) (xcache.slots() - k); lend_first_now = lend_first; borrow = xcache.device_slot(lend_first); borrow_bytes = xcache.slot_offsets() ? (uint64_t) (xcache.bytes() - (int64_t) xcache.slot_offsets()[lend_first]) : (uint64_t) k * (uint64_t) strata::kernels::cpu::expert_layout().max_blob; } else if (o.prefill_auto) { o.prefill_chunk = 1024; // nothing lendable: small buffers of its own } } if (borrow != nullptr) std::fprintf(stderr, "strata serve: the prompt path borrows %lld cache slots (%.2f GiB)\n", (long long) (xcache.slots() - lend_first), (double) borrow_bytes / 1073741824.0); else std::fprintf(stderr, "strata serve: the prompt path allocates its own buffers (too few cache slots to borrow)\n"); if (!sp.init(wt, g, ss, srcp, &xcache, host_res.data(), o.prefill_chunk, main_cs, err, borrow, borrow_bytes)) { std::fprintf(stderr, "strata serve: %s\n", err.c_str()); return 1; } mem_mark("the head and the prompt path"); // the penalty-history buffer: ONE row, the request's last `penalty_last_n` tokens, -1 padded in // front. Allocated once at the cap; a request without penalties gets a null row and takes the // byte-for-byte neutral path (no upload, no buffer handed to the sampler). constexpr int kPenaltyWindowCap = 4096; int32_t* d_hist = nullptr; std::vector hist_stage((size_t) kPenaltyWindowCap, -1); if (cudaMalloc(&d_hist, (size_t) kPenaltyWindowCap * sizeof(int32_t)) != cudaSuccess) { std::fprintf(stderr, "strata serve: the penalty-history allocation failed\n"); return 1; } strata::core::Verifier ver; strata::core::VerifyHits vh; vh.d_res = thits.d_res; vh.cache_base = thits.cache_base; vh.blob = thits.blob; if (!ver.init(wt, g, ss, vh, native_head.loaded() ? &native_head : nullptr, o.spec, err) || !mtp.bind(wt, &native_head, ver.final_R_all(), err)) { std::fprintf(stderr, "strata serve: %s\n", err.c_str()); return 1; } mem_mark("the verifier and the drafter's binding"); ver.set_split(o.spec_split); // auto: the copy kernel for every pack. DMA (the native packs' default until 0.1.13) has the host call // cudaMemcpyAsync + cudaLaunchHostFunc inside a verify window while the GPU spins on the flag they raise; // issue #31's thread dumps show the host stuck in that cudaMemcpyAsync on a driver lock for good. The copy // kernel needs no host CUDA call there, and costs ~1-3% decode on IQ3_S (45.3 -> 44.8 tok/s, 8 requests). ver.set_pcie_mode(o.pcie_mode == "dma" ? 0 : o.pcie_mode == "direct" ? 1 : 2); std::vector cur; // ---- the conversation cache (see ConvCheckpoint). `live` is what the session holds right now: the tokens // it has consumed, so a request that starts with exactly them continues without any copy. `checks` are the // saved points; every one of them is a prefix of `live` (the loop drops the rest), so they form a chain - // the radix cache's tree collapsed onto the one branch of history whose cells the session holds. The // chain's root is the deepest point every request so far shared (the end of the system prompt, in // practice); the retention policy pins it and rotates the rest LRU (conv_cache.hpp), so a NEW chat that // shares that prefix mounts through it instead of reading it again. std::vector live; std::vector live_imgs, req_imgs; bool live_ok = false; std::vector checks; uint64_t check_clock = 0; // the checkpoints' LRU clock; creation and every use advance it bool cvec_cached = true; // the control vector's state the live session and the checkpoints were read with int64_t pp_total = 0, pp_from = 0, pp_next_check = 0; Clock::time_point pp_t0 = Clock::now(); auto imgs_below = [&](const std::vector& all, int64_t L) { std::vector v; for (const ImgKey& k : all) if (k.start < L) v.push_back(k); return v; }; // a checkpoint of the state after `cur[0, L)`; false only when the copy itself failed auto checkpoint_at = [&](int64_t L) -> bool { if (o.prompt_cache <= 0 || L < 1) return true; for (ConvCheckpoint& c : checks) if ((int64_t) c.ids.size() == L) { c.used = ++check_clock; return true; } ConvCheckpoint c; c.ids.assign(cur.begin(), cur.begin() + L); c.imgs = imgs_below(req_imgs, L); if (cudaDeviceSynchronize() != cudaSuccess || !checkpoint_save(c, ss, g)) return false; c.used = ++check_clock; checks.push_back(std::move(c)); while ((int) checks.size() > o.prompt_cache) { std::vector stamps; stamps.reserve(checks.size()); for (const ConvCheckpoint& k : checks) stamps.push_back(k.used); const size_t victim = strata::program::conv_cache::eviction_victim(stamps.data(), stamps.size(), o.prompt_cache); checks.erase(checks.begin() + (std::ptrdiff_t) victim); } return true; }; sp.on_chunk = [&](const float* R_rows, int64_t T, int64_t p0, std::string& e) -> bool { std::vector nxt((size_t) T); for (int64_t t = 0; t < T; ++t) nxt[(size_t) t] = (int32_t) cur[(size_t) (p0 + t + 1)]; if (!mtp.prefill(R_rows, nxt.data(), T, p0, e)) return false; // progress for the server window: PP const int64_t done = p0 + T; const double ms = std::chrono::duration(Clock::now() - pp_t0).count(); std::printf("PP %lld %lld %.0f %.1f\n", (long long) done, (long long) pp_total, ms, ms > 0.0 ? 1000.0 * (double) (done - pp_from) / ms : 0.0); strata::core::progress_at("reading the prompt (batched), done up to token", done); strata::core::progress_beat(); std::fflush(stdout); if (o.prompt_cache_every > 0 && done >= pp_next_check) { if (!checkpoint_at(done)) { e = "saving a conversation checkpoint failed"; return false; } pp_next_check = done + o.prompt_cache_every; } return true; }; drive.d.plan = ver.plan_sink(); drive.d.pcie_num = std::max(0, std::min(256, (int) (o.pcie_frac * 256.0 + 0.5))); if (o.adapt_every > 0 && o.adapt_swaps > 0) drive.d.usage.assign((size_t) (g.n_layers * g.n_expert), 0.0f); cudaStream_t adapt_stream = nullptr; if (cudaStreamCreateWithFlags(&adapt_stream, cudaStreamNonBlocking) != cudaSuccess) { std::fprintf(stderr, "strata serve: cannot create the refill stream\n"); return 1; } // plan v0.3 P6: swaps in flight - (residency index, slot) admitted when adapt_ev has completed std::vector> pending; cudaEvent_t adapt_ev = nullptr; cudaEventCreateWithFlags(&adapt_ev, cudaEventDisableTiming); auto apply_pending = [&](bool wait) { if (pending.empty()) return; if (wait) cudaEventSynchronize(adapt_ev); else if (cudaEventQuery(adapt_ev) != cudaSuccess) return; for (const auto& [i, slot] : pending) host_res[(size_t) i] = slot; pending.clear(); if (d_res != nullptr) cudaMemcpy(d_res, host_res.data(), host_res.size() * sizeof(int32_t), cudaMemcpyHostToDevice); }; // the VRAM tier follows the conversation (the same rule as the speculative loop below) auto adapt = [&]() -> bool { if (!pending.empty()) return true; // the previous swaps are still in flight struct Swap { float gain; int32_t layer, in, out; }; std::vector swaps; std::vector> cand, vict; for (int64_t l = 0; l < g.n_layers; ++l) { cand.clear(); vict.clear(); const float* u = drive.d.usage.data() + l * g.n_expert; const int32_t* r = host_res.data() + l * g.n_expert; for (int32_t e = 0; e < (int32_t) g.n_expert; ++e) { if (r[e] < 0) { if (u[e] >= 2.0f) cand.emplace_back(u[e], e); } else vict.emplace_back(u[e], e); } if (cand.empty() || vict.empty()) continue; std::sort(cand.begin(), cand.end(), [](auto& a, auto& b) { return a.first > b.first; }); const size_t nc = std::min(cand.size(), vict.size()); std::partial_sort(vict.begin(), vict.begin() + (ptrdiff_t) nc, vict.end(), [](auto& a, auto& b) { return a.first < b.first; }); for (size_t i = 0; i < nc; ++i) { if (cand[i].first < vict[i].first + 1.5f) break; swaps.push_back({cand[i].first - vict[i].first, (int32_t) l, cand[i].second, vict[i].second}); } } std::sort(swaps.begin(), swaps.end(), [](const Swap& a, const Swap& b) { return a.gain > b.gain; }); if ((int) swaps.size() > o.adapt_swaps) swaps.resize((size_t) o.adapt_swaps); for (const Swap& s : swaps) { const size_t in = (size_t) s.layer * g.n_expert + s.in, out = (size_t) s.layer * g.n_expert + s.out; const int32_t slot = host_res[out]; const uint8_t* b = srcp->blob(s.layer, s.in); if (slot < 0 || b == nullptr || cudaMemcpyAsync(xcache.device_slot(slot), b, (size_t) strata::kernels::cpu::expert_layout().blob_bytes(s.layer), cudaMemcpyHostToDevice, adapt_stream) != cudaSuccess) return false; host_res[out] = strata::core::kNotResident; // evicted now: the CPU computes it meanwhile pending.emplace_back((int32_t) in, slot); // resident once the copy has landed } if (!swaps.empty()) cudaEventRecord(adapt_ev, adapt_stream); for (float& v : drive.d.usage) v *= 0.7f; return true; }; // stdin is read on its own thread, so a STOP line reaches a request that is still running (the client went // away, or pressed Esc): the flag is checked between prompt chunks and between verify windows. std::atomic stop_req{false}; std::mutex in_mu; std::condition_variable in_cv; std::deque in_lines; bool in_eof = false; std::thread([&] { // read(2) on the descriptor, not std::cin: glibc's exit() flushes every stdio stream and waits for // stdin's lock, which getline holds while it waits for input - an engine ending on an error (every // std::exit) would hang in exit() on Linux, and the server would wait for it forever std::string l, buf; char chunk[4096]; auto getline_fd = [&](std::string& out) -> bool { for (;;) { const size_t nlpos = buf.find('\n'); if (nlpos != std::string::npos) { out.assign(buf, 0, nlpos); buf.erase(0, nlpos + 1); return true; } #if defined(_WIN32) const int n = _read(0, chunk, (unsigned) sizeof chunk); #else const ssize_t n = ::read(0, chunk, sizeof chunk); if (n < 0 && errno == EINTR) continue; #endif if (n <= 0) { if (buf.empty()) return false; out.swap(buf); buf.clear(); return true; } buf.append(chunk, (size_t) n); } }; while (getline_fd(l)) { if (!l.empty() && l.back() == '\r') l.pop_back(); if (l == "STOP") { stop_req.store(true); continue; } std::lock_guard lk(in_mu); in_lines.push_back(l); in_cv.notify_one(); } std::lock_guard lk(in_mu); in_eof = true; in_cv.notify_one(); }).detach(); auto next_line = [&](std::string& out) -> bool { std::unique_lock lk(in_mu); in_cv.wait(lk, [&] { return !in_lines.empty() || in_eof; }); if (in_lines.empty()) return false; out = std::move(in_lines.front()); in_lines.pop_front(); return true; }; sp.should_stop = [&] { return stop_req.load(); }; // STRATA_TRACE=1: one stderr line per step of a request (the log shows where a request stops) const bool trace = std::getenv("STRATA_TRACE") != nullptr; auto tr = [&](const char* what, long long a = -1, long long b = -1) { if (!trace) return; std::fprintf(stderr, "strata trace: %s %lld %lld\n", what, a, b); std::fflush(stderr); }; { // what is left once everything is allocated: under WDDM a GPU filled to the brim does not fail, it pages - // and a page-in while the verify graph spins on a host flag stalls the request for good size_t free_b = 0, total_b = 0; cudaMemGetInfo(&free_b, &total_b); // below ~256 MiB a later allocation (a first-used window's buffers, the desktop, another program) can make // the driver page GPU memory, and a verify graph spinning on a host flag then never finishes const int64_t free_mib = (int64_t) (free_b >> 20); if (free_mib >= 256) { std::fprintf(stderr, "strata serve: %lld MiB of VRAM free with everything loaded\n", (long long) free_mib); } else { std::fprintf(stderr, "strata serve: %lld MiB of VRAM free with everything loaded - LOW: requests may stall;" " add --vram-reserve-mib %lld to the config's args (or lower --max-context)\n", (long long) free_mib, (long long) (o.vram_reserve_mib + 512 - free_mib)); } } // what the server's Monitor tab shows (servers before 0.1.8 skip unknown lines until READY) { size_t free_b = 0, total_b = 0; cudaMemGetInfo(&free_b, &total_b); std::printf("INFO context=%lld kv=%s kv_resident=%lld expert_slots=%lld expert_cache_mib=%lld spec=%d " "mtp_max=%d lookup=%d vram_free_mib=%lld cvec=%s arena_mib=%lld engine=" STRATA_VERSION "\n", (long long) o.max_context, o.kv.c_str(), (long long) (g.n_qsa_layers() > 0 && ss.qsa_states[0].kv_mode == 1 ? ss.qsa_states[0].n_slots * 4 : 0), (long long) xcache.slots(), (long long) (xcache.bytes() >> 20), o.spec, o.mtp_max_t, o.suffix_draft, (long long) (free_b >> 20), cvec_summary.c_str(), (long long) (strata::kernels::cpu::expert_layout().total >> 20)); } // issue #29: a request whose heartbeat (tokens, prompt chunks, verify windows) stops for this long is stuck on // a flag nobody will raise - end the engine with where it was, so the server starts it again instead of the // GPU spinning forever. STRATA_WATCHDOG_S=0 turns it off. Issue #31: before it does, it reports what every // part was doing (stall_report), so one occurrence says where the wait is. { const char* ws = std::getenv("STRATA_WATCHDOG_S"); const int limit = ws ? std::atoi(ws) : 60; // one step (a prompt layer, a verify window) takes seconds if (limit > 0) std::thread([limit] { strata::core::Progress& p = strata::core::progress(); uint64_t last = p.beats.load(), ticks_at = p.ticks.load(); auto since = std::chrono::steady_clock::now(); for (;;) { std::this_thread::sleep_for(std::chrono::seconds(1)); const auto now = std::chrono::steady_clock::now(); const uint64_t b = p.beats.load(); if (!p.busy.load() || b != last) { last = b; ticks_at = p.ticks.load(); since = now; continue; } if (now - since < std::chrono::seconds(limit)) continue; std::fprintf(stderr, "strata serve: no progress for %d s during a request (%s %lld) - stopping " "the engine so the server starts it again (issue #29)\n", limit, p.where.load(), (long long) p.detail.load()); stall_report(stderr, p.ticks.load() - ticks_at); std::fflush(stderr); std::abort(); } }).detach(); } std::printf("READY %lld stop\n", (long long) o.max_context); // "stop": this engine honours STOP std::fflush(stdout); std::string line; int64_t rounds = 0; const int S = o.spec; const int S_mtp = o.mtp_max_t > 0 ? std::min(o.mtp_max_t, S) : S; // the MTP's windows; suffixes go up to S if (S_mtp < S) mtp.set_max_drafts(S_mtp - 1); strata::spec::SuffixDrafter sfx(std::max(1, o.suffix_draft), 64, (size_t) o.max_context + 4096); strata::spec::DraftPolicy policy(S); // MTP or lookup window, learned over the whole process // The vision path (--vision): GENI carries images. The file is one // or more strata-vision records (int32 'SVE1', n, nx, ny, n_embd, then n x n_embd floats) in prompt order; // each image's rows go to its run of <|image_pad|> tokens, whose M-RoPE positions are mtmd's: t = p, // h = p + y, w = p + x, and the text after the image continues at p + max(nx, ny). constexpr int64_t kImagePad = 248056; // qwen4exp.ple.image_token_id: the PLE hash reads it for image cells bool mrope_identity = true; std::vector img_rows; std::vector row_ptr; while (next_line(line)) { if (line == "QUIT") break; // the watchdog watches a request from here until this iteration ends, whichever way it ends struct BusyScope { BusyScope() { strata::core::progress().busy.store(true); strata::core::progress_at("request"); } ~BusyScope() { strata::core::progress().busy.store(false); strata::core::progress_at("idle"); } } busy_scope; stop_req.store(false); // a STOP that arrived between requests is stale const bool geni = line.rfind("GENI ", 0) == 0; if (!geni && line.rfind("GEN ", 0) != 0) { std::printf("ERR expected: GEN or GENI \n"); continue; } char* endp = nullptr; const long long max_new = std::strtoll(line.c_str() + (geni ? 5 : 4), &endp, 10); // optional sampling keys between max_new and the ids: temperature=F, top_p=F, top_k=N, min_p=F, // penalty_last_n=N, penalty_repeat=F, penalty_freq=F, penalty_present=F, seed=N (text requests // only). Absent keys keep today's behavior: greedy, no penalties. float req_temperature = 0.0f, req_top_p = 1.0f; int req_top_k = 20; // the sampler's own default; the sampled path REQUIRES top_k in 1..64 unsigned long long req_seed = 0; float req_min_p = 0.0f, req_penalty_repeat = 1.0f, req_penalty_freq = 0.0f, req_penalty_present = 0.0f; int req_penalty_last_n = 0; int req_cvec = 1; // cvec=0|1: a loaded control vector for this request (on when absent) if (endp != nullptr) { // GENI takes only cvec=; its file path is the first token without an = for (;;) { while (*endp == ' ') ++endp; const char* start = endp; while (*endp != '\0' && *endp != ' ') ++endp; if (endp == start) break; const std::string tok(start, (size_t) (endp - start)); const size_t eq = tok.find('='); if (eq == std::string::npos) { endp = const_cast(start); break; } const std::string key = tok.substr(0, eq); const float fv = std::strtof(tok.c_str() + eq + 1, nullptr); if (key == "cvec") req_cvec = std::atoi(tok.c_str() + eq + 1); else if (geni) {} // image requests decode greedily else if (key == "temperature") req_temperature = fv; else if (key == "top_p") req_top_p = fv; else if (key == "top_k") req_top_k = std::atoi(tok.c_str() + eq + 1); else if (key == "min_p") req_min_p = fv; else if (key == "penalty_last_n") req_penalty_last_n = std::atoi(tok.c_str() + eq + 1); else if (key == "penalty_repeat") req_penalty_repeat = fv; else if (key == "penalty_freq") req_penalty_freq = fv; else if (key == "penalty_present") req_penalty_present = fv; else if (key == "seed") req_seed = std::strtoull(tok.c_str() + eq + 1, nullptr, 10); // unknown keys are skipped: the ids start at the first token without '=' } } std::string emb_path; if (geni && endp != nullptr) { while (*endp == ' ') ++endp; char* gap = std::strchr(endp, ' '); if (gap != nullptr) { emb_path.assign(endp, (size_t) (gap - endp)); endp = gap; } } std::vector ids; std::string pe; if (max_new < 1 || endp == nullptr || (geni && emb_path.empty()) || !parse_i64_list(endp, ids, pe)) { std::printf("ERR bad request: %s\n", pe.empty() ? "max_new" : pe.c_str()); continue; } const int64_t n = (int64_t) ids.size(); req_imgs.clear(); if (geni && !o.vision) { std::printf("ERR this engine was started without --vision\n"); continue; } if (geni || !mrope_identity) { // positions for every cell this request can reach; the identity again for a text request std::string ve; row_ptr.assign((size_t) n, nullptr); const int64_t cells = (int64_t) mrope_host.size() / 3; auto put = [&](int64_t c, int64_t t, int64_t h, int64_t w) { mrope_host[(size_t) c * 3] = (int32_t) t; mrope_host[(size_t) c * 3 + 1] = (int32_t) h; mrope_host[(size_t) c * 3 + 2] = (int32_t) w; }; if (!geni) { for (int64_t c = 0; c < cells; ++c) put(c, c, c, c); } else { struct Img { int64_t n, nx, ny; size_t off; }; std::vector imgs; img_rows.clear(); std::FILE* f = std::fopen(emb_path.c_str(), "rb"); if (!f) ve = "cannot open " + emb_path; while (f && ve.empty()) { int32_t hdr[5]; const size_t got = std::fread(hdr, sizeof(int32_t), 5, f); if (got == 0) break; if (got != 5 || hdr[0] != 0x31455653 || hdr[1] < 1 || hdr[2] < 1 || hdr[3] < 1 || (int64_t) hdr[2] * hdr[3] != hdr[1] || hdr[4] != (int32_t) g.n_embd) { ve = "bad embeddings file (expected strata-vision records of width " + std::to_string((long long) g.n_embd) + ")"; break; } const size_t off = img_rows.size(), cnt = (size_t) hdr[1] * (size_t) hdr[4]; img_rows.resize(off + cnt); if (std::fread(img_rows.data() + off, sizeof(float), cnt, f) != cnt) { ve = "short embeddings file"; break; } imgs.push_back({hdr[1], hdr[2], hdr[3], off}); } if (f) std::fclose(f); int64_t p = 0, i = 0; size_t k = 0; while (ve.empty() && i < n) { if (ids[(size_t) i] != kImagePad) { put(i, p, p, p); ++p; ++i; continue; } if (k >= imgs.size()) { ve = "the prompt has more images than the embeddings file"; break; } const Img& im = imgs[k++]; { // what the conversation cache compares: a picture is its grid and its embeddings const int64_t grid[3] = {im.n, im.nx, im.ny}; uint64_t h = fnv1a(grid, sizeof grid); h = fnv1a(img_rows.data() + im.off, (size_t) im.n * (size_t) g.n_embd * sizeof(float), h); req_imgs.push_back({i, h}); } for (int64_t j = 0; j < im.n && ve.empty(); ++j) if (i + j >= n || ids[(size_t) (i + j)] != kImagePad) ve = "image " + std::to_string(k) + " has " + std::to_string((long long) im.n) + " rows but fewer <|image_pad|> tokens"; for (int64_t j = 0; j < im.n && ve.empty(); ++j) { const int64_t y = j / im.nx, x = j % im.nx; put(i + j, p, p + y, p + x); row_ptr[(size_t) (i + j)] = img_rows.data() + im.off + (size_t) j * (size_t) g.n_embd; } i += im.n; p += std::max(im.nx, im.ny); } if (ve.empty() && k != imgs.size()) ve = "the embeddings file has more images than the prompt"; if (ve.empty() && n > 0 && ids[(size_t) (n - 1)] == kImagePad) ve = "the prompt cannot end in an image"; for (int64_t c = n; ve.empty() && c < cells; ++c) put(c, p + (c - n), p + (c - n), p + (c - n)); } tr("positions built", (long long) img_rows.size()); cudaDeviceSynchronize(); tr("device idle"); if (ve.empty() && cudaMemcpy(d_mrope, mrope_host.data(), mrope_host.size() * sizeof(int32_t), cudaMemcpyHostToDevice) != cudaSuccess) ve = "the image position upload failed"; if (!ve.empty()) { // leave the table as the identity so the next text request is untouched for (int64_t c = 0; c < cells; ++c) put(c, c, c, c); cudaMemcpy(d_mrope, mrope_host.data(), mrope_host.size() * sizeof(int32_t), cudaMemcpyHostToDevice); mrope_identity = true; std::printf("ERR %s\n", ve.c_str()); std::fflush(stdout); continue; } mrope_identity = !geni; } sp.embd_rows = geni ? row_ptr.data() : nullptr; if (n + max_new + 8 > o.max_context) { std::printf("ERR prompt (%lld tokens) + max_new (%lld) exceeds the context (%lld)\n", (long long) n, (long long) max_new, (long long) o.max_context); continue; } bool bad = false; for (int64_t t : ids) bad = bad || t < 0 || t >= n_vocab; if (bad) { std::printf("ERR a token id is outside the vocabulary\n"); continue; } cur = ids; const Clock::time_point r0 = Clock::now(); // ---- where this request starts reading: the live session, or a checkpoint, whose tokens AND pictures are // exactly the start of this prompt - at most n - 1 of them, the last token is always the first window auto starts_with = [&](const std::vector& pre, const std::vector& pre_imgs) -> bool { const int64_t L = (int64_t) pre.size(); if (L < 1 || L > n - 1) return false; for (int64_t i = 0; i < L; ++i) if ((int32_t) ids[(size_t) i] != pre[(size_t) i]) return false; return imgs_below(req_imgs, L) == pre_imgs; }; // the control vector for this request. The live session and the checkpoints were read one way, so a // switch reads the prompt again from the start if (strata::kernels::cvec().loaded()) { const bool want = req_cvec != 0; if (want != cvec_cached) { live_ok = false; checks.clear(); cvec_cached = want; } strata::kernels::cvec_set_enabled(want); } int64_t resume = 0; bool from_live = false; if (o.prompt_cache > 0) { if (live_ok && starts_with(live, live_imgs)) { resume = (int64_t) live.size(); from_live = true; } for (const ConvCheckpoint& c : checks) if ((int64_t) c.ids.size() > resume && starts_with(c.ids, c.imgs)) { resume = (int64_t) c.ids.size(); from_live = false; } } // this request rewrites every cell from `resume` on, so a checkpoint past it (or not on this prompt's // path) no longer has its cells; the ones kept are prefixes of both the old tokens and the new checks.erase(std::remove_if(checks.begin(), checks.end(), [&](const ConvCheckpoint& c) { return (int64_t) c.ids.size() > resume || !starts_with(c.ids, c.imgs); }), checks.end()); live_ok = false; // until this request has finished, the session is in between int64_t reread_to = -1; // STRATA_CKPT_REREAD only: read [0, reread_to) again instead of restoring if (resume == 0) { strata::core::session_zero(ss, g, nullptr, main_cs); cudaStreamSynchronize(main_stream); checks.clear(); } else if (!from_live) { ConvCheckpoint* c = nullptr; for (ConvCheckpoint& k : checks) if ((int64_t) k.ids.size() == resume) c = &k; if (c != nullptr) c->used = ++check_clock; // mounting through it is the use LRU counts static const bool reread = std::getenv("STRATA_CKPT_REREAD") != nullptr; if (reread && c != nullptr) { // THE CHECK OF THE CHECKPOINT: instead of restoring it, read its tokens again from position 0 in // one run (below, with the prompt path's slots lent like any read) - the same chunks the request // that saved it read them in, when that request started at 0. With the VRAM expert set fixed // (--adapt-swaps 0) the answer must match the restored one token for token; anything the // checkpoint missed shows up as a difference. strata::core::session_zero(ss, g, nullptr, main_cs); cudaStreamSynchronize(main_stream); reread_to = resume; std::fprintf(stderr, "strata serve: STRATA_CKPT_REREAD: reading %lld tokens again instead of " "restoring\n", (long long) resume); } else if (c == nullptr || !checkpoint_restore(*c, ss, g)) { std::printf("ERR restoring a conversation checkpoint failed\n"); return 1; } } // KV streaming: the drafter's ring may hold cells past `resume` from a longer turn; the main layers' // host copies and slots are always current (every writer writes both), so they need nothing if (resume > 0 && reread_to <= 0) mtp.kv_restore(resume); tr("request", n, geni ? 1 : 0); mtp.set_prompt_len(n); const int64_t read_from = reread_to > 0 ? 0 : resume; pp_total = n; pp_from = read_from; pp_t0 = r0; pp_next_check = reread_to > 0 ? INT64_MAX : resume + o.prompt_cache_every; std::printf("RESUME %lld\n", (long long) resume); // before reading: this many prompt tokens are reused strata::core::progress_at("reading the prompt, from token", read_from); std::fflush(stdout); // A SHORT PART OF THE PROMPT - the new message of a chat that continues from a checkpoint, the assistant // header - goes through the verify windows, S tokens at a time, as decode reads them. The batched path // costs ~300 ms per run however few tokens it has (it streams every expert the chunk routes to that is // not in VRAM over PCIe), and it borrows slots it must refill after (~180 ms); a window costs ~16 ms a // token, with the misses on the CPU. Each part below is decided on its own, so a long first message is // read batched and its header still goes through the windows. Picture rows need the batched path. // STRATA_CKPT_REREAD compares a restored checkpoint with a batched re-read, so it keeps every read batched. static const bool no_short = std::getenv("STRATA_CKPT_REREAD") != nullptr; auto windows_ok = [&](int64_t a, int64_t b) -> bool { if (no_short || b - a > o.short_read) return false; if (sp.embd_rows != nullptr) for (int64_t i = a; i < b; ++i) if (sp.embd_rows[i] != nullptr) return false; return true; }; // tokens [a, b) through the windows: commit all of them, then give the draft layer their residuals auto read_windows = [&](int64_t a, int64_t b, std::string& e) -> bool { std::vector win((size_t) S), outw((size_t) S), nxt((size_t) S); for (int64_t q = a; q < b;) { if (stop_req.load()) { e = "cancelled"; return false; } const int T = (int) std::min(S, b - q); for (int t = 0; t < T; ++t) { win[(size_t) t] = (int32_t) cur[(size_t) (q + t)]; nxt[(size_t) t] = (int32_t) cur[(size_t) (q + t + 1)]; } drive.d.layers = 0; drive.d.experts = 0; drive.d.failed = false; if (!ver.run(T, win.data(), q, &drive_pool_multi, &drive, outw.data(), e) || drive.d.failed) { if (drive.d.failed && drive.d.fail) e = drive.d.fail; return false; } if (!ver.commit(T, e) || !mtp.prefill(ver.final_R_all(), nxt.data(), T, q, e)) return false; q += T; } const double ms = std::chrono::duration(Clock::now() - pp_t0).count(); std::printf("PP %lld %lld %.0f %.1f\n", (long long) b, (long long) pp_total, ms, ms > 0.0 ? 1000.0 * (double) (b - pp_from) / ms : 0.0); strata::core::progress_beat(); std::fflush(stdout); return true; }; // the batched path's slots are lent just before its first run and given back (refilled) before a window // reads - so the windows always see the whole expert cache - or once the prompt is read std::vector> lent_now; int64_t lent_chunk = 0; // the chunk the lent slots hold the prompt path's buffers for auto refill = [&](std::string& e) -> bool { if (lent_now.empty()) return true; tr("refill start", (long long) lent_now.size()); for (const auto& [i, slot] : lent_now) { const uint8_t* b = srcp->blob(i / g.n_expert, i % g.n_expert); if (b == nullptr || !xcache.fill_slot_blocking(slot, b, e, (int64_t) strata::kernels::cpu::expert_layout().blob_bytes(i / g.n_expert))) return false; host_res[(size_t) i] = slot; } cudaMemcpy(d_res, host_res.data(), host_res.size() * sizeof(int32_t), cudaMemcpyHostToDevice); lent_now.clear(); lent_chunk = 0; return true; }; // lend the slots `tokens` batched prompt tokens need: the prompt path's buffers for min(chunk, tokens // rounded up to 256), laid out in the last of the slots it may borrow auto lend = [&](int64_t tokens, std::string& e) -> bool { if (lend_first < 0) return true; // its own buffers: nothing to lend const int64_t want = std::min(o.prefill_chunk, (tokens + 255) / 256 * 256); if (!lent_now.empty()) { if (want <= lent_chunk) return true; if (!refill(e)) return false; } const int32_t first = std::max(lend_first, (int32_t) (xcache.slots() - lend_slots(want))); if (want != sp.chunk() || first != lend_first_now) { if (!sp.relayout(want, xcache.device_slot(first), lend_bytes(first), e)) return false; lend_first_now = first; } for (size_t i = 0; i < host_res.size(); ++i) if (host_res[i] >= first) { lent_now.emplace_back((int32_t) i, host_res[i]); host_res[i] = strata::core::kNotResident; } cudaMemcpy(d_res, host_res.data(), host_res.size() * sizeof(int32_t), cudaMemcpyHostToDevice); lent_chunk = want; return true; }; apply_pending(true); // per-request sampling for the verify window's head (greedy when temperature is absent) strata::kernels::SamplerParams req_sp; req_sp.greedy = req_temperature <= 0.0f; req_sp.temperature = req_temperature; req_sp.top_p = req_top_p; req_sp.top_k = req_top_k; req_sp.seed = req_seed ? req_seed : (unsigned long long) std::chrono::steady_clock::now().time_since_epoch().count(); req_sp.min_p = std::clamp(req_min_p, 0.0f, 1.0f); req_sp.penalty_last_n = std::max(req_penalty_last_n, 0); req_sp.penalty_repeat = req_penalty_repeat; req_sp.penalty_freq = req_penalty_freq; req_sp.penalty_present = req_penalty_present; req_sp.counter = 0; ver.set_sampling(req_sp); const int hist_n = std::min(req_sp.penalty_last_n, kPenaltyWindowCap); ver.set_history(hist_n > 0 ? d_hist : nullptr, hist_n); bool cancelled = false; tr("prompt start", n - 1); // The prompt is read in two parts when it has a turn boundary past `resume`: up to the last <|im_start|> // (the conversation so far), a checkpoint there, then the new turn's header. The next request of the same // chat renders the same history - but not always the same header or the thinking of this reply - so that // checkpoint is the one it reuses. int64_t turn_at = -1; if (o.prompt_cache > 0 && o.turn_token >= 0) for (int64_t i = n - 1; i > resume; --i) if (ids[(size_t) i] == o.turn_token) { turn_at = i; break; } int64_t at = read_from; for (const int64_t to : {reread_to, turn_at, n - 1}) { if (to <= at) continue; const bool win = windows_ok(at, to); if (win && !refill(err)) { std::printf("ERR refilling a lent slot failed: %s\n", err.c_str()); return 1; } if (!win && !lend(to - at, err)) { std::printf("ERR lending the prompt path its slots failed: %s\n", err.c_str()); return 1; } const auto tsp = Clock::now(); const bool sp_ok = win ? read_windows(at, to, err) : sp.run(ids.data() + at, to - at, at, err); if (trace) { std::fprintf(stderr, "strata trace: read %lld tokens (%s) in %.1f ms\n", (long long) (to - at), win ? "windows" : "batched", std::chrono::duration(Clock::now() - tsp).count()); std::fflush(stderr); } if (!sp_ok) { if (!stop_req.load()) { std::fprintf(stderr, "strata serve: %s\n", err.c_str()); std::printf("ERR %s\n", err.c_str()); return 1; } cancelled = true; // stopped while reading the prompt: refill the lent slots below, then DONE cancel break; } at = to; if (to == turn_at && !checkpoint_at(to)) { std::printf("ERR saving a conversation checkpoint failed\n"); return 1; } } if (!refill(err)) { std::printf("ERR refilling a lent slot failed: %s\n", err.c_str()); return 1; } tr("prompt done (slots refilled)"); const double prompt_ms = std::chrono::duration(Clock::now() - r0).count(); std::printf("REUSED %lld\n", (long long) resume); // the prompt is read; the first window comes next std::fflush(stdout); // the verify windows: the first holds the last prompt token alone int64_t p = n - 1; int32_t x = (int32_t) ids[(size_t) (n - 1)]; std::vector drafts((size_t) S, 0), window((size_t) S), outv((size_t) S); std::vector dprob((size_t) S, 0.0f); std::vector sbuf((size_t) S, 0); if (o.suffix_draft > 0) { sfx.reset(); for (int64_t t : ids) sfx.append((int32_t) t); } bool first_window = true; int64_t produced_n = 0, sfx_windows = 0, sfx_drafts = 0, sfx_ok = 0; int64_t draft_offered = 0, draft_accepted = 0; // what the session holds once this request is done: the prompt read so far, then every committed token std::vector consumed; consumed.reserve((size_t) (n + max_new + S)); for (int64_t i = 0; i < n - 1; ++i) consumed.push_back((int32_t) ids[(size_t) i]); const char* finish = "length"; const Clock::time_point d0 = Clock::now(); if (cancelled) finish = "cancel"; while (!cancelled && produced_n < max_new) { int T = S_mtp; if (o.spec_min_p > 0.0) { T = 1; while (T < S_mtp && dprob[(size_t) T - 1] >= (float) o.spec_min_p) ++T; } if (first_window) T = 1; // a repeat of earlier context (prompt lookup) where the MTP's own first guess agrees: the policy takes it // when its expected tokens per ms, from the measured acceptance and window costs, beat the MTP window's bool from_sfx = false; int sfx_match = 0; if (o.suffix_draft > 0 && !first_window) { const int k = sfx.propose(S - 1, sbuf.data()); sfx_match = sfx.last_match(); if (k > 0 && sbuf[0] == drafts[0]) { const strata::spec::DraftPolicy::Pick pk = policy.choose(T, k, sfx_match); if (pk.lookup) { T = pk.t; from_sfx = true; } } } const bool timed_round = !first_window; const Clock::time_point round0 = Clock::now(); if (p + T > o.max_context) break; window[0] = x; for (int i = 1; i < T; ++i) window[(size_t) i] = from_sfx ? sbuf[(size_t) i - 1] : drafts[(size_t) i - 1]; drive.d.layers = 0; drive.d.experts = 0; drive.d.failed = false; apply_pending(false); if (hist_n > 0) { // the tail the penalties count over: the tokens the state has consumed plus the fed-back // head `x` (it joins `consumed` only after this window commits). Most recent LAST, // -1 in the unused front slots - the sampler's row layout. const int64_t avail = (int64_t) consumed.size() + 1; const int take = (int) std::min(hist_n, avail); std::fill(hist_stage.begin(), hist_stage.begin() + hist_n, -1); for (int j = 0; j < take - 1; ++j) hist_stage[(size_t) (hist_n - take + j)] = consumed[(size_t) ((int64_t) consumed.size() - (take - 1) + j)]; hist_stage[(size_t) (hist_n - 1)] = (int32_t) x; cudaMemcpy(d_hist, hist_stage.data(), (size_t) hist_n * sizeof(int32_t), cudaMemcpyHostToDevice); } tr("window", p, T); if (!ver.run(T, window.data(), p, &drive_pool_multi, &drive, outv.data(), err) || drive.d.failed) { std::printf("ERR %s\n", drive.d.failed && drive.d.fail ? drive.d.fail : err.c_str()); return 1; } int a = 0; while (a < T - 1 && window[(size_t) a + 1] == outv[(size_t) a]) ++a; if (from_sfx) { ++sfx_windows; sfx_drafts += T - 1; sfx_ok += a; } std::thread adapt_thr; // the adaptive tier beside the commit and the draft (as in generate) bool adapt_ok = true; if (!drive.d.usage.empty() && ((rounds + 1) % o.adapt_every) == 0) adapt_thr = std::thread([&] { adapt_ok = adapt(); }); if (!ver.commit(a + 1, err)) { if (adapt_thr.joinable()) adapt_thr.join(); std::printf("ERR %s\n", err.c_str()); return 1; } // the window's first a + 1 tokens are in the session now (the last output is not: it is next x) for (int i = 0; i <= a; ++i) consumed.push_back(window[(size_t) i]); draft_offered += T - 1; draft_accepted += a; first_window = false; bool eos = false; for (int i = 0; i <= a && produced_n < max_new && !eos; ++i) { std::printf("T %d\n", (int) outv[(size_t) i]); strata::core::progress_beat(); ++produced_n; if (o.suffix_draft > 0) sfx.append(outv[(size_t) i]); eos = std::find(o.eos_ids.begin(), o.eos_ids.end(), (int64_t) outv[(size_t) i]) != o.eos_ids.end(); } std::fflush(stdout); ++rounds; const bool drafted = eos || produced_n >= max_new || mtp.draft(T, outv.data(), p, a, drafts.data(), err, dprob.data(), (float) o.spec_min_p); if (adapt_thr.joinable()) adapt_thr.join(); if (!adapt_ok) { std::printf("ERR an adaptive refill failed\n"); return 1; } if (!drafted) { std::printf("ERR %s\n", err.c_str()); return 1; } if (timed_round && !eos) policy.observe(from_sfx, T, a, sfx_match, std::chrono::duration(Clock::now() - round0).count()); if (eos) { finish = "stop"; break; } if (stop_req.load()) { finish = "cancel"; break; } x = outv[(size_t) a]; p += a + 1; } const double decode_ms = std::chrono::duration(Clock::now() - d0).count(); if (!cancelled) { // a prompt stopped halfway leaves the session somewhere between two chunks: nothing to continue from // (the checkpoints taken while reading it are still good) live.swap(consumed); live_imgs = imgs_below(req_imgs, (int64_t) live.size()); live_ok = o.prompt_cache > 0; } static const bool state_hash = std::getenv("STRATA_STATE_HASH") != nullptr; if (state_hash && live_ok) { // DEBUG: a fingerprint of every part of the session over the positions it holds ([0, L)), and // separately of what lies past them in the last KV page (stale cells, fine unless something reads them) cudaDeviceSynchronize(); const int64_t L = (int64_t) live.size(); const strata::kernels::QsaShapes qs = [&] { strata::kernels::QsaShapes s = strata::kernels::qsa_real_shapes(); s.n_head_kv = g.n_head_kv; s.head_dim = g.head_dim; s.idx_dim = g.idx_key_dim; return s; }(); auto hash_dev = [&](const void* p, size_t bytes, uint64_t h) { std::vector b(bytes); if (bytes) cudaMemcpy(b.data(), p, bytes, cudaMemcpyDefault); // VRAM or a streamed host copy return fnv1a(b.data(), b.size(), h); }; // the cells [c0, c1) of one int8 K or V pool ([page][kv_head][page_size][head_dim]), bytes per value `w` auto hash_cells = [&](const void* pool, int64_t per_cell, int64_t c0, int64_t c1, uint64_t h) { const int64_t ps = qs.page_size; for (int64_t pg = c0 / ps; pg * ps < c1; ++pg) for (int64_t hd = 0; hd < qs.n_head_kv; ++hd) { const int64_t a = std::max(c0, pg * ps) - pg * ps, e = std::min(c1, (pg + 1) * ps) - pg * ps; const size_t off = (size_t) (((pg * qs.n_head_kv + hd) * ps + a) * per_cell); h = hash_dev((const uint8_t*) pool + off, (size_t) ((e - a) * per_cell), h); } return h; }; const ConvStateSizes z = conv_state_sizes(g); uint64_t h_gdn = hash_dev(ss.gdn_state, z.gdn, 1469598103934665603ull); if (std::getenv("STRATA_STATE_HASH_GDN") != nullptr) { // per GDN layer: which one differs first const size_t per = z.gdn / (size_t) g.n_gdn_layers(); std::string s; char b[8]; for (int64_t i = 0; i < g.n_gdn_layers(); ++i) { std::snprintf(b, sizeof(b), "%04llx ", (unsigned long long) (hash_dev((const uint8_t*) ss.gdn_state + i * per, per, 1469598103934665603ull) & 0xffff)); s += b; } std::fprintf(stderr, "strata serve: STATE_HASH_GDN %s\n", s.c_str()); } uint64_t h_ple = hash_dev(ss.ple_hist, z.ple, 1469598103934665603ull); uint64_t h_tail = 1469598103934665603ull, h_pool = h_tail, h_kv = h_tail, h_stale = h_tail; const int64_t kvb = qs.head_dim, scb = (qs.head_dim / 64) * 2; // a state's K/V arrays and their bytes per (cell, head) row: the host copy when it has one auto kv_arrays = [&](const strata::core::QsaState& st) { const bool h = st.kv_mode != 0; std::vector> a; if (st.kv_q4) { const int64_t q4b = (int64_t) strata::kernels::kv_q4_bytes_per_head((int) qs.head_dim); a = {{h ? st.host.k_q4 : st.k_q4, q4b}, {h ? st.host.v_q4 : st.v_q4, q4b}}; } else { a = {{h ? st.host.k_q : st.k_q, kvb}, {h ? st.host.v_q : st.v_q, kvb}, {h ? st.host.k_scale : st.k_scale, scb}, {h ? st.host.v_scale : st.v_scale, scb}}; } return a; }; const int64_t end_cell = std::min(((L + qs.page_size - 1) / qs.page_size) * qs.page_size, ss.qsa_states[0].max_cells); for (int64_t i = 0; i < g.n_qsa_layers(); ++i) { const strata::core::QsaState& st = ss.qsa_states[i]; h_tail = hash_dev(st.idx_tail, z.tail, h_tail); h_pool = hash_dev(st.idx_pooled, (size_t) (L / qs.idx_block) * qs.idx_dim * 4, h_pool); // KV streaming: the host copy is the identity layout and holds every cell for (const auto& [pool, w] : kv_arrays(st)) { h_kv = hash_cells(pool, w, 0, L, h_kv); h_stale = hash_cells(pool, w, L, end_cell, h_stale); } } const strata::core::QsaState& ms = mtp.kv_state(); uint64_t h_mtp = 1469598103934665603ull; const int64_t mL = std::min(L, ms.max_cells); for (const auto& [pool, w] : kv_arrays(ms)) if (pool != nullptr) h_mtp = hash_cells(pool, w, 0, mL, h_mtp); std::fprintf(stderr, "strata serve: STATE_HASH L=%lld gdn=%016llx ple=%016llx tail=%016llx pooled=%016llx " "kv=%016llx mtp=%016llx stale=%016llx ple_prev=%d,%d\n", (long long) L, (unsigned long long) h_gdn, (unsigned long long) h_ple, (unsigned long long) h_tail, (unsigned long long) h_pool, (unsigned long long) h_kv, (unsigned long long) h_mtp, (unsigned long long) h_stale, ss.ple_prev[0], ss.ple_prev[1]); } // DONE std::printf("DONE %lld %lld %.1f %.1f %s %lld %lld %lld\n", (long long) produced_n, (long long) n, prompt_ms, decode_ms, finish, (long long) draft_accepted, (long long) draft_offered, (long long) resume); std::fflush(stdout); const int64_t fresh = n - resume; std::fprintf(stderr, "strata serve: prompt %lld tokens = %lld reused + %lld read in %.0f ms (%.1f tok/s), " "%lld generated in %.0f ms (%.1f tok/s), drafts accepted %lld of %lld, %zu checkpoints%s\n", (long long) n, (long long) resume, (long long) fresh, prompt_ms, prompt_ms > 0 ? 1000.0 * fresh / prompt_ms : 0.0, (long long) produced_n, decode_ms, decode_ms > 0 ? 1000.0 * produced_n / decode_ms : 0.0, (long long) draft_accepted, (long long) draft_offered, checks.size(), cancelled ? " (cancelled)" : ""); if (g.n_qsa_layers() > 0 && ss.qsa_states[0].kv_mode == 1) { // KV streaming, cumulative over the process: blocks the selections named vs blocks read from RAM uint64_t miss = 0, look = 0; bool over = false; for (int64_t i = 0; i < g.n_qsa_layers(); ++i) { const strata::kernels::KvStreamCounters c = strata::kernels::kv_stream_counters(ss.qsa_states[i].map); miss += c.misses; look += c.lookups; over = over || c.overflow; } std::fprintf(stderr, "strata serve: KV streaming: %.2f%% of %llu block reads hit VRAM, %.1f MiB read " "from RAM%s\n", look ? 100.0 * (double) (look - miss) / (double) look : 100.0, (unsigned long long) look, (double) miss * 4224.0 / 1048576.0, over ? " - OVERFLOW (too few resident cells)" : ""); } if (sfx_windows > 0) std::fprintf(stderr, "strata serve: suffix drafts: %lld windows, %lld of %lld drafts accepted\n", (long long) sfx_windows, (long long) sfx_ok, (long long) sfx_drafts); } return 0; } // ---- plan v0.3 P5: the prompt's conditioning positions [0, n_prompt - 1) in batched chunks. The token loop // then starts at the last prompt position, whose prediction is the first generated token. int64_t pos_start = 0; int64_t spec_pos = 0; // plan v0.3 P6: where the speculative loop starts (0 = not used) strata::prefill::Prefill prefill; double prefill_batched_ms = 0; std::FILE* final_r = o.dump_final_r.empty() ? nullptr : std::fopen(o.dump_final_r.c_str(), "wb"); std::vector final_r_host(final_r ? (size_t) (g.hc * g.n_embd) : 0); std::vector> lent; // (residency index, slot) lent to the prompt path if (o.prefill_chunk > 0 && n_prompt > 1) { void* borrow = nullptr; uint64_t borrow_bytes = 0; if (!o.no_prefill_borrow && !host_res.empty() && d_res != nullptr) { int64_t chunk = o.prefill_chunk; int64_t k = plan_lend(chunk); // auto: the largest chunk that fits; fixed: halved to fit if (k > 0 && chunk > (n_prompt - 1 + 255) / 256 * 256) { // no bigger than the prompt needs chunk = std::max(256, (n_prompt - 1 + 255) / 256 * 256); k = lend_slots(chunk); if (!o.prefill_auto) o.prefill_chunk = chunk; } if (o.prefill_auto) { o.prefill_chunk = k > 0 ? chunk : 1024; std::fprintf(stderr, "strata generate: prompt chunk auto: %lld tokens\n", (long long) o.prefill_chunk); } else if (chunk != o.prefill_chunk) { k = 0; // a fixed chunk that does not fit: its own buffers, as before } const int64_t blob = (int64_t) strata::kernels::cpu::expert_layout().max_blob; if (k > 0) { // the lent slots are refilled after the prompt const int32_t first = (int32_t) (xcache.slots() - k); for (size_t i = 0; i < host_res.size(); ++i) if (host_res[i] >= first) { lent.emplace_back((int32_t) i, host_res[i]); host_res[i] = strata::core::kNotResident; } cudaMemcpy(d_res, host_res.data(), host_res.size() * sizeof(int32_t), cudaMemcpyHostToDevice); borrow = xcache.device_slot(first); borrow_bytes = xcache.slot_offsets() ? (uint64_t) (xcache.bytes() - (int64_t) xcache.slot_offsets()[first]) : (uint64_t) k * (uint64_t) blob; std::fprintf(stderr, "strata generate: prompt path borrows %lld cache slots (%.2f GiB)\n", (long long) k, (double) borrow_bytes / 1073741824.0); } } if (borrow == nullptr) std::fprintf(stderr, "strata generate: prompt path allocates its own buffers (no cache slots to borrow)\n"); if (!prefill.init(wt, g, ss, srcp, o.expert_cache > 0 ? &xcache : nullptr, host_res.empty() ? nullptr : host_res.data(), o.prefill_chunk, main_cs, err, borrow, borrow_bytes)) { std::fprintf(stderr, "strata generate: %s\n", err.c_str()); return 1; } if (!o.mtp.empty()) { if (!mtp.bind(wt, &native_head, nullptr, err)) { std::fprintf(stderr, "strata generate: %s\n", err.c_str()); return 1; } prefill.on_chunk = [&](const float* R_rows, int64_t T, int64_t p0, std::string& e) -> bool { // cell i pairs R_i with the token at i + 1 (every such token is in the prompt) std::vector nxt((size_t) T); for (int64_t t = 0; t < T; ++t) nxt[(size_t) t] = (int32_t) o.tokens[(size_t) (p0 + t + 1)]; return mtp.prefill(R_rows, nxt.data(), T, p0, e); }; } const Clock::time_point tp0 = Clock::now(); const int64_t n_batched = (o.prefill_until > 0 && o.prefill_until < n_prompt - 1) ? o.prefill_until : n_prompt - 1; if (!prefill.run(o.tokens.data(), n_batched, 0, err)) { std::fprintf(stderr, "strata generate: %s\n", err.c_str()); return 1; } // refill the lent slots from the arena and give them back to the decode tier if (!lent.empty()) { const Clock::time_point tr = Clock::now(); for (const auto& [i, slot] : lent) { const uint8_t* b = srcp->blob(i / g.n_expert, i % g.n_expert); if (b == nullptr || !xcache.fill_slot_blocking(slot, b, err, (int64_t) strata::kernels::cpu::expert_layout().blob_bytes(i / g.n_expert))) { std::fprintf(stderr, "strata generate: refilling a lent slot failed: %s\n", err.c_str()); return 1; } host_res[(size_t) i] = slot; } cudaMemcpy(d_res, host_res.data(), host_res.size() * sizeof(int32_t), cudaMemcpyHostToDevice); std::fprintf(stderr, "strata generate: %zu lent slots refilled in %.1f ms\n", lent.size(), std::chrono::duration(Clock::now() - tr).count()); } prefill_batched_ms = std::chrono::duration(Clock::now() - tp0).count(); prefill_ms += prefill_batched_ms; pos_start = n_batched; tok = o.tokens[(size_t) pos_start]; // the PLE window of the token path: the two tokens before `pos_start` ss.ple_prev[0] = pos_start >= 2 ? (int32_t) o.tokens[(size_t) (pos_start - 2)] : -1; ss.ple_prev[1] = pos_start >= 1 ? (int32_t) o.tokens[(size_t) (pos_start - 1)] : -1; const strata::prefill::PrefillStats& ps = prefill.stats(); std::fprintf(stderr, "strata generate: prefill %lld tokens in %lld chunks, %.1f ms (%.1f tok/s); experts " "streamed %lld (%lld by DMA, host %.1f ms), resident %lld; PLE %.1f ms\n", (long long) ps.tokens, (long long) ps.chunks, ps.ms_total, ps.ms_total > 0 ? 1000.0 * (double) ps.tokens / ps.ms_total : 0.0, (long long) ps.experts_streamed, (long long) ps.experts_dma, ps.ms_experts_host, (long long) ps.experts_resident, ps.ms_ple); } for (int64_t pos = pos_start;; ++pos) { // plan v0.3 P6: a native pack's last prompt token is the first verify window (T = 1) if (native_pack) { spec_pos = pos; break; } if (pos >= o.max_context) { std::fprintf(stderr, "strata generate: ran out of context at position %lld\n", (long long) pos); return 2; } // **THE TOKEN TIMER STARTS HERE, BEFORE ANY OF THE TOKEN'S WORK (A7).** It used to start after // `put_input`/`embed_row`, which excluded the embedding and the PLE window advance from the reported // rate, and it stopped before the NaN scan, the logits dump and the sampler. The published tok/s // figure is a WALL-CLOCK rate: everything one token costs, PLE advance to sampled id. A rate that // excludes real per-token work is not a rate anyone can plan against. const Clock::time_point t0 = Clock::now(); // **THE PLE'S TOKEN WINDOW ADVANCES HERE, ONCE PER TOKEN, AND `ple_stage_token` RUNS OUTSIDE THE // CAPTURE.** Both are the driver's job: `ngram_rows` is a host hash over the last three tokens and the // table gather is a host read, so either one inside a captured graph would run once at capture time and // replay forever. `ple_prev` is OLDEST FIRST and `-1` means "no predecessor", which `ngram_rows` // treats as the EOS cut - a sequence boundary. ss.ple_token = (int32_t) tok; Clock::time_point tp = Clock::now(); // Plan v0.3 P2: the 16 SSD reads start here and complete while the embedding is staged; `ms_ple` is // the issue plus the time still spent WAITING afterwards, i.e. the part the embedding did not hide. if (ss.ple.ready() && !strata::core::ple_issue_token(ss.ple, err)) { std::fprintf(stderr, "strata generate: %s\n", err.c_str()); return 1; } { const Clock::time_point n = Clock::now(); ms_ple += std::chrono::duration(n - tp).count(); tp = n; } if (!put_input(tok, pos)) return 1; { const Clock::time_point n = Clock::now(); ms_embed += std::chrono::duration(n - tp).count(); tp = n; } if (ss.ple.ready() && !strata::core::ple_finish_token(ss.ple, token_stream, err)) { std::fprintf(stderr, "strata generate: %s\n", err.c_str()); return 1; } { const Clock::time_point n = Clock::now(); ms_ple += std::chrono::duration(n - tp).count(); tp = n; } if (pos % 256 == 0 || pos + 1 >= n_prompt - 1) std::fprintf(stderr, "strata generate: position %lld, token %lld%s\n", (long long) pos, (long long) tok, pos < n_prompt ? " (prompt)" : ""); // **`d.layers` IS THE BLOB'S LAYER AXIS, NOT A COUNTER.** The adapter uses it to index // `experts.bin` as `layer * n_expert + expert`, so it MUST restart at 0 for every token. Leaving it // running across tokens asks for layer 48 of a 48-layer file on the second token - which // `FileExpertSource` REFUSES rather than wrapping into layer 0's experts, and that refusal is the only // reason this was a clean error instead of a silently wrong second token. drive.d.layers = 0; drive.d.experts = 0; drive.d.failed = false; err.clear(); if (o.no_capture) { if (!strata::core::session_token(wt, g, pos, /*pos_base=*/0, ss, d_parts, main_cs, o.sync_every_layer, err)) { std::fprintf(stderr, "strata generate: session_token: %s\n", err.c_str()); return 1; } } else { strata::core::doorbell_reset(db); if (tgraph.captured) { if (!strata::core::session_run_token(g, pos, /*pos_base=*/0, ss, tgraph, pool_fn, pool_user, loop_scratch.y_miss, main_cs, err)) { std::fprintf(stderr, "strata generate: %s\n", err.c_str()); return 1; } } else if (!strata::core::session_loop(g, pos, /*pos_base=*/0, ss, gr, pool_fn, hit_fn, pool_user, /*overlap=*/true, main_cs, err, layer_stage, &loop_scratch)) { std::fprintf(stderr, "strata generate: session_loop: %s\n", err.c_str()); return 1; } } if (final_r != nullptr) { cudaMemcpy(final_r_host.data(), ss.R, final_r_host.size() * sizeof(float), cudaMemcpyDeviceToHost); const int64_t posrec[2] = {pos, tok}; std::fwrite(posrec, sizeof posrec, 1, final_r); std::fwrite(final_r_host.data(), sizeof(float), final_r_host.size(), final_r); } if (drive.d.failed) { std::fprintf(stderr, "strata generate: the expert pool failed at layer %lld expert %lld: %s\n", (long long) drive.d.fail_layer, (long long) drive.d.fail_expert, drive.d.fail ? drive.d.fail : "(no message)"); return 1; } { // **THE LAYER LOOP ITSELF, WHICH IS WHAT `--gpu-only-full` HAS TO BE COMPARED AGAINST.** const Clock::time_point n = Clock::now(); ms_layers += std::chrono::duration(n - tp).count(); tp = n; } // ---- the ladder for THIS position, in the order `session_loop` filled it: layer 0 first. if (layer_dump != nullptr) std::fwrite(layer_stage, sizeof(float), layer_floats, layer_dump); if (half_dump != nullptr) { std::fwrite(half_stage, sizeof(float), (size_t) g.n_layers * (size_t) half_stride, half_dump); } if (!run_head(token_stream)) { std::fprintf(stderr, "strata generate: lm_head: %s\n", err.c_str()); return 1; } // **A CHECKPOINT AFTER THE HEAD, BECAUSE AN ASYNC FAULT IS STICKY AND LIES ABOUT WHERE IT HAPPENED.** // Measured, and it cost an hour: without this, `embed_row`'s D2H on the NEXT token reported "an illegal // memory access" at a plane offset that has nothing to do with the fault, and the layer that actually // faulted had completed its own error checks successfully - because its kernels had not run yet. A // sticky error surfaces at the next SYNCHRONISING call, which is whatever happens to come next. if (!o.stream_token && cudaDeviceSynchronize() != cudaSuccess) { std::fprintf(stderr, "strata generate: the device faulted in lm_head at position %lld: %s\n", (long long) pos, cudaGetErrorString(cudaGetLastError())); return 1; } { const Clock::time_point n = Clock::now(); ms_head += std::chrono::duration(n - tp).count(); tp = n; } const bool emit_logits = dump != nullptr && strata::program::logits_selection::selected(pos, dump_positions, o.logits_stride); const bool read_logits = !o.stream_token || o.check_logits || emit_logits; if (read_logits && (cudaMemcpyAsync(logits.data(), d_logits, (size_t) n_vocab * 4, cudaMemcpyDeviceToHost, (cudaStream_t) token_stream) != cudaSuccess || cudaStreamSynchronize((cudaStream_t) token_stream) != cudaSuccess)) { std::fprintf(stderr, "strata generate: reading the logits back failed\n"); return 1; } int bad = 0; if (read_logits) for (float v : logits) if (!std::isfinite(v)) ++bad; if (bad != 0) { std::fprintf(stderr, "strata generate: %d of %lld logits are not finite at position %lld\n", bad, (long long) n_vocab, (long long) pos); return 1; } if (emit_logits && std::fwrite(logits.data(), sizeof(float), (size_t) n_vocab, dump) != (size_t) n_vocab) { std::fprintf(stderr, "strata generate: cannot write logits at position %lld\n", (long long) pos); std::fclose(dump); return 1; } { // **993 KB OF SYNCHRONOUS D2H AND A 248,320-FLOAT HOST SCAN, EVERY TOKEN.** (The review's notes // say 151,936 floats; the artifact's `output.weight` is 248,320 rows, so the real figure is 1.6x // that - a number nobody had checked because nothing measured this term.) R2.6 asks for the dump // and the scan to be behind flags; round 36 did exactly that and measured it SLOWER, because on // this driver a large blocking readback is also what flushes the pipeline for the sampler that // follows. Timed so the claim can be re-checked rather than remembered. const Clock::time_point n = Clock::now(); ms_readback += std::chrono::duration(n - tp).count(); tp = n; } int next = 0; // Teacher-forced prompt rows consume no generation draws. sp.counter = (uint64_t) produced.size(); strata::kernels::sample_tokens(d_logits, 1, (int) n_vocab, nullptr, 0, sp, d_next, token_stream); if (cudaMemcpyAsync(&next, d_next, sizeof(int), cudaMemcpyDeviceToHost, (cudaStream_t) token_stream) != cudaSuccess || cudaStreamSynchronize((cudaStream_t) token_stream) != cudaSuccess) { std::fprintf(stderr, "strata generate: reading the sampled token back failed: %s\n", cudaGetErrorString(cudaGetLastError())); return 1; } // The sampled-token synchronization also completes every captured QSA // status readback. Retain one status per layer so a later layer cannot // hide an earlier failure; no extra synchronization or token allocation. if (o.native_flash_attn_short) for (int64_t i = 0; i < g.n_qsa_layers(); ++i) { const int32_t status = ss.qsa_states[i].host_step[strata::kernels::kStepCount]; if (status != 0) { std::fprintf(stderr, "strata generate: native attention status %d at QSA layer %lld, position %lld\n", status, (long long) i, (long long) pos); return 1; } } if (next < 0 || next >= n_vocab) { std::fprintf(stderr, "strata generate: the sampler returned %d, outside 0..%lld\n", next, (long long) (n_vocab - 1)); return 1; } { // **TWO DEVICE-WIDE SYNCS FOR FOUR BYTES.** `sample_tokens(nullptr)` ends in // `cudaDeviceSynchronize()` (`sampler.cu:245`) and the blocking 4-byte read below is the second. const Clock::time_point n = Clock::now(); ms_sample += std::chrono::duration(n - tp).count(); ++phase_tokens; } // CHARGED HERE, AFTER THE SAMPLER, so the wall-clock rate covers the whole token including the embedding, // the NaN scan, the logits readback and the sample (A7). Only DECODE positions count; prefill is // measured separately. if (pos >= n_prompt - 1) total_ms += std::chrono::duration(Clock::now() - t0).count(); else prefill_ms += std::chrono::duration(Clock::now() - t0).count(); if (pos == n_prompt - 1) ttft_ms = std::chrono::duration(Clock::now() - t_start).count(); // **THE PREDICTION AT THE LAST PROMPT POSITION *IS* THE FIRST GENERATED TOKEN.** Sampling on every // position and recording only from `n_prompt - 1` onward is what keeps the two cases from needing // separate handling - and the version that "obviously" only samples after the prompt loses exactly one // token's worth of conditioning. if (pos >= n_prompt - 1) produced.push_back(next); if ((int64_t) produced.size() >= o.max_new) break; if (o.stop_eos && pos >= n_prompt - 1 && std::find(o.eos_ids.begin(), o.eos_ids.end(), (int64_t) next) != o.eos_ids.end()) break; // TEACHER FORCING while the prompt lasts: the next input is the prompt's own next token, not the // model's guess. Feeding the guess would make the run depend on the model's own errors from position // 1, which is a different (and worse) measurement of the same prompt. // and the window advances: the token just decoded becomes the newest predecessor. ss.ple_prev[0] = ss.ple_prev[1]; ss.ple_prev[1] = (int32_t) tok; tok = (pos + 1 < n_prompt) ? o.tokens[(size_t) (pos + 1)] : next; // Plan v0.3 P6: from the first generated token on, the speculative loop below takes over. if (o.spec > 0 && pos >= n_prompt - 1) { spec_pos = pos + 1; break; } } // ================================ plan v0.3 P6: SPECULATIVE DECODING ================================ // // Each round verifies [the last emitted token, drafts...] in one window; the window's argmax after token t // is exactly what greedy decode would emit there, so the first draft that differs ends the round and the // round emits (accepted drafts + 1) tokens. `commit` keeps the state of the tokens that were emitted. const bool ended = o.stop_eos && !produced.empty() && std::find(o.eos_ids.begin(), o.eos_ids.end(), (int64_t) produced.back()) != o.eos_ids.end(); if (spec_pos > 0 && (int64_t) produced.size() < o.max_new && !ended) { std::vector oracle; if (!o.spec_oracle.empty()) { std::ifstream in(o.spec_oracle); std::string text((std::istreambuf_iterator(in)), std::istreambuf_iterator()); std::string e; if (!in || !parse_i64_list(text.c_str(), oracle, e)) { std::fprintf(stderr, "strata generate: cannot read --spec-oracle %s\n", o.spec_oracle.c_str()); return 2; } } if (thits.d_res == nullptr) { std::fprintf(stderr, "strata generate: --spec needs the device residency table (--expert-profile, " "--expert-cache and the token graph)\n"); return 2; } mem_mark("the head and the prompt path"); strata::core::Verifier ver; strata::core::VerifyHits vh; vh.d_res = thits.d_res; vh.cache_base = thits.cache_base; vh.blob = thits.blob; if (!ver.init(wt, g, ss, vh, native_head.loaded() ? &native_head : nullptr, o.spec, err)) { std::fprintf(stderr, "strata generate: %s\n", err.c_str()); return 1; } const bool use_mtp = !o.mtp.empty(); if (use_mtp && !mtp.bind(wt, &native_head, ver.final_R_all(), err)) { std::fprintf(stderr, "strata generate: %s\n", err.c_str()); return 1; } mem_mark("the verifier and the drafter's binding"); ver.set_split(o.spec_split); // auto: the copy kernel for every pack. DMA (the native packs' default until 0.1.13) has the host call // cudaMemcpyAsync + cudaLaunchHostFunc inside a verify window while the GPU spins on the flag they raise; // issue #31's thread dumps show the host stuck in that cudaMemcpyAsync on a driver lock for good. The copy // kernel needs no host CUDA call there, and costs ~1-3% decode on IQ3_S (45.3 -> 44.8 tok/s, 8 requests). ver.set_pcie_mode(o.pcie_mode == "dma" ? 0 : o.pcie_mode == "direct" ? 1 : 2); drive.d.plan = ver.plan_sink(); drive.d.pcie_num = (int) (o.pcie_frac * 256.0 + 0.5); if (drive.d.pcie_num < 0) drive.d.pcie_num = 0; if (drive.d.pcie_num > 256) drive.d.pcie_num = 256; const int64_t pcie0 = drive.d.pcie_experts; if (o.adapt_every > 0 && o.adapt_swaps > 0) drive.d.usage.assign((size_t) (g.n_layers * g.n_expert), 0.0f); int64_t swaps_total = 0; double ms_adapt = 0; cudaStream_t adapt_stream = nullptr; if (!drive.d.usage.empty() && cudaStreamCreateWithFlags(&adapt_stream, cudaStreamNonBlocking) != cudaSuccess) { std::fprintf(stderr, "strata generate: cannot create the refill stream\n"); return 1; } // plan v0.3 P6: swaps in flight - (residency index, slot) admitted when adapt_ev has completed std::vector> pending; cudaEvent_t adapt_ev = nullptr; cudaEventCreateWithFlags(&adapt_ev, cudaEventDisableTiming); auto apply_pending = [&](bool wait) { if (pending.empty()) return; if (wait) cudaEventSynchronize(adapt_ev); else if (cudaEventQuery(adapt_ev) != cudaSuccess) return; for (const auto& [i, slot] : pending) host_res[(size_t) i] = slot; pending.clear(); if (d_res != nullptr) cudaMemcpy(d_res, host_res.data(), host_res.size() * sizeof(int32_t), cudaMemcpyHostToDevice); }; // Plan v0.3 P6: the VRAM tier follows the conversation. Candidates are missing experts routed at least // twice (decayed); each is paired with its layer's least-routed resident expert and swapped when it was // routed clearly more often. Copies run between rounds, when the GPU is idle. auto adapt = [&]() -> bool { const Clock::time_point ta = Clock::now(); if (!pending.empty()) return true; // the previous swaps are still in flight struct Swap { float gain; int32_t layer, in, out; }; std::vector swaps; std::vector> cand, vict; for (int64_t l = 0; l < g.n_layers; ++l) { cand.clear(); vict.clear(); const float* u = drive.d.usage.data() + l * g.n_expert; const int32_t* r = host_res.data() + l * g.n_expert; for (int32_t e = 0; e < (int32_t) g.n_expert; ++e) { if (r[e] < 0) { if (u[e] >= 2.0f) cand.emplace_back(u[e], e); } else vict.emplace_back(u[e], e); } if (cand.empty() || vict.empty()) continue; std::sort(cand.begin(), cand.end(), [](auto& a, auto& b) { return a.first > b.first; }); const size_t nc = std::min(cand.size(), vict.size()); std::partial_sort(vict.begin(), vict.begin() + (ptrdiff_t) nc, vict.end(), [](auto& a, auto& b) { return a.first < b.first; }); for (size_t i = 0; i < nc; ++i) { if (cand[i].first < vict[i].first + 1.5f) break; swaps.push_back({cand[i].first - vict[i].first, (int32_t) l, cand[i].second, vict[i].second}); } } std::sort(swaps.begin(), swaps.end(), [](const Swap& a, const Swap& b) { return a.gain > b.gain; }); if ((int) swaps.size() > o.adapt_swaps) swaps.resize((size_t) o.adapt_swaps); for (const Swap& s : swaps) { const size_t in = (size_t) s.layer * g.n_expert + s.in, out = (size_t) s.layer * g.n_expert + s.out; const int32_t slot = host_res[out]; const uint8_t* b = srcp->blob(s.layer, s.in); // asynchronous: the copies run while the MTP drafts; the next window waits for them if (slot < 0 || b == nullptr || cudaMemcpyAsync(xcache.device_slot(slot), b, (size_t) strata::kernels::cpu::expert_layout().blob_bytes(s.layer), cudaMemcpyHostToDevice, adapt_stream) != cudaSuccess) { std::fprintf(stderr, "strata generate: an adaptive refill failed\n"); return false; } host_res[out] = strata::core::kNotResident; // evicted now: the CPU computes it meanwhile pending.emplace_back((int32_t) in, slot); // resident once the copy has landed } if (!swaps.empty()) cudaEventRecord(adapt_ev, adapt_stream); for (float& v : drive.d.usage) v *= 0.7f; swaps_total += (int64_t) swaps.size(); ms_adapt += std::chrono::duration(Clock::now() - ta).count(); return true; }; int64_t p = spec_pos; int32_t x = (int32_t) tok; std::vector drafts((size_t) o.spec, 0); std::vector dprob((size_t) o.spec, 1.0f); std::vector window_hist((size_t) o.spec + 1, 0); // plan v0.3 P6: with a native pack the first window is the last prompt token alone (it produces the first // generated token and the MTP's first cell); otherwise the token loop already did that. bool first_window = native_pack; if (use_mtp && !first_window && !mtp.draft_first(o.spec, ss.R, x, p - 1, drafts.data(), err, dprob.data(), (float) o.spec_min_p)) { std::fprintf(stderr, "strata generate: %s\n", err.c_str()); return 1; } std::vector window((size_t) o.spec), outv((size_t) o.spec); std::vector accepted_hist((size_t) o.spec, 0); int64_t rounds = 0, drafts_total = 0, drafts_ok = 0, corrupt_counter = 0; const int S_mtp = o.mtp_max_t > 0 ? std::min(o.mtp_max_t, o.spec) : o.spec; if (use_mtp && S_mtp < o.spec) mtp.set_max_drafts(S_mtp - 1); strata::spec::SuffixDrafter sfx(std::max(1, o.suffix_draft), 64, (size_t) o.max_context + 4096); strata::spec::DraftPolicy policy(o.spec); // MTP or lookup window (see draft_policy.hpp) std::vector sbuf((size_t) o.spec, 0); int64_t sfx_windows = 0, sfx_drafts = 0, sfx_ok = 0; if (o.suffix_draft > 0) { for (int64_t t : o.tokens) sfx.append((int32_t) t); for (int64_t t : produced) sfx.append((int32_t) t); } const double pool_ms0 = drive.cpu_ms; const int64_t misses0 = drive.d.multi_misses, entries0 = drive.d.multi_entries; while ((int64_t) produced.size() < o.max_new) { const Clock::time_point t0 = Clock::now(); int T = S_mtp; if (use_mtp && o.spec_min_p > 0.0) { T = 1; while (T < S_mtp && dprob[(size_t) T - 1] >= (float) o.spec_min_p) ++T; } if (first_window) T = 1; bool from_sfx = false; int sfx_match = 0; if (o.suffix_draft > 0 && !first_window) { const int k = sfx.propose(o.spec - 1, sbuf.data()); sfx_match = sfx.last_match(); if (k > 0 && (!use_mtp || sbuf[0] == drafts[0])) { const strata::spec::DraftPolicy::Pick pk = policy.choose(T, k, sfx_match); if (pk.lookup) { T = pk.t; from_sfx = true; } } } const bool timed_round = !first_window; ++window_hist[(size_t) T]; if (p + T > o.max_context) { std::fprintf(stderr, "strata generate: ran out of context at position %lld\n", (long long) p); return 2; } window[0] = x; for (int i = 1; i < T; ++i) { const size_t at = produced.size() - 1 + (size_t) i; int32_t d = from_sfx ? sbuf[(size_t) i - 1] : use_mtp ? drafts[(size_t) i - 1] : at < oracle.size() ? (int32_t) oracle[at] : 0; if (o.spec_corrupt > 0 && (++corrupt_counter % o.spec_corrupt) == 0) d = (d + 1) % (int32_t) n_vocab; window[(size_t) i] = d; } drive.d.layers = 0; drive.d.experts = 0; drive.d.failed = false; apply_pending(false); if (!ver.run(T, window.data(), p, &drive_pool_multi, &drive, outv.data(), err)) { std::fprintf(stderr, "strata generate: %s\n", err.c_str()); return 1; } if (drive.d.failed) { std::fprintf(stderr, "strata generate: the expert pool failed at layer %lld expert %lld: %s\n", (long long) drive.d.fail_layer, (long long) drive.d.fail_expert, drive.d.fail ? drive.d.fail : "(no message)"); return 1; } int a = 0; while (a < T - 1 && window[(size_t) a + 1] == outv[(size_t) a]) ++a; if (first_window) { first_window = false; ttft_ms = std::chrono::duration(Clock::now() - t_start).count(); } // plan v0.3 P6: the adaptive tier's host work (ranking, copy submission) runs on its own thread while the // GPU commits and drafts; it touches only the residency tables, which nothing reads until the next window std::thread adapt_thr; bool adapt_ok = true; if (!drive.d.usage.empty() && ((rounds + 1) % o.adapt_every) == 0) adapt_thr = std::thread([&] { adapt_ok = adapt(); }); if (!ver.commit(a + 1, err)) { if (adapt_thr.joinable()) adapt_thr.join(); std::fprintf(stderr, "strata generate: %s\n", err.c_str()); return 1; } ++rounds; drafts_total += T - 1; drafts_ok += a; ++accepted_hist[(size_t) a]; if (from_sfx) { ++sfx_windows; sfx_drafts += T - 1; sfx_ok += a; } bool eos = false; for (int i = 0; i <= a && (int64_t) produced.size() < o.max_new && !eos; ++i) { produced.push_back(outv[(size_t) i]); if (o.suffix_draft > 0) sfx.append(outv[(size_t) i]); eos = o.stop_eos && std::find(o.eos_ids.begin(), o.eos_ids.end(), (int64_t) outv[(size_t) i]) != o.eos_ids.end(); } if (eos) { if (adapt_thr.joinable()) adapt_thr.join(); total_ms += std::chrono::duration(Clock::now() - t0).count(); break; } const bool drafted = !use_mtp || (int64_t) produced.size() >= o.max_new || mtp.draft(T, outv.data(), p, a, drafts.data(), err, dprob.data(), (float) o.spec_min_p); if (adapt_thr.joinable()) adapt_thr.join(); if (!adapt_ok) return 1; if (!drafted) { std::fprintf(stderr, "strata generate: %s\n", err.c_str()); return 1; } x = outv[(size_t) a]; p += a + 1; const double round_ms = std::chrono::duration(Clock::now() - t0).count(); total_ms += round_ms; if (timed_round) policy.observe(from_sfx, T, a, sfx_match, round_ms); if (rounds % 64 == 0) std::fprintf(stderr, "strata generate: position %lld, %lld tokens, %lld rounds\n", (long long) p, (long long) produced.size(), (long long) rounds); } std::printf("%-24s %lld rounds of %d, drafts accepted %lld of %lld (%.3f), %.2f tokens per round\n", "speculation", (long long) rounds, o.spec, (long long) drafts_ok, (long long) drafts_total, drafts_total > 0 ? (double) drafts_ok / (double) drafts_total : 0.0, rounds > 0 ? (double) (drafts_ok + rounds) / (double) rounds : 0.0); if (o.spec_min_p > 0.0) { std::printf("%-24s", "window sizes"); for (size_t i = 1; i < window_hist.size(); ++i) std::printf(" T%zu:%lld", i, (long long) window_hist[i]); std::printf(" (min draft probability %.2f)\n", o.spec_min_p); } if (o.suffix_draft > 0) std::printf("%-24s %lld windows, drafts accepted %lld of %lld\n", "suffix drafts", (long long) sfx_windows, (long long) sfx_ok, (long long) sfx_drafts); std::printf("%-24s", "accepted per round"); for (size_t i = 0; i < accepted_hist.size(); ++i) std::printf(" %zu:%lld", i, (long long) accepted_hist[i]); std::printf("\n"); if (rounds > 0) std::printf("%-24s wait for rings %.3f pool %.3f host %.3f commit %.3f ms/round; CPU experts %.2f " "distinct / %.2f routed per layer\n", "verify window", ver.ms_wait / rounds, ver.ms_pool / rounds, ver.ms_host / rounds, ver.ms_commit / rounds, (double) (drive.d.multi_misses - misses0) / (double) (rounds * g.n_layers), (double) (drive.d.multi_entries - entries0) / (double) (rounds * g.n_layers)); if (rounds > 0) std::printf("%-24s gate/up %.3f quantize %.3f down %.3f ms/round; %.1f GB/s over the rows phases; " "CPU pool call %.3f ms/round\n", "pool multi", pool.ms_multi_gu / rounds, pool.ms_multi_q / rounds, pool.ms_multi_down / rounds, (double) pool.multi_bytes / 1e6 / std::max(1e-9, pool.ms_multi_gu + pool.ms_multi_down), (drive.cpu_ms - pool_ms0) / rounds); if (rounds > 0) std::printf("%-24s plan %.3f activation quantize %.3f jobs %.3f run %.3f ms/round\n", "dispatch", drive.d.ms_plan / rounds, drive.d.ms_actq / rounds, drive.d.ms_jobs / rounds, drive.d.ms_run / rounds); if (rounds > 0 && !drive.d.usage.empty()) std::printf("%-24s %lld experts swapped into the VRAM tier (every %d rounds, %.3f ms/round)\n", "adaptive tier", (long long) swaps_total, o.adapt_every, ms_adapt / rounds); if (rounds > 0 && drive.d.pcie_num > 0) std::printf("%-24s %.2f distinct experts per layer read over PCIe (share %d/256 of the misses)\n", "pcie experts", (double) (drive.d.pcie_experts - pcie0) / (double) (rounds * g.n_layers), drive.d.pcie_num); (void) pool_ms0; if (use_mtp && rounds > 0) std::printf("%-24s %.3f ms/round drafting (%lld rounds), MTP prompt %.1f ms, %.0f MiB of VRAM\n", "mtp", mtp.ms_draft / (double) mtp.rounds, (long long) mtp.rounds, mtp.ms_prefill, (double) mtp.vram_bytes() / 1048576.0); } if (dump != nullptr && std::fclose(dump) != 0) { std::fprintf(stderr, "strata generate: cannot finish logits dump\n"); return 1; } if (layer_dump != nullptr) { std::fclose(layer_dump); cudaFreeHost(layer_stage); std::printf("%-24s %s (%lld layers + the input x %d streams x %lld per position)\n", "layers dumped", o.dump_layers.c_str(), (long long) g.n_layers, (int) g.hc, (long long) g.n_embd); } if (half_dump != nullptr) { std::fclose(half_dump); cudaFreeHost(half_stage); std::printf("%-24s %s (%lld layers x %llu per position)\n", "halves dumped", o.dump_halves.c_str(), (long long) g.n_layers, (unsigned long long) half_stride); } if (routing != nullptr) { std::fclose(routing); drive.routing = nullptr; std::printf("%-24s %s (%lld records of layer, k, ids, weights)\n", "routing dumped", o.dump_routing.c_str(), (long long) drive.calls); } if (o.stage_timing) strata::core::stage_timing_report(g.n_layers); const int64_t decoded = (int64_t) produced.size(); std::printf("prompt :"); for (int64_t t : o.tokens) std::printf(" %lld", (long long) t); std::printf("\noutput :"); for (int64_t t : produced) std::printf(" %lld", (long long) t); std::printf("\n"); const double decode_ms = decoded > 0 ? total_ms / (double) decoded : 0.0; std::printf("%-24s %lld tokens in %.1f ms -> %.2f tok/s\n", "decode", (long long) decoded, total_ms, decode_ms > 0.0 ? 1000.0 / decode_ms : 0.0); if (n_prompt > 1) std::printf("%-24s %lld tokens in %.1f ms -> %.2f tok/s (time to first token %.1f ms)\n", "prefill", (long long) (n_prompt - 1), prefill_ms, prefill_ms > 0 ? 1000.0 * (double) (n_prompt - 1) / prefill_ms : 0.0, ttft_ms); if (!o.dump_mixed.empty()) { std::vector mx((size_t) g.n_embd); if (cudaMemcpy(mx.data(), ss.block.mixed, mx.size() * sizeof(float), cudaMemcpyDeviceToHost) != cudaSuccess) { std::fprintf(stderr, "strata generate: reading mixed back failed\n"); return 1; } std::FILE* mf = std::fopen(o.dump_mixed.c_str(), "wb"); if (mf == nullptr) { std::fprintf(stderr, "strata generate: cannot write %s\n", o.dump_mixed.c_str()); return 1; } std::fwrite(mx.data(), sizeof(float), mx.size(), mf); std::fclose(mf); double s2 = 0, mag = 0; for (float v : mx) { s2 += (double) v * (double) v; mag += std::fabs((double) v); } std::printf("%-24s %s (n_embd %lld, rms %.5g, mean|.| %.5g)\n", "mixed dumped", o.dump_mixed.c_str(), (long long) g.n_embd, std::sqrt(s2 / (double) mx.size()), mag / (double) mx.size()); } // ---- the residual, for bisecting the head against the layers (see `dump_residual`'s note) if (!o.dump_residual.empty()) { std::vector R((size_t) g.hc * g.n_embd); if (cudaMemcpy(R.data(), ss.R, R.size() * sizeof(float), cudaMemcpyDeviceToHost) != cudaSuccess) { std::fprintf(stderr, "strata generate: reading R back failed\n"); return 1; } std::FILE* rf = std::fopen(o.dump_residual.c_str(), "wb"); if (rf == nullptr) { std::fprintf(stderr, "strata generate: cannot write %s\n", o.dump_residual.c_str()); return 1; } const int32_t hdr[2] = {(int32_t) g.hc, (int32_t) g.n_embd}; std::fwrite(hdr, sizeof hdr, 1, rf); std::fwrite(R.data(), sizeof(float), R.size(), rf); std::fclose(rf); double mag = 0, mx = 0; int bad = 0; for (float v : R) { if (!std::isfinite(v)) ++bad; else { mag += std::fabs((double) v); mx = std::max(mx, (double) std::fabs((double) v)); } } std::printf("%-24s %s (%d x %lld, nonfinite %d, mean|.| %.4g, max|.| %.4g)\n", "residual dumped", o.dump_residual.c_str(), (int) g.hc, (long long) g.n_embd, bad, mag / (double) R.size(), mx); } if (o.stats) { std::printf("%-24s %.3f ms/token (WALL CLOCK: embed, layers, head, sample)\n", " per token", decode_ms); // **THE PER-TOKEN HOST TERM, WHICH `--gpu-only-full` CANNOT SEE.** That measurement never enters the // token loop, so it excludes all six of these. On the 78-token fixture + 200 generated tokens the six // sum to ~9 ms of non-layer work against ~1.5 ms of actual head GPU work - 16% of the token, and it is // not the pool. if (phase_tokens > 0) { const double pt = (double) phase_tokens; std::printf("%-24s PLE %.3f embed %.3f LAYERS %.3f head %.3f readback %.3f sample %.3f " "(sum %.3f of %.3f ms)\n", " token host phases", ms_ple / pt, ms_embed / pt, ms_layers / pt, ms_head / pt, ms_readback / pt, ms_sample / pt, (ms_ple + ms_embed + ms_layers + ms_head + ms_readback + ms_sample) / pt, decode_ms); } if (const std::string io = ple_table.io_report(); !io.empty()) std::printf(" %s\n", io.c_str()); // **THE DENOMINATOR IS THE POSITIONS THE POOL ACTUALLY RAN ON, NOT THE DECODED TOKENS (A6).** // `drive_pool` is called once per layer per position and PREFILL runs the loop too, so accumulating // `cpu_ms` over prefill and then dividing by `decoded` inflates this figure. `drive.calls / n_layers` // is the number of positions - the same correction the ring counters below already received, which is // why they print "of 192" rather than "240 of 192". const double pool_positions = g.n_layers > 0 ? (double) drive.calls / (double) g.n_layers : 0.0; std::printf("%-24s %.3f ms/token over %lld layers (%.0f positions, %lld dispatches)\n", " the CPU expert pool", pool_positions > 0.0 ? drive.cpu_ms / pool_positions : 0.0, (long long) g.n_layers, pool_positions, (long long) drive.calls); // **AND WHERE INSIDE `run()` IT WENT.** Three phases per layer and they were one number, which cannot // tell a pool that is slow at the WORK from one that is slow at the SYNCHRONISATION - opposite fixes. // Wait-for-park is expected to be ~0 (the workers re-parked at the end of the previous layer); the // question is whether the time is in the drain or in the re-park barrier. if (pool_positions > 0.0) { double wp = 0, dr = 0, rp = 0; pool.phase_ms(wp, dr, rp); const double per = pool_positions; std::printf("%-24s wait-park %.3f drain %.3f re-park %.3f ms/token\n", " pool phases", wp / per, dr / per, rp / per); } std::printf("%-24s %lld blobs read\n", " expert blobs", (long long) srcp->reads()); // ---- **R4's DISPATCH MEASUREMENT: h, ON THE ENGINE'S OWN ROUTING.** No offline trace, no corpus // question, no k-fold - these are the ids the router actually produced on this run. Reported as // hits/lookups so it can be read directly as the h the cache would deliver, and alongside `refused` // so a full cache is visible rather than silently capping the rate. if (o.expert_cache > 0) { const int64_t look = drive.d.cache_hits + drive.d.cache_admitted + drive.d.cache_refused; const int64_t hl = drive.d.hit_ready + drive.d.hit_late; std::printf("%-24s %lld of %lld layers the hit work was DONE when the pool returned\n", " R4 overlap", (long long) drive.d.hit_ready, (long long) hl); std::printf("%-24s %lld of %lld = %.4f (%lld admitted, %lld refused, cache %.4f%% full)\n", " R4 expert-cache hits", (long long) drive.d.cache_hits, (long long) look, look > 0 ? (double) drive.d.cache_hits / (double) look : 0.0, (long long) drive.d.cache_admitted, (long long) drive.d.cache_refused, 100.0 * (double) (drive.d.cache_admitted + drive.d.cache_hits > 0 ? (double) xcache.resident() / (double) xcache.slots() : 0.0)); } if (tgraph.captured && tgraph.calls > 0) { const double per = (double) tgraph.calls; std::printf("%-24s wait for rings %.3f pool %.3f ms/token (%lld flushes over %lld positions)\n", " token graph", tgraph.ms_wait / per, tgraph.ms_pool / per, (long long) tgraph.flushes, (long long) tgraph.calls); } if (gr.captured && gr.calls_total > 0) { // The counters are CUMULATIVE over every `session_loop` call, and PREFILL runs the loop too - so // the denominator is the number of positions, not the number of generated tokens. Dividing by // `n_layers * decoded` printed "240 of 192", which is a reporting bug that looks like a ring // firing more often than it should. const int64_t positions = gr.calls_total; std::printf("%-24s %lld of %lld over %lld positions\n", " rings seen MID-GRAPH", (long long) gr.rings_mid_graph, (long long) (g.n_layers * positions), (long long) positions); std::printf("%-24s %.3f ms of a %.3f ms layer\n", " ring latency", gr.ms_to_ring / (double) (g.n_layers * positions), decode_ms / (double) g.n_layers); // **THE ROUND TRIP, SPLIT AT THE RING.** `ring latency` is the first half and stops when the ring // is seen; this is the second half - the driver calls after it, during which the GPU is IDLE // because `post[l]` has not been launched yet. `--no-pool` is the arm that isolates it: 38.73 // ms/token against a 26.32 ms pure-GPU floor is 12.4 ms of round trip with no expert work at all. // // Same denominator as the pool line above (the positions the loop actually ran on), so the two can // be added without one of them being inflated by prefill. const double perlap = (double) (g.n_layers * positions); std::printf("%-24s %.3f ms/token over %.0f positions (%.3f ms/layer, after the ring)\n", " host after ring", pool_positions > 0.0 ? gr.ms_host / pool_positions : 0.0, pool_positions, gr.ms_host / perlap); } } if (dump != nullptr) std::printf("%-24s %s\n", "logits dumped", o.dump_logits.c_str()); strata::core::session_graphs_free(gr); strata::core::doorbell_free(db); cudaFree(d_next); cudaFree(d_logits); cudaFree(d_emb); cudaFree(d_parts); cudaFree(sbuf); cudaFree(arena); return 0; }