




import argparse
import json

import gdown
import numpy as np
import torch
from huggingface_hub import hf_hub_download

from transformers import (
    VideoMAEConfig,
    VideoMAEForPreTraining,
    VideoMAEForVideoClassification,
    VideoMAEImageProcessor,
)



def 				_A  (		lowerCAmelCase_	:   Any     ):
     """simple docstring"""

     lowerCAmelCase__            =					VideoMAEConfig()

     set_architecture_configs(lowerCAmelCase_  ,							lowerCAmelCase_     )

     if "finetuned" not in model_name:
          lowerCAmelCase__            =					False

     if "finetuned" in model_name:
          lowerCAmelCase__            =					"huggingface/label-files"
          if "kinetics" in model_name:
               lowerCAmelCase__            =					400
               lowerCAmelCase__            =					"kinetics400-id2label.json"
          elif "ssv2" in model_name:
               lowerCAmelCase__            =					174
               lowerCAmelCase__            =					"something-something-v2-id2label.json"
          else:
               raise ValueError("Model name should either contain 'kinetics' or 'ssv2' in case it's fine-tuned."     )
          lowerCAmelCase__            =					json.load(open(hf_hub_download(lowerCAmelCase_  ,							lowerCAmelCase_  ,							repo_type="dataset"     )  ,							"r"     )     )
          lowerCAmelCase__            =					{int(lowerCAmelCase_     ): v for k, v in idalabel.items()}
          lowerCAmelCase__            =					idalabel
          lowerCAmelCase__            =					{v: k for k, v in idalabel.items()}

     return config



def 				_A  (		lowerCAmelCase_	:   str  ,							lowerCAmelCase_	:   int     ):
     """simple docstring"""

     if "small" in model_name:
          lowerCAmelCase__            =					384
          lowerCAmelCase__            =					1536
          lowerCAmelCase__            =					12
          lowerCAmelCase__            =					16
          lowerCAmelCase__            =					12
          lowerCAmelCase__            =					3
          lowerCAmelCase__            =					192
          lowerCAmelCase__            =					768
     elif "large" in model_name:
          lowerCAmelCase__            =					1024
          lowerCAmelCase__            =					4096
          lowerCAmelCase__            =					24
          lowerCAmelCase__            =					16
          lowerCAmelCase__            =					12
          lowerCAmelCase__            =					8
          lowerCAmelCase__            =					512
          lowerCAmelCase__            =					2048
     elif "huge" in model_name:
          lowerCAmelCase__            =					1280
          lowerCAmelCase__            =					5120
          lowerCAmelCase__            =					32
          lowerCAmelCase__            =					16
          lowerCAmelCase__            =					12
          lowerCAmelCase__            =					8
          lowerCAmelCase__            =					640
          lowerCAmelCase__            =					2560
     elif "base" not in model_name:
          raise ValueError("Model name should include either \"small\", \"base\", \"large\", or \"huge\""     )



