

import unittest

from transformers import is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...generation.test_utils import GenerationTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
       import torch

       from transformers import (
           OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST,
           OpenAIGPTConfig,
           OpenAIGPTDoubleHeadsModel,
           OpenAIGPTForSequenceClassification,
           OpenAIGPTLMHeadModel,
           OpenAIGPTModel,
       )



class       __snake_case  :

  def __init__(					self						,		_A						,		_A=13						,		_A=7						,		_A=True						,		_A=True						,		_A=True						,		_A=99						,		_A=32						,		_A=5						,		_A=4						,		_A=37						,		_A="gelu"						,		_A=0.1						,		_A=0.1						,		_A=512						,		_A=16						,		_A=2						,		_A=0.0_2						,		_A=3						,		_A=4						,		_A=None						,		):
      SCREAMING_SNAKE_CASE_									=						parent
      SCREAMING_SNAKE_CASE_									=						batch_size
      SCREAMING_SNAKE_CASE_									=						seq_length
      SCREAMING_SNAKE_CASE_									=						is_training
      SCREAMING_SNAKE_CASE_									=						use_token_type_ids
      SCREAMING_SNAKE_CASE_									=						use_labels
      SCREAMING_SNAKE_CASE_									=						vocab_size
      SCREAMING_SNAKE_CASE_									=						hidden_size
      SCREAMING_SNAKE_CASE_									=						num_hidden_layers
      SCREAMING_SNAKE_CASE_									=						num_attention_heads
      SCREAMING_SNAKE_CASE_									=						intermediate_size
      SCREAMING_SNAKE_CASE_									=						hidden_act
      SCREAMING_SNAKE_CASE_									=						hidden_dropout_prob
      SCREAMING_SNAKE_CASE_									=						attention_probs_dropout_prob
      SCREAMING_SNAKE_CASE_									=						max_position_embeddings
      SCREAMING_SNAKE_CASE_									=						type_vocab_size
      SCREAMING_SNAKE_CASE_									=						type_sequence_label_size
      SCREAMING_SNAKE_CASE_									=						initializer_range
      SCREAMING_SNAKE_CASE_									=						num_labels
      SCREAMING_SNAKE_CASE_									=						num_choices
      SCREAMING_SNAKE_CASE_									=						scope
      SCREAMING_SNAKE_CASE_									=						self.vocab_size - 1

  def 						lowerCAmelCase__     (					self):
      SCREAMING_SNAKE_CASE_									=						ids_tensor([self.batch_size, self.seq_length]						,		self.vocab_size)

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

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

      SCREAMING_SNAKE_CASE_									=						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						,		)

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

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

  def 						lowerCAmelCase__     (					self						,		_A						,		_A						,		_A						,		_A						,		*_A):
      SCREAMING_SNAKE_CASE_									=						OpenAIGPTModel(config=lowerCAmelCase__)
      model.to(lowerCAmelCase__)
      model.eval()

      SCREAMING_SNAKE_CASE_									=						model(lowerCAmelCase__						,		token_type_ids=lowerCAmelCase__						,		head_mask=lowerCAmelCase__)
      SCREAMING_SNAKE_CASE_									=						model(lowerCAmelCase__						,		token_type_ids=lowerCAmelCase__)
      SCREAMING_SNAKE_CASE_									=						model(lowerCAmelCase__)

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

  def 						lowerCAmelCase__     (					self						,		_A						,		_A						,		_A						,		_A						,		*_A):
      SCREAMING_SNAKE_CASE_									=						OpenAIGPTLMHeadModel(lowerCAmelCase__)
      model.to(lowerCAmelCase__)
      model.eval()

      SCREAMING_SNAKE_CASE_									=						model(lowerCAmelCase__						,		token_type_ids=lowerCAmelCase__						,		labels=lowerCAmelCase__)
      self.parent.assertEqual(result.loss.shape						,		())
      self.parent.assertEqual(result.logits.shape						,		(self.batch_size, self.seq_length, self.vocab_size))

  def 						lowerCAmelCase__     (					self						,		_A						,		_A						,		_A						,		_A						,		*_A):
      SCREAMING_SNAKE_CASE_									=						OpenAIGPTDoubleHeadsModel(lowerCAmelCase__)
      model.to(lowerCAmelCase__)
      model.eval()

      SCREAMING_SNAKE_CASE_									=						model(lowerCAmelCase__						,		token_type_ids=lowerCAmelCase__						,		labels=lowerCAmelCase__)
      self.parent.assertEqual(result.loss.shape						,		())
      self.parent.assertEqual(result.logits.shape						,		(self.batch_size, self.seq_length, self.vocab_size))

  def 						lowerCAmelCase__     (					self						,		_A						,		_A						,		_A						,		_A						,		*_A):
      SCREAMING_SNAKE_CASE_									=						self.num_labels
      SCREAMING_SNAKE_CASE_									=						OpenAIGPTForSequenceClassification(lowerCAmelCase__)
      model.to(lowerCAmelCase__)
      model.eval()

      SCREAMING_SNAKE_CASE_									=						ids_tensor([self.batch_size]						,		self.type_sequence_label_size)
      SCREAMING_SNAKE_CASE_									=						model(lowerCAmelCase__						,		token_type_ids=lowerCAmelCase__						,		labels=lowerCAmelCase__)
      self.parent.assertEqual(result.logits.shape						,		(self.batch_size, self.num_labels))


  def 						lowerCAmelCase__     (					self):
      SCREAMING_SNAKE_CASE_									=						self.prepare_config_and_inputs()
      (
          SCREAMING_SNAKE_CASE_ 
      )									=						config_and_inputs
      SCREAMING_SNAKE_CASE_									=						{
          "input_ids": input_ids,
          "token_type_ids": token_type_ids,
          "head_mask": head_mask,
      }

      return config, inputs_dict






