


import os
import socket
from contextlib import contextmanager

import torch

from ..commands.config.default import write_basic_config  # noqa: F401
from ..state import PartialState
from .dataclasses import DistributedType
from .imports import is_deepspeed_available, is_tpu_available
from .transformer_engine import convert_model
from .versions import is_torch_version


if is_deepspeed_available():
     from deepspeed import DeepSpeedEngine

if is_tpu_available(check_device=False):
     import torch_xla.core.xla_model as xm
def 				__lowerCamelCase						( UpperCAmelCase_						:		Optional[Any]						):




      """simple docstring"""





      if is_torch_version('''<''' ,  '''2.0.0'''						) or not hasattr(UpperCAmelCase_ ,  '''_dynamo'''						):
            return False
      return isinstance(UpperCAmelCase_ ,  torch._dynamo.eval_frame.OptimizedModule						)
def 				__lowerCamelCase						( UpperCAmelCase_						:		Dict ,  UpperCAmelCase_						:		bool = True						):




      """simple docstring"""





      a						:List[Any] 							=       (torch.nn.parallel.DistributedDataParallel, torch.nn.DataParallel)

      a						:List[str] 							=       is_compiled_module(UpperCAmelCase_						)
      if is_compiled:
            a						:Tuple 							=       model
            a						:Optional[int] 							=       model._orig_mod

      if is_deepspeed_available():
            options += (DeepSpeedEngine,)

      while isinstance(UpperCAmelCase_ ,  UpperCAmelCase_						):
            a						:Any 							=       model.module

      if not keep_fpaa_wrapper:
            a						:Union[str, Any] 							=       getattr(UpperCAmelCase_ ,  '''forward'''						)
            a						:str 							=       model.__dict__.pop('''_original_forward''' ,  UpperCAmelCase_						)
            if original_forward is not None:
                  while hasattr(UpperCAmelCase_ ,  '''__wrapped__'''						):
                        a						:Tuple 							=       forward.__wrapped__
                        if forward == original_forward:
                              break
                  a						:Union[str, Any] 							=       forward
            if getattr(UpperCAmelCase_ ,  '''_converted_to_transformer_engine''' ,  UpperCAmelCase_						):
                  convert_model(UpperCAmelCase_ ,  to_transformer_engine=UpperCAmelCase_						)

      if is_compiled:
            a						:List[Any] 							=       model
            a						:int 							=       compiled_model

      return model
def 				__lowerCamelCase						( ):




      """simple docstring"""





      PartialState().wait_for_everyone()
def 				__lowerCamelCase						( UpperCAmelCase_						:		str ,  UpperCAmelCase_						:		int						):




      """simple docstring"""





      if PartialState().distributed_type == DistributedType.TPU:
            xm.save(UpperCAmelCase_ ,  UpperCAmelCase_						)
      elif PartialState().local_process_index == 0:
            torch.save(UpperCAmelCase_ ,  UpperCAmelCase_						)
@contextmanager
def 				__lowerCamelCase						( **UpperCAmelCase_						:		Union[str, Any]						):




      """simple docstring"""





      for key, value in kwargs.items():
            a						:Union[str, Any] 							=       str(UpperCAmelCase_						)

      yield

      for key in kwargs:
            if key.upper() in os.environ:
                  del os.environ[key.upper()]
def 				__lowerCamelCase						( UpperCAmelCase_						:		Dict						):




      """simple docstring"""





      if not hasattr(UpperCAmelCase_ ,  '''__qualname__'''						) and not hasattr(UpperCAmelCase_ ,  '''__name__'''						):
            a						:List[str] 							=       getattr(UpperCAmelCase_ ,  '''__class__''' ,  UpperCAmelCase_						)
      if hasattr(UpperCAmelCase_ ,  '''__qualname__'''						):
            return obj.__qualname__
      if hasattr(UpperCAmelCase_ ,  '''__name__'''						):
            return obj.__name__
      return str(UpperCAmelCase_						)
