





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


__magic_name__   :						Optional[int]    =			logging.getLogger(__name__)


class       __SCREAMING_SNAKE_CASE	(						__UpperCamelCase			):



     '''simple docstring'''






     def __init__(					self						,   lowerCamelCase						,   lowerCamelCase						,   lowerCamelCase						,   lowerCamelCase=None	):
       super().__init__(
           lowerCamelCase						,   question_encoder_tokenizer=lowerCamelCase						,   generator_tokenizer=lowerCamelCase						,   index=lowerCamelCase						,   init_retrieval=lowerCamelCase						,   )
       _snake_case     		=				None

     def 	UpperCamelCase(					self						,   lowerCamelCase	):
       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     		=				self._infer_socket_ifname()
         # avoid clash with the NCCL port
         _snake_case     		=				str(distributed_port + 1	)
         _snake_case     		=				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 	UpperCamelCase(					self	):
       return dist.get_rank(group=self.process_group	) == 0

     def 	UpperCamelCase(					self						,   lowerCamelCase						,   lowerCamelCase						,   lowerCamelCase=torch.floataa	):
       _snake_case     		=				torch.empty(lowerCamelCase						,   dtype=lowerCamelCase	)
       dist.scatter(lowerCamelCase						,   src=0						,   scatter_list=lowerCamelCase						,   group=self.process_group	)
       return target_tensor

     def 	UpperCamelCase(					self	):
       _snake_case     		=				psutil.net_if_addrs()
       # a hacky way to deal with varying network interface names
       _snake_case     		=				next((addr for addr in addrs if addr.startswith("e"	))						,   lowerCamelCase	)
       return ifname






     def 	UpperCamelCase(					self						,   lowerCamelCase						,   lowerCamelCase	):

       # single GPU training
       if not dist.is_initialized():
         _snake_case		,		_snake_case     		=				self._main_retrieve(lowerCamelCase						,   lowerCamelCase	)
         return retrieved_doc_embeds, doc_ids, self.index.get_doc_dicts(lowerCamelCase	)

       # distributed training
       _snake_case     		=				dist.get_world_size(group=self.process_group	)

       # gather logic
       _snake_case     		=				None
       if self._is_main():
         _snake_case     		=				[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     		=				question_hidden_states.shape[0]
       _snake_case     		=				[]
       _snake_case     		=				[]
       if self._is_main():
         assert len(lowerCamelCase	) == world_size
         _snake_case		,		_snake_case     		=				self._main_retrieve(torch.cat(lowerCamelCase	).numpy()						,   lowerCamelCase	)
         _snake_case		,		_snake_case     		=				torch.tensor(lowerCamelCase	), torch.tensor(lowerCamelCase	)
         _snake_case     		=				self._chunk_tensor(lowerCamelCase						,   lowerCamelCase	)
         _snake_case     		=				self._chunk_tensor(lowerCamelCase						,   lowerCamelCase	)
       _snake_case     		=				self._scattered(lowerCamelCase						,   [n_queries, n_docs]						,   target_type=torch.intaa	)
       _snake_case     		=				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	)









'''simple docstring'''



from ...configuration_utils import PretrainedConfig
from ...utils import logging
from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices


__magic_name__   :						Any    =			logging.get_logger(__name__)

__magic_name__   :						Dict    =			{
    """google/bit-50""": """https://huggingface.co/google/bit-50/resolve/main/config.json""",
}


class       __SCREAMING_SNAKE_CASE	(						__UpperCamelCase					,    __UpperCamelCase			):



     '''simple docstring'''






     UpperCAmelCase__       :						List[str]      =   '''bit'''
     UpperCAmelCase__       :						Union[str, Any]      =   ['''preactivation''', '''bottleneck''']
     UpperCAmelCase__       :						int      =   ['''SAME''', '''VALID''']

     def __init__(					self						,   lowerCamelCase=3						,   lowerCamelCase=64						,   lowerCamelCase=[256, 512, 1_024, 2_048]						,   lowerCamelCase=[3, 4, 6, 3]						,   lowerCamelCase="preactivation"						,   lowerCamelCase="relu"						,   lowerCamelCase=None						,   lowerCamelCase=32						,   lowerCamelCase=0.0						,   lowerCamelCase=False						,   lowerCamelCase=32						,   lowerCamelCase=1						,   lowerCamelCase=None						,   lowerCamelCase=None						,   **lowerCamelCase						,   ):
       super().__init__(**lowerCamelCase	)
       if layer_type not in self.layer_types:
         raise ValueError(F'''layer_type={layer_type} is not one of {",".join(self.layer_types	)}'''	)
       if global_padding is not None:
         if global_padding.upper() in self.supported_padding:
           _snake_case     		=				global_padding.upper()
         else:
           raise ValueError(F'''Padding strategy {global_padding} not supported'''	)
       _snake_case     		=				num_channels
       _snake_case     		=				embedding_size
       _snake_case     		=				hidden_sizes
       _snake_case     		=				depths
       _snake_case     		=				layer_type
       _snake_case     		=				hidden_act
       _snake_case     		=				global_padding
       _snake_case     		=				num_groups
       _snake_case     		=				drop_path_rate
       _snake_case     		=				embedding_dynamic_padding
       _snake_case     		=				output_stride
       _snake_case     		=				width_factor

       _snake_case     		=				["stem"] + [F'''stage{idx}''' for idx in range(1						,   len(lowerCamelCase	) + 1	)]
       _snake_case		,		_snake_case     		=				get_aligned_output_features_output_indices(
           out_features=lowerCamelCase						,   out_indices=lowerCamelCase						,   stage_names=self.stage_names	)