@require_torch
class       __snake_case  (    a__     , a__     , a__     , unittest.TestCase       ):
  __lowerCAmelCase     :	List[Any]						 =  (
      (OpenAIGPTModel, OpenAIGPTLMHeadModel, OpenAIGPTDoubleHeadsModel, OpenAIGPTForSequenceClassification)
      if is_torch_available()
      else ()
  )
  __lowerCAmelCase     :	Dict						 =  (
      (OpenAIGPTLMHeadModel,) if is_torch_available() else ()
  )  # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly
  __lowerCAmelCase     :	Any						 =  (
      {
          """feature-extraction""": OpenAIGPTModel,
          """text-classification""": OpenAIGPTForSequenceClassification,
          """text-generation""": OpenAIGPTLMHeadModel,
          """zero-shot""": OpenAIGPTForSequenceClassification,
      }
      if is_torch_available()
      else {}
  )

  def 						lowerCAmelCase__     (					self						,		_A						,		_A						,		_A						,		_A						,		_A):
      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 						lowerCAmelCase__     (					self						,		_A						,		_A						,		_A=False):
      SCREAMING_SNAKE_CASE_									=						super()._prepare_for_class(lowerCAmelCase__						,		lowerCAmelCase__						,		return_labels=lowerCAmelCase__)

      if return_labels:
          if model_class.__name__ == "OpenAIGPTDoubleHeadsModel":
              SCREAMING_SNAKE_CASE_									=						torch.zeros(
                  (self.model_tester.batch_size, self.model_tester.num_choices, self.model_tester.seq_length)						,		dtype=torch.long						,		device=lowerCAmelCase__						,		)
              SCREAMING_SNAKE_CASE_									=						inputs_dict["labels"]
              SCREAMING_SNAKE_CASE_									=						inputs_dict["labels"]
              SCREAMING_SNAKE_CASE_									=						torch.zeros(
                  (self.model_tester.batch_size, self.model_tester.num_choices)						,		dtype=torch.long						,		device=lowerCAmelCase__						,		)
              SCREAMING_SNAKE_CASE_									=						torch.zeros(
                  self.model_tester.batch_size						,		dtype=torch.long						,		device=lowerCAmelCase__)
      return inputs_dict

  def 						lowerCAmelCase__     (					self):
      SCREAMING_SNAKE_CASE_									=						OpenAIGPTModelTester(self)
      SCREAMING_SNAKE_CASE_									=						ConfigTester(self						,		config_class=lowerCAmelCase__						,		n_embd=37)

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

  def 						lowerCAmelCase__     (					self):
      SCREAMING_SNAKE_CASE_									=						self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_openai_gpt_model(*lowerCAmelCase__)

  def 						lowerCAmelCase__     (					self):
      SCREAMING_SNAKE_CASE_									=						self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_lm_head_model(*lowerCAmelCase__)

  def 						lowerCAmelCase__     (					self):
      SCREAMING_SNAKE_CASE_									=						self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_double_lm_head_model(*lowerCAmelCase__)

  def 						lowerCAmelCase__     (					self):
      SCREAMING_SNAKE_CASE_									=						self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*lowerCAmelCase__)


  @slow
  def 						lowerCAmelCase__     (					self):
      for model_name in OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
          SCREAMING_SNAKE_CASE_									=						OpenAIGPTModel.from_pretrained(lowerCAmelCase__)
          self.assertIsNotNone(lowerCAmelCase__)






