ThingsAI commited on
Commit
91da9fd
·
verified ·
1 Parent(s): e0d9139

fix: NaN guard in generate_text + _keys_to_ignore_on_load_missing

Browse files
Files changed (1) hide show
  1. modeling_quark.py +21 -9
modeling_quark.py CHANGED
@@ -129,6 +129,8 @@ class QuarkPreTrainedModel(PreTrainedModel):
129
 
130
 
131
  class QuarkForCausalLM(QuarkPreTrainedModel):
 
 
132
  """
133
  Quark autoregressive language model.
134
  Compatibile con AutoModelForCausalLM.from_pretrained(..., trust_remote_code=True)
@@ -192,17 +194,27 @@ class QuarkForCausalLM(QuarkPreTrainedModel):
192
  for _ in range(max_new_tokens):
193
  out = self(ctx[:, -self.config.max_seq_len:])
194
  logits = out.logits[0, -1, :].float()
195
- if temperature > 0:
196
- logits /= temperature
197
- probs = F.softmax(logits, dim=-1)
 
 
 
 
 
 
 
198
  sorted_p, sorted_i = torch.sort(probs, descending=True)
199
- cum_p = torch.cumsum(sorted_p, dim=-1)
200
- remove = cum_p - sorted_p > top_p
 
201
  sorted_p[remove] = 0.0
202
- sorted_p /= sorted_p.sum()
203
- token = sorted_i[torch.multinomial(sorted_p, 1)].unsqueeze(0).unsqueeze(0)
204
- else:
205
- token = logits.argmax().view(1, 1)
 
 
206
  ctx = torch.cat([ctx, token], dim=1)
207
  if eos_token_id is not None and token.item() == eos_token_id:
208
  break
 
129
 
130
 
131
  class QuarkForCausalLM(QuarkPreTrainedModel):
132
+ # lm_head è tied con embed_tokens — non è missing, è intenzionale
133
+ _keys_to_ignore_on_load_missing = ["lm_head.weight"]
134
  """
135
  Quark autoregressive language model.
136
  Compatibile con AutoModelForCausalLM.from_pretrained(..., trust_remote_code=True)
 
194
  for _ in range(max_new_tokens):
195
  out = self(ctx[:, -self.config.max_seq_len:])
196
  logits = out.logits[0, -1, :].float()
197
+ # Greedy se temperature=0 o logits degeneri
198
+ if temperature <= 0 or torch.isnan(logits).any() or torch.isinf(logits).any():
199
+ token = logits.argmax().view(1, 1)
200
+ else:
201
+ logits = logits / temperature
202
+ # Stabilizza prima del softmax
203
+ logits = logits - logits.max()
204
+ probs = F.softmax(logits, dim=-1)
205
+ probs = torch.clamp(probs, min=0.0)
206
+ # Top-p nucleus
207
  sorted_p, sorted_i = torch.sort(probs, descending=True)
208
+ cum_p = torch.cumsum(sorted_p, dim=-1)
209
+ remove = (cum_p - sorted_p) > top_p
210
+ sorted_p = sorted_p.clone()
211
  sorted_p[remove] = 0.0
212
+ total = sorted_p.sum()
213
+ if total <= 0:
214
+ token = sorted_i[0].view(1, 1)
215
+ else:
216
+ sorted_p /= total
217
+ token = sorted_i[torch.multinomial(sorted_p, 1)].unsqueeze(0).unsqueeze(0)
218
  ctx = torch.cat([ctx, token], dim=1)
219
  if eos_token_id is not None and token.item() == eos_token_id:
220
  break