



'''simple docstring'''




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, prepare_image_inputs


if is_torch_available():
						import torch

if is_vision_available():
						from PIL import Image

						from transformers import PoolFormerImageProcessor






class 		_UpperCamelCase						(							unittest.TestCase					):


			'''simple docstring'''

			def __init__(							self  :				Union[str, Any]							,  lowerCAmelCase__  :				Optional[Any]							,  lowerCAmelCase__  :				Dict=7							,  lowerCAmelCase__  :				Any=3							,  lowerCAmelCase__  :				str=3_0							,  lowerCAmelCase__  :				List[str]=4_0_0							,  lowerCAmelCase__  :				int=True							,  lowerCAmelCase__  :				Optional[Any]=None							,  lowerCAmelCase__  :				Optional[Any]=0.9							,  lowerCAmelCase__  :				Union[str, Any]=None							,  lowerCAmelCase__  :				str=True							,  lowerCAmelCase__  :				Any=[0.5, 0.5, 0.5]							,  lowerCAmelCase__  :				str=[0.5, 0.5, 0.5]							,  ):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE :	Dict					 =	size if size is not None else {"""shortest_edge""": 3_0}
					__SCREAMING_SNAKE_CASE :	List[str]					 =	crop_size if crop_size is not None else {"""height""": 3_0, """width""": 3_0}
					__SCREAMING_SNAKE_CASE :	Any					 =	parent
					__SCREAMING_SNAKE_CASE :	List[Any]					 =	batch_size
					__SCREAMING_SNAKE_CASE :	Optional[Any]					 =	num_channels
					__SCREAMING_SNAKE_CASE :	Optional[Any]					 =	min_resolution
					__SCREAMING_SNAKE_CASE :	Union[str, Any]					 =	max_resolution
					__SCREAMING_SNAKE_CASE :	Union[str, Any]					 =	do_resize_and_center_crop
					__SCREAMING_SNAKE_CASE :	List[Any]					 =	size
					__SCREAMING_SNAKE_CASE :	Union[str, Any]					 =	crop_pct
					__SCREAMING_SNAKE_CASE :	Dict					 =	crop_size
					__SCREAMING_SNAKE_CASE :	Any					 =	do_normalize
					__SCREAMING_SNAKE_CASE :	Optional[int]					 =	image_mean
					__SCREAMING_SNAKE_CASE :	List[str]					 =	image_std



			def UpperCamelCase__      (							self  :				Any		):





					"""simple docstring"""






					return {
					    "size": self.size,
					    "do_resize_and_center_crop": self.do_resize_and_center_crop,
					    "crop_pct": self.crop_pct,
					    "crop_size": self.crop_size,
					    "do_normalize": self.do_normalize,
					    "image_mean": self.image_mean,
					    "image_std": self.image_std,
					}






