






"""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      UpperCamelCase_       :



  def __init__(					self       ,						snake_case__       ,						)	->	Tuple:



         """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					=	5_12
         UpperCAmelCase					=	16
         UpperCAmelCase					=	2
         UpperCAmelCase					=	0.02
         UpperCAmelCase					=	3
         UpperCAmelCase					=	4
         UpperCAmelCase					=	None
         UpperCAmelCase					=	self.vocab_size - 1



  def 						UpperCamelCase_ (					self )	->	List[Any]:



         """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 						UpperCamelCase_ (					self       ,						snake_case__       ,						snake_case__       ,						snake_case__       ,						snake_case__       ,						snake_case__       ,						*snake_case__ )	->	Dict:



         """simple docstring"""





         UpperCAmelCase					=	TFOpenAIGPTModel(config=snake_case__ )
         UpperCAmelCase					=	{"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
         UpperCAmelCase					=	model(snake_case__ )

         UpperCAmelCase					=	[input_ids, input_mask]
         UpperCAmelCase					=	model(snake_case__ )

         UpperCAmelCase					=	model(snake_case__ )

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



  def 						UpperCamelCase_ (					self       ,						snake_case__       ,						snake_case__       ,						snake_case__       ,						snake_case__       ,						snake_case__       ,						*snake_case__ )	->	str:



         """simple docstring"""





         UpperCAmelCase					=	TFOpenAIGPTLMHeadModel(config=snake_case__ )
         UpperCAmelCase					=	{"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
         UpperCAmelCase					=	model(snake_case__ )
         self.parent.assertEqual(result.logits.shape       ,						(self.batch_size, self.seq_length, self.vocab_size) )



  def 						UpperCamelCase_ (					self       ,						snake_case__       ,						snake_case__       ,						snake_case__       ,						snake_case__       ,						snake_case__       ,						snake_case__       ,						*snake_case__ )	->	int:



         """simple docstring"""





         UpperCAmelCase					=	TFOpenAIGPTDoubleHeadsModel(config=snake_case__ )

         UpperCAmelCase					=	tf.tile(tf.expand_dims(snake_case__       ,						1 )       ,						(1, self.num_choices, 1) )
         UpperCAmelCase					=	tf.tile(tf.expand_dims(snake_case__       ,						1 )       ,						(1, self.num_choices, 1) )
         UpperCAmelCase					=	tf.tile(tf.expand_dims(snake_case__       ,						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(snake_case__ )
         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       ,						snake_case__       ,						snake_case__       ,						snake_case__       ,						snake_case__       ,						snake_case__       ,						*snake_case__ )	->	Any:



         """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(snake_case__ )
         UpperCAmelCase					=	model(snake_case__ )
         self.parent.assertEqual(result.logits.shape       ,						(self.batch_size, self.num_labels) )



  def 						UpperCamelCase_ (					self )	->	Any:



         """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      UpperCamelCase_       (				a_      ,    a_      ,    unittest.TestCase     ):
  _A				:    List[str] =		(
      (TFOpenAIGPTModel, TFOpenAIGPTLMHeadModel, TFOpenAIGPTDoubleHeadsModel, TFOpenAIGPTForSequenceClassification)
      if is_tf_available()
      else ()
  )
  _A				:    Dict =		(
      (TFOpenAIGPTLMHeadModel,) if is_tf_available() else ()
  )  # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly
  _A				:    List[str] =		(
      {
          'feature-extraction': TFOpenAIGPTModel,
          'text-classification': TFOpenAIGPTForSequenceClassification,
          'text-generation': TFOpenAIGPTLMHeadModel,
          'zero-shot': TFOpenAIGPTForSequenceClassification,
      }
      if is_tf_available()
      else {}
  )
  _A				:    Dict =		False
  _A				:    Tuple =		False



  def 						UpperCamelCase_ (					self       ,						snake_case__       ,						snake_case__       ,						snake_case__       ,						snake_case__       ,						snake_case__ )	->	Dict:



         """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 						UpperCamelCase_ (					self )	->	Optional[Any]:



         """simple docstring"""





         UpperCAmelCase					=	TFOpenAIGPTModelTester(self )
         UpperCAmelCase					=	ConfigTester(self       ,						config_class=snake_case__       ,						n_embd=37 )



  def 						UpperCamelCase_ (					self )	->	List[Any]:



         """simple docstring"""





         self.config_tester.run_common_tests()



  def 						UpperCamelCase_ (					self )	->	Optional[int]:



         """simple docstring"""





         UpperCAmelCase					=	self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_openai_gpt_model(*snake_case__ )



  def 						UpperCamelCase_ (					self )	->	Optional[Any]:



         """simple docstring"""





         UpperCAmelCase					=	self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_openai_gpt_lm_head(*snake_case__ )



  def 						UpperCamelCase_ (					self )	->	Optional[Any]:



         """simple docstring"""





         UpperCAmelCase					=	self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_openai_gpt_double_head(*snake_case__ )



  def 						UpperCamelCase_ (					self )	->	Dict:



         """simple docstring"""





         UpperCAmelCase					=	self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*snake_case__ )



  @slow
  def 						UpperCamelCase_ (					self )	->	Optional[int]:



         """simple docstring"""





         for model_name in TF_OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                UpperCAmelCase					=	TFOpenAIGPTModel.from_pretrained(snake_case__ )
                self.assertIsNotNone(snake_case__ )





