Skip to content

Commit

Permalink
Fix lm_eval issue of llama (#1606)
Browse files Browse the repository at this point in the history
Signed-off-by: Wang, Yi A <[email protected]>
  • Loading branch information
sywangyi authored Jan 27, 2025
1 parent 1e9bd35 commit 3c251d0
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions optimum/habana/transformers/models/llama/modeling_llama.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,15 +404,18 @@ def allocate(self, inp_seq_len, dtype, device, shape):

@staticmethod
def update(prev, cur, dim, idx, inp_seq_len):
orig_cur = cur
if prev.shape == cur.shape:
prev.copy_(cur)
return orig_cur
if idx is not None and cur.shape[2] > 1 and cur.shape[2] <= prev.shape[2]:
# Initialize
prev[:, :, :inp_seq_len, :].copy_(cur)
return orig_cur
if inp_seq_len != -1:
# reuse cache logic
orig_cur = cur
if prev.shape == cur.shape:
prev.copy_(cur)
return orig_cur
if cur.shape[2] > 1 and cur.shape[2] <= prev.shape[2]:
# Initialize
prev[:, :, :inp_seq_len, :].copy_(cur)
return orig_cur
if idx is not None:
# 2+ tokenizer logic if model is static shape optimized
prev.index_copy_(dim, idx - 1, cur)
return prev
else:
Expand Down Expand Up @@ -657,9 +660,11 @@ def pre_attn_forward(
past_value = torch.zeros(
key_states.shape, dtype=self.get_k_proj_weight_dtype(), device=key_states.device
)
past_key.copy_(key_states)
past_value.copy_(value_states)
# Return list instead of tuple
past_key_value = [past_key, past_value]
if (
elif (
token_idx is not None
and num_virtual_tokens is not None
and num_virtual_tokens == past_key_value[0].shape[-2]
Expand Down

0 comments on commit 3c251d0

Please sign in to comment.