


'''simple docstring'''






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


lowercase							=logging.get_logger(__name__)

lowercase							={
    'studio-ousia/luke-base': 'https://huggingface.co/studio-ousia/luke-base/resolve/main/config.json',
    'studio-ousia/luke-large': 'https://huggingface.co/studio-ousia/luke-large/resolve/main/config.json',
}






class  __magic_name__							(		__lowerCamelCase    ):
       UpperCAmelCase										='luke'






       def __init__(      self  ,							snake_case=5_0_2_6_7  ,							snake_case=5_0_0_0_0_0  ,							snake_case=7_6_8  ,							snake_case=2_5_6  ,							snake_case=1_2  ,							snake_case=1_2  ,							snake_case=3_0_7_2  ,							snake_case="gelu"  ,							snake_case=0.1  ,							snake_case=0.1  ,							snake_case=5_1_2  ,							snake_case=2  ,							snake_case=0.02  ,							snake_case=1E-1_2  ,							snake_case=True  ,							snake_case=None  ,							snake_case=1  ,							snake_case=0  ,							snake_case=2  ,							**snake_case  ,							)						->  Optional[Any]:




          '''simple docstring'''
          super().__init__(pad_token_id=UpperCamelCase_  ,							bos_token_id=UpperCamelCase_  ,							eos_token_id=UpperCamelCase_  ,							**UpperCamelCase_)

          _UpperCAmelCase       :       Any							   =vocab_size
          _UpperCAmelCase       :       Optional[int]							   =entity_vocab_size
          _UpperCAmelCase       :       List[str]							   =hidden_size
          _UpperCAmelCase       :       Dict							   =entity_emb_size
          _UpperCAmelCase       :       Tuple							   =num_hidden_layers
          _UpperCAmelCase       :       List[str]							   =num_attention_heads
          _UpperCAmelCase       :       Dict							   =hidden_act
          _UpperCAmelCase       :       Optional[Any]							   =intermediate_size
          _UpperCAmelCase       :       List[str]							   =hidden_dropout_prob
          _UpperCAmelCase       :       List[Any]							   =attention_probs_dropout_prob
          _UpperCAmelCase       :       str							   =max_position_embeddings
          _UpperCAmelCase       :       List[str]							   =type_vocab_size
          _UpperCAmelCase       :       Optional[int]							   =initializer_range
          _UpperCAmelCase       :       Any							   =layer_norm_eps
          _UpperCAmelCase       :       List[str]							   =use_entity_aware_attention
          _UpperCAmelCase       :       int							   =classifier_dropout





'''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


lowercase							=logging.getLogger(__name__)






class  __magic_name__							(		lowerCAmelCase    ):






       def __init__(      self  ,							snake_case  ,							snake_case  ,							snake_case  ,							snake_case=None)						->  List[Any]:




          '''simple docstring'''
          super().__init__(
              snake_case  ,							question_encoder_tokenizer=snake_case  ,							generator_tokenizer=snake_case  ,							index=snake_case  ,							init_retrieval=snake_case  ,							)
          _UpperCAmelCase       :       Union[str, Any]							   =None






       def       lowerCAmelCase		(      self  ,							snake_case)						->  Optional[Any]:




          '''simple docstring'''
          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
             _UpperCAmelCase       :       Optional[Any]							   =self._infer_socket_ifname()
             # avoid clash with the NCCL port
             _UpperCAmelCase       :       Optional[int]							   =str(distributed_port + 1)
             _UpperCAmelCase       :       Any							   =dist.new_group(ranks=snake_case  ,							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)						->  Tuple:




          '''simple docstring'''
          return dist.get_rank(group=self.process_group) == 0






       def       lowerCAmelCase		(      self  ,							snake_case  ,							snake_case  ,							snake_case=torch.floataa)						->  Union[str, Any]:




          '''simple docstring'''
          _UpperCAmelCase       :       Dict							   =torch.empty(snake_case  ,							dtype=snake_case)
          dist.scatter(snake_case  ,							src=0  ,							scatter_list=snake_case  ,							group=self.process_group)
          return target_tensor






       def       lowerCAmelCase		(      self)						->  Optional[Any]:




          '''simple docstring'''
          _UpperCAmelCase       :       Dict							   =psutil.net_if_addrs()
          # a hacky way to deal with varying network interface names
          _UpperCAmelCase       :       str							   =next((addr for addr in addrs if addr.startswith('e'))  ,							snake_case)
          return ifname






       def       lowerCAmelCase		(      self  ,							snake_case  ,							snake_case)						->  Tuple[np.ndarray, List[dict]]:




          '''simple docstring'''

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

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

          # gather logic
          _UpperCAmelCase       :       str							   =None
          if self._is_main():
             _UpperCAmelCase       :       Union[str, Any]							   =[torch.empty(question_hidden_states.shape  ,							dtype=torch.floataa) for _ in range(snake_case)]
          dist.gather(torch.tensor(snake_case)  ,							dst=0  ,							gather_list=snake_case  ,							group=self.process_group)

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

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

