code: def _apply_penalties(
      logits: torch.Tensor,
      output_tokens: List[List[int]],
      presence_penalties: List[float],
      frequency_penalties: List[float],
  ) -> torch.Tensor:
      num_seqs, vocab_size = logits.shape
      for i in range(num_seqs):
          if not output_tokens[i]:
              continue
          p = presence_penalties[i]
          f = frequency_penalties[i]
          if abs(p) < _SAMPLING_EPS and abs(f) < _SAMPLING_EPS:
              continue
          break
      else:
          # Return early if all sequences have zero penalties.
          return logits
  
      max_output_len = max(len(tokens) for tokens in output_tokens)
      padded_output_tokens = [
          tokens + [vocab_size] * (max_output_len - len(tokens))
          for tokens in output_tokens
      ]
      output_tokens_tensor = torch.tensor(
          padded_output_tokens, dtype=torch.long, device=logits.device
      )
  
      # Compute the bin counts for the output tokens.
      # vocab_size + 1 for padding.
      bin_counts = torch.zeros(
          (num_seqs, vocab_size + 1),
          dtype=torch.long,
          device=logits.device,
      )
      bin_counts.scatter_add_(
          1, output_tokens_tensor, torch.ones_like(output_tokens_tensor)
      )
      bin_counts = bin_counts[:, :vocab_size]  # Remove the padding bin.
  
      frequency_penalties = torch.tensor(
          frequency_penalties, dtype=logits.dtype, device=logits.device
      )
      presence_penalties = torch.tensor(
          presence_penalties, dtype=logits.dtype, device=logits.device
      )
  
      # We follow the definition in OpenAI API.
      # Refer to https://platform.openai.com/docs/api-reference/parameter-details
      logits -= frequency_penalties.unsqueeze(dim=1) * bin_counts
      logits -= presence_penalties.unsqueeze(dim=1) * (bin_counts > 0)
      return logits
  
imports: from typing import Dict, List, Optional, Tuple
  import torch
  import torch.nn
  from bigdl.llm.vllm.model_executor.input_metadata import InputMetadata
  from bigdl.llm.vllm.sampling_params import SamplingParams, SamplingType
  from bigdl.llm.vllm.sequence import SamplerOutput, SequenceGroupMetadata, SequenceData, SequenceOutputs
  import time
label_desc: Use image node_id 4 for calling a global function with example usage: _apply_penalties(logits, output_tokens, presence_penalties, frequency_penalties) and returns: logits, logits