class_docstr: GPT Language Model
docstr: Take a conditioning sequence of indices idx (LongTensor of shape (b,t)) and complete
  the sequence max_new_tokens times, feeding the predictions back into the model each time.
  Most likely you'll want to make sure to be in model.eval() mode of operation for this.
docstr_tok: ["Take","a","conditioning","sequence","of","indices","idx","(","LongTensor","of","shape","(","b",",","t",")",")","and","complete","the","sequence","max_new_tokens","times",",","feeding","the","predictions","back","into","the","model","each","time",".","Most","likely","you","'ll","want","to","make","sure","to","be","in","model.eval","(",")","mode","of","operation","for","this","."]
code: def generate(
      self,
      idx: torch.Tensor,
      max_new_tokens: int,
      temperature: float = 1.0,
      do_sample: bool = False,
      top_k: Optional[int] = None,
  ) -> torch.Tensor:
      """
      Take a conditioning sequence of indices idx (LongTensor of shape (b,t)) and complete
      the sequence max_new_tokens times, feeding the predictions back into the model each time.
      Most likely you'll want to make sure to be in model.eval() mode of operation for this.
      """
      for _ in range(max_new_tokens):
          # if the sequence context is growing too long we must crop it at block_size
          idx_cond = (
              idx
              if idx.size(1) <= self.block_size
              else idx[:, -self.block_size :]
          )
          # forward the model to get the logits for the index in the sequence
          logits, _ = self(idx_cond)
          # pluck the logits at the final step and scale by desired temperature
          logits = logits[:, -1, :] / temperature
          # optionally crop the logits to only the top k options
          if top_k is not None:
              v, _ = torch.topk(logits, top_k)
              logits[logits < v[:, [-1]]] = -float("Inf")
          # apply softmax to convert logits to (normalized) probabilities
          probs = F.softmax(logits, dim=-1)
          # either sample from the distribution or take the most likely element
          if do_sample:
              idx_next = torch.multinomial(probs, num_samples=1)
          else:
              _, idx_next = torch.topk(probs, k=1, dim=-1)
          # append sampled index to the running sequence and continue
          idx = torch.cat((idx, idx_next), dim=1)
  
      return idx
  
imports: import math
  from dataclasses import dataclass
  from typing import Optional, Tuple
  import torch
  import torch.nn
  from torch.nn import functional
label_desc: Use image node_id 5 for calling the GPT obj's underlying member method code with example usage: obj.generate(idx, max_new_tokens, temperature, do_sample, top_k) and returns: idx