Skip to content
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

replicate kv for some models when tp is divisble by kv_head_num #2874

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
22 changes: 22 additions & 0 deletions lmdeploy/turbomind/deploy/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,13 +202,35 @@ def _reorder_and_merge(self, qkvo):
o = torch.zeros_like(q)
return qkv, o

def _repeat_kv(self, qkvo, kind: str):
"""replicate kv."""
q, k, v, o = qkvo
head_dim = self.model.model_config.size_per_head
hidden_dim = self.model.model_config.hidden_units

def _repeat(x):
dim = hidden_dim if kind != 'bias' else 1
x = x.view(-1, head_dim, dim).repeat(1, self.model.repeat_kv, 1)
x = x.reshape(-1, dim)
return x

k, v = map(_repeat, (k, v))
if kind == 'bias':
if o is None:
o = torch.zeros(hidden_dim, dtype=q.dtype, device=q.device)
q, k, v, o = map(torch.squeeze, (q, k, v, o))

return (q, k, v, o)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this file, all layout adjustment is performed on the transposed weight ([input_dim, output_dim]), please keep it consistent.

def _export(self, idx: int, qkvo, kind: str, pack_fn, **kwargs):
if all(x is None for x in qkvo):
return
is_lora_a, is_lora_b = get_lora_flags(kind)
if is_lora_a:
qkv, o = map(transpose, qkvo)
else:
if self.model.repeat_kv:
qkvo = self._repeat_kv(qkvo, kind)
qkv, o = self._reorder_and_merge(qkvo)
self.model.save_split(pack_fn(qkv),
self._attn.format(idx, 'w_qkv', kind),
Expand Down
11 changes: 11 additions & 0 deletions lmdeploy/turbomind/deploy/target_model/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,17 @@ def __init__(self,
self.model_config.expert_inter_size = _pad_inter_size(
self.model_config.expert_inter_size,
self.model_config.group_size, self.tensor_para_size)

# head_num is divisble by tp but kv_head_num is not
# and tp is divisble by kv_head_num
assert self.model_config.head_num % self.tensor_para_size == 0
self.repeat_kv = 0
if (self.tensor_para_size > self.model_config.kv_head_num and
self.tensor_para_size % self.model_config.kv_head_num == 0):
self.repeat_kv = (self.tensor_para_size //
self.model_config.kv_head_num)
self.model_config.kv_head_num = self.tensor_para_size

self.model_config.verify()
assert self.model_config.kv_head_num % self.tensor_para_size == 0

Expand Down