class_docstr: Utilities for converting HFBertModels to ParlAI Models.
class_docstr_tok: ["Utilities","for","converting","HFBertModels","to","ParlAI","Models","."]
docstr: Load BERT State from HF Model, convert to ParlAI Model.
  
  :param state_dict:
      ParlAI model state_dict
  :param pretrained_dpr_path:
      path to pretrained DPR model
  :param encoder_type:
      whether we're loading a document or query encoder.
  
  :return new_state_dict:
      return a state_dict with loaded weights.
docstr_tok: ["Load","BERT","State","from","HF","Model",",","convert","to","ParlAI","Model",".",":","param","state_dict",":","ParlAI","model","state_dict",":","param","pretrained_dpr_path",":","path","to","pretrained","DPR","model",":","param","encoder_type",":","whether","we","'re","loading","a","document","or","query","encoder",".",":","return","new_state_dict",":","return","a","state_dict","with","loaded","weights","."]
code: def load_bert_state(
      datapath: str,
      state_dict: Dict[str, torch.Tensor],
      pretrained_dpr_path: str,
      encoder_type: str = "query",
  ) -> Dict[str, torch.Tensor]:
      """
      Load BERT State from HF Model, convert to ParlAI Model.
  
      :param state_dict:
          ParlAI model state_dict
      :param pretrained_dpr_path:
          path to pretrained DPR model
      :param encoder_type:
          whether we're loading a document or query encoder.
  
      :return new_state_dict:
          return a state_dict with loaded weights.
      """
  
      try:
          bert_model = BertModel.from_pretrained("bert-base-uncased")
      except OSError:
          model_path = PathManager.get_local_path(
              os.path.join(datapath, "bert_base_uncased")
          )
          bert_model = BertModel.from_pretrained(model_path)
  
      if pretrained_dpr_path:
          BertConversionUtils.load_dpr_model(
              bert_model, pretrained_dpr_path, encoder_type
          )
      bert_state_dict = bert_model.state_dict()
      for key in BERT_COMPATIBILITY_KEYS:
          bert_state_dict.pop(key, None)
      return_dict = BertConversionUtils.convert_bert_to_parlai(
          bert_state_dict
      )
  
      assert all(
          a in return_dict for a in state_dict
      ), f"not all weights are being loaded: {[k for k in state_dict if k not in return_dict]}"
      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 1 for calling the BertConversionUtils obj's underlying member method code with example usage: obj.load_bert_state(datapath, state_dict, pretrained_dpr_path, encoder_type) and returns: return_dict