



import inspect
import math
import tempfile
import unittest

import numpy as np

from transformers import ViTMAEConfig
from transformers.testing_utils import require_torch, require_vision, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
			import torch
			from torch import nn

			from transformers import ViTMAEForPreTraining, ViTMAEModel
			from transformers.models.vit.modeling_vit import VIT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
			from PIL import Image

			from transformers import ViTImageProcessor




class 	lowerCamelCase      :







					'''simple docstring'''







					def __init__(			self    ,	lowerCAmelCase    ,	lowerCAmelCase=13    ,	lowerCAmelCase=30    ,	lowerCAmelCase=2    ,	lowerCAmelCase=3    ,	lowerCAmelCase=True    ,	lowerCAmelCase=True    ,	lowerCAmelCase=32    ,	lowerCAmelCase=5    ,	lowerCAmelCase=4    ,	lowerCAmelCase=37    ,	lowerCAmelCase="gelu"    ,	lowerCAmelCase=0.1    ,	lowerCAmelCase=0.1    ,	lowerCAmelCase=10    ,	lowerCAmelCase=0.02    ,	lowerCAmelCase=3    ,	lowerCAmelCase=0.6    ,	lowerCAmelCase=None    ,	):
							UpperCAmelCase_						=				parent
							UpperCAmelCase_						=				batch_size
							UpperCAmelCase_						=				image_size
							UpperCAmelCase_						=				patch_size
							UpperCAmelCase_						=				num_channels
							UpperCAmelCase_						=				is_training
							UpperCAmelCase_						=				use_labels
							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_						=				type_sequence_label_size
							UpperCAmelCase_						=				initializer_range
							UpperCAmelCase_						=				mask_ratio
							UpperCAmelCase_						=				scope

							# in ViTMAE, the expected sequence length = (num_patches + 1) * (1 - config.mask_ratio), rounded above
							# (we add 1 for the [CLS] token)
							UpperCAmelCase_						=				(image_size // patch_size) ** 2
							UpperCAmelCase_						=				int(math.ceil((1 - mask_ratio) * (num_patches + 1)     )     )



					def    A__		(			self     ):
							UpperCAmelCase_						=				floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]     )

							UpperCAmelCase_						=				None
							if self.use_labels:
									UpperCAmelCase_						=				ids_tensor([self.batch_size]    ,	self.type_sequence_label_size     )

							UpperCAmelCase_						=				self.get_config()

							return config, pixel_values, labels



					def    A__		(			self     ):
							return ViTMAEConfig(
							    image_size=self.image_size    ,	patch_size=self.patch_size    ,	num_channels=self.num_channels    ,	hidden_size=self.hidden_size    ,	num_hidden_layers=self.num_hidden_layers    ,	num_attention_heads=self.num_attention_heads    ,	intermediate_size=self.intermediate_size    ,	hidden_act=self.hidden_act    ,	hidden_dropout_prob=self.hidden_dropout_prob    ,	attention_probs_dropout_prob=self.attention_probs_dropout_prob    ,	is_decoder=_lowerCAmelCase    ,	initializer_range=self.initializer_range    ,	mask_ratio=self.mask_ratio    ,	)



					def    A__		(			self    ,	lowerCAmelCase    ,	lowerCAmelCase    ,	lowerCAmelCase     ):
							UpperCAmelCase_						=				ViTMAEModel(config=_lowerCAmelCase     )
							model.to(_lowerCAmelCase     )
							model.eval()
							UpperCAmelCase_						=				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     ):
							UpperCAmelCase_						=				ViTMAEForPreTraining(_lowerCAmelCase     )
							model.to(_lowerCAmelCase     )
							model.eval()
							UpperCAmelCase_						=				model(_lowerCAmelCase     )
							UpperCAmelCase_						=				(self.image_size // self.patch_size) ** 2
							UpperCAmelCase_						=				self.patch_size**2 * self.num_channels
							self.parent.assertEqual(result.logits.shape    ,	(self.batch_size, num_patches, expected_num_channels)     )

							# test greyscale images
							UpperCAmelCase_						=				1
							UpperCAmelCase_						=				ViTMAEForPreTraining(_lowerCAmelCase     )
							model.to(_lowerCAmelCase     )
							model.eval()
							UpperCAmelCase_						=				floats_tensor([self.batch_size, 1, self.image_size, self.image_size]     )
							UpperCAmelCase_						=				model(_lowerCAmelCase     )
							UpperCAmelCase_						=				self.patch_size**2
							self.parent.assertEqual(result.logits.shape    ,	(self.batch_size, num_patches, expected_num_channels)     )



					def    A__		(			self     ):
							UpperCAmelCase_						=				self.prepare_config_and_inputs()
							UpperCAmelCase_					,						UpperCAmelCase_					,						UpperCAmelCase_						=				config_and_inputs
							UpperCAmelCase_						=				{"pixel_values": pixel_values}
							return config, inputs_dict





@require_torch
class 	lowerCamelCase      (						_lowerCAmelCase,     _lowerCAmelCase,     unittest.TestCase  ):







					'''simple docstring'''







					lowerCAmelCase_				:    Tuple									=  (ViTMAEModel, ViTMAEForPreTraining) if is_torch_available() else ()
					lowerCAmelCase_				:    Optional[int]									=  {'feature-extraction': ViTMAEModel} if is_torch_available() else {}

					lowerCAmelCase_				:    Optional[int]									=  False
					lowerCAmelCase_				:    str									=  False
					lowerCAmelCase_				:    Dict									=  False
					lowerCAmelCase_				:    Any									=  False



					def    A__		(			self     ):
							UpperCAmelCase_						=				ViTMAEModelTester(self     )
							UpperCAmelCase_						=				ConfigTester(self    ,	config_class=_lowerCAmelCase    ,	has_text_modality=_lowerCAmelCase    ,	hidden_size=37     )



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



					@unittest.skip(reason="ViTMAE does not use inputs_embeds"     )
					def    A__		(			self     ):
							pass



					def    A__		(			self     ):
							UpperCAmelCase_					,						UpperCAmelCase_						=				self.model_tester.prepare_config_and_inputs_for_common()

							for model_class in self.all_model_classes:
									UpperCAmelCase_						=				model_class(_lowerCAmelCase     )
									self.assertIsInstance(model.get_input_embeddings()    ,	(nn.Module)     )
									UpperCAmelCase_						=				model.get_output_embeddings()
									self.assertTrue(x is None or isinstance(_lowerCAmelCase    ,	nn.Linear     )     )



					def    A__		(			self     ):
							UpperCAmelCase_					,						UpperCAmelCase_						=				self.model_tester.prepare_config_and_inputs_for_common()

							for model_class in self.all_model_classes:
									UpperCAmelCase_						=				model_class(_lowerCAmelCase     )
									UpperCAmelCase_						=				inspect.signature(model.forward     )
									# signature.parameters is an OrderedDict => so arg_names order is deterministic
									UpperCAmelCase_						=				[*signature.parameters.keys()]

									UpperCAmelCase_						=				["pixel_values"]
									self.assertListEqual(arg_names[:1]    ,	_lowerCAmelCase     )



					def    A__		(			self     ):
							UpperCAmelCase_						=				self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*_lowerCAmelCase     )



					def    A__		(			self     ):
							UpperCAmelCase_						=				self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_pretraining(*_lowerCAmelCase     )



					def    A__		(			self    ,	lowerCAmelCase    ,	lowerCAmelCase    ,	lowerCAmelCase     ):
							np.random.seed(2     )

							UpperCAmelCase_						=				int((pt_model.config.image_size // pt_model.config.patch_size) ** 2     )
							UpperCAmelCase_						=				np.random.uniform(size=(self.model_tester.batch_size, num_patches)     )
							UpperCAmelCase_						=				torch.from_numpy(_lowerCAmelCase     )

							# Add `noise` argument.
							# PT inputs will be prepared in `super().check_pt_tf_models()` with this added `noise` argument
							UpperCAmelCase_						=				pt_noise

							super().check_pt_tf_models(_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase     )



					def    A__		(			self     ):
							UpperCAmelCase_					,						UpperCAmelCase_						=				self.model_tester.prepare_config_and_inputs_for_common()

							for model_class in self.all_model_classes:
									UpperCAmelCase_						=				model_class(_lowerCAmelCase     )
									model.to(_lowerCAmelCase     )
									model.eval()
									# make random mask reproducible
									torch.manual_seed(2     )
									with torch.no_grad():
											UpperCAmelCase_						=				model(**self._prepare_for_class(_lowerCAmelCase    ,	_lowerCAmelCase     )     )

									UpperCAmelCase_						=				outputs[0].cpu().numpy()
									UpperCAmelCase_						=				0

									with tempfile.TemporaryDirectory() as tmpdirname:
											model.save_pretrained(_lowerCAmelCase     )
											UpperCAmelCase_						=				model_class.from_pretrained(_lowerCAmelCase     )
											model.to(_lowerCAmelCase     )
											# make random mask reproducible
											torch.manual_seed(2     )
											with torch.no_grad():
													UpperCAmelCase_						=				model(**self._prepare_for_class(_lowerCAmelCase    ,	_lowerCAmelCase     )     )

											# Make sure we don't have nans
											UpperCAmelCase_						=				after_outputs[0].cpu().numpy()
											UpperCAmelCase_						=				0
											UpperCAmelCase_						=				np.amax(np.abs(out_a - out_a     )     )
											self.assertLessEqual(_lowerCAmelCase    ,	1e-5     )



					@unittest.skip(
					    reason="ViTMAE returns a random mask + ids_restore in each forward pass. See test_save_load\n    to get deterministic results."     )
					def    A__		(			self     ):
							pass



					@unittest.skip(
					    reason="ViTMAE returns a random mask + ids_restore in each forward pass. See test_save_load\n    to get deterministic results."     )
					def    A__		(			self     ):
							pass



					@unittest.skip(
					    reason="ViTMAE returns a random mask + ids_restore in each forward pass. See test_save_load\n    to get deterministic results."     )
					def    A__		(			self     ):
							pass



					@unittest.skip(reason="ViTMAE returns a random mask + ids_restore in each forward pass. See test_save_load"     )
					def    A__		(			self     ):
							pass



					@unittest.skip("Will be fixed soon by reducing the size of the model used for common tests."     )
					def    A__		(			self     ):
							pass



					@slow
					def    A__		(			self     ):







							for model_name in VIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
									UpperCAmelCase_						=				ViTMAEModel.from_pretrained(_lowerCAmelCase     )
									self.assertIsNotNone(_lowerCAmelCase     )







def snake_case__  (			)		->							List[str]:
		UpperCAmelCase_						=				Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" )
		return image




@require_torch
@require_vision
class 	lowerCamelCase      (						unittest.TestCase  ):







					'''simple docstring'''







					@cached_property
					def    A__		(			self     ):
							return ViTImageProcessor.from_pretrained("facebook/vit-mae-base"     ) if is_vision_available() else None



					@slow
					def    A__		(			self     ):
							np.random.seed(2     )

							UpperCAmelCase_						=				ViTMAEForPreTraining.from_pretrained("facebook/vit-mae-base"     ).to(_lowerCAmelCase     )

							UpperCAmelCase_						=				self.default_image_processor
							UpperCAmelCase_						=				prepare_img()
							UpperCAmelCase_						=				image_processor(images=_lowerCAmelCase    ,	return_tensors="pt"     ).to(_lowerCAmelCase     )

							# prepare a noise vector that will be also used for testing the TF model
							# (this way we can ensure that the PT and TF models operate on the same inputs)
							UpperCAmelCase_						=				ViTMAEConfig()
							UpperCAmelCase_						=				int((vit_mae_config.image_size // vit_mae_config.patch_size) ** 2     )
							UpperCAmelCase_						=				np.random.uniform(size=(1, num_patches)     )

							# forward pass
							with torch.no_grad():
									UpperCAmelCase_						=				model(**_lowerCAmelCase    ,	noise=torch.from_numpy(_lowerCAmelCase     ).to(device=_lowerCAmelCase     )     )

							# verify the logits
							UpperCAmelCase_						=				torch.Size((1, 196, 768)     )
							self.assertEqual(outputs.logits.shape    ,	_lowerCAmelCase     )

							UpperCAmelCase_						=				torch.tensor(
							    [[-0.0548, -1.7023, -0.9325], [0.3721, -0.5670, -0.2233], [0.8235, -1.3878, -0.3524]]     )

							self.assertTrue(torch.allclose(outputs.logits[0, :3, :3]    ,	expected_slice.to(_lowerCAmelCase     )    ,	atol=1e-4     )     )






import unittest

import numpy as np
import torch
from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer

from diffusers import (
    AutoencoderKL,
    DDIMScheduler,
    DPMSolverMultistepScheduler,
    TextToVideoSDPipeline,
    UNetaDConditionModel,
)
from diffusers.utils import is_xformers_available, load_numpy, skip_mps, slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism

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


enable_full_determinism()



@skip_mps
class 	lowerCamelCase      (						lowercase__,     unittest.TestCase  ):







					'''simple docstring'''







					lowerCAmelCase_				:    int									=  TextToVideoSDPipeline
					lowerCAmelCase_				:    Dict									=  TEXT_TO_IMAGE_PARAMS
					lowerCAmelCase_				:    Optional[Any]									=  TEXT_TO_IMAGE_BATCH_PARAMS
					# No `output_type`.
					lowerCAmelCase_				:    Optional[Any]									=  frozenset(
					    [
					        'num_inference_steps',
					        'generator',
					        'latents',
					        'return_dict',
					        'callback',
					        'callback_steps',
					    ]  )



					def    A__		(			self     ):
							torch.manual_seed(0     )
							UpperCAmelCase_						=				UNetaDConditionModel(
							    block_out_channels=(32, 64, 64, 64)    ,	layers_per_block=2    ,	sample_size=32    ,	in_channels=4    ,	out_channels=4    ,	down_block_types=("CrossAttnDownBlock3D", "CrossAttnDownBlock3D", "CrossAttnDownBlock3D", "DownBlock3D")    ,	up_block_types=("UpBlock3D", "CrossAttnUpBlock3D", "CrossAttnUpBlock3D", "CrossAttnUpBlock3D")    ,	cross_attention_dim=32    ,	attention_head_dim=4    ,	)
							UpperCAmelCase_						=				DDIMScheduler(
							    beta_start=0.00085    ,	beta_end=0.012    ,	beta_schedule="scaled_linear"    ,	clip_sample=lowerCAmelCase    ,	set_alpha_to_one=lowerCAmelCase    ,	)
							torch.manual_seed(0     )
							UpperCAmelCase_						=				AutoencoderKL(
							    block_out_channels=[32, 64]    ,	in_channels=3    ,	out_channels=3    ,	down_block_types=["DownEncoderBlock2D", "DownEncoderBlock2D"]    ,	up_block_types=["UpDecoderBlock2D", "UpDecoderBlock2D"]    ,	latent_channels=4    ,	sample_size=128    ,	)
							torch.manual_seed(0     )
							UpperCAmelCase_						=				CLIPTextConfig(
							    bos_token_id=0    ,	eos_token_id=2    ,	hidden_size=32    ,	intermediate_size=37    ,	layer_norm_eps=1e-0_5    ,	num_attention_heads=4    ,	num_hidden_layers=5    ,	pad_token_id=1    ,	vocab_size=1000    ,	hidden_act="gelu"    ,	projection_dim=512    ,	)
							UpperCAmelCase_						=				CLIPTextModel(lowerCAmelCase     )
							UpperCAmelCase_						=				CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip"     )

							UpperCAmelCase_						=				{
							    "unet": unet,
							    "scheduler": scheduler,
							    "vae": vae,
							    "text_encoder": text_encoder,
							    "tokenizer": tokenizer,
							}
							return components



					def    A__		(			self    ,	lowerCAmelCase    ,	lowerCAmelCase=0     ):
							if str(lowerCAmelCase     ).startswith("mps"     ):
									UpperCAmelCase_						=				torch.manual_seed(lowerCAmelCase     )
							else:
									UpperCAmelCase_						=				torch.Generator(device=lowerCAmelCase     ).manual_seed(lowerCAmelCase     )
							UpperCAmelCase_						=				{
							    "prompt": "A painting of a squirrel eating a burger",
							    "generator": generator,
							    "num_inference_steps": 2,
							    "guidance_scale": 6.0,
							    "output_type": "pt",
							}
							return inputs



					def    A__		(			self     ):
							UpperCAmelCase_						=				"cpu"  # ensure determinism for the device-dependent torch.Generator
							UpperCAmelCase_						=				self.get_dummy_components()
							UpperCAmelCase_						=				TextToVideoSDPipeline(**lowerCAmelCase     )
							UpperCAmelCase_						=				sd_pipe.to(lowerCAmelCase     )
							sd_pipe.set_progress_bar_config(disable=lowerCAmelCase     )

							UpperCAmelCase_						=				self.get_dummy_inputs(lowerCAmelCase     )
							UpperCAmelCase_						=				"np"
							UpperCAmelCase_						=				sd_pipe(**lowerCAmelCase     ).frames
							UpperCAmelCase_						=				frames[0][-3:, -3:, -1]

							assert frames[0].shape == (64, 64, 3)
							UpperCAmelCase_						=				np.array([158.0, 160.0, 153.0, 125.0, 100.0, 121.0, 111.0, 93.0, 113.0]     )

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



					def    A__		(			self     ):
							self._test_attention_slicing_forward_pass(test_mean_pixel_difference=lowerCAmelCase    ,	expected_max_diff=3e-3     )



					@unittest.skipIf(
					    torch_device != "cuda" or not is_xformers_available()    ,	reason="XFormers attention is only available with CUDA and `xformers` installed"    ,	)
					def    A__		(			self     ):
							self._test_xformers_attention_forwardGenerator_pass(test_mean_pixel_difference=lowerCAmelCase    ,	expected_max_diff=1e-2     )



					@unittest.skip(reason="Batching needs to be properly figured out first for this pipeline."     )
					def    A__		(			self     ):
							pass



					@unittest.skip(reason="Batching needs to be properly figured out first for this pipeline."     )
					def    A__		(			self     ):
							pass



					@unittest.skip(reason="`num_images_per_prompt` argument is not supported for this pipeline."     )
					def    A__		(			self     ):
							pass



					def    A__		(			self     ):
							return super().test_progress_bar()





@slow
@skip_mps
class 	lowerCamelCase      (						unittest.TestCase  ):







					'''simple docstring'''







					def    A__		(			self     ):
							UpperCAmelCase_						=				load_numpy(
							    "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/text_to_video/video.npy"     )

							UpperCAmelCase_						=				TextToVideoSDPipeline.from_pretrained("damo-vilab/text-to-video-ms-1.7b"     )
							UpperCAmelCase_						=				DPMSolverMultistepScheduler.from_config(pipe.scheduler.config     )
							UpperCAmelCase_						=				pipe.to("cuda"     )

							UpperCAmelCase_						=				"Spiderman is surfing"
							UpperCAmelCase_						=				torch.Generator(device="cpu"     ).manual_seed(0     )

							UpperCAmelCase_						=				pipe(lowerCAmelCase    ,	generator=lowerCAmelCase    ,	num_inference_steps=25    ,	output_type="pt"     ).frames
							UpperCAmelCase_						=				video_frames.cpu().numpy()

							assert np.abs(expected_video - video     ).mean() < 5e-2



					def    A__		(			self     ):
							UpperCAmelCase_						=				load_numpy(
							    "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/text_to_video/video_2step.npy"     )

							UpperCAmelCase_						=				TextToVideoSDPipeline.from_pretrained("damo-vilab/text-to-video-ms-1.7b"     )
							UpperCAmelCase_						=				pipe.to("cuda"     )

							UpperCAmelCase_						=				"Spiderman is surfing"
							UpperCAmelCase_						=				torch.Generator(device="cpu"     ).manual_seed(0     )

							UpperCAmelCase_						=				pipe(lowerCAmelCase    ,	generator=lowerCAmelCase    ,	num_inference_steps=2    ,	output_type="pt"     ).frames
							UpperCAmelCase_						=				video_frames.cpu().numpy()

							assert np.abs(expected_video - video     ).mean() < 5e-2