@require_tf
class      UpperCamelCase_       (				unittest.TestCase     ):



  @slow
  def 						UpperCamelCase_ (					self )	->	List[str]:



         """simple docstring"""





         UpperCAmelCase					=	TFOpenAIGPTLMHeadModel.from_pretrained("""openai-gpt""" )
         UpperCAmelCase					=	tf.convert_to_tensor([[4_81, 47_35, 5_44]]       ,						dtype=tf.intaa )  # the president is
         UpperCAmelCase					=	[
             4_81,
             47_35,
             5_44,
             2_46,
             9_63,
             8_70,
             7_62,
             2_39,
             2_44,
             4_04_77,
             2_44,
             2_49,
             7_19,
             8_81,
             4_87,
             5_44,
             2_40,
             2_44,
             6_03,
             4_81,
         ]  # the president is a very good man. " \n " i\'m sure he is, " said the

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



import unittest

import numpy as np

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin


if is_torch_available():
 import torch

if is_vision_available():
 from PIL import Image

 from transformers import CLIPImageProcessor



class     lowercase						(       unittest.TestCase					):



 '''simple docstring'''


 def __init__(self       ,       __a       ,       __a=7       ,       __a=3       ,       __a=18       ,       __a=30       ,       __a=400       ,       __a=True       ,       __a=None       ,       __a=True       ,       __a=None       ,       __a=True       ,       __a=[0.48_14_54_66, 0.4_57_82_75, 0.40_82_10_73]       ,       __a=[0.26_86_29_54, 0.26_13_02_58, 0.27_57_77_11]       ,       __a=True       ,       ) ->					Optional[int]:






  """simple docstring"""





  UpperCAmelCase__							=				size if size is not None else {'shortest_edge': 20}
  UpperCAmelCase__							=				crop_size if crop_size is not None else {'height': 18, 'width': 18}
  UpperCAmelCase__							=				parent
  UpperCAmelCase__							=				batch_size
  UpperCAmelCase__							=				num_channels
  UpperCAmelCase__							=				image_size
  UpperCAmelCase__							=				min_resolution
  UpperCAmelCase__							=				max_resolution
  UpperCAmelCase__							=				do_resize
  UpperCAmelCase__							=				size
  UpperCAmelCase__							=				do_center_crop
  UpperCAmelCase__							=				crop_size
  UpperCAmelCase__							=				do_normalize
  UpperCAmelCase__							=				image_mean
  UpperCAmelCase__							=				image_std
  UpperCAmelCase__							=				do_convert_rgb




 def   UpperCamelCase__							(self ) ->					List[str]:






  """simple docstring"""





  return {
      "do_resize": self.do_resize,
      "size": self.size,
      "do_center_crop": self.do_center_crop,
      "crop_size": self.crop_size,
      "do_normalize": self.do_normalize,
      "image_mean": self.image_mean,
      "image_std": self.image_std,
      "do_convert_rgb": self.do_convert_rgb,
  }




 def   UpperCamelCase__							(self       ,       __a=False       ,       __a=False       ,       __a=False ) ->					Union[str, Any]:






  """simple docstring"""





  assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time"

  if equal_resolution:
   UpperCAmelCase__							=				[]
   for i in range(self.batch_size ):
    image_inputs.append(
        np.random.randint(
            255       ,       size=(self.num_channels, self.max_resolution, self.max_resolution)       ,       dtype=np.uinta ) )
  else:
   UpperCAmelCase__							=				[]
   for i in range(self.batch_size ):
    UpperCAmelCase__				,    UpperCAmelCase__							=				np.random.choice(np.arange(self.min_resolution       ,       self.max_resolution )       ,       2 )
    image_inputs.append(np.random.randint(255       ,       size=(self.num_channels, width, height)       ,       dtype=np.uinta ) )

  if not numpify and not torchify:
   # PIL expects the channel dimension as last dimension
   UpperCAmelCase__							=				[Image.fromarray(np.moveaxis(__a       ,       0       ,       -1 ) ) for x in image_inputs]

  if torchify:
   UpperCAmelCase__							=				[torch.from_numpy(__a ) for x in image_inputs]

  return image_inputs







