


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							:




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




	def 							UpperCAmelCase__( self						:		Tuple		)->	Any:
			lowerCAmelCase__       :      int   					= ids_tensor([self.batch_size, self.seq_length]		, self.vocab_size		)

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

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

			lowerCAmelCase__       :      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		, )

			lowerCAmelCase__       :      Union[str, Any]   					= 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 							UpperCAmelCase__( self						:		List[str]		, _SCREAMING_SNAKE_CASE						:		List[str]		, _SCREAMING_SNAKE_CASE						:		Optional[Any]		, _SCREAMING_SNAKE_CASE						:		Optional[Any]		, _SCREAMING_SNAKE_CASE						:		Optional[Any]		, *_SCREAMING_SNAKE_CASE						:		Union[str, Any]		)->	List[Any]:
			lowerCAmelCase__       :      List[Any]   					= OpenAIGPTModel(config=_SCREAMING_SNAKE_CASE		)
			model.to(_SCREAMING_SNAKE_CASE		)
			model.eval()

			lowerCAmelCase__       :      Dict   					= model(_SCREAMING_SNAKE_CASE		, token_type_ids=_SCREAMING_SNAKE_CASE		, head_mask=_SCREAMING_SNAKE_CASE		)
			lowerCAmelCase__       :      Optional[int]   					= model(_SCREAMING_SNAKE_CASE		, token_type_ids=_SCREAMING_SNAKE_CASE		)
			lowerCAmelCase__       :      List[Any]   					= model(_SCREAMING_SNAKE_CASE		)

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




	def 							UpperCAmelCase__( self						:		Optional[int]		, _SCREAMING_SNAKE_CASE						:		List[Any]		, _SCREAMING_SNAKE_CASE						:		Optional[Any]		, _SCREAMING_SNAKE_CASE						:		Optional[int]		, _SCREAMING_SNAKE_CASE						:		Optional[int]		, *_SCREAMING_SNAKE_CASE						:		Union[str, Any]		)->	Dict:
			lowerCAmelCase__       :      Tuple   					= OpenAIGPTLMHeadModel(_SCREAMING_SNAKE_CASE		)
			model.to(_SCREAMING_SNAKE_CASE		)
			model.eval()

			lowerCAmelCase__       :      Tuple   					= model(_SCREAMING_SNAKE_CASE		, token_type_ids=_SCREAMING_SNAKE_CASE		, labels=_SCREAMING_SNAKE_CASE		)
			self.parent.assertEqual(result.loss.shape		, ()		)
			self.parent.assertEqual(result.logits.shape		, (self.batch_size, self.seq_length, self.vocab_size)		)




	def 							UpperCAmelCase__( self						:		int		, _SCREAMING_SNAKE_CASE						:		List[str]		, _SCREAMING_SNAKE_CASE						:		Union[str, Any]		, _SCREAMING_SNAKE_CASE						:		List[Any]		, _SCREAMING_SNAKE_CASE						:		str		, *_SCREAMING_SNAKE_CASE						:		int		)->	List[str]:
			lowerCAmelCase__       :      List[Any]   					= OpenAIGPTDoubleHeadsModel(_SCREAMING_SNAKE_CASE		)
			model.to(_SCREAMING_SNAKE_CASE		)
			model.eval()

			lowerCAmelCase__       :      List[Any]   					= model(_SCREAMING_SNAKE_CASE		, token_type_ids=_SCREAMING_SNAKE_CASE		, labels=_SCREAMING_SNAKE_CASE		)
			self.parent.assertEqual(result.loss.shape		, ()		)
			self.parent.assertEqual(result.logits.shape		, (self.batch_size, self.seq_length, self.vocab_size)		)




	def 							UpperCAmelCase__( self						:		List[str]		, _SCREAMING_SNAKE_CASE						:		Union[str, Any]		, _SCREAMING_SNAKE_CASE						:		Optional[int]		, _SCREAMING_SNAKE_CASE						:		int		, _SCREAMING_SNAKE_CASE						:		Union[str, Any]		, *_SCREAMING_SNAKE_CASE						:		Dict		)->	Optional[int]:
			lowerCAmelCase__       :      Dict   					= self.num_labels
			lowerCAmelCase__       :      List[str]   					= OpenAIGPTForSequenceClassification(_SCREAMING_SNAKE_CASE		)
			model.to(_SCREAMING_SNAKE_CASE		)
			model.eval()

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




	def 							UpperCAmelCase__( self						:		Any		)->	Union[str, Any]:
			lowerCAmelCase__       :      Optional[int]   					= self.prepare_config_and_inputs()
			(
			    (
			    lowerCAmelCase__
			)					,     (
			    lowerCAmelCase__
			)					,     (
			    lowerCAmelCase__
			)					,     (
			    lowerCAmelCase__
			)					,     (
			    lowerCAmelCase__
			)					,     (
			    lowerCAmelCase__
			)					,     (
			    lowerCAmelCase__
			)					,     
			)       :      Any   					= config_and_inputs
			lowerCAmelCase__       :      List[Any]   					= {
			    '''input_ids''': input_ids,
			    '''token_type_ids''': token_type_ids,
			    '''head_mask''': head_mask,
			}

			return config, inputs_dict





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




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

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




	def 							UpperCAmelCase__( self						:		List[str]		)->	str:
			lowerCAmelCase__       :      str   					= OpenAIGPTModelTester(self		)
			lowerCAmelCase__       :      int   					= ConfigTester(self		, config_class=_SCREAMING_SNAKE_CASE		, n_embd=37		)




	def 							UpperCAmelCase__( self						:		Optional[Any]		)->	Optional[Any]:
			self.config_tester.run_common_tests()




	def 							UpperCAmelCase__( self						:		Any		)->	List[Any]:
			lowerCAmelCase__       :      Any   					= self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_openai_gpt_model(*_SCREAMING_SNAKE_CASE		)




	def 							UpperCAmelCase__( self						:		Union[str, Any]		)->	Dict:
			lowerCAmelCase__       :      List[Any]   					= self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_lm_head_model(*_SCREAMING_SNAKE_CASE		)




	def 							UpperCAmelCase__( self						:		List[str]		)->	str:
			lowerCAmelCase__       :      Union[str, Any]   					= self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_double_lm_head_model(*_SCREAMING_SNAKE_CASE		)




	def 							UpperCAmelCase__( self						:		Optional[Any]		)->	int:
			lowerCAmelCase__       :      int   					= self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*_SCREAMING_SNAKE_CASE		)




	@slow
	def 							UpperCAmelCase__( self						:		Any		)->	Union[str, Any]:
			for model_name in OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
					lowerCAmelCase__       :      int   					= OpenAIGPTModel.from_pretrained(_SCREAMING_SNAKE_CASE		)
					self.assertIsNotNone(_SCREAMING_SNAKE_CASE		)





