





import unittest

from transformers import SPIECE_UNDERLINE, XLNetTokenizer, XLNetTokenizerFast
from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_tokenizers, slow

from ...test_tokenization_common import TokenizerTesterMixin


lowerCAmelCase							:       List[Any]					 =	get_tests_dir("""fixtures/test_sentencepiece.model""")





@require_sentencepiece
@require_tokenizers
class 				a			(      __lowercase   ,unittest.TestCase			):
				SCREAMING_SNAKE_CASE__     :   Dict		      =     XLNetTokenizer
				SCREAMING_SNAKE_CASE__     :   List[str]		      =     XLNetTokenizerFast
				SCREAMING_SNAKE_CASE__     :   Any		      =     True
				SCREAMING_SNAKE_CASE__     :   Dict		      =     True






				def 			snake_case_    (  self					):

										"""simple docstring"""




										super().setUp()

										# We have a SentencePiece fixture for testing
										__SCREAMING_SNAKE_CASE:				Dict           =   XLNetTokenizer(_lowerCAmelCase			, keep_accents=_lowerCAmelCase					)
										tokenizer.sanitize_special_tokens()
										tokenizer.save_pretrained(self.tmpdirname					)






				def 			snake_case_    (  self					):

										"""simple docstring"""




										__SCREAMING_SNAKE_CASE:				List[Any]           =   '''<s>'''
										__SCREAMING_SNAKE_CASE:				int           =   1

										self.assertEqual(self.get_tokenizer()._convert_token_to_id(_lowerCAmelCase					)			, _lowerCAmelCase					)
										self.assertEqual(self.get_tokenizer()._convert_id_to_token(_lowerCAmelCase					)			, _lowerCAmelCase					)






				def 			snake_case_    (  self					):

										"""simple docstring"""




										__SCREAMING_SNAKE_CASE:				Any           =   list(self.get_tokenizer().get_vocab().keys()					)

										self.assertEqual(vocab_keys[0]			, '''<unk>'''					)
										self.assertEqual(vocab_keys[1]			, '''<s>'''					)
										self.assertEqual(vocab_keys[-1]			, '''<eod>'''					)
										self.assertEqual(len(_lowerCAmelCase					)			, 1006					)






				def 			snake_case_    (  self					):

										"""simple docstring"""




										self.assertEqual(self.get_tokenizer().vocab_size			, 1000					)






				def 			snake_case_    (  self					):

										"""simple docstring"""




										__SCREAMING_SNAKE_CASE:				Dict           =   XLNetTokenizer(_lowerCAmelCase			, keep_accents=_lowerCAmelCase					)

										__SCREAMING_SNAKE_CASE:				Union[str, Any]           =   tokenizer.tokenize('''This is a test'''					)
										self.assertListEqual(_lowerCAmelCase			, ['''▁This''', '''▁is''', '''▁a''', '''▁t''', '''est''']					)

										self.assertListEqual(tokenizer.convert_tokens_to_ids(_lowerCAmelCase					)			, [285, 46, 10, 170, 382]					)

										__SCREAMING_SNAKE_CASE:				int           =   tokenizer.tokenize('''I was born in 92000, and this is falsé.'''					)
										self.assertListEqual(
										    _lowerCAmelCase			, [
										        SPIECE_UNDERLINE + '''I''',
										        SPIECE_UNDERLINE + '''was''',
										        SPIECE_UNDERLINE + '''b''',
										        '''or''',
										        '''n''',
										        SPIECE_UNDERLINE + '''in''',
										        SPIECE_UNDERLINE + '''''',
										        '''9''',
										        '''2''',
										        '''0''',
										        '''0''',
										        '''0''',
										        ''',''',
										        SPIECE_UNDERLINE + '''and''',
										        SPIECE_UNDERLINE + '''this''',
										        SPIECE_UNDERLINE + '''is''',
										        SPIECE_UNDERLINE + '''f''',
										        '''al''',
										        '''s''',
										        '''é''',
										        '''.''',
										    ]			, )
										__SCREAMING_SNAKE_CASE:				str           =   tokenizer.convert_tokens_to_ids(_lowerCAmelCase					)
										self.assertListEqual(_lowerCAmelCase			, [8, 21, 84, 55, 24, 19, 7, 0, 602, 347, 347, 347, 3, 12, 66, 46, 72, 80, 6, 0, 4]					)

										__SCREAMING_SNAKE_CASE:				int           =   tokenizer.convert_ids_to_tokens(_lowerCAmelCase					)
										self.assertListEqual(
										    _lowerCAmelCase			, [
										        SPIECE_UNDERLINE + '''I''',
										        SPIECE_UNDERLINE + '''was''',
										        SPIECE_UNDERLINE + '''b''',
										        '''or''',
										        '''n''',
										        SPIECE_UNDERLINE + '''in''',
										        SPIECE_UNDERLINE + '''''',
										        '''<unk>''',
										        '''2''',
										        '''0''',
										        '''0''',
										        '''0''',
										        ''',''',
										        SPIECE_UNDERLINE + '''and''',
										        SPIECE_UNDERLINE + '''this''',
										        SPIECE_UNDERLINE + '''is''',
										        SPIECE_UNDERLINE + '''f''',
										        '''al''',
										        '''s''',
										        '''<unk>''',
										        '''.''',
										    ]			, )






				def 			snake_case_    (  self					):

										"""simple docstring"""




										__SCREAMING_SNAKE_CASE:				Union[str, Any]           =   XLNetTokenizer(_lowerCAmelCase			, do_lower_case=_lowerCAmelCase					)
										__SCREAMING_SNAKE_CASE:				Optional[Any]           =   tokenizer.tokenize('''I was born in 92000, and this is falsé.'''					)
										self.assertListEqual(
										    _lowerCAmelCase			, [
										        SPIECE_UNDERLINE + '''''',
										        '''i''',
										        SPIECE_UNDERLINE + '''was''',
										        SPIECE_UNDERLINE + '''b''',
										        '''or''',
										        '''n''',
										        SPIECE_UNDERLINE + '''in''',
										        SPIECE_UNDERLINE + '''''',
										        '''9''',
										        '''2''',
										        '''0''',
										        '''0''',
										        '''0''',
										        ''',''',
										        SPIECE_UNDERLINE + '''and''',
										        SPIECE_UNDERLINE + '''this''',
										        SPIECE_UNDERLINE + '''is''',
										        SPIECE_UNDERLINE + '''f''',
										        '''al''',
										        '''se''',
										        '''.''',
										    ]			, )
										self.assertListEqual(tokenizer.tokenize('''H\u00E9llo'''					)			, ['''▁he''', '''ll''', '''o''']					)






				def 			snake_case_    (  self					):

										"""simple docstring"""




										__SCREAMING_SNAKE_CASE:				Union[str, Any]           =   XLNetTokenizer(_lowerCAmelCase			, do_lower_case=_lowerCAmelCase					)
										__SCREAMING_SNAKE_CASE:				Optional[int]           =   tokenizer.tokenize('''I was born in 92000, and this is falsé.'''					)
										self.assertListEqual(
										    _lowerCAmelCase			, [
										        SPIECE_UNDERLINE + '''I''',
										        SPIECE_UNDERLINE + '''was''',
										        SPIECE_UNDERLINE + '''b''',
										        '''or''',
										        '''n''',
										        SPIECE_UNDERLINE + '''in''',
										        SPIECE_UNDERLINE + '''''',
										        '''9''',
										        '''2''',
										        '''0''',
										        '''0''',
										        '''0''',
										        ''',''',
										        SPIECE_UNDERLINE + '''and''',
										        SPIECE_UNDERLINE + '''this''',
										        SPIECE_UNDERLINE + '''is''',
										        SPIECE_UNDERLINE + '''f''',
										        '''al''',
										        '''se''',
										        '''.''',
										    ]			, )






				@slow
				def 			snake_case_    (  self					):

										"""simple docstring"""




										__SCREAMING_SNAKE_CASE:				Union[str, Any]           =   XLNetTokenizer.from_pretrained('''xlnet-base-cased'''					)

										__SCREAMING_SNAKE_CASE:				List[Any]           =   tokenizer.encode('''sequence builders'''			, add_special_tokens=_lowerCAmelCase					)
										__SCREAMING_SNAKE_CASE:				Any           =   tokenizer.encode('''multi-sequence build'''			, add_special_tokens=_lowerCAmelCase					)

										__SCREAMING_SNAKE_CASE:				Tuple           =   tokenizer.build_inputs_with_special_tokens(_lowerCAmelCase					)
										__SCREAMING_SNAKE_CASE:				List[str]           =   tokenizer.build_inputs_with_special_tokens(_lowerCAmelCase			, _lowerCAmelCase					)

										assert encoded_sentence == text + [4, 3]
										assert encoded_pair == text + [4] + text_a + [4, 3]






				@slow
				def 			snake_case_    (  self					):

										"""simple docstring"""




										__SCREAMING_SNAKE_CASE:				str           =   {'''input_ids''': [[17, 21442, 270, 17, 10, 14645, 318, 34, 17, 4546, 3145, 787, 13, 7752, 22018, 23, 21, 17, 4546, 3145, 787, 13, 3352, 14431, 13, 5500, 11, 1176, 580, 13, 16819, 4797, 23, 17, 10, 17135, 658, 19, 457, 7932, 13, 184, 19, 3154, 17135, 6468, 19, 1404, 12269, 19, 4229, 5356, 16264, 46, 19, 17, 20545, 10395, 9, 9, 9, 11, 28, 6421, 9531, 20729, 17, 10, 353, 17022, 11, 21, 6421, 9531, 16949, 17, 10, 11509, 753, 11, 33, 95, 2421, 7385, 956, 14431, 2626, 25, 842, 7385, 4836, 21, 1429, 2272, 9855, 3120, 161, 24738, 19, 13203, 658, 218, 787, 21, 430, 18482, 847, 2637, 9, 4, 3], [5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]}  # noqa: E501
										# fmt: on

										self.tokenizer_integration_test_util(
										    expected_encoding=_lowerCAmelCase			, model_name='''xlnet-base-cased'''			, revision='''c841166438c31ec7ca9a106dee7bb312b73ae511'''			, )










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



