class_docstr: Contrastive Model, currently mainly focused on memory bank and CSC.
class_docstr_tok: ["Contrastive","Model",",","currently","mainly","focused","on","memory","bank","and","CSC","."]
code: def __init__(self, cfg):
      super(ContrastiveModel, self).__init__()
      # Construct the model.
      self.backbone = _MODEL_TYPES[cfg.MODEL.ARCH](cfg)
      self.type = cfg.CONTRASTIVE.TYPE
      self.T = cfg.CONTRASTIVE.T
      self.dim = cfg.CONTRASTIVE.DIM
      self.length = cfg.CONTRASTIVE.LENGTH
      self.k = cfg.CONTRASTIVE.QUEUE_LEN
      self.mmt = cfg.CONTRASTIVE.MOMENTUM
      self.momentum_annealing = cfg.CONTRASTIVE.MOMENTUM_ANNEALING
      self.duration = 1
      self.cfg = cfg
      self.num_gpus = cfg.NUM_GPUS
      self.l2_norm = Normalize()
      self.knn_num_imgs = 0
      self.knn_on = cfg.CONTRASTIVE.KNN_ON
      self.train_labels = np.zeros((0,), dtype=np.int32)
      self.num_pos = 2
      self.num_crops = (
          self.cfg.DATA.TRAIN_CROP_NUM_TEMPORAL
          * self.cfg.DATA.TRAIN_CROP_NUM_SPATIAL
      )
      self.nce_loss_fun = losses.get_loss_func("contrastive_loss")(
          reduction="mean"
      )
      assert self.cfg.MODEL.LOSS_FUNC == "contrastive_loss"
      self.softmax = nn.Softmax(dim=1).cuda()
  
      if self.type == "mem":
          self.mem_type = cfg.CONTRASTIVE.MEM_TYPE
          if self.mem_type == "1d":
              self.memory = Memory1D(
                  self.length, self.duration, self.dim, cfg
              )
          else:
              self.memory = Memory(
                  self.length, self.duration, self.dim, cfg
              )
          self.examplar_type = "video"
          self.interp = cfg.CONTRASTIVE.INTERP_MEMORY
      elif self.type == "self":
          pass
      elif self.type == "moco" or self.type == "byol":
          # MoCo components
          self.backbone_hist = _MODEL_TYPES[cfg.MODEL.ARCH](cfg)
          for p in self.backbone_hist.parameters():
              p.requires_grad = False
          self.register_buffer("ptr", torch.tensor([0]))
          self.ptr.requires_grad = False
          stdv = 1.0 / math.sqrt(self.dim / 3)
          self.register_buffer(
              "queue_x",
              torch.rand(self.k, self.dim).mul_(2 * stdv).add_(-stdv),
          )
          self.register_buffer(
              "iter", torch.zeros([1], dtype=torch.long)
          )
          self._batch_shuffle_on = (
              False
              if (
                  "sync" in cfg.BN.NORM_TYPE
                  and cfg.BN.NUM_SYNC_DEVICES == cfg.NUM_GPUS
              )
              or self.type == "byol"
              else True
          )
      elif self.type == "swav":
          self.swav_use_public_code = True
          if self.swav_use_public_code:
              self.swav_prototypes = nn.Linear(
                  self.dim, 1000, bias=False
              )  # for orig implementation
          else:
              self.swav_prototypes = nn.Parameter(
                  torch.randn((self.dim, 1000), dtype=torch.float)
              )
          self.swav_eps_sinkhorn = 0.05
          self.swav_use_the_queue = False
          # optionally starts a queue
          if self.cfg.CONTRASTIVE.SWAV_QEUE_LEN > 0:
              self.register_buffer(
                  "queue_swav",
                  torch.zeros(
                      2,  # = args.crops_for_assign
                      self.cfg.CONTRASTIVE.SWAV_QEUE_LEN
                      // du.get_world_size(),
                      self.dim,
                  ),
              )
      elif self.type == "simclr":
          self._simclr_precompute_pos_neg_mask_multi()
      self.simclr_dist_on = cfg.CONTRASTIVE.SIMCLR_DIST_ON
  
      # self.knn_mem = Memory1D(self.length, 1, self.dim, cfg) #  does not work
      if self.knn_on:
          self.knn_mem = Memory(self.length, 1, self.dim, cfg)
  
imports: import math
  import numpy
  import torch
  import torch.nn
  import torch.nn.functional
  import slowfast.models.losses
  import slowfast.utils.distributed
  import slowfast.utils.logging
  from slowfast.models.video_model_builder import X3D, MViT, ResNet, SlowFast
  from .build import MODEL_REGISTRY
label_desc: Use image node_id 1 to create a new ContrastiveModel object from inherited base classes: nn with example: obj = ContrastiveModel(cfg)