





import json
import pathlib
import unittest

import numpy as np

from transformers.testing_utils import require_torch, require_vision, slow
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 ConditionalDetrImageProcessor





class 				a			(      unittest.TestCase			):






				def __init__(  self			, _lowerCAmelCase			, _lowerCAmelCase=7			, _lowerCAmelCase=3			, _lowerCAmelCase=30			, _lowerCAmelCase=400			, _lowerCAmelCase=True			, _lowerCAmelCase=None			, _lowerCAmelCase=True			, _lowerCAmelCase=[0.5, 0.5, 0.5]			, _lowerCAmelCase=[0.5, 0.5, 0.5]			, _lowerCAmelCase=True			, _lowerCAmelCase=1 / 255			, _lowerCAmelCase=True			, ):

										"""simple docstring"""




										__SCREAMING_SNAKE_CASE:				int           =   size if size is not None else {'''shortest_edge''': 18, '''longest_edge''': 1333}
										__SCREAMING_SNAKE_CASE:				Dict           =   parent
										__SCREAMING_SNAKE_CASE:				List[str]           =   batch_size
										__SCREAMING_SNAKE_CASE:				Optional[int]           =   num_channels
										__SCREAMING_SNAKE_CASE:				Optional[Any]           =   min_resolution
										__SCREAMING_SNAKE_CASE:				Tuple           =   max_resolution
										__SCREAMING_SNAKE_CASE:				List[Any]           =   do_resize
										__SCREAMING_SNAKE_CASE:				Any           =   size
										__SCREAMING_SNAKE_CASE:				List[str]           =   do_normalize
										__SCREAMING_SNAKE_CASE:				List[str]           =   image_mean
										__SCREAMING_SNAKE_CASE:				Tuple           =   image_std
										__SCREAMING_SNAKE_CASE:				Tuple           =   do_rescale
										__SCREAMING_SNAKE_CASE:				int           =   rescale_factor
										__SCREAMING_SNAKE_CASE:				int           =   do_pad






				def 			snake_case_    (  self					):

										"""simple docstring"""




										return {
										    "do_resize": self.do_resize,
										    "size": self.size,
										    "do_normalize": self.do_normalize,
										    "image_mean": self.image_mean,
										    "image_std": self.image_std,
										    "do_rescale": self.do_rescale,
										    "rescale_factor": self.rescale_factor,
										    "do_pad": self.do_pad,
										}






				def 			snake_case_    (  self			, _lowerCAmelCase			, _lowerCAmelCase=False					):

										"""simple docstring"""




										if not batched:
																__SCREAMING_SNAKE_CASE:				List[str]           =   image_inputs[0]
																if isinstance(_lowerCAmelCase			, Image.Image					):
																						__SCREAMING_SNAKE_CASE       ,__SCREAMING_SNAKE_CASE:				Tuple           =   image.size
																else:
																						__SCREAMING_SNAKE_CASE       ,__SCREAMING_SNAKE_CASE:				List[str]           =   image.shape[1], image.shape[2]
																if w < h:
																						__SCREAMING_SNAKE_CASE:				Optional[int]           =   int(self.size['''shortest_edge'''] * h / w					)
																						__SCREAMING_SNAKE_CASE:				List[str]           =   self.size['''shortest_edge''']
																elif w > h:
																						__SCREAMING_SNAKE_CASE:				Union[str, Any]           =   self.size['''shortest_edge''']
																						__SCREAMING_SNAKE_CASE:				Optional[int]           =   int(self.size['''shortest_edge'''] * w / h					)
																else:
																						__SCREAMING_SNAKE_CASE:				Optional[Any]           =   self.size['''shortest_edge''']
																						__SCREAMING_SNAKE_CASE:				Dict           =   self.size['''shortest_edge''']

										else:
																__SCREAMING_SNAKE_CASE:				Optional[int]           =   []
																for image in image_inputs:
																						__SCREAMING_SNAKE_CASE       ,__SCREAMING_SNAKE_CASE:				Tuple           =   self.get_expected_values([image]					)
																						expected_values.append((expected_height, expected_width)					)
																__SCREAMING_SNAKE_CASE:				Optional[Any]           =   max(_lowerCAmelCase			, key=lambda _lowerCAmelCase					: item[0]					)[0]
																__SCREAMING_SNAKE_CASE:				Optional[int]           =   max(_lowerCAmelCase			, key=lambda _lowerCAmelCase					: item[1]					)[1]

										return expected_height, expected_width





