




import unittest

import numpy as np

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin


if is_torch_available():
				import torch

if is_vision_available():
				from PIL import Image

				from transformers import TvltImageProcessor


def 							__SCREAMING_SNAKE_CASE						(    snake_case_			,					snake_case_=10			,					snake_case_=10			,					snake_case_=False			,					snake_case_=False		):

						'''simple docstring'''


						_UpperCAmelCase									=  []
						for i in range(image_processor_tester.num_frames		):
												video.append(np.random.randint(255			,					size=(image_processor_tester.num_channels, width, height)			,					dtype=np.uinta		)		)

						if not numpify and not torchify:
												# PIL expects the channel dimension as last dimension
												_UpperCAmelCase									=  [Image.fromarray(np.moveaxis(snake_case_			,					0			,					-1		)		) for frame in video]

						if torchify:
												_UpperCAmelCase									=  [torch.from_numpy(snake_case_		) for frame in video]

						return video


def 							__SCREAMING_SNAKE_CASE						(    snake_case_			,					snake_case_=False			,					snake_case_=False			,					snake_case_=False		):

						'''simple docstring'''


						assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time"

						_UpperCAmelCase									=  []
						for i in range(image_processor_tester.batch_size		):
												if equal_resolution:
																		_UpperCAmelCase									=  _UpperCAmelCase									=  image_processor_tester.max_resolution
												else:
																		_UpperCAmelCase							,				_UpperCAmelCase									=  np.random.choice(
																		    np.arange(image_processor_tester.min_resolution			,					image_processor_tester.max_resolution		)			,					2		)
																		_UpperCAmelCase									=  prepare_video(
																		    image_processor_tester=snake_case_			,					width=snake_case_			,					height=snake_case_			,					numpify=snake_case_			,					torchify=snake_case_			,					)
												video_inputs.append(snake_case_		)

						return video_inputs






class 					__lowerCAmelCase				( unittest.TestCase				):







			def __init__( self					:    List[Any]					,					snake_case__					:    List[Any]					,					snake_case__					:    Optional[Any]=7					,					snake_case__					:    str=3					,					snake_case__					:    Optional[Any]=4					,					snake_case__					:    Optional[int]=18					,					snake_case__					:    Tuple=30					,					snake_case__					:    Optional[Any]=400					,					snake_case__					:    List[str]=True					,					snake_case__					:    Dict=None					,					snake_case__					:    Union[str, Any]=True					,					snake_case__					:    List[str]=[0.5, 0.5, 0.5]					,					snake_case__					:    Tuple=[0.5, 0.5, 0.5]					,					snake_case__					:    Dict=True					,					snake_case__					:    int=None					,					):




									"""simple docstring"""


									_UpperCAmelCase									=  size if size is not None else {"shortest_edge": 18}
									_UpperCAmelCase									=  crop_size if crop_size is not None else {"height": 18, "width": 18}

									_UpperCAmelCase									=  parent
									_UpperCAmelCase									=  batch_size
									_UpperCAmelCase									=  num_channels
									_UpperCAmelCase									=  num_frames
									_UpperCAmelCase									=  image_size
									_UpperCAmelCase									=  min_resolution
									_UpperCAmelCase									=  max_resolution
									_UpperCAmelCase									=  do_resize
									_UpperCAmelCase									=  size
									_UpperCAmelCase									=  do_normalize
									_UpperCAmelCase									=  image_mean
									_UpperCAmelCase									=  image_std
									_UpperCAmelCase									=  do_center_crop
									_UpperCAmelCase									=  crop_size







			def UpperCamelCase    ( self					:    int   ):




									"""simple docstring"""


									return {
									    "image_mean": self.image_mean,
									    "image_std": self.image_std,
									    "do_normalize": self.do_normalize,
									    "do_resize": self.do_resize,
									    "size": self.size,
									    "do_center_crop": self.do_center_crop,
									    "crop_size": self.crop_size,
									}






