


'''simple docstring'''






import unittest

import torch

from diffusers import VQModel
from diffusers.utils import floats_tensor, torch_device
from diffusers.utils.testing_utils import enable_full_determinism

from .test_modeling_common import ModelTesterMixin, UNetTesterMixin


enable_full_determinism()






class  __magic_name__							(		lowerCAmelCase ,lowerCAmelCase ,unittest.TestCase    ):
       UpperCAmelCase										=VQModel
       UpperCAmelCase										="sample"






       @property
       def       lowerCAmelCase		(      self  ,							snake_case=(3_2, 3_2))						->  int:




          '''simple docstring'''
          _UpperCAmelCase       :       Optional[Any]							   =4
          _UpperCAmelCase       :       Optional[int]							   =3

          _UpperCAmelCase       :       Dict							   =floats_tensor((batch_size, num_channels) + sizes).to(snake_case)

          return {"sample": image}






       @property
       def       lowerCAmelCase		(      self)						->  Optional[int]:




          '''simple docstring'''
          return (3, 3_2, 3_2)






       @property
       def       lowerCAmelCase		(      self)						->  Dict:




          '''simple docstring'''
          return (3, 3_2, 3_2)






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




          '''simple docstring'''
          _UpperCAmelCase       :       List[Any]							   ={
              'block_out_channels': [3_2, 6_4],
              'in_channels': 3,
              'out_channels': 3,
              'down_block_types': ['DownEncoderBlock2D', 'DownEncoderBlock2D'],
              'up_block_types': ['UpDecoderBlock2D', 'UpDecoderBlock2D'],
              'latent_channels': 3,
          }
          _UpperCAmelCase       :       Optional[Any]							   =self.dummy_input
          return init_dict, inputs_dict






       def       lowerCAmelCase		(      self)						->  List[str]:




          '''simple docstring'''
          pass






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




          '''simple docstring'''
          pass






       def       lowerCAmelCase		(      self)						->  Any:




          '''simple docstring'''
          _UpperCAmelCase   ,	_UpperCAmelCase       :       int							   =VQModel.from_pretrained('fusing/vqgan-dummy'  ,							output_loading_info=snake_case)
          self.assertIsNotNone(snake_case)
          self.assertEqual(len(loading_info['missing_keys'])  ,							0)

          model.to(snake_case)
          _UpperCAmelCase       :       Union[str, Any]							   =model(**self.dummy_input)

          assert image is not None, "Make sure output is not None"






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




          '''simple docstring'''
          _UpperCAmelCase       :       str							   =VQModel.from_pretrained('fusing/vqgan-dummy')
          model.to(snake_case).eval()

          torch.manual_seed(0)
          if torch.cuda.is_available():
             torch.cuda.manual_seed_all(0)

          _UpperCAmelCase       :       Optional[int]							   =torch.randn(1  ,							model.config.in_channels  ,							model.config.sample_size  ,							model.config.sample_size)
          _UpperCAmelCase       :       str							   =image.to(snake_case)
          with torch.no_grad():
             _UpperCAmelCase       :       Optional[int]							   =model(snake_case).sample

          _UpperCAmelCase       :       int							   =output[0, -1, -3:, -3:].flatten().cpu()
          # fmt: off
          _UpperCAmelCase       :       Any							   =torch.tensor([-0.01_53, -0.40_44, -0.18_80, -0.51_61, -0.24_18, -0.40_72, -0.16_12, -0.06_33, -0.01_43])
          # fmt: on
          self.assertTrue(torch.allclose(snake_case  ,							snake_case  ,							atol=1E-3))





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

