class_docstr:     Data collator used for completion tasks. It ensures that all the tokens of the labels are set to an 'ignore_index'
      when they do not come from the assistant. This ensure that the loss is only
      calculated on the completion made by the assistant.
  
      Args:
          response_template (`Union[str, List[int]]`): the template form that indicates the start of the response, typically something like
              '### Response:
  '. It can also be passed as tokenized ids, which can be useful when using a tokenizer that encodes the response
              differently if it does not have proper context.
          instruction_template (`Union[str, List[int]]`): the template form that indicates the start of the human instruction, typically something like
              '### Human:
  '. Useful for assistant-style conversation datasets. It can also be passed as tokenized ids.
          mlm (`bool`, *optional*, defaults to `False`): Whether or not to use masked language modeling in the underlying
              `DataCollatorForLanguageModeling` class. Note that this option currently has no effect but is present
               for flexibility and backwards-compatibility.
          ignore_index (`int`, *optional*, defaults to `-100`):
              The index to use to ignore the initial tokens with
      
class_docstr_tok: ["Data","collator","used","for","completion","tasks",".","It","ensures","that","all","the","tokens","of","the","labels","are","set","to","an","'ignore_index'","when","they","do","not","come","from","the","assistant",".","This","ensure","that","the","loss","is","only","calculated","on","the","completion","made","by","the","assistant",".","Args",":","response_template","(","`","Union","[","str",",","List","[","int","]","]","`",")",":","the","template","form","that","indicates","the","start","of","the","response",",","typically","something","like","'","#","#","#","Response",":","'",".","It","can","also","be","passed","as","tokenized","ids",",","which","can","be","useful","when","using","a","tokenizer","that","encodes","the","response","differently","if","it","does","not","have","proper","context",".","instruction_template","(","`","Union","[","str",",","List","[","int","]","]","`",")",":","the","template","form","that","indicates","the","start","of","the","human","instruction",",","typically","something","like","'","#","#","#","Human",":","'",".","Useful","for","assistant-style","conversation","datasets",".","It","can","also","be","passed","as","tokenized","ids",".","mlm","(","`","bool","`",",","*","optional","*",",","defaults","to","`","False","`",")",":","Whether","or","not","to","use","masked","language","modeling","in","the","underlying","`","DataCollatorForLanguageModeling","`","class",".","Note","that","this","option","currently","has","no","effect","but","is","present","for","flexibility","and","backwards-compatibility",".","ignore_index","(","`","int","`",",","*","optional","*",",","defaults","to","`","-100","`",")",":","The","index","to","use","to","ignore","the","initial","tokens","with"]
code: def torch_call(
      self, examples: List[Union[List[int], Any, Dict[str, Any]]]
  ) -> Dict[str, Any]:
      batch = super().torch_call(examples)
  
      if self.instruction_template is None:
          for i in range(len(examples)):
              response_token_ids_start_idx = None
  
              for idx in np.where(
                  batch["labels"][i] == self.response_token_ids[0]
              )[0]:
                  # `response_token_ids` is `'### Response:\n'`, here we are just making sure that the token IDs match
                  if (
                      self.response_token_ids
                      == batch["labels"][i][
                          idx : idx + len(self.response_token_ids)
                      ].tolist()
                  ):
                      response_token_ids_start_idx = idx
  
              if response_token_ids_start_idx is None:
                  warnings.warn(
                      f"Could not find response key `{self.response_template}` in the "
                      f'following instance: {self.tokenizer.decode(batch["input_ids"][i])} '
                      f"This instance will be ignored in loss calculation. "
                      f"Note, if this happens often, consider increasing the `max_seq_length`."
                  )
                  batch["labels"][i, :] = self.ignore_index
              else:
                  response_token_ids_end_idx = (
                      response_token_ids_start_idx
                      + len(self.response_token_ids)
                  )
  
                  # Make pytorch loss function ignore all tokens up through the end of the response key
                  batch["labels"][
                      i, :response_token_ids_end_idx
                  ] = self.ignore_index
  
      else:
          for i in range(len(examples)):
              response_token_ids_idxs = []
              human_token_ids_idxs = []
  
              for assistant_idx in np.where(
                  batch["labels"][i] == self.response_token_ids[0]
              )[0]:
                  # find the indexes of the start of a response.
                  if (
                      self.response_token_ids
                      == batch["labels"][i][
                          assistant_idx : assistant_idx
                          + len(self.response_token_ids)
                      ].tolist()
                  ):
                      response_token_ids_idxs.append(
                          assistant_idx + len(self.response_token_ids)
                      )
  
              if len(response_token_ids_idxs) == 0:
                  warnings.warn(
                      f"Could not find response key `{self.response_template}` in the "
                      f'following instance: {self.tokenizer.decode(batch["input_ids"][i])} '
                      f"This instance will be ignored in loss calculation. "
                      f"Note, if this happens often, consider increasing the `max_seq_length`."
                  )
                  batch["labels"][i, :] = self.ignore_index
  
              human_token_ids = self.instruction_token_ids
              for human_idx in np.where(
                  batch["labels"][i] == human_token_ids[0]
              )[0]:
                  # find the indexes of the start of a human answer.
                  if (
                      human_token_ids
                      == batch["labels"][i][
                          human_idx : human_idx + len(human_token_ids)
                      ].tolist()
                  ):
                      human_token_ids_idxs.append(human_idx)
  
              if len(human_token_ids_idxs) == 0:
                  warnings.warn(
                      f"Could not find instruction key `{self.instruction_template}` in the "
                      f'following instance: {self.tokenizer.decode(batch["input_ids"][i])} '
                      f"This instance will be ignored in loss calculation. "
                      f"Note, if this happens often, consider increasing the `max_seq_length`."
                  )
                  batch["labels"][i, :] = self.ignore_index
  
              if (
                  len(human_token_ids_idxs) > 0
                  and len(response_token_ids_idxs) > 0
                  and human_token_ids_idxs[0]
                  > response_token_ids_idxs[0]
              ):
                  human_token_ids_idxs = [0] + human_token_ids_idxs
  
              for idx, (start, end) in enumerate(
                  zip(human_token_ids_idxs, response_token_ids_idxs)
              ):
                  # Make pytorch loss function ignore all non response tokens
                  if idx != 0:
                      batch["labels"][i, start:end] = self.ignore_index
                  else:
                      batch["labels"][i, :end] = self.ignore_index
  
              if len(response_token_ids_idxs) < len(
                  human_token_ids_idxs
              ):
                  batch["labels"][
                      i, human_token_ids_idxs[-1] :
                  ] = self.ignore_index
  
      return batch
  
imports: import random
  import warnings
  from collections import deque
  from dataclasses import dataclass
  from typing import Any, Dict, List, Optional, Tuple, Union
  import numpy
  import torch
  from torch.nn.utils.rnn import pad_sequence
  from torch.utils.data import IterableDataset
  from transformers import DataCollatorForLanguageModeling, PreTrainedTokenizerBase
  from ..import_utils import is_unsloth_available
label_desc: Use image node_id 2 for calling the DataCollatorForCompletionOnlyLM obj's underlying member method code with example usage: obj.torch_call(examples) and returns: batch