This function has been mostly taken from huggingface conversational ai code at https://medium.com/huggingface/how-to-build-a-state-of-the-art- conversational-ai-with-transfer-learning-2d818ac26313
(logits, top_k=0, top_p=0.0, filter_value=-float("Inf"))
| 73 | |
| 74 | |
| 75 | def top_k_logits(logits, top_k=0, top_p=0.0, filter_value=-float("Inf")): |
| 76 | """This function has been mostly taken from huggingface conversational |
| 77 | ai code at |
| 78 | https://medium.com/huggingface/how-to-build-a-state-of-the-art- |
| 79 | conversational-ai-with-transfer-learning-2d818ac26313""" |
| 80 | |
| 81 | if top_k > 0: |
| 82 | # Remove all tokens with a probability less than the |
| 83 | # last token of the top-k |
| 84 | indices_to_remove = logits < torch.topk(logits, top_k)[0][..., -1, None] |
| 85 | logits[indices_to_remove] = filter_value |
| 86 | |
| 87 | if top_p > 0.0: |
| 88 | # Cconvert to 1D |
| 89 | sorted_logits, sorted_indices = torch.sort(logits, descending=True, dim=-1) |
| 90 | cumulative_probs = torch.cumsum(F.softmax(sorted_logits, dim=-1), dim=-1) |
| 91 | |
| 92 | # Remove tokens with cumulative probability above the threshold |
| 93 | sorted_indices_to_remove = cumulative_probs > top_p |
| 94 | # Shift the indices to the right to keep also the first token |
| 95 | # above the threshold |
| 96 | sorted_indices_to_remove[..., 1:] = sorted_indices_to_remove[..., :-1].clone() |
| 97 | sorted_indices_to_remove[..., 0] = 0 |
| 98 | for i in range(sorted_indices.size(0)): |
| 99 | indices_to_remove = sorted_indices[i][sorted_indices_to_remove[i]] |
| 100 | logits[i][indices_to_remove] = filter_value |
| 101 | |
| 102 | return logits |
| 103 | |
| 104 | |
| 105 | def generate_samples_input_from_file(model): |
no test coverage detected