code: def forward(
      self,
      source,
      target=None,
      id=None,
      mode=None,
      padding_mask=None,
      mask=True,
      features_only=False,
      force_remove_masked=False,
      remove_extra_tokens=True,
      precomputed_mask=None,
  ):
      if mode is None:
          assert self.cfg.supported_modality is not None
          mode = self.cfg.supported_modality
  
      if isinstance(mode, Modality):
          mode = mode.name
  
      feature_extractor = self.modality_encoders[mode]
  
      mask_seeds = None
      if id is not None:
          mask_seeds = MaskSeed(
              seed=self.cfg.seed, update=self.num_updates, ids=id
          )
  
      extractor_out = feature_extractor(
          source,
          padding_mask,
          mask,
          remove_masked=not features_only or force_remove_masked,
          clone_batch=self.cfg.clone_batch if not features_only else 1,
          mask_seeds=mask_seeds,
          precomputed_mask=precomputed_mask,
      )
  
      x = extractor_out["x"]
      encoder_mask = extractor_out["encoder_mask"]
      masked_padding_mask = extractor_out["padding_mask"]
      masked_alibi_bias = extractor_out.get("alibi_bias", None)
      alibi_scale = extractor_out.get("alibi_scale", None)
  
      if self.dropout_input is not None:
          x = self.dropout_input(x)
  
      layer_results = []
      for i, blk in enumerate(self.blocks):
          if (
              not self.training
              or self.cfg.layerdrop == 0
              or (np.random.random() > self.cfg.layerdrop)
          ):
              ab = masked_alibi_bias
              if ab is not None and alibi_scale is not None:
                  scale = (
                      alibi_scale[i]
                      if alibi_scale.size(0) > 1
                      else alibi_scale.squeeze(0)
                  )
                  ab = ab * scale.type_as(ab)
  
              x, lr = blk(
                  x,
                  padding_mask=masked_padding_mask,
                  alibi_bias=ab,
              )
              if features_only:
                  layer_results.append(lr)
  
      if self.norm is not None:
          x = self.norm(x)
  
      if features_only:
          if remove_extra_tokens:
              x = x[
                  :, feature_extractor.modality_cfg.num_extra_tokens :
              ]
              if masked_padding_mask is not None:
                  masked_padding_mask = masked_padding_mask[
                      :,
                      feature_extractor.modality_cfg.num_extra_tokens :,
                  ]
  
          return {
              "x": x,
              "padding_mask": masked_padding_mask,
              "layer_results": layer_results,
              "mask": encoder_mask,
          }
  
      xs = []
  
      if self.shared_decoder is not None:
          dx = self.forward_decoder(
              x,
              feature_extractor,
              self.shared_decoder,
              encoder_mask,
          )
          xs.append(dx)
      if feature_extractor.decoder is not None:
          dx = self.forward_decoder(
              x,
              feature_extractor,
              feature_extractor.decoder,
              encoder_mask,
          )
          xs.append(dx)
          orig_x = x
  
      assert len(xs) > 0
  
      p = next(self.ema.model.parameters())
      device = x.device
      dtype = x.dtype
      ema_device = p.device
      ema_dtype = p.dtype
  
      if not self.cfg.ema_same_dtype:
          dtype = ema_dtype
  
      if ema_device != device or ema_dtype != dtype:
          logger.info(
              f"adjusting ema dtype to {dtype} and device to {device}"
          )
          self.ema.model = self.ema.model.to(dtype=dtype, device=device)
          ema_dtype = dtype
  
          def to_device(d):
              for k, p in d.items():
                  if isinstance(d[k], dict):
                      to_device(d[k])
                  else:
                      d[k] = p.to(device=device)
  
          to_device(self.ema.fp32_params)
      tm = self.ema.model
  
      with torch.no_grad():
          tm.eval()
  
          if self.cfg.ema_encoder_only:
              assert target is None
              ema_input = extractor_out["local_features"]
              ema_input = feature_extractor.contextualized_features(
                  ema_input.to(dtype=ema_dtype),
                  padding_mask,
                  mask=False,
                  remove_masked=False,
              )
              ema_blocks = tm
          else:
              ema_blocks = tm.blocks
              if feature_extractor.modality_cfg.ema_local_encoder:
                  inp = (
                      target.to(dtype=ema_dtype)
                      if target is not None
                      else source.to(dtype=ema_dtype)
                  )
                  ema_input = tm.modality_encoders[mode](
                      inp,
                      padding_mask,
                      mask=False,
                      remove_masked=False,
                  )
              else:
                  assert target is None
                  ema_input = extractor_out["local_features"]
                  ema_feature_enc = tm.modality_encoders[mode]
                  ema_input = ema_feature_enc.contextualized_features(
                      ema_input.to(dtype=ema_dtype),
                      padding_mask,
                      mask=False,
                      remove_masked=False,
                  )
  
          ema_padding_mask = ema_input["padding_mask"]
          ema_alibi_bias = ema_input.get("alibi_bias", None)
          ema_alibi_scale = ema_input.get("alibi_scale", None)
          ema_input = ema_input["x"]
  
          y = []
          ema_x = []
          extra_tokens = feature_extractor.modality_cfg.num_extra_tokens
          for i, blk in enumerate(ema_blocks):
              ab = ema_alibi_bias
              if ab is not None and alibi_scale is not None:
                  scale = (
                      ema_alibi_scale[i]
                      if ema_alibi_scale.size(0) > 1
                      else ema_alibi_scale.squeeze(0)
                  )
                  ab = ab * scale.type_as(ab)
  
              ema_input, lr = blk(
                  ema_input,
                  padding_mask=ema_padding_mask,
                  alibi_bias=ab,
              )
              y.append(lr[:, extra_tokens:])
              ema_x.append(ema_input[:, extra_tokens:])
  
      y = self.make_targets(y, self.average_top_k_layers)
      orig_targets = y
  
      if self.cfg.clone_batch > 1:
          y = y.repeat_interleave(self.cfg.clone_batch, 0)
  
      masked = encoder_mask.mask.unsqueeze(-1)
      masked_b = encoder_mask.mask.bool()
      y = y[masked_b]
  
      if xs[0].size(1) == masked_b.size(1):
          xs = [x[masked_b] for x in xs]
      else:
          xs = [x.reshape(-1, x.size(-1)) for x in xs]
  
      sample_size = masked.sum().long()
  
      result = {
          "losses": {},
          "sample_size": sample_size,
      }
  
      sample_size = result["sample_size"]
  
      if self.cfg.cls_loss > 0:
          assert extra_tokens > 0
          cls_target = orig_targets.mean(dim=1)
          if self.cfg.clone_batch > 1:
              cls_target = cls_target.repeat_interleave(
                  self.cfg.clone_batch, 0
              )
          cls_pred = x[:, extra_tokens - 1]
          result["losses"]["cls"] = self.d2v_loss(
              cls_pred, cls_target
          ) * (self.cfg.cls_loss * sample_size)
  
      if self.cfg.recon_loss > 0:
          with torch.no_grad():
              target = feature_extractor.patchify(source)
              mean = target.mean(dim=-1, keepdim=True)
              var = target.var(dim=-1, keepdim=True)
              target = (target - mean) / (var + 1.0e-6) ** 0.5
  
              if self.cfg.clone_batch > 1:
                  target = target.repeat_interleave(
                      self.cfg.clone_batch, 0
                  )
  
              if masked_b is not None:
                  target = target[masked_b]
  
          recon = xs[0]
          if self.recon_proj is not None:
              recon = self.recon_proj(recon)
  
          result["losses"]["recon"] = (
              self.d2v_loss(recon, target.float()) * self.cfg.recon_loss
          )
  
      if self.cfg.d2v_loss > 0:
          for i, x in enumerate(xs):
              reg_loss = self.d2v_loss(x, y)
              n = (
                  f"{mode}_regression_{i}"
                  if len(xs) > 1
                  else f"{mode}_regression"
              )
              result["losses"][n] = reg_loss * self.cfg.d2v_loss
  
      suffix = "" if len(self.modalities) == 1 else f"_{mode}"
      with torch.no_grad():
          if encoder_mask is not None:
              result["masked_pct"] = 1 - (
                  encoder_mask.ids_keep.size(1)
                  / encoder_mask.ids_restore.size(1)
              )
          for i, x in enumerate(xs):
              n = (
                  f"pred_var{suffix}_{i}"
                  if len(xs) > 1
                  else f"pred_var{suffix}"
              )
              result[n] = self.compute_var(x.float())
          if self.ema is not None:
              for k, v in self.ema.logs.items():
                  result[k] = v
  
          y = y.float()
          result[f"target_var{suffix}"] = self.compute_var(y)
  
          if self.num_updates > 5000:
              if (
                  result[f"target_var{suffix}"]
                  < self.cfg.min_target_var
              ):
                  logger.error(
                      f"target var is {result[f'target_var{suffix}'].item()} < {self.cfg.min_target_var}, exiting ({mode})"
                  )
                  raise Exception(
                      f"target var is {result[f'target_var{suffix}'].item()} < {self.cfg.min_target_var}, exiting ({mode})"
                  )
  
              for k in result.keys():
                  if (
                      k.startswith("pred_var")
                      and result[k] < self.cfg.min_pred_var
                  ):
                      logger.error(
                          f"{k} is {result[k].item()} < {self.cfg.min_pred_var}, exiting ({mode})"
                      )
                      raise Exception(
                          f"{k} is {result[k].item()} < {self.cfg.min_pred_var}, exiting ({mode})"
                      )
  
          result["ema_decay"] = self.ema.get_decay() * 1000
  
      return result
  
imports: import logging
  import math
  from dataclasses import dataclass, field
  from typing import Optional, Callable
  from functools import partial
  import numpy
  from omegaconf import II
  import torch
  import torch.nn
  import torch.nn.functional
  import torch.distributed
  from fairseq.modules import EMAModule, EMAModuleConfig
  from fairseq.dataclass import FairseqDataclass
  from fairseq.models import BaseFairseqModel, register_model
  from examples.data2vec.data.modality import Modality
  from examples.data2vec.models.modalities.base import MaskSeed, D2vModalityConfig, ModalitySpecificEncoder, get_annealed_rate
  from examples.data2vec.models.modalities.modules import D2vDecoderConfig, AltBlock, Decoder1d
  from examples.data2vec.models.modalities.audio import D2vAudioConfig, AudioEncoder
  from examples.data2vec.models.modalities.images import D2vImageConfig, ImageEncoder
  from examples.data2vec.models.modalities.text import D2vTextConfig, TextEncoder
label_desc: Use image node_id 10 for calling the Data2VecMultiModel obj's underlying member method code with example usage: obj.forward(source, target, id, mode, padding_mask, mask, features_only, force_remove_masked, remove_extra_tokens, precomputed_mask) and returns: result, dict