Takes a conditioning sequence (prompt) as input and continues to generate as many tokens as requested. The implementation of this function is modified from A. Karpathy's nanoGPT. Args: model: The model to use. prompt: Tensor of shape (T) with indices of the prompt seque
(
fabric: Fabric,
model: GPT,
prompt: torch.Tensor,
max_returned_tokens: int,
*,
temperature: float = 1.0,
top_k: Optional[int] = None,
top_p: float = 1.0,
eos_id: Optional[int] = None,
include_prompt: bool = True,
)
| 66 | |
| 67 | @torch.inference_mode() |
| 68 | def generate( |
| 69 | fabric: Fabric, |
| 70 | model: GPT, |
| 71 | prompt: torch.Tensor, |
| 72 | max_returned_tokens: int, |
| 73 | *, |
| 74 | temperature: float = 1.0, |
| 75 | top_k: Optional[int] = None, |
| 76 | top_p: float = 1.0, |
| 77 | eos_id: Optional[int] = None, |
| 78 | include_prompt: bool = True, |
| 79 | ) -> torch.Tensor: |
| 80 | """ |
| 81 | Takes a conditioning sequence (prompt) as input and continues to generate as many tokens as requested. |
| 82 | The implementation of this function is modified from A. Karpathy's nanoGPT. |
| 83 | |
| 84 | Args: |
| 85 | model: The model to use. |
| 86 | prompt: Tensor of shape (T) with indices of the prompt sequence. |
| 87 | max_returned_tokens: The maximum number of tokens to return (given plus generated). |
| 88 | temperature: Scales the predicted logits by 1 / temperature. |
| 89 | top_k: If specified, only sample among the tokens with the k highest probabilities. |
| 90 | top_p: If specified, it represents the cumulative probability threshold to consider in the sampling process. |
| 91 | In top-p sampling, the next token is sampled from the highest probability tokens |
| 92 | whose cumulative probability exceeds the threshold `top_p`. When specified, |
| 93 | it must be `0 <= top_p <= 1`. Here, `top_p=0` is equivalent |
| 94 | to sampling the most probable token, while `top_p=1` samples from the whole distribution. |
| 95 | It can be used in conjunction with `top_k` and `temperature` with the following order |
| 96 | of application: |
| 97 | |
| 98 | 1. `top_k` sampling |
| 99 | 2. `temperature` scaling |
| 100 | 3. `top_p` sampling |
| 101 | |
| 102 | For more details, see https://arxiv.org/abs/1904.09751 |
| 103 | or https://huyenchip.com/2024/01/16/sampling.html#top_p |
| 104 | eos_id: If specified, stop generating any more token once the <eos> token is triggered. |
| 105 | include_prompt: If true (default) prepends the prompt (after applying the prompt style) to the output. |
| 106 | """ |
| 107 | T = prompt.size(0) |
| 108 | assert max_returned_tokens > T |
| 109 | if model.max_seq_length < max_returned_tokens - 1: |
| 110 | # rolling the kv cache based on the `input_pos` value would be necessary. However, doing so would introduce a |
| 111 | # data dependency on the `input_pos` tensor and impact model compilation. Since this setting is uncommon, we do |
| 112 | # not support it to avoid negatively impacting the overall speed |
| 113 | raise NotImplementedError( |
| 114 | f'max_seq_length {model.max_seq_length} needs to be >= {max_returned_tokens - 1}' |
| 115 | ) |
| 116 | |
| 117 | model.reset_cache() |
| 118 | # set the max_seq_length to limit the memory usage to what we need |
| 119 | model.max_seq_length = max_returned_tokens |
| 120 | |
| 121 | device = prompt.device |
| 122 | if include_prompt: |
| 123 | tokens = [prompt] |
| 124 | else: |
| 125 | tokens = [] |
no test coverage detected