@require_torch
@require_vision
class 					__lowerCAmelCase				( UpperCAmelCase__      ,	unittest.TestCase				):
			snake_case_   :				Union[str, Any]				  =  TvltImageProcessor if is_vision_available() else None







			def UpperCamelCase    ( self					:    Union[str, Any]   ):




									"""simple docstring"""


									_UpperCAmelCase									=  TvltImageProcessorTester(self   )







			@property
			def UpperCamelCase    ( self					:    Dict   ):




									"""simple docstring"""


									return self.image_processor_tester.prepare_image_processor_dict()







			def UpperCamelCase    ( self					:    List[str]   ):




									"""simple docstring"""


									_UpperCAmelCase									=  self.image_processing_class(**self.image_processor_dict   )
									self.assertTrue(hasattr(snake_case__					,					"image_mean"   )   )
									self.assertTrue(hasattr(snake_case__					,					"image_std"   )   )
									self.assertTrue(hasattr(snake_case__					,					"do_normalize"   )   )
									self.assertTrue(hasattr(snake_case__					,					"do_resize"   )   )
									self.assertTrue(hasattr(snake_case__					,					"do_center_crop"   )   )
									self.assertTrue(hasattr(snake_case__					,					"size"   )   )







			def UpperCamelCase    ( self					:    Union[str, Any]   ):




									"""simple docstring"""


									_UpperCAmelCase									=  self.image_processing_class(**self.image_processor_dict   )
									# create random PIL videos
									_UpperCAmelCase									=  prepare_video_inputs(self.image_processor_tester					,					equal_resolution=snake_case__   )
									for video in video_inputs:
															self.assertIsInstance(snake_case__					,					snake_case__   )
															self.assertIsInstance(video[0]					,					Image.Image   )

									# Test not batched input
									_UpperCAmelCase									=  image_processor(video_inputs[0]					,					return_tensors="pt"   ).pixel_values
									self.assertEqual(
									    encoded_videos.shape					,					(
									        1,
									        self.image_processor_tester.num_frames,
									        self.image_processor_tester.num_channels,
									        self.image_processor_tester.crop_size["height"],
									        self.image_processor_tester.crop_size["width"],
									    )					,					)

									# Test batched
									_UpperCAmelCase									=  image_processor(snake_case__					,					return_tensors="pt"   ).pixel_values
									self.assertEqual(
									    encoded_videos.shape					,					(
									        self.image_processor_tester.batch_size,
									        self.image_processor_tester.num_frames,
									        self.image_processor_tester.num_channels,
									        self.image_processor_tester.crop_size["height"],
									        self.image_processor_tester.crop_size["width"],
									    )					,					)







			def UpperCamelCase    ( self					:    int   ):




									"""simple docstring"""


									_UpperCAmelCase									=  self.image_processing_class(**self.image_processor_dict   )
									# create random numpy tensors
									_UpperCAmelCase									=  prepare_video_inputs(self.image_processor_tester					,					equal_resolution=snake_case__					,					numpify=snake_case__   )
									for video in video_inputs:
															self.assertIsInstance(snake_case__					,					snake_case__   )
															self.assertIsInstance(video[0]					,					np.ndarray   )

									# Test not batched input
									_UpperCAmelCase									=  image_processor(video_inputs[0]					,					return_tensors="pt"   ).pixel_values
									self.assertEqual(
									    encoded_videos.shape					,					(
									        1,
									        self.image_processor_tester.num_frames,
									        self.image_processor_tester.num_channels,
									        self.image_processor_tester.crop_size["height"],
									        self.image_processor_tester.crop_size["width"],
									    )					,					)

									# Test batched
									_UpperCAmelCase									=  image_processor(snake_case__					,					return_tensors="pt"   ).pixel_values
									self.assertEqual(
									    encoded_videos.shape					,					(
									        self.image_processor_tester.batch_size,
									        self.image_processor_tester.num_frames,
									        self.image_processor_tester.num_channels,
									        self.image_processor_tester.crop_size["height"],
									        self.image_processor_tester.crop_size["width"],
									    )					,					)







			def UpperCamelCase    ( self					:    Union[str, Any]   ):




									"""simple docstring"""


									_UpperCAmelCase									=  self.image_processing_class(**self.image_processor_dict   )
									# create random PyTorch tensors
									_UpperCAmelCase									=  prepare_video_inputs(self.image_processor_tester					,					equal_resolution=snake_case__					,					torchify=snake_case__   )
									for video in video_inputs:
															self.assertIsInstance(snake_case__					,					snake_case__   )
															self.assertIsInstance(video[0]					,					torch.Tensor   )

									# Test not batched input
									_UpperCAmelCase									=  image_processor(video_inputs[0]					,					return_tensors="pt"   ).pixel_values
									self.assertEqual(
									    encoded_videos.shape					,					(
									        1,
									        self.image_processor_tester.num_frames,
									        self.image_processor_tester.num_channels,
									        self.image_processor_tester.crop_size["height"],
									        self.image_processor_tester.crop_size["width"],
									    )					,					)

									# Test batched
									_UpperCAmelCase									=  image_processor(snake_case__					,					return_tensors="pt"   ).pixel_values
									self.assertEqual(
									    encoded_videos.shape					,					(
									        self.image_processor_tester.batch_size,
									        self.image_processor_tester.num_frames,
									        self.image_processor_tester.num_channels,
									        self.image_processor_tester.crop_size["height"],
									        self.image_processor_tester.crop_size["width"],
									    )					,					)










