Skip to content

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions mla/impl/absorbed_cache_compressed_move_elision.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,13 +155,13 @@ def forward(self, hidden_states_q: torch.Tensor, q_position_ids: torch.LongTenso
k_pe = k_pe.view(bsz, 1, kv_seq_len, self.qk_rope_head_dim)

kv_b_proj = self.kv_b_proj.weight.view(self.num_heads, -1, self.kv_lora_rank)
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)
Copy link
Contributor

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


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)
Copy link
Contributor

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)

attn_weights = (torch.matmul(q_pe, k_pe.mT) + torch.matmul(q_nope, compressed_kv.unsqueeze(-3).mT)) * self.softmax_scale
if attn_weights.size() != (bsz, self.num_heads, q_len, kv_seq_len):
raise ValueError(
Expand All @@ -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)
Copy link
Contributor

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)


if attn_output.size() != (bsz, self.num_heads, q_len, self.v_head_dim):
raise ValueError(
Expand Down