@require_torch
@require_vision
class     lowercase						(       _UpperCamelCase ,				unittest.TestCase					):



 '''simple docstring'''


 __SCREAMING_SNAKE_CASE												=				CLIPImageProcessor if is_vision_available() else None




 def   UpperCamelCase__							(self ) ->					Union[str, Any]:






  """simple docstring"""





  UpperCAmelCase__							=				CLIPImageProcessingTester(self )




 @property
 def   UpperCamelCase__							(self ) ->					List[str]:






  """simple docstring"""





  return self.image_processor_tester.prepare_image_processor_dict()




 def   UpperCamelCase__							(self ) ->					Any:






  """simple docstring"""





  UpperCAmelCase__							=				self.image_processing_class(**self.image_processor_dict )
  self.assertTrue(hasattr(__a       ,       'do_resize' ) )
  self.assertTrue(hasattr(__a       ,       'size' ) )
  self.assertTrue(hasattr(__a       ,       'do_center_crop' ) )
  self.assertTrue(hasattr(__a       ,       'center_crop' ) )
  self.assertTrue(hasattr(__a       ,       'do_normalize' ) )
  self.assertTrue(hasattr(__a       ,       'image_mean' ) )
  self.assertTrue(hasattr(__a       ,       'image_std' ) )
  self.assertTrue(hasattr(__a       ,       'do_convert_rgb' ) )




 def   UpperCamelCase__							(self ) ->					str:






  """simple docstring"""





  UpperCAmelCase__							=				self.image_processing_class.from_dict(self.image_processor_dict )
  self.assertEqual(image_processor.size       ,       {'shortest_edge': 20} )
  self.assertEqual(image_processor.crop_size       ,       {'height': 18, 'width': 18} )

  UpperCAmelCase__							=				self.image_processing_class.from_dict(self.image_processor_dict       ,       size=42       ,       crop_size=84 )
  self.assertEqual(image_processor.size       ,       {'shortest_edge': 42} )
  self.assertEqual(image_processor.crop_size       ,       {'height': 84, 'width': 84} )




 def   UpperCamelCase__							(self ) ->					List[Any]:






  """simple docstring"""





  pass




 def   UpperCamelCase__							(self ) ->					Dict:






  """simple docstring"""





  UpperCAmelCase__							=				self.image_processing_class(**self.image_processor_dict )
  # create random PIL images
  UpperCAmelCase__							=				self.image_processor_tester.prepare_inputs(equal_resolution=__a )
  for image in image_inputs:
   self.assertIsInstance(__a       ,       Image.Image )

  # Test not batched input
  UpperCAmelCase__							=				image_processing(image_inputs[0]       ,       return_tensors='pt' ).pixel_values
  self.assertEqual(
      encoded_images.shape       ,       (
          1,
          self.image_processor_tester.num_channels,
          self.image_processor_tester.crop_size['height'],
          self.image_processor_tester.crop_size['width'],
      )       ,       )

  # Test batched
  UpperCAmelCase__							=				image_processing(__a       ,       return_tensors='pt' ).pixel_values
  self.assertEqual(
      encoded_images.shape       ,       (
          self.image_processor_tester.batch_size,
          self.image_processor_tester.num_channels,
          self.image_processor_tester.crop_size['height'],
          self.image_processor_tester.crop_size['width'],
      )       ,       )




 def   UpperCamelCase__							(self ) ->					Optional[Any]:






  """simple docstring"""





  UpperCAmelCase__							=				self.image_processing_class(**self.image_processor_dict )
  # create random numpy tensors
  UpperCAmelCase__							=				self.image_processor_tester.prepare_inputs(equal_resolution=__a       ,       numpify=__a )
  for image in image_inputs:
   self.assertIsInstance(__a       ,       np.ndarray )

  # Test not batched input
  UpperCAmelCase__							=				image_processing(image_inputs[0]       ,       return_tensors='pt' ).pixel_values
  self.assertEqual(
      encoded_images.shape       ,       (
          1,
          self.image_processor_tester.num_channels,
          self.image_processor_tester.crop_size['height'],
          self.image_processor_tester.crop_size['width'],
      )       ,       )

  # Test batched
  UpperCAmelCase__							=				image_processing(__a       ,       return_tensors='pt' ).pixel_values
  self.assertEqual(
      encoded_images.shape       ,       (
          self.image_processor_tester.batch_size,
          self.image_processor_tester.num_channels,
          self.image_processor_tester.crop_size['height'],
          self.image_processor_tester.crop_size['width'],
      )       ,       )




 def   UpperCamelCase__							(self ) ->					Tuple:






  """simple docstring"""





  UpperCAmelCase__							=				self.image_processing_class(**self.image_processor_dict )
  # create random PyTorch tensors
  UpperCAmelCase__							=				self.image_processor_tester.prepare_inputs(equal_resolution=__a       ,       torchify=__a )
  for image in image_inputs:
   self.assertIsInstance(__a       ,       torch.Tensor )

  # Test not batched input
  UpperCAmelCase__							=				image_processing(image_inputs[0]       ,       return_tensors='pt' ).pixel_values
  self.assertEqual(
      encoded_images.shape       ,       (
          1,
          self.image_processor_tester.num_channels,
          self.image_processor_tester.crop_size['height'],
          self.image_processor_tester.crop_size['width'],
      )       ,       )

  # Test batched
  UpperCAmelCase__							=				image_processing(__a       ,       return_tensors='pt' ).pixel_values
  self.assertEqual(
      encoded_images.shape       ,       (
          self.image_processor_tester.batch_size,
          self.image_processor_tester.num_channels,
          self.image_processor_tester.crop_size['height'],
          self.image_processor_tester.crop_size['width'],
      )       ,       )







