
"""simple docstring"""




import gc
import random
import unittest

import torch

from diffusers import (
    IFImgaImgPipeline,
    IFImgaImgSuperResolutionPipeline,
    IFInpaintingPipeline,
    IFInpaintingSuperResolutionPipeline,
    IFPipeline,
    IFSuperResolutionPipeline,
)
from diffusers.models.attention_processor import AttnAddedKVProcessor
from diffusers.utils.import_utils import is_xformers_available
from diffusers.utils.testing_utils import floats_tensor, load_numpy, require_torch_gpu, skip_mps, slow, torch_device

from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_PARAMS
from ..test_pipelines_common import PipelineTesterMixin, assert_mean_pixel_difference
from . import IFPipelineTesterMixin



@skip_mps
class 	lowerCAmelCase_						(a__ ,					a__ ,					unittest.TestCase  ):


				"""simple docstring"""



				__UpperCamelCase      :						Optional[Any]					       =   IFPipeline
				__UpperCamelCase      :						Dict					       =   TEXT_TO_IMAGE_PARAMS - {'''width''', '''height''', '''latents'''}
				__UpperCamelCase      :						Any					       =   TEXT_TO_IMAGE_BATCH_PARAMS
				__UpperCamelCase      :						Union[str, Any]					       =   PipelineTesterMixin.required_optional_params - {'''latents'''}



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



											"""simple docstring"""




											return self._get_dummy_components()



				def 	__magic_name__    (self   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__=0    )					->		List[Any]:



											"""simple docstring"""




											if str(SCREAMING_SNAKE_CASE__    ).startswith("""mps"""    ):
																		SCREAMING_SNAKE_CASE__						:       Dict           =   torch.manual_seed(SCREAMING_SNAKE_CASE__    )
											else:
																		SCREAMING_SNAKE_CASE__						:       Any           =   torch.Generator(device=SCREAMING_SNAKE_CASE__    ).manual_seed(SCREAMING_SNAKE_CASE__    )

											SCREAMING_SNAKE_CASE__						:       int           =   {
											    """prompt""": """A painting of a squirrel eating a burger""",
											    """generator""": generator,
											    """num_inference_steps""": 2,
											    """output_type""": """numpy""",
											}

											return inputs



				def 	__magic_name__    (self    )					->		Tuple:



											"""simple docstring"""




											self._test_save_load_optional_components()



				@unittest.skipIf(torch_device != """cuda"""   ,       reason="""float16 requires CUDA"""    )
				def 	__magic_name__    (self    )					->		List[str]:



											"""simple docstring"""




											super().test_save_load_floataa(expected_max_diff=1E-1    )



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



											"""simple docstring"""




											self._test_attention_slicing_forward_pass(expected_max_diff=1E-2    )



				def 	__magic_name__    (self    )					->		Tuple:



											"""simple docstring"""




											self._test_save_load_local()



				def 	__magic_name__    (self    )					->		Dict:



											"""simple docstring"""




											self._test_inference_batch_single_identical(
											    expected_max_diff=1E-2   ,       )



				@unittest.skipIf(
				    torch_device != """cuda""" or not is_xformers_available()   ,       reason="""XFormers attention is only available with CUDA and `xformers` installed"""   ,       )
				def 	__magic_name__    (self    )					->		Optional[int]:



											"""simple docstring"""




											self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1E-3    )






