Skip to content
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
11 changes: 9 additions & 2 deletions optimum/habana/transformers/models/llama/modeling_llama.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,13 @@ def get_k_proj_weight(self):
return self.k_proj.qweight
return self.k_proj.weight

def get_k_proj_weight_dtype(self):
""" 4bit quantization in GPTQ replaces the k_proj.weight with qweight.
Scales tensor gets the weight dtype. """
if hasattr(self.k_proj, 'qweight'):
return self.k_proj.scales.dtype
return self.k_proj.weight.dtype

def allocate_kv_cache(self, batch_size, max_seq_len, inp_seq_len):
cache_shape = (batch_size, self.num_key_value_heads, max_seq_len, self.head_dim)
device = self.get_k_proj_weight().device
Expand Down Expand Up @@ -418,9 +425,9 @@ def pre_attn_forward(
past_key_value = (self.k_cache.get_shape(), self.v_cache.get_shape())
else:
if past_key_value is None:
past_key = torch.zeros(key_states.shape, dtype=self.k_proj.weight.dtype, device=key_states.device)
past_key = torch.zeros(key_states.shape, dtype=self.get_k_proj_weight_dtype(), device=key_states.device)
past_value = torch.zeros(
key_states.shape, dtype=self.k_proj.weight.dtype, device=key_states.device
key_states.shape, dtype=self.get_k_proj_weight_dtype(), device=key_states.device
)
# Return list instead of tuple
past_key_value = [past_key, past_value]
Expand Down