



'''simple docstring'''







import random
import unittest

import torch

from diffusers import IFSuperResolutionPipeline
from diffusers.utils import floats_tensor
from diffusers.utils.import_utils import is_xformers_available
from diffusers.utils.testing_utils import skip_mps, torch_device

from ..pipeline_params import TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS, TEXT_GUIDED_IMAGE_VARIATION_PARAMS
from ..test_pipelines_common import PipelineTesterMixin
from . import IFPipelineTesterMixin




@skip_mps
class 				__lowerCAmelCase(							lowerCAmelCase__		,			lowerCAmelCase__		,			unittest.TestCase      ):
			__snake_case  :		List[Any]		=      IFSuperResolutionPipeline
			__snake_case  :		Optional[Any]		=      TEXT_GUIDED_IMAGE_VARIATION_PARAMS - {'width', 'height'}
			__snake_case  :		Optional[int]		=      TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS
			__snake_case  :		Dict		=      PipelineTesterMixin.required_optional_params - {'latents'}






			def 				_lowercase	(   self  :							str	):




					"""simple docstring"""







					return self._get_superresolution_dummy_components()






			def 				_lowercase	(   self  :							Optional[Any]      ,       SCREAMING_SNAKE_CASE  :							Optional[int]      ,       SCREAMING_SNAKE_CASE  :							Optional[Any]=0	):




					"""simple docstring"""







					if str(SCREAMING_SNAKE_CASE	).startswith('mps'	):
							SCREAMING_SNAKE_CASE_						:List[str]										=      torch.manual_seed(SCREAMING_SNAKE_CASE	)
					else:
							SCREAMING_SNAKE_CASE_						:Optional[Any]										=      torch.Generator(device=SCREAMING_SNAKE_CASE	).manual_seed(SCREAMING_SNAKE_CASE	)

					SCREAMING_SNAKE_CASE_						:Any										=      floats_tensor((1, 3, 32, 32)      ,       rng=random.Random(SCREAMING_SNAKE_CASE	)	).to(SCREAMING_SNAKE_CASE	)

					SCREAMING_SNAKE_CASE_						:List[Any]										=      {
					    'prompt': 'A painting of a squirrel eating a burger',
					    'image': image,
					    'generator': generator,
					    'num_inference_steps': 2,
					    'output_type': 'numpy',
					}

					return inputs






			@unittest.skipIf(
			    torch_device != 'cuda' or not is_xformers_available()      ,       reason='XFormers attention is only available with CUDA and `xformers` installed'      ,       )
			def 				_lowercase	(   self  :							Tuple	):




					"""simple docstring"""







					self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1E-3	)






			def 				_lowercase	(   self  :							Optional[int]	):




					"""simple docstring"""







					self._test_save_load_optional_components()






			@unittest.skipIf(torch_device != 'cuda'      ,       reason='float16 requires CUDA'	)
			def 				_lowercase	(   self  :							str	):




					"""simple docstring"""







					super().test_save_load_floataa(expected_max_diff=1E-1	)






			def 				_lowercase	(   self  :							Optional[int]	):




					"""simple docstring"""







					self._test_attention_slicing_forward_pass(expected_max_diff=1E-2	)






			def 				_lowercase	(   self  :							Optional[Any]	):




					"""simple docstring"""







					self._test_save_load_local()






			def 				_lowercase	(   self  :							str	):




					"""simple docstring"""







					self._test_inference_batch_single_identical(
					    expected_max_diff=1E-2      ,       )



import functools
from math import sqrt
from time import time



def  snake_case_(_UpperCamelCase					,			*_UpperCamelCase					,			**_UpperCamelCase	)				->      str:



  """simple docstring"""






  _snake_case		=				time()
  _snake_case		=				func(*_UpperCamelCase					,			**_UpperCamelCase	)
  _snake_case		=				time()
  if int(end - start	) > 0:
    print(F"""{func.__name__} runtime: {(end - start):0.4f} s"""	)
  else:
    print(F"""{func.__name__} runtime: {(end - start) * 1_000:0.4f} ms"""	)
  return output



def  snake_case_(_UpperCamelCase	)				->      list[int]:



  """simple docstring"""






  if n < 0:
    raise Exception('''n is negative'''	)
  if n == 0:
    return [0]
  _snake_case		=				[0, 1]
  for _ in range(n - 1	):
    fib.append(fib[-1] + fib[-2]	)
  return fib



def  snake_case_(_UpperCamelCase	)				->      list[int]:



  """simple docstring"""






  def fib_recursive_term(_UpperCamelCase	) -> int:
    if i < 0:
      raise Exception('''n is negative'''	)
    if i < 2:
      return i
    return fib_recursive_term(i - 1	) + fib_recursive_term(i - 2	)

  if n < 0:
    raise Exception('''n is negative'''	)
  return [fib_recursive_term(_UpperCamelCase	) for i in range(n + 1	)]



def  snake_case_(_UpperCamelCase	)				->      list[int]:



  """simple docstring"""






  @functools.cache
  def fib_recursive_term(_UpperCamelCase	) -> int:
    if i < 0:
      raise Exception('''n is negative'''	)
    if i < 2:
      return i
    return fib_recursive_term(i - 1	) + fib_recursive_term(i - 2	)

  if n < 0:
    raise Exception('''n is negative'''	)
  return [fib_recursive_term(_UpperCamelCase	) for i in range(n + 1	)]



def  snake_case_(_UpperCamelCase	)				->      list[int]:



  """simple docstring"""






  if n < 0:
    raise Exception('''n is negative'''	)
  # Cache must be outside recursuive function
  # other it will reset every time it calls itself.
  _snake_case		=				{0: 0, 1: 1, 2: 1}  # Prefilled cache

  def rec_fn_memoized(_UpperCamelCase	) -> int:
    if num in cache:
      return cache[num]

    _snake_case		=				rec_fn_memoized(num - 1	) + rec_fn_memoized(num - 2	)
    _snake_case		=				value
    return value

  return [rec_fn_memoized(_UpperCamelCase	) for i in range(n + 1	)]



def  snake_case_(_UpperCamelCase	)				->      list[int]:



  """simple docstring"""






  if n < 0:
    raise Exception('''n is negative'''	)
  if n >= 1_475:
    raise Exception('''n is too large'''	)
  _snake_case		=				sqrt(5	)
  _snake_case		=				(1 + sqrt_a) / 2
  return [round(phi**i / sqrt_a	) for i in range(n + 1	)]


if __name__ == "__main__":
 __A     						=      30
 time_func(fib_iterative, num)
 time_func(fib_recursive, num)  # Around 3s runtime
 time_func(fib_recursive_cached, num)  # Around 0ms runtime
 time_func(fib_memoization, num)
 time_func(fib_binet, num)

