Repository navigation
model : add DeepSeek-V4.1-Flash-1017 (deepseek41), exact reference implementation - #289
Draft
danielhanchen wants to merge 10 commits into
Draft
danielhanchen wants to merge 10 commits into
danielhanchen wants to merge 10 commits into
Conversation
FP8 32x32 linears are dequantized to BF16 (exact), routed experts are copied bit-exact as MXFP4, Engram tables are precomputed as BF16 (e4m3 * 2^e8m0, exact) and streamed in bounded chunks, router and Engram gate are kept in F32. --outtype f32 keeps every dense tensor and the Engram tables in F32. The Engram tables of all layers are written as one per_layer_token_embd (rows concatenated in layer order) with the PLE keys of qwen4exp: ple.layers, ple.ngram_size, ple.heads_per_ngram, ple.layer_multipliers, ple.head_offsets (absolute rows), ple.head_vocab_sizes and embedding_length_per_layer_input. The compressed token map and the pad token id stay deepseek41.engram keys.
…upport - arch, KV keys and Engram tensors for deepseek41; the Engram tables of all layers are one per_layer_token_embd described by the qwen4exp PLE keys, the compressed token map and the pad token id are the only new Engram keys - hparams for the cross-layer KV / indexer sharing and the two-level candidate selection - DSV4 KV cache: compress ratios are taken from the slot state instead of the V4 constants; for V4.1 the CSA slot is ratio 2, the HCA slot ratio 1, without overlap and without the lightning-indexer cache, the index K is stored next to the compressed KV and only the KV source layers own rows - the window cells keep the token ids (saved and restored with the state) for the Engram n-gram history
Engram (host-side n-gram hash, rows gathered on the CPU from per_layer_token_embd, which is read lazily from the mapping like the qwen4exp n-gram table, its rows prefetched per ubatch), single-pass mHC, ratio 1/2 compressors with cross-layer KV / index-K / top-k sharing and the two-level candidate block selection. Every compressed layer attends densely over [window | all compressed rows] with the top-k applied as a mask. The indexer score, candidate blocks and top-k are computed per chunk of query tokens, so the [index heads, tokens, rows] intermediate stays near 2^26 elements at long context (a single chunk, the plain graph, whenever it fits). Tokens shared by several sequences are supported; embedding input skips Engram, like the masked positions of the reference. The model reuses the DeepSeek-V4 loader and graph helpers; the HC mix computation is split out of build_hc_pre (unchanged for V4). The F32 router, HC and indexer matmuls request F32 precision, and the router weights are normalized by sum + 1e-20 as in the reference. hash_layer_count is optional for V4.1: it has no hash layers, and V4.1 GGUFs converted before the key was written lack it.
…d DSML tool calls - test-llama-archs: a 6-layer config with window-only, Engram, ratio-2 and ratio-1 KV sources, top-k reuse and a candidate-masked index layer; the model saver writes the deepseek41 sharing and Engram keys (round trip) - the official chat template (ported from encoding.py), template detection, and the DSML tool call parser for the V4.1 tag names (leading space: <|DSML| calls>, <|DSML| invoke>, <|DSML| parameter>) - a V4.1 content continuation in thinking mode consumes the bare </think> of the generation prompt
- remove the V4.1 chat template file, its template detection, the DSML parser change and their test-chat cases; the converter drops the inherited DeepSeek-V4 default template (a template shipped with the model is still added by set_vocab) - hash_layer_count is required again (the converter writes 0 for V4.1) - the converter no longer imports sympy - engram_index moves out of models.h; comments that restate the code are removed
MXFP8 is the OCP microscaling FP8 format: 32 E4M3 values sharing one E8M0 scale (33 bytes per block). DeepSeek-V4.1 stores its Engram tables this way; keeping them as MXFP8 halves the table size against the exact BF16 lookup (203 GB vs 393 GB) with identical dequantized values. The type is gather-only: CPU and CUDA support GET_ROWS (source 0) and refuse every other op in supports_op, so misuse fails at scheduling. Row (de)quantizers use 64-bit block loops since the tables exceed 2^31 elements. gguf-py gains the type and a reference (de)quantizer; test-backend-ops covers GET_ROWS. MXFP8 takes the next free type id, 43.
The checkpoint's Engram tables are e4m3 rows with one e8m0 scale per 32 elements, i.e. MXFP8 block for block: copy them bit-exact as [e8m0 | 32 x e4m3] blocks into per_layer_token_embd instead of precomputing the BF16 lookup. NaN scale or code bytes are rejected. DSV41_ENGRAM_TYPE=bf16 keeps the exact BF16 table (F32 with --outtype f32).
This was referenced Oct 19, 2026
…d deepseek41 tensors as ndarray
… KV cache; instantiate add_kv for int32 arrays
This was referenced Oct 19, 2026
Member
Author
|
Pushed f42144f: the final projection now goes through CPU repro on a six-layer fixture with rank-1 adapters (128 logits):
No-adapter logits are bit-identical before and after for F32 and Q8_0 output heads. |
This was referenced Oct 22, 2026
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds DeepSeek-V4.1-Flash-1017 (
deepseek41) as a plain reference implementation: exact, unoptimized, and as small as the model allows. Performance work, quantized KV cache, the DSpark draft, the chat template and DSML tool-call parsing, and vision come as separate PRs stacked on this one.The branch is based on upstream
797de63b2, which is tagb11716. The diff is only this change: 33 files, +1661/-51. The onlyggml/change is a gather-only MXFP8 type for the Engram table (see below).What it adds
src/models/deepseek41.cpp, reusing the DeepSeek-V4 loader and graph helpers):build_hc_pre, and DeepSeek-V4 output is unchanged.[window | all compressed rows], with the top-k selection applied as a mask. This is the unfused reference path.[index heads, tokens, rows]intermediate near 2^26 elements at long context, and short prompts run as a single chunk.GGML_PREC_F32. Router weights are normalized bysum + 1e-20, as in the reference.per_layer_token_embd:per_layer_token_embd.weight[256, 768,022,850], with layer 1's rows first.qwen4exp(ple.layers,ple.ngram_size,ple.heads_per_ngram,ple.layer_multipliers,ple.head_offsets,ple.head_vocab_sizes). Only the compressed token map and the pad token id are new keys.qwen4expn-gram table:--lazy-mode autois the default and applies to tensors over 4 GiB. Each ubatch prefetches the rows of both layers in onellama_prefetch_rowscall.src/llama-kv-cache-dsv4.*):ggml/,gguf-py/): the OCP microscaling FP8 block, 32 E4M3 values with one E8M0 scale (33 bytes), asGGML_TYPE_MXFP8 = 43.GET_ROWSfrom it, andsupports_oprefuses every other op, so any other use fails at scheduling.test-backend-opscoversGET_ROWS.conversion/deepseek41.py):DSV41_ENGRAM_TYPE=bf16writes the exact BF16 lookup instead, streamed in bounded chunks.--outtype f32keeps every dense tensor in F32 (and the table too withDSV41_ENGRAM_TYPE=bf16).test-llama-archsgets a 6-layer config covering window-only, Engram, ratio-2 and ratio-1 KV sources, top-k reuse and a candidate-masked index layer. The model saver writes the new keys, and the round trip is covered.Verification
All results are local, on B200 (CUDA
sm_100a) and x86 CPU.Converter:
e4m3 * 2^(e8m0-127)lookup bit for bit. The sample covers both tables, all 24 hash columns, and the first and last rows. A seeded off-by-one fails the check.Fixture gates on CPU against the official F32 reference (
model.py, matched precision):test-backend-ops:GET_ROWS249/249 on CPU and 225/225 on CUDA, including the MXFP8 cases.Seeded bugs: the gate was calibrated with seeded bugs, and every one of them fails it:
Full model on CUDA (2 x B200, the published UD-Q8_K_XL GGUF with the MXFP8 table, read lazily; the BF16 table gives the same numbers) against the official F32 reference, on this branch's head: mean KL 4.76e-3 on wiki1100 and 2.81e-3 / 2.85e-3 on long17k, with top-1 agreement 98.6% and 98.7%. The official native kernels score 2.6e-2 / 1.5e-2 against the same reference.
llama-serverloads both published files with default settings and answers chat requests with the template embedded in the GGUF. With-ot per_layer_token_embd.weight=CPUautomatic fitting is off, so a context size has to be given (-c 32768works); the full default context needs a large compute buffer on the reference attention path.Revisions since the first gated revision:
sgn(0)in the Engram signed sqrt, the embeddings output width, the per-chunk indexer, and GGUF validation.per_layer_token_embdlayout with lazy reading and prefetch.Lazy table on a cold NVMe page cache (a quantized model whose non-table weights total 114 GiB, 2 GPUs):
--lazy-mode off(table resident)Runtime:
llama-batched -np 2and-np 4match-np 1.Other models:
test-llama-archs -ocan write (134 random models) was run on CPU with this branch and with the base, at ubatch 512 and 7. Logits are bit-identical in all 264 runs; the 4 others (diffusion models at ubatch 7) abort the same way on both.test-llama-archspasses 140/140 on CPU (139 on the base, plus deepseek41), andtest-chatpasses.test-llama-contextpasses on the generated deepseek41 and deepseek4 models; the quantized KV case is skipped for deepseek41 (context creation fails with an error, quantized KV comes in a stacked PR).LLAMA_FATAL_WARNINGS=ON.Known gaps
-ctk q8_0) (model : deepseek41 quantized K cache, shared prompt tokens, Engram host placement #293).softplusunderflows to 0 below x = -16.6, which can drop a token's routed experts (1 in 118,880 router rows on a real quant): ggml : use log1p in softplus so negative inputs do not underflow to 0 #290.