


import gc
import random
import unittest

import numpy as np
import torch
from transformers import CLIPImageProcessor, CLIPVisionConfig, CLIPVisionModel

from diffusers import HeunDiscreteScheduler, PriorTransformer, ShapEImgaImgPipeline
from diffusers.pipelines.shap_e import ShapERenderer
from diffusers.utils import floats_tensor, load_image, load_numpy, slow
from diffusers.utils.testing_utils import require_torch_gpu, torch_device

from ..test_pipelines_common import PipelineTesterMixin, assert_mean_pixel_difference





class       A    (   A_					,				unittest.TestCase ):
							UpperCamelCase_   :							Optional[int]       =ShapEImgaImgPipeline
							UpperCamelCase_   :							int       =['''image''']
							UpperCamelCase_   :							int       =['''image''']
							UpperCamelCase_   :							Union[str, Any]       =[
							    '''num_images_per_prompt''',
							    '''num_inference_steps''',
							    '''generator''',
							    '''latents''',
							    '''guidance_scale''',
							    '''frame_size''',
							    '''output_type''',
							    '''return_dict''',
							]
							UpperCamelCase_   :							Optional[Any]       =False


							@property
							def 			_A       (self					):
									return 3_2


							@property
							def 			_A       (self					):
									return 3_2


							@property
							def 			_A       (self					):
									return self.time_input_dim * 4


							@property
							def 			_A       (self					):
									return 8


							@property
							def 			_A       (self					):
									torch.manual_seed(0					)
									__lowercase=			CLIPVisionConfig(
									    hidden_size=self.text_embedder_hidden_size      ,				image_size=6_4      ,				projection_dim=self.text_embedder_hidden_size      ,				intermediate_size=3_7      ,				num_attention_heads=4      ,				num_channels=3      ,				num_hidden_layers=5      ,				patch_size=1      ,				)

									__lowercase=			CLIPVisionModel(lowerCAmelCase					)
									return model


							@property
							def 			_A       (self					):
									__lowercase=			CLIPImageProcessor(
									    crop_size=2_2_4      ,				do_center_crop=lowerCAmelCase      ,				do_normalize=lowerCAmelCase      ,				do_resize=lowerCAmelCase      ,				image_mean=[0.48_14_54_66, 0.4_57_82_75, 0.40_82_10_73]      ,				image_std=[0.26_86_29_54, 0.26_13_02_58, 0.27_57_77_11]      ,				resample=3      ,				size=2_2_4      ,				)

									return image_processor


							@property
							def 			_A       (self					):
									torch.manual_seed(0					)

									__lowercase=			{
									    'num_attention_heads': 2,
									    'attention_head_dim': 1_6,
									    'embedding_dim': self.time_input_dim,
									    'num_embeddings': 3_2,
									    'embedding_proj_dim': self.text_embedder_hidden_size,
									    'time_embed_dim': self.time_embed_dim,
									    'num_layers': 1,
									    'clip_embed_dim': self.time_input_dim * 2,
									    'additional_embeddings': 0,
									    'time_embed_act_fn': 'gelu',
									    'norm_in_type': 'layer',
									    'embedding_proj_norm_type': 'layer',
									    'encoder_hid_proj_type': None,
									    'added_emb_type': None,
									}

									__lowercase=			PriorTransformer(**lowerCAmelCase					)
									return model


							@property
							def 			_A       (self					):
									torch.manual_seed(0					)

									__lowercase=			{
									    'param_shapes': (
									        (self.renderer_dim, 9_3),
									        (self.renderer_dim, 8),
									        (self.renderer_dim, 8),
									        (self.renderer_dim, 8),
									    ),
									    'd_latent': self.time_input_dim,
									    'd_hidden': self.renderer_dim,
									    'n_output': 1_2,
									    'background': (
									        0.1,
									        0.1,
									        0.1,
									    ),
									}
									__lowercase=			ShapERenderer(**lowerCAmelCase					)
									return model


							def 			_A       (self					):
									__lowercase=			self.dummy_prior
									__lowercase=			self.dummy_image_encoder
									__lowercase=			self.dummy_image_processor
									__lowercase=			self.dummy_renderer

									__lowercase=			HeunDiscreteScheduler(
									    beta_schedule='exp'      ,				num_train_timesteps=1_0_2_4      ,				prediction_type='sample'      ,				use_karras_sigmas=lowerCAmelCase      ,				clip_sample=lowerCAmelCase      ,				clip_sample_range=1.0      ,				)
									__lowercase=			{
									    'prior': prior,
									    'image_encoder': image_encoder,
									    'image_processor': image_processor,
									    'renderer': renderer,
									    'scheduler': scheduler,
									}

									return components


							def 			_A       (self      ,				lowerCAmelCase      ,				lowerCAmelCase=0					):
									__lowercase=			floats_tensor((1, 3, 6_4, 6_4)      ,				rng=random.Random(lowerCAmelCase					)					).to(lowerCAmelCase					)

									if str(lowerCAmelCase					).startswith('mps'					):
											__lowercase=			torch.manual_seed(lowerCAmelCase					)
									else:
											__lowercase=			torch.Generator(device=lowerCAmelCase					).manual_seed(lowerCAmelCase					)
									__lowercase=			{
									    'image': input_image,
									    'generator': generator,
									    'num_inference_steps': 1,
									    'frame_size': 3_2,
									    'output_type': 'np',
									}
									return inputs


							def 			_A       (self					):
									__lowercase=			'cpu'

									__lowercase=			self.get_dummy_components()

									__lowercase=			self.pipeline_class(**lowerCAmelCase					)
									__lowercase=			pipe.to(lowerCAmelCase					)

									pipe.set_progress_bar_config(disable=lowerCAmelCase					)

									__lowercase=			pipe(**self.get_dummy_inputs(lowerCAmelCase					)					)
									__lowercase=			output.images[0]
									__lowercase=			image[0, -3:, -3:, -1]

									assert image.shape == (2_0, 3_2, 3_2, 3)

									__lowercase=			np.array(
									    [
									        0.00_03_92_16,
									        0.00_03_92_16,
									        0.00_03_92_16,
									        0.00_03_92_16,
									        0.00_03_92_16,
									        0.00_03_92_16,
									        0.00_03_92_16,
									        0.00_03_92_16,
									        0.00_03_92_16,
									    ]					)

									assert np.abs(image_slice.flatten() - expected_slice					).max() < 1E-2


							def 			_A       (self					):
									# NOTE: Larger batch sizes cause this test to timeout, only test on smaller batches
									self._test_inference_batch_consistent(batch_sizes=[1, 2]					)


							def 			_A       (self					):
									__lowercase=			torch_device == 'cpu'
									__lowercase=			True
									self._test_inference_batch_single_identical(
									    batch_size=2      ,				test_max_difference=lowerCAmelCase      ,				relax_max_difference=lowerCAmelCase      ,				)







							def 			_A       (self					):
									__lowercase=			self.get_dummy_components()
									__lowercase=			self.pipeline_class(**lowerCAmelCase					)
									__lowercase=			pipe.to(lowerCAmelCase					)
									pipe.set_progress_bar_config(disable=lowerCAmelCase					)

									__lowercase=			1
									__lowercase=			2

									__lowercase=			self.get_dummy_inputs(lowerCAmelCase					)

									for key in inputs.keys():
											if key in self.batch_params:
													__lowercase=			batch_size * [inputs[key]]

									__lowercase=			pipe(**lowerCAmelCase      ,				num_images_per_prompt=lowerCAmelCase					)[0]

									assert images.shape[0] == batch_size * num_images_per_prompt





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


							def 			_A       (self					):
									# clean up the VRAM after each test
									super().tearDown()
									gc.collect()
									torch.cuda.empty_cache()







							def 			_A       (self					):
									__lowercase=			load_image(
									    'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main' '/shap_e/corgi.png'					)
									__lowercase=			load_numpy(
									    'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'
									    '/shap_e/test_shap_e_img2img_out.npy'					)
									__lowercase=			ShapEImgaImgPipeline.from_pretrained('openai/shap-e-img2img'					)
									__lowercase=			pipe.to(lowerCAmelCase					)
									pipe.set_progress_bar_config(disable=lowerCAmelCase					)

									__lowercase=			torch.Generator(device=lowerCAmelCase					).manual_seed(0					)

									__lowercase=			pipe(
									    lowerCAmelCase      ,				generator=lowerCAmelCase      ,				guidance_scale=3.0      ,				num_inference_steps=6_4      ,				frame_size=6_4      ,				output_type='np'      ,				).images[0]

									assert images.shape == (2_0, 6_4, 6_4, 3)

									assert_mean_pixel_difference(lowerCAmelCase      ,				lowerCAmelCase					)










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


							def __init__(self      ,				lowerCAmelCase      ,				lowerCAmelCase=1_3      ,				lowerCAmelCase=7      ,				lowerCAmelCase=True      ,				lowerCAmelCase=True      ,				lowerCAmelCase=True      ,				lowerCAmelCase=9_9      ,				lowerCAmelCase=3_2      ,				lowerCAmelCase=5      ,				lowerCAmelCase=4      ,				lowerCAmelCase=3_7      ,				lowerCAmelCase="gelu"      ,				lowerCAmelCase=0.1      ,				lowerCAmelCase=0.1      ,				lowerCAmelCase=5_1_2      ,				lowerCAmelCase=1_6      ,				lowerCAmelCase=2      ,				lowerCAmelCase=0.02      ,				lowerCAmelCase=3      ,				lowerCAmelCase=4      ,				lowerCAmelCase=None      ,				):
									__lowercase=			parent
									__lowercase=			batch_size
									__lowercase=			seq_length
									__lowercase=			is_training
									__lowercase=			use_token_type_ids
									__lowercase=			use_labels
									__lowercase=			vocab_size
									__lowercase=			hidden_size
									__lowercase=			num_hidden_layers
									__lowercase=			num_attention_heads
									__lowercase=			intermediate_size
									__lowercase=			hidden_act
									__lowercase=			hidden_dropout_prob
									__lowercase=			attention_probs_dropout_prob
									__lowercase=			max_position_embeddings
									__lowercase=			type_vocab_size
									__lowercase=			type_sequence_label_size
									__lowercase=			initializer_range
									__lowercase=			num_labels
									__lowercase=			num_choices
									__lowercase=			scope
									__lowercase=			self.vocab_size - 1


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

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

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

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

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

									__lowercase=			model(lowerCAmelCase      ,				token_type_ids=lowerCAmelCase      ,				head_mask=lowerCAmelCase					)
									__lowercase=			model(lowerCAmelCase      ,				token_type_ids=lowerCAmelCase					)
									__lowercase=			model(lowerCAmelCase					)

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


							def 			_A       (self      ,				lowerCAmelCase      ,				lowerCAmelCase      ,				lowerCAmelCase      ,				lowerCAmelCase      ,				*lowerCAmelCase					):
									__lowercase=			OpenAIGPTLMHeadModel(lowerCAmelCase					)
									model.to(lowerCAmelCase					)
									model.eval()

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

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

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







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

									return config, inputs_dict





