




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


SCREAMING_SNAKE_CASE    =	logging.getLogger(__name__)




class      UpperCAmelCase_  (						A_							):




    def __init__(   self						:	List[str]     ,			snake_case_						:	Optional[Any]     ,			snake_case_						:	Any     ,			snake_case_						:	Optional[Any]     ,			snake_case_						:	Union[str, Any]=None				)				->							Tuple:




     '''simple docstring'''

     super().__init__(
         snake_case_     ,			question_encoder_tokenizer=snake_case_     ,			generator_tokenizer=snake_case_     ,			index=snake_case_     ,			init_retrieval=snake_case_     ,			)
     A__	=    None




    def 							__magic_name__							(   self						:	Optional[int]     ,			snake_case_						:	int				)				->							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
      A__	=    self._infer_socket_ifname()
      # avoid clash with the NCCL port
      A__	=    str(distributed_port + 1				)
      A__	=    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 							__magic_name__							(   self						:	Tuple				)				->							List[Any]:




     '''simple docstring'''

     return dist.get_rank(group=self.process_group				) == 0




    def 							__magic_name__							(   self						:	str     ,			snake_case_						:	str     ,			snake_case_						:	Union[str, Any]     ,			snake_case_						:	List[str]=torch.floataa				)				->							int:




     '''simple docstring'''

     A__	=    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 							__magic_name__							(   self						:	Union[str, Any]				)				->							Any:




     '''simple docstring'''

     A__	=    psutil.net_if_addrs()
     # a hacky way to deal with varying network interface names
     A__	=    next((addr for addr in addrs if addr.startswith("e"				))     ,			snake_case_				)
     return ifname




    def 							__magic_name__							(   self						:	Tuple     ,			snake_case_						:	np.ndarray     ,			snake_case_						:	int				)				->							Tuple[np.ndarray, List[dict]]:




     '''simple docstring'''

     if not dist.is_initialized():
      A__,				A__	=    self._main_retrieve(snake_case_     ,			snake_case_				)
      return retrieved_doc_embeds, doc_ids, self.index.get_doc_dicts(snake_case_				)

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

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











"""simple docstring"""



import shutil
import tempfile
import unittest

import numpy as np
import pytest

from transformers import is_speech_available, is_vision_available
from transformers.testing_utils import require_torch


if is_vision_available():
      from transformers import TvltImageProcessor

if is_speech_available():
      from transformers import TvltFeatureExtractor

from transformers import TvltProcessor




@require_torch
class      UpperCAmelCase_  (						unittest.TestCase							):




    def 							__magic_name__							(   self						:	str				)				->							Dict:




     '''simple docstring'''

     A__	=    "ZinengTang/tvlt-base"
     A__	=    tempfile.mkdtemp()




    def 							__magic_name__							(   self						:	int     ,			**snake_case_						:	Dict				)				->							Union[str, Any]:




     '''simple docstring'''

     return TvltImageProcessor.from_pretrained(self.checkpoint     ,			**snake_case_				)




    def 							__magic_name__							(   self						:	Optional[int]     ,			**snake_case_						:	str				)				->							List[str]:




     '''simple docstring'''

     return TvltFeatureExtractor.from_pretrained(self.checkpoint     ,			**snake_case_				)




    def 							__magic_name__							(   self						:	Optional[int]				)				->							str:




     '''simple docstring'''

     shutil.rmtree(self.tmpdirname				)




    def 							__magic_name__							(   self						:	Tuple				)				->							Any:




     '''simple docstring'''

     A__	=    self.get_image_processor()
     A__	=    self.get_feature_extractor()

     A__	=    TvltProcessor(image_processor=snake_case_     ,			feature_extractor=snake_case_				)
     processor.save_pretrained(self.tmpdirname				)
     A__	=    TvltProcessor.from_pretrained(self.tmpdirname				)

     self.assertIsInstance(processor.feature_extractor     ,			snake_case_				)
     self.assertIsInstance(processor.image_processor     ,			snake_case_				)




    def 							__magic_name__							(   self						:	Optional[Any]				)				->							Union[str, Any]:




     '''simple docstring'''

     A__	=    self.get_image_processor()
     A__	=    self.get_feature_extractor()

     A__	=    TvltProcessor(image_processor=snake_case_     ,			feature_extractor=snake_case_				)

     A__	=    np.ones([12_000]				)

     A__	=    feature_extractor(snake_case_     ,			return_tensors="np"				)
     A__	=    processor(audio=snake_case_     ,			return_tensors="np"				)

     for key in audio_dict.keys():
      self.assertAlmostEqual(audio_dict[key].sum()     ,			input_processor[key].sum()     ,			delta=1e-2				)




    def 							__magic_name__							(   self						:	Tuple				)				->							Tuple:




     '''simple docstring'''

     A__	=    self.get_image_processor()
     A__	=    self.get_feature_extractor()

     A__	=    TvltProcessor(image_processor=snake_case_     ,			feature_extractor=snake_case_				)

     A__	=    np.ones([3, 224, 224]				)

     A__	=    image_processor(snake_case_     ,			return_tensors="np"				)
     A__	=    processor(images=snake_case_     ,			return_tensors="np"				)

     for key in image_dict.keys():
      self.assertAlmostEqual(image_dict[key].sum()     ,			input_processor[key].sum()     ,			delta=1e-2				)




    def 							__magic_name__							(   self						:	Tuple				)				->							Optional[int]:




     '''simple docstring'''

     A__	=    self.get_image_processor()
     A__	=    self.get_feature_extractor()

     A__	=    TvltProcessor(image_processor=snake_case_     ,			feature_extractor=snake_case_				)

     A__	=    np.ones([12_000]				)
     A__	=    np.ones([3, 224, 224]				)

     A__	=    processor(audio=snake_case_     ,			images=snake_case_				)

     self.assertListEqual(list(inputs.keys()				)     ,			["audio_values", "audio_mask", "pixel_values", "pixel_mask"]				)

     # test if it raises when no input is passed
     with pytest.raises(snake_case_				):
      processor()




    def 							__magic_name__							(   self						:	Optional[Any]				)				->							Tuple:




     '''simple docstring'''

     A__	=    self.get_image_processor()
     A__	=    self.get_feature_extractor()

     A__	=    TvltProcessor(image_processor=snake_case_     ,			feature_extractor=snake_case_				)

     self.assertListEqual(
         processor.model_input_names     ,			image_processor.model_input_names + feature_extractor.model_input_names     ,			msg="`processor` and `image_processor`+`feature_extractor` model input names do not match"     ,			)





