



import logging
import random

import ray

from transformers import RagConfig, RagRetriever, RagTokenizer
from transformers.models.rag.retrieval_rag import CustomHFIndex


lowerCamelCase__     =	logging.getLogger(__name__)






class 				SCREAMING_SNAKE_CASE     :





							def __init__(				self						:						Optional[Any]		):

											'''simple docstring'''
											__a					       =						False





							def   UpperCamelCase_   (				self						:						List[Any] ,			__lowercase						:						Optional[int] ,			__lowercase						:						List[Any] ,			__lowercase						:						Union[str, Any] ,			__lowercase						:						Optional[int]		):

											'''simple docstring'''
											if not self.initialized:
															__a					       =						RagRetriever(
															    __lowercase ,			question_encoder_tokenizer=__lowercase ,			generator_tokenizer=__lowercase ,			index=__lowercase ,			init_retrieval=__lowercase ,			)
															__a					       =						True





							def   UpperCamelCase_   (				self						:						int		):

											'''simple docstring'''
											self.retriever.index.init_index()





							def   UpperCamelCase_   (				self						:						Any		):

											'''simple docstring'''
											# delete the old self.retriever object before assigning the new index
											del self.retriever
											__a					       =						False







							def   UpperCamelCase_   (				self						:						Optional[Any] ,			__lowercase						:						Optional[int] ,			__lowercase						:						Optional[Any]		):

											'''simple docstring'''
											__a       ,   __a					       =						self.retriever._main_retrieve(__lowercase ,			__lowercase		)
											__a					       =						self.retriever.index.get_doc_dicts(__lowercase		)
											return doc_ids, retrieved_doc_embeds, doc_dicts






class 				SCREAMING_SNAKE_CASE     (							lowerCamelCase__   ):





							def __init__(				self						:						List[str] ,			__lowercase						:						Optional[int] ,			__lowercase						:						Union[str, Any] ,			__lowercase						:						Optional[int] ,			__lowercase						:						Optional[int] ,			__lowercase						:						Union[str, Any]=None		):

											'''simple docstring'''
											if index is not None and index.is_initialized() and len(__lowercase		) > 0:
															raise ValueError(
															    """When using Ray for distributed fine-tuning, """
															    """you'll need to provide the paths instead, """
															    """as the dataset and the index are loaded """
															    """separately. More info in examples/rag/use_own_knowledge_dataset.py """		)

											super().__init__(
											    __lowercase ,			question_encoder_tokenizer=__lowercase ,			generator_tokenizer=__lowercase ,			index=__lowercase ,			init_retrieval=__lowercase ,			)

											__a					       =						retrieval_workers
											__a					       =						question_encoder_tokenizer
											__a					       =						generator_tokenizer
											if len(self.retrieval_workers		) > 0:
															ray.get(
															    [
															        worker.create_rag_retriever.remote(__lowercase ,			__lowercase ,			__lowercase ,			__lowercase		)
															        for worker in self.retrieval_workers
															    ]		)





							def   UpperCamelCase_   (				self						:						int		):

											'''simple docstring'''
											logger.info("""initializing retrieval"""		)

											if len(self.retrieval_workers		) > 0:
															ray.get([worker.init_retrieval.remote() for worker in self.retrieval_workers]		)
											else:
															# Non-distributed training. Load index into this same process.
															self.index.init_index()





							def   UpperCamelCase_   (				self						:						Dict ,			__lowercase						:						str ,			__lowercase						:						Any		):

											'''simple docstring'''
											if len(self.retrieval_workers		) > 0:
															# Select a random retrieval actor.
															__a					       =						self.retrieval_workers[random.randint(0 ,			len(self.retrieval_workers		) - 1		)]
															__a       ,   __a       ,   __a					       =						ray.get(
															    random_worker.retrieve.remote(__lowercase ,			__lowercase		)		)
											else:
															__a       ,   __a					       =						self._main_retrieve(__lowercase ,			__lowercase		)
															__a					       =						self.index.get_doc_dicts(__lowercase		)
											return retrieved_doc_embeds, doc_ids, doc_dicts





							@classmethod
							def   UpperCamelCase_   (				cls						:						Union[str, Any] ,			__lowercase						:						int ,			__lowercase						:						Any=None ,			**__lowercase						:						Union[str, Any]		):

											'''simple docstring'''
											return super(__lowercase ,			cls		).get_tokenizers(__lowercase ,			__lowercase ,			**__lowercase		)





							@classmethod
							def   UpperCamelCase_   (				cls						:						Any ,			__lowercase						:						Dict ,			__lowercase						:						Dict ,			__lowercase						:						str=None ,			**__lowercase						:						List[Any]		):

											'''simple docstring'''
											__a					       =						kwargs.pop("""config""" ,			__lowercase		) or RagConfig.from_pretrained(__lowercase ,			**__lowercase		)
											__a					       =						RagTokenizer.from_pretrained(__lowercase ,			config=__lowercase		)
											__a					       =						rag_tokenizer.question_encoder
											__a					       =						rag_tokenizer.generator

											if indexed_dataset is not None:
															__a					       =						"""custom"""
															__a					       =						CustomHFIndex(config.retrieval_vector_size ,			__lowercase		)
											else:
															__a					       =						cls._build_index(__lowercase		)

											return cls(
											    __lowercase ,			question_encoder_tokenizer=__lowercase ,			generator_tokenizer=__lowercase ,			retrieval_workers=__lowercase ,			index=__lowercase ,			)







							def   UpperCamelCase_   (				self						:						Dict		):

											'''simple docstring'''
											logger.info("""re-loading the new dataset with embeddings"""		)
											# access from the training loop

											ray.get([worker.clear_object.remote() for worker in self.retrieval_workers]		)

											# build the index object again
											__a					       =						self._build_index(self.config		)

											ray.get(
											    [
											        worker.create_rag_retriever.remote(
											            self.config ,			self.question_encoder_tokenizer ,			self.generator_tokenizer ,			__lowercase		)
											        for worker in self.retrieval_workers
											    ]		)