import argparse
import os

import torch
from torchvision.datasets.utils import download_url

from diffusers import AutoencoderKL, DDIMScheduler, DiTPipeline, TransformeraDModel


lowercase_			:       Tuple						       =				{5_12: 'DiT-XL-2-512x512.pt', 2_56: 'DiT-XL-2-256x256.pt'}


def 							__SCREAMING_SNAKE_CASE						(    snake_case_		):

						'''simple docstring'''


						_UpperCAmelCase									=  f"""pretrained_models/{model_name}"""
						if not os.path.isfile(snake_case_		):
												os.makedirs("pretrained_models"			,					exist_ok=snake_case_		)
												_UpperCAmelCase									=  f"""https://dl.fbaipublicfiles.com/DiT/models/{model_name}"""
												download_url(snake_case_			,					"pretrained_models"		)
						_UpperCAmelCase									=  torch.load(snake_case_			,					map_location=lambda snake_case_			,					snake_case_		: storage		)
						return model


def 							__SCREAMING_SNAKE_CASE						(    snake_case_		):

						'''simple docstring'''


						_UpperCAmelCase									=  download_model(pretrained_models[args.image_size]		)

						_UpperCAmelCase									=  state_dict["x_embedder.proj.weight"]
						_UpperCAmelCase									=  state_dict["x_embedder.proj.bias"]
						state_dict.pop("x_embedder.proj.weight"		)
						state_dict.pop("x_embedder.proj.bias"		)

						for depth in range(28		):
												_UpperCAmelCase									=  state_dict[
												    "t_embedder.mlp.0.weight"
												]
												_UpperCAmelCase									=  state_dict[
												    "t_embedder.mlp.0.bias"
												]
												_UpperCAmelCase									=  state_dict[
												    "t_embedder.mlp.2.weight"
												]
												_UpperCAmelCase									=  state_dict[
												    "t_embedder.mlp.2.bias"
												]
												_UpperCAmelCase									=  state_dict[
												    "y_embedder.embedding_table.weight"
												]

												_UpperCAmelCase									=  state_dict[
												    f"""blocks.{depth}.adaLN_modulation.1.weight"""
												]
												_UpperCAmelCase									=  state_dict[
												    f"""blocks.{depth}.adaLN_modulation.1.bias"""
												]

												_UpperCAmelCase							,				_UpperCAmelCase							,				_UpperCAmelCase									=  torch.chunk(state_dict[f"""blocks.{depth}.attn.qkv.weight"""]			,					3			,					dim=0		)
												_UpperCAmelCase							,				_UpperCAmelCase							,				_UpperCAmelCase									=  torch.chunk(state_dict[f"""blocks.{depth}.attn.qkv.bias"""]			,					3			,					dim=0		)

												_UpperCAmelCase									=  q
												_UpperCAmelCase									=  q_bias
												_UpperCAmelCase									=  k
												_UpperCAmelCase									=  k_bias
												_UpperCAmelCase									=  v
												_UpperCAmelCase									=  v_bias

												_UpperCAmelCase									=  state_dict[
												    f"""blocks.{depth}.attn.proj.weight"""
												]
												_UpperCAmelCase									=  state_dict[f"""blocks.{depth}.attn.proj.bias"""]

												_UpperCAmelCase									=  state_dict[f"""blocks.{depth}.mlp.fc1.weight"""]
												_UpperCAmelCase									=  state_dict[f"""blocks.{depth}.mlp.fc1.bias"""]
												_UpperCAmelCase									=  state_dict[f"""blocks.{depth}.mlp.fc2.weight"""]
												_UpperCAmelCase									=  state_dict[f"""blocks.{depth}.mlp.fc2.bias"""]

												state_dict.pop(f"""blocks.{depth}.attn.qkv.weight"""		)
												state_dict.pop(f"""blocks.{depth}.attn.qkv.bias"""		)
												state_dict.pop(f"""blocks.{depth}.attn.proj.weight"""		)
												state_dict.pop(f"""blocks.{depth}.attn.proj.bias"""		)
												state_dict.pop(f"""blocks.{depth}.mlp.fc1.weight"""		)
												state_dict.pop(f"""blocks.{depth}.mlp.fc1.bias"""		)
												state_dict.pop(f"""blocks.{depth}.mlp.fc2.weight"""		)
												state_dict.pop(f"""blocks.{depth}.mlp.fc2.bias"""		)
												state_dict.pop(f"""blocks.{depth}.adaLN_modulation.1.weight"""		)
												state_dict.pop(f"""blocks.{depth}.adaLN_modulation.1.bias"""		)

						state_dict.pop("t_embedder.mlp.0.weight"		)
						state_dict.pop("t_embedder.mlp.0.bias"		)
						state_dict.pop("t_embedder.mlp.2.weight"		)
						state_dict.pop("t_embedder.mlp.2.bias"		)
						state_dict.pop("y_embedder.embedding_table.weight"		)

						_UpperCAmelCase									=  state_dict["final_layer.adaLN_modulation.1.weight"]
						_UpperCAmelCase									=  state_dict["final_layer.adaLN_modulation.1.bias"]
						_UpperCAmelCase									=  state_dict["final_layer.linear.weight"]
						_UpperCAmelCase									=  state_dict["final_layer.linear.bias"]

						state_dict.pop("final_layer.linear.weight"		)
						state_dict.pop("final_layer.linear.bias"		)
						state_dict.pop("final_layer.adaLN_modulation.1.weight"		)
						state_dict.pop("final_layer.adaLN_modulation.1.bias"		)

						# DiT XL/2
						_UpperCAmelCase									=  TransformeraDModel(
						    sample_size=args.image_size // 8			,					num_layers=28			,					attention_head_dim=72			,					in_channels=4			,					out_channels=8			,					patch_size=2			,					attention_bias=snake_case_			,					num_attention_heads=16			,					activation_fn="gelu-approximate"			,					num_embeds_ada_norm=1000			,					norm_type="ada_norm_zero"			,					norm_elementwise_affine=snake_case_			,					)
						transformer.load_state_dict(snake_case_			,					strict=snake_case_		)

						_UpperCAmelCase									=  DDIMScheduler(
						    num_train_timesteps=1000			,					beta_schedule="linear"			,					prediction_type="epsilon"			,					clip_sample=snake_case_			,					)

						_UpperCAmelCase									=  AutoencoderKL.from_pretrained(args.vae_model		)

						_UpperCAmelCase									=  DiTPipeline(transformer=snake_case_			,					vae=snake_case_			,					scheduler=snake_case_		)

						if args.save:
												pipeline.save_pretrained(args.checkpoint_path		)


if __name__ == "__main__":
				lowercase_			:       Union[str, Any]						       =				argparse.ArgumentParser()

				parser.add_argument(
				    '--image_size',
				    default=2_56,
				    type=int,
				    required=False,
				    help='Image size of pretrained model, either 256 or 512.',
				)
				parser.add_argument(
				    '--vae_model',
				    default='stabilityai/sd-vae-ft-ema',
				    type=str,
				    required=False,
				    help='Path to pretrained VAE model, either stabilityai/sd-vae-ft-mse or stabilityai/sd-vae-ft-ema.',
				)
				parser.add_argument(
				    '--save', default=True, type=bool, required=False, help='Whether to save the converted pipeline or not.'
				)
				parser.add_argument(
				    '--checkpoint_path', default=None, type=str, required=True, help='Path to the output pipeline.'
				)

				lowercase_			:       Tuple						       =				parser.parse_args()
				main(args)




