




from collections import OrderedDict
from typing import Mapping

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


_lowerCamelCase      :						Optional[Any]							=  logging.get_logger(__name__)

_lowerCamelCase      :						Dict							=  {
    """distilbert-base-uncased""": """https://huggingface.co/distilbert-base-uncased/resolve/main/config.json""",
    """distilbert-base-uncased-distilled-squad""": (
        """https://huggingface.co/distilbert-base-uncased-distilled-squad/resolve/main/config.json"""
    ),
    """distilbert-base-cased""": """https://huggingface.co/distilbert-base-cased/resolve/main/config.json""",
    """distilbert-base-cased-distilled-squad""": (
        """https://huggingface.co/distilbert-base-cased-distilled-squad/resolve/main/config.json"""
    ),
    """distilbert-base-german-cased""": """https://huggingface.co/distilbert-base-german-cased/resolve/main/config.json""",
    """distilbert-base-multilingual-cased""": (
        """https://huggingface.co/distilbert-base-multilingual-cased/resolve/main/config.json"""
    ),
    """distilbert-base-uncased-finetuned-sst-2-english""": (
        """https://huggingface.co/distilbert-base-uncased-finetuned-sst-2-english/resolve/main/config.json"""
    ),
}


class 							UpperCamelCase_       (					UpperCAmelCase__	):

    '''simple docstring'''






    UpperCAmelCase__				    = '''distilbert'''
    UpperCAmelCase__				    = {
        '''hidden_size''': '''dim''',
        '''num_attention_heads''': '''n_heads''',
        '''num_hidden_layers''': '''n_layers''',
    }


    def __init__(     self :      int   ,					UpperCAmelCase__ :      Dict=30_522   ,					UpperCAmelCase__ :      Optional[Any]=512   ,					UpperCAmelCase__ :      int=False   ,					UpperCAmelCase__ :      List[str]=6   ,					UpperCAmelCase__ :      Union[str, Any]=12   ,					UpperCAmelCase__ :      Optional[int]=768   ,					UpperCAmelCase__ :      List[Any]=4 * 768   ,					UpperCAmelCase__ :      List[str]=0.1   ,					UpperCAmelCase__ :      Dict=0.1   ,					UpperCAmelCase__ :      Any="gelu"   ,					UpperCAmelCase__ :      int=0.02   ,					UpperCAmelCase__ :      List[Any]=0.1   ,					UpperCAmelCase__ :      List[str]=0.2   ,					UpperCAmelCase__ :      List[str]=0   ,					**UpperCAmelCase__ :      Optional[Any]   ,					)		->str:





           '''simple docstring'''

           A__ 					=       vocab_size
           A__ 					=       max_position_embeddings
           A__ 					=       sinusoidal_pos_embds
           A__ 					=       n_layers
           A__ 					=       n_heads
           A__ 					=       dim
           A__ 					=       hidden_dim
           A__ 					=       dropout
           A__ 					=       attention_dropout
           A__ 					=       activation
           A__ 					=       initializer_range
           A__ 					=       qa_dropout
           A__ 					=       seq_classif_dropout
           super().__init__(**UpperCAmelCase__   ,					pad_token_id=UpperCAmelCase__)


class 							UpperCamelCase_       (					UpperCAmelCase__	):

    '''simple docstring'''






    @property
    def 			SCREAMING_SNAKE_CASE				(     self :      int)		->Mapping[str, Mapping[int, str]]:





           '''simple docstring'''

           if self.task == "multiple-choice":
                  A__ 					=       {0: '''batch''', 1: '''choice''', 2: '''sequence'''}
           else:
                  A__ 					=       {0: '''batch''', 1: '''sequence'''}
           return OrderedDict(
               [
                   ('''input_ids''', dynamic_axis),
                   ('''attention_mask''', dynamic_axis),
               ])








'''simple docstring'''






import logging
import os
from typing import List, Tuple

import numpy as np
import psutil
import torch
import torch.distributed as dist

from transformers import RagRetriever


A_			      =		logging.getLogger(__name__)