@require_torch
class       A    (   A_					,				A_					,				A_					,				unittest.TestCase ):
							UpperCamelCase_   :							Optional[Any]       =(
							    (OpenAIGPTModel, OpenAIGPTLMHeadModel, OpenAIGPTDoubleHeadsModel, OpenAIGPTForSequenceClassification)
							    if is_torch_available()
							    else ()
							)
							UpperCamelCase_   :							Tuple       =(
							    (OpenAIGPTLMHeadModel,) if is_torch_available() else ()
							)  # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly
							UpperCamelCase_   :							List[str]       =(
							    {
							        '''feature-extraction''': OpenAIGPTModel,
							        '''text-classification''': OpenAIGPTForSequenceClassification,
							        '''text-generation''': OpenAIGPTLMHeadModel,
							        '''zero-shot''': OpenAIGPTForSequenceClassification,
							    }
							    if is_torch_available()
							    else {}
							)


							def 			_A       (self      ,				lowerCAmelCase      ,				lowerCAmelCase      ,				lowerCAmelCase      ,				lowerCAmelCase      ,				lowerCAmelCase					):
									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      ,				lowerCAmelCase      ,				lowerCAmelCase      ,				lowerCAmelCase=False					):
									__lowercase=			super()._prepare_for_class(lowerCAmelCase      ,				lowerCAmelCase      ,				return_labels=lowerCAmelCase					)

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


							def 			_A       (self					):
									__lowercase=			OpenAIGPTModelTester(self					)
									__lowercase=			ConfigTester(self      ,				config_class=lowerCAmelCase      ,				n_embd=3_7					)


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


							def 			_A       (self					):
									__lowercase=			self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_openai_gpt_model(*lowerCAmelCase					)


							def 			_A       (self					):
									__lowercase=			self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_lm_head_model(*lowerCAmelCase					)


							def 			_A       (self					):
									__lowercase=			self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_double_lm_head_model(*lowerCAmelCase					)


							def 			_A       (self					):
									__lowercase=			self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*lowerCAmelCase					)







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





@require_torch
class       A    (   unittest.TestCase ):


							@slow
							def 			_A       (self					):
									__lowercase=			OpenAIGPTLMHeadModel.from_pretrained('openai-gpt'					)
									model.to(lowerCAmelCase					)
									__lowercase=			torch.tensor([[4_8_1, 4_7_3_5, 5_4_4]]      ,				dtype=torch.long      ,				device=lowerCAmelCase					)  # the president is
									__lowercase=			[
									    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

									__lowercase=			model.generate(lowerCAmelCase      ,				do_sample=lowerCAmelCase					)
									self.assertListEqual(output_ids[0].tolist()      ,				lowerCAmelCase					)






