


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       :Tuple      ,__snake_case       :Optional[int]      ,__snake_case       :List[Any]=13      ,__snake_case       :Dict=7      ,__snake_case       :Tuple=True      ,__snake_case       :List[Any]=True      ,__snake_case       :int=True      ,__snake_case       :List[str]=99      ,__snake_case       :List[str]=32      ,__snake_case       :Tuple=5      ,__snake_case       :Dict=4      ,__snake_case       :str=37      ,__snake_case       :int="gelu"      ,__snake_case       :Dict=0.1      ,__snake_case       :Optional[int]=0.1      ,__snake_case       :int=5_12      ,__snake_case       :Union[str, Any]=16      ,__snake_case       :Tuple=2      ,__snake_case       :int=0.02      ,__snake_case       :Any=3      ,__snake_case       :List[str]=4      ,__snake_case       :List[Any]=None      ,) ->   Optional[int]:
												a__					=					parent
												a__					=					batch_size
												a__					=					seq_length
												a__					=					is_training
												a__					=					use_token_type_ids
												a__					=					use_labels
												a__					=					vocab_size
												a__					=					hidden_size
												a__					=					num_hidden_layers
												a__					=					num_attention_heads
												a__					=					intermediate_size
												a__					=					hidden_act
												a__					=					hidden_dropout_prob
												a__					=					attention_probs_dropout_prob
												a__					=					max_position_embeddings
												a__					=					type_vocab_size
												a__					=					type_sequence_label_size
												a__					=					initializer_range
												a__					=					num_labels
												a__					=					num_choices
												a__					=					scope
												a__					=					self.vocab_size - 1
					def 			lowerCamelCase__(				self       :str				) ->   str:
												a__					=					ids_tensor([self.batch_size, self.seq_length]      ,self.vocab_size				)

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

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

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

												a__					=					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       :Dict      ,__snake_case       :Tuple      ,__snake_case       :List[str]      ,__snake_case       :Tuple      ,__snake_case       :Tuple      ,*__snake_case       :int				) ->   Tuple:
												a__					=					OpenAIGPTModel(config=__snake_case				)
												model.to(__snake_case				)
												model.eval()

												a__					=					model(__snake_case      ,token_type_ids=__snake_case      ,head_mask=__snake_case				)
												a__					=					model(__snake_case      ,token_type_ids=__snake_case				)
												a__					=					model(__snake_case				)

												self.parent.assertEqual(result.last_hidden_state.shape      ,(self.batch_size, self.seq_length, self.hidden_size)				)
					def 			lowerCamelCase__(				self       :Optional[Any]      ,__snake_case       :Dict      ,__snake_case       :Tuple      ,__snake_case       :str      ,__snake_case       :Any      ,*__snake_case       :Tuple				) ->   Union[str, Any]:
												a__					=					OpenAIGPTLMHeadModel(__snake_case				)
												model.to(__snake_case				)
												model.eval()

												a__					=					model(__snake_case      ,token_type_ids=__snake_case      ,labels=__snake_case				)
												self.parent.assertEqual(result.loss.shape      ,()				)
												self.parent.assertEqual(result.logits.shape      ,(self.batch_size, self.seq_length, self.vocab_size)				)
					def 			lowerCamelCase__(				self       :Tuple      ,__snake_case       :Tuple      ,__snake_case       :str      ,__snake_case       :Optional[Any]      ,__snake_case       :int      ,*__snake_case       :List[str]				) ->   List[Any]:
												a__					=					OpenAIGPTDoubleHeadsModel(__snake_case				)
												model.to(__snake_case				)
												model.eval()

												a__					=					model(__snake_case      ,token_type_ids=__snake_case      ,labels=__snake_case				)
												self.parent.assertEqual(result.loss.shape      ,()				)
												self.parent.assertEqual(result.logits.shape      ,(self.batch_size, self.seq_length, self.vocab_size)				)
					def 			lowerCamelCase__(				self       :Any      ,__snake_case       :Dict      ,__snake_case       :List[Any]      ,__snake_case       :Optional[Any]      ,__snake_case       :Tuple      ,*__snake_case       :Dict				) ->   List[Any]:
												a__					=					self.num_labels
												a__					=					OpenAIGPTForSequenceClassification(__snake_case				)
												model.to(__snake_case				)
												model.eval()

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



					def 			lowerCamelCase__(				self       :str				) ->   Any:
												a__					=					self.prepare_config_and_inputs()
												(
												    (
												    a__
												)				, (
												    a__
												)				, (
												    a__
												)				, (
												    a__
												)				, (
												    a__
												)				, (
												    a__
												)				, (
												    a__
												)				, 
												)					=					config_and_inputs
												a__					=					{
												    'input_ids': input_ids,
												    'token_type_ids': token_type_ids,
												    'head_mask': head_mask,
												}

												return config, inputs_dict







