

'''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 			A_					(							_snake_case			,  _snake_case			,  unittest.TestCase				):






     '''simple docstring'''



     UpperCAmelCase_		:      Union[str, Any]    						=    IFSuperResolutionPipeline
     UpperCAmelCase_		:      int    						=    TEXT_GUIDED_IMAGE_VARIATION_PARAMS - {"""width""", """height"""}
     UpperCAmelCase_		:      List[Any]    						=    TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS
     UpperCAmelCase_		:      Optional[Any]    						=    PipelineTesterMixin.required_optional_params - {"""latents"""}


     def   UpperCAmelCase_			(						self					:	Optional[int]			)		->      Optional[Any]:
      return self._get_superresolution_dummy_components()


     def   UpperCAmelCase_			(						self					:	Any	,     lowercase_					:	Optional[int]	,     lowercase_					:	Optional[Any]=0			)		->      Tuple:
      if str(lowercase_			).startswith('mps'			):
       UpperCAmelCase			:      Dict		   =					torch.manual_seed(lowercase_			)
      else:
       UpperCAmelCase			:      Optional[int]		   =					torch.Generator(device=lowercase_			).manual_seed(lowercase_			)

      UpperCAmelCase			:      Any		   =					floats_tensor((1, 3, 32, 32)	,     rng=random.Random(lowercase_			)			).to(lowercase_			)

      UpperCAmelCase			:      Union[str, 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   UpperCAmelCase_			(						self					:	List[str]			)		->      Any:
      self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1E-3			)


     def   UpperCAmelCase_			(						self					:	Optional[Any]			)		->      int:
      self._test_save_load_optional_components()


     @unittest.skipIf(torch_device != 'cuda'	,     reason='float16 requires CUDA'			)
     def   UpperCAmelCase_			(						self					:	Dict			)		->      Tuple:
      # Due to non-determinism in save load of the hf-internal-testing/tiny-random-t5 text encoder
      super().test_save_load_floataa(expected_max_diff=1E-1			)


     def   UpperCAmelCase_			(						self					:	Optional[int]			)		->      int:
      self._test_attention_slicing_forward_pass(expected_max_diff=1E-2			)


     def   UpperCAmelCase_			(						self					:	Tuple			)		->      int:
      self._test_save_load_local()




     def   UpperCAmelCase_			(						self					:	Dict			)		->      Union[str, Any]:
      self._test_inference_batch_single_identical(
          expected_max_diff=1E-2	,     )







'''simple docstring'''

from __future__ import annotations




def  UpperCamelCase(	UpperCAmelCase_    ,			UpperCAmelCase_    ,			UpperCAmelCase_      ):
 assert n > 1 and a > 0 and greatest_common_divisor(UpperCAmelCase_    ,			UpperCAmelCase_      ) == 1
 ((UpperCAmelCase)   ,					(UpperCAmelCase)   ,					(UpperCAmelCase))			:      Dict		   =					extended_gcd(UpperCAmelCase_    ,			UpperCAmelCase_      )  # Implemented below
 UpperCAmelCase			:      int		   =					(b * s) % n
 return x




def  UpperCamelCase(	UpperCAmelCase_    ,			UpperCAmelCase_      ):
 ((UpperCAmelCase)   ,					(UpperCAmelCase))			:      int		   =					extended_euclid(UpperCAmelCase_    ,			UpperCAmelCase_      )  # Implemented below
 if b < 0:
  UpperCAmelCase			:      Optional[int]		   =					(b % n + n) % n
 return b




def  UpperCamelCase(	UpperCAmelCase_    ,			UpperCAmelCase_    ,			UpperCAmelCase_      ):
 UpperCAmelCase			:      Tuple		   =					invert_modulo(UpperCAmelCase_    ,			UpperCAmelCase_      )
 UpperCAmelCase			:      List[Any]		   =					(b * s) % n
 return x




def  UpperCamelCase(	UpperCAmelCase_    ,			UpperCAmelCase_      ):
 assert a >= 0 and b >= 0

 if b == 0:
  UpperCAmelCase   ,					UpperCAmelCase   ,					UpperCAmelCase			:      Dict		   =					a, 1, 0
 else:
  ((UpperCAmelCase)   ,					(UpperCAmelCase)   ,					(UpperCAmelCase))			:      Optional[int]		   =					extended_gcd(UpperCAmelCase_    ,			a % b      )
  UpperCAmelCase			:      Dict		   =					q
  UpperCAmelCase			:      int		   =					p - q * (a // b)

 assert a % d == 0 and b % d == 0
 assert d == a * x + b * y

 return (d, x, y)




def  UpperCamelCase(	UpperCAmelCase_    ,			UpperCAmelCase_      ):
 if b == 0:
  return (1, 0)
 ((UpperCAmelCase)   ,					(UpperCAmelCase))			:      Tuple		   =					extended_euclid(UpperCAmelCase_    ,			a % b      )
 UpperCAmelCase			:      Tuple		   =					a // b
 return (y, x - k * y)




def  UpperCamelCase(	UpperCAmelCase_    ,			UpperCAmelCase_      ):
 if a < b:
  UpperCAmelCase   ,					UpperCAmelCase			:      Union[str, Any]		   =					b, a

 while a % b != 0:
  UpperCAmelCase   ,					UpperCAmelCase			:      int		   =					b, a % b

 return b


if __name__ == "__main__":
 from doctest import testmod

 testmod(name="modular_division", verbose=True)
 testmod(name="modular_division2", verbose=True)
 testmod(name="invert_modulo", verbose=True)
 testmod(name="extended_gcd", verbose=True)
 testmod(name="extended_euclid", verbose=True)
 testmod(name="greatest_common_divisor", verbose=True)




