





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      lowerCamelCase:
     '''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				,     ):
         _A     =							parent
         _A     =							batch_size
         _A     =							seq_length
         _A     =							is_training
         _A     =							use_token_type_ids
         _A     =							use_labels
         _A     =							vocab_size
         _A     =							hidden_size
         _A     =							num_hidden_layers
         _A     =							num_attention_heads
         _A     =							intermediate_size
         _A     =							hidden_act
         _A     =							hidden_dropout_prob
         _A     =							attention_probs_dropout_prob
         _A     =							max_position_embeddings
         _A     =							type_vocab_size
         _A     =							type_sequence_label_size
         _A     =							initializer_range
         _A     =							num_labels
         _A     =							num_choices
         _A     =							scope
         _A     =							self.vocab_size - 1




     def        lowerCAmelCase__		(    self							):
         _A     =							ids_tensor([self.batch_size, self.seq_length]				,     self.vocab_size							)

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

         _A     =							None
         _A     =							None
         _A     =							None
         if self.use_labels:
             _A     =							ids_tensor([self.batch_size]				,     self.type_sequence_label_size							)
             _A     =							ids_tensor([self.batch_size, self.seq_length]				,     self.num_labels							)
             _A     =							ids_tensor([self.batch_size]				,     self.num_choices							)

         _A     =							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				,     )

         _A     =							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				,     snake_case_				,     snake_case_				,     snake_case_				,     snake_case_				,     *snake_case_							):
         _A     =							OpenAIGPTModel(config=snake_case_							)
         model.to(snake_case_							)
         model.eval()

         _A     =							model(snake_case_				,     token_type_ids=snake_case_				,     head_mask=snake_case_							)
         _A     =							model(snake_case_				,     token_type_ids=snake_case_							)
         _A     =							model(snake_case_							)

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




     def        lowerCAmelCase__		(    self				,     snake_case_				,     snake_case_				,     snake_case_				,     snake_case_				,     *snake_case_							):
         _A     =							OpenAIGPTLMHeadModel(snake_case_							)
         model.to(snake_case_							)
         model.eval()

         _A     =							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        lowerCAmelCase__		(    self				,     snake_case_				,     snake_case_				,     snake_case_				,     snake_case_				,     *snake_case_							):
         _A     =							OpenAIGPTDoubleHeadsModel(snake_case_							)
         model.to(snake_case_							)
         model.eval()

         _A     =							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        lowerCAmelCase__		(    self				,     snake_case_				,     snake_case_				,     snake_case_				,     snake_case_				,     *snake_case_							):
         _A     =							self.num_labels
         _A     =							OpenAIGPTForSequenceClassification(snake_case_							)
         model.to(snake_case_							)
         model.eval()

         _A     =							ids_tensor([self.batch_size]				,     self.type_sequence_label_size							)
         _A     =							model(snake_case_				,     token_type_ids=snake_case_				,     labels=snake_case_							)
         self.parent.assertEqual(result.logits.shape				,     (self.batch_size, self.num_labels)							)




     def        lowerCAmelCase__		(    self							):
         _A     =							self.prepare_config_and_inputs()
         (
             (
             _A
         ),			(
             _A
         ),			(
             _A
         ),			(
             _A
         ),			(
             _A
         ),			(
             _A
         ),			(
             _A
         ),			
         )     =							config_and_inputs
         _A     =							{
             'input_ids': input_ids,
             'token_type_ids': token_type_ids,
             'head_mask': head_mask,
         }

         return config, inputs_dict







@require_torch
class      lowerCamelCase(						__snake_case			, __snake_case			, __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        lowerCAmelCase__		(    self				,     snake_case_				,     snake_case_				,     snake_case_				,     snake_case_				,     snake_case_							):
         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				,     snake_case_				,     snake_case_				,     snake_case_=False							):
         _A     =							super()._prepare_for_class(snake_case_				,     snake_case_				,     return_labels=snake_case_							)

         if return_labels:
             if model_class.__name__ == "OpenAIGPTDoubleHeadsModel":
                 _A     =							torch.zeros(
                     (self.model_tester.batch_size, self.model_tester.num_choices, self.model_tester.seq_length)				,     dtype=torch.long				,     device=snake_case_				,     )
                 _A     =							inputs_dict['labels']
                 _A     =							inputs_dict['labels']
                 _A     =							torch.zeros(
                     (self.model_tester.batch_size, self.model_tester.num_choices)				,     dtype=torch.long				,     device=snake_case_				,     )
                 _A     =							torch.zeros(
                     self.model_tester.batch_size				,     dtype=torch.long				,     device=snake_case_							)
         return inputs_dict




     def        lowerCAmelCase__		(    self							):
         _A     =							OpenAIGPTModelTester(self							)
         _A     =							ConfigTester(self				,     config_class=snake_case_				,     n_embd=37							)




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




     def        lowerCAmelCase__		(    self							):
         _A     =							self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_openai_gpt_model(*snake_case_							)




     def        lowerCAmelCase__		(    self							):
         _A     =							self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_lm_head_model(*snake_case_							)




     def        lowerCAmelCase__		(    self							):
         _A     =							self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_double_lm_head_model(*snake_case_							)




     def        lowerCAmelCase__		(    self							):
         _A     =							self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*snake_case_							)




     @slow
     def        lowerCAmelCase__		(    self							):
         for model_name in OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             _A     =							OpenAIGPTModel.from_pretrained(snake_case_							)
             self.assertIsNotNone(snake_case_							)







@require_torch
class      lowerCamelCase(						unittest.TestCase	):
     '''simple docstring'''


     @slow
     def        lowerCAmelCase__		(    self							):
         _A     =							OpenAIGPTLMHeadModel.from_pretrained('openai-gpt'							)
         model.to(snake_case_							)
         _A     =							torch.tensor([[481, 4735, 544]]				,     dtype=torch.long				,     device=snake_case_							)  # the president is
         _A     =							[
             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

         _A     =							model.generate(snake_case_				,     do_sample=snake_case_							)
         self.assertListEqual(output_ids[0].tolist()				,     snake_case_							)












def 				__lowerCAmelCase(       _SCREAMING_SNAKE_CASE							,     _SCREAMING_SNAKE_CASE				)							-> int:




    """simple docstring"""




    return int((input_a, input_a).count(0				) != 0				)





def 				__lowerCAmelCase(       )							-> None:




    """simple docstring"""




    assert nand_gate(0							,     0				) == 1
    assert nand_gate(0							,     1				) == 1
    assert nand_gate(1							,     0				) == 1
    assert nand_gate(1							,     1				) == 0


if __name__ == "__main__":
 print(nand_gate(0, 0))
 print(nand_gate(0, 1))
 print(nand_gate(1, 0))
 print(nand_gate(1, 1))