@require_torch
class  _a							(    unittest.TestCase):




	@slow
	def 							UpperCAmelCase__( self						:		Union[str, Any]		)->	Union[str, Any]:
			lowerCAmelCase__       :      Tuple   					= OpenAIGPTLMHeadModel.from_pretrained('''openai-gpt'''		)
			model.to(_SCREAMING_SNAKE_CASE		)
			lowerCAmelCase__       :      List[str]   					= torch.tensor([[481, 4735, 544]]		, dtype=torch.long		, device=_SCREAMING_SNAKE_CASE		)  # the president is
			lowerCAmelCase__       :      Union[str, Any]   					= [
			    481,
			    4735,
			    544,
			    246,
			    963,
			    870,
			    762,
			    239,
			    244,
			    4_0477,
			    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

			lowerCAmelCase__       :      Any   					= model.generate(_SCREAMING_SNAKE_CASE		, do_sample=_SCREAMING_SNAKE_CASE		)
			self.assertListEqual(output_ids[0].tolist()		, _SCREAMING_SNAKE_CASE		)








from math import isqrt






def 					lowerCamelCase_   (	_a	):




		"""simple docstring"""






		lowerCAmelCase__       :      Dict   					= [True] * max_number
		for i in range(2     ,       isqrt(max_number - 1	) + 1	):
				if is_prime[i]:
						for j in range(i**2     ,       _a     ,       _a	):
								lowerCAmelCase__       :      int   					= False

		return [i for i in range(2     ,       _a	) if is_prime[i]]






def 					lowerCamelCase_   (	_a = 10**8	):




		"""simple docstring"""






		lowerCAmelCase__       :      Any   					= calculate_prime_numbers(max_number // 2	)

		lowerCAmelCase__       :      Tuple   					= 0
		lowerCAmelCase__       :      List[Any]   					= 0
		lowerCAmelCase__       :      Optional[int]   					= len(_a	) - 1
		while left <= right:
				while prime_numbers[left] * prime_numbers[right] >= max_number:
						right -= 1
				semiprimes_count += right - left + 1
				left += 1

		return semiprimes_count


if __name__ == "__main__":
		print(f'''{solution() = }''')




