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

Support num_key_value_heads #447

Merged
merged 2 commits into from
Oct 13, 2023
Merged
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
9 changes: 6 additions & 3 deletions optimum/intel/generation/modeling.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,9 @@ def forward(
nb_pkv = 2
num_layers = self.normalized_config.num_layers
num_attention_heads = self.normalized_config.num_attention_heads
num_key_value_heads = num_attention_heads
if hasattr(self.normalized_config, "num_key_value_heads"):
num_key_value_heads = self.normalized_config.num_key_value_heads
hidden_size = self.normalized_config.hidden_size
d_k = hidden_size // num_attention_heads
if self.config.model_type == "gpt_bigcode":
Expand All @@ -282,7 +285,7 @@ def forward(
empty_tensor = empty_tensor.to(self.model_dtype)
past_key_values = tuple([empty_tensor] * num_layers)
elif self.config.model_type != "bloom":
new_shape = [input_ids.shape[0], num_attention_heads, 0, d_k]
new_shape = [input_ids.shape[0], num_key_value_heads, 0, d_k]
empty_tensor = torch.empty(size=new_shape)
if self.model_dtype is not None:
empty_tensor = empty_tensor.to(self.model_dtype)
Expand All @@ -291,9 +294,9 @@ def forward(
pkv = ()
for nb_pkv in range(nb_pkv):
if nb_pkv % 2 == 0:
new_shape = [input_ids.shape[0] * num_attention_heads, d_k, 0]
new_shape = [input_ids.shape[0] * num_key_value_heads, d_k, 0]
else:
new_shape = [input_ids.shape[0] * num_attention_heads, 0, d_k]
new_shape = [input_ids.shape[0] * num_key_value_heads, 0, d_k]
empty_tensor = torch.empty(size=new_shape)
if self.model_dtype is not None:
empty_tensor = empty_tensor.to(self.model_dtype)
Expand Down
Loading