



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


snake_case_			:     str  	=  logging.getLogger(__name__)






class 							lowercase__     (							lowercase       ):





	def __init__(			self :    Tuple			,lowerCamelCase__ :    Optional[Any]			,lowerCamelCase__ :    Optional[int]			,lowerCamelCase__ :    Optional[Any]			,lowerCamelCase__ :    int=None			):







							'''simple docstring'''
							super().__init__(
							    lowerCamelCase__			,question_encoder_tokenizer=lowerCamelCase__			,generator_tokenizer=lowerCamelCase__			,index=lowerCamelCase__			,init_retrieval=lowerCamelCase__			,)
							_UpperCamelCase :		Dict			    =       None





	def      UpperCamelCase_  (			self :    str			,lowerCamelCase__ :    int			):







							'''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 :		Tuple			    =       self._infer_socket_ifname()
													# avoid clash with the NCCL port
													_UpperCamelCase :		Optional[int]			    =       str(distributed_port + 1			)
													_UpperCamelCase :		Union[str, 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      UpperCamelCase_  (			self :    Any			):







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





	def      UpperCamelCase_  (			self :    Optional[int]			,lowerCamelCase__ :    List[Any]			,lowerCamelCase__ :    Optional[Any]			,lowerCamelCase__ :    Dict=torch.floataa			):







							'''simple docstring'''
							_UpperCamelCase :		Tuple			    =       torch.empty(lowerCamelCase__			,dtype=lowerCamelCase__			)
							dist.scatter(lowerCamelCase__			,src=0			,scatter_list=lowerCamelCase__			,group=self.process_group			)
							return target_tensor





	def      UpperCamelCase_  (			self :    List[Any]			):







							'''simple docstring'''
							_UpperCamelCase :		Tuple			    =       psutil.net_if_addrs()
							# a hacky way to deal with varying network interface names
							_UpperCamelCase :		Optional[Any]			    =       next((addr for addr in addrs if addr.startswith('e'			))			,lowerCamelCase__			)
							return ifname





	def      UpperCamelCase_  (			self :    Tuple			,lowerCamelCase__ :    np.ndarray			,lowerCamelCase__ :    int			):







							'''simple docstring'''

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

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

							# gather logic
							_UpperCamelCase :		str			    =       None
							if self._is_main():
													_UpperCamelCase :		List[Any]			    =       [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
							_UpperCamelCase :		Optional[int]			    =       question_hidden_states.shape[0]
							_UpperCamelCase :		List[str]			    =       []
							_UpperCamelCase :		Tuple			    =       []
							if self._is_main():
													assert len(lowerCamelCase__			) == world_size
													_UpperCamelCase    ,      _UpperCamelCase :		List[Any]			    =       self._main_retrieve(torch.cat(lowerCamelCase__			).numpy()			,lowerCamelCase__			)
													_UpperCamelCase    ,      _UpperCamelCase :		Tuple			    =       torch.tensor(lowerCamelCase__			), torch.tensor(lowerCamelCase__			)
													_UpperCamelCase :		Any			    =       self._chunk_tensor(lowerCamelCase__			,lowerCamelCase__			)
													_UpperCamelCase :		Optional[Any]			    =       self._chunk_tensor(lowerCamelCase__			,lowerCamelCase__			)
							_UpperCamelCase :		Union[str, Any]			    =       self._scattered(lowerCamelCase__			,[n_queries, n_docs]			,target_type=torch.intaa			)
							_UpperCamelCase :		Union[str, Any]			    =       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 typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available


snake_case_			:     Any  	=  {
    'configuration_mvp': ['MVP_PRETRAINED_CONFIG_ARCHIVE_MAP', 'MvpConfig', 'MvpOnnxConfig'],
    'tokenization_mvp': ['MvpTokenizer'],
}

try:
					if not is_tokenizers_available():
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					pass
else:
					snake_case_			:     Dict  	=  ['MvpTokenizerFast']

try:
					if not is_torch_available():
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					pass
else:
					snake_case_			:     List[Any]  	=  [
					    'MVP_PRETRAINED_MODEL_ARCHIVE_LIST',
					    'MvpForCausalLM',
					    'MvpForConditionalGeneration',
					    'MvpForQuestionAnswering',
					    'MvpForSequenceClassification',
					    'MvpModel',
					    'MvpPreTrainedModel',
					]

if TYPE_CHECKING:
					from .configuration_mvp import MVP_PRETRAINED_CONFIG_ARCHIVE_MAP, MvpConfig, MvpOnnxConfig
					from .tokenization_mvp import MvpTokenizer

					try:
										if not is_tokenizers_available():
															raise OptionalDependencyNotAvailable()
					except OptionalDependencyNotAvailable:
										pass
					else:
										from .tokenization_mvp_fast import MvpTokenizerFast

					try:
										if not is_torch_available():
															raise OptionalDependencyNotAvailable()
					except OptionalDependencyNotAvailable:
										pass
					else:
										from .modeling_mvp import (
										    MVP_PRETRAINED_MODEL_ARCHIVE_LIST,
										    MvpForCausalLM,
										    MvpForConditionalGeneration,
										    MvpForQuestionAnswering,
										    MvpForSequenceClassification,
										    MvpModel,
										    MvpPreTrainedModel,
										)

else:
					import sys

					snake_case_			:     Optional[Any]  	=  _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)


