class_docstr: This class jointly handles the hidden transforms and hidden loss transforms.
  It helps in validating, and applying the transforms.
class_docstr_tok: ["This","class","jointly","handles","the","hidden","transforms","and","hidden","loss","transforms",".","It","helps","in","validating",",","and","applying","the","transforms","."]
code: def __init__(
      self,
      hidden_transforms: List[MegatronBaseHiddenLoss] = [],
      hidden_loss_transforms: List[MegatronBaseHiddenTransform] = [],
      enc_output_name: str = "hiddens",  # name (key) of the encoder output
      tokens_loss_weight: float = 1.0,  # weight of the tokens loss
      loss_prefix: str = "hiddens_",  # if not None or "", add this prefix to all loss names
  ):
      super().__init__()
      self.hidden_transforms = hidden_transforms
      self.hidden_loss_transforms = hidden_loss_transforms
      self.enc_output_name = enc_output_name
      self.tokens_loss_weight = tokens_loss_weight
      self.loss_prefix = loss_prefix
  
      # register all hidden / loss transforms as submodules to support learned parameters
      if not all(
          [
              isinstance(ht, MegatronBaseHiddenLoss)
              for ht in self.hidden_loss_transforms
          ]
      ):
          raise TypeError(
              f"hidden_loss_transforms should be a list of MegatronBaseHiddenLoss, but got {hidden_loss_transforms}"
          )
      self.hidden_loss_transforms = torch.nn.ModuleList(
          self.hidden_loss_transforms
      )
      if not all(
          [
              isinstance(ht, MegatronBaseHiddenTransform)
              for ht in self.hidden_transforms
          ]
      ):
          raise TypeError(
              f"hidden_transforms should be a list of MegatronBaseHiddenTransform, but got {hidden_transforms}"
          )
      self.hidden_transforms = torch.nn.ModuleList(
          self.hidden_transforms
      )
  
      # validate the inputs and outputs of all hidden transforms (make sure there are no duplicate output names)
      duplicate_names = {}
      # initialize with available outputs from hidden transforms with hiddens and mask as default
      hidden_outputs = set(["hiddens", "hiddens_mask", "enc_output"])
      for ht in self.hidden_transforms:
          # validate that all required inputs are available by order of hidden transforms
          cur_input_names = set(ht.input_names)
          if not cur_input_names.issubset(hidden_outputs):
              raise ValueError(
                  f"Hidden transform {ht.name} requires inputs {cur_input_names - hidden_outputs} that are not available"
              )
  
          # collect all duplicate output names
          cur_hidden_outputs = set(ht.output_names)
          if not cur_hidden_outputs.isdisjoint(hidden_outputs):
              duplicate_names[ht.name] = list(
                  cur_hidden_outputs.intersection(hidden_outputs)
              )
  
          hidden_outputs.update(cur_hidden_outputs)
  
      # fail here reporting all duplicate output names
      if duplicate_names:
          raise ValueError(
              f"Hidden transforms have duplicate outputs {{name: [duplicate outputs]}} = {duplicate_names}"
          )
  
      # validate that all loss transforms are supported by output of hidden transforms ("hiddens" is given by default)
      loss_inputs = set(
          itertools.chain(
              *[lt.input_names for lt in self.hidden_loss_transforms]
          )
      )
      if not loss_inputs.issubset(hidden_outputs):
          loss_inputs_dict = {
              lt.name: lt.input_names
              for lt in self.hidden_loss_transforms
          }
          raise ValueError(
              f"Loss transforms inputs = {loss_inputs - hidden_outputs} are not supported by hidden transforms with hidden_outputs = {hidden_outputs}, expected inputs per loss = {loss_inputs_dict}"
          )
  
imports: import functools
  import itertools
  from typing import List
  import torch
  from omegaconf.dictconfig import DictConfig
  from omegaconf.omegaconf import OmegaConf
  from nemo.collections.nlp.modules.common.megatron.hiddens.megatron_hidden_loss import MegatronBaseHiddenLoss
  from nemo.collections.nlp.modules.common.megatron.hiddens.megatron_hidden_transform import MegatronBaseHiddenTransform
  from nemo.collections.nlp.modules.common.megatron.utils import ApexGuardDefaults
  from nemo.utils import logging
  from nemo.utils.model_utils import import_class_by_path
label_desc: Use image node_id 1 to create a new MegatronHiddensModule object from inherited base classes: torch.nn with example: obj = MegatronHiddensModule(hidden_transforms, hidden_loss_transforms, enc_output_name, tokens_loss_weight, loss_prefix)