


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  _a							:




	def __init__( self						:		str		)->	Any:
			lowerCAmelCase__       :      Any   					= False




	def 							UpperCAmelCase__( self						:		Tuple		, _SCREAMING_SNAKE_CASE						:		List[Any]		, _SCREAMING_SNAKE_CASE						:		Union[str, Any]		, _SCREAMING_SNAKE_CASE						:		Optional[Any]		, _SCREAMING_SNAKE_CASE						:		List[str]		)->	List[str]:


			if not self.initialized:
					lowerCAmelCase__       :      Optional[int]   					= RagRetriever(
					    __UpperCAmelCase		, question_encoder_tokenizer=__UpperCAmelCase		, generator_tokenizer=__UpperCAmelCase		, index=__UpperCAmelCase		, init_retrieval=__UpperCAmelCase		, )
					lowerCAmelCase__       :      str   					= True




	def 							UpperCAmelCase__( self						:		Any		)->	Tuple:
			self.retriever.index.init_index()




	def 							UpperCAmelCase__( self						:		Union[str, Any]		, _SCREAMING_SNAKE_CASE						:		str		, _SCREAMING_SNAKE_CASE						:		Tuple		)->	List[str]:
			lowerCAmelCase__					,     lowerCAmelCase__       :      List[Any]   					= self.retriever._main_retrieve(__UpperCAmelCase		, __UpperCAmelCase		)
			return doc_ids, retrieved_doc_embeds





class  _a							(    _lowercase):




	def __init__( self						:		int		, _SCREAMING_SNAKE_CASE						:		Tuple		, _SCREAMING_SNAKE_CASE						:		Tuple		, _SCREAMING_SNAKE_CASE						:		Dict		, _SCREAMING_SNAKE_CASE						:		Dict		, _SCREAMING_SNAKE_CASE						:		Dict=None		)->	Optional[Any]:


			if index is not None and index.is_initialized() and len(__UpperCAmelCase		) > 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__(
			    __UpperCAmelCase		, question_encoder_tokenizer=__UpperCAmelCase		, generator_tokenizer=__UpperCAmelCase		, index=__UpperCAmelCase		, init_retrieval=__UpperCAmelCase		, )
			lowerCAmelCase__       :      int   					= retrieval_workers
			if len(self.retrieval_workers		) > 0:
					ray.get(
					    [
					        worker.create_rag_retriever.remote(__UpperCAmelCase		, __UpperCAmelCase		, __UpperCAmelCase		, __UpperCAmelCase		)
					        for worker in self.retrieval_workers
					    ]		)




	def 							UpperCAmelCase__( self						:		List[Any]		)->	Tuple:
			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						:		Optional[Any]		, _SCREAMING_SNAKE_CASE						:		Any		, _SCREAMING_SNAKE_CASE						:		Tuple		)->	Dict:


			if len(self.retrieval_workers		) > 0:
					# Select a random retrieval actor.
					lowerCAmelCase__       :      List[str]   					= self.retrieval_workers[random.randint(0		, len(self.retrieval_workers		) - 1		)]
					lowerCAmelCase__					,     lowerCAmelCase__       :      Union[str, Any]   					= ray.get(random_worker.retrieve.remote(__UpperCAmelCase		, __UpperCAmelCase		)		)
			else:
					lowerCAmelCase__					,     lowerCAmelCase__       :      Dict   					= self._main_retrieve(__UpperCAmelCase		, __UpperCAmelCase		)
			return retrieved_doc_embeds, doc_ids, self.index.get_doc_dicts(__UpperCAmelCase		)




	@classmethod
	def 							UpperCAmelCase__( cls						:		List[str]		, _SCREAMING_SNAKE_CASE						:		Union[str, Any]		, _SCREAMING_SNAKE_CASE						:		Tuple=None		, **_SCREAMING_SNAKE_CASE						:		Optional[int]		)->	List[Any]:
			return super(__UpperCAmelCase		, cls		).get_tokenizers(__UpperCAmelCase		, __UpperCAmelCase		, **__UpperCAmelCase		)




	@classmethod
	def 							UpperCAmelCase__( cls						:		Optional[int]		, _SCREAMING_SNAKE_CASE						:		int		, _SCREAMING_SNAKE_CASE						:		List[str]		, _SCREAMING_SNAKE_CASE						:		Optional[Any]=None		, **_SCREAMING_SNAKE_CASE						:		int		)->	str:
			lowerCAmelCase__       :      List[Any]   					= kwargs.pop('''config'''		, __UpperCAmelCase		) or RagConfig.from_pretrained(__UpperCAmelCase		, **__UpperCAmelCase		)
			lowerCAmelCase__       :      int   					= RagTokenizer.from_pretrained(__UpperCAmelCase		, config=__UpperCAmelCase		)
			lowerCAmelCase__       :      Any   					= rag_tokenizer.question_encoder
			lowerCAmelCase__       :      int   					= rag_tokenizer.generator
			if indexed_dataset is not None:
					lowerCAmelCase__       :      List[Any]   					= '''custom'''
					lowerCAmelCase__       :      List[str]   					= CustomHFIndex(config.retrieval_vector_size		, __UpperCAmelCase		)
			else:
					lowerCAmelCase__       :      str   					= cls._build_index(__UpperCAmelCase		)
			return cls(
			    __UpperCAmelCase		, question_encoder_tokenizer=__UpperCAmelCase		, generator_tokenizer=__UpperCAmelCase		, retrieval_workers=__UpperCAmelCase		, index=__UpperCAmelCase		, )








