AxionLab-official commited on
Commit
321d5f9
verified
1 Parent(s): 2b2f300

Update modeling_axion.py

Browse files
Files changed (1) hide show
  1. modeling_axion.py +15 -1
modeling_axion.py CHANGED
@@ -69,6 +69,19 @@ class DeepSeekNanoForCausalLM(PreTrainedModel):
69
  labels=None, use_cache=False, **kwargs):
70
  x = self.embed(input_ids)
71
  new_caches = [] if use_cache else None
 
 
 
 
 
 
 
 
 
 
 
 
 
72
  for i, block in enumerate(self.blocks):
73
  cache = past_key_values[i] if past_key_values else None
74
  x, nc = block(x, kv_cache=cache, use_cache=use_cache)
@@ -82,6 +95,7 @@ class DeepSeekNanoForCausalLM(PreTrainedModel):
82
  return CausalLMOutputWithPast(loss=loss, logits=logits, past_key_values=new_caches)
83
 
84
  def prepare_inputs_for_generation(self, input_ids, past_key_values=None, **kwargs):
85
- if past_key_values:
 
86
  input_ids = input_ids[:, -1:]
87
  return {"input_ids": input_ids, "past_key_values": past_key_values, "use_cache": True}
 
69
  labels=None, use_cache=False, **kwargs):
70
  x = self.embed(input_ids)
71
  new_caches = [] if use_cache else None
72
+
73
+ # Compatibilidade com DynamicCache (Transformers >= 4.36)
74
+ # Converte para lista simples que o nosso MLA entende
75
+ if past_key_values is not None and not isinstance(past_key_values, list):
76
+ try:
77
+ past_key_values = [
78
+ (past_key_values.key_cache[i], past_key_values.value_cache[i])
79
+ if i < len(past_key_values.key_cache) else None
80
+ for i in range(len(self.blocks))
81
+ ]
82
+ except Exception:
83
+ past_key_values = None
84
+
85
  for i, block in enumerate(self.blocks):
86
  cache = past_key_values[i] if past_key_values else None
87
  x, nc = block(x, kv_cache=cache, use_cache=use_cache)
 
95
  return CausalLMOutputWithPast(loss=loss, logits=logits, past_key_values=new_caches)
96
 
97
  def prepare_inputs_for_generation(self, input_ids, past_key_values=None, **kwargs):
98
+ # Se tem cache (qualquer tipo), usa s贸 o 煤ltimo token
99
+ if past_key_values is not None:
100
  input_ids = input_ids[:, -1:]
101
  return {"input_ids": input_ids, "past_key_values": past_key_values, "use_cache": True}