@require_torch
@require_vision
class 		_UpperCamelCase						(							lowerCamelCase__					,      unittest.TestCase					):


			'''simple docstring'''

			_A   :	Any    					=	PoolFormerImageProcessor if is_vision_available() else None

			def UpperCamelCase__      (							self  :				Tuple		):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE :	Tuple					 =	PoolFormerImageProcessingTester(self		)

			@property
			def UpperCamelCase__      (							self  :				Tuple		):





					"""simple docstring"""






					return self.image_processor_tester.prepare_image_processor_dict()

			def UpperCamelCase__      (							self  :				Optional[Any]		):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE :	str					 =	self.image_processing_class(**self.image_processor_dict		)
					self.assertTrue(hasattr(lowerCAmelCase__							,  """do_resize_and_center_crop"""		)		)
					self.assertTrue(hasattr(lowerCAmelCase__							,  """size"""		)		)
					self.assertTrue(hasattr(lowerCAmelCase__							,  """crop_pct"""		)		)
					self.assertTrue(hasattr(lowerCAmelCase__							,  """do_normalize"""		)		)
					self.assertTrue(hasattr(lowerCAmelCase__							,  """image_mean"""		)		)
					self.assertTrue(hasattr(lowerCAmelCase__							,  """image_std"""		)		)

			def UpperCamelCase__      (							self  :				str		):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE :	List[str]					 =	self.image_processing_class.from_dict(self.image_processor_dict		)
					self.assertEqual(image_processor.size							,  {"""shortest_edge""": 3_0}		)
					self.assertEqual(image_processor.crop_size							,  {"""height""": 3_0, """width""": 3_0}		)

					__SCREAMING_SNAKE_CASE :	List[str]					 =	self.image_processing_class.from_dict(self.image_processor_dict							,  size=4_2							,  crop_size=8_4		)
					self.assertEqual(image_processor.size							,  {"""shortest_edge""": 4_2}		)
					self.assertEqual(image_processor.crop_size							,  {"""height""": 8_4, """width""": 8_4}		)

			def UpperCamelCase__      (							self  :				Any		):





					"""simple docstring"""






					pass

			def UpperCamelCase__      (							self  :				Dict		):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE :	Any					 =	self.image_processing_class(**self.image_processor_dict		)
					# create random PIL images
					__SCREAMING_SNAKE_CASE :	Optional[Any]					 =	prepare_image_inputs(self.image_processor_tester							,  equal_resolution=lowerCAmelCase__		)
					for image in image_inputs:
							self.assertIsInstance(lowerCAmelCase__							,  Image.Image		)

					# Test not batched input
					__SCREAMING_SNAKE_CASE :	Optional[Any]					 =	image_processing(image_inputs[0]							,  return_tensors="""pt"""		).pixel_values

					self.assertEqual(
					    encoded_images.shape							,  (
					        1,
					        self.image_processor_tester.num_channels,
					        self.image_processor_tester.crop_size["""height"""],
					        self.image_processor_tester.crop_size["""width"""],
					    )							,  )

					# Test batched
					__SCREAMING_SNAKE_CASE :	Any					 =	image_processing(lowerCAmelCase__							,  return_tensors="""pt"""		).pixel_values
					self.assertEqual(
					    encoded_images.shape							,  (
					        self.image_processor_tester.batch_size,
					        self.image_processor_tester.num_channels,
					        self.image_processor_tester.crop_size["""height"""],
					        self.image_processor_tester.crop_size["""width"""],
					    )							,  )

			def UpperCamelCase__      (							self  :				Any		):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE :	List[Any]					 =	self.image_processing_class(**self.image_processor_dict		)
					# create random numpy tensors
					__SCREAMING_SNAKE_CASE :	Any					 =	prepare_image_inputs(self.image_processor_tester							,  equal_resolution=lowerCAmelCase__							,  numpify=lowerCAmelCase__		)
					for image in image_inputs:
							self.assertIsInstance(lowerCAmelCase__							,  np.ndarray		)

					# Test not batched input
					__SCREAMING_SNAKE_CASE :	Optional[int]					 =	image_processing(image_inputs[0]							,  return_tensors="""pt"""		).pixel_values
					self.assertEqual(
					    encoded_images.shape							,  (
					        1,
					        self.image_processor_tester.num_channels,
					        self.image_processor_tester.crop_size["""height"""],
					        self.image_processor_tester.crop_size["""width"""],
					    )							,  )

					# Test batched
					__SCREAMING_SNAKE_CASE :	Tuple					 =	image_processing(lowerCAmelCase__							,  return_tensors="""pt"""		).pixel_values
					self.assertEqual(
					    encoded_images.shape							,  (
					        self.image_processor_tester.batch_size,
					        self.image_processor_tester.num_channels,
					        self.image_processor_tester.crop_size["""height"""],
					        self.image_processor_tester.crop_size["""width"""],
					    )							,  )



			def UpperCamelCase__      (							self  :				Dict		):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE :	List[Any]					 =	self.image_processing_class(**self.image_processor_dict		)
					# create random PyTorch tensors
					__SCREAMING_SNAKE_CASE :	int					 =	prepare_image_inputs(self.image_processor_tester							,  equal_resolution=lowerCAmelCase__							,  torchify=lowerCAmelCase__		)
					for image in image_inputs:
							self.assertIsInstance(lowerCAmelCase__							,  torch.Tensor		)

					# Test not batched input
					__SCREAMING_SNAKE_CASE :	List[str]					 =	image_processing(image_inputs[0]							,  return_tensors="""pt"""		).pixel_values
					self.assertEqual(
					    encoded_images.shape							,  (
					        1,
					        self.image_processor_tester.num_channels,
					        self.image_processor_tester.crop_size["""height"""],
					        self.image_processor_tester.crop_size["""width"""],
					    )							,  )

					# Test batched
					__SCREAMING_SNAKE_CASE :	Dict					 =	image_processing(lowerCAmelCase__							,  return_tensors="""pt"""		).pixel_values
					self.assertEqual(
					    encoded_images.shape							,  (
					        self.image_processor_tester.batch_size,
					        self.image_processor_tester.num_channels,
					        self.image_processor_tester.crop_size["""height"""],
					        self.image_processor_tester.crop_size["""width"""],
					    )							,  )




