Skip to content

Commit f71094f

Browse files
quic-mamtamamtsing
andauthored
MLA Changes (quic#956)
fix kv head replication --------- Signed-off-by: Mamta Singh <mamtsing@qti.qualcomm.com> Co-authored-by: Mamta Singh <mamtsing@qti.qualcomm.com>
1 parent f1d0dc8 commit f71094f

2 files changed

Lines changed: 35 additions & 33 deletions

File tree

QEfficient/blocking/blocked_attention_forwards.py

Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -898,25 +898,25 @@ def blocked_kv_mla_attention_forward(
898898
absorption = False
899899

900900
k_heads, q_heads = compressed_kv_block.shape[1], query.shape[1]
901-
num_heads_to_repeat = q_heads - k_heads
902-
repeated_ckv_block = compressed_kv_block[:, 0, :, :].expand(
903-
batch_size, num_heads_to_repeat, -1, module.kv_lora_rank
904-
)
905-
compressed_kv_block = torch.cat((compressed_kv_block, repeated_ckv_block), dim=1)
906901

907-
repeated_k_pe_block = k_pe_block[:, 0, :, :].expand(
908-
batch_size, num_heads_to_repeat, -1, module.qk_rope_head_dim
909-
)
910-
k_pe_block = torch.cat((k_pe_block, repeated_k_pe_block), dim=1)
902+
if k_heads > 1:
903+
num_heads_to_repeat = math.ceil(q_heads / k_heads)
904+
compressed_kv_block = (
905+
compressed_kv_block.unsqueeze(2)
906+
.expand(-1, -1, num_heads_to_repeat, -1, -1)
907+
.reshape(batch_size, num_heads_to_repeat * k_heads, -1, module.config.kv_lora_rank)
908+
)
909+
compressed_kv_block = compressed_kv_block[:, :q_heads, :, :]
910+
911+
k_pe_block = (
912+
k_pe_block.unsqueeze(2)
913+
.expand(-1, -1, num_heads_to_repeat, -1, -1)
914+
.reshape(batch_size, num_heads_to_repeat * k_heads, -1, module.config.qk_rope_head_dim)
915+
)
916+
k_pe_block = k_pe_block[:, :q_heads, :, :]
911917

912918
if absorption:
913919
krope_nope = torch.cat((compressed_kv_block, k_pe_block), dim=-1)
914-
k_heads, q_heads = krope_nope.shape[1], query.shape[1]
915-
num_heads_to_repeat = q_heads - k_heads
916-
repeated_k = krope_nope[:, 0, :, :].expand(
917-
batch_size, num_heads_to_repeat, -1, module.qk_rope_head_dim + module.kv_lora_rank
918-
)
919-
krope_nope = torch.cat((krope_nope, repeated_k), dim=1)
920920
attn_weights_block = torch.matmul(query, krope_nope.transpose(2, 3)) * scaling
921921
# [1, 64, q_len, 576] X [1, 1, 576, kv_block_size] -> [1, 64, q_len, kv_block_size]
922922
attn_weights_block = torch.where(causal_mask_block, masked_tensor, attn_weights_block)
@@ -930,20 +930,8 @@ def blocked_kv_mla_attention_forward(
930930
skip_future,
931931
) # [1, 64, q_len, kv_block_size] X [1, 1, kv_block_size, 512] -> [1, 64, q_len, 512]
932932
else:
933-
k_heads, q_heads = compressed_kv_block.shape[1], query.shape[1]
934-
num_heads_to_repeat = q_heads - k_heads
935-
repeated_ckv_block = compressed_kv_block[:, 0, :, :].expand(
936-
batch_size, num_heads_to_repeat, -1, module.kv_lora_rank
937-
)
938-
compressed_kv_block = torch.cat((compressed_kv_block, repeated_ckv_block), dim=1)
939933
knope = torch.matmul(compressed_kv_block, per_head_k_up_normal)
940-
941-
repeated_k_pe_block = k_pe_block[:, 0, :, :].expand(
942-
batch_size, num_heads_to_repeat, -1, module.qk_rope_head_dim
943-
)
944-
k_pe_block = torch.cat((k_pe_block, repeated_k_pe_block), dim=1)
945-
946-
krope_nope = torch.cat((knope, k_pe_block.expand(-1, num_heads, -1, -1)), dim=-1)
934+
krope_nope = torch.cat((knope, k_pe_block), dim=-1)
947935
attn_weights_block = torch.matmul(query, krope_nope.transpose(2, 3)) * scaling
948936
attn_weights_block = torch.where(causal_mask_block, masked_tensor, attn_weights_block)
949937
current_max, current_denominator, output = update_running_softmax(

QEfficient/transformers/models/deepseek_v3/modeling_deepseek.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -451,12 +451,26 @@ def fused_forward_orig(
451451
k_pe = compressed_kvs.update_k_pe(k_pe, self.layer_idx, cache_kwargs)
452452

453453
k_heads, q_heads = kva.shape[1], q_pe.shape[1]
454-
num_heads_to_repeat = q_heads - k_heads
455-
repeated_kva = kva[:, 0, :, :].expand(bsz, num_heads_to_repeat, -1, self.kv_lora_rank)
456-
kva_expanded = torch.cat((kva, repeated_kva), dim=1)
457454

458-
repeated_k_pe = k_pe[:, 0, :, :].expand(bsz, num_heads_to_repeat, -1, self.qk_rope_head_dim)
459-
k_pe_expanded = torch.cat((k_pe, repeated_k_pe), dim=1)
455+
if k_heads > 1:
456+
num_heads_to_repeat = math.ceil(q_heads / k_heads)
457+
458+
kva_expanded = (
459+
kva.unsqueeze(2)
460+
.expand(-1, -1, num_heads_to_repeat, -1, -1)
461+
.reshape(bsz, num_heads_to_repeat * k_heads, -1, self.config.kv_lora_rank)
462+
)
463+
kva_expanded = kva_expanded[:, :q_heads, :, :]
464+
465+
k_pe_expanded = (
466+
k_pe.unsqueeze(2)
467+
.expand(-1, -1, num_heads_to_repeat, -1, -1)
468+
.reshape(bsz, num_heads_to_repeat * k_heads, -1, self.config.qk_rope_head_dim)
469+
)
470+
k_pe_expanded = k_pe_expanded[:, :q_heads, :, :]
471+
else:
472+
kva_expanded = kva
473+
k_pe_expanded = k_pe
460474

461475
v_up_per_head = self.v_up.squeeze(0).view(self.kv_lora_rank, self.num_heads, self.v_head_dim).permute(1, 0, 2)
462476
value_states = torch.matmul(kva_expanded, v_up_per_head)

0 commit comments

Comments
 (0)