





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							(		_lowerCamelCase			,       unittest.TestCase     ):
      a						       =							ConsistencyModelPipeline
      a						       =							UNCONDITIONAL_IMAGE_GENERATION_PARAMS
      a						       =							UNCONDITIONAL_IMAGE_GENERATION_BATCH_PARAMS

      # Override required_optional_params to remove num_images_per_prompt
      a						       =							frozenset(
          [
              '''num_inference_steps''',
              '''generator''',
              '''latents''',
              '''output_type''',
              '''return_dict''',
              '''callback''',
              '''callback_steps''',
          ]     )
      @property
      def 		_lowerCamelCase      (		self       ):
         A_     :				int			      =	UNetaDModel.from_pretrained(
             """diffusers/consistency-models-test"""      ,       subfolder="""test_unet"""      ,       )
         return unet
      @property
      def 		_lowerCamelCase      (		self       ):
         A_     :				Tuple			      =	UNetaDModel.from_pretrained(
             """diffusers/consistency-models-test"""      ,       subfolder="""test_unet_class_cond"""      ,       )
         return unet
      def 		_lowerCamelCase      (		self      ,       a__=False       ):
         if class_cond:
            A_     :				List[str]			      =	self.dummy_cond_unet
         else:
            A_     :				Union[str, Any]			      =	self.dummy_uncond_unet

         # Default to CM multistep sampler
         A_     :				List[Any]			      =	CMStochasticIterativeScheduler(
             num_train_timesteps=40      ,       sigma_min=0.002      ,       sigma_max=80.0      ,       )

         A_     :				Optional[Any]			      =	{
             """unet""": unet,
             """scheduler""": scheduler,
         }

         return components
      def 		_lowerCamelCase      (		self      ,       a__      ,       a__=0       ):
         if str(a__       ).startswith("""mps"""       ):
            A_     :				Optional[int]			      =	torch.manual_seed(a__       )
         else:
            A_     :				str			      =	torch.Generator(device=a__       ).manual_seed(a__       )

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

         return inputs
      def 		_lowerCamelCase      (		self       ):
         A_     :				Any			      =	"""cpu"""  # ensure determinism for the device-dependent torch.Generator
         A_     :				List[Any]			      =	self.get_dummy_components()
         A_     :				List[str]			      =	ConsistencyModelPipeline(**a__       )
         A_     :				Any			      =	pipe.to(a__       )
         pipe.set_progress_bar_config(disable=a__       )

         A_     :				Union[str, Any]			      =	self.get_dummy_inputs(a__       )
         A_     :				int			      =	pipe(**a__       ).images
         assert image.shape == (1, 32, 32, 3)

         A_     :				Union[str, Any]			      =	image[0, -3:, -3:, -1]
         A_     :				Union[str, 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 		_lowerCamelCase      (		self       ):
         A_     :				Optional[Any]			      =	"""cpu"""  # ensure determinism for the device-dependent torch.Generator
         A_     :				List[Any]			      =	self.get_dummy_components(class_cond=a__       )
         A_     :				Optional[Any]			      =	ConsistencyModelPipeline(**a__       )
         A_     :				Union[str, Any]			      =	pipe.to(a__       )
         pipe.set_progress_bar_config(disable=a__       )

         A_     :				Dict			      =	self.get_dummy_inputs(a__       )
         A_     :				int			      =	0
         A_     :				Tuple			      =	pipe(**a__       ).images
         assert image.shape == (1, 32, 32, 3)

         A_     :				Optional[Any]			      =	image[0, -3:, -3:, -1]
         A_     :				List[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 		_lowerCamelCase      (		self       ):
         A_     :				str			      =	"""cpu"""  # ensure determinism for the device-dependent torch.Generator
         A_     :				Any			      =	self.get_dummy_components()
         A_     :				Optional[Any]			      =	ConsistencyModelPipeline(**a__       )
         A_     :				int			      =	pipe.to(a__       )
         pipe.set_progress_bar_config(disable=a__       )

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

         A_     :				List[str]			      =	image[0, -3:, -3:, -1]
         A_     :				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 		_lowerCamelCase      (		self       ):
         A_     :				List[str]			      =	"""cpu"""  # ensure determinism for the device-dependent torch.Generator
         A_     :				Tuple			      =	self.get_dummy_components(class_cond=a__       )
         A_     :				List[Any]			      =	ConsistencyModelPipeline(**a__       )
         A_     :				List[str]			      =	pipe.to(a__       )
         pipe.set_progress_bar_config(disable=a__       )

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

         A_     :				int			      =	image[0, -3:, -3:, -1]
         A_     :				List[str]			      =	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    _UpperCAmelCase							(		unittest.TestCase     ):
      def 		_lowerCamelCase      (		self       ):
         super().tearDown()
         gc.collect()
         torch.cuda.empty_cache()
      def 		_lowerCamelCase      (		self      ,       a__=0      ,       a__=False      ,       a__="cpu"      ,       a__=torch.floataa      ,       a__=(1, 3, 64, 64)       ):
         A_     :				List[str]			      =	torch.manual_seed(a__       )

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

         if get_fixed_latents:
            A_     :				str			      =	self.get_fixed_latents(seed=a__      ,       device=a__      ,       dtype=a__      ,       shape=a__       )
            A_     :				Optional[Any]			      =	latents

         return inputs
      def 		_lowerCamelCase      (		self      ,       a__=0      ,       a__="cpu"      ,       a__=torch.floataa      ,       a__=(1, 3, 64, 64)       ):
         if type(a__       ) == str:
            A_     :				List[str]			      =	torch.device(a__       )
         A_     :				Any			      =	torch.Generator(device=a__       ).manual_seed(a__       )
         A_     :				str			      =	randn_tensor(a__      ,       generator=a__      ,       device=a__      ,       dtype=a__       )
         return latents
      def 		_lowerCamelCase      (		self       ):
         A_     :				int			      =	UNetaDModel.from_pretrained("""diffusers/consistency_models"""      ,       subfolder="""diffusers_cd_imagenet64_l2"""       )
         A_     :				Optional[int]			      =	CMStochasticIterativeScheduler(
             num_train_timesteps=40      ,       sigma_min=0.002      ,       sigma_max=80.0      ,       )
         A_     :				Dict			      =	ConsistencyModelPipeline(unet=a__      ,       scheduler=a__       )
         pipe.to(torch_device=a__       )
         pipe.set_progress_bar_config(disable=a__       )

         A_     :				List[Any]			      =	self.get_inputs()
         A_     :				str			      =	pipe(**a__       ).images
         assert image.shape == (1, 64, 64, 3)

         A_     :				List[str]			      =	image[0, -3:, -3:, -1]

         A_     :				Optional[int]			      =	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 		_lowerCamelCase      (		self       ):
         A_     :				Dict			      =	UNetaDModel.from_pretrained("""diffusers/consistency_models"""      ,       subfolder="""diffusers_cd_imagenet64_l2"""       )
         A_     :				Tuple			      =	CMStochasticIterativeScheduler(
             num_train_timesteps=40      ,       sigma_min=0.002      ,       sigma_max=80.0      ,       )
         A_     :				List[Any]			      =	ConsistencyModelPipeline(unet=a__      ,       scheduler=a__       )
         pipe.to(torch_device=a__       )
         pipe.set_progress_bar_config(disable=a__       )

         A_     :				Dict			      =	self.get_inputs()
         A_     :				Dict			      =	1
         A_     :				Optional[int]			      =	None
         A_     :				Dict			      =	pipe(**a__       ).images
         assert image.shape == (1, 64, 64, 3)

         A_     :				List[str]			      =	image[0, -3:, -3:, -1]

         A_     :				Optional[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 		_lowerCamelCase      (		self       ):
         A_     :				str			      =	UNetaDModel.from_pretrained("""diffusers/consistency_models"""      ,       subfolder="""diffusers_cd_imagenet64_l2"""       )
         A_     :				Union[str, Any]			      =	CMStochasticIterativeScheduler(
             num_train_timesteps=40      ,       sigma_min=0.002      ,       sigma_max=80.0      ,       )
         A_     :				Any			      =	ConsistencyModelPipeline(unet=a__      ,       scheduler=a__       )
         pipe.to(torch_device=a__      ,       torch_dtype=torch.floataa       )
         pipe.set_progress_bar_config(disable=a__       )

         A_     :				Optional[int]			      =	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_     :				Dict			      =	pipe(**a__       ).images
         assert image.shape == (1, 64, 64, 3)

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

         A_     :				Any			      =	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 		_lowerCamelCase      (		self       ):
         A_     :				Optional[int]			      =	UNetaDModel.from_pretrained("""diffusers/consistency_models"""      ,       subfolder="""diffusers_cd_imagenet64_l2"""       )
         A_     :				Dict			      =	CMStochasticIterativeScheduler(
             num_train_timesteps=40      ,       sigma_min=0.002      ,       sigma_max=80.0      ,       )
         A_     :				Tuple			      =	ConsistencyModelPipeline(unet=a__      ,       scheduler=a__       )
         pipe.to(torch_device=a__      ,       torch_dtype=torch.floataa       )
         pipe.set_progress_bar_config(disable=a__       )

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

         A_     :				Optional[int]			      =	image[0, -3:, -3:, -1]

         A_     :				Optional[int]			      =	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













import re



def     _lowerCAmelCase		(  _lowerCAmelCase     ):
   '''simple docstring'''


   A_     :				Any			      =	re.compile(r"""^(\+91[\-\s]?)?[0]?(91)?[789]\d{9}$"""     )
   if match := re.search(_lowerCAmelCase  ,_lowerCAmelCase     ):
      return match.string == phone
   return False


if __name__ == "__main__":
      print(indian_phone_validator("""+918827897895"""))