def 				_A  (		lowerCAmelCase_	:   Union[str, Any]     ):
     """simple docstring"""

     if "encoder." in name:
          lowerCAmelCase__            =					name.replace("encoder."  ,							""     )
     if "cls_token" in name:
          lowerCAmelCase__            =					name.replace("cls_token"  ,							"videomae.embeddings.cls_token"     )
     if "decoder_pos_embed" in name:
          lowerCAmelCase__            =					name.replace("decoder_pos_embed"  ,							"decoder.decoder_pos_embed"     )
     if "pos_embed" in name and "decoder" not in name:
          lowerCAmelCase__            =					name.replace("pos_embed"  ,							"videomae.embeddings.position_embeddings"     )
     if "patch_embed.proj" in name:
          lowerCAmelCase__            =					name.replace("patch_embed.proj"  ,							"videomae.embeddings.patch_embeddings.projection"     )
     if "patch_embed.norm" in name:
          lowerCAmelCase__            =					name.replace("patch_embed.norm"  ,							"videomae.embeddings.norm"     )
     if "decoder.blocks" in name:
          lowerCAmelCase__            =					name.replace("decoder.blocks"  ,							"decoder.decoder_layers"     )
     if "blocks" in name:
          lowerCAmelCase__            =					name.replace("blocks"  ,							"videomae.encoder.layer"     )
     if "attn.proj" in name:
          lowerCAmelCase__            =					name.replace("attn.proj"  ,							"attention.output.dense"     )
     if "attn" in name and "bias" not in name:
          lowerCAmelCase__            =					name.replace("attn"  ,							"attention.self"     )
     if "attn" in name:
          lowerCAmelCase__            =					name.replace("attn"  ,							"attention.attention"     )
     if "norm1" in name:
          lowerCAmelCase__            =					name.replace("norm1"  ,							"layernorm_before"     )
     if "norm2" in name:
          lowerCAmelCase__            =					name.replace("norm2"  ,							"layernorm_after"     )
     if "mlp.fc1" in name:
          lowerCAmelCase__            =					name.replace("mlp.fc1"  ,							"intermediate.dense"     )
     if "mlp.fc2" in name:
          lowerCAmelCase__            =					name.replace("mlp.fc2"  ,							"output.dense"     )
     if "decoder_embed" in name:
          lowerCAmelCase__            =					name.replace("decoder_embed"  ,							"decoder.decoder_embed"     )
     if "decoder_norm" in name:
          lowerCAmelCase__            =					name.replace("decoder_norm"  ,							"decoder.decoder_norm"     )
     if "decoder_pred" in name:
          lowerCAmelCase__            =					name.replace("decoder_pred"  ,							"decoder.decoder_pred"     )
     if "norm.weight" in name and "decoder" not in name and "fc" not in name:
          lowerCAmelCase__            =					name.replace("norm.weight"  ,							"videomae.layernorm.weight"     )
     if "norm.bias" in name and "decoder" not in name and "fc" not in name:
          lowerCAmelCase__            =					name.replace("norm.bias"  ,							"videomae.layernorm.bias"     )
     if "head" in name and "decoder" not in name:
          lowerCAmelCase__            =					name.replace("head"  ,							"classifier"     )

     return name



def 				_A  (		lowerCAmelCase_	:   Dict  ,							lowerCAmelCase_	:   Dict     ):
     """simple docstring"""

     for key in orig_state_dict.copy().keys():
          lowerCAmelCase__            =					orig_state_dict.pop(lowerCAmelCase_     )

          if key.startswith("encoder."     ):
               lowerCAmelCase__            =					key.replace("encoder."  ,							""     )

          if "qkv" in key:
               lowerCAmelCase__            =					key.split("."     )
               if key.startswith("decoder.blocks"     ):
                    lowerCAmelCase__            =					config.decoder_hidden_size
                    lowerCAmelCase__            =					int(key_split[2]     )
                    lowerCAmelCase__            =					"decoder.decoder_layers."
                    if "weight" in key:
                         lowerCAmelCase__            =					val[:dim, :]
                         lowerCAmelCase__            =					val[dim : dim * 2, :]
                         lowerCAmelCase__            =					val[-dim:, :]
               else:
                    lowerCAmelCase__            =					config.hidden_size
                    lowerCAmelCase__            =					int(key_split[1]     )
                    lowerCAmelCase__            =					"videomae.encoder.layer."
                    if "weight" in key:
                         lowerCAmelCase__            =					val[:dim, :]
                         lowerCAmelCase__            =					val[dim : dim * 2, :]
                         lowerCAmelCase__            =					val[-dim:, :]
          else:
               lowerCAmelCase__            =					val

     return orig_state_dict



def 				_A  (		):
     """simple docstring"""

     lowerCAmelCase__            =					hf_hub_download(
         repo_id="hf-internal-testing/spaghetti-video"  ,							filename="eating_spaghetti.npy"  ,							repo_type="dataset"     )
     lowerCAmelCase__            =					np.load(lowerCAmelCase_     )
     return list(lowerCAmelCase_     )



