class_docstr: VINVL wrapper for pretraining
  MLM loss is described in https://arxiv.org/pdf/2004.06165.pdf
  Contrastive loss is an itm loss to guess,
      0 for a match,
      1 for a corrupt caption,
      2 for corrupt image labels
  VinVL trains with object detection labels concatenated with the input text.
docstr: VinVL model constructor for pretraining.
  MLM and Contrastive Loss heads are configurable through Dict types.
  Consult MLM and MLP head classes for their config options.
  
  Args:
      mlm_config (Optional[MLM.Config], optional):
          Config object for MLM head.
          Defaults to MLM.Config which uses the default MLM configs.
      contrast_config (Optional[ThreeWayContrastive.Config], optional):
          Config object for the 3-way contrastive head.
          Defaults to ThreeWayContrastive.Config which uses a MLP with 3 classes
      random_init (bool, optional):
          Flag to load VinVL bert weights from random_init.
          Defaults to False.
      bert_model_name (str, optional):
          Name for base bert model.
          Used for VinVL base configs and weights.
          Defaults to "bert-base-uncased".
      img_feature_dim (int, optional):
          The size of the VinVL image feature inputs.
          Defaults to 2054.
      use_img_layernorm (bool, optional):
          Flag to use layernorm on image encoding.
          Defaults to True.
      img_layer_norm_eps (float, optional):
          Image layernorm epsilon. Defaults to 1e-12.
docstr_tok: ["VinVL","model","constructor","for","pretraining",".","MLM","and","Contrastive","Loss","heads","are","configurable","through","Dict","types",".","Consult","MLM","and","MLP","head","classes","for","their","config","options",".","Args",":","mlm_config","(","Optional","[","MLM.Config","]",",","optional",")",":","Config","object","for","MLM","head",".","Defaults","to","MLM.Config","which","uses","the","default","MLM","configs",".","contrast_config","(","Optional","[","ThreeWayContrastive.Config","]",",","optional",")",":","Config","object","for","the","3-way","contrastive","head",".","Defaults","to","ThreeWayContrastive.Config","which","uses","a","MLP","with","3","classes","random_init","(","bool",",","optional",")",":","Flag","to","load","VinVL","bert","weights","from","random_init",".","Defaults","to","False",".","bert_model_name","(","str",",","optional",")",":","Name","for","base","bert","model",".","Used","for","VinVL","base","configs","and","weights",".","Defaults","to","``","bert-base-uncased","''",".","img_feature_dim","(","int",",","optional",")",":","The","size","of","the","VinVL","image","feature","inputs",".","Defaults","to","2054.","use_img_layernorm","(","bool",",","optional",")",":","Flag","to","use","layernorm","on","image","encoding",".","Defaults","to","True",".","img_layer_norm_eps","(","float",",","optional",")",":","Image","layernorm","epsilon",".","Defaults","to","1e-12","."]
code: def __init__(
      self,
      mlm_config: Optional[MLM.Config] = None,
      contrast_config: Optional[ThreeWayContrastive.Config] = None,
      random_init: bool = False,
      bert_model_name: str = "bert-base-uncased",
      img_feature_dim: int = 2054,
      use_img_layernorm: bool = True,
      img_layer_norm_eps: float = 1e-12,
      *args,
      **kwargs,
  ):
      """VinVL model constructor for pretraining.
      MLM and Contrastive Loss heads are configurable through Dict types.
      Consult MLM and MLP head classes for their config options.
  
      Args:
          mlm_config (Optional[MLM.Config], optional):
              Config object for MLM head.
              Defaults to MLM.Config which uses the default MLM configs.
          contrast_config (Optional[ThreeWayContrastive.Config], optional):
              Config object for the 3-way contrastive head.
              Defaults to ThreeWayContrastive.Config which uses a MLP with 3 classes
          random_init (bool, optional):
              Flag to load VinVL bert weights from random_init.
              Defaults to False.
          bert_model_name (str, optional):
              Name for base bert model.
              Used for VinVL base configs and weights.
              Defaults to "bert-base-uncased".
          img_feature_dim (int, optional):
              The size of the VinVL image feature inputs.
              Defaults to 2054.
          use_img_layernorm (bool, optional):
              Flag to use layernorm on image encoding.
              Defaults to True.
          img_layer_norm_eps (float, optional):
              Image layernorm epsilon. Defaults to 1e-12.
      """
      super().__init__()
      if mlm_config is None:
          mlm_config = asdict(MLM.Config())
      if contrast_config is None:
          contrast_config = asdict(ThreeWayContrastive.Config())
  
      self.bert = build_vinvl_base(
          bert_model_name=bert_model_name,
          img_feature_dim=img_feature_dim,
          use_img_layernorm=use_img_layernorm,
          img_layer_norm_eps=img_layer_norm_eps,
          random_init=random_init,
      )
      self.mlm_head = MLM(config=mlm_config)
      self.ce_loss = nn.CrossEntropyLoss()
      self.contrast_head = ThreeWayContrastive(contrast_config)
  
imports: import logging
  from collections import namedtuple
  from dataclasses import asdict, dataclass
  from typing import Any, Dict, Optional, Tuple
  import torch
  from mmf.common.registry import registry
  from mmf.common.sample import SampleList
  from mmf.models.base_model import BaseModel
  from mmf.models.transformers.heads.contrastive import ThreeWayContrastive
  from mmf.models.transformers.heads.mlm import MLM
  from mmf.models.transformers.heads.mlp import MLP
  from mmf.utils.general import retry_n
  from omegaconf import MISSING, OmegaConf
  from torch import nn, Tensor
label_desc: Use image node_id 1 to create a new VinVLForPretraining object from inherited base classes: nn with example: obj = VinVLForPretraining(mlm_config, contrast_config, random_init, bert_model_name, img_feature_dim, use_img_layernorm, img_layer_norm_eps)