docstr: Calculate total tokens and truncate multiple contexts in truncation_fields.
  
  Args:
      template_ids (List[List[int]]): the list of separate prompt_template ids.
      template_ids_keys (List[str]): the list of placeholder keys or <template> (used to check key in truncation_fields).
  
  Returns:
      context_ids (List[int]): all context ids.
      label_ids (List[int]): all label ids.
docstr_tok: ["Calculate","total","tokens","and","truncate","multiple","contexts","in","truncation_fields",".","Args",":","template_ids","(","List","[","List","[","int","]","]",")",":","the","list","of","separate","prompt_template","ids",".","template_ids_keys","(","List","[","str","]",")",":","the","list","of","placeholder","keys","or","<","template",">","(","used","to","check","key","in","truncation_fields",")",".","Returns",":","context_ids","(","List","[","int","]",")",":","all","context","ids",".","label_ids","(","List","[","int","]",")",":","all","label","ids","."]
code: def _multiple_truncation(
      self, template_ids: List[List[int]], template_ids_keys: List[str]
  ):
      """
      Calculate total tokens and truncate multiple contexts in truncation_fields.
  
      Args:
          template_ids (List[List[int]]): the list of separate prompt_template ids.
          template_ids_keys (List[str]): the list of placeholder keys or <template> (used to check key in truncation_fields).
  
      Returns:
          context_ids (List[int]): all context ids.
          label_ids (List[int]): all label ids.
      """
      context_ids = template_ids[:-1]
      label_ids = template_ids[-1]
      total_ids = (
          self.virtual_tokens
          + sum(len(ids) for ids in context_ids)
          + max(len(label_ids), self.tokens_to_generate)
          + self.add_bos
          + self.add_sep
          + self.add_eos  # Only training need to consider eos token
      )
  
      if total_ids > self.max_seq_length:
          truncation_length_total = total_ids - self.max_seq_length
          num_fields = len(self.truncation_fields)
          # sorted equal divide length to each field
          # examples:
          #   truncation_length_total = 3
          #   num_fields = 11
          #   truncation_length_list = [3,4,4]
          truncation_length_list = [
              truncation_length_total // num_fields
              + (1 if i < truncation_length_total % num_fields else 0)
              for i in range(num_fields)[::-1]
          ]
  
          for i, (ids, key) in enumerate(
              zip(template_ids, template_ids_keys)
          ):
              if key in self.truncation_fields:
                  truncation_length = truncation_length_list.pop()
                  if len(ids) < truncation_length:
                      logging.warning(
                          f"{key} is not long enough to truncate."
                      )
                      truncation_length = len(ids)
  
                  if self.truncation_method == "left":
                      window_offset = truncation_length
                  elif self.truncation_method == "right":
                      window_offset = 0
                  else:
                      raise ValueError(
                          f"{self.truncation_method} is not supported"
                      )
  
                  window_length = len(ids) - truncation_length
                  template_ids[i] = ids[
                      window_offset : window_offset + window_length
                  ]
  
      context_ids = [i for ids in template_ids[:-1] for i in ids]
      label_ids = template_ids[-1]
      return context_ids, label_ids
  
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 GPTSFTDataset obj's underlying member method code with example usage: obj._multiple_truncation(template_ids, template_ids_keys) and returns: context_ids,  label_ids