






"""simple docstring"""



from __future__ import annotations

import unittest

from transformers import OpenAIGPTConfig, is_tf_available
from transformers.testing_utils import require_tf, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
    import tensorflow as tf

    from transformers.models.openai.modeling_tf_openai import (
        TF_OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST,
        TFOpenAIGPTDoubleHeadsModel,
        TFOpenAIGPTForSequenceClassification,
        TFOpenAIGPTLMHeadModel,
        TFOpenAIGPTModel,
    )

class 			a	:







    def __init__(     self				,						_lowerCamelCase				,						):
      lowercase				       =			parent
      lowercase				       =			1_3
      lowercase				       =			7
      lowercase				       =			True
      lowercase				       =			True
      lowercase				       =			True
      lowercase				       =			True
      lowercase				       =			True
      lowercase				       =			9_9
      lowercase				       =			3_2
      lowercase				       =			2
      lowercase				       =			4
      lowercase				       =			3_7
      lowercase				       =			'gelu'
      lowercase				       =			0.1
      lowercase				       =			0.1
      lowercase				       =			5_1_2
      lowercase				       =			1_6
      lowercase				       =			2
      lowercase				       =			0.0_2
      lowercase				       =			3
      lowercase				       =			4
      lowercase				       =			None
      lowercase				       =			self.vocab_size - 1







    def      UpperCamelCase_ (     self			):
      lowercase				       =			ids_tensor([self.batch_size, self.seq_length]				,						self.vocab_size			)

      lowercase				       =			None
      if self.use_input_mask:
        lowercase				       =			random_attention_mask([self.batch_size, self.seq_length]			)

      lowercase				       =			None
      if self.use_token_type_ids:
        lowercase				       =			ids_tensor([self.batch_size, self.seq_length]				,						self.type_vocab_size			)

      lowercase				       =			None
      if self.use_mc_token_ids:
        lowercase				       =			ids_tensor([self.batch_size, self.num_choices]				,						self.seq_length			)

      lowercase				       =			None
      lowercase				       =			None
      lowercase				       =			None
      if self.use_labels:
        lowercase				       =			ids_tensor([self.batch_size]				,						self.type_sequence_label_size			)
        lowercase				       =			ids_tensor([self.batch_size, self.seq_length]				,						self.num_labels			)
        lowercase				       =			ids_tensor([self.batch_size]				,						self.num_choices			)

      lowercase				       =			OpenAIGPTConfig(
          vocab_size=self.vocab_size				,						n_embd=self.hidden_size				,						n_layer=self.num_hidden_layers				,						n_head=self.num_attention_heads				,						n_positions=self.max_position_embeddings				,						pad_token_id=self.pad_token_id				,						)

      lowercase				       =			ids_tensor([self.num_hidden_layers, self.num_attention_heads]				,						2			)

      return (
          config,
          input_ids,
          input_mask,
          head_mask,
          token_type_ids,
          mc_token_ids,
          sequence_labels,
          token_labels,
          choice_labels,
      )







    def      UpperCamelCase_ (     self				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						*_lowerCamelCase			):
      lowercase				       =			TFOpenAIGPTModel(config=_lowerCamelCase			)
      lowercase				       =			{'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
      lowercase				       =			model(_lowerCamelCase			)

      lowercase				       =			[input_ids, input_mask]
      lowercase				       =			model(_lowerCamelCase			)

      lowercase				       =			model(_lowerCamelCase			)

      self.parent.assertEqual(result.last_hidden_state.shape				,						(self.batch_size, self.seq_length, self.hidden_size)			)







    def      UpperCamelCase_ (     self				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						*_lowerCamelCase			):
      lowercase				       =			TFOpenAIGPTLMHeadModel(config=_lowerCamelCase			)
      lowercase				       =			{'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
      lowercase				       =			model(_lowerCamelCase			)
      self.parent.assertEqual(result.logits.shape				,						(self.batch_size, self.seq_length, self.vocab_size)			)







    def      UpperCamelCase_ (     self				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						*_lowerCamelCase			):
      lowercase				       =			TFOpenAIGPTDoubleHeadsModel(config=_lowerCamelCase			)

      lowercase				       =			tf.tile(tf.expand_dims(_lowerCamelCase				,						1			)				,						(1, self.num_choices, 1)			)
      lowercase				       =			tf.tile(tf.expand_dims(_lowerCamelCase				,						1			)				,						(1, self.num_choices, 1)			)
      lowercase				       =			tf.tile(tf.expand_dims(_lowerCamelCase				,						1			)				,						(1, self.num_choices, 1)			)

      lowercase				       =			{
          'input_ids': multiple_choice_inputs_ids,
          'mc_token_ids': mc_token_ids,
          'attention_mask': multiple_choice_input_mask,
          'token_type_ids': multiple_choice_token_type_ids,
      }
      lowercase				       =			model(_lowerCamelCase			)
      self.parent.assertEqual(
          result.logits.shape				,						(self.batch_size, self.num_choices, self.seq_length, self.vocab_size)			)
      self.parent.assertEqual(result.mc_logits.shape				,						(self.batch_size, self.num_choices)			)







    def      UpperCamelCase_ (     self				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						*_lowerCamelCase			):
      lowercase				       =			self.num_labels
      lowercase				       =			ids_tensor([self.batch_size]				,						self.type_sequence_label_size			)
      lowercase				       =			{
          'input_ids': input_ids,
          'attention_mask': input_mask,
          'token_type_ids': token_type_ids,
          'labels': sequence_labels,
      }
      lowercase				       =			TFOpenAIGPTForSequenceClassification(_lowerCamelCase			)
      lowercase				       =			model(_lowerCamelCase			)
      self.parent.assertEqual(result.logits.shape				,						(self.batch_size, self.num_labels)			)







    def      UpperCamelCase_ (     self			):
      lowercase				       =			self.prepare_config_and_inputs()

      (
          (
          lowercase
      )       ,	(
          lowercase
      )       ,	(
          lowercase
      )       ,	(
          lowercase
      )       ,	(
          lowercase
      )       ,	(
          lowercase
      )       ,	(
          lowercase
      )       ,	(
          lowercase
      )       ,	(
          lowercase
      )       ,	
      )				       =			config_and_inputs

      lowercase				       =			{'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask}
      return config, inputs_dict


@require_tf
class 			a	(  a_,							a_,							unittest.TestCase      ):
    UpperCAmelCase_					:	Any      	=(
        (TFOpenAIGPTModel, TFOpenAIGPTLMHeadModel, TFOpenAIGPTDoubleHeadsModel, TFOpenAIGPTForSequenceClassification)
        if is_tf_available()
        else ()
    )
    UpperCAmelCase_					:	int      	=(
        (TFOpenAIGPTLMHeadModel,) if is_tf_available() else ()
    )  # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly
    UpperCAmelCase_					:	str      	=(
        {
            "feature-extraction": TFOpenAIGPTModel,
            "text-classification": TFOpenAIGPTForSequenceClassification,
            "text-generation": TFOpenAIGPTLMHeadModel,
            "zero-shot": TFOpenAIGPTForSequenceClassification,
        }
        if is_tf_available()
        else {}
    )
    UpperCAmelCase_					:	List[str]      	=False
    UpperCAmelCase_					:	Optional[int]      	=False







    def      UpperCamelCase_ (     self				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase			):
      if pipeline_test_casse_name == "ZeroShotClassificationPipelineTests":
        # Get `tokenizer does not have a padding token` error for both fast/slow tokenizers.
        # `OpenAIGPTConfig` was never used in pipeline tests, either because of a missing checkpoint or because a
        # tiny config could not be created.
        return True

      return False







    def      UpperCamelCase_ (     self			):
      lowercase				       =			TFOpenAIGPTModelTester(self			)
      lowercase				       =			ConfigTester(self				,						config_class=_lowerCamelCase				,						n_embd=3_7			)







    def      UpperCamelCase_ (     self			):
      self.config_tester.run_common_tests()







    def      UpperCamelCase_ (     self			):
      lowercase				       =			self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_openai_gpt_model(*_lowerCamelCase			)







    def      UpperCamelCase_ (     self			):
      lowercase				       =			self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_openai_gpt_lm_head(*_lowerCamelCase			)







    def      UpperCamelCase_ (     self			):
      lowercase				       =			self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_openai_gpt_double_head(*_lowerCamelCase			)







    def      UpperCamelCase_ (     self			):
      lowercase				       =			self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*_lowerCamelCase			)







    @slow
    def      UpperCamelCase_ (     self			):
      for model_name in TF_OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
        lowercase				       =			TFOpenAIGPTModel.from_pretrained(_lowerCamelCase			)
        self.assertIsNotNone(_lowerCamelCase			)


@require_tf
class 			a	(  unittest.TestCase      ):







    @slow
    def      UpperCamelCase_ (     self			):
      lowercase				       =			TFOpenAIGPTLMHeadModel.from_pretrained('openai-gpt'			)
      lowercase				       =			tf.convert_to_tensor([[4_8_1, 4_7_3_5, 5_4_4]]				,						dtype=tf.intaa			)  # the president is
      lowercase				       =			[
          4_8_1,
          4_7_3_5,
          5_4_4,
          2_4_6,
          9_6_3,
          8_7_0,
          7_6_2,
          2_3_9,
          2_4_4,
          4_0_4_7_7,
          2_4_4,
          2_4_9,
          7_1_9,
          8_8_1,
          4_8_7,
          5_4_4,
          2_4_0,
          2_4_4,
          6_0_3,
          4_8_1,
      ]  # the president is a very good man. " \n " i\'m sure he is, " said the

      lowercase				       =			model.generate(_lowerCamelCase				,						do_sample=_lowerCamelCase			)
      self.assertListEqual(output_ids[0].numpy().tolist()				,						_lowerCamelCase			)






"""simple docstring"""






import logging
import random

import ray

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


SCREAMING_SNAKE_CASE__				:  Optional[Any]						= logging.getLogger(__name__)


class 	A_       :





  """simple docstring"""

  def __init__(						self						)				->					Dict:
     a   :	List[str]						=    False
  def 							lowercase_     (						self  ,					__UpperCAmelCase  ,					__UpperCAmelCase  ,					__UpperCAmelCase  ,					__UpperCAmelCase						)				->					List[str]:
     if not self.initialized:
        a   :	Dict						=    RagRetriever(
            __UpperCAmelCase  ,					question_encoder_tokenizer=__UpperCAmelCase  ,					generator_tokenizer=__UpperCAmelCase  ,					index=__UpperCAmelCase  ,					init_retrieval=__UpperCAmelCase  ,					)
        a   :	Dict						=    True
  def 							lowercase_     (						self						)				->					List[Any]:
     self.retriever.index.init_index()
  def 							lowercase_     (						self						)				->					List[Any]:
     # delete the old self.retriever object before assigning the new index
     del self.retriever
     a   :	Dict						=    False






  def 							lowercase_     (						self  ,					__UpperCAmelCase  ,					__UpperCAmelCase						)				->					Optional[int]:
     a	, a   :	List[str]						=    self.retriever._main_retrieve(__UpperCAmelCase  ,					__UpperCAmelCase						)
     a   :	List[Any]						=    self.retriever.index.get_doc_dicts(__UpperCAmelCase						)
     return doc_ids, retrieved_doc_embeds, doc_dicts



class 	A_       (     _UpperCAmelCase   ):





  """simple docstring"""

  def __init__(						self  ,					__UpperCAmelCase  ,					__UpperCAmelCase  ,					__UpperCAmelCase  ,					__UpperCAmelCase  ,					__UpperCAmelCase=None						)				->					Tuple:
     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  ,					)

     a   :	Tuple						=    retrieval_workers
     a   :	List[str]						=    question_encoder_tokenizer
     a   :	Union[str, Any]						=    generator_tokenizer
     if len(self.retrieval_workers						) > 0:
        ray.get(
            [
                worker.create_rag_retriever.remote(__UpperCAmelCase  ,					__UpperCAmelCase  ,					__UpperCAmelCase  ,					__UpperCAmelCase						)
                for worker in self.retrieval_workers
            ]						)
  def 							lowercase_     (						self						)				->					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 							lowercase_     (						self  ,					__UpperCAmelCase  ,					__UpperCAmelCase						)				->					Tuple:
     if len(self.retrieval_workers						) > 0:
        # Select a random retrieval actor.
        a   :	Dict						=    self.retrieval_workers[random.randint(0  ,					len(self.retrieval_workers						) - 1						)]
        a	, a	, a   :	Any						=    ray.get(
            random_worker.retrieve.remote(__UpperCAmelCase  ,					__UpperCAmelCase						)						)
     else:
        a	, a   :	str						=    self._main_retrieve(__UpperCAmelCase  ,					__UpperCAmelCase						)
        a   :	str						=    self.index.get_doc_dicts(__UpperCAmelCase						)
     return retrieved_doc_embeds, doc_ids, doc_dicts
  @classmethod
  def 							lowercase_     (						cls  ,					__UpperCAmelCase  ,					__UpperCAmelCase=None  ,					**__UpperCAmelCase						)				->					int:
     return super(__UpperCAmelCase  ,					cls						).get_tokenizers(__UpperCAmelCase  ,					__UpperCAmelCase  ,					**__UpperCAmelCase						)
  @classmethod
  def 							lowercase_     (						cls  ,					__UpperCAmelCase  ,					__UpperCAmelCase  ,					__UpperCAmelCase=None  ,					**__UpperCAmelCase						)				->					str:
     a   :	Union[str, Any]						=    kwargs.pop('config'  ,					__UpperCAmelCase						) or RagConfig.from_pretrained(__UpperCAmelCase  ,					**__UpperCAmelCase						)
     a   :	Optional[Any]						=    RagTokenizer.from_pretrained(__UpperCAmelCase  ,					config=__UpperCAmelCase						)
     a   :	List[str]						=    rag_tokenizer.question_encoder
     a   :	int						=    rag_tokenizer.generator

     if indexed_dataset is not None:
        a   :	Optional[int]						=    'custom'
        a   :	str						=    CustomHFIndex(config.retrieval_vector_size  ,					__UpperCAmelCase						)
     else:
        a   :	Dict						=    cls._build_index(__UpperCAmelCase						)

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






  def 							lowercase_     (						self						)				->					List[Any]:
     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   :	Optional[Any]						=    self._build_index(self.config						)

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





