

"""simple docstring"""


import gc
import unittest

from diffusers import FlaxDPMSolverMultistepScheduler, FlaxStableDiffusionPipeline
from diffusers.utils import is_flax_available, slow
from diffusers.utils.testing_utils import require_flax


if is_flax_available():
  import jax
  import jax.numpy as jnp
  from flax.jax_utils import replicate
  from flax.training.common_utils import shard

@slow
@require_flax
class      __a							(unittest.TestCase):






     '''simple docstring'''

     def 					_a			(						self  )  ->      Dict:


           """simple docstring"""


           super().tearDown()
           gc.collect()

     def 					_a			(						self  )  ->      Dict:


           """simple docstring"""


           SCREAMING_SNAKE_CASE__		,		SCREAMING_SNAKE_CASE__   :      Any      		=	FlaxStableDiffusionPipeline.from_pretrained(
               """stabilityai/stable-diffusion-2"""	,    revision="""bf16"""	,    dtype=jnp.bfloataa	,    )

           SCREAMING_SNAKE_CASE__   :      Optional[Any]      		=	"""A painting of a squirrel eating a burger"""
           SCREAMING_SNAKE_CASE__   :      Optional[Any]      		=	jax.device_count()
           SCREAMING_SNAKE_CASE__   :      Any      		=	num_samples * [prompt]
           SCREAMING_SNAKE_CASE__   :      Dict      		=	sd_pipe.prepare_inputs(_a  )

           SCREAMING_SNAKE_CASE__   :      Dict      		=	replicate(_a  )
           SCREAMING_SNAKE_CASE__   :      Tuple      		=	shard(_a  )

           SCREAMING_SNAKE_CASE__   :      int      		=	jax.random.PRNGKey(0  )
           SCREAMING_SNAKE_CASE__   :      Optional[int]      		=	jax.random.split(_a	,    jax.device_count()  )

           SCREAMING_SNAKE_CASE__   :      int      		=	sd_pipe(_a	,    _a	,    _a	,    num_inference_steps=25	,    jit=_a  )[0]
           assert images.shape == (jax.device_count(), 1, 768, 768, 3)

           SCREAMING_SNAKE_CASE__   :      int      		=	images.reshape((images.shape[0] * images.shape[1],) + images.shape[-3:]  )
           SCREAMING_SNAKE_CASE__   :      int      		=	images[0, 253:256, 253:256, -1]

           SCREAMING_SNAKE_CASE__   :      Optional[int]      		=	jnp.asarray(jax.device_get(image_slice.flatten()  )  )
           SCREAMING_SNAKE_CASE__   :      Optional[int]      		=	jnp.array([0.4_238, 0.4_414, 0.4_395, 0.4_453, 0.4_629, 0.4_590, 0.4_531, 0.45_508, 0.4_512]  )
           print(f'''output_slice: {output_slice}'''  )
           assert jnp.abs(output_slice - expected_slice  ).max() < 1E-2





     def 					_a			(						self  )  ->      str:


           """simple docstring"""


           SCREAMING_SNAKE_CASE__   :      str      		=	"""stabilityai/stable-diffusion-2"""
           SCREAMING_SNAKE_CASE__		,		SCREAMING_SNAKE_CASE__   :      Tuple      		=	FlaxDPMSolverMultistepScheduler.from_pretrained(_a	,    subfolder="""scheduler"""  )
           SCREAMING_SNAKE_CASE__		,		SCREAMING_SNAKE_CASE__   :      int      		=	FlaxStableDiffusionPipeline.from_pretrained(
               _a	,    scheduler=_a	,    revision="""bf16"""	,    dtype=jnp.bfloataa	,    )
           SCREAMING_SNAKE_CASE__   :      int      		=	scheduler_params

           SCREAMING_SNAKE_CASE__   :      Tuple      		=	"""A painting of a squirrel eating a burger"""
           SCREAMING_SNAKE_CASE__   :      str      		=	jax.device_count()
           SCREAMING_SNAKE_CASE__   :      int      		=	num_samples * [prompt]
           SCREAMING_SNAKE_CASE__   :      Optional[int]      		=	sd_pipe.prepare_inputs(_a  )

           SCREAMING_SNAKE_CASE__   :      str      		=	replicate(_a  )
           SCREAMING_SNAKE_CASE__   :      Optional[int]      		=	shard(_a  )

           SCREAMING_SNAKE_CASE__   :      List[Any]      		=	jax.random.PRNGKey(0  )
           SCREAMING_SNAKE_CASE__   :      Optional[int]      		=	jax.random.split(_a	,    jax.device_count()  )

           SCREAMING_SNAKE_CASE__   :      Tuple      		=	sd_pipe(_a	,    _a	,    _a	,    num_inference_steps=25	,    jit=_a  )[0]
           assert images.shape == (jax.device_count(), 1, 768, 768, 3)

           SCREAMING_SNAKE_CASE__   :      Optional[int]      		=	images.reshape((images.shape[0] * images.shape[1],) + images.shape[-3:]  )
           SCREAMING_SNAKE_CASE__   :      int      		=	images[0, 253:256, 253:256, -1]

           SCREAMING_SNAKE_CASE__   :      int      		=	jnp.asarray(jax.device_get(image_slice.flatten()  )  )
           SCREAMING_SNAKE_CASE__   :      Tuple      		=	jnp.array([0.4_336, 0.42_969, 0.4_453, 0.4_199, 0.4_297, 0.4_531, 0.4_434, 0.4_434, 0.4_297]  )
           print(f'''output_slice: {output_slice}'''  )
           assert jnp.abs(output_slice - expected_slice  ).max() < 1E-2







