






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 							__a							:





		'''simple docstring'''



		def __init__(						self ,    UpperCamelCase__ ,    UpperCamelCase__=13 ,    UpperCamelCase__=7 ,    UpperCamelCase__=True ,    UpperCamelCase__=True ,    UpperCamelCase__=True ,    UpperCamelCase__=99 ,    UpperCamelCase__=32 ,    UpperCamelCase__=5 ,    UpperCamelCase__=4 ,    UpperCamelCase__=37 ,    UpperCamelCase__="gelu" ,    UpperCamelCase__=0.1 ,    UpperCamelCase__=0.1 ,    UpperCamelCase__=512 ,    UpperCamelCase__=16 ,    UpperCamelCase__=2 ,    UpperCamelCase__=0.02 ,    UpperCamelCase__=3 ,    UpperCamelCase__=4 ,    UpperCamelCase__=None ,    ):
						SCREAMING_SNAKE_CASE_   :	Dict													=   parent
						SCREAMING_SNAKE_CASE_   :	Tuple													=   batch_size
						SCREAMING_SNAKE_CASE_   :	Optional[int]													=   seq_length
						SCREAMING_SNAKE_CASE_   :	Tuple													=   is_training
						SCREAMING_SNAKE_CASE_   :	Tuple													=   use_token_type_ids
						SCREAMING_SNAKE_CASE_   :	Union[str, Any]													=   use_labels
						SCREAMING_SNAKE_CASE_   :	int													=   vocab_size
						SCREAMING_SNAKE_CASE_   :	List[Any]													=   hidden_size
						SCREAMING_SNAKE_CASE_   :	int													=   num_hidden_layers
						SCREAMING_SNAKE_CASE_   :	int													=   num_attention_heads
						SCREAMING_SNAKE_CASE_   :	Dict													=   intermediate_size
						SCREAMING_SNAKE_CASE_   :	Dict													=   hidden_act
						SCREAMING_SNAKE_CASE_   :	Union[str, Any]													=   hidden_dropout_prob
						SCREAMING_SNAKE_CASE_   :	Any													=   attention_probs_dropout_prob
						SCREAMING_SNAKE_CASE_   :	Dict													=   max_position_embeddings
						SCREAMING_SNAKE_CASE_   :	Dict													=   type_vocab_size
						SCREAMING_SNAKE_CASE_   :	Union[str, Any]													=   type_sequence_label_size
						SCREAMING_SNAKE_CASE_   :	Tuple													=   initializer_range
						SCREAMING_SNAKE_CASE_   :	Tuple													=   num_labels
						SCREAMING_SNAKE_CASE_   :	Dict													=   num_choices
						SCREAMING_SNAKE_CASE_   :	Union[str, Any]													=   scope
						SCREAMING_SNAKE_CASE_   :	Any													=   self.vocab_size - 1


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

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

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

						SCREAMING_SNAKE_CASE_   :	List[Any]													=   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_   :	Tuple													=   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   __snake_case			(						self ,    UpperCamelCase__ ,    UpperCamelCase__ ,    UpperCamelCase__ ,    UpperCamelCase__ ,    *UpperCamelCase__       ):
						SCREAMING_SNAKE_CASE_   :	int													=   OpenAIGPTModel(config=UpperCamelCase__       )
						model.to(UpperCamelCase__       )
						model.eval()

						SCREAMING_SNAKE_CASE_   :	str													=   model(UpperCamelCase__ ,    token_type_ids=UpperCamelCase__ ,    head_mask=UpperCamelCase__       )
						SCREAMING_SNAKE_CASE_   :	Tuple													=   model(UpperCamelCase__ ,    token_type_ids=UpperCamelCase__       )
						SCREAMING_SNAKE_CASE_   :	Any													=   model(UpperCamelCase__       )

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


		def   __snake_case			(						self ,    UpperCamelCase__ ,    UpperCamelCase__ ,    UpperCamelCase__ ,    UpperCamelCase__ ,    *UpperCamelCase__       ):
						SCREAMING_SNAKE_CASE_   :	Tuple													=   OpenAIGPTLMHeadModel(UpperCamelCase__       )
						model.to(UpperCamelCase__       )
						model.eval()

						SCREAMING_SNAKE_CASE_   :	List[str]													=   model(UpperCamelCase__ ,    token_type_ids=UpperCamelCase__ ,    labels=UpperCamelCase__       )
						self.parent.assertEqual(result.loss.shape ,    ()       )
						self.parent.assertEqual(result.logits.shape ,    (self.batch_size, self.seq_length, self.vocab_size)       )


		def   __snake_case			(						self ,    UpperCamelCase__ ,    UpperCamelCase__ ,    UpperCamelCase__ ,    UpperCamelCase__ ,    *UpperCamelCase__       ):
						SCREAMING_SNAKE_CASE_   :	Any													=   OpenAIGPTDoubleHeadsModel(UpperCamelCase__       )
						model.to(UpperCamelCase__       )
						model.eval()

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


		def   __snake_case			(						self ,    UpperCamelCase__ ,    UpperCamelCase__ ,    UpperCamelCase__ ,    UpperCamelCase__ ,    *UpperCamelCase__       ):
						SCREAMING_SNAKE_CASE_   :	str													=   self.num_labels
						SCREAMING_SNAKE_CASE_   :	int													=   OpenAIGPTForSequenceClassification(UpperCamelCase__       )
						model.to(UpperCamelCase__       )
						model.eval()

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






		def   __snake_case			(						self       ):
						SCREAMING_SNAKE_CASE_   :	Optional[int]													=   self.prepare_config_and_inputs()
						(
						    (
						    SCREAMING_SNAKE_CASE_
						)							,       (
						    SCREAMING_SNAKE_CASE_
						)							,       (
						    SCREAMING_SNAKE_CASE_
						)							,       (
						    SCREAMING_SNAKE_CASE_
						)							,       (
						    SCREAMING_SNAKE_CASE_
						)							,       (
						    SCREAMING_SNAKE_CASE_
						)							,       (
						    SCREAMING_SNAKE_CASE_
						)							,       
						)   :	Optional[Any]													=   config_and_inputs
						SCREAMING_SNAKE_CASE_   :	List[Any]													=   {
						    'input_ids': input_ids,
						    'token_type_ids': token_type_ids,
						    'head_mask': head_mask,
						}

						return config, inputs_dict