from collections import OrderedDict
from typing import Mapping

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


lowerCamelCase						=		logging.get_logger(__name__)

lowerCamelCase						=		{
    '''YituTech/conv-bert-base''': '''https://huggingface.co/YituTech/conv-bert-base/resolve/main/config.json''',
    '''YituTech/conv-bert-medium-small''': (
        '''https://huggingface.co/YituTech/conv-bert-medium-small/resolve/main/config.json'''
    ),
    '''YituTech/conv-bert-small''': '''https://huggingface.co/YituTech/conv-bert-small/resolve/main/config.json''',
    # See all ConvBERT models at https://huggingface.co/models?filter=convbert
}





class  _a							(    _lowercase):
	_a  :  Dict				   =							'''convbert'''




	def __init__( self						:		Union[str, Any]		, _SCREAMING_SNAKE_CASE						:		str=3_0522		, _SCREAMING_SNAKE_CASE						:		Union[str, Any]=768		, _SCREAMING_SNAKE_CASE						:		Tuple=12		, _SCREAMING_SNAKE_CASE						:		str=12		, _SCREAMING_SNAKE_CASE						:		Any=3072		, _SCREAMING_SNAKE_CASE						:		List[Any]="gelu"		, _SCREAMING_SNAKE_CASE						:		Dict=0.1		, _SCREAMING_SNAKE_CASE						:		List[str]=0.1		, _SCREAMING_SNAKE_CASE						:		int=512		, _SCREAMING_SNAKE_CASE						:		Any=2		, _SCREAMING_SNAKE_CASE						:		Optional[int]=0.02		, _SCREAMING_SNAKE_CASE						:		Optional[int]=1E-12		, _SCREAMING_SNAKE_CASE						:		Union[str, Any]=1		, _SCREAMING_SNAKE_CASE						:		Optional[int]=0		, _SCREAMING_SNAKE_CASE						:		Dict=2		, _SCREAMING_SNAKE_CASE						:		Union[str, Any]=768		, _SCREAMING_SNAKE_CASE						:		Optional[Any]=2		, _SCREAMING_SNAKE_CASE						:		Tuple=9		, _SCREAMING_SNAKE_CASE						:		int=1		, _SCREAMING_SNAKE_CASE						:		Tuple=None		, **_SCREAMING_SNAKE_CASE						:		List[str]		, )->	List[str]:
			super().__init__(
			    pad_token_id=_SCREAMING_SNAKE_CASE		, bos_token_id=_SCREAMING_SNAKE_CASE		, eos_token_id=_SCREAMING_SNAKE_CASE		, **_SCREAMING_SNAKE_CASE		, )

			lowerCAmelCase__       :      Union[str, Any]   					= vocab_size
			lowerCAmelCase__       :      List[Any]   					= hidden_size
			lowerCAmelCase__       :      Tuple   					= num_hidden_layers
			lowerCAmelCase__       :      Union[str, Any]   					= num_attention_heads
			lowerCAmelCase__       :      Optional[Any]   					= intermediate_size
			lowerCAmelCase__       :      Tuple   					= hidden_act
			lowerCAmelCase__       :      Dict   					= hidden_dropout_prob
			lowerCAmelCase__       :      str   					= attention_probs_dropout_prob
			lowerCAmelCase__       :      str   					= max_position_embeddings
			lowerCAmelCase__       :      Tuple   					= type_vocab_size
			lowerCAmelCase__       :      Optional[Any]   					= initializer_range
			lowerCAmelCase__       :      List[Any]   					= layer_norm_eps
			lowerCAmelCase__       :      int   					= embedding_size
			lowerCAmelCase__       :      Union[str, Any]   					= head_ratio
			lowerCAmelCase__       :      Optional[int]   					= conv_kernel_size
			lowerCAmelCase__       :      List[str]   					= num_groups
			lowerCAmelCase__       :      Dict   					= classifier_dropout





class  _a							(    _lowercase):




	@property
	def 							UpperCAmelCase__( self						:		Tuple		)->	Mapping[str, Mapping[int, str]]:
			if self.task == "multiple-choice":
					lowerCAmelCase__       :      Optional[Any]   					= {0: '''batch''', 1: '''choice''', 2: '''sequence'''}
			else:
					lowerCAmelCase__       :      List[str]   					= {0: '''batch''', 1: '''sequence'''}
			return OrderedDict(
			    [
			        ('''input_ids''', dynamic_axis),
			        ('''attention_mask''', dynamic_axis),
			        ('''token_type_ids''', dynamic_axis),
			    ]		)