def 				_A  (		lowerCAmelCase_	:   List[str]  ,							lowerCAmelCase_	:   str  ,							lowerCAmelCase_	:   Dict  ,							lowerCAmelCase_	:   str     ):
     """simple docstring"""

     lowerCAmelCase__            =					get_videomae_config(lowerCAmelCase_     )

     if "finetuned" in model_name:
          lowerCAmelCase__            =					VideoMAEForVideoClassification(lowerCAmelCase_     )
     else:
          lowerCAmelCase__            =					VideoMAEForPreTraining(lowerCAmelCase_     )

     # download original checkpoint, hosted on Google Drive
     lowerCAmelCase__            =					"pytorch_model.bin"
     gdown.cached_download(lowerCAmelCase_  ,							lowerCAmelCase_  ,							quiet=lowerCAmelCase_     )
     lowerCAmelCase__            =					torch.load(lowerCAmelCase_  ,							map_location="cpu"     )
     if "model" in files:
          lowerCAmelCase__            =					files["model"]
     else:
          lowerCAmelCase__            =					files["module"]
     lowerCAmelCase__            =					convert_state_dict(lowerCAmelCase_  ,							lowerCAmelCase_     )

     model.load_state_dict(lowerCAmelCase_     )
     model.eval()

     # verify model on basic input
     lowerCAmelCase__            =					VideoMAEImageProcessor(image_mean=[0.5, 0.5, 0.5]  ,							image_std=[0.5, 0.5, 0.5]     )
     lowerCAmelCase__            =					prepare_video()
     lowerCAmelCase__            =					image_processor(lowerCAmelCase_  ,							return_tensors="pt"     )

     if "finetuned" not in model_name:
          lowerCAmelCase__            =					hf_hub_download(repo_id="hf-internal-testing/bool-masked-pos"  ,							filename="bool_masked_pos.pt"     )
          lowerCAmelCase__            =					torch.load(lowerCAmelCase_     )

     lowerCAmelCase__            =					model(**lowerCAmelCase_     )
     lowerCAmelCase__            =					outputs.logits

     lowerCAmelCase__            =					[
         "videomae-small-finetuned-kinetics",
         "videomae-small-finetuned-ssv2",
         # Kinetics-400 checkpoints (short = pretrained only for 800 epochs instead of 1600)
         "videomae-base-short",
         "videomae-base-short-finetuned-kinetics",
         "videomae-base",
         "videomae-base-finetuned-kinetics",
         "videomae-large",
         "videomae-large-finetuned-kinetics",
         "videomae-huge-finetuned-kinetics",
         # Something-Something-v2 checkpoints (short = pretrained only for 800 epochs instead of 2400)
         "videomae-base-short-ssv2",
         "videomae-base-short-finetuned-ssv2",
         "videomae-base-ssv2",
         "videomae-base-finetuned-ssv2",
     ]

     # NOTE: logits were tested with image_mean and image_std equal to [0.5, 0.5, 0.5] and [0.5, 0.5, 0.5]
     if model_name == "videomae-small-finetuned-kinetics":
          lowerCAmelCase__            =					torch.Size([1, 400]     )
          lowerCAmelCase__            =					torch.tensor([-0.9291, -0.4061, -0.9307]     )
     elif model_name == "videomae-small-finetuned-ssv2":
          lowerCAmelCase__            =					torch.Size([1, 174]     )
          lowerCAmelCase__            =					torch.tensor([0.2671, -0.4689, -0.8235]     )
     elif model_name == "videomae-base":
          lowerCAmelCase__            =					torch.Size([1, 1408, 1536]     )
          lowerCAmelCase__            =					torch.tensor([[0.7739, 0.7968, 0.7089], [0.6701, 0.7487, 0.6209], [0.4287, 0.5158, 0.4773]]     )
     elif model_name == "videomae-base-short":
          lowerCAmelCase__            =					torch.Size([1, 1408, 1536]     )
          lowerCAmelCase__            =					torch.tensor([[0.7994, 0.9612, 0.8508], [0.7401, 0.8958, 0.8302], [0.5862, 0.7468, 0.7325]]     )
          # we verified the loss both for normalized and unnormalized targets for this one
          lowerCAmelCase__            =					torch.tensor([0.5142]     ) if config.norm_pix_loss else torch.tensor([0.6469]     )
     elif model_name == "videomae-large":
          lowerCAmelCase__            =					torch.Size([1, 1408, 1536]     )
          lowerCAmelCase__            =					torch.tensor([[0.7149, 0.7997, 0.6966], [0.6768, 0.7869, 0.6948], [0.5139, 0.6221, 0.5605]]     )
     elif model_name == "videomae-large-finetuned-kinetics":
          lowerCAmelCase__            =					torch.Size([1, 400]     )
          lowerCAmelCase__            =					torch.tensor([0.0771, 0.0011, -0.3625]     )
     elif model_name == "videomae-huge-finetuned-kinetics":
          lowerCAmelCase__            =					torch.Size([1, 400]     )
          lowerCAmelCase__            =					torch.tensor([0.2433, 0.1632, -0.4894]     )
     elif model_name == "videomae-base-short-finetuned-kinetics":
          lowerCAmelCase__            =					torch.Size([1, 400]     )
          lowerCAmelCase__            =					torch.tensor([0.6588, 0.0990, -0.2493]     )
     elif model_name == "videomae-base-finetuned-kinetics":
          lowerCAmelCase__            =					torch.Size([1, 400]     )
          lowerCAmelCase__            =					torch.tensor([0.3669, -0.0688, -0.2421]     )
     elif model_name == "videomae-base-short-ssv2":
          lowerCAmelCase__            =					torch.Size([1, 1408, 1536]     )
          lowerCAmelCase__            =					torch.tensor([[0.4712, 0.5296, 0.5786], [0.2278, 0.2729, 0.4026], [0.0352, 0.0730, 0.2506]]     )
     elif model_name == "videomae-base-short-finetuned-ssv2":
          lowerCAmelCase__            =					torch.Size([1, 174]     )
          lowerCAmelCase__            =					torch.tensor([-0.0537, -0.1539, -0.3266]     )
     elif model_name == "videomae-base-ssv2":
          lowerCAmelCase__            =					torch.Size([1, 1408, 1536]     )
          lowerCAmelCase__            =					torch.tensor([[0.8131, 0.8727, 0.8546], [0.7366, 0.9377, 0.8870], [0.5935, 0.8874, 0.8564]]     )
     elif model_name == "videomae-base-finetuned-ssv2":
          lowerCAmelCase__            =					torch.Size([1, 174]     )
          lowerCAmelCase__            =					torch.tensor([0.1961, -0.8337, -0.6389]     )
     else:
          raise ValueError(F'Model name not supported. Should be one of {model_names}'     )

     # verify logits
     assert logits.shape == expected_shape
     if "finetuned" in model_name:
          assert torch.allclose(logits[0, :3]  ,							lowerCAmelCase_  ,							atol=1E-4     )
     else:
          print("Logits:"  ,							logits[0, :3, :3]     )
          assert torch.allclose(logits[0, :3, :3]  ,							lowerCAmelCase_  ,							atol=1E-4     )
     print("Logits ok!"     )

     # verify loss, if applicable
     if model_name == "videomae-base-short":
          lowerCAmelCase__            =					outputs.loss
          assert torch.allclose(lowerCAmelCase_  ,							lowerCAmelCase_  ,							atol=1E-4     )
          print("Loss ok!"     )

     if pytorch_dump_folder_path is not None:
          print(F'Saving model and image processor to {pytorch_dump_folder_path}'     )
          image_processor.save_pretrained(lowerCAmelCase_     )
          model.save_pretrained(lowerCAmelCase_     )

     if push_to_hub:
          print("Pushing to the hub..."     )
          model.push_to_hub(lowerCAmelCase_  ,							organization="nielsr"     )


