-
Notifications
You must be signed in to change notification settings - Fork 16
optimize_a_cc_me_absorb #4
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
Good point! Actually, we found that using |
@@ -174,7 +174,7 @@ def forward(self, hidden_states_q: torch.Tensor, q_position_ids: torch.LongTenso | |||
attn_weights, dim=-1, dtype=torch.float32 | |||
).to(q_nope.dtype) | |||
attn_output = torch.einsum('bhql,blc->bhqc', attn_weights, compressed_kv) | |||
attn_output = torch.matmul(attn_output, out_absorb.mT) # torch.einsum('bhqc,hdc->bhqd', attn_output, out_absorb) | |||
attn_output = attn_output = torch.matmul(attn_output.permute(2, 1, 0, 3), out_absorb.mT).permute(2, 1, 0, 3) # torch.einsum('bhqc,hdc->bhqd', attn_output, out_absorb) |
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.
can be changed to attn_output = torch.einsum('bhqc,hdc->bhqd', attn_output, out_absorb)
|
||
cos, sin = self.rotary_emb(q_pe) | ||
q_pe = apply_rotary_pos_emb(q_pe, cos, sin, q_position_ids) | ||
|
||
q_nope = torch.matmul(q_nope, q_absorb) | ||
q_nope = torch.matmul(q_nope.transpose(0, 2), q_absorb).transpose(0, 2) |
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.
can be changed to q_nope = torch.einsum('bhqd,hdc->bhqc', q_nope, q_absorb)
q_absorb = kv_b_proj[:, :self.qk_nope_head_dim,:] | ||
out_absorb = kv_b_proj[:, self.qk_nope_head_dim:, :] | ||
q_absorb = kv_b_proj[:, :self.qk_nope_head_dim,:].unsqueeze(0) | ||
out_absorb = kv_b_proj[:, self.qk_nope_head_dim:, :].unsqueeze(0) |
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.
no need to unsqueeze here if einsum is used in line 164
We found that below two operations are memory inefficient:
e.g.
q_nope: [B, M, 1, 128]
q_absorb: [1, M, 128, 512]
In this shape, we found that it will trigger elementwise data copy of B times, so we simply permute the q_nope from [B, M, 1, 128] to [1, M, B, 128], to reduce redundant memory movement and also make [1, 128] @ [128, 512] (GEMV) to [B, 128] @ [128, 512] (GEMM).
Below are the test details:
Accuracy

Performance (A100)
python benchmark.py A_CC_ME 1024 --bsz 32