
"""simple docstring"""

import unittest

import numpy as np
import torch

from diffusers import KarrasVePipeline, KarrasVeScheduler, UNetaDModel
from diffusers.utils.testing_utils import enable_full_determinism, require_torch, slow, torch_device


enable_full_determinism()






class 	_lowercase       (    unittest.TestCase		):




	@property
	def       _UpperCamelCase   (			self )		->							Tuple:
			torch.manual_seed(0 )
			lowerCamelCase					:      Tuple          =   UNetaDModel(
			    block_out_channels=(32, 64)		,			layers_per_block=2		,			sample_size=32		,			in_channels=3		,			out_channels=3		,			down_block_types=('DownBlock2D', 'AttnDownBlock2D')		,			up_block_types=('AttnUpBlock2D', 'UpBlock2D')		,			)
			return model




	def       _UpperCamelCase   (			self )		->							List[Any]:
			lowerCamelCase					:      str          =   self.dummy_uncond_unet
			lowerCamelCase					:      Optional[int]          =   KarrasVeScheduler()

			lowerCamelCase					:      str          =   KarrasVePipeline(unet=__lowercase		,			scheduler=__lowercase )
			pipe.to(__lowercase )
			pipe.set_progress_bar_config(disable=__lowercase )

			lowerCamelCase					:      List[Any]          =   torch.manual_seed(0 )
			lowerCamelCase					:      List[Any]          =   pipe(num_inference_steps=2		,			generator=__lowercase		,			output_type='numpy' ).images

			lowerCamelCase					:      List[str]          =   torch.manual_seed(0 )
			lowerCamelCase					:      Any          =   pipe(num_inference_steps=2		,			generator=__lowercase		,			output_type='numpy'		,			return_dict=__lowercase )[0]

			lowerCamelCase					:      Dict          =   image[0, -3:, -3:, -1]
			lowerCamelCase					:      Optional[Any]          =   image_from_tuple[0, -3:, -3:, -1]

			assert image.shape == (1, 32, 32, 3)
			lowerCamelCase					:      Optional[int]          =   np.array([0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0] )

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






@slow
@require_torch
class 	_lowercase       (    unittest.TestCase		):




	def       _UpperCamelCase   (			self )		->							Optional[int]:
			lowerCamelCase					:      Any          =   '''google/ncsnpp-celebahq-256'''
			lowerCamelCase					:      List[str]          =   UNetaDModel.from_pretrained(__lowercase )
			lowerCamelCase					:      int          =   KarrasVeScheduler()

			lowerCamelCase					:      List[str]          =   KarrasVePipeline(unet=__lowercase		,			scheduler=__lowercase )
			pipe.to(__lowercase )
			pipe.set_progress_bar_config(disable=__lowercase )

			lowerCamelCase					:      Dict          =   torch.manual_seed(0 )
			lowerCamelCase					:      List[str]          =   pipe(num_inference_steps=20		,			generator=__lowercase		,			output_type='numpy' ).images

			lowerCamelCase					:      List[Any]          =   image[0, -3:, -3:, -1]
			assert image.shape == (1, 256, 256, 3)
			lowerCamelCase					:      Optional[Any]          =   np.array([0.578, 0.5811, 0.5924, 0.5809, 0.587, 0.5886, 0.5861, 0.5802, 0.586] )

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








"""simple docstring"""

import os
import sys
from contextlib import contextmanager


# Windows only
if os.name == "nt":
					import ctypes
					import msvcrt  # noqa






					class 	_lowercase       (    ctypes.Structure		):
						# _fields is a specific attr expected by ctypes
						lowercase_				=     [('size', ctypes.c_int), ('visible', ctypes.c_byte)]
def 		UpperCAmelCase (		):





		'''simple docstring'''


		if os.name == "nt":
				lowerCamelCase					:      Optional[int]          =   CursorInfo()
				lowerCamelCase					:      Union[str, Any]          =   ctypes.windll.kernelaa.GetStdHandle(-11      )
				ctypes.windll.kernelaa.GetConsoleCursorInfo(a_,						ctypes.byref(a_      )      )
				lowerCamelCase					:      Dict          =   False
				ctypes.windll.kernelaa.SetConsoleCursorInfo(a_,						ctypes.byref(a_      )      )
		elif os.name == "posix":
				sys.stdout.write('\033[?25l'      )
				sys.stdout.flush()
def 		UpperCAmelCase (		):





		'''simple docstring'''


		if os.name == "nt":
				lowerCamelCase					:      List[str]          =   CursorInfo()
				lowerCamelCase					:      List[Any]          =   ctypes.windll.kernelaa.GetStdHandle(-11      )
				ctypes.windll.kernelaa.GetConsoleCursorInfo(a_,						ctypes.byref(a_      )      )
				lowerCamelCase					:      Optional[Any]          =   True
				ctypes.windll.kernelaa.SetConsoleCursorInfo(a_,						ctypes.byref(a_      )      )
		elif os.name == "posix":
				sys.stdout.write('\033[?25h'      )
				sys.stdout.flush()





@contextmanager
def 		UpperCAmelCase (		):





		'''simple docstring'''


		try:
				hide_cursor()
				yield
		finally:
				show_cursor()






