code: 
  
  
  '''simple docstring'''
  
  
  
  import random
  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():
  	import PIL
  
  	from transformers import FlavaImageProcessor
  	from transformers.image_utils import PILImageResampling
  	from transformers.models.flava.image_processing_flava import (
  	    FLAVA_CODEBOOK_MEAN,
  	    FLAVA_CODEBOOK_STD,
  	    FLAVA_IMAGE_MEAN,
  	    FLAVA_IMAGE_STD,
  	)
  else:
  	__lowercase			:		Dict =			None
  
  
  
  
  
  class 							__UpperCamelCase       (	unittest.TestCase      ):
  
  
  
  
  	def __init__(     self		,       __a		,       __a=7		,       __a=3		,       __a=30		,       __a=400		,       __a=True		,       __a=None		,       __a=True		,       __a=None		,       __a=None		,       __a=True		,       __a=1 / 255		,       __a=True		,       __a=FLAVA_IMAGE_MEAN		,       __a=FLAVA_IMAGE_STD		,       __a=14		,       __a=75		,       __a=None		,       __a=16		,       __a=0.3		,       __a=None		,       __a=True		,       __a=None		,       __a=None		,       __a=True		,       __a=None		,       __a=True		,       __a=True		,       __a=FLAVA_CODEBOOK_MEAN		,       __a=FLAVA_CODEBOOK_STD		,       ):
  
  						'''simple docstring'''
  
  
  
  
  
  						__a    : str      					=						size if size is not None else {'height': 224, 'width': 224}
  						__a    : List[Any]      					=						crop_size if crop_size is not None else {'height': 224, 'width': 224}
  						__a    : Dict      					=						codebook_size if codebook_size is not None else {'height': 112, 'width': 112}
  						__a    : str      					=						codebook_crop_size if codebook_crop_size is not None else {'height': 112, 'width': 112}
  
  						__a    : str      					=						parent
  						__a    : Tuple      					=						batch_size
  						__a    : Any      					=						num_channels
  						__a    : List[Any]      					=						do_resize
  						__a    : str      					=						do_rescale
  						__a    : Dict      					=						rescale_factor
  						__a    : List[Any]      					=						min_resolution
  						__a    : Tuple      					=						max_resolution
  						__a    : List[str]      					=						size
  						__a    : Union[str, Any]      					=						resample if resample is not None else PILImageResampling.BICUBIC
  						__a    : Optional[int]      					=						do_normalize
  						__a    : Dict      					=						image_mean
  						__a    : Any      					=						image_std
  						__a    : List[Any]      					=						do_center_crop
  						__a    : Tuple      					=						crop_size
  
  						__a    : str      					=						input_size_patches
  						__a    : Union[str, Any]      					=						total_mask_patches
  						__a    : List[Any]      					=						mask_group_max_patches
  						__a    : Union[str, Any]      					=						mask_group_min_patches
  						__a    : Optional[Any]      					=						mask_group_min_aspect_ratio
  						__a    : Optional[Any]      					=						mask_group_max_aspect_ratio
  
  						__a    : Optional[Any]      					=						codebook_do_resize
  						__a    : Optional[int]      					=						codebook_size
  						__a    : int      					=						codebook_resample if codebook_resample is not None else PILImageResampling.LANCZOS
  						__a    : int      					=						codebook_do_center_crop
  						__a    : str      					=						codebook_crop_size
  						__a    : str      					=						codebook_do_map_pixels
  						__a    : str      					=						codebook_do_normalize
  						__a    : Tuple      					=						codebook_image_mean
  						__a    : str      					=						codebook_image_std
  
  
  
  
  	def    __UpperCAmelCase							(     self							):
  
  						'''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,
  						    "resample": self.resample,
  						    "do_rescale": self.do_rescale,
  						    "rescale_factor": self.rescale_factor,
  						    "do_center_crop": self.do_center_crop,
  						    "crop_size": self.crop_size,
  						    "input_size_patches": self.input_size_patches,
  						    "total_mask_patches": self.total_mask_patches,
  						    "mask_group_max_patches": self.mask_group_max_patches,
  						    "mask_group_min_patches": self.mask_group_min_patches,
  						    "mask_group_min_aspect_ratio": self.mask_group_min_aspect_ratio,
  						    "mask_group_max_aspect_ratio": self.mask_group_min_aspect_ratio,
  						    "codebook_do_resize": self.codebook_do_resize,
  						    "codebook_size": self.codebook_size,
  						    "codebook_resample": self.codebook_resample,
  						    "codebook_do_center_crop": self.codebook_do_center_crop,
  						    "codebook_crop_size": self.codebook_crop_size,
  						    "codebook_do_map_pixels": self.codebook_do_map_pixels,
  						    "codebook_do_normalize": self.codebook_do_normalize,
  						    "codebook_image_mean": self.codebook_image_mean,
  						    "codebook_image_std": self.codebook_image_std,
  						}
  
  
  
  
  	def    __UpperCAmelCase							(     self							):
  
  						'''simple docstring'''
  
  
  
  
  
  						return (self.size["height"], self.size["width"])
  
  
  
  
  	def    __UpperCAmelCase							(     self							):
  
  						'''simple docstring'''
  
  
  
  
  
  						return (
  						    (self.input_size_patches, self.input_size_patches)
  						    if not isinstance(self.input_size_patches		,       __a							)
  						    else self.input_size_patches
  						)
  
  
  
  
  
  	def    __UpperCAmelCase							(     self							):
  
  						'''simple docstring'''
  
  
  
  
  
  						return (self.codebook_size["height"], self.codebook_size["width"])
  
  
  
  
  
  @require_torch
  @require_vision
  class 							__UpperCamelCase       (	_a	,   unittest.TestCase      ):
  	A_					    =					FlavaImageProcessor if is_vision_available() else None
  	A_					    =					None
  
  
  
  
  	def    __UpperCAmelCase							(     self							):
  
  						'''simple docstring'''
  
  
  
  
  
  						__a    : Dict      					=						FlavaImageProcessingTester(self							)
  
  
  
  
  	@property
  	def    __UpperCAmelCase							(     self							):
  
  						'''simple docstring'''
  
  
  
  
  
  						return self.image_processor_tester.prepare_image_processor_dict()
  
  
  
  
  	def    __UpperCAmelCase							(     self							):
  
  						'''simple docstring'''
  
  
  
  
  
  						__a    : str      					=						self.image_processing_class(**self.image_processor_dict							)
  						self.assertTrue(hasattr(__a		,       'image_mean'							)							)
  						self.assertTrue(hasattr(__a		,       'image_std'							)							)
  						self.assertTrue(hasattr(__a		,       'do_normalize'							)							)
  						self.assertTrue(hasattr(__a		,       'do_resize'							)							)
  						self.assertTrue(hasattr(__a		,       'resample'							)							)
  						self.assertTrue(hasattr(__a		,       'crop_size'							)							)
  						self.assertTrue(hasattr(__a		,       'do_center_crop'							)							)
  						self.assertTrue(hasattr(__a		,       'do_rescale'							)							)
  						self.assertTrue(hasattr(__a		,       'rescale_factor'							)							)
  						self.assertTrue(hasattr(__a		,       'masking_generator'							)							)
  						self.assertTrue(hasattr(__a		,       'codebook_do_resize'							)							)
  						self.assertTrue(hasattr(__a		,       'codebook_size'							)							)
  						self.assertTrue(hasattr(__a		,       'codebook_resample'							)							)
  						self.assertTrue(hasattr(__a		,       'codebook_do_center_crop'							)							)
  						self.assertTrue(hasattr(__a		,       'codebook_crop_size'							)							)
  						self.assertTrue(hasattr(__a		,       'codebook_do_map_pixels'							)							)
  						self.assertTrue(hasattr(__a		,       'codebook_do_normalize'							)							)
  						self.assertTrue(hasattr(__a		,       'codebook_image_mean'							)							)
  						self.assertTrue(hasattr(__a		,       'codebook_image_std'							)							)
  
  
  
  
  	def    __UpperCAmelCase							(     self							):
  
  						'''simple docstring'''
  
  
  
  
  
  						__a    : int      					=						self.image_processing_class.from_dict(self.image_processor_dict							)
  						self.assertEqual(image_processor.size		,       {'height': 224, 'width': 224}							)
  						self.assertEqual(image_processor.crop_size		,       {'height': 224, 'width': 224}							)
  						self.assertEqual(image_processor.codebook_size		,       {'height': 112, 'width': 112}							)
  						self.assertEqual(image_processor.codebook_crop_size		,       {'height': 112, 'width': 112}							)
  
  						__a    : Tuple      					=						self.image_processing_class.from_dict(
  						    self.image_processor_dict		,       size=42		,       crop_size=84		,       codebook_size=33		,       codebook_crop_size=66							)
  						self.assertEqual(image_processor.size		,       {'height': 42, 'width': 42}							)
  						self.assertEqual(image_processor.crop_size		,       {'height': 84, 'width': 84}							)
  						self.assertEqual(image_processor.codebook_size		,       {'height': 33, 'width': 33}							)
  						self.assertEqual(image_processor.codebook_crop_size		,       {'height': 66, 'width': 66}							)
  
  
  
  
  	def    __UpperCAmelCase							(     self							):
  
  						'''simple docstring'''
  
  
  
  
  
  						pass
  
  
  
  
  	def    __UpperCAmelCase							(     self							):
  
  						'''simple docstring'''
  
  
  
  
  
  						__a    : List[Any]      					=						self.image_processing_class(**self.image_processor_dict							)
  						# create random PIL images
  						__a    : Union[str, Any]      					=						prepare_image_inputs(self.image_processor_tester		,       equal_resolution=__a							)
  						for image in image_inputs:
  											self.assertIsInstance(__a		,       PIL.Image.Image							)
  
  						# Test not batched input
  						__a    : List[str]      					=						image_processing(image_inputs[0]		,       return_tensors='pt'							)
  
  						# Test no bool masked pos
  						self.assertFalse('bool_masked_pos' in encoded_images							)
  
  						__a					,       __a    : Dict      					=						self.image_processor_tester.get_expected_image_size()
  
  						self.assertEqual(
  						    encoded_images.pixel_values.shape		,       (1, self.image_processor_tester.num_channels, expected_height, expected_width)		,       )
  
  						# Test batched
  						__a    : Any      					=						image_processing(__a		,       return_tensors='pt'							)
  						__a					,       __a    : Tuple      					=						self.image_processor_tester.get_expected_image_size()
  
  						# Test no bool masked pos
  						self.assertFalse('bool_masked_pos' in encoded_images							)
  
  						self.assertEqual(
  						    encoded_images.pixel_values.shape		,       (
  						        self.image_processor_tester.batch_size,
  						        self.image_processor_tester.num_channels,
  						        expected_height,
  						        expected_width,
  						    )		,       )
  
  
  
  
  	def    __UpperCAmelCase							(     self		,       __a		,       __a							):
  
  						'''simple docstring'''
  
  
  
  
  
  						__a    : List[str]      					=						self.image_processing_class(**self.image_processor_dict							)
  						# create random tensors
  						__a    : str      					=						prepare_image_inputs(self.image_processor_tester		,       equal_resolution=__a		,       **__a							)
  						for image in image_inputs:
  											self.assertIsInstance(__a		,       __a							)
  
  						# Test not batched input
  						__a    : List[str]      					=						image_processing(image_inputs[0]		,       return_tensors='pt'							)
  
  						__a					,       __a    : str      					=						self.image_processor_tester.get_expected_image_size()
  						self.assertEqual(
  						    encoded_images.pixel_values.shape		,       (1, self.image_processor_tester.num_channels, expected_height, expected_width)		,       )
  
  						__a    : Optional[Any]      					=						image_processing(__a		,       return_image_mask=__a		,       return_tensors='pt'							)
  
  						__a					,       __a    : int      					=						self.image_processor_tester.get_expected_image_size()
  						self.assertEqual(
  						    encoded_images.pixel_values.shape		,       (
  						        self.image_processor_tester.batch_size,
  						        self.image_processor_tester.num_channels,
  						        expected_height,
  						        expected_width,
  						    )		,       )
  
  						__a					,       __a    : Dict      					=						self.image_processor_tester.get_expected_mask_size()
  						self.assertEqual(
  						    encoded_images.bool_masked_pos.shape		,       (
  						        self.image_processor_tester.batch_size,
  						        expected_height,
  						        expected_width,
  						    )		,       )
  
  						# Test batched
  						__a    : Union[str, Any]      					=						image_processing(__a		,       return_tensors='pt'							).pixel_values
  
  						__a					,       __a    : int      					=						self.image_processor_tester.get_expected_image_size()
  						self.assertEqual(
  						    encoded_images.shape		,       (
  						        self.image_processor_tester.batch_size,
  						        self.image_processor_tester.num_channels,
  						        expected_height,
  						        expected_width,
  						    )		,       )
  
  						# Test masking
  						__a    : List[str]      					=						image_processing(__a		,       return_image_mask=__a		,       return_tensors='pt'							)
  
  						__a					,       __a    : Any      					=						self.image_processor_tester.get_expected_image_size()
  						self.assertEqual(
  						    encoded_images.pixel_values.shape		,       (
  						        self.image_processor_tester.batch_size,
  						        self.image_processor_tester.num_channels,
  						        expected_height,
  						        expected_width,
  						    )		,       )
  
  						__a					,       __a    : Optional[int]      					=						self.image_processor_tester.get_expected_mask_size()
  						self.assertEqual(
  						    encoded_images.bool_masked_pos.shape		,       (
  						        self.image_processor_tester.batch_size,
  						        expected_height,
  						        expected_width,
  						    )		,       )
  
  
  
  
  	def    __UpperCAmelCase							(     self							):
  
  						'''simple docstring'''
  
  
  
  
  
  						self._test_call_framework(np.ndarray		,       prepare_kwargs={'numpify': True}							)
  
  
  
  
  	def    __UpperCAmelCase							(     self							):
  
  						'''simple docstring'''
  
  
  
  
  
  						self._test_call_framework(torch.Tensor		,       prepare_kwargs={'torchify': True}							)
  
  
  
  
  	def    __UpperCAmelCase							(     self							):
  
  						'''simple docstring'''
  
  
  
  
  
  						random.seed(1234							)
  						__a    : Any      					=						self.image_processing_class(**self.image_processor_dict							)
  						__a    : Dict      					=						prepare_image_inputs(self.image_processor_tester		,       equal_resolution=__a		,       torchify=__a							)
  
  						# Test not batched input
  						__a    : Optional[int]      					=						image_processing(image_inputs[0]		,       return_image_mask=__a		,       return_tensors='pt'							)
  						self.assertEqual(encoded_images.bool_masked_pos.sum().item()		,       75							)
  
  
  
  
  
  	def    __UpperCAmelCase							(     self							):
  
  						'''simple docstring'''
  
  
  
  
  
  						__a    : int      					=						self.image_processing_class(**self.image_processor_dict							)
  						# create random PIL images
  						__a    : Dict      					=						prepare_image_inputs(self.image_processor_tester		,       equal_resolution=__a							)
  						for image in image_inputs:
  											self.assertIsInstance(__a		,       PIL.Image.Image							)
  
  						# Test not batched input
  						__a    : Optional[Any]      					=						image_processing(image_inputs[0]		,       return_codebook_pixels=__a		,       return_tensors='pt'							)
  						__a					,       __a    : str      					=						self.image_processor_tester.get_expected_codebook_image_size()
  						self.assertEqual(
  						    encoded_images.codebook_pixel_values.shape		,       (1, self.image_processor_tester.num_channels, expected_height, expected_width)		,       )
  
  						# Test batched
  						__a    : Union[str, Any]      					=						image_processing(__a		,       return_codebook_pixels=__a		,       return_tensors='pt'							)
  						__a					,       __a    : Optional[Any]      					=						self.image_processor_tester.get_expected_codebook_image_size()
  						self.assertEqual(
  						    encoded_images.codebook_pixel_values.shape		,       (
  						        self.image_processor_tester.batch_size,
  						        self.image_processor_tester.num_channels,
  						        expected_height,
  						        expected_width,
  						    )		,       )
  
  
  
  
style_context: 
  
  
  '''simple docstring'''
  
  
  
  import torch
  
  from diffusers import KDPMaAncestralDiscreteScheduler
  from diffusers.utils import torch_device
  
  from .test_schedulers import SchedulerCommonTest
  
  
  
  
  
  class 							__UpperCamelCase       (	lowerCAmelCase_      ):
  	A_			    =					(KDPMaAncestralDiscreteScheduler,)
  	A_			    =					10
  
  
  
  
  	def    __UpperCAmelCase							(     self		,       **__a							):
  
  						'''simple docstring'''
  
  
  
  
  
  						__a    : Dict      					=						{
  						    'num_train_timesteps': 1100,
  						    'beta_start': 0.0001,
  						    'beta_end': 0.02,
  						    'beta_schedule': 'linear',
  						}
  
  						config.update(**__a							)
  						return config
  
  
  
  
  	def    __UpperCAmelCase							(     self							):
  
  						'''simple docstring'''
  
  
  
  
  
  						for timesteps in [10, 50, 100, 1000]:
  											self.check_over_configs(num_train_timesteps=__a							)
  
  
  
  
  	def    __UpperCAmelCase							(     self							):
  
  						'''simple docstring'''
  
  
  
  
  
  						for beta_start, beta_end in zip([0.00001, 0.0001, 0.001]		,       [0.0002, 0.002, 0.02]							):
  											self.check_over_configs(beta_start=__a		,       beta_end=__a							)
  
  
  
  
  	def    __UpperCAmelCase							(     self							):
  
  						'''simple docstring'''
  
  
  
  
  
  						for schedule in ["linear", "scaled_linear"]:
  											self.check_over_configs(beta_schedule=__a							)
  
  
  
  
  	def    __UpperCAmelCase							(     self							):
  
  						'''simple docstring'''
  
  
  
  
  
  						if torch_device == "mps":
  											return
  						__a    : Dict      					=						self.scheduler_classes[0]
  						__a    : Tuple      					=						self.get_scheduler_config()
  						__a    : Optional[int]      					=						scheduler_class(**__a							)
  
  						scheduler.set_timesteps(self.num_inference_steps							)
  
  						__a    : int      					=						torch.manual_seed(0							)
  
  						__a    : Dict      					=						self.dummy_model()
  						__a    : List[Any]      					=						self.dummy_sample_deter * scheduler.init_noise_sigma
  						__a    : int      					=						sample.to(__a							)
  
  						for i, t in enumerate(scheduler.timesteps							):
  											__a    : Any      					=						scheduler.scale_model_input(__a		,       __a							)
  
  											__a    : int      					=						model(__a		,       __a							)
  
  											__a    : Dict      					=						scheduler.step(__a		,       __a		,       __a		,       generator=__a							)
  											__a    : int      					=						output.prev_sample
  
  						__a    : Tuple      					=						torch.sum(torch.abs(__a							)							)
  						__a    : List[str]      					=						torch.mean(torch.abs(__a							)							)
  
  						assert abs(result_sum.item() - 13849.3877							) < 1E-2
  						assert abs(result_mean.item() - 18.0331							) < 5E-3
  
  
  
  
  	def    __UpperCAmelCase							(     self							):
  
  						'''simple docstring'''
  
  
  
  
  
  						for prediction_type in ["epsilon", "v_prediction"]:
  											self.check_over_configs(prediction_type=__a							)
  
  
  
  
  	def    __UpperCAmelCase							(     self							):
  
  						'''simple docstring'''
  
  
  
  
  
  						if torch_device == "mps":
  											return
  						__a    : Dict      					=						self.scheduler_classes[0]
  						__a    : Dict      					=						self.get_scheduler_config(prediction_type='v_prediction'							)
  						__a    : List[str]      					=						scheduler_class(**__a							)
  
  						scheduler.set_timesteps(self.num_inference_steps							)
  
  						__a    : Dict      					=						self.dummy_model()
  						__a    : List[str]      					=						self.dummy_sample_deter * scheduler.init_noise_sigma
  						__a    : List[str]      					=						sample.to(__a							)
  
  						__a    : Tuple      					=						torch.manual_seed(0							)
  
  						for i, t in enumerate(scheduler.timesteps							):
  											__a    : List[Any]      					=						scheduler.scale_model_input(__a		,       __a							)
  
  											__a    : Optional[int]      					=						model(__a		,       __a							)
  
  											__a    : Union[str, Any]      					=						scheduler.step(__a		,       __a		,       __a		,       generator=__a							)
  											__a    : Union[str, Any]      					=						output.prev_sample
  
  						__a    : Any      					=						torch.sum(torch.abs(__a							)							)
  						__a    : Optional[int]      					=						torch.mean(torch.abs(__a							)							)
  
  						assert abs(result_sum.item() - 328.9970							) < 1E-2
  						assert abs(result_mean.item() - 0.4284							) < 1E-3
  
  
  
  
  
  	def    __UpperCAmelCase							(     self							):
  
  						'''simple docstring'''
  
  
  
  
  
  						if torch_device == "mps":
  											return
  						__a    : List[str]      					=						self.scheduler_classes[0]
  						__a    : Union[str, Any]      					=						self.get_scheduler_config()
  						__a    : List[Any]      					=						scheduler_class(**__a							)
  
  						scheduler.set_timesteps(self.num_inference_steps		,       device=__a							)
  						__a    : Union[str, Any]      					=						torch.manual_seed(0							)
  
  						__a    : List[str]      					=						self.dummy_model()
  						__a    : Any      					=						self.dummy_sample_deter.to(__a							) * scheduler.init_noise_sigma
  
  						for t in scheduler.timesteps:
  											__a    : List[Any]      					=						scheduler.scale_model_input(__a		,       __a							)
  
  											__a    : str      					=						model(__a		,       __a							)
  
  											__a    : str      					=						scheduler.step(__a		,       __a		,       __a		,       generator=__a							)
  											__a    : Any      					=						output.prev_sample
  
  						__a    : Union[str, Any]      					=						torch.sum(torch.abs(__a							)							)
  						__a    : str      					=						torch.mean(torch.abs(__a							)							)
  
  						assert abs(result_sum.item() - 13849.3818							) < 1E-1
  						assert abs(result_mean.item() - 18.0331							) < 1E-3
  
  
  
  