'''simple docstring'''




import torch

from diffusers import KDPMaDiscreteScheduler
from diffusers.utils import torch_device

from .test_schedulers import SchedulerCommonTest






class 		_UpperCamelCase						(							lowerCamelCase__					):


			'''simple docstring'''

			_A   :	Optional[int]    					=	(KDPMaDiscreteScheduler,)
			_A   :	Dict    					=	10

			def UpperCamelCase__      (							self  :				Optional[int]							,  **lowerCAmelCase__  :				List[Any]		):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE :	Any					 =	{
					    """num_train_timesteps""": 1_1_0_0,
					    """beta_start""": 0.00_01,
					    """beta_end""": 0.02,
					    """beta_schedule""": """linear""",
					}

					config.update(**lowerCAmelCase__		)
					return config

			def UpperCamelCase__      (							self  :				int		):





					"""simple docstring"""






					for timesteps in [1_0, 5_0, 1_0_0, 1_0_0_0]:
							self.check_over_configs(num_train_timesteps=lowerCAmelCase__		)

			def UpperCamelCase__      (							self  :				str		):





					"""simple docstring"""






					for beta_start, beta_end in zip([0.0_00_01, 0.00_01, 0.0_01]							,  [0.00_02, 0.0_02, 0.02]		):
							self.check_over_configs(beta_start=lowerCAmelCase__							,  beta_end=lowerCAmelCase__		)

			def UpperCamelCase__      (							self  :				Tuple		):





					"""simple docstring"""






					for schedule in ["linear", "scaled_linear"]:
							self.check_over_configs(beta_schedule=lowerCAmelCase__		)

			def UpperCamelCase__      (							self  :				int		):





					"""simple docstring"""






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

			def UpperCamelCase__      (							self  :				List[str]		):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE :	Any					 =	self.scheduler_classes[0]
					__SCREAMING_SNAKE_CASE :	Tuple					 =	self.get_scheduler_config(prediction_type="""v_prediction"""		)
					__SCREAMING_SNAKE_CASE :	Optional[int]					 =	scheduler_class(**lowerCAmelCase__		)

					scheduler.set_timesteps(self.num_inference_steps		)

					__SCREAMING_SNAKE_CASE :	Optional[int]					 =	self.dummy_model()
					__SCREAMING_SNAKE_CASE :	Optional[Any]					 =	self.dummy_sample_deter * scheduler.init_noise_sigma
					__SCREAMING_SNAKE_CASE :	Union[str, Any]					 =	sample.to(lowerCAmelCase__		)

					for i, t in enumerate(scheduler.timesteps		):
							__SCREAMING_SNAKE_CASE :	List[Any]					 =	scheduler.scale_model_input(lowerCAmelCase__							,  lowerCAmelCase__		)

							__SCREAMING_SNAKE_CASE :	Optional[Any]					 =	model(lowerCAmelCase__							,  lowerCAmelCase__		)

							__SCREAMING_SNAKE_CASE :	Dict					 =	scheduler.step(lowerCAmelCase__							,  lowerCAmelCase__							,  lowerCAmelCase__		)
							__SCREAMING_SNAKE_CASE :	List[str]					 =	output.prev_sample

					__SCREAMING_SNAKE_CASE :	List[Any]					 =	torch.sum(torch.abs(lowerCAmelCase__		)		)
					__SCREAMING_SNAKE_CASE :	List[Any]					 =	torch.mean(torch.abs(lowerCAmelCase__		)		)

					if torch_device in ["cpu", "mps"]:
							assert abs(result_sum.item() - 4.69_34E-07		) < 1E-2
							assert abs(result_mean.item() - 6.11_12E-10		) < 1E-3
					else:
							# CUDA
							assert abs(result_sum.item() - 4.6_93_42_86_50_17_09_72E-07		) < 1E-2
							assert abs(result_mean.item() - 0.00_02		) < 1E-3

			def UpperCamelCase__      (							self  :				Optional[int]		):





					"""simple docstring"""






					if torch_device == "mps":
							return
					__SCREAMING_SNAKE_CASE :	List[Any]					 =	self.scheduler_classes[0]
					__SCREAMING_SNAKE_CASE :	str					 =	self.get_scheduler_config()
					__SCREAMING_SNAKE_CASE :	Optional[Any]					 =	scheduler_class(**lowerCAmelCase__		)

					scheduler.set_timesteps(self.num_inference_steps		)

					__SCREAMING_SNAKE_CASE :	Union[str, Any]					 =	self.dummy_model()
					__SCREAMING_SNAKE_CASE :	Optional[int]					 =	self.dummy_sample_deter * scheduler.init_noise_sigma
					__SCREAMING_SNAKE_CASE :	str					 =	sample.to(lowerCAmelCase__		)

					for i, t in enumerate(scheduler.timesteps		):
							__SCREAMING_SNAKE_CASE :	Tuple					 =	scheduler.scale_model_input(lowerCAmelCase__							,  lowerCAmelCase__		)

							__SCREAMING_SNAKE_CASE :	Optional[Any]					 =	model(lowerCAmelCase__							,  lowerCAmelCase__		)

							__SCREAMING_SNAKE_CASE :	Any					 =	scheduler.step(lowerCAmelCase__							,  lowerCAmelCase__							,  lowerCAmelCase__		)
							__SCREAMING_SNAKE_CASE :	List[Any]					 =	output.prev_sample

					__SCREAMING_SNAKE_CASE :	int					 =	torch.sum(torch.abs(lowerCAmelCase__		)		)
					__SCREAMING_SNAKE_CASE :	int					 =	torch.mean(torch.abs(lowerCAmelCase__		)		)

					if torch_device in ["cpu", "mps"]:
							assert abs(result_sum.item() - 20.41_25		) < 1E-2
							assert abs(result_mean.item() - 0.02_66		) < 1E-3
					else:
							# CUDA
							assert abs(result_sum.item() - 20.41_25		) < 1E-2
							assert abs(result_mean.item() - 0.02_66		) < 1E-3



			def UpperCamelCase__      (							self  :				List[str]		):





					"""simple docstring"""






					if torch_device == "mps":
							return
					__SCREAMING_SNAKE_CASE :	Dict					 =	self.scheduler_classes[0]
					__SCREAMING_SNAKE_CASE :	Dict					 =	self.get_scheduler_config()
					__SCREAMING_SNAKE_CASE :	List[Any]					 =	scheduler_class(**lowerCAmelCase__		)

					scheduler.set_timesteps(self.num_inference_steps							,  device=lowerCAmelCase__		)

					__SCREAMING_SNAKE_CASE :	List[Any]					 =	self.dummy_model()
					__SCREAMING_SNAKE_CASE :	Optional[Any]					 =	self.dummy_sample_deter.to(lowerCAmelCase__		) * scheduler.init_noise_sigma

					for t in scheduler.timesteps:
							__SCREAMING_SNAKE_CASE :	Dict					 =	scheduler.scale_model_input(lowerCAmelCase__							,  lowerCAmelCase__		)

							__SCREAMING_SNAKE_CASE :	Any					 =	model(lowerCAmelCase__							,  lowerCAmelCase__		)

							__SCREAMING_SNAKE_CASE :	Union[str, Any]					 =	scheduler.step(lowerCAmelCase__							,  lowerCAmelCase__							,  lowerCAmelCase__		)
							__SCREAMING_SNAKE_CASE :	str					 =	output.prev_sample

					__SCREAMING_SNAKE_CASE :	Dict					 =	torch.sum(torch.abs(lowerCAmelCase__		)		)
					__SCREAMING_SNAKE_CASE :	int					 =	torch.mean(torch.abs(lowerCAmelCase__		)		)

					if str(lowerCAmelCase__		).startswith("""cpu"""		):
							# The following sum varies between 148 and 156 on mps. Why?
							assert abs(result_sum.item() - 20.41_25		) < 1E-2
							assert abs(result_mean.item() - 0.02_66		) < 1E-3
					else:
							# CUDA
							assert abs(result_sum.item() - 20.41_25		) < 1E-2
							assert abs(result_mean.item() - 0.02_66		) < 1E-3