


from ...configuration_utils import PretrainedConfig


UpperCAmelCase_      : Any  					=       {
    'google/tapas-base-finetuned-sqa': (
        'https://huggingface.co/google/tapas-base-finetuned-sqa/resolve/main/config.json'
    ),
    'google/tapas-base-finetuned-wtq': (
        'https://huggingface.co/google/tapas-base-finetuned-wtq/resolve/main/config.json'
    ),
    'google/tapas-base-finetuned-wikisql-supervised': (
        'https://huggingface.co/google/tapas-base-finetuned-wikisql-supervised/resolve/main/config.json'
    ),
    'google/tapas-base-finetuned-tabfact': (
        'https://huggingface.co/google/tapas-base-finetuned-tabfact/resolve/main/config.json'
    ),
}
class  SCREAMING_SNAKE_CASE__  (				lowercase__			):
      snake_case__				:     List[Any]        =						'''tapas'''
      def __init__(       self				:       Optional[int]     ,							SCREAMING_SNAKE_CASE__				:       int=3_0_5_2_2     ,							SCREAMING_SNAKE_CASE__				:       Union[str, Any]=7_6_8     ,							SCREAMING_SNAKE_CASE__				:       Union[str, Any]=1_2     ,							SCREAMING_SNAKE_CASE__				:       List[Any]=1_2     ,							SCREAMING_SNAKE_CASE__				:       Union[str, Any]=3_0_7_2     ,							SCREAMING_SNAKE_CASE__				:       Optional[Any]="gelu"     ,							SCREAMING_SNAKE_CASE__				:       int=0.1     ,							SCREAMING_SNAKE_CASE__				:       Union[str, Any]=0.1     ,							SCREAMING_SNAKE_CASE__				:       Tuple=1_0_2_4     ,							SCREAMING_SNAKE_CASE__				:       List[str]=[3, 2_5_6, 2_5_6, 2, 2_5_6, 2_5_6, 1_0]     ,							SCREAMING_SNAKE_CASE__				:       str=0.02     ,							SCREAMING_SNAKE_CASE__				:       Union[str, Any]=1E-12     ,							SCREAMING_SNAKE_CASE__				:       Optional[int]=0     ,							SCREAMING_SNAKE_CASE__				:       List[Any]=10.0     ,							SCREAMING_SNAKE_CASE__				:       Optional[int]=0     ,							SCREAMING_SNAKE_CASE__				:       Optional[int]=1.0     ,							SCREAMING_SNAKE_CASE__				:       List[Any]=None     ,							SCREAMING_SNAKE_CASE__				:       List[str]=1.0     ,							SCREAMING_SNAKE_CASE__				:       List[Any]=False     ,							SCREAMING_SNAKE_CASE__				:       List[Any]=None     ,							SCREAMING_SNAKE_CASE__				:       int=1.0     ,							SCREAMING_SNAKE_CASE__				:       int=1.0     ,							SCREAMING_SNAKE_CASE__				:       Tuple=False     ,							SCREAMING_SNAKE_CASE__				:       Union[str, Any]=False     ,							SCREAMING_SNAKE_CASE__				:       List[Any]="ratio"     ,							SCREAMING_SNAKE_CASE__				:       Union[str, Any]=None     ,							SCREAMING_SNAKE_CASE__				:       Dict=None     ,							SCREAMING_SNAKE_CASE__				:       List[str]=6_4     ,							SCREAMING_SNAKE_CASE__				:       Union[str, Any]=3_2     ,							SCREAMING_SNAKE_CASE__				:       List[Any]=False     ,							SCREAMING_SNAKE_CASE__				:       Any=True     ,							SCREAMING_SNAKE_CASE__				:       Tuple=False     ,							SCREAMING_SNAKE_CASE__				:       Union[str, Any]=False     ,							SCREAMING_SNAKE_CASE__				:       Union[str, Any]=True     ,							SCREAMING_SNAKE_CASE__				:       List[Any]=False     ,							SCREAMING_SNAKE_CASE__				:       List[Any]=None     ,							SCREAMING_SNAKE_CASE__				:       Tuple=None     ,							**SCREAMING_SNAKE_CASE__				:       str     ,							)		->			str:
             super().__init__(pad_token_id=SCREAMING_SNAKE_CASE__     ,							**SCREAMING_SNAKE_CASE__     )

             # BERT hyperparameters (with updated max_position_embeddings and type_vocab_sizes)
             a_					:		Any							=  vocab_size
             a_					:		int							=  hidden_size
             a_					:		List[str]							=  num_hidden_layers
             a_					:		Optional[Any]							=  num_attention_heads
             a_					:		Optional[int]							=  hidden_act
             a_					:		List[Any]							=  intermediate_size
             a_					:		Union[str, Any]							=  hidden_dropout_prob
             a_					:		Union[str, Any]							=  attention_probs_dropout_prob
             a_					:		Optional[Any]							=  max_position_embeddings
             a_					:		int							=  type_vocab_sizes
             a_					:		str							=  initializer_range
             a_					:		Optional[Any]							=  layer_norm_eps

             # Fine-tuning task hyperparameters
             a_					:		List[Any]							=  positive_label_weight
             a_					:		int							=  num_aggregation_labels
             a_					:		Union[str, Any]							=  aggregation_loss_weight
             a_					:		Dict							=  use_answer_as_supervision
             a_					:		Optional[int]							=  answer_loss_importance
             a_					:		Any							=  use_normalized_answer_loss
             a_					:		Optional[int]							=  huber_loss_delta
             a_					:		Tuple							=  temperature
             a_					:		str							=  aggregation_temperature
             a_					:		Union[str, Any]							=  use_gumbel_for_cells
             a_					:		Tuple							=  use_gumbel_for_aggregation
             a_					:		Any							=  average_approximation_function
             a_					:		Dict							=  cell_selection_preference
             a_					:		Any							=  answer_loss_cutoff
             a_					:		Dict							=  max_num_rows
             a_					:		str							=  max_num_columns
             a_					:		Union[str, Any]							=  average_logits_per_cell
             a_					:		List[Any]							=  select_one_column
             a_					:		Any							=  allow_empty_column_selection
             a_					:		int							=  init_cell_selection_weights_to_zero
             a_					:		List[Any]							=  reset_position_index_per_cell
             a_					:		Tuple							=  disable_per_token_loss

             # Aggregation hyperparameters
             a_					:		Union[str, Any]							=  aggregation_labels
             a_					:		Optional[int]							=  no_aggregation_label_index

             if isinstance(self.aggregation_labels     ,							SCREAMING_SNAKE_CASE__     ):
                    a_					:		Optional[Any]							=  {int(SCREAMING_SNAKE_CASE__     ): v for k, v in aggregation_labels.items()}



