




"""simple docstring"""






import gc
import tempfile
import unittest

import numpy as np
import torch

from diffusers import VersatileDiffusionTextToImagePipeline
from diffusers.utils.testing_utils import nightly, require_torch_gpu, torch_device


A	     =					False




class 			__lowercase     (     unittest.TestCase							):







					'''simple docstring'''




					pass






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







					'''simple docstring'''




					def 	_lowerCamelCase						(  self       ):
									# clean up the VRAM after each test
									super().tearDown()
									gc.collect()
									torch.cuda.empty_cache()







					def 	_lowerCamelCase						(  self       ):
									__a	:  Dict         =					VersatileDiffusionTextToImagePipeline.from_pretrained('''shi-labs/versatile-diffusion'''       )
									# remove text_unet
									pipe.remove_unused_weights()
									pipe.to(_UpperCAmelCase       )
									pipe.set_progress_bar_config(disable=_UpperCAmelCase       )

									__a	:  List[Any]         =					'''A painting of a squirrel eating a burger '''
									__a	:  Union[str, Any]         =					torch.manual_seed(0       )
									__a	:  List[Any]         =					pipe(
									    prompt=_UpperCAmelCase		,     generator=_UpperCAmelCase		,     guidance_scale=7.5		,     num_inference_steps=2		,     output_type='''numpy'''       ).images

									with tempfile.TemporaryDirectory() as tmpdirname:
													pipe.save_pretrained(_UpperCAmelCase       )
													__a	:  Any         =					VersatileDiffusionTextToImagePipeline.from_pretrained(_UpperCAmelCase       )
									pipe.to(_UpperCAmelCase       )
									pipe.set_progress_bar_config(disable=_UpperCAmelCase       )

									__a	:  Union[str, Any]         =					generator.manual_seed(0       )
									__a	:  Any         =					pipe(
									    prompt=_UpperCAmelCase		,     generator=_UpperCAmelCase		,     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 	_lowerCamelCase						(  self       ):
									__a	:  Union[str, Any]         =					VersatileDiffusionTextToImagePipeline.from_pretrained(
									    '''shi-labs/versatile-diffusion'''		,     torch_dtype=torch.floataa       )
									pipe.to(_UpperCAmelCase       )
									pipe.set_progress_bar_config(disable=_UpperCAmelCase       )

									__a	:  Optional[int]         =					'''A painting of a squirrel eating a burger '''
									__a	:  Tuple         =					torch.manual_seed(0       )
									__a	:  int         =					pipe(
									    prompt=_UpperCAmelCase		,     generator=_UpperCAmelCase		,     guidance_scale=7.5		,     num_inference_steps=50		,     output_type='''numpy'''       ).images

									__a	:  List[str]         =					image[0, 253:256, 253:256, -1]

									assert image.shape == (1, 512, 512, 3)
									__a	:  Optional[int]         =					np.array([0.3_3_6_7, 0.3_1_6_9, 0.2_6_5_6, 0.3_8_7_0, 0.4_7_9_0, 0.3_7_9_6, 0.4_0_0_9, 0.4_8_7_8, 0.4_7_7_8]       )

									assert np.abs(image_slice.flatten() - expected_slice       ).max() < 1e-2






"""simple docstring"""






import os
import string
import sys


A	     =					1 << 8

A	     =					{
    '''tab''': ord('''\t'''),
    '''newline''': ord('''\r'''),
    '''esc''': 27,
    '''up''': 65 + ARROW_KEY_FLAG,
    '''down''': 66 + ARROW_KEY_FLAG,
    '''right''': 67 + ARROW_KEY_FLAG,
    '''left''': 68 + ARROW_KEY_FLAG,
    '''mod_int''': 91,
    '''undefined''': sys.maxsize,
    '''interrupt''': 3,
    '''insert''': 50,
    '''delete''': 51,
    '''pg_up''': 53,
    '''pg_down''': 54,
}

A	     =					KEYMAP['''up''']
A	     =					KEYMAP['''left''']

if sys.platform == "win32":
		A	     =					[]
		A	     =					{
		    B'''\xe0H''': KEYMAP['''up'''] - ARROW_KEY_FLAG,
		    B'''\x00H''': KEYMAP['''up'''] - ARROW_KEY_FLAG,
		    B'''\xe0P''': KEYMAP['''down'''] - ARROW_KEY_FLAG,
		    B'''\x00P''': KEYMAP['''down'''] - ARROW_KEY_FLAG,
		    B'''\xe0M''': KEYMAP['''right'''] - ARROW_KEY_FLAG,
		    B'''\x00M''': KEYMAP['''right'''] - ARROW_KEY_FLAG,
		    B'''\xe0K''': KEYMAP['''left'''] - ARROW_KEY_FLAG,
		    B'''\x00K''': KEYMAP['''left'''] - ARROW_KEY_FLAG,
		}

for i in range(10):
		A	     =					ord(str(i))

def       __A (     )						->						Dict:
				if os.name == "nt":
								import msvcrt

								__a	:  Optional[Any]         =					'''mbcs'''
								# Flush the keyboard buffer
								while msvcrt.kbhit():
												msvcrt.getch()
								if len(a_) == 0:
												# Read the keystroke
												__a	:  Optional[Any]         =					msvcrt.getch()

												# If it is a prefix char, get second part
												if ch in (b"\x00", b"\xe0"):
																__a	:  Optional[Any]         =					ch + msvcrt.getch()
																# Translate actual Win chars to bullet char types
																try:
																				__a	:  Union[str, Any]         =					chr(WIN_KEYMAP[cha])
																				WIN_CH_BUFFER.append(chr(KEYMAP['''mod_int''']))
																				WIN_CH_BUFFER.append(a_)
																				if ord(a_) in (
																				    KEYMAP["insert"] - 1 << 9,
																				    KEYMAP["delete"] - 1 << 9,
																				    KEYMAP["pg_up"] - 1 << 9,
																				    KEYMAP["pg_down"] - 1 << 9,
																				):
																								WIN_CH_BUFFER.append(chr(1_26))
																				__a	:  str         =					chr(KEYMAP['''esc'''])
																except KeyError:
																				__a	:  str         =					cha[1]
												else:
																__a	:  Optional[Any]         =					ch.decode(a_)
								else:
												__a	:  Union[str, Any]         =					WIN_CH_BUFFER.pop(0)
				elif os.name == "posix":
								import termios
								import tty

								__a	:  Any         =					sys.stdin.fileno()
								__a	:  List[str]         =					termios.tcgetattr(a_)
								try:
												tty.setraw(a_)
												__a	:  int         =					sys.stdin.read(1)
								finally:
												termios.tcsetattr(a_				,    termios.TCSADRAIN				,    a_)
				return ch






def       __A (     )						->						str:
				__a	:  Any         =					get_raw_chars()
				if ord(a_) in [KEYMAP["interrupt"], KEYMAP["newline"]]:
								return char

				elif ord(a_) == KEYMAP["esc"]:
								__a	:  str         =					get_raw_chars()
								if ord(a_) == KEYMAP["mod_int"]:
												__a	:  List[str]         =					get_raw_chars()
												if ord(a_) >= KEYMAP["arrow_begin"] - ARROW_KEY_FLAG and ord(a_) <= KEYMAP["arrow_end"] - ARROW_KEY_FLAG:
																return chr(ord(a_) + ARROW_KEY_FLAG)
												else:
																return KEYMAP["undefined"]
								else:
												return get_raw_chars()

				else:
								if char in string.printable:
												return char
								else:
												return KEYMAP["undefined"]