|
@@ -269,13 +269,14 @@ class MiniMindForCausalLM(PreTrainedModel, GenerationMixin):
|
|
|
past_len = past_key_values[0][0].shape[1] if past_key_values else 0
|
|
past_len = past_key_values[0][0].shape[1] if past_key_values else 0
|
|
|
outputs = self.forward(input_ids[:, past_len:], attention_mask, past_key_values, use_cache=use_cache, **kwargs)
|
|
outputs = self.forward(input_ids[:, past_len:], attention_mask, past_key_values, use_cache=use_cache, **kwargs)
|
|
|
attention_mask = torch.cat([attention_mask, attention_mask.new_ones(attention_mask.shape[0], 1)], -1) if attention_mask is not None else None
|
|
attention_mask = torch.cat([attention_mask, attention_mask.new_ones(attention_mask.shape[0], 1)], -1) if attention_mask is not None else None
|
|
|
- logits = outputs.logits[:, -1, :] / temperature
|
|
|
|
|
|
|
+ logits = outputs.logits[:, -1, :]
|
|
|
|
|
+ if do_sample: logits = logits / temperature
|
|
|
if repetition_penalty != 1.0:
|
|
if repetition_penalty != 1.0:
|
|
|
for i in range(input_ids.shape[0]):
|
|
for i in range(input_ids.shape[0]):
|
|
|
seen = torch.unique(input_ids[i]); score = logits[i, seen]; logits[i, seen] = torch.where(score > 0, score / repetition_penalty, score * repetition_penalty)
|
|
seen = torch.unique(input_ids[i]); score = logits[i, seen]; logits[i, seen] = torch.where(score > 0, score / repetition_penalty, score * repetition_penalty)
|
|
|
- if top_k > 0:
|
|
|
|
|
|
|
+ if do_sample and top_k > 0:
|
|
|
logits[logits < torch.topk(logits, top_k)[0][..., -1, None]] = -float('inf')
|
|
logits[logits < torch.topk(logits, top_k)[0][..., -1, None]] = -float('inf')
|
|
|
- if top_p < 1.0:
|
|
|
|
|
|
|
+ if do_sample and top_p < 1.0:
|
|
|
sorted_logits, sorted_indices = torch.sort(logits, descending=True)
|
|
sorted_logits, sorted_indices = torch.sort(logits, descending=True)
|
|
|
mask = torch.cumsum(torch.softmax(sorted_logits, dim=-1), dim=-1) > top_p
|
|
mask = torch.cumsum(torch.softmax(sorted_logits, dim=-1), dim=-1) > top_p
|
|
|
mask[..., 1:], mask[..., 0] = mask[..., :-1].clone(), 0
|
|
mask[..., 1:], mask[..., 0] = mask[..., :-1].clone(), 0
|