


def   _lowerCAmelCase				(   UpperCamelCase__:     str							,					UpperCamelCase__:     str						) ->   float:





	"""simple docstring"""







	def get_matched_characters(UpperCamelCase__:     str							,					UpperCamelCase__:     str						) -> str:
		A								=				[]
		A								=				min(len(_stra						)							,					len(_stra						)						) // 2
		for i, l in enumerate(_stra						):
			A								=				int(max(0							,					i - limit						)						)
			A								=				int(min(i + limit + 1							,					len(_stra						)						)						)
			if l in _stra[left:right]:
				matched.append(UpperCamelCase__						)
				A								=				f'{_stra[0:_stra.index(UpperCamelCase__						)]} {_stra[_stra.index(UpperCamelCase__						) + 1:]}'

		return "".join(UpperCamelCase__						)

	# matching characters
	A								=				get_matched_characters(UpperCamelCase__							,					UpperCamelCase__						)
	A								=				get_matched_characters(UpperCamelCase__							,					UpperCamelCase__						)
	A								=				len(UpperCamelCase__						)

	# transposition
	A								=				(
	    len([(ca, ca) for ca, ca in zip(UpperCamelCase__							,					UpperCamelCase__						) if ca != ca]						) // 2
	)

	if not match_count:
		A								=				0.0
	else:
		A								=				(
		    1
		    / 3
		    * (
		        match_count / len(UpperCamelCase__						)
		        + match_count / len(UpperCamelCase__						)
		        + (match_count - transpositions) / match_count
		    )
		)

	# common prefix up to 4 characters
	A								=				0
	for ca, ca in zip(stra[:4]							,					stra[:4]						):
		if ca == ca:
			prefix_len += 1
		else:
			break

	return jaro + 0.1 * prefix_len * (1 - jaro)


