


'''simple docstring'''






from typing import Dict, List, Optional, Union

import numpy as np

from transformers.utils import is_vision_available
from transformers.utils.generic import TensorType

from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict
from ...image_transforms import (
    center_crop,
    get_resize_output_image_size,
    normalize,
    rescale,
    resize,
    to_channel_dimension_format,
)
from ...image_utils import (
    IMAGENET_STANDARD_MEAN,
    IMAGENET_STANDARD_STD,
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    is_valid_image,
    to_numpy_array,
    valid_images,
)
from ...utils import logging


if is_vision_available():
 import PIL

lowercase							=logging.get_logger(__name__)





def lowerCamelCase__      (						__lowerCamelCase	:							Dict							):







   '''simple docstring'''




   if isinstance(__lowerCamelCase	,   (list, tuple)							) and isinstance(videos[0]	,   (list, tuple)							) and is_valid_image(videos[0][0]							):
      return videos

   elif isinstance(__lowerCamelCase	,   (list, tuple)							) and is_valid_image(videos[0]							):
      return [videos]

   elif is_valid_image(__lowerCamelCase							):
      return [[videos]]

   raise ValueError(f"Could not make batched video from {videos}"							)






class  __magic_name__							(		lowerCAmelCase    ):
       UpperCAmelCase										=["pixel_values"]






       def __init__(      self  ,							snake_case = True  ,							snake_case = None  ,							snake_case = PILImageResampling.BILINEAR  ,							snake_case = True  ,							snake_case = None  ,							snake_case = True  ,							snake_case = 1 / 2_5_5  ,							snake_case = True  ,							snake_case = True  ,							snake_case = None  ,							snake_case = None  ,							**snake_case  ,							)						->  None:




          '''simple docstring'''
          super().__init__(**snake_case)
          _UpperCAmelCase       :       Any							   =size if size is not None else {'shortest_edge': 2_5_6}
          _UpperCAmelCase       :       List[str]							   =get_size_dict(snake_case  ,							default_to_square=snake_case)
          _UpperCAmelCase       :       Tuple							   =crop_size if crop_size is not None else {'height': 2_2_4, 'width': 2_2_4}
          _UpperCAmelCase       :       Optional[int]							   =get_size_dict(snake_case  ,							param_name='crop_size')

          _UpperCAmelCase       :       int							   =do_resize
          _UpperCAmelCase       :       Any							   =size
          _UpperCAmelCase       :       Optional[Any]							   =do_center_crop
          _UpperCAmelCase       :       List[str]							   =crop_size
          _UpperCAmelCase       :       List[Any]							   =resample
          _UpperCAmelCase       :       int							   =do_rescale
          _UpperCAmelCase       :       List[str]							   =rescale_factor
          _UpperCAmelCase       :       str							   =offset
          _UpperCAmelCase       :       int							   =do_normalize
          _UpperCAmelCase       :       Optional[int]							   =image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN
          _UpperCAmelCase       :       List[Any]							   =image_std if image_std is not None else IMAGENET_STANDARD_STD






       def       lowerCAmelCase		(      self  ,							snake_case  ,							snake_case  ,							snake_case = PILImageResampling.BILINEAR  ,							snake_case = None  ,							**snake_case  ,							)						->  np.ndarray:




          '''simple docstring'''
          _UpperCAmelCase       :       Union[str, Any]							   =get_size_dict(snake_case  ,							default_to_square=snake_case)
          if "shortest_edge" in size:
             _UpperCAmelCase       :       Union[str, Any]							   =get_resize_output_image_size(snake_case  ,							size['shortest_edge']  ,							default_to_square=snake_case)
          elif "height" in size and "width" in size:
             _UpperCAmelCase       :       Union[str, Any]							   =(size['height'], size['width'])
          else:
             raise ValueError(f"Size must have 'height' and 'width' or 'shortest_edge' as keys. Got {size.keys()}")
          return resize(snake_case  ,							size=snake_case  ,							resample=snake_case  ,							data_format=snake_case  ,							**snake_case)






       def       lowerCAmelCase		(      self  ,							snake_case  ,							snake_case  ,							snake_case = None  ,							**snake_case  ,							)						->  np.ndarray:




          '''simple docstring'''
          _UpperCAmelCase       :       Union[str, Any]							   =get_size_dict(snake_case)
          if "height" not in size or "width" not in size:
             raise ValueError(f"Size must have 'height' and 'width' as keys. Got {size.keys()}")
          return center_crop(snake_case  ,							size=(size['height'], size['width'])  ,							data_format=snake_case  ,							**snake_case)






       def       lowerCAmelCase		(      self  ,							snake_case  ,							snake_case  ,							snake_case = True  ,							snake_case = None  ,							**snake_case  ,							)						->  Tuple:




          '''simple docstring'''
          _UpperCAmelCase       :       Union[str, Any]							   =image.astype(np.floataa)
          if offset:
             _UpperCAmelCase       :       Tuple							   =image - (scale / 2)
          return rescale(snake_case  ,							scale=snake_case  ,							data_format=snake_case  ,							**snake_case)






       def       lowerCAmelCase		(      self  ,							snake_case  ,							snake_case  ,							snake_case  ,							snake_case = None  ,							**snake_case  ,							)						->  np.ndarray:




          '''simple docstring'''
          return normalize(snake_case  ,							mean=snake_case  ,							std=snake_case  ,							data_format=snake_case  ,							**snake_case)






       def       lowerCAmelCase		(      self  ,							snake_case  ,							snake_case = None  ,							snake_case = None  ,							snake_case = None  ,							snake_case = None  ,							snake_case = None  ,							snake_case = None  ,							snake_case = None  ,							snake_case = None  ,							snake_case = None  ,							snake_case = None  ,							snake_case = None  ,							snake_case = ChannelDimension.FIRST  ,							)						->  np.ndarray:




          '''simple docstring'''
          if do_resize and size is None or resample is None:
             raise ValueError('Size and resample must be specified if do_resize is True.')

          if do_center_crop and crop_size is None:
             raise ValueError('Crop size must be specified if do_center_crop is True.')

          if do_rescale and rescale_factor is None:
             raise ValueError('Rescale factor must be specified if do_rescale is True.')

          if do_normalize and (image_mean is None or image_std is None):
             raise ValueError('Image mean and std must be specified if do_normalize is True.')

          if offset and not do_rescale:
             raise ValueError('For offset, do_rescale must also be set to True.')

          # All transformations expect numpy arrays.
          _UpperCAmelCase       :       Union[str, Any]							   =to_numpy_array(snake_case)

          if do_resize:
             _UpperCAmelCase       :       Union[str, Any]							   =self.resize(image=snake_case  ,							size=snake_case  ,							resample=snake_case)

          if do_center_crop:
             _UpperCAmelCase       :       Any							   =self.center_crop(snake_case  ,							size=snake_case)

          if do_rescale:
             _UpperCAmelCase       :       Dict							   =self.rescale(image=snake_case  ,							scale=snake_case  ,							offset=snake_case)

          if do_normalize:
             _UpperCAmelCase       :       Dict							   =self.normalize(image=snake_case  ,							mean=snake_case  ,							std=snake_case)

          _UpperCAmelCase       :       Dict							   =to_channel_dimension_format(snake_case  ,							snake_case)
          return image






       def       lowerCAmelCase		(      self  ,							snake_case  ,							snake_case = None  ,							snake_case = None  ,							snake_case = None  ,							snake_case = None  ,							snake_case = None  ,							snake_case = None  ,							snake_case = None  ,							snake_case = None  ,							snake_case = None  ,							snake_case = None  ,							snake_case = None  ,							snake_case = None  ,							snake_case = ChannelDimension.FIRST  ,							**snake_case  ,							)						->  PIL.Image.Image:




          '''simple docstring'''
          _UpperCAmelCase       :       Optional[int]							   =do_resize if do_resize is not None else self.do_resize
          _UpperCAmelCase       :       Union[str, Any]							   =resample if resample is not None else self.resample
          _UpperCAmelCase       :       Dict							   =do_center_crop if do_center_crop is not None else self.do_center_crop
          _UpperCAmelCase       :       Optional[int]							   =do_rescale if do_rescale is not None else self.do_rescale
          _UpperCAmelCase       :       Dict							   =rescale_factor if rescale_factor is not None else self.rescale_factor
          _UpperCAmelCase       :       str							   =offset if offset is not None else self.offset
          _UpperCAmelCase       :       Dict							   =do_normalize if do_normalize is not None else self.do_normalize
          _UpperCAmelCase       :       Union[str, Any]							   =image_mean if image_mean is not None else self.image_mean
          _UpperCAmelCase       :       Union[str, Any]							   =image_std if image_std is not None else self.image_std

          _UpperCAmelCase       :       List[str]							   =size if size is not None else self.size
          _UpperCAmelCase       :       Tuple							   =get_size_dict(snake_case  ,							default_to_square=snake_case)
          _UpperCAmelCase       :       str							   =crop_size if crop_size is not None else self.crop_size
          _UpperCAmelCase       :       int							   =get_size_dict(snake_case  ,							param_name='crop_size')

          if not valid_images(snake_case):
             raise ValueError(
                 'Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, '
                 'torch.Tensor, tf.Tensor or jax.ndarray.')

          _UpperCAmelCase       :       List[Any]							   =make_batched(snake_case)

          _UpperCAmelCase       :       str							   =[
              [
                  self._preprocess_image(
                      image=snake_case  ,							do_resize=snake_case  ,							size=snake_case  ,							resample=snake_case  ,							do_center_crop=snake_case  ,							crop_size=snake_case  ,							do_rescale=snake_case  ,							rescale_factor=snake_case  ,							offset=snake_case  ,							do_normalize=snake_case  ,							image_mean=snake_case  ,							image_std=snake_case  ,							data_format=snake_case  ,							)
                  for img in video
              ]
              for video in videos
          ]

          _UpperCAmelCase       :       Optional[int]							   ={'pixel_values': videos}
          return BatchFeature(data=snake_case  ,							tensor_type=snake_case)





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