@require_torch
class       __snake_case  (    unittest.TestCase       ):

  @slow
  def 						lowerCAmelCase__     (					self):
      SCREAMING_SNAKE_CASE_									=						OpenAIGPTLMHeadModel.from_pretrained('openai-gpt')
      model.to(lowerCAmelCase__)
      SCREAMING_SNAKE_CASE_									=						torch.tensor([[481, 4735, 544]]						,		dtype=torch.long						,		device=lowerCAmelCase__)  # the president is
      SCREAMING_SNAKE_CASE_									=						[
          481,
          4735,
          544,
          246,
          963,
          870,
          762,
          239,
          244,
          40477,
          244,
          249,
          719,
          881,
          487,
          544,
          240,
          244,
          603,
          481,
      ]  # the president is a very good man. " \n " i\'m sure he is, " said the

      SCREAMING_SNAKE_CASE_									=						model.generate(lowerCAmelCase__						,		do_sample=lowerCAmelCase__)
      self.assertListEqual(output_ids[0].tolist()						,		lowerCAmelCase__)






import unittest

from transformers import load_tool

from .test_tools_common import ToolTesterMixin


UpperCamelCase__  :      int			=    "\nHugging Face was founded in 2016 by French entrepreneurs Clément Delangue, Julien Chaumond, and Thomas Wolf originally as a company that developed a chatbot app targeted at teenagers.[2] After open-sourcing the model behind the chatbot, the company pivoted to focus on being a platform for machine learning.\n\nIn March 2021, Hugging Face raised $40 million in a Series B funding round.[3]\n\nOn April 28, 2021, the company launched the BigScience Research Workshop in collaboration with several other research groups to release an open large language model.[4] In 2022, the workshop concluded with the announcement of BLOOM, a multilingual large language model with 176 billion parameters.[5]\n"



class       __snake_case  (    unittest.TestCase     , lowerCAmelCase__       ):

  def 						lowerCAmelCase__     (					self):
      SCREAMING_SNAKE_CASE_									=						load_tool('text-question-answering')
      self.tool.setup()
      SCREAMING_SNAKE_CASE_									=						load_tool('text-question-answering'						,		remote=_A)

  def 						lowerCAmelCase__     (					self):
      SCREAMING_SNAKE_CASE_									=						self.tool(_A						,		'What did Hugging Face do in April 2021?')
      self.assertEqual(_A						,		'launched the BigScience Research Workshop')

  def 						lowerCAmelCase__     (					self):
      SCREAMING_SNAKE_CASE_									=						self.remote_tool(_A						,		'What did Hugging Face do in April 2021?')
      self.assertEqual(_A						,		'launched the BigScience Research Workshop')

  def 						lowerCAmelCase__     (					self):
      SCREAMING_SNAKE_CASE_									=						self.tool(text=_A						,		question='What did Hugging Face do in April 2021?')
      self.assertEqual(_A						,		'launched the BigScience Research Workshop')


  def 						lowerCAmelCase__     (					self):
      SCREAMING_SNAKE_CASE_									=						self.remote_tool(text=_A						,		question='What did Hugging Face do in April 2021?')
      self.assertEqual(_A						,		'launched the BigScience Research Workshop')