@slow
@require_torch_gpu
class 	lowerCAmelCase_						(unittest.TestCase  ):


				"""simple docstring"""



				def 	__magic_name__    (self    )					->		Dict:



											"""simple docstring"""




											super().tearDown()
											gc.collect()
											torch.cuda.empty_cache()



				def 	__magic_name__    (self    )					->		Optional[int]:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__						:       Optional[int]           =   IFPipeline.from_pretrained("""DeepFloyd/IF-I-XL-v1.0"""   ,       variant="""fp16"""   ,       torch_dtype=torch.floataa    )

											SCREAMING_SNAKE_CASE__						:       Dict           =   IFSuperResolutionPipeline.from_pretrained(
											    """DeepFloyd/IF-II-L-v1.0"""   ,       variant="""fp16"""   ,       torch_dtype=torch.floataa   ,       text_encoder=SCREAMING_SNAKE_CASE__   ,       tokenizer=SCREAMING_SNAKE_CASE__    )

											# pre compute text embeddings and remove T5 to save memory

											pipe_a.text_encoder.to("""cuda"""    )

											SCREAMING_SNAKE_CASE__      ,      SCREAMING_SNAKE_CASE__						:       Union[str, Any]           =   pipe_a.encode_prompt("""anime turtle"""   ,       device="""cuda"""    )

											del pipe_a.tokenizer
											del pipe_a.text_encoder
											gc.collect()

											SCREAMING_SNAKE_CASE__						:       List[str]           =   None
											SCREAMING_SNAKE_CASE__						:       Union[str, Any]           =   None

											pipe_a.enable_model_cpu_offload()
											pipe_a.enable_model_cpu_offload()

											pipe_a.unet.set_attn_processor(AttnAddedKVProcessor()    )
											pipe_a.unet.set_attn_processor(AttnAddedKVProcessor()    )

											self._test_if(SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__    )

											pipe_a.remove_all_hooks()
											pipe_a.remove_all_hooks()

											# img2img

											SCREAMING_SNAKE_CASE__						:       Union[str, Any]           =   IFImgaImgPipeline(**pipe_a.components    )
											SCREAMING_SNAKE_CASE__						:       Optional[Any]           =   IFImgaImgSuperResolutionPipeline(**pipe_a.components    )

											pipe_a.enable_model_cpu_offload()
											pipe_a.enable_model_cpu_offload()

											pipe_a.unet.set_attn_processor(AttnAddedKVProcessor()    )
											pipe_a.unet.set_attn_processor(AttnAddedKVProcessor()    )

											self._test_if_imgaimg(SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__    )

											pipe_a.remove_all_hooks()
											pipe_a.remove_all_hooks()

											# inpainting

											SCREAMING_SNAKE_CASE__						:       Optional[Any]           =   IFInpaintingPipeline(**pipe_a.components    )
											SCREAMING_SNAKE_CASE__						:       int           =   IFInpaintingSuperResolutionPipeline(**pipe_a.components    )

											pipe_a.enable_model_cpu_offload()
											pipe_a.enable_model_cpu_offload()

											pipe_a.unet.set_attn_processor(AttnAddedKVProcessor()    )
											pipe_a.unet.set_attn_processor(AttnAddedKVProcessor()    )

											self._test_if_inpainting(SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__    )



				def 	__magic_name__    (self   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__    )					->		Optional[int]:



											"""simple docstring"""




											_start_torch_memory_measurement()

											SCREAMING_SNAKE_CASE__						:       Tuple           =   torch.Generator(device="""cpu"""    ).manual_seed(0    )
											SCREAMING_SNAKE_CASE__						:       Dict           =   pipe_a(
											    prompt_embeds=SCREAMING_SNAKE_CASE__   ,       negative_prompt_embeds=SCREAMING_SNAKE_CASE__   ,       num_inference_steps=2   ,       generator=SCREAMING_SNAKE_CASE__   ,       output_type="""np"""   ,       )

											SCREAMING_SNAKE_CASE__						:       int           =   output.images[0]

											assert image.shape == (64, 64, 3)

											SCREAMING_SNAKE_CASE__						:       Any           =   torch.cuda.max_memory_allocated()
											assert mem_bytes < 13 * 10**9

											SCREAMING_SNAKE_CASE__						:       Optional[int]           =   load_numpy(
											    """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if.npy"""    )
											assert_mean_pixel_difference(SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__    )

											# pipeline 2

											_start_torch_memory_measurement()

											SCREAMING_SNAKE_CASE__						:       int           =   torch.Generator(device="""cpu"""    ).manual_seed(0    )

											SCREAMING_SNAKE_CASE__						:       Optional[int]           =   floats_tensor((1, 3, 64, 64)   ,       rng=random.Random(0    )    ).to(SCREAMING_SNAKE_CASE__    )

											SCREAMING_SNAKE_CASE__						:       Optional[Any]           =   pipe_a(
											    prompt_embeds=SCREAMING_SNAKE_CASE__   ,       negative_prompt_embeds=SCREAMING_SNAKE_CASE__   ,       image=SCREAMING_SNAKE_CASE__   ,       generator=SCREAMING_SNAKE_CASE__   ,       num_inference_steps=2   ,       output_type="""np"""   ,       )

											SCREAMING_SNAKE_CASE__						:       List[str]           =   output.images[0]

											assert image.shape == (2_56, 2_56, 3)

											SCREAMING_SNAKE_CASE__						:       List[Any]           =   torch.cuda.max_memory_allocated()
											assert mem_bytes < 4 * 10**9

											SCREAMING_SNAKE_CASE__						:       int           =   load_numpy(
											    """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if_superresolution_stage_II.npy"""    )
											assert_mean_pixel_difference(SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__    )



				def 	__magic_name__    (self   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__    )					->		str:



											"""simple docstring"""




											_start_torch_memory_measurement()

											SCREAMING_SNAKE_CASE__						:       int           =   floats_tensor((1, 3, 64, 64)   ,       rng=random.Random(0    )    ).to(SCREAMING_SNAKE_CASE__    )

											SCREAMING_SNAKE_CASE__						:       Tuple           =   torch.Generator(device="""cpu"""    ).manual_seed(0    )

											SCREAMING_SNAKE_CASE__						:       Any           =   pipe_a(
											    prompt_embeds=SCREAMING_SNAKE_CASE__   ,       negative_prompt_embeds=SCREAMING_SNAKE_CASE__   ,       image=SCREAMING_SNAKE_CASE__   ,       num_inference_steps=2   ,       generator=SCREAMING_SNAKE_CASE__   ,       output_type="""np"""   ,       )

											SCREAMING_SNAKE_CASE__						:       Dict           =   output.images[0]

											assert image.shape == (64, 64, 3)

											SCREAMING_SNAKE_CASE__						:       Optional[Any]           =   torch.cuda.max_memory_allocated()
											assert mem_bytes < 10 * 10**9

											SCREAMING_SNAKE_CASE__						:       str           =   load_numpy(
											    """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if_img2img.npy"""    )
											assert_mean_pixel_difference(SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__    )

											# pipeline 2

											_start_torch_memory_measurement()

											SCREAMING_SNAKE_CASE__						:       Union[str, Any]           =   torch.Generator(device="""cpu"""    ).manual_seed(0    )

											SCREAMING_SNAKE_CASE__						:       List[Any]           =   floats_tensor((1, 3, 2_56, 2_56)   ,       rng=random.Random(0    )    ).to(SCREAMING_SNAKE_CASE__    )
											SCREAMING_SNAKE_CASE__						:       Optional[Any]           =   floats_tensor((1, 3, 64, 64)   ,       rng=random.Random(0    )    ).to(SCREAMING_SNAKE_CASE__    )

											SCREAMING_SNAKE_CASE__						:       Optional[Any]           =   pipe_a(
											    prompt_embeds=SCREAMING_SNAKE_CASE__   ,       negative_prompt_embeds=SCREAMING_SNAKE_CASE__   ,       image=SCREAMING_SNAKE_CASE__   ,       original_image=SCREAMING_SNAKE_CASE__   ,       generator=SCREAMING_SNAKE_CASE__   ,       num_inference_steps=2   ,       output_type="""np"""   ,       )

											SCREAMING_SNAKE_CASE__						:       List[Any]           =   output.images[0]

											assert image.shape == (2_56, 2_56, 3)

											SCREAMING_SNAKE_CASE__						:       Optional[Any]           =   torch.cuda.max_memory_allocated()
											assert mem_bytes < 4 * 10**9

											SCREAMING_SNAKE_CASE__						:       str           =   load_numpy(
											    """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if_img2img_superresolution_stage_II.npy"""    )
											assert_mean_pixel_difference(SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__    )



				def 	__magic_name__    (self   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__    )					->		Union[str, Any]:



											"""simple docstring"""




											_start_torch_memory_measurement()

											SCREAMING_SNAKE_CASE__						:       Dict           =   floats_tensor((1, 3, 64, 64)   ,       rng=random.Random(0    )    ).to(SCREAMING_SNAKE_CASE__    )
											SCREAMING_SNAKE_CASE__						:       Dict           =   floats_tensor((1, 3, 64, 64)   ,       rng=random.Random(1    )    ).to(SCREAMING_SNAKE_CASE__    )

											SCREAMING_SNAKE_CASE__						:       Dict           =   torch.Generator(device="""cpu"""    ).manual_seed(0    )
											SCREAMING_SNAKE_CASE__						:       List[str]           =   pipe_a(
											    prompt_embeds=SCREAMING_SNAKE_CASE__   ,       negative_prompt_embeds=SCREAMING_SNAKE_CASE__   ,       image=SCREAMING_SNAKE_CASE__   ,       mask_image=SCREAMING_SNAKE_CASE__   ,       num_inference_steps=2   ,       generator=SCREAMING_SNAKE_CASE__   ,       output_type="""np"""   ,       )

											SCREAMING_SNAKE_CASE__						:       List[Any]           =   output.images[0]

											assert image.shape == (64, 64, 3)

											SCREAMING_SNAKE_CASE__						:       Tuple           =   torch.cuda.max_memory_allocated()
											assert mem_bytes < 10 * 10**9

											SCREAMING_SNAKE_CASE__						:       Optional[Any]           =   load_numpy(
											    """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if_inpainting.npy"""    )
											assert_mean_pixel_difference(SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__    )

											# pipeline 2

											_start_torch_memory_measurement()

											SCREAMING_SNAKE_CASE__						:       int           =   torch.Generator(device="""cpu"""    ).manual_seed(0    )

											SCREAMING_SNAKE_CASE__						:       Dict           =   floats_tensor((1, 3, 64, 64)   ,       rng=random.Random(0    )    ).to(SCREAMING_SNAKE_CASE__    )
											SCREAMING_SNAKE_CASE__						:       int           =   floats_tensor((1, 3, 2_56, 2_56)   ,       rng=random.Random(0    )    ).to(SCREAMING_SNAKE_CASE__    )
											SCREAMING_SNAKE_CASE__						:       int           =   floats_tensor((1, 3, 2_56, 2_56)   ,       rng=random.Random(1    )    ).to(SCREAMING_SNAKE_CASE__    )

											SCREAMING_SNAKE_CASE__						:       int           =   pipe_a(
											    prompt_embeds=SCREAMING_SNAKE_CASE__   ,       negative_prompt_embeds=SCREAMING_SNAKE_CASE__   ,       image=SCREAMING_SNAKE_CASE__   ,       mask_image=SCREAMING_SNAKE_CASE__   ,       original_image=SCREAMING_SNAKE_CASE__   ,       generator=SCREAMING_SNAKE_CASE__   ,       num_inference_steps=2   ,       output_type="""np"""   ,       )

											SCREAMING_SNAKE_CASE__						:       Dict           =   output.images[0]

											assert image.shape == (2_56, 2_56, 3)

											SCREAMING_SNAKE_CASE__						:       List[str]           =   torch.cuda.max_memory_allocated()
											assert mem_bytes < 4 * 10**9

											SCREAMING_SNAKE_CASE__						:       Any           =   load_numpy(
											    """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if_inpainting_superresolution_stage_II.npy"""    )
											assert_mean_pixel_difference(SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__    )