@require_torch
class 							__a							(  __A ,	__A ,	__A ,	unittest.TestCase						):





		'''simple docstring'''



		UpperCAmelCase__      :				str				    =						(
		    (OpenAIGPTModel, OpenAIGPTLMHeadModel, OpenAIGPTDoubleHeadsModel, OpenAIGPTForSequenceClassification)
		    if is_torch_available()
		    else ()
		)
		UpperCAmelCase__      :				Union[str, Any]				    =						(
		    (OpenAIGPTLMHeadModel,) if is_torch_available() else ()
		)  # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly
		UpperCAmelCase__      :				Dict				    =						(
		    {
		        """feature-extraction""": OpenAIGPTModel,
		        """text-classification""": OpenAIGPTForSequenceClassification,
		        """text-generation""": OpenAIGPTLMHeadModel,
		        """zero-shot""": OpenAIGPTForSequenceClassification,
		    }
		    if is_torch_available()
		    else {}
		)


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

						if return_labels:
										if model_class.__name__ == "OpenAIGPTDoubleHeadsModel":
														SCREAMING_SNAKE_CASE_   :	str													=   torch.zeros(
														    (self.model_tester.batch_size, self.model_tester.num_choices, self.model_tester.seq_length) ,    dtype=torch.long ,    device=UpperCamelCase__ ,    )
														SCREAMING_SNAKE_CASE_   :	List[str]													=   inputs_dict['labels']
														SCREAMING_SNAKE_CASE_   :	Optional[int]													=   inputs_dict['labels']
														SCREAMING_SNAKE_CASE_   :	str													=   torch.zeros(
														    (self.model_tester.batch_size, self.model_tester.num_choices) ,    dtype=torch.long ,    device=UpperCamelCase__ ,    )
														SCREAMING_SNAKE_CASE_   :	Optional[Any]													=   torch.zeros(
														    self.model_tester.batch_size ,    dtype=torch.long ,    device=UpperCamelCase__       )
						return inputs_dict


		def   __snake_case			(						self       ):
						SCREAMING_SNAKE_CASE_   :	List[Any]													=   OpenAIGPTModelTester(self       )
						SCREAMING_SNAKE_CASE_   :	Union[str, Any]													=   ConfigTester(self ,    config_class=UpperCamelCase__ ,    n_embd=37       )


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


		def   __snake_case			(						self       ):
						SCREAMING_SNAKE_CASE_   :	Any													=   self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_openai_gpt_model(*UpperCamelCase__       )


		def   __snake_case			(						self       ):
						SCREAMING_SNAKE_CASE_   :	str													=   self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_lm_head_model(*UpperCamelCase__       )


		def   __snake_case			(						self       ):
						SCREAMING_SNAKE_CASE_   :	Dict													=   self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_double_lm_head_model(*UpperCamelCase__       )


		def   __snake_case			(						self       ):
						SCREAMING_SNAKE_CASE_   :	List[Any]													=   self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*UpperCamelCase__       )






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






