Repository navigation
Conversation
Assisted-by: Claude Opus 5.5
1 of 5 tasks
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.
Overview
This chunks the DeepSeek-V4.1 fused indexer and the dense attention over the queries of a ubatch, so the
[n_comp, tokens]scores and the[n_raw + n_comp, tokens]masks are only allocated for one chunk at a time. At 640k context and-ub 4096the compute buffer of the GPU that holds the candidate layers goes from 20138 MiB to 14768 MiB, at the same prefill speed.The fused indexer was always called on the whole ubatch, and the attention builds its masks for all tokens of the ubatch at once:
llama.cpp/src/models/deepseek41.cpp
Lines 752 to 758 in d4adfb4
Now the indexer uses chunks of about 2^28 score elements and the attention splits the queries into equal chunks of at most about 2^28 mask cells. Each chunk is expanded into the graph before the next one, so the allocator can reuse its memory.
The chunks of the input masks are views that all layers share, like
gather_mask_of:llama.cpp/src/models/models.h
Lines 1580 to 1584 in d4adfb4
Without this, each layer makes new views of the input masks, and the scheduler copies every view to the GPU again. In my first version this added 2.2 TB of host to device copies in a 262k token pass and made prefill 9% slower.
Additional information
The output is not bit-identical, because the attention runs on smaller query batches. Perplexity is the same within the error (numbers below). The chunked path only runs with one sequence stream; with more streams the code takes the old path.
Measurements
2x RTX 5090, UD-Q4_K_XL, f16 KV, experts on the CPU (
--n-cpu-moe 40 -ts 0.48,0.52), master d4adfb4 plus one local commit that adds the MXFP8 tensor type of the Unsloth engram shard (see #33316). Compute buffers fromsched_reserveat-c 655360:Whole-graph compute buffer on one device (CPU,
llama-fit-params), master vs this PR:llama-perplexity -c 262144 --chunks 1 -b 4096 -ub 4096, same machine and settings:test-llama-archs -a deepseek41passes on CPU, CUDA0 and CUDA1. The test model is too small to reach the chunk sizes, so I also ran it with the chunks forced to 1/4 of the ubatch (indexer) and 16 tokens (attention); both paths ran and the result is the same (NMSE vs. CPU 3.11e-06 on both GPUs). TheMetaconfig could not run on this machine (NCCL fails in my container on master as well).Requirements
Written with Claude Opus 5.5