def lowercase_  (   ):
							torch.cuda.empty_cache()
							torch.cuda.reset_max_memory_allocated()
							torch.cuda.reset_peak_memory_stats()








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

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

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

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

             a_					:		List[str]							=  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        SCREAMING_SNAKE_CASE							(       self				:       List[str]     ,							SCREAMING_SNAKE_CASE__				:       Any     ,							SCREAMING_SNAKE_CASE__				:       Union[str, Any]     ,							SCREAMING_SNAKE_CASE__				:       Any     ,							SCREAMING_SNAKE_CASE__				:       List[str]     ,							*SCREAMING_SNAKE_CASE__				:       Tuple     )		->			Union[str, Any]:
             a_					:		Dict							=  OpenAIGPTModel(config=SCREAMING_SNAKE_CASE__     )
             model.to(SCREAMING_SNAKE_CASE__     )
             model.eval()

             a_					:		str							=  model(SCREAMING_SNAKE_CASE__     ,							token_type_ids=SCREAMING_SNAKE_CASE__     ,							head_mask=SCREAMING_SNAKE_CASE__     )
             a_					:		Dict							=  model(SCREAMING_SNAKE_CASE__     ,							token_type_ids=SCREAMING_SNAKE_CASE__     )
             a_					:		Dict							=  model(SCREAMING_SNAKE_CASE__     )

             self.parent.assertEqual(result.last_hidden_state.shape     ,							(self.batch_size, self.seq_length, self.hidden_size)     )
      def        SCREAMING_SNAKE_CASE							(       self				:       Optional[int]     ,							SCREAMING_SNAKE_CASE__				:       List[str]     ,							SCREAMING_SNAKE_CASE__				:       Tuple     ,							SCREAMING_SNAKE_CASE__				:       Tuple     ,							SCREAMING_SNAKE_CASE__				:       Union[str, Any]     ,							*SCREAMING_SNAKE_CASE__				:       Optional[int]     )		->			Any:
             a_					:		str							=  OpenAIGPTLMHeadModel(SCREAMING_SNAKE_CASE__     )
             model.to(SCREAMING_SNAKE_CASE__     )
             model.eval()

             a_					:		Optional[int]							=  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        SCREAMING_SNAKE_CASE							(       self				:       Optional[Any]     ,							SCREAMING_SNAKE_CASE__				:       Optional[Any]     ,							SCREAMING_SNAKE_CASE__				:       int     ,							SCREAMING_SNAKE_CASE__				:       Optional[int]     ,							SCREAMING_SNAKE_CASE__				:       Optional[Any]     ,							*SCREAMING_SNAKE_CASE__				:       Union[str, Any]     )		->			Dict:
             a_					:		int							=  OpenAIGPTDoubleHeadsModel(SCREAMING_SNAKE_CASE__     )
             model.to(SCREAMING_SNAKE_CASE__     )
             model.eval()

             a_					:		str							=  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        SCREAMING_SNAKE_CASE							(       self				:       Tuple     ,							SCREAMING_SNAKE_CASE__				:       Dict     ,							SCREAMING_SNAKE_CASE__				:       Dict     ,							SCREAMING_SNAKE_CASE__				:       List[Any]     ,							SCREAMING_SNAKE_CASE__				:       Union[str, Any]     ,							*SCREAMING_SNAKE_CASE__				:       str     )		->			List[str]:
             a_					:		Any							=  self.num_labels
             a_					:		Dict							=  OpenAIGPTForSequenceClassification(SCREAMING_SNAKE_CASE__     )
             model.to(SCREAMING_SNAKE_CASE__     )
             model.eval()

             a_					:		Dict							=  ids_tensor([self.batch_size]     ,							self.type_sequence_label_size     )
             a_					:		Any							=  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        SCREAMING_SNAKE_CASE							(       self				:       Union[str, Any]     )		->			Tuple:
             a_					:		Optional[Any]							=  self.prepare_config_and_inputs()
             (
                 (
                 a_
             )							,		(
                 a_
             )							,		(
                 a_
             )							,		(
                 a_
             )							,		(
                 a_
             )							,		(
                 a_
             )							,		(
                 a_
             )							,		
             )					:		Optional[Any]							=  config_and_inputs
             a_					:		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__  (				lowercase__						,    lowercase__						,    lowercase__						,    unittest.TestCase			):
      snake_case__				:     Tuple        =						(
          (OpenAIGPTModel, OpenAIGPTLMHeadModel, OpenAIGPTDoubleHeadsModel, OpenAIGPTForSequenceClassification)
          if is_torch_available()
          else ()
      )
      snake_case__				:     List[str]        =						(
          (OpenAIGPTLMHeadModel,) if is_torch_available() else ()
      )  # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly
      snake_case__				:     Dict        =						(
          {
              '''feature-extraction''': OpenAIGPTModel,
              '''text-classification''': OpenAIGPTForSequenceClassification,
              '''text-generation''': OpenAIGPTLMHeadModel,
              '''zero-shot''': OpenAIGPTForSequenceClassification,
          }
          if is_torch_available()
          else {}
      )
      def        SCREAMING_SNAKE_CASE							(       self				:       List[str]     ,							SCREAMING_SNAKE_CASE__				:       Tuple     ,							SCREAMING_SNAKE_CASE__				:       Optional[Any]     ,							SCREAMING_SNAKE_CASE__				:       Tuple     ,							SCREAMING_SNAKE_CASE__				:       Dict     ,							SCREAMING_SNAKE_CASE__				:       List[str]     )		->			Dict:
             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        SCREAMING_SNAKE_CASE							(       self				:       int     ,							SCREAMING_SNAKE_CASE__				:       Dict     ,							SCREAMING_SNAKE_CASE__				:       Dict     ,							SCREAMING_SNAKE_CASE__				:       Any=False     )		->			List[str]:
             a_					:		str							=  super()._prepare_for_class(SCREAMING_SNAKE_CASE__     ,							SCREAMING_SNAKE_CASE__     ,							return_labels=SCREAMING_SNAKE_CASE__     )

             if return_labels:
                    if model_class.__name__ == "OpenAIGPTDoubleHeadsModel":
                           a_					:		Optional[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__     ,							)
                           a_					:		str							=  inputs_dict['labels']
                           a_					:		Optional[int]							=  inputs_dict['labels']
                           a_					:		Optional[int]							=  torch.zeros(
                               (self.model_tester.batch_size, self.model_tester.num_choices)     ,							dtype=torch.long     ,							device=SCREAMING_SNAKE_CASE__     ,							)
                           a_					:		Union[str, Any]							=  torch.zeros(
                               self.model_tester.batch_size     ,							dtype=torch.long     ,							device=SCREAMING_SNAKE_CASE__     )
             return inputs_dict
      def        SCREAMING_SNAKE_CASE							(       self				:       str     )		->			List[Any]:
             a_					:		str							=  OpenAIGPTModelTester(self     )
             a_					:		int							=  ConfigTester(self     ,							config_class=SCREAMING_SNAKE_CASE__     ,							n_embd=3_7     )
      def        SCREAMING_SNAKE_CASE							(       self				:       Tuple     )		->			Tuple:
             self.config_tester.run_common_tests()
      def        SCREAMING_SNAKE_CASE							(       self				:       List[Any]     )		->			Tuple:
             a_					:		Dict							=  self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_openai_gpt_model(*SCREAMING_SNAKE_CASE__     )
      def        SCREAMING_SNAKE_CASE							(       self				:       Optional[int]     )		->			Tuple:
             a_					:		Any							=  self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_lm_head_model(*SCREAMING_SNAKE_CASE__     )
      def        SCREAMING_SNAKE_CASE							(       self				:       Any     )		->			Optional[Any]:
             a_					:		Optional[int]							=  self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_double_lm_head_model(*SCREAMING_SNAKE_CASE__     )
      def        SCREAMING_SNAKE_CASE							(       self				:       Optional[Any]     )		->			Optional[Any]:
             a_					:		int							=  self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*SCREAMING_SNAKE_CASE__     )

      @slow
      def        SCREAMING_SNAKE_CASE							(       self				:       List[str]     )		->			str:
             for model_name in OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                    a_					:		str							=  OpenAIGPTModel.from_pretrained(SCREAMING_SNAKE_CASE__     )
                    self.assertIsNotNone(SCREAMING_SNAKE_CASE__     )
@require_torch
class  SCREAMING_SNAKE_CASE__  (				unittest.TestCase			):
      @slow
      def        SCREAMING_SNAKE_CASE							(       self				:       Dict     )		->			int:
             a_					:		Dict							=  OpenAIGPTLMHeadModel.from_pretrained('openai-gpt'     )
             model.to(SCREAMING_SNAKE_CASE__     )
             a_					:		List[Any]							=  torch.tensor([[4_8_1, 4_7_3_5, 5_4_4]]     ,							dtype=torch.long     ,							device=SCREAMING_SNAKE_CASE__     )  # the president is
             a_					:		Tuple							=  [
                 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

             a_					:		Dict							=  model.generate(SCREAMING_SNAKE_CASE__     ,							do_sample=SCREAMING_SNAKE_CASE__     )
             self.assertListEqual(output_ids[0].tolist()     ,							SCREAMING_SNAKE_CASE__     )


