optimize_a_cc_me_absorb#4
Open
jychen21 wants to merge 1 commit into
Open
Conversation
Contributor
|
Good point! Actually, we found that using |
wx-csy
reviewed
Aug 29, 2024
| ).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) |
Contributor
There was a problem hiding this comment.
can be changed to attn_output = torch.einsum('bhqc,hdc->bhqd', attn_output, out_absorb)
wx-csy
reviewed
Aug 29, 2024
| 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) |
Contributor
There was a problem hiding this comment.
can be changed to q_nope = torch.einsum('bhqd,hdc->bhqc', q_nope, q_absorb)
wx-csy
reviewed
Aug 29, 2024
| 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) |
Contributor
There was a problem hiding this comment.
no need to unsqueeze here if einsum is used in line 164
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.
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