


'''simple docstring'''







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



     """simple docstring"""






     def __init__( self      ,	snake_case__      ,	snake_case__=13      ,	snake_case__=7      ,	snake_case__=True      ,	snake_case__=True      ,	snake_case__=True      ,	snake_case__=99      ,	snake_case__=32      ,	snake_case__=5      ,	snake_case__=4      ,	snake_case__=37      ,	snake_case__="gelu"      ,	snake_case__=0.1      ,	snake_case__=0.1      ,	snake_case__=512      ,	snake_case__=16      ,	snake_case__=2      ,	snake_case__=0.02      ,	snake_case__=3      ,	snake_case__=4      ,	snake_case__=None      ,	):

            '''simple docstring'''

            _lowerCAmelCase    :							Optional[Any]   			=       parent
            _lowerCAmelCase    :							Any   			=       batch_size
            _lowerCAmelCase    :							Any   			=       seq_length
            _lowerCAmelCase    :							Union[str, Any]   			=       is_training
            _lowerCAmelCase    :							Tuple   			=       use_token_type_ids
            _lowerCAmelCase    :							int   			=       use_labels
            _lowerCAmelCase    :							List[Any]   			=       vocab_size
            _lowerCAmelCase    :							Any   			=       hidden_size
            _lowerCAmelCase    :							int   			=       num_hidden_layers
            _lowerCAmelCase    :							List[str]   			=       num_attention_heads
            _lowerCAmelCase    :							Optional[Any]   			=       intermediate_size
            _lowerCAmelCase    :							Optional[int]   			=       hidden_act
            _lowerCAmelCase    :							str   			=       hidden_dropout_prob
            _lowerCAmelCase    :							int   			=       attention_probs_dropout_prob
            _lowerCAmelCase    :							Union[str, Any]   			=       max_position_embeddings
            _lowerCAmelCase    :							int   			=       type_vocab_size
            _lowerCAmelCase    :							Union[str, Any]   			=       type_sequence_label_size
            _lowerCAmelCase    :							Optional[int]   			=       initializer_range
            _lowerCAmelCase    :							Tuple   			=       num_labels
            _lowerCAmelCase    :							List[str]   			=       num_choices
            _lowerCAmelCase    :							List[str]   			=       scope
            _lowerCAmelCase    :							Optional[Any]   			=       self.vocab_size - 1







     def a	( self   ):

            '''simple docstring'''

            _lowerCAmelCase    :							List[str]   			=       ids_tensor([self.batch_size, self.seq_length]      ,	self.vocab_size   )

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

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

            _lowerCAmelCase    :							Tuple   			=       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    :							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 a	( self      ,	snake_case__      ,	snake_case__      ,	snake_case__      ,	snake_case__      ,	*snake_case__   ):

            '''simple docstring'''

            _lowerCAmelCase    :							str   			=       OpenAIGPTModel(config=snake_case__   )
            model.to(snake_case__   )
            model.eval()

            _lowerCAmelCase    :							str   			=       model(snake_case__      ,	token_type_ids=snake_case__      ,	head_mask=snake_case__   )
            _lowerCAmelCase    :							int   			=       model(snake_case__      ,	token_type_ids=snake_case__   )
            _lowerCAmelCase    :							Optional[int]   			=       model(snake_case__   )

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







     def a	( self      ,	snake_case__      ,	snake_case__      ,	snake_case__      ,	snake_case__      ,	*snake_case__   ):

            '''simple docstring'''

            _lowerCAmelCase    :							Optional[int]   			=       OpenAIGPTLMHeadModel(snake_case__   )
            model.to(snake_case__   )
            model.eval()

            _lowerCAmelCase    :							Any   			=       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 a	( self      ,	snake_case__      ,	snake_case__      ,	snake_case__      ,	snake_case__      ,	*snake_case__   ):

            '''simple docstring'''

            _lowerCAmelCase    :							Union[str, Any]   			=       OpenAIGPTDoubleHeadsModel(snake_case__   )
            model.to(snake_case__   )
            model.eval()

            _lowerCAmelCase    :							List[str]   			=       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 a	( self      ,	snake_case__      ,	snake_case__      ,	snake_case__      ,	snake_case__      ,	*snake_case__   ):

            '''simple docstring'''

            _lowerCAmelCase    :							List[str]   			=       self.num_labels
            _lowerCAmelCase    :							str   			=       OpenAIGPTForSequenceClassification(snake_case__   )
            model.to(snake_case__   )
            model.eval()

            _lowerCAmelCase    :							Tuple   			=       ids_tensor([self.batch_size]      ,	self.type_sequence_label_size   )
            _lowerCAmelCase    :							Union[str, Any]   			=       model(snake_case__      ,	token_type_ids=snake_case__      ,	labels=snake_case__   )
            self.parent.assertEqual(result.logits.shape      ,	(self.batch_size, self.num_labels)   )







     def a	( self   ):

            '''simple docstring'''

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

            return config, inputs_dict