@require_torch
@require_vision
class     lowercase						(       _UpperCamelCase ,				unittest.TestCase					):



 '''simple docstring'''


 __SCREAMING_SNAKE_CASE												=				CLIPImageProcessor if is_vision_available() else None




 def   UpperCamelCase__							(self ) ->					Optional[int]:






  """simple docstring"""





  UpperCAmelCase__							=				CLIPImageProcessingTester(self       ,       num_channels=4 )
  UpperCAmelCase__							=				3




 @property
 def   UpperCamelCase__							(self ) ->					Optional[int]:






  """simple docstring"""





  return self.image_processor_tester.prepare_image_processor_dict()




 def   UpperCamelCase__							(self ) ->					List[str]:






  """simple docstring"""





  UpperCAmelCase__							=				self.image_processing_class(**self.image_processor_dict )
  self.assertTrue(hasattr(__a       ,       'do_resize' ) )
  self.assertTrue(hasattr(__a       ,       'size' ) )
  self.assertTrue(hasattr(__a       ,       'do_center_crop' ) )
  self.assertTrue(hasattr(__a       ,       'center_crop' ) )
  self.assertTrue(hasattr(__a       ,       'do_normalize' ) )
  self.assertTrue(hasattr(__a       ,       'image_mean' ) )
  self.assertTrue(hasattr(__a       ,       'image_std' ) )
  self.assertTrue(hasattr(__a       ,       'do_convert_rgb' ) )




 def   UpperCamelCase__							(self ) ->					Tuple:






  """simple docstring"""





  pass




 def   UpperCamelCase__							(self ) ->					Optional[int]:






  """simple docstring"""





  UpperCAmelCase__							=				self.image_processing_class(**self.image_processor_dict )
  # create random PIL images
  UpperCAmelCase__							=				self.image_processor_tester.prepare_inputs(equal_resolution=__a )
  for image in image_inputs:
   self.assertIsInstance(__a       ,       Image.Image )

  # Test not batched input
  UpperCAmelCase__							=				image_processing(image_inputs[0]       ,       return_tensors='pt' ).pixel_values
  self.assertEqual(
      encoded_images.shape       ,       (
          1,
          self.expected_encoded_image_num_channels,
          self.image_processor_tester.crop_size['height'],
          self.image_processor_tester.crop_size['width'],
      )       ,       )

  # Test batched
  UpperCAmelCase__							=				image_processing(__a       ,       return_tensors='pt' ).pixel_values
  self.assertEqual(
      encoded_images.shape       ,       (
          self.image_processor_tester.batch_size,
          self.expected_encoded_image_num_channels,
          self.image_processor_tester.crop_size['height'],
          self.image_processor_tester.crop_size['width'],
      )       ,       )



