Add flops calculation for DeepSeek v3.2#2979
Merged
copybara-service[bot] merged 1 commit intomainfrom Jan 28, 2026
Merged
Conversation
2ccaf7e to
573d0a4
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
4 tasks
shuningjin
reviewed
Jan 24, 2026
Collaborator
There was a problem hiding this comment.
Thanks for the careful calculation! Here is the summary of my understanding and questions. Would appreciate your confirmation and clarification (could be in-line).
Let B=batch_size, T=max_target_length, K=index_topk
Indexer Flops
b=B, s=t=T, h=index_n_heads, d=index_head_dim
# indexer linear flop
I_1 = q_proj + k_proj + head_proj
# Q = RoPE(Wq @ q_lora), [b, t, q_lora_rank] x [b, t, q_lora_rank, h * d] -> [b, t, h * d]
q_proj = 2 * B * T * index_n_heads * index_head_dim * q_lora_rank
# K = RoPE(Norm(Wk @ X)), [b, s, embed_dim] x [b, s, emb_dim, d] -> [b, s, d]
k_proj = 2 * B * T * index_head_dim * emb_dim
# Head_Weights = (W_proj @ X), [b, t, embed_dim] x [b, t, emb_dim, h] -> [b, t, h]
head_proj = 2 * B * T * index_n_heads * emb_dim
# indexer quadratic flop (causal factor 0.5 applied)
I_2 = qk_product + index_score
# Logits = ReLU(Q @ K.T), "bthd, bsd -> btsh"
qk_product = B * T^2 * index_n_heads * index_head_dim
# Score = Sum_head(Logits * Head_Weights), "btsh, bth -> bts"
index_score = B * T^2 * index_n_heads
[Remark] Your code looks correct. Might be good to clarify in the comments, especially about causality.
Regular causal attention: B * H * D * T^2, where H = num_query_heads and D = qk_head_dim_sum + v_head_dim (MLA specific)
Sparse causal attention (
- Current logic:
regular_causal_attention_flop + indexer_flop - [Question 1] Shall we keep or skip indexer_flop? In our actual implementation, we skip the indexer computation in this case.
Sparse causal attention (
- Current logic:
2 * B * H * D * (T * K) + indexer_flop. - [Question 2] This seems an approximation, overestimating the cost during the "warm-up" phase (first K tokens). Shall we use the exact formula:
2 * B * H * D * (T * K - K^2 / 2) + indexer_flop?
[Question 3] Should the Indexer cost be accounted inside MLA cost or outside?
dcbe7e6 to
86b5b57
Compare
67f8d7d to
c8897a5
Compare
shuningjin
approved these changes
Jan 27, 2026
gagika
approved these changes
Jan 28, 2026
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.
Description
Add flops calculation for DeepSeek v3.2, and this PR depends on this change
Tests
Checklist
Before submitting this PR, please make sure (put X in square brackets):
gemini-reviewlabel.