-
Notifications
You must be signed in to change notification settings - Fork 456
Add flops calculation for DeepSeek v3.2 #2979
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
2ccaf7e to
573d0a4
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the clarification and visualization! LGTM.
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.