class_docstr: Utilities for converting HFBertModels to ParlAI Models.
class_docstr_tok: ["Utilities","for","converting","HFBertModels","to","ParlAI","Models","."]
docstr: Load saved state from pretrained DPR model directly into given bert_model.
  
  :param bert_model:
      bert model to load
  :param pretrained_dpr_path:
      path to pretrained DPR BERT Model
  :param encoder_type:
      whether we're loading a document or query encoder.
docstr_tok: ["Load","saved","state","from","pretrained","DPR","model","directly","into","given","bert_model",".",":","param","bert_model",":","bert","model","to","load",":","param","pretrained_dpr_path",":","path","to","pretrained","DPR","BERT","Model",":","param","encoder_type",":","whether","we","'re","loading","a","document","or","query","encoder","."]
code: def load_dpr_model(
      bert_model: BertModel, pretrained_dpr_path: str, encoder_type: str
  ):
      """
      Load saved state from pretrained DPR model directly into given bert_model.
  
      :param bert_model:
          bert model to load
      :param pretrained_dpr_path:
          path to pretrained DPR BERT Model
      :param encoder_type:
          whether we're loading a document or query encoder.
      """
      saved_state = torch.load(pretrained_dpr_path, map_location="cpu")
      model_to_load = (
          bert_model.module
          if hasattr(bert_model, "module")
          else bert_model
      )
  
      prefix = (
          "question_model." if encoder_type == "query" else "ctx_model."
      )
      prefix_len = len(prefix)
      encoder_state = {
          key[prefix_len:]: value
          for (key, value) in saved_state["model_dict"].items()
          if key.startswith(prefix)
      }
      encoder_state.update(
          {
              k: v
              for k, v in saved_state["model_dict"].items()
              if "encode_proj" in k
          }
      )
      try:
          model_to_load.load_state_dict(encoder_state)
      except RuntimeError:
          for key in BERT_COMPATIBILITY_KEYS:
              encoder_state[key] = recursive_getattr(model_to_load, key)
          model_to_load.load_state_dict(encoder_state)
  
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 2 for calling the BertConversionUtils obj's underlying member method code with example usage: obj.load_dpr_model(bert_model, pretrained_dpr_path, encoder_type) without return types