code: 
  
  
  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   __lowerCamelCase :
  
  
  
  
  
  
  
         def __init__( self:   List[Any],A_:   Any,):
  
  
  
  
  
  
              '''simple docstring'''
  
  
  
  
              __UpperCamelCase   = parent
              __UpperCamelCase   = 13
              __UpperCamelCase   = 7
              __UpperCamelCase   = True
              __UpperCamelCase   = True
              __UpperCamelCase   = True
              __UpperCamelCase   = True
              __UpperCamelCase   = True
              __UpperCamelCase   = 99
              __UpperCamelCase   = 32
              __UpperCamelCase   = 2
              __UpperCamelCase   = 4
              __UpperCamelCase   = 37
              __UpperCamelCase   = 'gelu'
              __UpperCamelCase   = 0.1
              __UpperCamelCase   = 0.1
              __UpperCamelCase   = 512
              __UpperCamelCase   = 16
              __UpperCamelCase   = 2
              __UpperCamelCase   = 0.0_2
              __UpperCamelCase   = 3
              __UpperCamelCase   = 4
              __UpperCamelCase   = None
              __UpperCamelCase   = self.vocab_size - 1
  
  
  
  
  
  
  
         def 		snake_case_  ( self:   int				):
  
  
  
  
  
  
              '''simple docstring'''
  
  
  
  
              __UpperCamelCase   = ids_tensor([self.batch_size, self.seq_length],self.vocab_size				)
  
              __UpperCamelCase   = None
              if self.use_input_mask:
                   __UpperCamelCase   = random_attention_mask([self.batch_size, self.seq_length]				)
  
              __UpperCamelCase   = None
              if self.use_token_type_ids:
                   __UpperCamelCase   = ids_tensor([self.batch_size, self.seq_length],self.type_vocab_size				)
  
              __UpperCamelCase   = None
              if self.use_mc_token_ids:
                   __UpperCamelCase   = ids_tensor([self.batch_size, self.num_choices],self.seq_length				)
  
              __UpperCamelCase   = None
              __UpperCamelCase   = None
              __UpperCamelCase   = None
              if self.use_labels:
                   __UpperCamelCase   = ids_tensor([self.batch_size],self.type_sequence_label_size				)
                   __UpperCamelCase   = ids_tensor([self.batch_size, self.seq_length],self.num_labels				)
                   __UpperCamelCase   = ids_tensor([self.batch_size],self.num_choices				)
  
              __UpperCamelCase   = 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,)
  
              __UpperCamelCase   = 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 		snake_case_  ( self:   Tuple,A_:   Optional[int],A_:   Any,A_:   Union[str, Any],A_:   str,A_:   Union[str, Any],*A_:   str				):
  
  
  
  
  
  
              '''simple docstring'''
  
  
  
  
              __UpperCamelCase   = TFOpenAIGPTModel(config=A_				)
              __UpperCamelCase   = {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
              __UpperCamelCase   = model(A_				)
  
              __UpperCamelCase   = [input_ids, input_mask]
              __UpperCamelCase   = model(A_				)
  
              __UpperCamelCase   = model(A_				)
  
              self.parent.assertEqual(result.last_hidden_state.shape,(self.batch_size, self.seq_length, self.hidden_size)				)
  
  
  
  
  
  
  
         def 		snake_case_  ( self:   str,A_:   List[str],A_:   int,A_:   Any,A_:   List[Any],A_:   Optional[Any],*A_:   Union[str, Any]				):
  
  
  
  
  
  
              '''simple docstring'''
  
  
  
  
              __UpperCamelCase   = TFOpenAIGPTLMHeadModel(config=A_				)
              __UpperCamelCase   = {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
              __UpperCamelCase   = model(A_				)
              self.parent.assertEqual(result.logits.shape,(self.batch_size, self.seq_length, self.vocab_size)				)
  
  
  
  
  
  
  
         def 		snake_case_  ( self:   Optional[Any],A_:   Tuple,A_:   Dict,A_:   List[Any],A_:   List[Any],A_:   Any,A_:   Union[str, Any],*A_:   str				):
  
  
  
  
  
  
              '''simple docstring'''
  
  
  
  
              __UpperCamelCase   = TFOpenAIGPTDoubleHeadsModel(config=A_				)
  
              __UpperCamelCase   = tf.tile(tf.expand_dims(A_,1				),(1, self.num_choices, 1)				)
              __UpperCamelCase   = tf.tile(tf.expand_dims(A_,1				),(1, self.num_choices, 1)				)
              __UpperCamelCase   = tf.tile(tf.expand_dims(A_,1				),(1, self.num_choices, 1)				)
  
              __UpperCamelCase   = {
                  '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,
              }
              __UpperCamelCase   = model(A_				)
              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 		snake_case_  ( self:   Union[str, Any],A_:   int,A_:   int,A_:   int,A_:   Any,A_:   int,*A_:   Tuple				):
  
  
  
  
  
  
              '''simple docstring'''
  
  
  
  
              __UpperCamelCase   = self.num_labels
              __UpperCamelCase   = ids_tensor([self.batch_size],self.type_sequence_label_size				)
              __UpperCamelCase   = {
                  'input_ids': input_ids,
                  'attention_mask': input_mask,
                  'token_type_ids': token_type_ids,
                  'labels': sequence_labels,
              }
              __UpperCamelCase   = TFOpenAIGPTForSequenceClassification(A_				)
              __UpperCamelCase   = model(A_				)
              self.parent.assertEqual(result.logits.shape,(self.batch_size, self.num_labels)				)
  
  
  
  
  
  
  
         def 		snake_case_  ( self:   List[str]				):
  
  
  
  
  
  
              '''simple docstring'''
  
  
  
  
              __UpperCamelCase   = self.prepare_config_and_inputs()
  
              (
                  (
                  __UpperCamelCase						
              ),						(
                  __UpperCamelCase						
              ),						(
                  __UpperCamelCase						
              ),						(
                  __UpperCamelCase						
              ),						(
                  __UpperCamelCase						
              ),						(
                  __UpperCamelCase						
              ),						(
                  __UpperCamelCase						
              ),						(
                  __UpperCamelCase						
              ),						(
                  __UpperCamelCase						
              ),												
              )   = config_and_inputs
  
              __UpperCamelCase   = {'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask}
              return config, inputs_dict
  
  
  
  @require_tf
  class   __lowerCamelCase (_a							,			_a							,			unittest.TestCase	):
         _lowercase       							=    (
             (TFOpenAIGPTModel, TFOpenAIGPTLMHeadModel, TFOpenAIGPTDoubleHeadsModel, TFOpenAIGPTForSequenceClassification)
             if is_tf_available()
             else ()
         )
         _lowercase       							=    (
             (TFOpenAIGPTLMHeadModel,) if is_tf_available() else ()
         )  # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly
         _lowercase       							=    (
             {
                 """feature-extraction""": TFOpenAIGPTModel,
                 """text-classification""": TFOpenAIGPTForSequenceClassification,
                 """text-generation""": TFOpenAIGPTLMHeadModel,
                 """zero-shot""": TFOpenAIGPTForSequenceClassification,
             }
             if is_tf_available()
             else {}
         )
         _lowercase       							=    False
         _lowercase       							=    False
  
  
  
  
  
  
  
         def 		snake_case_  ( self:   Union[str, Any],A_:   Optional[int],A_:   Dict,A_:   Optional[int],A_:   Tuple,A_:   Optional[int]				):
  
  
  
  
  
  
              '''simple docstring'''
  
  
  
  
              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 		snake_case_  ( self:   Union[str, Any]				):
  
  
  
  
  
  
              '''simple docstring'''
  
  
  
  
              __UpperCamelCase   = TFOpenAIGPTModelTester(self				)
              __UpperCamelCase   = ConfigTester(self,config_class=A_,n_embd=37				)
  
  
  
  
  
  
  
         def 		snake_case_  ( self:   List[str]				):
  
  
  
  
  
  
              '''simple docstring'''
  
  
  
  
              self.config_tester.run_common_tests()
  
  
  
  
  
  
  
         def 		snake_case_  ( self:   Dict				):
  
  
  
  
  
  
              '''simple docstring'''
  
  
  
  
              __UpperCamelCase   = self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_openai_gpt_model(*A_				)
  
  
  
  
  
  
  
         def 		snake_case_  ( self:   Optional[Any]				):
  
  
  
  
  
  
              '''simple docstring'''
  
  
  
  
              __UpperCamelCase   = self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_openai_gpt_lm_head(*A_				)
  
  
  
  
  
  
  
         def 		snake_case_  ( self:   Optional[int]				):
  
  
  
  
  
  
              '''simple docstring'''
  
  
  
  
              __UpperCamelCase   = self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_openai_gpt_double_head(*A_				)
  
  
  
  
  
  
  
         def 		snake_case_  ( self:   List[Any]				):
  
  
  
  
  
  
              '''simple docstring'''
  
  
  
  
              __UpperCamelCase   = self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*A_				)
  
  
  
  
  
  
  
         @slow
         def 		snake_case_  ( self:   str				):
  
  
  
  
  
  
              '''simple docstring'''
  
  
  
  
              for model_name in TF_OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                   __UpperCamelCase   = TFOpenAIGPTModel.from_pretrained(A_				)
                   self.assertIsNotNone(A_				)
  
  
  
  @require_tf
  class   __lowerCamelCase (unittest.TestCase	):
  
  
  
  
  
  
  
         @slow
         def 		snake_case_  ( self:   Optional[Any]				):
  
  
  
  
  
  
              '''simple docstring'''
  
  
  
  
              __UpperCamelCase   = TFOpenAIGPTLMHeadModel.from_pretrained('openai-gpt'				)
              __UpperCamelCase   = tf.convert_to_tensor([[481, 4735, 544]],dtype=tf.intaa				)  # the president is
              __UpperCamelCase   = [
                  481,
                  4735,
                  544,
                  246,
                  963,
                  870,
                  762,
                  239,
                  244,
                  4_0477,
                  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
  
              __UpperCamelCase   = model.generate(A_,do_sample=A_				)
              self.assertListEqual(output_ids[0].numpy().tolist(),A_				)
  
  
  
  
  
style_context: 
  
  
  import unittest
  
  import requests
  
  from transformers import MODEL_FOR_VISION_2_SEQ_MAPPING, TF_MODEL_FOR_VISION_2_SEQ_MAPPING, is_vision_available
  from transformers.pipelines import pipeline
  from transformers.testing_utils import (
      is_pipeline_test,
      is_torch_available,
      require_tf,
      require_torch,
      require_vision,
      slow,
  )
  
  from .test_pipelines_common import ANY
  
  
  if is_torch_available():
      from transformers.pytorch_utils import is_torch_greater_or_equal_than_1_11
  else:
      __snake_case             =	False
  
  
  if is_vision_available():
      from PIL import Image
  else:
  
  
  
      class   __lowerCamelCase :
  
  
  
  
  
  
  
             @staticmethod
             def 		snake_case_  ( *A_:   List[str],**A_:   Any				):
  
  
  
  
  
  
                  '''simple docstring'''
  
  
  
  
                  pass
  
  
  
  @is_pipeline_test
  @require_vision
  class   __lowerCamelCase (unittest.TestCase	):
         _lowercase       							=    MODEL_FOR_VISION_2_SEQ_MAPPING
         _lowercase       							=    TF_MODEL_FOR_VISION_2_SEQ_MAPPING
  
  
  
  
  
  
  
         def 		snake_case_  ( self:   Dict,A_:   int,A_:   List[str],A_:   List[str]				):
  
  
  
  
  
  
              '''simple docstring'''
  
  
  
  
              __UpperCamelCase   = pipeline('image-to-text',model=A_,tokenizer=A_,image_processor=A_				)
              __UpperCamelCase   = [
                  Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'				),
                  './tests/fixtures/tests_samples/COCO/000000039769.png',
              ]
              return pipe, examples
  
  
  
  
  
  
  
         def 		snake_case_  ( self:   Union[str, Any],A_:   Optional[Any],A_:   Optional[int]				):
  
  
  
  
  
  
              '''simple docstring'''
  
  
  
  
              __UpperCamelCase   = pipe(A_				)
              self.assertEqual(
                  A_,[
                      [{'generated_text': ANY(A_				)}],
                      [{'generated_text': ANY(A_				)}],
                  ],)
  
  
  
  
  
  
  
         @require_tf
         def 		snake_case_  ( self:   str				):
  
  
  
  
  
  
              '''simple docstring'''
  
  
  
  
              __UpperCamelCase   = pipeline('image-to-text',model='hf-internal-testing/tiny-random-vit-gpt2',framework='tf'				)
              __UpperCamelCase   = './tests/fixtures/tests_samples/COCO/000000039769.png'
  
              __UpperCamelCase   = pipe(A_				)
              self.assertEqual(
                  A_,[
                      {
                          'generated_text': 'growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO'
                      },
                  ],)
  
              __UpperCamelCase   = pipe([image, image]				)
              self.assertEqual(
                  A_,[
                      [
                          {
                              'generated_text': 'growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO'
                          }
                      ],
                      [
                          {
                              'generated_text': 'growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO'
                          }
                      ],
                  ],)
  
              __UpperCamelCase   = pipe(A_,max_new_tokens=1				)
              self.assertEqual(
                  A_,[{'generated_text': 'growth'}],)
  
  
  
  
  
  
  
         @require_torch
         def 		snake_case_  ( self:   Any				):
  
  
  
  
  
  
              '''simple docstring'''
  
  
  
  
              __UpperCamelCase   = pipeline('image-to-text',model='hf-internal-testing/tiny-random-vit-gpt2'				)
              __UpperCamelCase   = './tests/fixtures/tests_samples/COCO/000000039769.png'
  
              __UpperCamelCase   = pipe(A_				)
              self.assertEqual(
                  A_,[
                      {
                          'generated_text': 'growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO'
                      },
                  ],)
  
              __UpperCamelCase   = pipe([image, image]				)
              self.assertEqual(
                  A_,[
                      [
                          {
                              'generated_text': 'growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO'
                          }
                      ],
                      [
                          {
                              'generated_text': 'growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO'
                          }
                      ],
                  ],)
  
  
  
  
  
  
  
         @require_torch
         def 		snake_case_  ( self:   str				):
  
  
  
  
  
  
              '''simple docstring'''
  
  
  
  
              __UpperCamelCase   = pipeline('image-to-text',model='hf-internal-testing/tiny-random-BlipForConditionalGeneration'				)
              __UpperCamelCase   = './tests/fixtures/tests_samples/COCO/000000039769.png'
              __UpperCamelCase   = 'a photo of'
  
              __UpperCamelCase   = pipe(A_,prompt=A_				)
              self.assertTrue(outputs[0]['generated_text'].startswith(A_				)				)
  
  
  
  
  
  
  
         @slow
         @require_torch
         def 		snake_case_  ( self:   Dict				):
  
  
  
  
  
  
              '''simple docstring'''
  
  
  
  
              __UpperCamelCase   = pipeline('image-to-text',model='ydshieh/vit-gpt2-coco-en'				)
              __UpperCamelCase   = './tests/fixtures/tests_samples/COCO/000000039769.png'
  
              __UpperCamelCase   = pipe(A_				)
              self.assertEqual(A_,[{'generated_text': 'a cat laying on a blanket next to a cat laying on a bed '}]				)
  
              __UpperCamelCase   = pipe([image, image]				)
              self.assertEqual(
                  A_,[
                      [{'generated_text': 'a cat laying on a blanket next to a cat laying on a bed '}],
                      [{'generated_text': 'a cat laying on a blanket next to a cat laying on a bed '}],
                  ],)
  
  
  
  
  
  
  
         @slow
         @require_torch
         def 		snake_case_  ( self:   Dict				):
  
  
  
  
  
  
              '''simple docstring'''
  
  
  
  
              __UpperCamelCase   = pipeline('image-to-text',model='Salesforce/blip-image-captioning-base'				)
              __UpperCamelCase   = 'https://huggingface.co/datasets/sayakpaul/sample-datasets/resolve/main/pokemon.png'
              __UpperCamelCase   = Image.open(requests.get(A_,stream=A_				).raw				)
  
              __UpperCamelCase   = pipe(A_				)
              self.assertEqual(A_,[{'generated_text': 'a pink pokemon pokemon with a blue shirt and a blue shirt'}]				)
  
  
  
  
  
  
  
         @slow
         @require_torch
         def 		snake_case_  ( self:   Dict				):
  
  
  
  
  
  
              '''simple docstring'''
  
  
  
  
              __UpperCamelCase   = pipeline('image-to-text',model='microsoft/git-base-coco'				)
              __UpperCamelCase   = 'https://huggingface.co/datasets/sayakpaul/sample-datasets/resolve/main/pokemon.png'
              __UpperCamelCase   = Image.open(requests.get(A_,stream=A_				).raw				)
  
              __UpperCamelCase   = pipe(A_				)
              self.assertEqual(A_,[{'generated_text': 'a cartoon of a purple character.'}]				)
  
  
  
  
  
  
  
         @slow
         @require_torch
         def 		snake_case_  ( self:   Dict				):
  
  
  
  
  
  
              '''simple docstring'''
  
  
  
  
              __UpperCamelCase   = pipeline('image-to-text',model='Salesforce/blip-image-captioning-base'				)
              __UpperCamelCase   = 'https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/ai2d-demo.jpg'
              __UpperCamelCase   = Image.open(requests.get(A_,stream=A_				).raw				)
  
              __UpperCamelCase   = 'a photography of'
  
              __UpperCamelCase   = pipe(A_,prompt=A_				)
              self.assertEqual(A_,[{'generated_text': 'a photography of a volcano'}]				)
  
              with self.assertRaises(A_				):
                   __UpperCamelCase   = pipe([image, image],prompt=[prompt, prompt]				)
  
  
  
  
  
  
  
         @slow
         @require_torch
         def 		snake_case_  ( self:   List[Any]				):
  
  
  
  
  
  
              '''simple docstring'''
  
  
  
  
              __UpperCamelCase   = pipeline('image-to-text',model='microsoft/git-base-coco'				)
              __UpperCamelCase   = 'https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/ai2d-demo.jpg'
              __UpperCamelCase   = Image.open(requests.get(A_,stream=A_				).raw				)
  
              __UpperCamelCase   = 'a photo of a'
  
              __UpperCamelCase   = pipe(A_,prompt=A_				)
              self.assertEqual(A_,[{'generated_text': 'a photo of a tent with a tent and a tent in the background.'}]				)
  
              with self.assertRaises(A_				):
                   __UpperCamelCase   = pipe([image, image],prompt=[prompt, prompt]				)
  
  
  
  
  
  
  
         @unittest.skipIf(
             not is_torch_greater_or_equal_than_1_11,reason='`Pix2StructImageProcessor` requires `torch>=1.11.0`.'				)
         @slow
         @require_torch
         def 		snake_case_  ( self:   List[Any]				):
  
  
  
  
  
  
              '''simple docstring'''
  
  
  
  
              __UpperCamelCase   = pipeline('image-to-text',model='google/pix2struct-ai2d-base'				)
              __UpperCamelCase   = 'https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/ai2d-demo.jpg'
              __UpperCamelCase   = Image.open(requests.get(A_,stream=A_				).raw				)
  
              __UpperCamelCase   = 'What does the label 15 represent? (1) lava (2) core (3) tunnel (4) ash cloud'
  
              __UpperCamelCase   = pipe(A_,prompt=A_				)
              self.assertEqual(A_,[{'generated_text': 'ash cloud'}]				)
  
              with self.assertRaises(A_				):
                   __UpperCamelCase   = pipe([image, image],prompt=[prompt, prompt]				)
  
  
  
  
  
  
  
         @slow
         @require_tf
         def 		snake_case_  ( self:   Any				):
  
  
  
  
  
  
              '''simple docstring'''
  
  
  
  
              __UpperCamelCase   = pipeline('image-to-text',model='ydshieh/vit-gpt2-coco-en',framework='tf'				)
              __UpperCamelCase   = './tests/fixtures/tests_samples/COCO/000000039769.png'
  
              __UpperCamelCase   = pipe(A_				)
              self.assertEqual(A_,[{'generated_text': 'a cat laying on a blanket next to a cat laying on a bed '}]				)
  
              __UpperCamelCase   = pipe([image, image]				)
              self.assertEqual(
                  A_,[
                      [{'generated_text': 'a cat laying on a blanket next to a cat laying on a bed '}],
                      [{'generated_text': 'a cat laying on a blanket next to a cat laying on a bed '}],
                  ],)
  
  
  