code: def collate_fn(self, batch):
      input_ids = [
          np.concatenate(
              [
                  item["input_ids"][
                      item["seq_boundaries"][i] : item[
                          "seq_boundaries"
                      ][i + 1]
                      - 1
                  ]
                  for i in range(len(item["seq_boundaries"]) - 1)
              ]
          )
          for item in batch
      ]
      labels = [
          np.concatenate(
              [
                  item["input_ids"][
                      item["seq_boundaries"][i]
                      + 1 : item["seq_boundaries"][i + 1]
                  ]
                  for i in range(len(item["seq_boundaries"]) - 1)
              ]
          )
          for item in batch
      ]
  
      loss_mask = [self._build_loss_mask(item) for item in batch]
  
      token_count = [item.shape[0] for item in input_ids]
  
      if self.pad_to_max_length:
          max_length = self.max_seq_length
      else:
          # pad to the nearest multiple of 16 for FP8 training
          # for many datasets in practice, all packed sequence lengths are very close to the
          # target length (2048, 4096, 8192), so there is very minimal padding
          max_length = max(len(l) for l in input_ids)
          max_length = min(
              self.max_seq_length, self._ceil_to_nearest(max_length, 16)
          )
      assert max_length <= self.max_seq_length
  
      position_ids: List[List[int]] = []
      cu_seqlens: List[List[int]] = []
      for item in batch:
          position_ids.append([])
          cu_seqlens.append([0])
          seqlens = np.array(item["seq_boundaries"][1:]) - np.array(
              item["seq_boundaries"][:-1]
          )
          for l in seqlens:
              # length minus 1 because input_ids is truncated by 1 for labels
              position_ids[-1].extend(list(range(l - 1)))
              cu_seqlens[-1].append(cu_seqlens[-1][-1] + l - 1)
          # set last seq to the max seq len because rope and attn kernels expect no padding
          cu_seqlens[-1][-1] = max_length
  
      assert len(input_ids[0]) == len(
          position_ids[0]
      ), "Dataset problem: input_ids and position_ids lengths don't match"
  
      cu_seqlens = self._collate_item(
          cu_seqlens,
          max_length=max(len(l) for l in cu_seqlens) + 1,
          pad_id=-1,
      )
      input_ids = self._collate_item(
          input_ids, max_length=max_length, pad_id=self.tokenizer.eos_id
      )
      labels = self._collate_item(
          labels, max_length=max_length, pad_id=self.tokenizer.eos_id
      )
      loss_mask = self._collate_item(
          loss_mask, max_length=max_length, pad_id=0
      )
      position_ids = self._collate_item(
          position_ids, max_length=max_length, pad_id=0
      )
  
      processed_batch = {
          "tokens": torch.LongTensor(input_ids),
          "labels": torch.LongTensor(labels),
          "attention_mask": torch.LongTensor(
              [1] * len(input_ids)
          ),  # no attention mask is needed for packed seq
          "loss_mask": torch.LongTensor(loss_mask),
          "position_ids": torch.LongTensor(position_ids),
          "cu_seqlens": torch.IntTensor(
              cu_seqlens
          ),  # cu_seqlens_q must be in dtype torch.int32
          "token_count": token_count,
      }
  
      return processed_batch
  
imports: import re
  from typing import List, Mapping, Optional
  import datasets
  import numpy
  import torch
  from datasets import load_dataset
  from nemo.collections.common.tokenizers.tokenizer_spec import TokenizerSpec
  from nemo.collections.nlp.data.language_modeling.megatron.dataset_utils import get_samples_mapping
  from nemo.collections.nlp.data.language_modeling.text_memmap_dataset import JSONLMemMapDataset
  from nemo.core.classes import Dataset
  from nemo.utils import logging
label_desc: Use image node_id 7 for calling the GPTSFTPackedDataset obj's underlying member method code with example usage: obj.collate_fn(batch) and returns: processed_batch