code: def _collate_batch(
      samples, tokenizer, pad_to_multiple_of: Optional[int] = None
  ):
      """Collate `samples` into a batch, using the information in `tokenizer` for padding if necessary."""
      # Tensorize if necessary.
      if isinstance(samples[0], (list, tuple)):
          samples = [torch.tensor(e, dtype=torch.long) for e in samples]
  
      # Check if padding is necessary.
      length_of_first = samples[0].size(0)
      are_tensors_same_length = all(
          x.size(0) == length_of_first for x in samples
      )
      if are_tensors_same_length and (
          pad_to_multiple_of is None
          or length_of_first % pad_to_multiple_of == 0
      ):
          return torch.stack(samples, dim=0)
  
      # If yes, check if we have a `pad_token`.
      if tokenizer._pad_token is None:
          raise ValueError(
              "You are attempting to pad samples but the tokenizer you are using"
              f" ({tokenizer.__class__.__name__}) does not have a pad token."
          )
  
      # Creating the full tensor and filling it with our data.
      max_length = max(x.size(0) for x in samples)
      if pad_to_multiple_of is not None and (
          max_length % pad_to_multiple_of != 0
      ):
          max_length = (
              (max_length // pad_to_multiple_of) + 1
          ) * pad_to_multiple_of
      result = samples[0].new_full(
          [len(samples), max_length], tokenizer.pad_token_id
      )
      for i, example in enumerate(samples):
          if tokenizer.padding_side == "right":
              result[i, : example.shape[0]] = example
          else:
              result[i, -example.shape[0] :] = example
      return result
  
imports: from typing import List, Optional
  import itertools, os
  import torch
  from torch.nn.utils.rnn import pad_sequence
label_desc: Use image node_id 3 for calling a global function with example usage: _collate_batch(samples, tokenizer, pad_to_multiple_of) and returns: result, torch