





'''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     __lowerCAmelCase					:






       """simple docstring"""





       def __init__(							self       :		List[str]	,			lowerCAmelCase__       :		Optional[Any]	,			lowerCAmelCase__       :		int=13	,			lowerCAmelCase__       :		Dict=7	,			lowerCAmelCase__       :		List[Any]=True	,			lowerCAmelCase__       :		List[str]=True	,			lowerCAmelCase__       :		Dict=True	,			lowerCAmelCase__       :		Tuple=99	,			lowerCAmelCase__       :		str=32	,			lowerCAmelCase__       :		List[Any]=5	,			lowerCAmelCase__       :		str=4	,			lowerCAmelCase__       :		Dict=37	,			lowerCAmelCase__       :		int="gelu"	,			lowerCAmelCase__       :		Tuple=0.1	,			lowerCAmelCase__       :		Any=0.1	,			lowerCAmelCase__       :		str=512	,			lowerCAmelCase__       :		Optional[int]=16	,			lowerCAmelCase__       :		Optional[int]=2	,			lowerCAmelCase__       :		str=0.02	,			lowerCAmelCase__       :		str=3	,			lowerCAmelCase__       :		int=4	,			lowerCAmelCase__       :		Union[str, Any]=None	,			)   ->					str:

            '''simple docstring'''




            _UpperCamelCase       =	parent
            _UpperCamelCase       =	batch_size
            _UpperCamelCase       =	seq_length
            _UpperCamelCase       =	is_training
            _UpperCamelCase       =	use_token_type_ids
            _UpperCamelCase       =	use_labels
            _UpperCamelCase       =	vocab_size
            _UpperCamelCase       =	hidden_size
            _UpperCamelCase       =	num_hidden_layers
            _UpperCamelCase       =	num_attention_heads
            _UpperCamelCase       =	intermediate_size
            _UpperCamelCase       =	hidden_act
            _UpperCamelCase       =	hidden_dropout_prob
            _UpperCamelCase       =	attention_probs_dropout_prob
            _UpperCamelCase       =	max_position_embeddings
            _UpperCamelCase       =	type_vocab_size
            _UpperCamelCase       =	type_sequence_label_size
            _UpperCamelCase       =	initializer_range
            _UpperCamelCase       =	num_labels
            _UpperCamelCase       =	num_choices
            _UpperCamelCase       =	scope
            _UpperCamelCase       =	self.vocab_size - 1





       def snake_case__						(							self       :		int					)   ->					Union[str, Any]:

            '''simple docstring'''




            _UpperCamelCase       =	ids_tensor([self.batch_size, self.seq_length]	,			self.vocab_size					)

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

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

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

            _UpperCamelCase       =	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       :		Optional[int]	,			lowerCAmelCase__       :		Union[str, Any]	,			lowerCAmelCase__       :		List[Any]	,			lowerCAmelCase__       :		Optional[int]	,			lowerCAmelCase__       :		Dict	,			*lowerCAmelCase__       :		Optional[Any]					)   ->					Tuple:

            '''simple docstring'''




            _UpperCamelCase       =	OpenAIGPTModel(config=lowerCAmelCase__					)
            model.to(lowerCAmelCase__					)
            model.eval()

            _UpperCamelCase       =	model(lowerCAmelCase__	,			token_type_ids=lowerCAmelCase__	,			head_mask=lowerCAmelCase__					)
            _UpperCamelCase       =	model(lowerCAmelCase__	,			token_type_ids=lowerCAmelCase__					)
            _UpperCamelCase       =	model(lowerCAmelCase__					)

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





       def snake_case__						(							self       :		List[Any]	,			lowerCAmelCase__       :		List[str]	,			lowerCAmelCase__       :		Union[str, Any]	,			lowerCAmelCase__       :		Any	,			lowerCAmelCase__       :		List[Any]	,			*lowerCAmelCase__       :		List[Any]					)   ->					Optional[Any]:

            '''simple docstring'''




            _UpperCamelCase       =	OpenAIGPTLMHeadModel(lowerCAmelCase__					)
            model.to(lowerCAmelCase__					)
            model.eval()

            _UpperCamelCase       =	model(lowerCAmelCase__	,			token_type_ids=lowerCAmelCase__	,			labels=lowerCAmelCase__					)
            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[Any]	,			lowerCAmelCase__       :		Dict	,			lowerCAmelCase__       :		Optional[Any]	,			lowerCAmelCase__       :		int	,			lowerCAmelCase__       :		List[str]	,			*lowerCAmelCase__       :		Optional[int]					)   ->					List[Any]:

            '''simple docstring'''




            _UpperCamelCase       =	OpenAIGPTDoubleHeadsModel(lowerCAmelCase__					)
            model.to(lowerCAmelCase__					)
            model.eval()

            _UpperCamelCase       =	model(lowerCAmelCase__	,			token_type_ids=lowerCAmelCase__	,			labels=lowerCAmelCase__					)
            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       :		List[Any]	,			lowerCAmelCase__       :		Any	,			lowerCAmelCase__       :		Optional[int]	,			lowerCAmelCase__       :		List[str]	,			lowerCAmelCase__       :		List[Any]	,			*lowerCAmelCase__       :		Optional[Any]					)   ->					str:

            '''simple docstring'''




            _UpperCamelCase       =	self.num_labels
            _UpperCamelCase       =	OpenAIGPTForSequenceClassification(lowerCAmelCase__					)
            model.to(lowerCAmelCase__					)
            model.eval()

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







       def snake_case__						(							self       :		Dict					)   ->					Tuple:

            '''simple docstring'''




            _UpperCamelCase       =	self.prepare_config_and_inputs()
            (
                (
                _UpperCamelCase
            )   ,    (
                _UpperCamelCase
            )   ,    (
                _UpperCamelCase
            )   ,    (
                _UpperCamelCase
            )   ,    (
                _UpperCamelCase
            )   ,    (
                _UpperCamelCase
            )   ,    (
                _UpperCamelCase
            )   ,    
            )       =	config_and_inputs
            _UpperCamelCase       =	{
                '''input_ids''': input_ids,
                '''token_type_ids''': token_type_ids,
                '''head_mask''': head_mask,
            }

            return config, inputs_dict

@require_torch
class     __lowerCAmelCase					(   __magic_name__						,						__magic_name__						,						__magic_name__						,						unittest.TestCase			):






       """simple docstring"""
       _snake_case     :					List[str]						    =   (
           (OpenAIGPTModel, OpenAIGPTLMHeadModel, OpenAIGPTDoubleHeadsModel, OpenAIGPTForSequenceClassification)
           if is_torch_available()
           else ()
       )
       _snake_case     :					str						    =   (
           (OpenAIGPTLMHeadModel,) if is_torch_available() else ()
       )  # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly
       _snake_case     :					str						    =   (
           {
               'feature-extraction': OpenAIGPTModel,
               'text-classification': OpenAIGPTForSequenceClassification,
               'text-generation': OpenAIGPTLMHeadModel,
               'zero-shot': OpenAIGPTForSequenceClassification,
           }
           if is_torch_available()
           else {}
       )





       def snake_case__						(							self       :		List[str]	,			lowerCAmelCase__       :		Any	,			lowerCAmelCase__       :		Optional[Any]	,			lowerCAmelCase__       :		List[str]	,			lowerCAmelCase__       :		Any	,			lowerCAmelCase__       :		int					)   ->					Optional[int]:

            '''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 snake_case__						(							self       :		int	,			lowerCAmelCase__       :		Optional[Any]	,			lowerCAmelCase__       :		Optional[int]	,			lowerCAmelCase__       :		Optional[int]=False					)   ->					Union[str, Any]:

            '''simple docstring'''




            _UpperCamelCase       =	super()._prepare_for_class(lowerCAmelCase__	,			lowerCAmelCase__	,			return_labels=lowerCAmelCase__					)

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





       def snake_case__						(							self       :		int					)   ->					Any:

            '''simple docstring'''




            _UpperCamelCase       =	OpenAIGPTModelTester(self					)
            _UpperCamelCase       =	ConfigTester(self	,			config_class=lowerCAmelCase__	,			n_embd=37					)





       def snake_case__						(							self       :		Optional[Any]					)   ->					Tuple:

            '''simple docstring'''




            self.config_tester.run_common_tests()





       def snake_case__						(							self       :		Any					)   ->					Optional[Any]:

            '''simple docstring'''




            _UpperCamelCase       =	self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_openai_gpt_model(*lowerCAmelCase__					)





       def snake_case__						(							self       :		Any					)   ->					Any:

            '''simple docstring'''




            _UpperCamelCase       =	self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_lm_head_model(*lowerCAmelCase__					)





       def snake_case__						(							self       :		Optional[int]					)   ->					List[str]:

            '''simple docstring'''




            _UpperCamelCase       =	self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_double_lm_head_model(*lowerCAmelCase__					)





       def snake_case__						(							self       :		List[Any]					)   ->					Optional[Any]:

            '''simple docstring'''




            _UpperCamelCase       =	self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*lowerCAmelCase__					)







       @slow
       def snake_case__						(							self       :		str					)   ->					Union[str, Any]:

            '''simple docstring'''




            for model_name in OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                 _UpperCamelCase       =	OpenAIGPTModel.from_pretrained(lowerCAmelCase__					)
                 self.assertIsNotNone(lowerCAmelCase__					)