@require_torch
class 	UpperCamelCase__					( SCREAMING_SNAKE_CASE_    ,					SCREAMING_SNAKE_CASE_    ,					SCREAMING_SNAKE_CASE_    ,					unittest.TestCase	):



     """simple docstring"""






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







     def a	( self      ,	snake_case__      ,	snake_case__      ,	snake_case__      ,	snake_case__      ,	snake_case__   ):

            '''simple docstring'''

            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 a	( self      ,	snake_case__      ,	snake_case__      ,	snake_case__=False   ):

            '''simple docstring'''

            _lowerCAmelCase    :							List[Any]   			=       super()._prepare_for_class(snake_case__      ,	snake_case__      ,	return_labels=snake_case__   )

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







     def a	( self   ):

            '''simple docstring'''

            _lowerCAmelCase    :							str   			=       OpenAIGPTModelTester(self   )
            _lowerCAmelCase    :							Union[str, Any]   			=       ConfigTester(self      ,	config_class=snake_case__      ,	n_embd=37   )







     def a	( self   ):

            '''simple docstring'''

            self.config_tester.run_common_tests()







     def a	( self   ):

            '''simple docstring'''

            _lowerCAmelCase    :							int   			=       self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_openai_gpt_model(*snake_case__   )







     def a	( self   ):

            '''simple docstring'''

            _lowerCAmelCase    :							List[str]   			=       self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_lm_head_model(*snake_case__   )







     def a	( self   ):

            '''simple docstring'''

            _lowerCAmelCase    :							Tuple   			=       self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_double_lm_head_model(*snake_case__   )







     def a	( self   ):

            '''simple docstring'''

            _lowerCAmelCase    :							Tuple   			=       self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*snake_case__   )







     @slow
     def a	( self   ):

            '''simple docstring'''

            for model_name in OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                   _lowerCAmelCase    :							Optional[Any]   			=       OpenAIGPTModel.from_pretrained(snake_case__   )
                   self.assertIsNotNone(snake_case__   )
@require_torch
class 	UpperCamelCase__					( unittest.TestCase	):



     """simple docstring"""






     @slow
     def a	( self   ):

            '''simple docstring'''

            _lowerCAmelCase    :							Any   			=       OpenAIGPTLMHeadModel.from_pretrained('openai-gpt'   )
            model.to(snake_case__   )
            _lowerCAmelCase    :							int   			=       torch.tensor([[481, 4735, 544]]      ,	dtype=torch.long      ,	device=snake_case__   )  # the president is
            _lowerCAmelCase    :							str   			=       [
                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    :							Dict   			=       model.generate(snake_case__      ,	do_sample=snake_case__   )
            self.assertListEqual(output_ids[0].tolist()      ,	snake_case__   )





'''simple docstring'''







lowerCAmelCase    :     Optional[int]					=						[sum(int(c, 10) ** 2 for c in i.__str__()) for i in range(10_00_00)]






def 			lowercase				(_A					):







       """simple docstring"""







       _lowerCAmelCase    :							str   			=       0
       while number:
              # Increased Speed Slightly by checking every 5 digits together.
              sum_of_digits_squared += DIGITS_SQUARED[number % 1_0_0_0_0_0]
              number //= 1_0_0_0_0_0

       return sum_of_digits_squared


# There are 2 Chains made,
# One ends with 89 with the chain member 58 being the one which when declared first,
# there will be the least number of iterations for all the members to be checked.

# The other one ends with 1 and has only one element 1.

# So 58 and 1 are chosen to be declared at the starting.

# Changed dictionary to an array to quicken the solution
lowerCAmelCase    :     list[bool | None]					=						[None] * 10_00_00_00
lowerCAmelCase    :     List[str]					=						True
lowerCAmelCase    :     Union[str, Any]					=						False






def 			lowercase				(_A					):







       """simple docstring"""







       if CHAINS[number - 1] is not None:
              return CHAINS[number - 1]  # type: ignore

       _lowerCAmelCase    :							Any   			=       chain(next_number(_A					)					)
       _lowerCAmelCase    :							List[str]   			=       number_chain

       while number < 1_0_0_0_0_0_0_0:
              _lowerCAmelCase    :							Tuple   			=       number_chain
              number *= 1_0

       return number_chain






def 			lowercase				(_A = 1_0_0_0_0_0_0_0					):







       """simple docstring"""







       for i in range(1   ,						_A					):
              if CHAINS[i] is None:
                     chain(i + 1					)

       return CHAINS[:number].count(_A					)


if __name__ == "__main__":
       import doctest

       doctest.testmod()
       print(F'''{solution() = }''')

