class_docstr: Collate examples for supervised fine-tuning.
code: def __call__(
      self, instances: Sequence[Dict]
  ) -> Dict[str, torch.Tensor]:
      max_len = max(
          instance["tokens"].shape[0] for instance in instances
      )
      max_len = (max_len - 1) // 4 * 4 + 4
      for instance in instances:
          pad_len = max_len - instance["tokens"].shape[0]
          instance["tokens"] = F.pad(
              instance["tokens"], (0, pad_len), "constant", 0
          )
          instance["labels"] = F.pad(
              instance["labels"], (0, pad_len), "constant", -1
          )
  
      batch = default_collate(instances)
      tokenizer = self.tokenizer
      model_cfg = self.model_cfg
  
      tokens = batch["tokens"]
      labels = batch["labels"]
      media = batch.get("image")
  
      (
          attention_mask,
          loss_mask,
          position_ids,
      ) = get_ltor_masks_and_position_ids(
          data=tokens,
          eod_token=tokenizer.eos_id,
          eod_mask_loss=model_cfg.data.get("eod_mask_loss", False),
          reset_attention_mask=False,
          reset_position_ids=False,
      )
  
      loss_mask[labels == -1] = 0.0
      tokens[tokens == -1] = 0
      labels[labels == -1] = 0
  
      if media is None:
          raise NotImplementedError
      else:
          media = rearrange(media, "b T c h w -> b T 1 c h w")
  
      batch = {
          "tokens": tokens,
          "labels": labels,
          "attention_mask": attention_mask,
          "loss_mask": loss_mask,
          "position_ids": position_ids,
          "media": media,
      }
      return batch
  
imports: import copy
  import json
  import logging
  import os
  import re
  import tarfile
  from dataclasses import dataclass
  from typing import Any, Dict, List, Sequence, Union
  import torch
  import torch.nn.functional
  import transformers
  from einops import rearrange
  from omegaconf import DictConfig
  from PIL import Image
  from torch.utils.data import Dataset, default_collate
  from transformers import CLIPImageProcessor
  import nemo.collections.multimodal.data.neva.conversation
  from nemo.collections.nlp.modules.common.megatron.utils import get_ltor_masks_and_position_ids
label_desc: Use image node_id 1 for calling the DataCollatorForSupervisedDataset obj's underlying member method code with example usage: obj.__call__(instances) and returns: batch