





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







       def __init__(				self					:     str		, A					:     str		, A					:     List[Any]=1_3		, A					:     Optional[int]=7		, A					:     List[Any]=True		, A					:     List[str]=True		, A					:     Tuple=True		, A					:     Optional[int]=9_9		, A					:     str=3_2		, A					:     Tuple=5		, A					:     str=4		, A					:     Any=3_7		, A					:     str="gelu"		, A					:     int=0.1		, A					:     Optional[int]=0.1		, A					:     str=5_1_2		, A					:     Optional[Any]=1_6		, A					:     int=2		, A					:     Optional[int]=0.02		, A					:     Optional[int]=3		, A					:     List[str]=4		, A					:     List[Any]=None		, )       ->Optional[Any]:
          lowerCamelCase__       :						str        =							parent
          lowerCamelCase__       :						int        =							batch_size
          lowerCamelCase__       :						Optional[Any]        =							seq_length
          lowerCamelCase__       :						int        =							is_training
          lowerCamelCase__       :						Any        =							use_token_type_ids
          lowerCamelCase__       :						Any        =							use_labels
          lowerCamelCase__       :						int        =							vocab_size
          lowerCamelCase__       :						int        =							hidden_size
          lowerCamelCase__       :						Dict        =							num_hidden_layers
          lowerCamelCase__       :						Tuple        =							num_attention_heads
          lowerCamelCase__       :						Union[str, Any]        =							intermediate_size
          lowerCamelCase__       :						Union[str, Any]        =							hidden_act
          lowerCamelCase__       :						str        =							hidden_dropout_prob
          lowerCamelCase__       :						Optional[int]        =							attention_probs_dropout_prob
          lowerCamelCase__       :						Any        =							max_position_embeddings
          lowerCamelCase__       :						Union[str, Any]        =							type_vocab_size
          lowerCamelCase__       :						Optional[int]        =							type_sequence_label_size
          lowerCamelCase__       :						int        =							initializer_range
          lowerCamelCase__       :						Union[str, Any]        =							num_labels
          lowerCamelCase__       :						Optional[int]        =							num_choices
          lowerCamelCase__       :						Dict        =							scope
          lowerCamelCase__       :						Tuple        =							self.vocab_size - 1







       def 						__lowerCamelCase   (				self					:     int      )       ->Optional[int]:
          lowerCamelCase__       :						List[Any]        =							ids_tensor([self.batch_size, self.seq_length]		, self.vocab_size      )

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

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

          lowerCamelCase__       :						Union[str, 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 						__lowerCamelCase   (				self					:     Tuple		, A					:     Optional[Any]		, A					:     int		, A					:     Any		, A					:     Any		, *A					:     str      )       ->List[Any]:
          lowerCamelCase__       :						Union[str, Any]        =							OpenAIGPTModel(config=A      )
          model.to(A      )
          model.eval()

          lowerCamelCase__       :						Dict        =							model(A		, token_type_ids=A		, head_mask=A      )
          lowerCamelCase__       :						Optional[int]        =							model(A		, token_type_ids=A      )
          lowerCamelCase__       :						Tuple        =							model(A      )

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







       def 						__lowerCamelCase   (				self					:     str		, A					:     str		, A					:     List[str]		, A					:     Optional[Any]		, A					:     Optional[Any]		, *A					:     Tuple      )       ->List[Any]:
          lowerCamelCase__       :						Optional[Any]        =							OpenAIGPTLMHeadModel(A      )
          model.to(A      )
          model.eval()

          lowerCamelCase__       :						Any        =							model(A		, token_type_ids=A		, labels=A      )
          self.parent.assertEqual(result.loss.shape		, ()      )
          self.parent.assertEqual(result.logits.shape		, (self.batch_size, self.seq_length, self.vocab_size)      )







       def 						__lowerCamelCase   (				self					:     List[Any]		, A					:     int		, A					:     Union[str, Any]		, A					:     Optional[Any]		, A					:     Optional[int]		, *A					:     List[Any]      )       ->Union[str, Any]:
          lowerCamelCase__       :						List[str]        =							OpenAIGPTDoubleHeadsModel(A      )
          model.to(A      )
          model.eval()

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







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

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







       def 						__lowerCamelCase   (				self					:     Any      )       ->Dict:
          lowerCamelCase__       :						int        =							self.prepare_config_and_inputs()
          (
              lowerCamelCase__							
          )       :						Dict        =							config_and_inputs
          lowerCamelCase__       :						Optional[int]        =							{
              '''input_ids''': input_ids,
              '''token_type_ids''': token_type_ids,
              '''head_mask''': head_mask,
          }

          return config, inputs_dict


@require_torch
class        __SCREAMING_SNAKE_CASE      (			lowerCAmelCase_					,lowerCAmelCase_					,lowerCAmelCase_					,unittest.TestCase ):
       _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				:  Union[str, Any]			   =   (
           {
               "feature-extraction": OpenAIGPTModel,
               "text-classification": OpenAIGPTForSequenceClassification,
               "text-generation": OpenAIGPTLMHeadModel,
               "zero-shot": OpenAIGPTForSequenceClassification,
           }
           if is_torch_available()
           else {}
       )







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

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







       def 						__lowerCamelCase   (				self					:     int      )       ->List[str]:
          lowerCamelCase__       :						Tuple        =							OpenAIGPTModelTester(self      )
          lowerCamelCase__       :						Tuple        =							ConfigTester(self		, config_class=A		, n_embd=3_7      )







       def 						__lowerCamelCase   (				self					:     Union[str, Any]      )       ->int:
          self.config_tester.run_common_tests()







       def 						__lowerCamelCase   (				self					:     Optional[int]      )       ->str:
          lowerCamelCase__       :						List[Any]        =							self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_openai_gpt_model(*A      )







       def 						__lowerCamelCase   (				self					:     Tuple      )       ->str:
          lowerCamelCase__       :						str        =							self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_lm_head_model(*A      )







       def 						__lowerCamelCase   (				self					:     Any      )       ->str:
          lowerCamelCase__       :						Optional[int]        =							self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_double_lm_head_model(*A      )







       def 						__lowerCamelCase   (				self					:     int      )       ->List[Any]:
          lowerCamelCase__       :						List[Any]        =							self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*A      )







       @slow
       def 						__lowerCamelCase   (				self					:     str      )       ->Optional[Any]:
          for model_name in OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             lowerCamelCase__       :						Optional[Any]        =							OpenAIGPTModel.from_pretrained(A      )
             self.assertIsNotNone(A      )


