docstr: Top-k and top-p sampling.
  Args:
      model_inputs (dict): input ids
      generate_length (int, optional, defaults to 100): maximum generation length
      top_k (int, optional, defaults to 0): keep only top k tokens with highest probability. 0 means keeping all tokens.
      top_p (int, optional, defaults to 0.9): keep the top tokens with cumulative probability >= top_p.
      temperature (int, optional, defaults to 0.9): the value that can cool down the logits distribution.
      repetition_penalty (float, optional, defaults to 1.0): repetition penalty coefficient, 1.0 means no penalty.
      repetition_window (int, optional, defaults to None): window size of repetition penalty, None means that all output tokens are penalized.
docstr_tok: ["Top-k","and","top-p","sampling",".","Args",":","model_inputs","(","dict",")",":","input","ids","generate_length","(","int",",","optional",",","defaults","to","100",")",":","maximum","generation","length","top_k","(","int",",","optional",",","defaults","to","0",")",":","keep","only","top","k","tokens","with","highest","probability",".","0","means","keeping","all","tokens",".","top_p","(","int",",","optional",",","defaults","to","0.9",")",":","keep","the","top","tokens","with","cumulative","probability",">","=","top_p",".","temperature","(","int",",","optional",",","defaults","to","0.9",")",":","the","value","that","can","cool","down","the","logits","distribution",".","repetition_penalty","(","float",",","optional",",","defaults","to","1.0",")",":","repetition","penalty","coefficient",",","1.0","means","no","penalty",".","repetition_window","(","int",",","optional",",","defaults","to","None",")",":","window","size","of","repetition","penalty",",","None","means","that","all","output","tokens","are","penalized","."]
code: def _decode(
      self,
      model_inputs,
      max_length=100,
      top_k=0,
      top_p=0.9,
      temperature=0.9,
      repetition_penalty=1.0,
      repetition_window=None,
      **kwargs,
  ):
      """
      Top-k and top-p sampling.
      Args:
          model_inputs (dict): input ids
          generate_length (int, optional, defaults to 100): maximum generation length
          top_k (int, optional, defaults to 0): keep only top k tokens with highest probability. 0 means keeping all tokens.
          top_p (int, optional, defaults to 0.9): keep the top tokens with cumulative probability >= top_p.
          temperature (int, optional, defaults to 0.9): the value that can cool down the logits distribution.
          repetition_penalty (float, optional, defaults to 1.0): repetition penalty coefficient, 1.0 means no penalty.
          repetition_window (int, optional, defaults to None): window size of repetition penalty, None means that all output tokens are penalized.
      """  # noqa: E501
      # generate_length + 1 for EOS token
      max_length += 1
  
      input = model_inputs["input"]
      length = model_inputs["length"]
      context = model_inputs["context"]
      position = model_inputs["position"]
      segment = model_inputs["segment"]
      span = model_inputs["span"]
      batch_size = input.size(0)
  
      pred_start_index = input.size(-1)
      past_key_values = None
      done = [False for _ in range(batch_size)]
      results = [None for _ in range(batch_size)]
      for i in range(max_length):
          if i == 0:
              logits, _, past_key_values = self.model.inference(
                  input=input,
                  length=length,
                  context=context,
                  position=position,
                  segment=segment,
                  span=span,
                  past_key_values=past_key_values,
              )
          else:
              logits, _, past_key_values = self.model.inference(
                  input=input[:, -1:],
                  length=length,
                  context=context,
                  position=position,
                  segment=segment,
                  span=span,
                  past_key_values=past_key_values,
              )
  
          logits = logits[:, -1, :]
  
          if i == 0:
              logits[:, self.tokenizer.eos_id] = -float("inf")
              logits[:, self.tokenizer.newline_id] = -float("inf")
  
          apply_repetition_penalty(
              logits,
              batch_size,
              1,
              input,
              repetition_penalty,
              pred_start_index,
              input.size(-1) - 1,
              repetition_window,
          )
  
          logits = logits / temperature
          logits = top_k_top_p_filtering(
              logits, top_k=top_k, top_p=top_p
          )
  
          probs = F.softmax(logits, dim=-1)
          next_token = torch.multinomial(probs, num_samples=1)
  
          for idx in range(batch_size):
              if not done[idx] and (
                  next_token[idx].item() == self.tokenizer.eos_id
                  or i == max_length - 1
              ):
                  done[idx] = True
                  results[idx] = input[idx, pred_start_index:].clone().cpu().tolist()  # type: ignore # noqa: E501
  
          if sum(done) == batch_size:
              break
  
          # update input ids
          input = torch.cat([input, next_token], dim=-1)
          length += 1
          context = torch.cat(
              [
                  context,
                  torch.ones(
                      (context.size(0), 1),
                      dtype=torch.int,
                      device=context.device,
                  ),
              ],
              dim=-1,
          )
          position = torch.cat([position, position[:, -1:] + 1], dim=-1)
          segment = torch.cat(
              [segment, segment[:, -1:]], dim=-1
          )  # segment id always the same as the previous token
          span = torch.cat([span, span[:, -1:]], dim=-1)
  
      result_text = list(map(self.tokenizer.decode, results))
      return result_text
  
file_path: DeepKE/example/llm/CPM-Bee/src/cpm_live/generation/ant.py
imports: import torch
  import torch.nn.functional
  from .generation_utils import BeamHypotheses, apply_repetition_penalty, top_k_top_p_filtering
  from ..utils import pad
label_desc: Use image node_id 1 for calling the CPMAntRandomSampling obj's underlying member method code with example usage: obj._decode(model_inputs, max_length, top_k, top_p, temperature, repetition_penalty, repetition_window) and returns: result_text