if __name__ == "__main__":
     UpperCamelCase								=	argparse.ArgumentParser()
     # Required parameters
     parser.add_argument(
         '--checkpoint_url',
         default='https://drive.google.com/u/1/uc?id=1tEhLyskjb755TJ65ptsrafUG2llSwQE1&amp;export=download&amp;confirm=t&amp;uuid=aa3276eb-fb7e-482a-adec-dc7171df14c4',
         type=str,
         help=(
             'URL of the original PyTorch checkpoint (on Google Drive) you\'d like to convert. Should be a direct'
             ' download link.'
         ),
     )
     parser.add_argument(
         '--pytorch_dump_folder_path',
         default='/Users/nielsrogge/Documents/VideoMAE/Test',
         type=str,
         help='Path to the output PyTorch model directory.',
     )
     parser.add_argument('--model_name', default='videomae-base', type=str, help='Name of the model.')
     parser.add_argument(
         '--push_to_hub', action='store_true', help='Whether or not to push the converted model to the 🤗 hub.'
     )

     UpperCamelCase								=	parser.parse_args()
     convert_videomae_checkpoint(args.checkpoint_url, args.pytorch_dump_folder_path, args.model_name, args.push_to_hub)






import gc
import random
import unittest

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

import diffusers
from diffusers import (
    AutoencoderKL,
    EulerDiscreteScheduler,
    StableDiffusionLatentUpscalePipeline,
    StableDiffusionPipeline,
    UNetaDConditionModel,
)
from diffusers.schedulers import KarrasDiffusionSchedulers
from diffusers.utils import floats_tensor, load_image, load_numpy, slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu

from ..pipeline_params import TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS, TEXT_GUIDED_IMAGE_VARIATION_PARAMS
from ..test_pipelines_common import PipelineKarrasSchedulerTesterMixin, PipelineLatentTesterMixin, PipelineTesterMixin


enable_full_determinism()





def 						__A						(      __lowerCamelCase       ) ->			Optional[int]:
	a									=				[tensor.shape for tensor in tensor_list]
	return all(shape == shapes[0] for shape in shapes[1:]       )

class 						__lowerCAmelCase      (      __magic_name__ ,			__magic_name__ ,			__magic_name__ ,			unittest.TestCase					):
							UpperCamelCase__   	=    StableDiffusionLatentUpscalePipeline
							UpperCamelCase__   	=    TEXT_GUIDED_IMAGE_VARIATION_PARAMS - {
							    '''height''',
							    '''width''',
							    '''cross_attention_kwargs''',
							    '''negative_prompt_embeds''',
							    '''prompt_embeds''',
							}
							UpperCamelCase__   	=    PipelineTesterMixin.required_optional_params - {'''num_images_per_prompt'''}
							UpperCamelCase__   	=    TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS
							UpperCamelCase__   	=    frozenset(
							    []					)  # TO-DO: update image_params once pipeline is refactored with VaeImageProcessor.preprocess
							UpperCamelCase__   	=    frozenset([]					)

							UpperCamelCase__   	=    True
							@property
							def  lowerCamelCase__				(		self		:List[str]							):







								'''simple docstring'''







								a									=				1
								a									=				4
								a									=				(16, 16)

								a									=				floats_tensor((batch_size, num_channels) + sizes						, rng=random.Random(0							)							).to(__magic_name__							)
								return image
							def  lowerCamelCase__				(		self		:Any							):







								'''simple docstring'''







								torch.manual_seed(0							)
								a									=				UNetaDConditionModel(
								    act_fn="""gelu"""						, attention_head_dim=8						, norm_num_groups=__magic_name__						, block_out_channels=[32, 32, 64, 64]						, time_cond_proj_dim=160						, conv_in_kernel=1						, conv_out_kernel=1						, cross_attention_dim=32						, down_block_types=(
								        """KDownBlock2D""",
								        """KCrossAttnDownBlock2D""",
								        """KCrossAttnDownBlock2D""",
								        """KCrossAttnDownBlock2D""",
								    )						, in_channels=8						, mid_block_type=__magic_name__						, only_cross_attention=__magic_name__						, out_channels=5						, resnet_time_scale_shift="""scale_shift"""						, time_embedding_type="""fourier"""						, timestep_post_act="""gelu"""						, up_block_types=("""KCrossAttnUpBlock2D""", """KCrossAttnUpBlock2D""", """KCrossAttnUpBlock2D""", """KUpBlock2D""")						, )
								a									=				AutoencoderKL(
								    block_out_channels=[32, 32, 64, 64]						, in_channels=3						, out_channels=3						, down_block_types=[
								        """DownEncoderBlock2D""",
								        """DownEncoderBlock2D""",
								        """DownEncoderBlock2D""",
								        """DownEncoderBlock2D""",
								    ]						, up_block_types=["""UpDecoderBlock2D""", """UpDecoderBlock2D""", """UpDecoderBlock2D""", """UpDecoderBlock2D"""]						, latent_channels=4						, )
								a									=				EulerDiscreteScheduler(prediction_type="""sample"""							)
								a									=				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="""quick_gelu"""						, projection_dim=512						, )
								a									=				CLIPTextModel(__magic_name__							)
								a									=				CLIPTokenizer.from_pretrained("""hf-internal-testing/tiny-random-clip"""							)

								a									=				{
								    """unet""": model.eval(),
								    """vae""": vae.eval(),
								    """scheduler""": scheduler,
								    """text_encoder""": text_encoder,
								    """tokenizer""": tokenizer,
								}

								return components
							def  lowerCamelCase__				(		self		:Optional[int]						, __magic_name__		:Tuple						, __magic_name__		:Optional[int]=0							):







								'''simple docstring'''







								if str(__magic_name__							).startswith("""mps"""							):
									a									=				torch.manual_seed(__magic_name__							)
								else:
									a									=				torch.Generator(device=__magic_name__							).manual_seed(__magic_name__							)
								a									=				{
								    """prompt""": """A painting of a squirrel eating a burger""",
								    """image""": self.dummy_image.cpu(),
								    """generator""": generator,
								    """num_inference_steps""": 2,
								    """output_type""": """numpy""",
								}
								return inputs
							def  lowerCamelCase__				(		self		:Optional[int]							):







								'''simple docstring'''







								a									=				"""cpu"""

								a									=				self.get_dummy_components()
								a									=				self.pipeline_class(**__magic_name__							)
								pipe.to(__magic_name__							)
								pipe.set_progress_bar_config(disable=__magic_name__							)

								a									=				self.get_dummy_inputs(__magic_name__							)
								a									=				pipe(**__magic_name__							).images
								a									=				image[0, -3:, -3:, -1]

								self.assertEqual(image.shape						, (1, 256, 256, 3)							)
								a									=				np.array(
								    [0.47222412, 0.41921633, 0.44717434, 0.46874192, 0.42588258, 0.46150726, 0.4677534, 0.45583832, 0.48579055]							)
								a									=				np.abs(image_slice.flatten() - expected_slice							).max()
								self.assertLessEqual(__magic_name__						, 1E-3							)
							def  lowerCamelCase__				(		self		:Any							):







								'''simple docstring'''







								super().test_attention_slicing_forward_pass(expected_max_diff=7E-3							)
							def  lowerCamelCase__				(		self		:Optional[int]							):







								'''simple docstring'''







								super().test_cpu_offload_forward_pass(expected_max_diff=3E-3							)
							def  lowerCamelCase__				(		self		:Dict							):







								'''simple docstring'''







								super().test_dict_tuple_outputs_equivalent(expected_max_difference=3E-3							)
							def  lowerCamelCase__				(		self		:List[Any]							):







								'''simple docstring'''







								super().test_inference_batch_single_identical(expected_max_diff=7E-3							)
							def  lowerCamelCase__				(		self		:Optional[int]							):







								'''simple docstring'''







								super().test_pt_np_pil_outputs_equivalent(expected_max_diff=3E-3							)
							def  lowerCamelCase__				(		self		:str							):







								'''simple docstring'''







								super().test_save_load_local(expected_max_difference=3E-3							)
							def  lowerCamelCase__				(		self		:Tuple							):







								'''simple docstring'''







								super().test_save_load_optional_components(expected_max_difference=3E-3							)





							def  lowerCamelCase__				(		self		:List[str]							):







								'''simple docstring'''







								a									=				[
								    """DDIMScheduler""",
								    """DDPMScheduler""",
								    """PNDMScheduler""",
								    """HeunDiscreteScheduler""",
								    """EulerAncestralDiscreteScheduler""",
								    """KDPM2DiscreteScheduler""",
								    """KDPM2AncestralDiscreteScheduler""",
								    """DPMSolverSDEScheduler""",
								]
								a									=				self.get_dummy_components()
								a									=				self.pipeline_class(**__magic_name__							)

								# make sure that PNDM does not need warm-up
								pipe.scheduler.register_to_config(skip_prk_steps=__magic_name__							)

								pipe.to(__magic_name__							)
								pipe.set_progress_bar_config(disable=__magic_name__							)
								a									=				self.get_dummy_inputs(__magic_name__							)
								a									=				2

								a									=				[]
								for scheduler_enum in KarrasDiffusionSchedulers:
									if scheduler_enum.name in skip_schedulers:
										# no sigma schedulers are not supported
										# no schedulers
										continue

									a									=				getattr(__magic_name__						, scheduler_enum.name							)
									a									=				scheduler_cls.from_config(pipe.scheduler.config							)
									a									=				pipe(**__magic_name__							)[0]
									outputs.append(__magic_name__							)

								assert check_same_shape(__magic_name__							)