def 				__lowerCamelCase						( UpperCAmelCase_						:		Optional[Any] ,  UpperCAmelCase_						:		Tuple						):




      """simple docstring"""





      for key, value in source.items():
            if isinstance(UpperCAmelCase_ ,  UpperCAmelCase_						):
                  a						:Tuple 							=       destination.setdefault(UpperCAmelCase_ ,  {}						)
                  merge_dicts(UpperCAmelCase_ ,  UpperCAmelCase_						)
            else:
                  a						:Optional[int] 							=       value

      return destination






def 				__lowerCamelCase						( UpperCAmelCase_						:		int = None						):




      """simple docstring"""





      if port is None:
            a						:Any 							=       2_9500
      with socket.socket(socket.AF_INET ,  socket.SOCK_STREAM						) as s:
            return s.connect_ex(('''localhost''', port)						) == 0









"""simple docstring"""







from ...configuration_utils import PretrainedConfig
from ...utils import logging


__lowercase    					=      logging.get_logger(__name__)

__lowercase    					=      {
    """facebook/dpr-ctx_encoder-single-nq-base""": (
        """https://huggingface.co/facebook/dpr-ctx_encoder-single-nq-base/resolve/main/config.json"""
    ),
    """facebook/dpr-question_encoder-single-nq-base""": (
        """https://huggingface.co/facebook/dpr-question_encoder-single-nq-base/resolve/main/config.json"""
    ),
    """facebook/dpr-reader-single-nq-base""": (
        """https://huggingface.co/facebook/dpr-reader-single-nq-base/resolve/main/config.json"""
    ),
    """facebook/dpr-ctx_encoder-multiset-base""": (
        """https://huggingface.co/facebook/dpr-ctx_encoder-multiset-base/resolve/main/config.json"""
    ),
    """facebook/dpr-question_encoder-multiset-base""": (
        """https://huggingface.co/facebook/dpr-question_encoder-multiset-base/resolve/main/config.json"""
    ),
    """facebook/dpr-reader-multiset-base""": (
        """https://huggingface.co/facebook/dpr-reader-multiset-base/resolve/main/config.json"""
    ),
}






class 						_A  (  _a  ):







    """simple docstring"""




    UpperCAmelCase       :							int	=       """dpr"""






    def __init__(					self				:    List[Any]				, __UpperCAmelCase				:    int=30522				, __UpperCAmelCase				:    Union[str, Any]=768				, __UpperCAmelCase				:    Dict=12				, __UpperCAmelCase				:    List[str]=12				, __UpperCAmelCase				:    Any=3072				, __UpperCAmelCase				:    Optional[int]="gelu"				, __UpperCAmelCase				:    Any=0.1				, __UpperCAmelCase				:    Union[str, Any]=0.1				, __UpperCAmelCase				:    str=512				, __UpperCAmelCase				:    List[str]=2				, __UpperCAmelCase				:    Tuple=0.02				, __UpperCAmelCase				:    List[str]=1e-12				, __UpperCAmelCase				:    List[str]=0				, __UpperCAmelCase				:    str="absolute"				, __UpperCAmelCase				:    int = 0				, **__UpperCAmelCase				:    Tuple				, ):
      super().__init__(pad_token_id=__UpperCAmelCase				, **__UpperCAmelCase)

      a						:		List[Any]      							=			vocab_size
      a						:		Optional[Any]      							=			hidden_size
      a						:		Union[str, Any]      							=			num_hidden_layers
      a						:		Dict      							=			num_attention_heads
      a						:		int      							=			hidden_act
      a						:		Any      							=			intermediate_size
      a						:		Any      							=			hidden_dropout_prob
      a						:		Dict      							=			attention_probs_dropout_prob
      a						:		Any      							=			max_position_embeddings
      a						:		Union[str, Any]      							=			type_vocab_size
      a						:		Optional[Any]      							=			initializer_range
      a						:		Dict      							=			layer_norm_eps
      a						:		int      							=			projection_dim
      a						:		str      							=			position_embedding_type