class        lowercase_      (						lowerCAmelCase_							):





     def __init__(     self     :					List[Any]     ,							__lowerCamelCase     :					Tuple     ,							__lowerCamelCase     :					str     ,							__lowerCamelCase     :					Tuple     ,							__lowerCamelCase     :					int=None			):
           super().__init__(
               __lowerCamelCase     ,							question_encoder_tokenizer=__lowerCamelCase     ,							generator_tokenizer=__lowerCamelCase     ,							index=__lowerCamelCase     ,							init_retrieval=__lowerCamelCase     ,							)
           snake_case__       :   Optional[int]	      =    None





     def 			_lowerCAmelCase      (     self     :					List[str]     ,							__lowerCamelCase     :					int			):
           logger.info('initializing retrieval'			)

           # initializing a separate process group for retrieval as the default
           # nccl backend doesn't support gather/scatter operations while gloo
           # is too slow to replace nccl for the core gpu communication
           if dist.is_initialized():
                 logger.info('dist initialized'			)
                 # needs to be set manually
                 snake_case__       :   Any	      =    self._infer_socket_ifname()
                 # avoid clash with the NCCL port
                 snake_case__       :   int	      =    str(distributed_port + 1			)
                 snake_case__       :   Any	      =    dist.new_group(ranks=__lowerCamelCase     ,							backend='gloo'			)

           # initialize retriever only on the main worker
           if not dist.is_initialized() or self._is_main():
                 logger.info('dist not initialized / main'			)
                 self.index.init_index()

           # all processes wait untill the retriever is initialized by the main process
           if dist.is_initialized():
                 torch.distributed.barrier(group=self.process_group			)





     def 			_lowerCAmelCase      (     self     :					List[Any]			):
           return dist.get_rank(group=self.process_group			) == 0





     def 			_lowerCAmelCase      (     self     :					Any     ,							__lowerCamelCase     :					Dict     ,							__lowerCamelCase     :					str     ,							__lowerCamelCase     :					Union[str, Any]=torch.floataa			):
           snake_case__       :   Optional[int]	      =    torch.empty(__lowerCamelCase     ,							dtype=__lowerCamelCase			)
           dist.scatter(__lowerCamelCase     ,							src=0     ,							scatter_list=__lowerCamelCase     ,							group=self.process_group			)
           return target_tensor





     def 			_lowerCAmelCase      (     self     :					Dict			):
           snake_case__       :   Union[str, Any]	      =    psutil.net_if_addrs()
           # a hacky way to deal with varying network interface names
           snake_case__       :   Any	      =    next((addr for addr in addrs if addr.startswith('e'			))     ,							__lowerCamelCase			)
           return ifname





     def 			_lowerCAmelCase      (     self     :					Tuple     ,							__lowerCamelCase     :					np.ndarray     ,							__lowerCamelCase     :					int			):

           # single GPU training
           if not dist.is_initialized():
                 snake_case__      ,							snake_case__       :   Optional[int]	      =    self._main_retrieve(__lowerCamelCase     ,							__lowerCamelCase			)
                 return retrieved_doc_embeds, doc_ids, self.index.get_doc_dicts(__lowerCamelCase			)

           # distributed training
           snake_case__       :   Optional[Any]	      =    dist.get_world_size(group=self.process_group			)

           # gather logic
           snake_case__       :   Optional[int]	      =    None
           if self._is_main():
                 snake_case__       :   Optional[int]	      =    [torch.empty(question_hidden_states.shape     ,							dtype=torch.floataa			) for _ in range(__lowerCamelCase			)]
           dist.gather(torch.tensor(__lowerCamelCase			)     ,							dst=0     ,							gather_list=__lowerCamelCase     ,							group=self.process_group			)

           # scatter logic
           snake_case__       :   Union[str, Any]	      =    question_hidden_states.shape[0]
           snake_case__       :   Tuple	      =    []
           snake_case__       :   List[Any]	      =    []
           if self._is_main():
                 assert len(__lowerCamelCase			) == world_size
                 snake_case__      ,							snake_case__       :   Optional[int]	      =    self._main_retrieve(torch.cat(__lowerCamelCase			).numpy()     ,							__lowerCamelCase			)
                 snake_case__      ,							snake_case__       :   Optional[Any]	      =    torch.tensor(__lowerCamelCase			), torch.tensor(__lowerCamelCase			)
                 snake_case__       :   str	      =    self._chunk_tensor(__lowerCamelCase     ,							__lowerCamelCase			)
                 snake_case__       :   Dict	      =    self._chunk_tensor(__lowerCamelCase     ,							__lowerCamelCase			)
           snake_case__       :   Tuple	      =    self._scattered(__lowerCamelCase     ,							[n_queries, n_docs]     ,							target_type=torch.intaa			)
           snake_case__       :   Dict	      =    self._scattered(__lowerCamelCase     ,							[n_queries, n_docs, question_hidden_states.shape[1]]			)

           return retrieved_doc_embeds.numpy(), doc_ids.numpy(), self.index.get_doc_dicts(__lowerCamelCase			)