"""simple docstring"""


def  _lowercase					(    __lowerCAmelCase  ,					__lowerCAmelCase							)       ->				bool:
      SCREAMING_SNAKE_CASE__   :      Optional[Any]      		=	len(__lowerCAmelCase							) + 1
      SCREAMING_SNAKE_CASE__   :      int      		=	len(__lowerCAmelCase							) + 1

      # dp is a 2d matrix where dp[i][j] denotes whether prefix string of
      # length i of input_string matches with prefix string of length j of
      # given pattern.
      # "dp" stands for dynamic programming.
      SCREAMING_SNAKE_CASE__   :      Dict      		=	[[0 for i in range(__lowerCAmelCase							)] for j in range(__lowerCAmelCase							)]

      # since string of zero length match pattern of zero length
      SCREAMING_SNAKE_CASE__   :      Dict      		=	1

      # since pattern of zero length will never match with string of non-zero length
      for i in range(1  ,					__lowerCAmelCase							):
            SCREAMING_SNAKE_CASE__   :      Union[str, Any]      		=	0

      # since string of zero length will match with pattern where there
      # is at least one * alternatively
      for j in range(1  ,					__lowerCAmelCase							):
            SCREAMING_SNAKE_CASE__   :      int      		=	dp[0][j - 2] if pattern[j - 1] == """*""" else 0

      # now using bottom-up approach to find for all remaining lengths
      for i in range(1  ,					__lowerCAmelCase							):
            for j in range(1  ,					__lowerCAmelCase							):
                  if input_string[i - 1] == pattern[j - 1] or pattern[j - 1] == ".":
                        SCREAMING_SNAKE_CASE__   :      Any      		=	dp[i - 1][j - 1]

                  elif pattern[j - 1] == "*":
                        if dp[i][j - 2] == 1:
                              SCREAMING_SNAKE_CASE__   :      List[str]      		=	1
                        elif pattern[j - 2] in (input_string[i - 1], "."):
                              SCREAMING_SNAKE_CASE__   :      List[Any]      		=	dp[i - 1][j]
                        else:
                              SCREAMING_SNAKE_CASE__   :      Optional[int]      		=	0
                  else:
                        SCREAMING_SNAKE_CASE__   :      Dict      		=	0

      return bool(dp[-1][-1]							)


if __name__ == "__main__":
  import doctest

  doctest.testmod()
  # inputing the strings
  # input_string = input("input a string :")
  # pattern = input("input a pattern :")

  a					:Any    = "aab"
  a					:Optional[Any]    = "c*a*b"

  # using function to check whether given string matches the given pattern
  if match_pattern(input_string, pattern):
    print(f'{input_string} matches the given pattern {pattern}')
  else:
    print(f'{input_string} does not match with the given pattern {pattern}')