@require_torch
@require_vision
class 				a			(      __lowercase   ,unittest.TestCase			):
				SCREAMING_SNAKE_CASE__     :   List[str]		      =     ConditionalDetrImageProcessor if is_vision_available() else None






				def 			snake_case_    (  self					):

										"""simple docstring"""




										__SCREAMING_SNAKE_CASE:				List[Any]           =   ConditionalDetrImageProcessingTester(self					)






				@property
				def 			snake_case_    (  self					):

										"""simple docstring"""




										return self.image_processor_tester.prepare_image_processor_dict()






				def 			snake_case_    (  self					):

										"""simple docstring"""




										__SCREAMING_SNAKE_CASE:				Union[str, Any]           =   self.image_processing_class(**self.image_processor_dict					)
										self.assertTrue(hasattr(_lowerCAmelCase			, '''image_mean'''					)					)
										self.assertTrue(hasattr(_lowerCAmelCase			, '''image_std'''					)					)
										self.assertTrue(hasattr(_lowerCAmelCase			, '''do_normalize'''					)					)
										self.assertTrue(hasattr(_lowerCAmelCase			, '''do_resize'''					)					)
										self.assertTrue(hasattr(_lowerCAmelCase			, '''size'''					)					)






				def 			snake_case_    (  self					):

										"""simple docstring"""




										__SCREAMING_SNAKE_CASE:				Optional[int]           =   self.image_processing_class.from_dict(self.image_processor_dict					)
										self.assertEqual(image_processor.size			, {'''shortest_edge''': 18, '''longest_edge''': 1333}					)
										self.assertEqual(image_processor.do_pad			, _lowerCAmelCase					)

										__SCREAMING_SNAKE_CASE:				Optional[Any]           =   self.image_processing_class.from_dict(
										    self.image_processor_dict			, size=42			, max_size=84			, pad_and_return_pixel_mask=_lowerCAmelCase					)
										self.assertEqual(image_processor.size			, {'''shortest_edge''': 42, '''longest_edge''': 84}					)
										self.assertEqual(image_processor.do_pad			, _lowerCAmelCase					)






				def 			snake_case_    (  self					):

										"""simple docstring"""




										pass






				def 			snake_case_    (  self					):

										"""simple docstring"""




										__SCREAMING_SNAKE_CASE:				List[str]           =   self.image_processing_class(**self.image_processor_dict					)
										# create random PIL images
										__SCREAMING_SNAKE_CASE:				int           =   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:				Any           =   image_processing(image_inputs[0]			, return_tensors='''pt'''					).pixel_values

										__SCREAMING_SNAKE_CASE       ,__SCREAMING_SNAKE_CASE:				str           =   self.image_processor_tester.get_expected_values(_lowerCAmelCase					)

										self.assertEqual(
										    encoded_images.shape			, (1, self.image_processor_tester.num_channels, expected_height, expected_width)			, )

										# Test batched
										__SCREAMING_SNAKE_CASE       ,__SCREAMING_SNAKE_CASE:				List[str]           =   self.image_processor_tester.get_expected_values(_lowerCAmelCase			, batched=_lowerCAmelCase					)

										__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,
										        expected_height,
										        expected_width,
										    )			, )






				def 			snake_case_    (  self					):

										"""simple docstring"""




										__SCREAMING_SNAKE_CASE:				Dict           =   self.image_processing_class(**self.image_processor_dict					)
										# create random numpy tensors
										__SCREAMING_SNAKE_CASE:				Dict           =   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:				Any           =   image_processing(image_inputs[0]			, return_tensors='''pt'''					).pixel_values

										__SCREAMING_SNAKE_CASE       ,__SCREAMING_SNAKE_CASE:				Tuple           =   self.image_processor_tester.get_expected_values(_lowerCAmelCase					)

										self.assertEqual(
										    encoded_images.shape			, (1, self.image_processor_tester.num_channels, expected_height, expected_width)			, )

										# Test batched
										__SCREAMING_SNAKE_CASE:				Optional[int]           =   image_processing(_lowerCAmelCase			, return_tensors='''pt'''					).pixel_values

										__SCREAMING_SNAKE_CASE       ,__SCREAMING_SNAKE_CASE:				Union[str, Any]           =   self.image_processor_tester.get_expected_values(_lowerCAmelCase			, batched=_lowerCAmelCase					)

										self.assertEqual(
										    encoded_images.shape			, (
										        self.image_processor_tester.batch_size,
										        self.image_processor_tester.num_channels,
										        expected_height,
										        expected_width,
										    )			, )






				def 			snake_case_    (  self					):

										"""simple docstring"""




										__SCREAMING_SNAKE_CASE:				str           =   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:				Optional[int]           =   image_processing(image_inputs[0]			, return_tensors='''pt'''					).pixel_values

										__SCREAMING_SNAKE_CASE       ,__SCREAMING_SNAKE_CASE:				List[str]           =   self.image_processor_tester.get_expected_values(_lowerCAmelCase					)

										self.assertEqual(
										    encoded_images.shape			, (1, self.image_processor_tester.num_channels, expected_height, expected_width)			, )

										# Test batched
										__SCREAMING_SNAKE_CASE:				Tuple           =   image_processing(_lowerCAmelCase			, return_tensors='''pt'''					).pixel_values

										__SCREAMING_SNAKE_CASE       ,__SCREAMING_SNAKE_CASE:				Dict           =   self.image_processor_tester.get_expected_values(_lowerCAmelCase			, batched=_lowerCAmelCase					)

										self.assertEqual(
										    encoded_images.shape			, (
										        self.image_processor_tester.batch_size,
										        self.image_processor_tester.num_channels,
										        expected_height,
										        expected_width,
										    )			, )






				@slow
				def 			snake_case_    (  self					):

										"""simple docstring"""




										__SCREAMING_SNAKE_CASE:				Dict           =   Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''					)
										with open('''./tests/fixtures/tests_samples/COCO/coco_annotations.txt'''			, '''r'''					) as f:
																__SCREAMING_SNAKE_CASE:				Tuple           =   json.loads(f.read()					)

										__SCREAMING_SNAKE_CASE:				Union[str, Any]           =   {'''image_id''': 39769, '''annotations''': target}

										# encode them
										__SCREAMING_SNAKE_CASE:				Tuple           =   ConditionalDetrImageProcessor.from_pretrained('''microsoft/conditional-detr-resnet-50'''					)
										__SCREAMING_SNAKE_CASE:				Any           =   image_processing(images=_lowerCAmelCase			, annotations=_lowerCAmelCase			, return_tensors='''pt'''					)

										# verify pixel values
										__SCREAMING_SNAKE_CASE:				Optional[Any]           =   torch.Size([1, 3, 800, 1066]					)
										self.assertEqual(encoding['''pixel_values'''].shape			, _lowerCAmelCase					)

										__SCREAMING_SNAKE_CASE:				List[str]           =   torch.tensor([0.2796, 0.3138, 0.3481]					)
										self.assertTrue(torch.allclose(encoding['''pixel_values'''][0, 0, 0, :3]			, _lowerCAmelCase			, atol=1e-4					)					)

										# verify area
										__SCREAMING_SNAKE_CASE:				Optional[int]           =   torch.tensor([5887.9600, 11250.2061, 489353.8438, 837122.7500, 147967.5156, 165732.3438]					)
										self.assertTrue(torch.allclose(encoding['''labels'''][0]['''area''']			, _lowerCAmelCase					)					)
										# verify boxes
										__SCREAMING_SNAKE_CASE:				int           =   torch.Size([6, 4]					)
										self.assertEqual(encoding['''labels'''][0]['''boxes'''].shape			, _lowerCAmelCase					)
										__SCREAMING_SNAKE_CASE:				str           =   torch.tensor([0.5503, 0.2765, 0.0604, 0.2215]					)
										self.assertTrue(torch.allclose(encoding['''labels'''][0]['''boxes'''][0]			, _lowerCAmelCase			, atol=1e-3					)					)
										# verify image_id
										__SCREAMING_SNAKE_CASE:				int           =   torch.tensor([39769]					)
										self.assertTrue(torch.allclose(encoding['''labels'''][0]['''image_id''']			, _lowerCAmelCase					)					)
										# verify is_crowd
										__SCREAMING_SNAKE_CASE:				Optional[int]           =   torch.tensor([0, 0, 0, 0, 0, 0]					)
										self.assertTrue(torch.allclose(encoding['''labels'''][0]['''iscrowd''']			, _lowerCAmelCase					)					)
										# verify class_labels
										__SCREAMING_SNAKE_CASE:				List[Any]           =   torch.tensor([75, 75, 63, 65, 17, 17]					)
										self.assertTrue(torch.allclose(encoding['''labels'''][0]['''class_labels''']			, _lowerCAmelCase					)					)
										# verify orig_size
										__SCREAMING_SNAKE_CASE:				str           =   torch.tensor([480, 640]					)
										self.assertTrue(torch.allclose(encoding['''labels'''][0]['''orig_size''']			, _lowerCAmelCase					)					)
										# verify size
										__SCREAMING_SNAKE_CASE:				List[Any]           =   torch.tensor([800, 1066]					)
										self.assertTrue(torch.allclose(encoding['''labels'''][0]['''size''']			, _lowerCAmelCase					)					)






				@slow
				def 			snake_case_    (  self					):

										"""simple docstring"""




										__SCREAMING_SNAKE_CASE:				str           =   Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''					)
										with open('''./tests/fixtures/tests_samples/COCO/coco_panoptic_annotations.txt'''			, '''r'''					) as f:
																__SCREAMING_SNAKE_CASE:				Dict           =   json.loads(f.read()					)

										__SCREAMING_SNAKE_CASE:				Tuple           =   {'''file_name''': '''000000039769.png''', '''image_id''': 39769, '''segments_info''': target}

										__SCREAMING_SNAKE_CASE:				Tuple           =   pathlib.Path('''./tests/fixtures/tests_samples/COCO/coco_panoptic'''					)

										# encode them
										__SCREAMING_SNAKE_CASE:				int           =   ConditionalDetrImageProcessor(format='''coco_panoptic'''					)
										__SCREAMING_SNAKE_CASE:				List[str]           =   image_processing(images=_lowerCAmelCase			, annotations=_lowerCAmelCase			, masks_path=_lowerCAmelCase			, return_tensors='''pt'''					)

										# verify pixel values
										__SCREAMING_SNAKE_CASE:				List[Any]           =   torch.Size([1, 3, 800, 1066]					)
										self.assertEqual(encoding['''pixel_values'''].shape			, _lowerCAmelCase					)

										__SCREAMING_SNAKE_CASE:				int           =   torch.tensor([0.2796, 0.3138, 0.3481]					)
										self.assertTrue(torch.allclose(encoding['''pixel_values'''][0, 0, 0, :3]			, _lowerCAmelCase			, atol=1e-4					)					)

										# verify area
										__SCREAMING_SNAKE_CASE:				Optional[int]           =   torch.tensor([147979.6875, 165527.0469, 484638.5938, 11292.9375, 5879.6562, 7634.1147]					)
										self.assertTrue(torch.allclose(encoding['''labels'''][0]['''area''']			, _lowerCAmelCase					)					)
										# verify boxes
										__SCREAMING_SNAKE_CASE:				Optional[int]           =   torch.Size([6, 4]					)
										self.assertEqual(encoding['''labels'''][0]['''boxes'''].shape			, _lowerCAmelCase					)
										__SCREAMING_SNAKE_CASE:				Tuple           =   torch.tensor([0.2625, 0.5437, 0.4688, 0.8625]					)
										self.assertTrue(torch.allclose(encoding['''labels'''][0]['''boxes'''][0]			, _lowerCAmelCase			, atol=1e-3					)					)
										# verify image_id
										__SCREAMING_SNAKE_CASE:				int           =   torch.tensor([39769]					)
										self.assertTrue(torch.allclose(encoding['''labels'''][0]['''image_id''']			, _lowerCAmelCase					)					)
										# verify is_crowd
										__SCREAMING_SNAKE_CASE:				Optional[Any]           =   torch.tensor([0, 0, 0, 0, 0, 0]					)
										self.assertTrue(torch.allclose(encoding['''labels'''][0]['''iscrowd''']			, _lowerCAmelCase					)					)
										# verify class_labels
										__SCREAMING_SNAKE_CASE:				int           =   torch.tensor([17, 17, 63, 75, 75, 93]					)
										self.assertTrue(torch.allclose(encoding['''labels'''][0]['''class_labels''']			, _lowerCAmelCase					)					)
										# verify masks
										__SCREAMING_SNAKE_CASE:				List[str]           =   822873
										self.assertEqual(encoding['''labels'''][0]['''masks'''].sum().item()			, _lowerCAmelCase					)
										# verify orig_size
										__SCREAMING_SNAKE_CASE:				Optional[Any]           =   torch.tensor([480, 640]					)
										self.assertTrue(torch.allclose(encoding['''labels'''][0]['''orig_size''']			, _lowerCAmelCase					)					)
										# verify size
										__SCREAMING_SNAKE_CASE:				Union[str, Any]           =   torch.tensor([800, 1066]					)
										self.assertTrue(torch.allclose(encoding['''labels'''][0]['''size''']			, _lowerCAmelCase					)					)