if __name__ == "__main__":
				import doctest

				doctest.testmod()
				print(jaro_winkler("hello", "world"))







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       _UpperCamelCase    (					__snake_case    ,		unittest.TestCase				):

				"""simple docstring"""







				lowerCAmelCase 		= ConsistencyModelPipeline
				lowerCAmelCase 		= UNCONDITIONAL_IMAGE_GENERATION_PARAMS
				lowerCAmelCase 		= UNCONDITIONAL_IMAGE_GENERATION_BATCH_PARAMS

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

				@property
				def      _UpperCAmelCase					(						self   )	->				Any:
					A								=				UNetaDModel.from_pretrained(
					    """diffusers/consistency-models-test"""				,   subfolder="""test_unet"""				,   )
					return unet

				@property
				def      _UpperCAmelCase					(						self   )	->				str:
					A								=				UNetaDModel.from_pretrained(
					    """diffusers/consistency-models-test"""				,   subfolder="""test_unet_class_cond"""				,   )
					return unet

				def      _UpperCAmelCase					(						self				,   a__=False   )	->				Tuple:
					if class_cond:
						A								=				self.dummy_cond_unet
					else:
						A								=				self.dummy_uncond_unet

					# Default to CM multistep sampler
					A								=				CMStochasticIterativeScheduler(
					    num_train_timesteps=40				,   sigma_min=0.0_02				,   sigma_max=80.0				,   )

					A								=				{
					    """unet""": unet,
					    """scheduler""": scheduler,
					}

					return components

				def      _UpperCAmelCase					(						self				,   a__				,   a__=0   )	->				Tuple:
					if str(a__   ).startswith("""mps"""   ):
						A								=				torch.manual_seed(a__   )
					else:
						A								=				torch.Generator(device=a__   ).manual_seed(a__   )

					A								=				{
					    """batch_size""": 1,
					    """num_inference_steps""": None,
					    """timesteps""": [22, 0],
					    """generator""": generator,
					    """output_type""": """np""",
					}

					return inputs

				def      _UpperCAmelCase					(						self   )	->				str:
					A								=				"""cpu"""  # ensure determinism for the device-dependent torch.Generator
					A								=				self.get_dummy_components()
					A								=				ConsistencyModelPipeline(**a__   )
					A								=				pipe.to(a__   )
					pipe.set_progress_bar_config(disable=a__   )

					A								=				self.get_dummy_inputs(a__   )
					A								=				pipe(**a__   ).images
					assert image.shape == (1, 32, 32, 3)

					A								=				image[0, -3:, -3:, -1]
					A								=				np.array([0.35_72, 0.62_73, 0.40_31, 0.39_61, 0.43_21, 0.57_30, 0.52_66, 0.47_80, 0.50_04]   )

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

				def      _UpperCAmelCase					(						self   )	->				Optional[int]:
					A								=				"""cpu"""  # ensure determinism for the device-dependent torch.Generator
					A								=				self.get_dummy_components(class_cond=a__   )
					A								=				ConsistencyModelPipeline(**a__   )
					A								=				pipe.to(a__   )
					pipe.set_progress_bar_config(disable=a__   )

					A								=				self.get_dummy_inputs(a__   )
					A								=				0
					A								=				pipe(**a__   ).images
					assert image.shape == (1, 32, 32, 3)

					A								=				image[0, -3:, -3:, -1]
					A								=				np.array([0.35_72, 0.62_73, 0.40_31, 0.39_61, 0.43_21, 0.57_30, 0.52_66, 0.47_80, 0.50_04]   )

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

				def      _UpperCAmelCase					(						self   )	->				Optional[int]:
					A								=				"""cpu"""  # ensure determinism for the device-dependent torch.Generator
					A								=				self.get_dummy_components()
					A								=				ConsistencyModelPipeline(**a__   )
					A								=				pipe.to(a__   )
					pipe.set_progress_bar_config(disable=a__   )

					A								=				self.get_dummy_inputs(a__   )
					A								=				1
					A								=				None
					A								=				pipe(**a__   ).images
					assert image.shape == (1, 32, 32, 3)

					A								=				image[0, -3:, -3:, -1]
					A								=				np.array([0.50_04, 0.50_04, 0.49_94, 0.50_08, 0.49_76, 0.50_18, 0.49_90, 0.49_82, 0.49_87]   )

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

				def      _UpperCAmelCase					(						self   )	->				str:
					A								=				"""cpu"""  # ensure determinism for the device-dependent torch.Generator
					A								=				self.get_dummy_components(class_cond=a__   )
					A								=				ConsistencyModelPipeline(**a__   )
					A								=				pipe.to(a__   )
					pipe.set_progress_bar_config(disable=a__   )

					A								=				self.get_dummy_inputs(a__   )
					A								=				1
					A								=				None
					A								=				0
					A								=				pipe(**a__   ).images
					assert image.shape == (1, 32, 32, 3)

					A								=				image[0, -3:, -3:, -1]
					A								=				np.array([0.50_04, 0.50_04, 0.49_94, 0.50_08, 0.49_76, 0.50_18, 0.49_90, 0.49_82, 0.49_87]   )

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






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

				"""simple docstring"""







				def      _UpperCAmelCase					(						self   )	->				Tuple:
					super().tearDown()
					gc.collect()
					torch.cuda.empty_cache()

				def      _UpperCAmelCase					(						self				,   a__=0				,   a__=False				,   a__="cpu"				,   a__=torch.floataa				,   a__=(1, 3, 64, 64)   )	->				str:
					A								=				torch.manual_seed(a__   )

					A								=				{
					    """num_inference_steps""": None,
					    """timesteps""": [22, 0],
					    """class_labels""": 0,
					    """generator""": generator,
					    """output_type""": """np""",
					}

					if get_fixed_latents:
						A								=				self.get_fixed_latents(seed=a__				,   device=a__				,   dtype=a__				,   shape=a__   )
						A								=				latents

					return inputs

				def      _UpperCAmelCase					(						self				,   a__=0				,   a__="cpu"				,   a__=torch.floataa				,   a__=(1, 3, 64, 64)   )	->				Optional[Any]:
					if type(a__   ) == str:
						A								=				torch.device(a__   )
					A								=				torch.Generator(device=a__   ).manual_seed(a__   )
					A								=				randn_tensor(a__				,   generator=a__				,   device=a__				,   dtype=a__   )
					return latents

				def      _UpperCAmelCase					(						self   )	->				Tuple:
					A								=				UNetaDModel.from_pretrained("""diffusers/consistency_models"""				,   subfolder="""diffusers_cd_imagenet64_l2"""   )
					A								=				CMStochasticIterativeScheduler(
					    num_train_timesteps=40				,   sigma_min=0.0_02				,   sigma_max=80.0				,   )
					A								=				ConsistencyModelPipeline(unet=a__				,   scheduler=a__   )
					pipe.to(torch_device=a__   )
					pipe.set_progress_bar_config(disable=a__   )

					A								=				self.get_inputs()
					A								=				pipe(**a__   ).images
					assert image.shape == (1, 64, 64, 3)

					A								=				image[0, -3:, -3:, -1]

					A								=				np.array([0.08_88, 0.08_81, 0.06_66, 0.04_79, 0.02_92, 0.01_95, 0.02_01, 0.01_63, 0.02_54]   )

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

				def      _UpperCAmelCase					(						self   )	->				Dict:
					A								=				UNetaDModel.from_pretrained("""diffusers/consistency_models"""				,   subfolder="""diffusers_cd_imagenet64_l2"""   )
					A								=				CMStochasticIterativeScheduler(
					    num_train_timesteps=40				,   sigma_min=0.0_02				,   sigma_max=80.0				,   )
					A								=				ConsistencyModelPipeline(unet=a__				,   scheduler=a__   )
					pipe.to(torch_device=a__   )
					pipe.set_progress_bar_config(disable=a__   )

					A								=				self.get_inputs()
					A								=				1
					A								=				None
					A								=				pipe(**a__   ).images
					assert image.shape == (1, 64, 64, 3)

					A								=				image[0, -3:, -3:, -1]

					A								=				np.array([0.03_40, 0.01_52, 0.00_63, 0.02_67, 0.02_21, 0.01_07, 0.04_16, 0.01_86, 0.02_17]   )

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

				@require_torch_a
				def      _UpperCAmelCase					(						self   )	->				Union[str, Any]:
					A								=				UNetaDModel.from_pretrained("""diffusers/consistency_models"""				,   subfolder="""diffusers_cd_imagenet64_l2"""   )
					A								=				CMStochasticIterativeScheduler(
					    num_train_timesteps=40				,   sigma_min=0.0_02				,   sigma_max=80.0				,   )
					A								=				ConsistencyModelPipeline(unet=a__				,   scheduler=a__   )
					pipe.to(torch_device=a__				,   torch_dtype=torch.floataa   )
					pipe.set_progress_bar_config(disable=a__   )

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

					A								=				image[0, -3:, -3:, -1]

					A								=				np.array([0.18_75, 0.14_28, 0.12_89, 0.21_51, 0.20_92, 0.14_77, 0.18_77, 0.16_41, 0.13_53]   )

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

				@require_torch_a
				def      _UpperCAmelCase					(						self   )	->				List[Any]:
					A								=				UNetaDModel.from_pretrained("""diffusers/consistency_models"""				,   subfolder="""diffusers_cd_imagenet64_l2"""   )
					A								=				CMStochasticIterativeScheduler(
					    num_train_timesteps=40				,   sigma_min=0.0_02				,   sigma_max=80.0				,   )
					A								=				ConsistencyModelPipeline(unet=a__				,   scheduler=a__   )
					pipe.to(torch_device=a__				,   torch_dtype=torch.floataa   )
					pipe.set_progress_bar_config(disable=a__   )

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

					A								=				image[0, -3:, -3:, -1]

					A								=				np.array([0.16_63, 0.19_48, 0.22_75, 0.16_80, 0.12_04, 0.12_45, 0.18_58, 0.13_38, 0.20_95]   )

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