@require_torch
class        __SCREAMING_SNAKE_CASE      (			unittest.TestCase ):







       @slow
       def 						__lowerCamelCase   (				self					:     Any      )       ->Union[str, Any]:
          lowerCamelCase__       :						Dict        =							OpenAIGPTLMHeadModel.from_pretrained('''openai-gpt'''      )
          model.to(A      )
          lowerCamelCase__       :						Tuple        =							torch.tensor([[4_8_1, 4_7_3_5, 5_4_4]]		, dtype=torch.long		, device=A      )  # the president is
          lowerCamelCase__       :						List[Any]        =							[
              4_8_1,
              4_7_3_5,
              5_4_4,
              2_4_6,
              9_6_3,
              8_7_0,
              7_6_2,
              2_3_9,
              2_4_4,
              4_0_4_7_7,
              2_4_4,
              2_4_9,
              7_1_9,
              8_8_1,
              4_8_7,
              5_4_4,
              2_4_0,
              2_4_4,
              6_0_3,
              4_8_1,
          ]  # the president is a very good man. " \n " i\'m sure he is, " said the

          lowerCamelCase__       :						Tuple        =							model.generate(A		, do_sample=A      )
          self.assertListEqual(output_ids[0].tolist()		, A      )










def 	_a						(   UpperCAmelCase					)    ->    int:







   """simple docstring"""
   if not isinstance(UpperCAmelCase      ,  UpperCAmelCase					) or number < 0:
      raise ValueError('''Input must be a non-negative integer'''					)

   lowerCamelCase__       :						List[str]        =							0
   while number:
      # This way we arrive at next set bit (next 1) instead of looping
      # through each bit and checking for 1s hence the
      # loop won't run 32 times it will only run the number of `1` times
      number &= number - 1
      count += 1
   return count


if __name__ == "__main__":
       import doctest

       doctest.testmod()