@require_torch
class     __lowerCAmelCase					(   unittest.TestCase			):






       """simple docstring"""





       @slow
       def snake_case__						(							self       :		str					)   ->					Dict:

            '''simple docstring'''




            _UpperCamelCase       =	OpenAIGPTLMHeadModel.from_pretrained('''openai-gpt'''					)
            model.to(lowerCAmelCase__					)
            _UpperCamelCase       =	torch.tensor([[481, 4735, 544]]	,			dtype=torch.long	,			device=lowerCAmelCase__					)  # the president is
            _UpperCamelCase       =	[
                481,
                4735,
                544,
                246,
                963,
                870,
                762,
                239,
                244,
                40477,
                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

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










'''simple docstring'''

import unittest

from diffusers.pipelines.pipeline_utils import is_safetensors_compatible
class     __lowerCAmelCase					(   unittest.TestCase			):






       """simple docstring"""





       def snake_case__						(							self       :		Tuple					)   ->					int:

            '''simple docstring'''




            _UpperCamelCase       =	[
                '''safety_checker/pytorch_model.bin''',
                '''safety_checker/model.safetensors''',
                '''vae/diffusion_pytorch_model.bin''',
                '''vae/diffusion_pytorch_model.safetensors''',
                '''text_encoder/pytorch_model.bin''',
                '''text_encoder/model.safetensors''',
                '''unet/diffusion_pytorch_model.bin''',
                '''unet/diffusion_pytorch_model.safetensors''',
            ]
            self.assertTrue(is_safetensors_compatible(lowerCAmelCase__					)					)





       def snake_case__						(							self       :		int					)   ->					Tuple:

            '''simple docstring'''




            _UpperCamelCase       =	[
                '''unet/diffusion_pytorch_model.bin''',
                '''unet/diffusion_pytorch_model.safetensors''',
            ]
            self.assertTrue(is_safetensors_compatible(lowerCAmelCase__					)					)





       def snake_case__						(							self       :		Optional[Any]					)   ->					List[str]:

            '''simple docstring'''




            _UpperCamelCase       =	[
                '''safety_checker/pytorch_model.bin''',
                '''safety_checker/model.safetensors''',
                '''vae/diffusion_pytorch_model.bin''',
                '''vae/diffusion_pytorch_model.safetensors''',
                '''text_encoder/pytorch_model.bin''',
                '''text_encoder/model.safetensors''',
                '''unet/diffusion_pytorch_model.bin''',
                # Removed: 'unet/diffusion_pytorch_model.safetensors',
            ]
            self.assertFalse(is_safetensors_compatible(lowerCAmelCase__					)					)





       def snake_case__						(							self       :		List[Any]					)   ->					List[Any]:

            '''simple docstring'''




            _UpperCamelCase       =	[
                '''text_encoder/pytorch_model.bin''',
                '''text_encoder/model.safetensors''',
            ]
            self.assertTrue(is_safetensors_compatible(lowerCAmelCase__					)					)





       def snake_case__						(							self       :		Dict					)   ->					Dict:

            '''simple docstring'''




            _UpperCamelCase       =	[
                '''safety_checker/pytorch_model.bin''',
                '''safety_checker/model.safetensors''',
                '''vae/diffusion_pytorch_model.bin''',
                '''vae/diffusion_pytorch_model.safetensors''',
                '''text_encoder/pytorch_model.bin''',
                # Removed: 'text_encoder/model.safetensors',
                '''unet/diffusion_pytorch_model.bin''',
                '''unet/diffusion_pytorch_model.safetensors''',
            ]
            self.assertFalse(is_safetensors_compatible(lowerCAmelCase__					)					)





       def snake_case__						(							self       :		Any					)   ->					Optional[int]:

            '''simple docstring'''




            _UpperCamelCase       =	[
                '''safety_checker/pytorch_model.fp16.bin''',
                '''safety_checker/model.fp16.safetensors''',
                '''vae/diffusion_pytorch_model.fp16.bin''',
                '''vae/diffusion_pytorch_model.fp16.safetensors''',
                '''text_encoder/pytorch_model.fp16.bin''',
                '''text_encoder/model.fp16.safetensors''',
                '''unet/diffusion_pytorch_model.fp16.bin''',
                '''unet/diffusion_pytorch_model.fp16.safetensors''',
            ]
            _UpperCamelCase       =	'''fp16'''
            self.assertTrue(is_safetensors_compatible(lowerCAmelCase__	,			variant=lowerCAmelCase__					)					)





       def snake_case__						(							self       :		Optional[Any]					)   ->					Dict:

            '''simple docstring'''




            _UpperCamelCase       =	[
                '''unet/diffusion_pytorch_model.fp16.bin''',
                '''unet/diffusion_pytorch_model.fp16.safetensors''',
            ]
            _UpperCamelCase       =	'''fp16'''
            self.assertTrue(is_safetensors_compatible(lowerCAmelCase__	,			variant=lowerCAmelCase__					)					)





       def snake_case__						(							self       :		Tuple					)   ->					Optional[Any]:

            '''simple docstring'''




            _UpperCamelCase       =	[
                '''unet/diffusion_pytorch_model.bin''',
                '''unet/diffusion_pytorch_model.safetensors''',
            ]
            _UpperCamelCase       =	'''fp16'''
            self.assertTrue(is_safetensors_compatible(lowerCAmelCase__	,			variant=lowerCAmelCase__					)					)





       def snake_case__						(							self       :		Tuple					)   ->					Union[str, Any]:

            '''simple docstring'''




            _UpperCamelCase       =	[
                '''safety_checker/pytorch_model.fp16.bin''',
                '''safety_checker/model.fp16.safetensors''',
                '''vae/diffusion_pytorch_model.fp16.bin''',
                '''vae/diffusion_pytorch_model.fp16.safetensors''',
                '''text_encoder/pytorch_model.fp16.bin''',
                '''text_encoder/model.fp16.safetensors''',
                '''unet/diffusion_pytorch_model.fp16.bin''',
                # Removed: 'unet/diffusion_pytorch_model.fp16.safetensors',
            ]
            _UpperCamelCase       =	'''fp16'''
            self.assertFalse(is_safetensors_compatible(lowerCAmelCase__	,			variant=lowerCAmelCase__					)					)





       def snake_case__						(							self       :		Optional[int]					)   ->					Dict:

            '''simple docstring'''




            _UpperCamelCase       =	[
                '''text_encoder/pytorch_model.fp16.bin''',
                '''text_encoder/model.fp16.safetensors''',
            ]
            _UpperCamelCase       =	'''fp16'''
            self.assertTrue(is_safetensors_compatible(lowerCAmelCase__	,			variant=lowerCAmelCase__					)					)





       def snake_case__						(							self       :		Optional[Any]					)   ->					str:

            '''simple docstring'''




            _UpperCamelCase       =	[
                '''text_encoder/pytorch_model.bin''',
                '''text_encoder/model.safetensors''',
            ]
            _UpperCamelCase       =	'''fp16'''
            self.assertTrue(is_safetensors_compatible(lowerCAmelCase__	,			variant=lowerCAmelCase__					)					)







       def snake_case__						(							self       :		List[str]					)   ->					Optional[int]:

            '''simple docstring'''




            _UpperCamelCase       =	[
                '''safety_checker/pytorch_model.fp16.bin''',
                '''safety_checker/model.fp16.safetensors''',
                '''vae/diffusion_pytorch_model.fp16.bin''',
                '''vae/diffusion_pytorch_model.fp16.safetensors''',
                '''text_encoder/pytorch_model.fp16.bin''',
                # 'text_encoder/model.fp16.safetensors',
                '''unet/diffusion_pytorch_model.fp16.bin''',
                '''unet/diffusion_pytorch_model.fp16.safetensors''',
            ]
            _UpperCamelCase       =	'''fp16'''
            self.assertFalse(is_safetensors_compatible(lowerCAmelCase__	,			variant=lowerCAmelCase__					)					)