@require_torch
class 							__a							(  unittest.TestCase						):





		'''simple docstring'''



		@slow
		def   __snake_case			(						self       ):
						SCREAMING_SNAKE_CASE_   :	List[Any]													=   OpenAIGPTLMHeadModel.from_pretrained('openai-gpt'       )
						model.to(UpperCamelCase__       )
						SCREAMING_SNAKE_CASE_   :	List[Any]													=   torch.tensor([[481, 4735, 544]] ,    dtype=torch.long ,    device=UpperCamelCase__       )  # the president is
						SCREAMING_SNAKE_CASE_   :	Any													=   [
						    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_   :	Optional[int]													=   model.generate(UpperCamelCase__ ,    do_sample=UpperCamelCase__       )
						self.assertListEqual(output_ids[0].tolist() ,    UpperCamelCase__       )







import colorsys

from PIL import Image  # type: ignore



def 					_lowerCamelCase(			lowerCAmelCase__				:		float    ,    lowerCAmelCase__				:		float    ,    lowerCAmelCase__				:		int      ):





				'''simple docstring'''







				SCREAMING_SNAKE_CASE_   :	Any													=   x
				SCREAMING_SNAKE_CASE_   :	Tuple													=   y
				for step in range(lowerCAmelCase__      ):  # noqa: B007
								SCREAMING_SNAKE_CASE_   :	str													=   a * a - b * b + x
								SCREAMING_SNAKE_CASE_   :	Any													=   2 * a * b + y
								SCREAMING_SNAKE_CASE_   :	int													=   a_new

								# divergence happens for all complex number with an absolute value
								# greater than 4
								if a * a + b * b > 4:
												break
				return step / (max_step - 1)



def 					_lowerCamelCase(			lowerCAmelCase__				:		float      ):





				'''simple docstring'''







				if distance == 1:
								return (0, 0, 0)
				else:
								return (255, 255, 255)



def 					_lowerCamelCase(			lowerCAmelCase__				:		float      ):





				'''simple docstring'''







				if distance == 1:
								return (0, 0, 0)
				else:
								return tuple(round(i * 255      ) for i in colorsys.hsv_to_rgb(lowerCAmelCase__    ,    1    ,    1      )      )



def 					_lowerCamelCase(			lowerCAmelCase__				:		int = 800    ,    lowerCAmelCase__				:		int = 600    ,    lowerCAmelCase__				:		float = -0.6    ,    lowerCAmelCase__				:		float = 0    ,    lowerCAmelCase__				:		float = 3.2    ,    lowerCAmelCase__				:		int = 50    ,    lowerCAmelCase__				:		bool = True    ,    ):





				'''simple docstring'''







				SCREAMING_SNAKE_CASE_   :	Optional[Any]													=   Image.new('RGB'    ,    (image_width, image_height)      )
				SCREAMING_SNAKE_CASE_   :	List[str]													=   img.load()

				# loop through the image-coordinates
				for image_x in range(lowerCAmelCase__      ):
								for image_y in range(lowerCAmelCase__      ):
												# determine the figure-coordinates based on the image-coordinates
												SCREAMING_SNAKE_CASE_   :	List[str]													=   figure_width / image_width * image_height
												SCREAMING_SNAKE_CASE_   :	List[Any]													=   figure_center_x + (image_x / image_width - 0.5) * figure_width
												SCREAMING_SNAKE_CASE_   :	Optional[Any]													=   figure_center_y + (image_y / image_height - 0.5) * figure_height

												SCREAMING_SNAKE_CASE_   :	str													=   get_distance(lowerCAmelCase__    ,    lowerCAmelCase__    ,    lowerCAmelCase__      )

												# color the corresponding pixel based on the selected coloring-function
												if use_distance_color_coding:
																SCREAMING_SNAKE_CASE_   :	int													=   get_color_coded_rgb(lowerCAmelCase__      )
												else:
																SCREAMING_SNAKE_CASE_   :	Dict													=   get_black_and_white_rgb(lowerCAmelCase__      )

				return img


if __name__ == "__main__":
				import doctest

				doctest.testmod()

				# colored version, full figure
				A        =			get_image()

				# uncomment for colored version, different section, zoomed in
				# img = get_image(figure_center_x = -0.6, figure_center_y = -0.4,
				# figure_width = 0.8)

				# uncomment for black and white version, full figure
				# img = get_image(use_distance_color_coding = False)

				# uncomment to save the image
				# img.save("mandelbrot.png")

				img.show()