code: def forward(
      self,
      source,
      padding_mask=None,
      mask=True,
      features_only=False,
      layer=None,
      mask_indices=None,
      mask_channel_indices=None,
      padding_count=None,
      corpus_key=None,
  ):
      if self.feature_grad_mult > 0:
          features = self.feature_extractor(source)
          if self.feature_grad_mult != 1.0:
              features = GradMultiply.apply(
                  features, self.feature_grad_mult
              )
      else:
          with torch.no_grad():
              features = self.feature_extractor(source)
  
      features_pen = features.float().pow(2).mean()
  
      features = features.transpose(1, 2)
      features = self.layer_norm(features)
      unmasked_features = features.clone()
  
      if padding_mask is not None and padding_mask.any():
          input_lengths = (1 - padding_mask.long()).sum(-1)
          # apply conv formula to get real output_lengths
          output_lengths = self._get_feat_extract_output_lengths(
              input_lengths
          )
  
          padding_mask = torch.zeros(
              features.shape[:2],
              dtype=features.dtype,
              device=features.device,
          )
  
          # these two operations makes sure that all values
          # before the output lengths indices are attended to
          padding_mask[
              (
                  torch.arange(
                      padding_mask.shape[0], device=padding_mask.device
                  ),
                  output_lengths - 1,
              )
          ] = 1
          padding_mask = (
              1 - padding_mask.flip([-1]).cumsum(-1).flip([-1])
          ).bool()
      else:
          padding_mask = None
  
      time_steps_to_drop = features.size(1) % self.crop_seq_to_multiple
      if time_steps_to_drop != 0:
          features = features[:, :-time_steps_to_drop]
          unmasked_features = unmasked_features[:, :-time_steps_to_drop]
          if padding_mask is not None:
              padding_mask = padding_mask[:, :-time_steps_to_drop]
  
      if self.post_extract_proj is not None:
          features = self.post_extract_proj(features)
  
      features = self.dropout_input(features)
      unmasked_features = self.dropout_features(unmasked_features)
  
      num_vars = None
      code_ppl = None
      prob_ppl = None
      curr_temp = None
  
      if self.input_quantizer:
          q = self.input_quantizer(features, produce_targets=False)
          features = q["x"]
          num_vars = q["num_vars"]
          code_ppl = q["code_perplexity"]
          prob_ppl = q["prob_perplexity"]
          curr_temp = q["temp"]
          features = self.project_inp(features)
  
      if mask:
          x, mask_indices = self.apply_mask(
              features,
              padding_mask,
              mask_indices=mask_indices,
              mask_channel_indices=mask_channel_indices,
          )
          if not is_xla_tensor(x) and mask_indices is not None:
              # tpu-comment: reducing the size in a dynamic way causes
              # too many recompilations on xla.
              y = unmasked_features[mask_indices].view(
                  unmasked_features.size(0),
                  -1,
                  unmasked_features.size(-1),
              )
          else:
              y = unmasked_features
      else:
          x = features
          y = unmasked_features
          mask_indices = None
  
      x, layer_results = self.encoder(
          x,
          padding_mask=padding_mask,
          layer=layer,
          corpus_key=corpus_key,
      )
  
      if features_only:
          return {
              "x": x,
              "padding_mask": padding_mask,
              "features": unmasked_features,
              "layer_results": layer_results,
          }
  
      if self.quantizer:
          if self.negatives_from_everywhere:
              q = self.quantizer(
                  unmasked_features, produce_targets=False
              )
              y = q["x"]
              num_vars = q["num_vars"]
              code_ppl = q["code_perplexity"]
              prob_ppl = q["prob_perplexity"]
              curr_temp = q["temp"]
              y = self.project_q(y)
  
              negs, _ = self.sample_negatives(
                  y,
                  mask_indices[0].sum(),
                  padding_count=padding_count,
              )
              y = y[mask_indices].view(y.size(0), -1, y.size(-1))
  
          else:
              q = self.quantizer(y, produce_targets=False)
              y = q["x"]
              num_vars = q["num_vars"]
              code_ppl = q["code_perplexity"]
              prob_ppl = q["prob_perplexity"]
              curr_temp = q["temp"]
  
              y = self.project_q(y)
  
              negs, _ = self.sample_negatives(
                  y,
                  y.size(1),
                  padding_count=padding_count,
              )
  
          if self.codebook_negatives > 0:
              cb_negs = self.quantizer.sample_from_codebook(
                  y.size(0) * y.size(1), self.codebook_negatives
              )
              cb_negs = cb_negs.view(
                  self.codebook_negatives, y.size(0), y.size(1), -1
              )  # order doesnt matter
              cb_negs = self.project_q(cb_negs)
              negs = torch.cat([negs, cb_negs], dim=0)
      else:
          y = self.project_q(y)
  
          if self.negatives_from_everywhere:
              negs, _ = self.sample_negatives(
                  unmasked_features,
                  y.size(1),
                  padding_count=padding_count,
              )
              negs = self.project_q(negs)
          else:
              negs, _ = self.sample_negatives(
                  y,
                  y.size(1),
                  padding_count=padding_count,
              )
  
      if not is_xla_tensor(x):
          # tpu-comment: reducing the size in a dynamic way causes
          # too many recompilations on xla.
          x = x[mask_indices].view(x.size(0), -1, x.size(-1))
  
      if self.target_glu:
          y = self.target_glu(y)
          negs = self.target_glu(negs)
  
      x = self.final_proj(x)
      x = self.compute_preds(x, y, negs)
  
      result = {
          "x": x,
          "padding_mask": padding_mask,
          "features_pen": features_pen,
      }
  
      if prob_ppl is not None:
          result["prob_perplexity"] = prob_ppl
          result["code_perplexity"] = code_ppl
          result["num_vars"] = num_vars
          result["temp"] = curr_temp
  
      return result
  
imports: import math
  from dataclasses import dataclass, field
  from typing import List, Tuple
  import numpy
  import torch
  import torch.nn
  import torch.nn.functional
  from fairseq import utils
  from fairseq.data.data_utils import compute_mask_indices
  from fairseq.dataclass import ChoiceEnum, FairseqDataclass
  from fairseq.distributed import fsdp_wrap
  from fairseq.models import BaseFairseqModel, register_model
  from fairseq.distributed.fully_sharded_data_parallel import FullyShardedDataParallel
  from fairseq.modules import Fp32GroupNorm, Fp32LayerNorm, GradMultiply, GumbelVectorQuantizer, LayerNorm, MultiheadAttention, RelPositionalEncoding, SamePad, TransposeLast
  from fairseq.modules.checkpoint_activations import checkpoint_wrapper
  from fairseq.modules.conformer_layer import ConformerWav2Vec2EncoderLayer
  from fairseq.modules.transformer_sentence_encoder import init_bert_params
  from fairseq.utils import buffered_arange, index_put, is_xla_tensor
  from .utils import pad_to_multiple
label_desc: Use image node_id 8 for calling the Wav2Vec2Model obj's underlying member method code with example usage: obj.forward(source, padding_mask, mask, features_only, layer, mask_indices, mask_channel_indices, padding_count, corpus_key) and returns: result, dict