"""simple docstring"""






import logging
import random

import ray

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


__lowerCAmelCase    :       int					     =logging.getLogger(__name__)





class  _A						:

      def __init__(						self						):







            """simple docstring"""



            lowercase														=	False

      def 				A__       (						self		,       __lowerCAmelCase		,       __lowerCAmelCase		,       __lowerCAmelCase		,       __lowerCAmelCase						):







            """simple docstring"""



            if not self.initialized:
                  lowercase														=	RagRetriever(
                      __lowerCAmelCase		,       question_encoder_tokenizer=__lowerCAmelCase		,       generator_tokenizer=__lowerCAmelCase		,       index=__lowerCAmelCase		,       init_retrieval=__lowerCAmelCase		,       )
                  lowercase														=	True

      def 				A__       (						self						):







            """simple docstring"""



            self.retriever.index.init_index()







      def 				A__       (						self		,       __lowerCAmelCase		,       __lowerCAmelCase						):







            """simple docstring"""



            lowercase							,							lowercase														=	self.retriever._main_retrieve(__lowerCAmelCase		,       __lowerCAmelCase						)
            return doc_ids, retrieved_doc_embeds






class  _A						(       lowerCAmelCase	):

      def __init__(						self		,       __lowerCAmelCase		,       __lowerCAmelCase		,       __lowerCAmelCase		,       __lowerCAmelCase		,       __lowerCAmelCase=None						):







            """simple docstring"""



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

      def 				A__       (						self						):







            """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 				A__       (						self		,       __lowerCAmelCase		,       __lowerCAmelCase						):







            """simple docstring"""



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

      @classmethod
      def 				A__       (						cls		,       __lowerCAmelCase		,       __lowerCAmelCase=None		,       **__lowerCAmelCase						):







            """simple docstring"""



            return super(__lowerCAmelCase		,       cls						).get_tokenizers(__lowerCAmelCase		,       __lowerCAmelCase		,       **__lowerCAmelCase						)







      @classmethod
      def 				A__       (						cls		,       __lowerCAmelCase		,       __lowerCAmelCase		,       __lowerCAmelCase=None		,       **__lowerCAmelCase						):







            """simple docstring"""



            lowercase														=	kwargs.pop("""config"""		,       __lowerCAmelCase						) or RagConfig.from_pretrained(__lowerCAmelCase		,       **__lowerCAmelCase						)
            lowercase														=	RagTokenizer.from_pretrained(__lowerCAmelCase		,       config=__lowerCAmelCase						)
            lowercase														=	rag_tokenizer.question_encoder
            lowercase														=	rag_tokenizer.generator
            if indexed_dataset is not None:
                  lowercase														=	"""custom"""
                  lowercase														=	CustomHFIndex(config.retrieval_vector_size		,       __lowerCAmelCase						)
            else:
                  lowercase														=	cls._build_index(__lowerCAmelCase						)
            return cls(
                __lowerCAmelCase		,       question_encoder_tokenizer=__lowerCAmelCase		,       generator_tokenizer=__lowerCAmelCase		,       retrieval_workers=__lowerCAmelCase		,       index=__lowerCAmelCase		,       )



