class_docstr: Megatron encoder-decoder base class
class_docstr_tok: ["Megatron","encoder-decoder","base","class"]
docstr: Prepares the global batch for megatron-core fwd/bwd functions.
  Global batch is a list of micro batches.
docstr_tok: ["Prepares","the","global","batch","for","megatron-core","fwd\/bwd","functions",".","Global","batch","is","a","list","of","micro","batches","."]
code: def _process_global_batch_without_megatron_batch_sampler(
      self, global_batch, tokenizer=None
  ):
      """Prepares the global batch for megatron-core fwd/bwd functions.
      Global batch is a list of micro batches.
      """
      tokenizer = self.tokenizer if tokenizer is None else tokenizer
      text_enc_list = []
      text_dec_list = []
      labels_list = []
      loss_mask_list = []
      enc_mask_list = []
      dec_mask_list = []
  
      # Determine the maximum encoder and decoder sequence lengths amongst microbatches and pad each microbatch to the max seq length.
      # NOTE: This should only happen for model finetuning where we pad dynamically. Training uses fixed training shapes.
  
      max_enc_seq_lenth = max(
          [
              micro_batch["text_enc"].shape[1]
              for micro_batch in global_batch
          ]
      )
      max_dec_seq_lenth = max(
          [
              micro_batch["text_dec"].shape[1]
              for micro_batch in global_batch
          ]
      )
  
      for micro_batch in global_batch:
          (
              text_enc,
              text_dec,
              loss_mask,
              labels,
              enc_mask,
              dec_mask,
          ) = self.process_micro_batch(micro_batch)
          # Check if encoder sequence length < max encoder sequence length of the global batch and pad.
          if text_enc.shape[1] < max_enc_seq_lenth:
              text_enc = torch.nn.functional.pad(
                  text_enc,
                  (0, max_enc_seq_lenth - text_enc.shape[1], 0, 0),
                  "constant",
                  tokenizer.pad_id,
              )
              enc_mask = torch.nn.functional.pad(
                  enc_mask,
                  (0, max_enc_seq_lenth - enc_mask.shape[1], 0, 0),
                  "constant",
                  0,
              )
          if text_dec.shape[1] < max_dec_seq_lenth:
              text_dec = torch.nn.functional.pad(
                  text_dec,
                  (0, max_dec_seq_lenth - text_dec.shape[1], 0, 0),
                  "constant",
                  tokenizer.pad_id,
              )
              dec_mask = torch.nn.functional.pad(
                  dec_mask,
                  (0, max_dec_seq_lenth - dec_mask.shape[1], 0, 0),
                  "constant",
                  0,
              )
              labels = torch.nn.functional.pad(
                  labels,
                  (0, max_dec_seq_lenth - labels.shape[1], 0, 0),
                  "constant",
                  tokenizer.pad_id,
              )
              loss_mask = torch.nn.functional.pad(
                  loss_mask,
                  (0, max_dec_seq_lenth - loss_mask.shape[1], 0, 0),
                  "constant",
                  0,
              )
          text_enc_list.append(text_enc)
          text_dec_list.append(text_dec)
          labels_list.append(labels)
          loss_mask_list.append(loss_mask)
          enc_mask_list.append(enc_mask)
          dec_mask_list.append(dec_mask)
  
      # Concatenate to (num_microbatches x micro_batch_size x seq_len)
      tokens_enc_tensor = torch.concat(text_enc_list, dim=0)
      tokens_dec_tensor = torch.concat(text_dec_list, dim=0)
      labels_tensor = torch.concat(labels_list, dim=0)
      loss_mask_tensor = torch.concat(loss_mask_list, dim=0)
      enc_mask_tensor = torch.concat(enc_mask_list, dim=0)
      dec_mask_tensor = torch.concat(dec_mask_list, dim=0)
  
      return {
          "text_enc": tokens_enc_tensor,
          "text_dec": tokens_dec_tensor,
          "loss_mask": loss_mask_tensor,
          "labels": labels_tensor,
          "enc_mask": enc_mask_tensor,
          "dec_mask": dec_mask_tensor,
      }
  
imports: import copy
  import functools
  import inspect
  from typing import Any, Dict, List, Optional
  import torch
  from omegaconf import OmegaConf, open_dict
  from omegaconf.dictconfig import DictConfig
  from pytorch_lightning.accelerators import CPUAccelerator
  from pytorch_lightning.trainer.trainer import Trainer
  from nemo.collections.nlp.data.language_modeling.megatron.data_samplers import MegatronPretrainingRandomSampler, MegatronPretrainingSampler
  from nemo.collections.nlp.models.language_modeling.megatron_base_model import MegatronBaseModel
  from nemo.collections.nlp.modules.common.megatron.build_model import build_model
  from nemo.collections.nlp.modules.common.megatron.module import Float16Module
  from nemo.collections.nlp.modules.common.megatron.token_level_encoder_decoder import MegatronTokenLevelEncoderDecoderModule
  from nemo.collections.nlp.modules.common.megatron.utils import ApexGuardDefaults, average_losses_across_data_parallel_group, get_params_for_weight_decay_optimization
  from nemo.collections.nlp.modules.common.text_generation_utils import compute_beam_search_len_penalty, get_sampling_token_fn
  from nemo.collections.nlp.parts.utils_funcs import get_last_rank
  from nemo.utils import AppState, logging
label_desc: Use image node_id 30 for calling the MegatronLMEncoderDecoderModel obj's underlying member method code with example usage: obj._process_global_batch_without_megatron_batch_sampler(global_batch, tokenizer) and returns: dict