import copy

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


lowerCamelCase__     =	logging.get_logger(__name__)

lowerCamelCase__     =	{
    """facebook/sam-vit-huge""": """https://huggingface.co/facebook/sam-vit-huge/resolve/main/config.json""",
    """facebook/sam-vit-large""": """https://huggingface.co/facebook/sam-vit-large/resolve/main/config.json""",
    """facebook/sam-vit-base""": """https://huggingface.co/facebook/sam-vit-base/resolve/main/config.json""",
}






class 				SCREAMING_SNAKE_CASE     (							lowerCamelCase__   ):





							def __init__(				self						:						List[Any] ,			__lowercase						:						Optional[Any]=256 ,			__lowercase						:						Optional[int]=1024 ,			__lowercase						:						Any=16 ,			__lowercase						:						Optional[Any]=16 ,			__lowercase						:						Optional[Any]=4 ,			__lowercase						:						Optional[int]="gelu" ,			__lowercase						:						List[Any]=1E-6 ,			**__lowercase						:						str ,			):

											'''simple docstring'''
											super().__init__(**__lowercase		)
											__a					       =						hidden_size
											__a					       =						image_size
											__a					       =						patch_size
											__a					       =						image_size // patch_size
											__a					       =						mask_input_channels
											__a					       =						num_point_embeddings
											__a					       =						hidden_act
											__a					       =						layer_norm_eps






class 				SCREAMING_SNAKE_CASE     (							lowerCamelCase__   ):





							def __init__(				self						:						int ,			__lowercase						:						Tuple=256 ,			__lowercase						:						List[Any]="relu" ,			__lowercase						:						Any=2048 ,			__lowercase						:						List[Any]=2 ,			__lowercase						:						List[str]=8 ,			__lowercase						:						Any=2 ,			__lowercase						:						List[Any]=3 ,			__lowercase						:						str=3 ,			__lowercase						:						str=256 ,			__lowercase						:						Optional[Any]=1E-6 ,			**__lowercase						:						Tuple ,			):

											'''simple docstring'''
											super().__init__(**__lowercase		)
											__a					       =						hidden_size
											__a					       =						hidden_act
											__a					       =						mlp_dim
											__a					       =						num_hidden_layers
											__a					       =						num_attention_heads
											__a					       =						attention_downsample_rate
											__a					       =						num_multimask_outputs
											__a					       =						iou_head_depth
											__a					       =						iou_head_hidden_dim
											__a					       =						layer_norm_eps






