


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


lowerCAmelCase	=							logging.getLogger(__name__)


class 	_a							(      lowerCamelCase__      ):

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





									"""simple docstring"""







									super().__init__(
									    snake_case__   ,						question_encoder_tokenizer=snake_case__   ,						generator_tokenizer=snake_case__   ,						index=snake_case__   ,						init_retrieval=snake_case__   ,						)
									lowercase__						=				None

							def 				lowerCamelCase_			(     self:     Tuple   ,						UpperCamelCase_:     Optional[Any]							)      ->       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
											lowercase__						=				self._infer_socket_ifname()
											# avoid clash with the NCCL port
											lowercase__						=				str(distributed_port + 1							)
											lowercase__						=				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:     Union[str, Any]							)      ->       Dict:





									"""simple docstring"""







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

							def 				lowerCamelCase_			(     self:     Any   ,						UpperCamelCase_:     List[str]   ,						UpperCamelCase_:     List[str]   ,						UpperCamelCase_:     Dict=torch.floataa							)      ->       int:





									"""simple docstring"""







									lowercase__						=				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:     Tuple							)      ->       str:





									"""simple docstring"""







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


							def 				lowerCamelCase_			(     self:     Tuple   ,						UpperCamelCase_:     Dict   ,						UpperCamelCase_:     Dict							)      ->       Tuple[np.ndarray, List[dict]]:





									"""simple docstring"""







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

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

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






import copy
import os
from typing import Union

from ...configuration_utils import PretrainedConfig
from ...utils import logging


lowerCAmelCase	=							logging.get_logger(__name__)

lowerCAmelCase	=							{
    'microsoft/git-base': 'https://huggingface.co/microsoft/git-base/resolve/main/config.json',
}


class 	_a							(      UpperCamelCase__      ):
							_lowercase     :      int  				=			'''git_vision_model'''

							def __init__(							self:     Tuple   ,						UpperCamelCase_:     Optional[int]=768   ,						UpperCamelCase_:     Optional[int]=3_072   ,						UpperCamelCase_:     Optional[int]=12   ,						UpperCamelCase_:     Union[str, Any]=12   ,						UpperCamelCase_:     Any=3   ,						UpperCamelCase_:     str=224   ,						UpperCamelCase_:     int=16   ,						UpperCamelCase_:     Any="quick_gelu"   ,						UpperCamelCase_:     Union[str, Any]=1E-5   ,						UpperCamelCase_:     Tuple=0.0   ,						UpperCamelCase_:     Tuple=0.02   ,						**UpperCamelCase_:     str   ,						)      ->       Optional[int]:





									"""simple docstring"""







									super().__init__(**UpperCamelCase_							)

									lowercase__						=				hidden_size
									lowercase__						=				intermediate_size
									lowercase__						=				num_hidden_layers
									lowercase__						=				num_attention_heads
									lowercase__						=				num_channels
									lowercase__						=				patch_size
									lowercase__						=				image_size
									lowercase__						=				initializer_range
									lowercase__						=				attention_dropout
									lowercase__						=				layer_norm_eps
									lowercase__						=				hidden_act


							@classmethod
							def 				lowerCamelCase_			(							cls:     Optional[Any]   ,						UpperCamelCase_:     Union[str, os.PathLike]   ,						**UpperCamelCase_:     List[Any]							)      ->       "PretrainedConfig":





									"""simple docstring"""







									cls._set_token_in_kwargs(UpperCamelCase_							)

									lowercase__					,		lowercase__						=				cls.get_config_dict(UpperCamelCase_   ,						**UpperCamelCase_							)

									# get the vision config dict if we are loading from GITConfig
									if config_dict.get('''model_type'''							) == "git":
											lowercase__						=				config_dict['''vision_config''']

									if "model_type" in config_dict and hasattr(cls   ,						'''model_type'''							) and config_dict["model_type"] != cls.model_type:
											logger.warning(
											    f'You are using a model of type {config_dict["model_type"]} to instantiate a model of type '
											    f'{cls.model_type}. This is not supported for all configurations of models and can yield errors.'							)

									return cls.from_dict(UpperCamelCase_   ,						**UpperCamelCase_							)


class 	_a							(      UpperCamelCase__      ):
							_lowercase     :      Optional[int]  				=			'''git'''

							def __init__(							self:     Optional[Any]   ,						UpperCamelCase_:     Optional[Any]=None   ,						UpperCamelCase_:     List[str]=30_522   ,						UpperCamelCase_:     List[Any]=768   ,						UpperCamelCase_:     List[Any]=6   ,						UpperCamelCase_:     Union[str, Any]=12   ,						UpperCamelCase_:     Any=3_072   ,						UpperCamelCase_:     Any="gelu"   ,						UpperCamelCase_:     List[Any]=0.1   ,						UpperCamelCase_:     Dict=0.1   ,						UpperCamelCase_:     str=1_024   ,						UpperCamelCase_:     Any=0.02   ,						UpperCamelCase_:     str=1E-1_2   ,						UpperCamelCase_:     Optional[int]=0   ,						UpperCamelCase_:     Optional[Any]="absolute"   ,						UpperCamelCase_:     Optional[int]=True   ,						UpperCamelCase_:     List[Any]=False   ,						UpperCamelCase_:     List[Any]=101   ,						UpperCamelCase_:     int=102   ,						UpperCamelCase_:     List[str]=None   ,						**UpperCamelCase_:     int   ,						)      ->       Union[str, Any]:





									"""simple docstring"""







									super().__init__(bos_token_id=UpperCamelCase_   ,						eos_token_id=UpperCamelCase_   ,						pad_token_id=UpperCamelCase_   ,						**UpperCamelCase_							)

									if vision_config is None:
											lowercase__						=				{}
											logger.info('''vision_config is None. initializing the GitVisionConfig with default values.'''							)

									lowercase__						=				GitVisionConfig(**UpperCamelCase_							)
									lowercase__						=				vocab_size
									lowercase__						=				hidden_size
									lowercase__						=				num_hidden_layers
									lowercase__						=				num_attention_heads
									lowercase__						=				hidden_act
									lowercase__						=				intermediate_size
									lowercase__						=				hidden_dropout_prob
									lowercase__						=				attention_probs_dropout_prob
									lowercase__						=				max_position_embeddings
									lowercase__						=				initializer_range
									lowercase__						=				layer_norm_eps
									lowercase__						=				position_embedding_type
									lowercase__						=				use_cache
									lowercase__						=				tie_word_embeddings
									lowercase__						=				num_image_with_embedding

									lowercase__						=				bos_token_id
									lowercase__						=				eos_token_id


							def 				lowerCamelCase_			(							self:     Dict							)      ->       Tuple:





									"""simple docstring"""







									lowercase__						=				copy.deepcopy(self.__dict__							)
									lowercase__						=				self.vision_config.to_dict()
									lowercase__						=				self.__class__.model_type
									return output


