

'''simple docstring'''

import gc
import tempfile
import unittest

import numpy as np
import torch

from diffusers import VersatileDiffusionDualGuidedPipeline
from diffusers.utils.testing_utils import load_image, nightly, require_torch_gpu, torch_device


lowercase__            =     False




@nightly
@require_torch_gpu
class 			A_					(							unittest.TestCase				):






     '''simple docstring'''



     def   UpperCAmelCase_			(						self					:	int			)		->      Optional[int]:
      # clean up the VRAM after each test
      super().tearDown()
      gc.collect()
      torch.cuda.empty_cache()


     def   UpperCAmelCase_			(						self					:	int			)		->      int:
      UpperCAmelCase			:      Tuple		   =					VersatileDiffusionDualGuidedPipeline.from_pretrained('shi-labs/versatile-diffusion'			)
      # remove text_unet
      pipe.remove_unused_weights()
      pipe.to(lowercase_			)
      pipe.set_progress_bar_config(disable=lowercase_			)

      UpperCAmelCase			:      Union[str, Any]		   =					load_image(
          'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg'			)

      UpperCAmelCase			:      List[str]		   =					torch.manual_seed(0			)
      UpperCAmelCase			:      Dict		   =					pipe(
          prompt='first prompt'	,     image=lowercase_	,     text_to_image_strength=0.75	,     generator=lowercase_	,     guidance_scale=7.5	,     num_inference_steps=2	,     output_type='numpy'	,     ).images

      with tempfile.TemporaryDirectory() as tmpdirname:
       pipe.save_pretrained(lowercase_			)
       UpperCAmelCase			:      List[str]		   =					VersatileDiffusionDualGuidedPipeline.from_pretrained(lowercase_			)

      pipe.to(lowercase_			)
      pipe.set_progress_bar_config(disable=lowercase_			)

      UpperCAmelCase			:      Union[str, Any]		   =					generator.manual_seed(0			)
      UpperCAmelCase			:      Tuple		   =					pipe(
          prompt='first prompt'	,     image=lowercase_	,     text_to_image_strength=0.75	,     generator=lowercase_	,     guidance_scale=7.5	,     num_inference_steps=2	,     output_type='numpy'	,     ).images

      assert np.abs(image - new_image			).sum() < 1E-5, "Models don't have the same forward pass"




     def   UpperCAmelCase_			(						self					:	Any			)		->      Optional[int]:
      UpperCAmelCase			:      str		   =					VersatileDiffusionDualGuidedPipeline.from_pretrained('shi-labs/versatile-diffusion'			)
      pipe.remove_unused_weights()
      pipe.to(lowercase_			)
      pipe.set_progress_bar_config(disable=lowercase_			)

      UpperCAmelCase			:      List[Any]		   =					'cyberpunk 2077'
      UpperCAmelCase			:      str		   =					load_image(
          'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg'			)
      UpperCAmelCase			:      Dict		   =					torch.manual_seed(0			)
      UpperCAmelCase			:      Tuple		   =					pipe(
          prompt=lowercase_	,     image=lowercase_	,     text_to_image_strength=0.75	,     generator=lowercase_	,     guidance_scale=7.5	,     num_inference_steps=50	,     output_type='numpy'	,     ).images

      UpperCAmelCase			:      Optional[int]		   =					image[0, 253:256, 253:256, -1]

      assert image.shape == (1, 512, 512, 3)
      UpperCAmelCase			:      str		   =					np.array([0.0787, 0.0849, 0.0826, 0.0812, 0.0807, 0.0795, 0.0818, 0.0798, 0.0779]			)

      assert np.abs(image_slice.flatten() - expected_slice			).max() < 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)




