code: def apply_repetition_penalty(
      logits,
      batch_size,
      num_beams,
      prev_output_tokens,
      repetition_penalty,
      start_idx=None,
      end_idx=None,
      window_size=None,
  ):
      # only conduct repetition penalty for the output
      assert (
          repetition_penalty >= 1
      ), "repetition penalty coefficient should >= 1"
      # repetition penalty (from CTRL paper https://arxiv.org/abs/1909.05858)
      for i in range(batch_size * num_beams):
          if start_idx is None or end_idx is None:
              output_tokens = prev_output_tokens[i].tolist()
          else:
              if end_idx >= start_idx:
                  if window_size:
                      output_tokens = prev_output_tokens[i][
                          max(
                              start_idx, end_idx + 1 - window_size
                          ) : end_idx
                          + 1
                      ].tolist()
                  else:
                      output_tokens = prev_output_tokens[i][
                          start_idx : end_idx + 1
                      ].tolist()
              else:
                  output_tokens = []
          for previous_token in set(output_tokens):
              # if score < 0 then repetition penalty has to
              # multiplied to reduce the previous token probability
              if logits[i, previous_token] < 0:
                  logits[i, previous_token] *= repetition_penalty
              else:
                  logits[i, previous_token] /= repetition_penalty
  
file_path: DeepKE/example/llm/CPM-Bee/src/cpm_live/generation/generation_utils.py
imports: import torch
  import torch.nn.functional
label_desc: Use image node_id 2 for calling a global function with example usage: apply_repetition_penalty(logits, batch_size, num_beams, prev_output_tokens, repetition_penalty, start_idx, end_idx, window_size) without return types