





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


UpperCAmelCase_ 							=       logging.getLogger(__name__)
class      UpperCamelCase_						(					_lowerCamelCase   ):




     def __init__(						self			,  lowerCAmelCase_			,  lowerCAmelCase_			,  lowerCAmelCase_			,  lowerCAmelCase_=None					)						->							Optional[int]:
          super().__init__(
              lowerCAmelCase_			,  question_encoder_tokenizer=lowerCAmelCase_			,  generator_tokenizer=lowerCAmelCase_			,  index=lowerCAmelCase_			,  init_retrieval=lowerCAmelCase_			,  )
          _snake_case													=			None




     def 		lowerCAmelCase     (						self			,  lowerCAmelCase_					)						->							Tuple:
          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 		lowerCAmelCase     (						self					)						->							Tuple:
          return dist.get_rank(group=self.process_group					) == 0




     def 		lowerCAmelCase     (						self			,  lowerCAmelCase_			,  lowerCAmelCase_			,  lowerCAmelCase_=torch.floataa					)						->							List[str]:
          _snake_case													=			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													=			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 		lowerCAmelCase     (						self			,  lowerCAmelCase_			,  lowerCAmelCase_					)						->							Tuple[np.ndarray, List[dict]]:

          # 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_					)










import warnings

from ...utils import logging
from .image_processing_flava import FlavaImageProcessor


UpperCAmelCase_ 							=       logging.get_logger(__name__)
class      UpperCamelCase_						(					_lowerCamelCase   ):




					def __init__(						self			,  *lowerCAmelCase_			,  **lowerCAmelCase_					)						->							None:
										warnings.warn(
										    'The class FlavaFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please'
										    ' use FlavaImageProcessor instead.'			,  lowerCAmelCase_			,  )
										super().__init__(*lowerCAmelCase_			,  **lowerCAmelCase_					)