class 				SCREAMING_SNAKE_CASE     (							lowerCamelCase__   ):





							def __init__(				self						:						List[Any] ,			__lowercase						:						List[Any]=768 ,			__lowercase						:						List[Any]=256 ,			__lowercase						:						Any=12 ,			__lowercase						:						Tuple=12 ,			__lowercase						:						Union[str, Any]=3 ,			__lowercase						:						Tuple=1024 ,			__lowercase						:						Any=16 ,			__lowercase						:						Dict="gelu" ,			__lowercase						:						List[Any]=1E-06 ,			__lowercase						:						List[str]=0.0 ,			__lowercase						:						Optional[Any]=1E-10 ,			__lowercase						:						Optional[Any]=True ,			__lowercase						:						str=4.0 ,			__lowercase						:						List[Any]=True ,			__lowercase						:						Dict=True ,			__lowercase						:						Optional[Any]=14 ,			__lowercase						:						Optional[int]=[2, 5, 8, 11] ,			__lowercase						:						List[str]=128 ,			__lowercase						:						Optional[int]=None ,			**__lowercase						:						List[Any] ,			):

											'''simple docstring'''
											super().__init__(**__lowercase		)

											__a					       =						hidden_size
											__a					       =						output_channels
											__a					       =						num_hidden_layers
											__a					       =						num_attention_heads
											__a					       =						num_channels
											__a					       =						image_size
											__a					       =						patch_size
											__a					       =						hidden_act
											__a					       =						layer_norm_eps
											__a					       =						attention_dropout
											__a					       =						initializer_range
											__a					       =						qkv_bias
											__a					       =						mlp_ratio
											__a					       =						use_abs_pos
											__a					       =						use_rel_pos
											__a					       =						window_size
											__a					       =						global_attn_indexes
											__a					       =						num_pos_feats
											__a					       =						int(hidden_size * mlp_ratio		) if mlp_dim is None else mlp_dim






class 				SCREAMING_SNAKE_CASE     (							lowerCamelCase__   ):
							__lowerCamelCase		:		Dict      ='sam'
							__lowerCamelCase		:		Any      =True





							def __init__(				self						:						List[str] ,			__lowercase						:						List[str]=None ,			__lowercase						:						Optional[int]=None ,			__lowercase						:						Dict=None ,			__lowercase						:						Optional[int]=0.02 ,			**__lowercase						:						List[str] ,			):

											'''simple docstring'''
											super().__init__(**__lowercase		)
											__a					       =						vision_config if vision_config is not None else {}
											__a					       =						prompt_encoder_config if prompt_encoder_config is not None else {}
											__a					       =						mask_decoder_config if mask_decoder_config is not None else {}

											if isinstance(__lowercase ,			__lowercase		):
															__a					       =						vision_config.to_dict()
											if isinstance(__lowercase ,			__lowercase		):
															__a					       =						prompt_encoder_config.to_dict()
											if isinstance(__lowercase ,			__lowercase		):
															__a					       =						mask_decoder_config.to_dict()

											__a					       =						SamVisionConfig(**__lowercase		)
											__a					       =						SamPromptEncoderConfig(**__lowercase		)
											__a					       =						SamMaskDecoderConfig(**__lowercase		)
											__a					       =						initializer_range







							def   UpperCamelCase_   (				self						:						Union[str, Any]		):

											'''simple docstring'''
											__a					       =						copy.deepcopy(self.__dict__		)
											__a					       =						self.vision_config.to_dict()
											__a					       =						self.prompt_encoder_config.to_dict()
											__a					       =						self.mask_decoder_config.to_dict()
											__a					       =						self.__class__.model_type
											return output