import gc
import unittest

import numpy as np
import torch
from torch.backends.cuda import sdp_kernel

from diffusers import (
    CMStochasticIterativeScheduler,
    ConsistencyModelPipeline,
    UNetaDModel,
)
from diffusers.utils import randn_tensor, slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_a, require_torch_gpu

from ..pipeline_params import UNCONDITIONAL_IMAGE_GENERATION_BATCH_PARAMS, UNCONDITIONAL_IMAGE_GENERATION_PARAMS
from ..test_pipelines_common import PipelineTesterMixin


enable_full_determinism()





class 				a			(      __lowercase   ,unittest.TestCase			):
				SCREAMING_SNAKE_CASE__     :   List[Any]		      =     ConsistencyModelPipeline
				SCREAMING_SNAKE_CASE__     :   Any		      =     UNCONDITIONAL_IMAGE_GENERATION_PARAMS
				SCREAMING_SNAKE_CASE__     :   str		      =     UNCONDITIONAL_IMAGE_GENERATION_BATCH_PARAMS

				# Override required_optional_params to remove num_images_per_prompt
				SCREAMING_SNAKE_CASE__     :   Optional[int]		      =     frozenset(
				    [
				        '''num_inference_steps''',
				        '''generator''',
				        '''latents''',
				        '''output_type''',
				        '''return_dict''',
				        '''callback''',
				        '''callback_steps''',
				    ]			)






				@property
				def 			snake_case_    (  self					):

										"""simple docstring"""




										__SCREAMING_SNAKE_CASE:				int           =   UNetaDModel.from_pretrained(
										    '''diffusers/consistency-models-test'''			, subfolder='''test_unet'''			, )
										return unet






				@property
				def 			snake_case_    (  self					):

										"""simple docstring"""




										__SCREAMING_SNAKE_CASE:				List[Any]           =   UNetaDModel.from_pretrained(
										    '''diffusers/consistency-models-test'''			, subfolder='''test_unet_class_cond'''			, )
										return unet






				def 			snake_case_    (  self			, _lowerCAmelCase=False					):

										"""simple docstring"""




										if class_cond:
																__SCREAMING_SNAKE_CASE:				List[Any]           =   self.dummy_cond_unet
										else:
																__SCREAMING_SNAKE_CASE:				Tuple           =   self.dummy_uncond_unet

										# Default to CM multistep sampler
										__SCREAMING_SNAKE_CASE:				Tuple           =   CMStochasticIterativeScheduler(
										    num_train_timesteps=40			, sigma_min=0.002			, sigma_max=80.0			, )

										__SCREAMING_SNAKE_CASE:				List[str]           =   {
										    '''unet''': unet,
										    '''scheduler''': scheduler,
										}

										return components






				def 			snake_case_    (  self			, _lowerCAmelCase			, _lowerCAmelCase=0					):

										"""simple docstring"""




										if str(_lowerCAmelCase					).startswith('''mps'''					):
																__SCREAMING_SNAKE_CASE:				List[str]           =   torch.manual_seed(_lowerCAmelCase					)
										else:
																__SCREAMING_SNAKE_CASE:				List[str]           =   torch.Generator(device=_lowerCAmelCase					).manual_seed(_lowerCAmelCase					)

										__SCREAMING_SNAKE_CASE:				Union[str, Any]           =   {
										    '''batch_size''': 1,
										    '''num_inference_steps''': None,
										    '''timesteps''': [22, 0],
										    '''generator''': generator,
										    '''output_type''': '''np''',
										}

										return inputs






				def 			snake_case_    (  self					):

										"""simple docstring"""




										__SCREAMING_SNAKE_CASE:				Dict           =   '''cpu'''  # ensure determinism for the device-dependent torch.Generator
										__SCREAMING_SNAKE_CASE:				Optional[int]           =   self.get_dummy_components()
										__SCREAMING_SNAKE_CASE:				Optional[Any]           =   ConsistencyModelPipeline(**_lowerCAmelCase					)
										__SCREAMING_SNAKE_CASE:				Any           =   pipe.to(_lowerCAmelCase					)
										pipe.set_progress_bar_config(disable=_lowerCAmelCase					)

										__SCREAMING_SNAKE_CASE:				Optional[int]           =   self.get_dummy_inputs(_lowerCAmelCase					)
										__SCREAMING_SNAKE_CASE:				Any           =   pipe(**_lowerCAmelCase					).images
										assert image.shape == (1, 32, 32, 3)

										__SCREAMING_SNAKE_CASE:				int           =   image[0, -3:, -3:, -1]
										__SCREAMING_SNAKE_CASE:				Optional[Any]           =   np.array([0.3572, 0.6273, 0.4031, 0.3961, 0.4321, 0.5730, 0.5266, 0.4780, 0.5004]					)

										assert np.abs(image_slice.flatten() - expected_slice					).max() < 1e-3






				def 			snake_case_    (  self					):

										"""simple docstring"""




										__SCREAMING_SNAKE_CASE:				Dict           =   '''cpu'''  # ensure determinism for the device-dependent torch.Generator
										__SCREAMING_SNAKE_CASE:				str           =   self.get_dummy_components(class_cond=_lowerCAmelCase					)
										__SCREAMING_SNAKE_CASE:				Any           =   ConsistencyModelPipeline(**_lowerCAmelCase					)
										__SCREAMING_SNAKE_CASE:				Any           =   pipe.to(_lowerCAmelCase					)
										pipe.set_progress_bar_config(disable=_lowerCAmelCase					)

										__SCREAMING_SNAKE_CASE:				Dict           =   self.get_dummy_inputs(_lowerCAmelCase					)
										__SCREAMING_SNAKE_CASE:				Dict           =   0
										__SCREAMING_SNAKE_CASE:				Optional[Any]           =   pipe(**_lowerCAmelCase					).images
										assert image.shape == (1, 32, 32, 3)

										__SCREAMING_SNAKE_CASE:				Optional[Any]           =   image[0, -3:, -3:, -1]
										__SCREAMING_SNAKE_CASE:				Optional[Any]           =   np.array([0.3572, 0.6273, 0.4031, 0.3961, 0.4321, 0.5730, 0.5266, 0.4780, 0.5004]					)

										assert np.abs(image_slice.flatten() - expected_slice					).max() < 1e-3






				def 			snake_case_    (  self					):

										"""simple docstring"""




										__SCREAMING_SNAKE_CASE:				Optional[int]           =   '''cpu'''  # ensure determinism for the device-dependent torch.Generator
										__SCREAMING_SNAKE_CASE:				Union[str, Any]           =   self.get_dummy_components()
										__SCREAMING_SNAKE_CASE:				Tuple           =   ConsistencyModelPipeline(**_lowerCAmelCase					)
										__SCREAMING_SNAKE_CASE:				List[str]           =   pipe.to(_lowerCAmelCase					)
										pipe.set_progress_bar_config(disable=_lowerCAmelCase					)

										__SCREAMING_SNAKE_CASE:				List[Any]           =   self.get_dummy_inputs(_lowerCAmelCase					)
										__SCREAMING_SNAKE_CASE:				int           =   1
										__SCREAMING_SNAKE_CASE:				Union[str, Any]           =   None
										__SCREAMING_SNAKE_CASE:				List[str]           =   pipe(**_lowerCAmelCase					).images
										assert image.shape == (1, 32, 32, 3)

										__SCREAMING_SNAKE_CASE:				Tuple           =   image[0, -3:, -3:, -1]
										__SCREAMING_SNAKE_CASE:				Optional[Any]           =   np.array([0.5004, 0.5004, 0.4994, 0.5008, 0.4976, 0.5018, 0.4990, 0.4982, 0.4987]					)

										assert np.abs(image_slice.flatten() - expected_slice					).max() < 1e-3






				def 			snake_case_    (  self					):

										"""simple docstring"""




										__SCREAMING_SNAKE_CASE:				int           =   '''cpu'''  # ensure determinism for the device-dependent torch.Generator
										__SCREAMING_SNAKE_CASE:				Optional[Any]           =   self.get_dummy_components(class_cond=_lowerCAmelCase					)
										__SCREAMING_SNAKE_CASE:				List[Any]           =   ConsistencyModelPipeline(**_lowerCAmelCase					)
										__SCREAMING_SNAKE_CASE:				List[Any]           =   pipe.to(_lowerCAmelCase					)
										pipe.set_progress_bar_config(disable=_lowerCAmelCase					)

										__SCREAMING_SNAKE_CASE:				Tuple           =   self.get_dummy_inputs(_lowerCAmelCase					)
										__SCREAMING_SNAKE_CASE:				Any           =   1
										__SCREAMING_SNAKE_CASE:				List[Any]           =   None
										__SCREAMING_SNAKE_CASE:				Tuple           =   0
										__SCREAMING_SNAKE_CASE:				List[str]           =   pipe(**_lowerCAmelCase					).images
										assert image.shape == (1, 32, 32, 3)

										__SCREAMING_SNAKE_CASE:				List[Any]           =   image[0, -3:, -3:, -1]
										__SCREAMING_SNAKE_CASE:				Any           =   np.array([0.5004, 0.5004, 0.4994, 0.5008, 0.4976, 0.5018, 0.4990, 0.4982, 0.4987]					)

										assert np.abs(image_slice.flatten() - expected_slice					).max() < 1e-3





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






				def 			snake_case_    (  self					):

										"""simple docstring"""




										super().tearDown()
										gc.collect()
										torch.cuda.empty_cache()






				def 			snake_case_    (  self			, _lowerCAmelCase=0			, _lowerCAmelCase=False			, _lowerCAmelCase="cpu"			, _lowerCAmelCase=torch.floataa			, _lowerCAmelCase=(1, 3, 64, 64)					):

										"""simple docstring"""




										__SCREAMING_SNAKE_CASE:				str           =   torch.manual_seed(_lowerCAmelCase					)

										__SCREAMING_SNAKE_CASE:				Dict           =   {
										    '''num_inference_steps''': None,
										    '''timesteps''': [22, 0],
										    '''class_labels''': 0,
										    '''generator''': generator,
										    '''output_type''': '''np''',
										}

										if get_fixed_latents:
																__SCREAMING_SNAKE_CASE:				Dict           =   self.get_fixed_latents(seed=_lowerCAmelCase			, device=_lowerCAmelCase			, dtype=_lowerCAmelCase			, shape=_lowerCAmelCase					)
																__SCREAMING_SNAKE_CASE:				Union[str, Any]           =   latents

										return inputs






				def 			snake_case_    (  self			, _lowerCAmelCase=0			, _lowerCAmelCase="cpu"			, _lowerCAmelCase=torch.floataa			, _lowerCAmelCase=(1, 3, 64, 64)					):

										"""simple docstring"""




										if type(_lowerCAmelCase					) == str:
																__SCREAMING_SNAKE_CASE:				List[Any]           =   torch.device(_lowerCAmelCase					)
										__SCREAMING_SNAKE_CASE:				Dict           =   torch.Generator(device=_lowerCAmelCase					).manual_seed(_lowerCAmelCase					)
										__SCREAMING_SNAKE_CASE:				int           =   randn_tensor(_lowerCAmelCase			, generator=_lowerCAmelCase			, device=_lowerCAmelCase			, dtype=_lowerCAmelCase					)
										return latents






				def 			snake_case_    (  self					):

										"""simple docstring"""




										__SCREAMING_SNAKE_CASE:				Union[str, Any]           =   UNetaDModel.from_pretrained('''diffusers/consistency_models'''			, subfolder='''diffusers_cd_imagenet64_l2'''					)
										__SCREAMING_SNAKE_CASE:				Union[str, Any]           =   CMStochasticIterativeScheduler(
										    num_train_timesteps=40			, sigma_min=0.002			, sigma_max=80.0			, )
										__SCREAMING_SNAKE_CASE:				Optional[int]           =   ConsistencyModelPipeline(unet=_lowerCAmelCase			, scheduler=_lowerCAmelCase					)
										pipe.to(torch_device=_lowerCAmelCase					)
										pipe.set_progress_bar_config(disable=_lowerCAmelCase					)

										__SCREAMING_SNAKE_CASE:				int           =   self.get_inputs()
										__SCREAMING_SNAKE_CASE:				List[Any]           =   pipe(**_lowerCAmelCase					).images
										assert image.shape == (1, 64, 64, 3)

										__SCREAMING_SNAKE_CASE:				Dict           =   image[0, -3:, -3:, -1]

										__SCREAMING_SNAKE_CASE:				Any           =   np.array([0.0888, 0.0881, 0.0666, 0.0479, 0.0292, 0.0195, 0.0201, 0.0163, 0.0254]					)

										assert np.abs(image_slice.flatten() - expected_slice					).max() < 2e-2






				def 			snake_case_    (  self					):

										"""simple docstring"""




										__SCREAMING_SNAKE_CASE:				List[Any]           =   UNetaDModel.from_pretrained('''diffusers/consistency_models'''			, subfolder='''diffusers_cd_imagenet64_l2'''					)
										__SCREAMING_SNAKE_CASE:				str           =   CMStochasticIterativeScheduler(
										    num_train_timesteps=40			, sigma_min=0.002			, sigma_max=80.0			, )
										__SCREAMING_SNAKE_CASE:				Union[str, Any]           =   ConsistencyModelPipeline(unet=_lowerCAmelCase			, scheduler=_lowerCAmelCase					)
										pipe.to(torch_device=_lowerCAmelCase					)
										pipe.set_progress_bar_config(disable=_lowerCAmelCase					)

										__SCREAMING_SNAKE_CASE:				List[str]           =   self.get_inputs()
										__SCREAMING_SNAKE_CASE:				Union[str, Any]           =   1
										__SCREAMING_SNAKE_CASE:				List[Any]           =   None
										__SCREAMING_SNAKE_CASE:				Optional[int]           =   pipe(**_lowerCAmelCase					).images
										assert image.shape == (1, 64, 64, 3)

										__SCREAMING_SNAKE_CASE:				Dict           =   image[0, -3:, -3:, -1]

										__SCREAMING_SNAKE_CASE:				Any           =   np.array([0.0340, 0.0152, 0.0063, 0.0267, 0.0221, 0.0107, 0.0416, 0.0186, 0.0217]					)

										assert np.abs(image_slice.flatten() - expected_slice					).max() < 2e-2






				@require_torch_a
				def 			snake_case_    (  self					):

										"""simple docstring"""




										__SCREAMING_SNAKE_CASE:				Tuple           =   UNetaDModel.from_pretrained('''diffusers/consistency_models'''			, subfolder='''diffusers_cd_imagenet64_l2'''					)
										__SCREAMING_SNAKE_CASE:				Union[str, Any]           =   CMStochasticIterativeScheduler(
										    num_train_timesteps=40			, sigma_min=0.002			, sigma_max=80.0			, )
										__SCREAMING_SNAKE_CASE:				List[Any]           =   ConsistencyModelPipeline(unet=_lowerCAmelCase			, scheduler=_lowerCAmelCase					)
										pipe.to(torch_device=_lowerCAmelCase			, torch_dtype=torch.floataa					)
										pipe.set_progress_bar_config(disable=_lowerCAmelCase					)

										__SCREAMING_SNAKE_CASE:				int           =   self.get_inputs(get_fixed_latents=_lowerCAmelCase			, device=_lowerCAmelCase					)
										# Ensure usage of flash attention in torch 2.0
										with sdp_kernel(enable_flash=_lowerCAmelCase			, enable_math=_lowerCAmelCase			, enable_mem_efficient=_lowerCAmelCase					):
																__SCREAMING_SNAKE_CASE:				Dict           =   pipe(**_lowerCAmelCase					).images
										assert image.shape == (1, 64, 64, 3)

										__SCREAMING_SNAKE_CASE:				List[Any]           =   image[0, -3:, -3:, -1]

										__SCREAMING_SNAKE_CASE:				Optional[int]           =   np.array([0.1875, 0.1428, 0.1289, 0.2151, 0.2092, 0.1477, 0.1877, 0.1641, 0.1353]					)

										assert np.abs(image_slice.flatten() - expected_slice					).max() < 1e-3






				@require_torch_a
				def 			snake_case_    (  self					):

										"""simple docstring"""




										__SCREAMING_SNAKE_CASE:				Optional[Any]           =   UNetaDModel.from_pretrained('''diffusers/consistency_models'''			, subfolder='''diffusers_cd_imagenet64_l2'''					)
										__SCREAMING_SNAKE_CASE:				List[str]           =   CMStochasticIterativeScheduler(
										    num_train_timesteps=40			, sigma_min=0.002			, sigma_max=80.0			, )
										__SCREAMING_SNAKE_CASE:				List[Any]           =   ConsistencyModelPipeline(unet=_lowerCAmelCase			, scheduler=_lowerCAmelCase					)
										pipe.to(torch_device=_lowerCAmelCase			, torch_dtype=torch.floataa					)
										pipe.set_progress_bar_config(disable=_lowerCAmelCase					)

										__SCREAMING_SNAKE_CASE:				int           =   self.get_inputs(get_fixed_latents=_lowerCAmelCase			, device=_lowerCAmelCase					)
										__SCREAMING_SNAKE_CASE:				Union[str, Any]           =   1
										__SCREAMING_SNAKE_CASE:				List[Any]           =   None
										# Ensure usage of flash attention in torch 2.0
										with sdp_kernel(enable_flash=_lowerCAmelCase			, enable_math=_lowerCAmelCase			, enable_mem_efficient=_lowerCAmelCase					):
																__SCREAMING_SNAKE_CASE:				Optional[int]           =   pipe(**_lowerCAmelCase					).images
										assert image.shape == (1, 64, 64, 3)

										__SCREAMING_SNAKE_CASE:				Tuple           =   image[0, -3:, -3:, -1]

										__SCREAMING_SNAKE_CASE:				Tuple           =   np.array([0.1663, 0.1948, 0.2275, 0.1680, 0.1204, 0.1245, 0.1858, 0.1338, 0.2095]					)

										assert np.abs(image_slice.flatten() - expected_slice					).max() < 1e-3