@require_torch
class 	snake_case_       (lowerCamelCase_     ,						lowerCamelCase_     ,						lowerCamelCase_     ,						unittest.TestCase				):
					UpperCAmelCase__ :   str   				=      (
					    (OpenAIGPTModel, OpenAIGPTLMHeadModel, OpenAIGPTDoubleHeadsModel, OpenAIGPTForSequenceClassification)
					    if is_torch_available()
					    else ()
					)
					UpperCAmelCase__ :   Dict   				=      (
					    (OpenAIGPTLMHeadModel,) if is_torch_available() else ()
					)  # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly
					UpperCAmelCase__ :   Optional[Any]   				=      (
					    {
					        '''feature-extraction''': OpenAIGPTModel,
					        '''text-classification''': OpenAIGPTForSequenceClassification,
					        '''text-generation''': OpenAIGPTLMHeadModel,
					        '''zero-shot''': OpenAIGPTForSequenceClassification,
					    }
					    if is_torch_available()
					    else {}
					)
					def 			lowerCamelCase__(				self       :Any      ,__snake_case       :Optional[int]      ,__snake_case       :Any      ,__snake_case       :Union[str, Any]      ,__snake_case       :Optional[int]      ,__snake_case       :List[Any]				) ->   int:
												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       :str      ,__snake_case       :List[str]      ,__snake_case       :List[Any]      ,__snake_case       :List[Any]=False				) ->   Optional[int]:
												a__					=					super()._prepare_for_class(__snake_case      ,__snake_case      ,return_labels=__snake_case				)

												if return_labels:
																			if model_class.__name__ == "OpenAIGPTDoubleHeadsModel":
																										a__					=					torch.zeros(
																										    (self.model_tester.batch_size, self.model_tester.num_choices, self.model_tester.seq_length)      ,dtype=torch.long      ,device=__snake_case      ,)
																										a__					=					inputs_dict['labels']
																										a__					=					inputs_dict['labels']
																										a__					=					torch.zeros(
																										    (self.model_tester.batch_size, self.model_tester.num_choices)      ,dtype=torch.long      ,device=__snake_case      ,)
																										a__					=					torch.zeros(
																										    self.model_tester.batch_size      ,dtype=torch.long      ,device=__snake_case				)
												return inputs_dict
					def 			lowerCamelCase__(				self       :Any				) ->   Tuple:
												a__					=					OpenAIGPTModelTester(self				)
												a__					=					ConfigTester(self      ,config_class=__snake_case      ,n_embd=37				)
					def 			lowerCamelCase__(				self       :Union[str, Any]				) ->   Union[str, Any]:
												self.config_tester.run_common_tests()
					def 			lowerCamelCase__(				self       :List[str]				) ->   Optional[int]:
												a__					=					self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_openai_gpt_model(*__snake_case				)
					def 			lowerCamelCase__(				self       :int				) ->   List[Any]:
												a__					=					self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_lm_head_model(*__snake_case				)
					def 			lowerCamelCase__(				self       :int				) ->   List[str]:
												a__					=					self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_double_lm_head_model(*__snake_case				)
					def 			lowerCamelCase__(				self       :Optional[Any]				) ->   int:
												a__					=					self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*__snake_case				)



					@slow
					def 			lowerCamelCase__(				self       :Dict				) ->   int:
												for model_name in OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																			a__					=					OpenAIGPTModel.from_pretrained(__snake_case				)
																			self.assertIsNotNone(__snake_case				)







