class_docstr: Megatron encoder-decoder base class
class_docstr_tok: ["Megatron","encoder-decoder","base","class"]
docstr: An auxiliary function that executes the fwd_bwd_step function and parse the returned values.
docstr_tok: ["An","auxiliary","function","that","executes","the","fwd_bwd_step","function","and","parse","the","returned","values","."]
code: def _execute_fwd_bwd_function(
      self,
      data_iterator,
      forward_only,
      tensor_shape,
      decoder_seq_length,
  ):
      """
      An auxiliary function that executes the fwd_bwd_step function and parse the returned values.
      """
      fwd_bwd_function = get_forward_backward_func()
  
      seq_length = tensor_shape[0]
  
      losses_reduced_per_micro_batch = fwd_bwd_function(
          forward_step_func=self.get_forward_output_and_loss_func(),
          data_iterator=data_iterator,
          model=[self.enc_dec_model],
          num_microbatches=get_num_microbatches(),
          forward_only=forward_only,
          seq_length=seq_length,
          micro_batch_size=get_micro_batch_size(),
          decoder_seq_length=decoder_seq_length,
      )
  
      # only the last stages of the pipeline return losses
      if losses_reduced_per_micro_batch:
          mean_loss_dict = {}
          for k in losses_reduced_per_micro_batch[0].keys():
              # average loss across micro batches
              mean_loss_dict[k] = torch.stack(
                  [
                      loss_reduced[k]
                      for loss_reduced in losses_reduced_per_micro_batch
                  ]
              ).mean()
      else:
          loss_mean = torch.tensor(0.0).cuda()
          mean_loss_dict = {"loss": loss_mean}
  
      return mean_loss_dict
  
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 8 for calling the MegatronLMEncoderDecoderModel obj's underlying member method code with example usage: obj._execute_fwd_bwd_function(data_iterator, forward_only, tensor_shape, decoder_seq_length) and returns: mean_loss_dict