code: def _get_ltor_masks_and_position_ids(
      data: torch.Tensor,
      eod_token: int,
      reset_position_ids: bool,
      reset_attention_mask: bool,
      eod_mask_loss: bool,
  ):
      """Build masks and position id for left to right model.
  
      Args:
          data (torch.Tensor): The data tenor that holds the tokens from the dataset
  
          eod_token (int): ID of the token to that is considered the EOD
  
          reset_position_ids (bool): Switch to reset the document position ID's
  
          reset_attention_mask (bool): Switch to reset the attention mask
  
          eod_mask_loss (bool): Switch to enable the EOD mask loss
  
      Returns:
          torch.Tensor : Attention mask needed to be used for Attention
  
          torch.Tensor : The mask used for loss value during training
  
          torch.Tensor : The position ID's of the token
  
      """
  
      # Extract batch size and sequence length.
      seq_length = data.numel()
  
      attention_mask = torch.tril(
          torch.ones((seq_length, seq_length), device=data.device)
      ).unsqueeze(0)
  
      # Loss mask.
      loss_mask = torch.ones(
          seq_length, dtype=torch.float, device=data.device
      )
      if eod_mask_loss:
          loss_mask[data == eod_token] = 0.0
  
      # Position ids.
      position_ids = torch.arange(
          seq_length, dtype=torch.long, device=data.device
      )
      # We need to clone as the ids will be modifed based on batch index.
      if reset_position_ids:
          position_ids = position_ids.clone()
  
      if reset_position_ids or reset_attention_mask:
          # Find indecies where EOD token is.
          eod_index = position_ids[data[b] == eod_token]
          # Detach indecies from positions if going to modify positions.
          if reset_position_ids:
              eod_index = eod_index.clone()
  
          # Loop through EOD indecies:
          prev_index = 0
          for j in range(eod_index.numel()):
              i = eod_index[j]
              # Mask attention loss.
              if reset_attention_mask:
                  attention_mask[0, (i + 1) :, : (i + 1)] = 0
              # Reset positions.
              if reset_position_ids:
                  position_ids[(i + 1) :] -= i + 1 - prev_index
                  prev_index = i + 1
  
      # Convert attention mask to binary:
      attention_mask = attention_mask < 0.5
  
      return attention_mask, loss_mask, position_ids
  
imports: import logging
  import os
  import time
  from dataclasses import dataclass
  from typing import Dict, Tuple, Union
  import numpy
  import torch
  from megatron.core.datasets.blended_megatron_dataset_config import BlendedMegatronDatasetConfig
  from megatron.core.datasets.indexed_dataset import MMapIndexedDataset
  from megatron.core.datasets.megatron_dataset import MegatronDataset
  from megatron.core.datasets.utils import Split, log_single_rank
label_desc: Use image node_id 3 for calling a global function with example usage: _get_ltor_masks_and_position_ids(data, eod_token, reset_position_ids, reset_attention_mask, eod_mask_loss) and returns: attention_mask,  loss_mask,  position_ids