@require_torch
class 	snake_case_       (unittest.TestCase				):
					@slow
					def 			lowerCamelCase__(				self       :Optional[int]				) ->   str:
												a__					=					OpenAIGPTLMHeadModel.from_pretrained('openai-gpt'				)
												model.to(__snake_case				)
												a__					=					torch.tensor([[4_81, 47_35, 5_44]]      ,dtype=torch.long      ,device=__snake_case				)  # the president is
												a__					=					[
												    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

												a__					=					model.generate(__snake_case      ,do_sample=__snake_case				)
												self.assertListEqual(output_ids[0].tolist()      ,__snake_case				)








from typing import Dict, List, Optional, Union

import numpy as np

from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict
from ...image_transforms import rescale, resize, to_channel_dimension_format
from ...image_utils import (
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    make_list_of_images,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, is_vision_available, logging


if is_vision_available():
						import PIL


snake_case							:	str					      =				logging.get_logger(__name__)






def 							__lowercase       (			__lowerCAmelCase							:    Union[str, Any] ,   __lowerCAmelCase							:    Optional[Any]       ):
							a__					=					b.T
							a__					=					np.sum(np.square(__lowerCAmelCase       ) ,   axis=1       )
							a__					=					np.sum(np.square(__lowerCAmelCase       ) ,   axis=0       )
							a__					=					np.matmul(__lowerCAmelCase ,   __lowerCAmelCase       )
							a__					=					aa[:, None] - 2 * ab + ba[None, :]
							return d






def 							__lowercase       (			__lowerCAmelCase							:    List[Any] ,   __lowerCAmelCase							:    str       ):
							a__					=					x.reshape(-1 ,   3       )
							a__					=					squared_euclidean_distance(__lowerCAmelCase ,   __lowerCAmelCase       )
							return np.argmin(__lowerCAmelCase ,   axis=1       )







class 	snake_case_       (lowerCamelCase_				):
					UpperCAmelCase__ :   Optional[Any]   				=      ['''pixel_values''']
					def __init__(				self       :Dict      ,__snake_case       :Optional[Union[List[List[int]], np.ndarray]] = None      ,__snake_case       :bool = True      ,__snake_case       :Dict[str, int] = None      ,__snake_case       :PILImageResampling = PILImageResampling.BILINEAR      ,__snake_case       :bool = True      ,__snake_case       :bool = True      ,**__snake_case       :Optional[int]      ,) ->   None:
												super().__init__(**__snake_case				)
												a__					=					size if size is not None else {'height': 2_56, 'width': 2_56}
												a__					=					get_size_dict(__snake_case				)
												a__					=					np.array(__snake_case				) if clusters is not None else None
												a__					=					do_resize
												a__					=					size
												a__					=					resample
												a__					=					do_normalize
												a__					=					do_color_quantize
					def 			lowerCamelCase__(				self       :Union[str, Any]      ,__snake_case       :np.ndarray      ,__snake_case       :Dict[str, int]      ,__snake_case       :PILImageResampling = PILImageResampling.BILINEAR      ,__snake_case       :Optional[Union[str, ChannelDimension]] = None      ,**__snake_case       :Any      ,) ->   np.ndarray:
												a__					=					get_size_dict(__snake_case				)
												if "height" not in size or "width" not in size:
																			raise ValueError(F'Size dictionary must contain both height and width keys. Got {size.keys()}'				)
												return resize(
												    __snake_case      ,size=(size['height'], size['width'])      ,resample=__snake_case      ,data_format=__snake_case      ,**__snake_case				)
					def 			lowerCamelCase__(				self       :List[Any]      ,__snake_case       :np.ndarray      ,__snake_case       :Optional[Union[str, ChannelDimension]] = None      ,) ->   np.ndarray:
												a__					=					rescale(image=__snake_case      ,scale=1 / 1_27.5      ,data_format=__snake_case				)
												a__					=					image - 1
												return image



					def 			lowerCamelCase__(				self       :Optional[Any]      ,__snake_case       :ImageInput      ,__snake_case       :bool = None      ,__snake_case       :Dict[str, int] = None      ,__snake_case       :PILImageResampling = None      ,__snake_case       :bool = None      ,__snake_case       :Optional[bool] = None      ,__snake_case       :Optional[Union[List[List[int]], np.ndarray]] = None      ,__snake_case       :Optional[Union[str, TensorType]] = None      ,__snake_case       :Optional[Union[str, ChannelDimension]] = ChannelDimension.FIRST      ,**__snake_case       :List[str]      ,) ->   PIL.Image.Image:
												a__					=					do_resize if do_resize is not None else self.do_resize
												a__					=					size if size is not None else self.size
												a__					=					get_size_dict(__snake_case				)
												a__					=					resample if resample is not None else self.resample
												a__					=					do_normalize if do_normalize is not None else self.do_normalize
												a__					=					do_color_quantize if do_color_quantize is not None else self.do_color_quantize
												a__					=					clusters if clusters is not None else self.clusters
												a__					=					np.array(__snake_case				)

												a__					=					make_list_of_images(__snake_case				)

												if not valid_images(__snake_case				):
																			raise ValueError(
																			    'Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, '
																			    'torch.Tensor, tf.Tensor or jax.ndarray.'				)

												if do_resize and size is None or resample is None:
																			raise ValueError('Size and resample must be specified if do_resize is True.'				)

												if do_color_quantize and clusters is None:
																			raise ValueError('Clusters must be specified if do_color_quantize is True.'				)

												# All transformations expect numpy arrays.
												a__					=					[to_numpy_array(__snake_case				) for image in images]

												if do_resize:
																			a__					=					[self.resize(image=__snake_case      ,size=__snake_case      ,resample=__snake_case				) for image in images]

												if do_normalize:
																			a__					=					[self.normalize(image=__snake_case				) for image in images]

												if do_color_quantize:
																			a__					=					[to_channel_dimension_format(__snake_case      ,ChannelDimension.LAST				) for image in images]
																			# color quantize from (batch_size, height, width, 3) to (batch_size, height, width)
																			a__					=					np.array(__snake_case				)
																			a__					=					color_quantize(__snake_case      ,__snake_case				).reshape(images.shape[:-1]				)

																			# flatten to (batch_size, height*width)
																			a__					=					images.shape[0]
																			a__					=					images.reshape(__snake_case      ,-1				)

																			# We need to convert back to a list of images to keep consistent behaviour across processors.
																			a__					=					list(__snake_case				)
												else:
																			a__					=					[to_channel_dimension_format(__snake_case      ,__snake_case				) for image in images]

												a__					=					{'input_ids': images}
												return BatchFeature(data=__snake_case      ,tensor_type=__snake_case				)




