






import gc
import unittest

import numpy as np
import torch

from diffusers import AutoencoderKL, DDIMScheduler, DiTPipeline, DPMSolverMultistepScheduler, TransformeraDModel
from diffusers.utils import is_xformers_available, load_numpy, slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu

from ..pipeline_params import (
    CLASS_CONDITIONED_IMAGE_GENERATION_BATCH_PARAMS,
    CLASS_CONDITIONED_IMAGE_GENERATION_PARAMS,
)
from ..test_pipelines_common import PipelineTesterMixin


enable_full_determinism()

class 				lowerCAmelCase_	(		__snake_case   ,		unittest.TestCase			):
				_UpperCamelCase					:       Any							       = DiTPipeline
				_UpperCamelCase					:       int							       = CLASS_CONDITIONED_IMAGE_GENERATION_PARAMS
				_UpperCamelCase					:       Dict							       = PipelineTesterMixin.required_optional_params - {
				    "latents",
				    "num_images_per_prompt",
				    "callback",
				    "callback_steps",
				}
				_UpperCamelCase					:       Union[str, Any]							       = CLASS_CONDITIONED_IMAGE_GENERATION_BATCH_PARAMS
				_UpperCamelCase					:       Union[str, Any]							       = False


				def 						__a					(					self      ):
											torch.manual_seed(0      )
											_lowercase							:   str 							=   TransformeraDModel(
											    sample_size=1_6 ,						num_layers=2 ,						patch_size=4 ,						attention_head_dim=8 ,						num_attention_heads=2 ,						in_channels=4 ,						out_channels=8 ,						attention_bias=_lowerCAmelCase ,						activation_fn='gelu-approximate' ,						num_embeds_ada_norm=1_0_0_0 ,						norm_type='ada_norm_zero' ,						norm_elementwise_affine=_lowerCAmelCase ,						)
											_lowercase							:   int 							=   AutoencoderKL()
											_lowercase							:   List[str] 							=   DDIMScheduler()
											_lowercase							:   Optional[int] 							=   {'transformer': transformer.eval(), 'vae': vae.eval(), 'scheduler': scheduler}
											return components


				def 						__a					(					self ,						_lowerCAmelCase ,						_lowerCAmelCase=0      ):
											if str(_lowerCAmelCase      ).startswith('mps'      ):
																		_lowercase							:   List[str] 							=   torch.manual_seed(_lowerCAmelCase      )
											else:
																		_lowercase							:   Any 							=   torch.Generator(device=_lowerCAmelCase      ).manual_seed(_lowerCAmelCase      )
											_lowercase							:   Union[str, Any] 							=   {
											    'class_labels': [1],
											    'generator': generator,
											    'num_inference_steps': 2,
											    'output_type': 'numpy',
											}
											return inputs


				def 						__a					(					self      ):
											_lowercase							:   Dict 							=   'cpu'

											_lowercase							:   List[Any] 							=   self.get_dummy_components()
											_lowercase							:   List[str] 							=   self.pipeline_class(**_lowerCAmelCase      )
											pipe.to(_lowerCAmelCase      )
											pipe.set_progress_bar_config(disable=_lowerCAmelCase      )

											_lowercase							:   Union[str, Any] 							=   self.get_dummy_inputs(_lowerCAmelCase      )
											_lowercase							:   Dict 							=   pipe(**_lowerCAmelCase      ).images
											_lowercase							:   List[Any] 							=   image[0, -3:, -3:, -1]

											self.assertEqual(image.shape ,						(1, 1_6, 1_6, 3)      )
											_lowercase							:   Optional[int] 							=   np.array([0.29_46, 0.66_01, 0.43_29, 0.32_96, 0.41_44, 0.53_19, 0.72_73, 0.50_13, 0.44_57]      )
											_lowercase							:   Union[str, Any] 							=   np.abs(image_slice.flatten() - expected_slice      ).max()
											self.assertLessEqual(_lowerCAmelCase ,						1E-3      )


				def 						__a					(					self      ):
											self._test_inference_batch_single_identical(relax_max_difference=_lowerCAmelCase ,						expected_max_diff=1E-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(expected_max_diff=1E-3      )




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


				def 						__a					(					self      ):
											super().tearDown()
											gc.collect()
											torch.cuda.empty_cache()


				def 						__a					(					self      ):
											_lowercase							:   List[Any] 							=   torch.manual_seed(0      )

											_lowercase							:   Optional[Any] 							=   DiTPipeline.from_pretrained('facebook/DiT-XL-2-256'      )
											pipe.to('cuda'      )

											_lowercase							:   List[Any] 							=   ['vase', 'umbrella', 'white shark', 'white wolf']
											_lowercase							:   List[str] 							=   pipe.get_label_ids(_lowerCAmelCase      )

											_lowercase							:   Tuple 							=   pipe(_lowerCAmelCase ,						generator=_lowerCAmelCase ,						num_inference_steps=4_0 ,						output_type='np'      ).images

											for word, image in zip(_lowerCAmelCase ,						_lowerCAmelCase      ):
																		_lowercase							:   Optional[Any] 							=   load_numpy(
																		    F"""https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/dit/{word}.npy"""      )
																		assert np.abs((expected_image - image).max()      ) < 1E-2


				def 						__a					(					self      ):
											_lowercase							:   str 							=   DiTPipeline.from_pretrained('facebook/DiT-XL-2-512'      )
											_lowercase							:   int 							=   DPMSolverMultistepScheduler.from_config(pipe.scheduler.config      )
											pipe.to('cuda'      )

											_lowercase							:   Optional[Any] 							=   ['vase', 'umbrella']
											_lowercase							:   List[Any] 							=   pipe.get_label_ids(_lowerCAmelCase      )

											_lowercase							:   List[str] 							=   torch.manual_seed(0      )
											_lowercase							:   List[Any] 							=   pipe(_lowerCAmelCase ,						generator=_lowerCAmelCase ,						num_inference_steps=2_5 ,						output_type='np'      ).images

											for word, image in zip(_lowerCAmelCase ,						_lowerCAmelCase      ):
																		_lowercase							:   Union[str, Any] 							=   load_numpy(
																		    'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'
																		    F"""/dit/{word}_512.npy"""      )

																		assert np.abs((expected_image - image).max()      ) < 1E-1












"""simple docstring"""
import tempfile

import torch

from diffusers import (
    DEISMultistepScheduler,
    DPMSolverMultistepScheduler,
    DPMSolverSinglestepScheduler,
    UniPCMultistepScheduler,
)

from .test_schedulers import SchedulerCommonTest



class   SCREAMING_SNAKE_CASE	(					_SCREAMING_SNAKE_CASE    ):





	"""simple docstring"""




	_A							:					Optional[int] 			=					(UniPCMultistepScheduler,)
	_A							:					Optional[Any] 			=					(("""num_inference_steps""", 25),)





	def  lowerCamelCase(self  ,  **lowerCAmelCase_				):
		A_		:    str						     = {
		    """num_train_timesteps""": 1000,
		    """beta_start""": 0.0001,
		    """beta_end""": 0.02,
		    """beta_schedule""": """linear""",
		    """solver_order""": 2,
		    """solver_type""": """bh2""",
		}

		config.update(**lowerCAmelCase_				)
		return config





	def  lowerCamelCase(self  ,  lowerCAmelCase_=0  ,  **lowerCAmelCase_				):
		A_		:    Tuple						     = dict(self.forward_default_kwargs				)
		A_		:    Any						     = kwargs.pop("""num_inference_steps"""  ,  lowerCAmelCase_				)
		A_		:    str						     = self.dummy_sample
		A_		:    Dict						     = 0.1 * sample
		A_		:    int						     = [residual + 0.2, residual + 0.15, residual + 0.10]

		for scheduler_class in self.scheduler_classes:
			A_		:    List[Any]						     = self.get_scheduler_config(**lowerCAmelCase_				)
			A_		:    Dict						     = scheduler_class(**lowerCAmelCase_				)
			scheduler.set_timesteps(lowerCAmelCase_				)
			# copy over dummy past residuals
			A_		:    Tuple						     = dummy_past_residuals[: scheduler.config.solver_order]

			with tempfile.TemporaryDirectory() as tmpdirname:
				scheduler.save_config(lowerCAmelCase_				)
				A_		:    Any						     = scheduler_class.from_pretrained(lowerCAmelCase_				)
				new_scheduler.set_timesteps(lowerCAmelCase_				)
				# copy over dummy past residuals
				A_		:    Any						     = dummy_past_residuals[: new_scheduler.config.solver_order]

			A_     ,  A_		:    Optional[int]						     = sample, sample
			for t in range(lowerCAmelCase_  ,  time_step + scheduler.config.solver_order + 1				):
				A_		:    Any						     = scheduler.step(lowerCAmelCase_  ,  lowerCAmelCase_  ,  lowerCAmelCase_  ,  **lowerCAmelCase_				).prev_sample
				A_		:    Dict						     = new_scheduler.step(lowerCAmelCase_  ,  lowerCAmelCase_  ,  lowerCAmelCase_  ,  **lowerCAmelCase_				).prev_sample

				assert torch.sum(torch.abs(output - new_output				)				) < 1e-5, "Scheduler outputs are not identical"





	def  lowerCamelCase(self  ,  lowerCAmelCase_=0  ,  **lowerCAmelCase_				):
		A_		:    Optional[int]						     = dict(self.forward_default_kwargs				)
		A_		:    int						     = kwargs.pop("""num_inference_steps"""  ,  lowerCAmelCase_				)
		A_		:    Optional[Any]						     = self.dummy_sample
		A_		:    List[Any]						     = 0.1 * sample
		A_		:    List[Any]						     = [residual + 0.2, residual + 0.15, residual + 0.10]

		for scheduler_class in self.scheduler_classes:
			A_		:    Dict						     = self.get_scheduler_config()
			A_		:    str						     = scheduler_class(**lowerCAmelCase_				)
			scheduler.set_timesteps(lowerCAmelCase_				)

			# copy over dummy past residuals (must be after setting timesteps)
			A_		:    Union[str, Any]						     = dummy_past_residuals[: scheduler.config.solver_order]

			with tempfile.TemporaryDirectory() as tmpdirname:
				scheduler.save_config(lowerCAmelCase_				)
				A_		:    Tuple						     = scheduler_class.from_pretrained(lowerCAmelCase_				)
				# copy over dummy past residuals
				new_scheduler.set_timesteps(lowerCAmelCase_				)

				# copy over dummy past residual (must be after setting timesteps)
				A_		:    Optional[int]						     = dummy_past_residuals[: new_scheduler.config.solver_order]

			A_		:    Tuple						     = scheduler.step(lowerCAmelCase_  ,  lowerCAmelCase_  ,  lowerCAmelCase_  ,  **lowerCAmelCase_				).prev_sample
			A_		:    str						     = new_scheduler.step(lowerCAmelCase_  ,  lowerCAmelCase_  ,  lowerCAmelCase_  ,  **lowerCAmelCase_				).prev_sample

			assert torch.sum(torch.abs(output - new_output				)				) < 1e-5, "Scheduler outputs are not identical"





	def  lowerCamelCase(self  ,  lowerCAmelCase_=None  ,  **lowerCAmelCase_				):
		if scheduler is None:
			A_		:    int						     = self.scheduler_classes[0]
			A_		:    List[str]						     = self.get_scheduler_config(**lowerCAmelCase_				)
			A_		:    Any						     = scheduler_class(**lowerCAmelCase_				)

		A_		:    int						     = self.scheduler_classes[0]
		A_		:    str						     = self.get_scheduler_config(**lowerCAmelCase_				)
		A_		:    Union[str, Any]						     = scheduler_class(**lowerCAmelCase_				)

		A_		:    Optional[int]						     = 10
		A_		:    Optional[Any]						     = self.dummy_model()
		A_		:    Optional[Any]						     = self.dummy_sample_deter
		scheduler.set_timesteps(lowerCAmelCase_				)

		for i, t in enumerate(scheduler.timesteps				):
			A_		:    Optional[int]						     = model(lowerCAmelCase_  ,  lowerCAmelCase_				)
			A_		:    Optional[int]						     = scheduler.step(lowerCAmelCase_  ,  lowerCAmelCase_  ,  lowerCAmelCase_				).prev_sample

		return sample





	def  lowerCamelCase(self				):
		A_		:    Any						     = dict(self.forward_default_kwargs				)

		A_		:    Any						     = kwargs.pop("""num_inference_steps"""  ,  lowerCAmelCase_				)

		for scheduler_class in self.scheduler_classes:
			A_		:    Dict						     = self.get_scheduler_config()
			A_		:    List[Any]						     = scheduler_class(**lowerCAmelCase_				)

			A_		:    int						     = self.dummy_sample
			A_		:    str						     = 0.1 * sample

			if num_inference_steps is not None and hasattr(lowerCAmelCase_  ,  """set_timesteps"""				):
				scheduler.set_timesteps(lowerCAmelCase_				)
			elif num_inference_steps is not None and not hasattr(lowerCAmelCase_  ,  """set_timesteps"""				):
				A_		:    Tuple						     = num_inference_steps

			# copy over dummy past residuals (must be done after set_timesteps)
			A_		:    List[Any]						     = [residual + 0.2, residual + 0.15, residual + 0.10]
			A_		:    Dict						     = dummy_past_residuals[: scheduler.config.solver_order]

			A_		:    str						     = scheduler.timesteps[5]
			A_		:    Any						     = scheduler.timesteps[6]

			A_		:    Tuple						     = scheduler.step(lowerCAmelCase_  ,  lowerCAmelCase_  ,  lowerCAmelCase_  ,  **lowerCAmelCase_				).prev_sample
			A_		:    Dict						     = scheduler.step(lowerCAmelCase_  ,  lowerCAmelCase_  ,  lowerCAmelCase_  ,  **lowerCAmelCase_				).prev_sample

			self.assertEqual(output_a.shape  ,  sample.shape				)
			self.assertEqual(output_a.shape  ,  output_a.shape				)





	def  lowerCamelCase(self				):
		# make sure that iterating over schedulers with same config names gives same results
		# for defaults
		A_		:    Tuple						     = UniPCMultistepScheduler(**self.get_scheduler_config()				)
		A_		:    List[str]						     = self.full_loop(scheduler=lowerCAmelCase_				)
		A_		:    int						     = torch.mean(torch.abs(lowerCAmelCase_				)				)

		assert abs(result_mean.item() - 0.2464				) < 1e-3

		A_		:    List[str]						     = DPMSolverSinglestepScheduler.from_config(scheduler.config				)
		A_		:    List[str]						     = DEISMultistepScheduler.from_config(scheduler.config				)
		A_		:    Optional[int]						     = DPMSolverMultistepScheduler.from_config(scheduler.config				)
		A_		:    str						     = UniPCMultistepScheduler.from_config(scheduler.config				)

		A_		:    Dict						     = self.full_loop(scheduler=lowerCAmelCase_				)
		A_		:    List[Any]						     = torch.mean(torch.abs(lowerCAmelCase_				)				)

		assert abs(result_mean.item() - 0.2464				) < 1e-3





	def  lowerCamelCase(self				):
		for timesteps in [25, 50, 100, 999, 1000]:
			self.check_over_configs(num_train_timesteps=lowerCAmelCase_				)





	def  lowerCamelCase(self				):
		self.check_over_configs(thresholding=lowerCAmelCase_				)
		for order in [1, 2, 3]:
			for solver_type in ["bh1", "bh2"]:
				for threshold in [0.5, 1.0, 2.0]:
					for prediction_type in ["epsilon", "sample"]:
						self.check_over_configs(
						    thresholding=lowerCAmelCase_  ,  prediction_type=lowerCAmelCase_  ,  sample_max_value=lowerCAmelCase_  ,  solver_order=lowerCAmelCase_  ,  solver_type=lowerCAmelCase_  ,  )





	def  lowerCamelCase(self				):
		for prediction_type in ["epsilon", "v_prediction"]:
			self.check_over_configs(prediction_type=lowerCAmelCase_				)





	def  lowerCamelCase(self				):
		for solver_type in ["bh1", "bh2"]:
			for order in [1, 2, 3]:
				for prediction_type in ["epsilon", "sample"]:
					self.check_over_configs(
					    solver_order=lowerCAmelCase_  ,  solver_type=lowerCAmelCase_  ,  prediction_type=lowerCAmelCase_  ,  )
					A_		:    Union[str, Any]						     = self.full_loop(
					    solver_order=lowerCAmelCase_  ,  solver_type=lowerCAmelCase_  ,  prediction_type=lowerCAmelCase_  ,  )
					assert not torch.isnan(lowerCAmelCase_				).any(), "Samples have nan numbers"





	def  lowerCamelCase(self				):
		self.check_over_configs(lower_order_final=lowerCAmelCase_				)
		self.check_over_configs(lower_order_final=lowerCAmelCase_				)





	def  lowerCamelCase(self				):
		for num_inference_steps in [1, 2, 3, 5, 10, 50, 100, 999, 1000]:
			self.check_over_forward(num_inference_steps=lowerCAmelCase_  ,  time_step=0				)





	def  lowerCamelCase(self				):
		A_		:    Optional[int]						     = self.full_loop()
		A_		:    Optional[int]						     = torch.mean(torch.abs(lowerCAmelCase_				)				)

		assert abs(result_mean.item() - 0.2464				) < 1e-3





	def  lowerCamelCase(self				):
		A_		:    str						     = self.full_loop(prediction_type="""v_prediction"""				)
		A_		:    Optional[Any]						     = torch.mean(torch.abs(lowerCAmelCase_				)				)

		assert abs(result_mean.item() - 0.1014				) < 1e-3





	def  lowerCamelCase(self				):
		A_		:    List[str]						     = self.scheduler_classes[0]
		A_		:    str						     = self.get_scheduler_config(thresholding=lowerCAmelCase_  ,  dynamic_thresholding_ratio=0				)
		A_		:    Union[str, Any]						     = scheduler_class(**lowerCAmelCase_				)

		A_		:    Any						     = 10
		A_		:    int						     = self.dummy_model()
		A_		:    Optional[Any]						     = self.dummy_sample_deter.half()
		scheduler.set_timesteps(lowerCAmelCase_				)

		for i, t in enumerate(scheduler.timesteps				):
			A_		:    Dict						     = model(lowerCAmelCase_  ,  lowerCAmelCase_				)
			A_		:    Tuple						     = scheduler.step(lowerCAmelCase_  ,  lowerCAmelCase_  ,  lowerCAmelCase_				).prev_sample

		assert sample.dtype == torch.floataa





	def  lowerCamelCase(self  ,  **lowerCAmelCase_				):
		for scheduler_class in self.scheduler_classes:
			A_		:    Tuple						     = self.get_scheduler_config(**lowerCAmelCase_				)
			A_		:    int						     = scheduler_class(**lowerCAmelCase_				)

			scheduler.set_timesteps(scheduler.config.num_train_timesteps				)
			assert len(scheduler.timesteps.unique()				) == scheduler.num_inference_steps

