class_docstr: Utilities for converting HFBertModels to ParlAI Models.
class_docstr_tok: ["Utilities","for","converting","HFBertModels","to","ParlAI","Models","."]
docstr: Convert a given BERT state dict to a ParlAI state dict.
  
  :param state_dict:
      BertModel state dict
  
  :return state_dict:
      return a state_dict that fits with ParlAI Models.
docstr_tok: ["Convert","a","given","BERT","state","dict","to","a","ParlAI","state","dict",".",":","param","state_dict",":","BertModel","state","dict",":","return","state_dict",":","return","a","state_dict","that","fits","with","ParlAI","Models","."]
code: def convert_bert_to_parlai(
      state_dict: Dict[str, torch.Tensor]
  ) -> Dict[str, torch.Tensor]:
      """
      Convert a given BERT state dict to a ParlAI state dict.
  
      :param state_dict:
          BertModel state dict
  
      :return state_dict:
          return a state_dict that fits with ParlAI Models.
      """
      return_dict = OrderedDict()
      for each_key in state_dict.keys():
          mapped_key = each_key
  
          # 0. Skip pooler
          if "pooler" in each_key:
              continue
  
          # 1. Map Embeddings
          for emb in BERT_EMB_DICT_MAP:
              mapped_key = mapped_key.replace(
                  emb, BERT_EMB_DICT_MAP[emb]
              )
  
          # 2. Map Layers
          if "encoder" in each_key and "layer" in each_key:
              mapped_key = mapped_key.replace("encoder.layer", "layers")
          # 3. map attention
          if "attention" in each_key:
              mapped_key = mapped_key.replace(
                  "attention.self.query", "attention.q_lin"
              )
              mapped_key = mapped_key.replace(
                  "attention.self.key", "attention.k_lin"
              )
              mapped_key = mapped_key.replace(
                  "attention.self.value", "attention.v_lin"
              )
              mapped_key = mapped_key.replace(
                  "attention.self.dropout", "attention.attn_dropout"
              )
              mapped_key = mapped_key.replace(
                  "attention.output.dense", "attention.out_lin"
              )
              mapped_key = mapped_key.replace(
                  "attention.output.LayerNorm", "norm1"
              )
              mapped_key = mapped_key.replace(
                  "attention.output.dropout", "ffn.relu_dropout"
              )
          # 4. Map FFN
          if "intermediate" in each_key:
              mapped_key = mapped_key.replace(
                  "intermediate.dense", "ffn.lin1"
              )
          if "output" in each_key:
              mapped_key = mapped_key.replace(
                  "output.dense", "ffn.lin2"
              )
              mapped_key = mapped_key.replace(
                  "output.LayerNorm", "norm2"
              )
              mapped_key = mapped_key.replace(
                  "output.dropout", "dropout"
              )
  
          return_dict[mapped_key] = state_dict[each_key]
  
      return return_dict
  
file_path: ParlAI/parlai/agents/rag/conversion_utils.py
imports: from collections import OrderedDict
  import os
  import torch
  from transformers import BertModel
  from typing import Dict
  from parlai.utils.io import PathManager
  from parlai.utils.misc import recursive_getattr
label_desc: Use image node_id 3 for calling the BertConversionUtils obj's underlying member method code with example usage: obj.convert_bert_to_parlai(state_dict) and returns: return_dict