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


__A	=					logging.getLogger(__name__)
class 			_snake_case  (       UpperCAmelCase__		):






						def __init__(							self   :      Optional[int]				,       UpperCAmelCase   :      Optional[Any]				,       UpperCAmelCase   :      Tuple				,       UpperCAmelCase   :      Tuple				,       UpperCAmelCase   :      Any=None     ):
													super().__init__(
													    _UpperCAmelCase				,       question_encoder_tokenizer=_UpperCAmelCase				,       generator_tokenizer=_UpperCAmelCase				,       index=_UpperCAmelCase				,       init_retrieval=_UpperCAmelCase				,       )
													__lowerCamelCase       :		Union[str, Any]		 =  None






						def    lowerCamelCase__  (							self   :      Optional[Any]				,       UpperCAmelCase   :      int     ):
													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
																				__lowerCamelCase       :		int		 =  self._infer_socket_ifname()
																				# avoid clash with the NCCL port
																				__lowerCamelCase       :		Any		 =  str(distributed_port + 1     )
																				__lowerCamelCase       :		str		 =  dist.new_group(ranks=_UpperCAmelCase				,       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   :      Optional[int]     ):
													return dist.get_rank(group=self.process_group     ) == 0






						def    lowerCamelCase__  (							self   :      List[str]				,       UpperCAmelCase   :      Union[str, Any]				,       UpperCAmelCase   :      str				,       UpperCAmelCase   :      Tuple=torch.floataa     ):
													__lowerCamelCase       :		Dict		 =  torch.empty(_UpperCAmelCase				,       dtype=_UpperCAmelCase     )
													dist.scatter(_UpperCAmelCase				,       src=0				,       scatter_list=_UpperCAmelCase				,       group=self.process_group     )
													return target_tensor






						def    lowerCamelCase__  (							self   :      List[Any]     ):
													__lowerCamelCase       :		Dict		 =  psutil.net_if_addrs()
													# a hacky way to deal with varying network interface names
													__lowerCamelCase       :		List[str]		 =  next((addr for addr in addrs if addr.startswith("e"     ))				,       _UpperCAmelCase     )
													return ifname







						def    lowerCamelCase__  (							self   :      Dict				,       UpperCAmelCase   :      np.ndarray				,       UpperCAmelCase   :      int     ):


													if not dist.is_initialized():
																				__lowerCamelCase ,	__lowerCamelCase       :		Tuple		 =  self._main_retrieve(_UpperCAmelCase				,       _UpperCAmelCase     )
																				return retrieved_doc_embeds, doc_ids, self.index.get_doc_dicts(_UpperCAmelCase     )

													# distributed training
													__lowerCamelCase       :		Any		 =  dist.get_world_size(group=self.process_group     )

													# gather logic
													__lowerCamelCase       :		Union[str, Any]		 =  None
													if self._is_main():
																				__lowerCamelCase       :		Any		 =  [torch.empty(question_hidden_states.shape				,       dtype=torch.floataa     ) for _ in range(_UpperCAmelCase     )]
													dist.gather(torch.tensor(_UpperCAmelCase     )				,       dst=0				,       gather_list=_UpperCAmelCase				,       group=self.process_group     )

													# scatter logic
													__lowerCamelCase       :		Tuple		 =  question_hidden_states.shape[0]
													__lowerCamelCase       :		str		 =  []
													__lowerCamelCase       :		int		 =  []
													if self._is_main():
																				assert len(_UpperCAmelCase     ) == world_size
																				__lowerCamelCase ,	__lowerCamelCase       :		Union[str, Any]		 =  self._main_retrieve(torch.cat(_UpperCAmelCase     ).numpy()				,       _UpperCAmelCase     )
																				__lowerCamelCase ,	__lowerCamelCase       :		Any		 =  torch.tensor(_UpperCAmelCase     ), torch.tensor(_UpperCAmelCase     )
																				__lowerCamelCase       :		Any		 =  self._chunk_tensor(_UpperCAmelCase				,       _UpperCAmelCase     )
																				__lowerCamelCase       :		Tuple		 =  self._chunk_tensor(_UpperCAmelCase				,       _UpperCAmelCase     )
													__lowerCamelCase       :		List[Any]		 =  self._scattered(_UpperCAmelCase				,       [n_queries, n_docs]				,       target_type=torch.intaa     )
													__lowerCamelCase       :		Dict		 =  self._scattered(_UpperCAmelCase				,       [n_queries, n_docs, question_hidden_states.shape[1]]     )

													return retrieved_doc_embeds.numpy(), doc_ids.numpy(), self.index.get_doc_dicts(_UpperCAmelCase     )



from collections import OrderedDict
from typing import Mapping

from packaging import version

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


A			:						Any        =  logging.get_logger(__name__)

A			:						Tuple        =  {
    'sail/poolformer_s12': 'https://huggingface.co/sail/poolformer_s12/resolve/main/config.json',
    # See all PoolFormer models at https://huggingface.co/models?filter=poolformer
}



class 				A       (			UpperCAmelCase__				):
					'''simple docstring'''






					A__     =					'''poolformer'''

					def __init__(self       :		Dict ,   _UpperCAmelCase       :		Union[str, Any]=3 ,   _UpperCAmelCase       :		str=16 ,   _UpperCAmelCase       :		Any=16 ,   _UpperCAmelCase       :		Union[str, Any]=3 ,   _UpperCAmelCase       :		Union[str, Any]=4.0 ,   _UpperCAmelCase       :		str=[2, 2, 6, 2] ,   _UpperCAmelCase       :		int=[64, 128, 320, 512] ,   _UpperCAmelCase       :		Union[str, Any]=[7, 3, 3, 3] ,   _UpperCAmelCase       :		List[Any]=[4, 2, 2, 2] ,   _UpperCAmelCase       :		Union[str, Any]=[2, 1, 1, 1] ,   _UpperCAmelCase       :		Optional[Any]=4 ,   _UpperCAmelCase       :		Dict=0.0 ,   _UpperCAmelCase       :		Union[str, Any]="gelu" ,   _UpperCAmelCase       :		Optional[Any]=True ,   _UpperCAmelCase       :		Tuple=1E-5 ,   _UpperCAmelCase       :		Tuple=0.02 ,   **_UpperCAmelCase       :		Union[str, Any] ,   )		->  Optional[int]:




							"""simple docstring"""


							lowercase__	     =						num_channels
							lowercase__	     =						patch_size
							lowercase__	     =						stride
							lowercase__	     =						padding
							lowercase__	     =						pool_size
							lowercase__	     =						hidden_sizes
							lowercase__	     =						mlp_ratio
							lowercase__	     =						depths
							lowercase__	     =						patch_sizes
							lowercase__	     =						strides
							lowercase__	     =						num_encoder_blocks
							lowercase__	     =						drop_path_rate
							lowercase__	     =						hidden_act
							lowercase__	     =						use_layer_scale
							lowercase__	     =						layer_scale_init_value
							lowercase__	     =						initializer_range
							super().__init__(**_UpperCAmelCase						)






class 				A       (			UpperCAmelCase__				):
					'''simple docstring'''






					A__     =					version.parse('''1.11'''				)

					@property
					def     lowerCamelCase__	(self       :		List[str]						)		->  Mapping[str, Mapping[int, str]]:




							"""simple docstring"""


							return OrderedDict(
							    [
							        ("""pixel_values""", {0: """batch""", 1: """num_channels""", 2: """height""", 3: """width"""}),
							    ]						)


					@property
					def     lowerCamelCase__	(self       :		Dict						)		->  float:




							"""simple docstring"""


							return 2E-3

