code: def __init__(self, args, task):
      super(BertRanker, self).__init__(args, task)
  
      init_model = getattr(args, "pretrained_model", "")
      self.joint_layers = nn.ModuleList()
      if os.path.isfile(init_model):
          print(f"initialize weight from {init_model}")
  
          from fairseq import hub_utils
  
          x = hub_utils.from_pretrained(
              os.path.dirname(init_model),
              checkpoint_file=os.path.basename(init_model),
          )
  
          in_state_dict = x["models"][0].state_dict()
          init_args = x["args"].model
  
          num_positional_emb = (
              init_args.max_positions + task.dictionary.pad() + 1
          )
  
          # follow the setup in roberta
          self.model = TransformerSentenceEncoder(
              padding_idx=task.dictionary.pad(),
              vocab_size=len(task.dictionary),
              num_encoder_layers=getattr(
                  args, "encoder_layers", init_args.encoder_layers
              ),
              embedding_dim=init_args.encoder_embed_dim,
              ffn_embedding_dim=init_args.encoder_ffn_embed_dim,
              num_attention_heads=init_args.encoder_attention_heads,
              dropout=init_args.dropout,
              attention_dropout=init_args.attention_dropout,
              activation_dropout=init_args.activation_dropout,
              num_segments=2,  # add language embeddings
              max_seq_len=num_positional_emb,
              offset_positions_by_padding=False,
              encoder_normalize_before=True,
              apply_bert_init=True,
              activation_fn=init_args.activation_fn,
              freeze_embeddings=args.freeze_embeddings,
              n_trans_layers_to_freeze=args.n_trans_layers_to_freeze,
          )
  
          # still need to learn segment embeddings as we added a second language embedding
          if args.freeze_embeddings:
              for p in self.model.segment_embeddings.parameters():
                  p.requires_grad = False
  
          update_init_roberta_model_state(in_state_dict)
          print("loading weights from the pretrained model")
          self.model.load_state_dict(
              in_state_dict, strict=False
          )  # ignore mismatch in language embeddings
  
          ffn_embedding_dim = init_args.encoder_ffn_embed_dim
          num_attention_heads = init_args.encoder_attention_heads
          dropout = init_args.dropout
          attention_dropout = init_args.attention_dropout
          activation_dropout = init_args.activation_dropout
          activation_fn = init_args.activation_fn
  
          classifier_embed_dim = getattr(
              args, "embed_dim", init_args.encoder_embed_dim
          )
          if classifier_embed_dim != init_args.encoder_embed_dim:
              self.transform_layer = nn.Linear(
                  init_args.encoder_embed_dim, classifier_embed_dim
              )
      else:
          self.model = TransformerSentenceEncoder(
              padding_idx=task.dictionary.pad(),
              vocab_size=len(task.dictionary),
              num_encoder_layers=args.encoder_layers,
              embedding_dim=args.embed_dim,
              ffn_embedding_dim=args.ffn_embed_dim,
              num_attention_heads=args.attention_heads,
              dropout=args.dropout,
              attention_dropout=args.attention_dropout,
              activation_dropout=args.activation_dropout,
              max_seq_len=task.max_positions()
              if task.max_positions()
              else args.tokens_per_sample,
              num_segments=2,
              offset_positions_by_padding=False,
              encoder_normalize_before=args.encoder_normalize_before,
              apply_bert_init=args.apply_bert_init,
              activation_fn=args.activation_fn,
          )
  
          classifier_embed_dim = args.embed_dim
          ffn_embedding_dim = args.ffn_embed_dim
          num_attention_heads = args.attention_heads
          dropout = args.dropout
          attention_dropout = args.attention_dropout
          activation_dropout = args.activation_dropout
          activation_fn = args.activation_fn
  
      self.joint_classification = args.joint_classification
      if args.joint_classification == "sent":
          if args.joint_normalize_before:
              self.joint_layer_norm = LayerNorm(classifier_embed_dim)
          else:
              self.joint_layer_norm = None
  
          self.joint_layers = nn.ModuleList(
              [
                  TransformerSentenceEncoderLayer(
                      embedding_dim=classifier_embed_dim,
                      ffn_embedding_dim=ffn_embedding_dim,
                      num_attention_heads=num_attention_heads,
                      dropout=dropout,
                      attention_dropout=attention_dropout,
                      activation_dropout=activation_dropout,
                      activation_fn=activation_fn,
                  )
                  for _ in range(args.num_joint_layers)
              ]
          )
  
      self.classifier = RobertaClassificationHead(
          classifier_embed_dim,
          classifier_embed_dim,
          1,  # num_classes
          "tanh",
          args.classifier_dropout,
      )
  
imports: from dataclasses import dataclass, field
  import os
  import torch
  import torch.nn
  from fairseq import utils
  from fairseq.dataclass import ChoiceEnum, FairseqDataclass
  from fairseq.models import BaseFairseqModel, register_model
  from fairseq.models.roberta.model import RobertaClassificationHead
  from fairseq.modules import LayerNorm, TransformerSentenceEncoder, TransformerSentenceEncoderLayer
label_desc: Use image node_id 1 to create a new BertRanker object from inherited base classes: BaseRanker with example: obj = BertRanker(args, task)