


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






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






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

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

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

				SCREAMING_SNAKE_CASE_				:int				     =						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				:     Dict   ,  UpperCAmelCase				:     Optional[int]   ,  UpperCAmelCase				:     Optional[int]   ,  UpperCAmelCase				:     List[Any]   ,  UpperCAmelCase				:     List[Any]   ,  *UpperCAmelCase				:     List[str]):
				SCREAMING_SNAKE_CASE_				:List[Any]				     =						OpenAIGPTModel(config=UpperCAmelCase)
				model.to(UpperCAmelCase)
				model.eval()

				SCREAMING_SNAKE_CASE_				:Any				     =						model(UpperCAmelCase   ,  token_type_ids=UpperCAmelCase   ,  head_mask=UpperCAmelCase)
				SCREAMING_SNAKE_CASE_				:Any				     =						model(UpperCAmelCase   ,  token_type_ids=UpperCAmelCase)
				SCREAMING_SNAKE_CASE_				:Dict				     =						model(UpperCAmelCase)

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






		def _snake_case		(						self				:     Any   ,  UpperCAmelCase				:     List[Any]   ,  UpperCAmelCase				:     Optional[Any]   ,  UpperCAmelCase				:     Optional[Any]   ,  UpperCAmelCase				:     List[Any]   ,  *UpperCAmelCase				:     List[str]):
				SCREAMING_SNAKE_CASE_				:Union[str, Any]				     =						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				:     Optional[int]   ,  UpperCAmelCase				:     Optional[Any]   ,  UpperCAmelCase				:     int   ,  UpperCAmelCase				:     List[str]   ,  UpperCAmelCase				:     Optional[Any]   ,  *UpperCAmelCase				:     Any):
				SCREAMING_SNAKE_CASE_				:List[str]				     =						OpenAIGPTDoubleHeadsModel(UpperCAmelCase)
				model.to(UpperCAmelCase)
				model.eval()

				SCREAMING_SNAKE_CASE_				:Dict				     =						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				:     int   ,  UpperCAmelCase				:     str   ,  UpperCAmelCase				:     int   ,  UpperCAmelCase				:     int   ,  UpperCAmelCase				:     Tuple   ,  *UpperCAmelCase				:     Tuple):
				SCREAMING_SNAKE_CASE_				:List[str]				     =						self.num_labels
				SCREAMING_SNAKE_CASE_				:Dict				     =						OpenAIGPTForSequenceClassification(UpperCAmelCase)
				model.to(UpperCAmelCase)
				model.eval()

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







		def _snake_case		(						self				:     int):
				SCREAMING_SNAKE_CASE_				:Union[str, Any]				     =						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_
				)     ,				
				)				:str				     =						config_and_inputs
				SCREAMING_SNAKE_CASE_				:Union[str, Any]				     =						{
				    "input_ids": input_ids,
				    "token_type_ids": token_type_ids,
				    "head_mask": head_mask,
				}

				return config, inputs_dict