@require_torch_gpu
@slow
class 						__lowerCAmelCase      (      unittest.TestCase					):
							def  lowerCamelCase__				(		self		:List[str]							):







								'''simple docstring'''







								super().tearDown()
								gc.collect()
								torch.cuda.empty_cache()
							def  lowerCamelCase__				(		self		:List[Any]							):







								'''simple docstring'''







								a									=				torch.manual_seed(33							)

								a									=				StableDiffusionPipeline.from_pretrained("""CompVis/stable-diffusion-v1-4"""						, torch_dtype=torch.floataa							)
								pipe.to("""cuda"""							)

								a									=				StableDiffusionLatentUpscalePipeline.from_pretrained(
								    """stabilityai/sd-x2-latent-upscaler"""						, torch_dtype=torch.floataa							)
								upscaler.to("""cuda"""							)

								a									=				"""a photo of an astronaut high resolution, unreal engine, ultra realistic"""

								a									=				pipe(__magic_name__						, generator=__magic_name__						, output_type="""latent"""							).images

								a									=				upscaler(
								    prompt=__magic_name__						, image=__magic_name__						, num_inference_steps=20						, guidance_scale=0						, generator=__magic_name__						, output_type="""np"""						, ).images[0]

								a									=				load_numpy(
								    """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/latent-upscaler/astronaut_1024.npy"""							)
								assert np.abs((expected_image - image).mean()							) < 5E-2





							def  lowerCamelCase__				(		self		:Optional[Any]							):







								'''simple docstring'''







								a									=				torch.manual_seed(33							)

								a									=				StableDiffusionLatentUpscalePipeline.from_pretrained(
								    """stabilityai/sd-x2-latent-upscaler"""						, torch_dtype=torch.floataa							)
								upscaler.to("""cuda"""							)

								a									=				"""the temple of fire by Ross Tran and Gerardo Dottori, oil on canvas"""

								a									=				load_image(
								    """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/latent-upscaler/fire_temple_512.png"""							)

								a									=				upscaler(
								    prompt=__magic_name__						, image=__magic_name__						, num_inference_steps=20						, guidance_scale=0						, generator=__magic_name__						, output_type="""np"""						, ).images[0]

								a									=				load_numpy(
								    """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/latent-upscaler/fire_temple_1024.npy"""							)
								assert np.abs((expected_image - image).max()							) < 5E-2

