
"""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			,						_SCREAMING_SNAKE_CASE			,						):
			__lowerCAmelCase     :		List[str]		     = parent
			__lowerCAmelCase     :		Optional[int]		     = 13
			__lowerCAmelCase     :		Optional[int]		     = 7
			__lowerCAmelCase     :		List[str]		     = True
			__lowerCAmelCase     :		Optional[int]		     = True
			__lowerCAmelCase     :		Any		     = True
			__lowerCAmelCase     :		Optional[Any]		     = True
			__lowerCAmelCase     :		str		     = True
			__lowerCAmelCase     :		Union[str, Any]		     = 99
			__lowerCAmelCase     :		List[str]		     = 32
			__lowerCAmelCase     :		Optional[int]		     = 2
			__lowerCAmelCase     :		List[Any]		     = 4
			__lowerCAmelCase     :		List[Any]		     = 37
			__lowerCAmelCase     :		Optional[int]		     = 'gelu'
			__lowerCAmelCase     :		List[Any]		     = 0.1
			__lowerCAmelCase     :		str		     = 0.1
			__lowerCAmelCase     :		Optional[int]		     = 5_12
			__lowerCAmelCase     :		str		     = 16
			__lowerCAmelCase     :		List[Any]		     = 2
			__lowerCAmelCase     :		Optional[Any]		     = 0.02
			__lowerCAmelCase     :		List[Any]		     = 3
			__lowerCAmelCase     :		List[str]		     = 4
			__lowerCAmelCase     :		Optional[Any]		     = None
			__lowerCAmelCase     :		Tuple		     = self.vocab_size - 1

		def 				__lowerCamelCase				( self					):
			__lowerCAmelCase     :		int		     = ids_tensor([self.batch_size, self.seq_length]			,						self.vocab_size					)

			__lowerCAmelCase     :		List[Any]		     = None
			if self.use_input_mask:
				__lowerCAmelCase     :		Optional[int]		     = random_attention_mask([self.batch_size, self.seq_length]					)

			__lowerCAmelCase     :		Dict		     = None
			if self.use_token_type_ids:
				__lowerCAmelCase     :		Optional[int]		     = ids_tensor([self.batch_size, self.seq_length]			,						self.type_vocab_size					)

			__lowerCAmelCase     :		Dict		     = None
			if self.use_mc_token_ids:
				__lowerCAmelCase     :		List[Any]		     = ids_tensor([self.batch_size, self.num_choices]			,						self.seq_length					)

			__lowerCAmelCase     :		Optional[int]		     = None
			__lowerCAmelCase     :		List[Any]		     = None
			__lowerCAmelCase     :		str		     = None
			if self.use_labels:
				__lowerCAmelCase     :		Tuple		     = ids_tensor([self.batch_size]			,						self.type_sequence_label_size					)
				__lowerCAmelCase     :		Any		     = ids_tensor([self.batch_size, self.seq_length]			,						self.num_labels					)
				__lowerCAmelCase     :		List[str]		     = ids_tensor([self.batch_size]			,						self.num_choices					)

			__lowerCAmelCase     :		Dict		     = 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			,						)

			__lowerCAmelCase     :		Optional[Any]		     = 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 				__lowerCamelCase				( self			,						_SCREAMING_SNAKE_CASE			,						_SCREAMING_SNAKE_CASE			,						_SCREAMING_SNAKE_CASE			,						_SCREAMING_SNAKE_CASE			,						_SCREAMING_SNAKE_CASE			,						*_SCREAMING_SNAKE_CASE					):
			__lowerCAmelCase     :		str		     = TFOpenAIGPTModel(config=_SCREAMING_SNAKE_CASE					)
			__lowerCAmelCase     :		Union[str, Any]		     = {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
			__lowerCAmelCase     :		List[str]		     = model(_SCREAMING_SNAKE_CASE					)

			__lowerCAmelCase     :		Optional[int]		     = [input_ids, input_mask]
			__lowerCAmelCase     :		List[Any]		     = model(_SCREAMING_SNAKE_CASE					)

			__lowerCAmelCase     :		Tuple		     = model(_SCREAMING_SNAKE_CASE					)

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

		def 				__lowerCamelCase				( self			,						_SCREAMING_SNAKE_CASE			,						_SCREAMING_SNAKE_CASE			,						_SCREAMING_SNAKE_CASE			,						_SCREAMING_SNAKE_CASE			,						_SCREAMING_SNAKE_CASE			,						*_SCREAMING_SNAKE_CASE					):
			__lowerCAmelCase     :		str		     = TFOpenAIGPTLMHeadModel(config=_SCREAMING_SNAKE_CASE					)
			__lowerCAmelCase     :		int		     = {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
			__lowerCAmelCase     :		Optional[Any]		     = model(_SCREAMING_SNAKE_CASE					)
			self.parent.assertEqual(result.logits.shape			,						(self.batch_size, self.seq_length, self.vocab_size)					)

		def 				__lowerCamelCase				( self			,						_SCREAMING_SNAKE_CASE			,						_SCREAMING_SNAKE_CASE			,						_SCREAMING_SNAKE_CASE			,						_SCREAMING_SNAKE_CASE			,						_SCREAMING_SNAKE_CASE			,						_SCREAMING_SNAKE_CASE			,						*_SCREAMING_SNAKE_CASE					):
			__lowerCAmelCase     :		int		     = TFOpenAIGPTDoubleHeadsModel(config=_SCREAMING_SNAKE_CASE					)

			__lowerCAmelCase     :		Optional[Any]		     = tf.tile(tf.expand_dims(_SCREAMING_SNAKE_CASE			,						1					)			,						(1, self.num_choices, 1)					)
			__lowerCAmelCase     :		Dict		     = tf.tile(tf.expand_dims(_SCREAMING_SNAKE_CASE			,						1					)			,						(1, self.num_choices, 1)					)
			__lowerCAmelCase     :		List[str]		     = tf.tile(tf.expand_dims(_SCREAMING_SNAKE_CASE			,						1					)			,						(1, self.num_choices, 1)					)

			__lowerCAmelCase     :		int		     = {
			    '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,
			}
			__lowerCAmelCase     :		str		     = model(_SCREAMING_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 				__lowerCamelCase				( self			,						_SCREAMING_SNAKE_CASE			,						_SCREAMING_SNAKE_CASE			,						_SCREAMING_SNAKE_CASE			,						_SCREAMING_SNAKE_CASE			,						_SCREAMING_SNAKE_CASE			,						*_SCREAMING_SNAKE_CASE					):
			__lowerCAmelCase     :		Optional[Any]		     = self.num_labels
			__lowerCAmelCase     :		Optional[int]		     = ids_tensor([self.batch_size]			,						self.type_sequence_label_size					)
			__lowerCAmelCase     :		List[str]		     = {
			    'input_ids': input_ids,
			    'attention_mask': input_mask,
			    'token_type_ids': token_type_ids,
			    'labels': sequence_labels,
			}
			__lowerCAmelCase     :		Union[str, Any]		     = TFOpenAIGPTForSequenceClassification(_SCREAMING_SNAKE_CASE					)
			__lowerCAmelCase     :		List[str]		     = model(_SCREAMING_SNAKE_CASE					)
			self.parent.assertEqual(result.logits.shape			,						(self.batch_size, self.num_labels)					)







		def 				__lowerCamelCase				( self					):
			__lowerCAmelCase     :		List[Any]		     = self.prepare_config_and_inputs()

			(
			    (
			    __lowerCAmelCase
			)     ,  (
			    __lowerCAmelCase
			)     ,  (
			    __lowerCAmelCase
			)     ,  (
			    __lowerCAmelCase
			)     ,  (
			    __lowerCAmelCase
			)     ,  (
			    __lowerCAmelCase
			)     ,  (
			    __lowerCAmelCase
			)     ,  (
			    __lowerCAmelCase
			)     ,  (
			    __lowerCAmelCase
			)     ,  
			)     :		Optional[Any]		     = config_and_inputs

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




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

		def 				__lowerCamelCase				( self			,						_SCREAMING_SNAKE_CASE			,						_SCREAMING_SNAKE_CASE			,						_SCREAMING_SNAKE_CASE			,						_SCREAMING_SNAKE_CASE			,						_SCREAMING_SNAKE_CASE					):
			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					):
			__lowerCAmelCase     :		List[Any]		     = TFOpenAIGPTModelTester(self					)
			__lowerCAmelCase     :		Optional[int]		     = ConfigTester(self			,						config_class=_SCREAMING_SNAKE_CASE			,						n_embd=37					)

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

		def 				__lowerCamelCase				( self					):
			__lowerCAmelCase     :		Dict		     = self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_openai_gpt_model(*_SCREAMING_SNAKE_CASE					)

		def 				__lowerCamelCase				( self					):
			__lowerCAmelCase     :		Union[str, Any]		     = self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_openai_gpt_lm_head(*_SCREAMING_SNAKE_CASE					)

		def 				__lowerCamelCase				( self					):
			__lowerCAmelCase     :		List[str]		     = self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_openai_gpt_double_head(*_SCREAMING_SNAKE_CASE					)

		def 				__lowerCamelCase				( self					):
			__lowerCAmelCase     :		List[Any]		     = self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*_SCREAMING_SNAKE_CASE					)







		@slow
		def 				__lowerCamelCase				( self					):
			for model_name in TF_OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
				__lowerCAmelCase     :		Dict		     = TFOpenAIGPTModel.from_pretrained(_SCREAMING_SNAKE_CASE					)
				self.assertIsNotNone(_SCREAMING_SNAKE_CASE					)




@require_tf
class 					A__			(      unittest.TestCase):

		@slow
		def 				__lowerCamelCase				( self					):
			__lowerCAmelCase     :		Any		     = TFOpenAIGPTLMHeadModel.from_pretrained('openai-gpt'					)
			__lowerCAmelCase     :		int		     = tf.convert_to_tensor([[4_81, 47_35, 5_44]]			,						dtype=tf.intaa					)  # the president is
			__lowerCAmelCase     :		Any		     = [
			    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

			__lowerCAmelCase     :		int		     = model.generate(_SCREAMING_SNAKE_CASE			,						do_sample=_SCREAMING_SNAKE_CASE					)
			self.assertListEqual(output_ids[0].numpy().tolist()			,						_SCREAMING_SNAKE_CASE					)

"""simple docstring"""







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, prepare_image_inputs


if is_torch_available():
							import torch

if is_vision_available():
							from PIL import Image

							from transformers import DeiTImageProcessor




class 					A__			(      unittest.TestCase):

		def __init__( self			,						_SCREAMING_SNAKE_CASE			,						_SCREAMING_SNAKE_CASE=7			,						_SCREAMING_SNAKE_CASE=3			,						_SCREAMING_SNAKE_CASE=18			,						_SCREAMING_SNAKE_CASE=30			,						_SCREAMING_SNAKE_CASE=4_00			,						_SCREAMING_SNAKE_CASE=True			,						_SCREAMING_SNAKE_CASE=None			,						_SCREAMING_SNAKE_CASE=True			,						_SCREAMING_SNAKE_CASE=None			,						_SCREAMING_SNAKE_CASE=True			,						_SCREAMING_SNAKE_CASE=[0.5, 0.5, 0.5]			,						_SCREAMING_SNAKE_CASE=[0.5, 0.5, 0.5]			,						):
			__lowerCAmelCase     :		List[str]		     = size if size is not None else {'height': 20, 'width': 20}
			__lowerCAmelCase     :		List[str]		     = crop_size if crop_size is not None else {'height': 18, 'width': 18}

			__lowerCAmelCase     :		Any		     = parent
			__lowerCAmelCase     :		Optional[Any]		     = batch_size
			__lowerCAmelCase     :		Any		     = num_channels
			__lowerCAmelCase     :		Dict		     = image_size
			__lowerCAmelCase     :		Tuple		     = min_resolution
			__lowerCAmelCase     :		Union[str, Any]		     = max_resolution
			__lowerCAmelCase     :		Optional[int]		     = do_resize
			__lowerCAmelCase     :		Any		     = size
			__lowerCAmelCase     :		Union[str, Any]		     = do_center_crop
			__lowerCAmelCase     :		List[Any]		     = crop_size
			__lowerCAmelCase     :		Optional[int]		     = do_normalize
			__lowerCAmelCase     :		int		     = image_mean
			__lowerCAmelCase     :		Tuple		     = image_std







		def 				__lowerCamelCase				( self					):
			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,
			}




@require_torch
@require_vision
class 					A__			(      _lowerCamelCase			,	unittest.TestCase):
		A_		:			List[str]							  =      DeiTImageProcessor if is_vision_available() else None
		A_		:			int							  =      True

		def 				__lowerCamelCase				( self					):
			__lowerCAmelCase     :		Optional[int]		     = DeiTImageProcessingTester(self					)

		@property
		def 				__lowerCamelCase				( self					):
			return self.image_processor_tester.prepare_image_processor_dict()

		def 				__lowerCamelCase				( self					):
			__lowerCAmelCase     :		Any		     = self.image_processing_class(**self.image_processor_dict					)
			self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE			,						'do_resize'					)					)
			self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE			,						'size'					)					)
			self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE			,						'do_center_crop'					)					)
			self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE			,						'center_crop'					)					)
			self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE			,						'do_normalize'					)					)
			self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE			,						'image_mean'					)					)
			self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE			,						'image_std'					)					)

		def 				__lowerCamelCase				( self					):
			__lowerCAmelCase     :		Tuple		     = self.image_processing_class.from_dict(self.image_processor_dict					)
			self.assertEqual(image_processor.size			,						{'height': 20, 'width': 20}					)
			self.assertEqual(image_processor.crop_size			,						{'height': 18, 'width': 18}					)

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

		def 				__lowerCamelCase				( self					):
			pass

		def 				__lowerCamelCase				( self					):
			# Initialize image_processing
			__lowerCAmelCase     :		Dict		     = self.image_processing_class(**self.image_processor_dict					)
			# create random PIL images
			__lowerCAmelCase     :		List[str]		     = prepare_image_inputs(self.image_processor_tester			,						equal_resolution=_SCREAMING_SNAKE_CASE					)
			for image in image_inputs:
				self.assertIsInstance(_SCREAMING_SNAKE_CASE			,						Image.Image					)

			# Test not batched input
			__lowerCAmelCase     :		Dict		     = 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
			__lowerCAmelCase     :		int		     = image_processing(_SCREAMING_SNAKE_CASE			,						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 				__lowerCamelCase				( self					):
			# Initialize image_processing
			__lowerCAmelCase     :		Optional[int]		     = self.image_processing_class(**self.image_processor_dict					)
			# create random numpy tensors
			__lowerCAmelCase     :		List[str]		     = prepare_image_inputs(self.image_processor_tester			,						equal_resolution=_SCREAMING_SNAKE_CASE			,						numpify=_SCREAMING_SNAKE_CASE					)
			for image in image_inputs:
				self.assertIsInstance(_SCREAMING_SNAKE_CASE			,						np.ndarray					)

			# Test not batched input
			__lowerCAmelCase     :		str		     = 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
			__lowerCAmelCase     :		Dict		     = image_processing(_SCREAMING_SNAKE_CASE			,						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 				__lowerCamelCase				( self					):
			# Initialize image_processing
			__lowerCAmelCase     :		Union[str, Any]		     = self.image_processing_class(**self.image_processor_dict					)
			# create random PyTorch tensors
			__lowerCAmelCase     :		List[str]		     = prepare_image_inputs(self.image_processor_tester			,						equal_resolution=_SCREAMING_SNAKE_CASE			,						torchify=_SCREAMING_SNAKE_CASE					)
			for image in image_inputs:
				self.assertIsInstance(_SCREAMING_SNAKE_CASE			,						torch.Tensor					)

			# Test not batched input
			__lowerCAmelCase     :		Union[str, Any]		     = 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
			__lowerCAmelCase     :		Optional[int]		     = image_processing(_SCREAMING_SNAKE_CASE			,						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'],
			    )			,						)