@require_torch
class 						_UpperCAmelCase      (     lowercase		,		lowercase		,		lowercase		,		unittest.TestCase							):
		lowerCamelCase_	:			Tuple       			=  (
		    (OpenAIGPTModel, OpenAIGPTLMHeadModel, OpenAIGPTDoubleHeadsModel, OpenAIGPTForSequenceClassification)
		    if is_torch_available()
		    else ()
		)
		lowerCamelCase_	:			Union[str, Any]       			=  (
		    (OpenAIGPTLMHeadModel,) if is_torch_available() else ()
		)  # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly
		lowerCamelCase_	:			List[str]       			=  (
		    {
		        """feature-extraction""": OpenAIGPTModel,
		        """text-classification""": OpenAIGPTForSequenceClassification,
		        """text-generation""": OpenAIGPTLMHeadModel,
		        """zero-shot""": OpenAIGPTForSequenceClassification,
		    }
		    if is_torch_available()
		    else {}
		)






		def _snake_case		(						self				:     Optional[int]   ,  UpperCAmelCase				:     Optional[Any]   ,  UpperCAmelCase				:     str   ,  UpperCAmelCase				:     List[Any]   ,  UpperCAmelCase				:     Tuple   ,  UpperCAmelCase				:     Optional[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 _snake_case		(						self				:     Any   ,  UpperCAmelCase				:     Optional[int]   ,  UpperCAmelCase				:     Dict   ,  UpperCAmelCase				:     int=False):
				SCREAMING_SNAKE_CASE_				:int				     =						super()._prepare_for_class(UpperCAmelCase   ,  UpperCAmelCase   ,  return_labels=UpperCAmelCase)

				if return_labels:
						if model_class.__name__ == "OpenAIGPTDoubleHeadsModel":
								SCREAMING_SNAKE_CASE_				:List[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_				:int				     =						inputs_dict["labels"]
								SCREAMING_SNAKE_CASE_				:List[Any]				     =						inputs_dict["labels"]
								SCREAMING_SNAKE_CASE_				:Any				     =						torch.zeros(
								    (self.model_tester.batch_size, self.model_tester.num_choices)   ,  dtype=torch.long   ,  device=UpperCAmelCase   ,  )
								SCREAMING_SNAKE_CASE_				:Optional[int]				     =						torch.zeros(
								    self.model_tester.batch_size   ,  dtype=torch.long   ,  device=UpperCAmelCase)
				return inputs_dict






		def _snake_case		(						self				:     Union[str, Any]):
				SCREAMING_SNAKE_CASE_				:List[str]				     =						OpenAIGPTModelTester(self)
				SCREAMING_SNAKE_CASE_				:Dict				     =						ConfigTester(self   ,  config_class=UpperCAmelCase   ,  n_embd=37)






		def _snake_case		(						self				:     List[str]):
				self.config_tester.run_common_tests()






		def _snake_case		(						self				:     Any):
				SCREAMING_SNAKE_CASE_				:Union[str, Any]				     =						self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_openai_gpt_model(*UpperCAmelCase)






		def _snake_case		(						self				:     Union[str, Any]):
				SCREAMING_SNAKE_CASE_				:Optional[Any]				     =						self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_lm_head_model(*UpperCAmelCase)






		def _snake_case		(						self				:     Any):
				SCREAMING_SNAKE_CASE_				:Optional[Any]				     =						self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_double_lm_head_model(*UpperCAmelCase)






		def _snake_case		(						self				:     Optional[int]):
				SCREAMING_SNAKE_CASE_				:Optional[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				:     int):
				for model_name in OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
						SCREAMING_SNAKE_CASE_				:Optional[Any]				     =						OpenAIGPTModel.from_pretrained(UpperCAmelCase)
						self.assertIsNotNone(UpperCAmelCase)




@require_torch
class 						_UpperCAmelCase      (     unittest.TestCase							):






		@slow
		def _snake_case		(						self				:     List[Any]):
				SCREAMING_SNAKE_CASE_				:str				     =						OpenAIGPTLMHeadModel.from_pretrained("openai-gpt")
				model.to(UpperCAmelCase)
				SCREAMING_SNAKE_CASE_				:Union[str, Any]				     =						torch.tensor([[4_81, 47_35, 5_44]]   ,  dtype=torch.long   ,  device=UpperCAmelCase)  # the president is
				SCREAMING_SNAKE_CASE_				:Dict				     =						[
				    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

				SCREAMING_SNAKE_CASE_				:int				     =						model.generate(UpperCAmelCase   ,  do_sample=UpperCAmelCase)
				self.assertListEqual(output_ids[0].tolist()   ,  UpperCAmelCase)










from timeit import timeit


def     lowercase	(    a ):



		'''simple docstring'''




		if number < 0:
				raise ValueError("the value of input must not be negative" )
		SCREAMING_SNAKE_CASE_				:Optional[int]				     =						0
		while number:
				number &= number - 1
				result += 1
		return result


def     lowercase	(    a ):



		'''simple docstring'''




		if number < 0:
				raise ValueError("the value of input must not be negative" )
		SCREAMING_SNAKE_CASE_				:List[Any]				     =						0
		while number:
				if number % 2 == 1:
						result += 1
				number >>= 1
		return result


def     lowercase	(    ):



		'''simple docstring'''




		def do_benchmark(a ) -> None:
				SCREAMING_SNAKE_CASE_				:List[Any]				     =						"import __main__ as z"
				print(F"Benchmark when {number = }:" )
				print(F"{get_set_bits_count_using_modulo_operator(a ) = }" )
				SCREAMING_SNAKE_CASE_				:List[Any]				     =						timeit("z.get_set_bits_count_using_modulo_operator(25)"     , setup=a )
				print(F"timeit() runs in {timing} seconds" )
				print(F"{get_set_bits_count_using_brian_kernighans_algorithm(a ) = }" )
				SCREAMING_SNAKE_CASE_				:str				     =						timeit(
				    "z.get_set_bits_count_using_brian_kernighans_algorithm(25)"     , setup=a     , )
				print(F"timeit() runs in {timing} seconds" )

		for number in (25, 37, 58, 0):
				do_benchmark(a )
				print()


if __name__ == "__main__":
		import doctest

		doctest.testmod()
		benchmark()






