
"""simple docstring"""



import gc
import math
import unittest

import torch

from diffusers import UNetaDModel
from diffusers.utils import floats_tensor, logging, slow, torch_all_close, torch_device
from diffusers.utils.testing_utils import enable_full_determinism

from .test_modeling_common import ModelTesterMixin, UNetTesterMixin


_snake_case          =						logging.get_logger(__name__)

enable_full_determinism()
class        _SCREAMING_SNAKE_CASE  (  UpperCAmelCase      ,				UpperCAmelCase      ,				unittest.TestCase							):






				'''simple docstring'''



				SCREAMING_SNAKE_CASE_:							Union[str, Any]							=					UNetaDModel
				SCREAMING_SNAKE_CASE_:							Union[str, Any]							=					"sample"


				@property
				def 				__lowerCamelCase	(					self	:			Optional[int]  )		->  Union[str, Any]:







											"""simple docstring"""




											_lowerCAmelCase  	=			4
											_lowerCAmelCase  	=			3
											_lowerCAmelCase  	=			(32, 32)

											_lowerCAmelCase  	=			floats_tensor((batch_size, num_channels) + sizes  ).to(UpperCAmelCase_  )
											_lowerCAmelCase  	=			torch.tensor([10]  ).to(UpperCAmelCase_  )

											return {"sample": noise, "timestep": time_step}


				@property
				def 				__lowerCamelCase	(					self	:			Optional[int]  )		->  List[str]:







											"""simple docstring"""




											return (3, 32, 32)


				@property
				def 				__lowerCamelCase	(					self	:			int  )		->  Optional[int]:







											"""simple docstring"""




											return (3, 32, 32)







				def 				__lowerCamelCase	(					self	:			Optional[int]  )		->  Union[str, Any]:







											"""simple docstring"""




											_lowerCAmelCase  	=			{
											    'block_out_channels': (32, 64),
											    'down_block_types': ('DownBlock2D', 'AttnDownBlock2D'),
											    'up_block_types': ('AttnUpBlock2D', 'UpBlock2D'),
											    'attention_head_dim': 3,
											    'out_channels': 3,
											    'in_channels': 3,
											    'layers_per_block': 2,
											    'sample_size': 32,
											}
											_lowerCAmelCase  	=			self.dummy_input
											return init_dict, inputs_dict






class        _SCREAMING_SNAKE_CASE  (  UpperCAmelCase      ,				UpperCAmelCase      ,				unittest.TestCase							):






				'''simple docstring'''



				SCREAMING_SNAKE_CASE_:							Optional[int]							=					UNetaDModel
				SCREAMING_SNAKE_CASE_:							Dict							=					"sample"


				@property
				def 				__lowerCamelCase	(					self	:			str  )		->  str:







											"""simple docstring"""




											_lowerCAmelCase  	=			4
											_lowerCAmelCase  	=			4
											_lowerCAmelCase  	=			(32, 32)

											_lowerCAmelCase  	=			floats_tensor((batch_size, num_channels) + sizes  ).to(UpperCAmelCase_  )
											_lowerCAmelCase  	=			torch.tensor([10]  ).to(UpperCAmelCase_  )

											return {"sample": noise, "timestep": time_step}


				@property
				def 				__lowerCamelCase	(					self	:			Union[str, Any]  )		->  Optional[int]:







											"""simple docstring"""




											return (4, 32, 32)


				@property
				def 				__lowerCamelCase	(					self	:			str  )		->  Dict:







											"""simple docstring"""




											return (4, 32, 32)


				def 				__lowerCamelCase	(					self	:			Optional[Any]  )		->  Union[str, Any]:







											"""simple docstring"""




											_lowerCAmelCase  	=			{
											    'sample_size': 32,
											    'in_channels': 4,
											    'out_channels': 4,
											    'layers_per_block': 2,
											    'block_out_channels': (32, 64),
											    'attention_head_dim': 32,
											    'down_block_types': ('DownBlock2D', 'DownBlock2D'),
											    'up_block_types': ('UpBlock2D', 'UpBlock2D'),
											}
											_lowerCAmelCase  	=			self.dummy_input
											return init_dict, inputs_dict


				def 				__lowerCamelCase	(					self	:			Tuple  )		->  Optional[Any]:







											"""simple docstring"""




											_lowerCAmelCase	,		_lowerCAmelCase  	=			UNetaDModel.from_pretrained('fusing/unet-ldm-dummy-update'				,     output_loading_info=UpperCAmelCase_  )

											self.assertIsNotNone(UpperCAmelCase_  )
											self.assertEqual(len(loading_info['missing_keys']  )				,     0  )

											model.to(UpperCAmelCase_  )
											_lowerCAmelCase  	=			model(**self.dummy_input  ).sample

											assert image is not None, "Make sure output is not None"


				@unittest.skipIf(torch_device != 'cuda'				,     'This test is supposed to run on GPU'  )
				def 				__lowerCamelCase	(					self	:			List[str]  )		->  Optional[Any]:







											"""simple docstring"""




											_lowerCAmelCase	,		_lowerCAmelCase  	=			UNetaDModel.from_pretrained('fusing/unet-ldm-dummy-update'				,     output_loading_info=UpperCAmelCase_  )
											model.to(UpperCAmelCase_  )
											_lowerCAmelCase  	=			model(**self.dummy_input  ).sample

											assert image is not None, "Make sure output is not None"


				@unittest.skipIf(torch_device != 'cuda'				,     'This test is supposed to run on GPU'  )
				def 				__lowerCamelCase	(					self	:			str  )		->  Optional[int]:







											"""simple docstring"""




											_lowerCAmelCase	,		_lowerCAmelCase  	=			UNetaDModel.from_pretrained('fusing/unet-ldm-dummy-update'				,     output_loading_info=UpperCAmelCase_  )
											model_accelerate.to(UpperCAmelCase_  )
											model_accelerate.eval()

											_lowerCAmelCase  	=			torch.randn(
											    1				,     model_accelerate.config.in_channels				,     model_accelerate.config.sample_size				,     model_accelerate.config.sample_size				,     generator=torch.manual_seed(0  )				,     )
											_lowerCAmelCase  	=			noise.to(UpperCAmelCase_  )
											_lowerCAmelCase  	=			torch.tensor([10] * noise.shape[0]  ).to(UpperCAmelCase_  )

											_lowerCAmelCase  	=			model_accelerate(UpperCAmelCase_				,     UpperCAmelCase_  )['sample']

											# two models don't need to stay in the device at the same time
											del model_accelerate
											torch.cuda.empty_cache()
											gc.collect()

											_lowerCAmelCase	,		_lowerCAmelCase  	=			UNetaDModel.from_pretrained(
											    'fusing/unet-ldm-dummy-update'				,     output_loading_info=UpperCAmelCase_				,     low_cpu_mem_usage=UpperCAmelCase_  )
											model_normal_load.to(UpperCAmelCase_  )
											model_normal_load.eval()
											_lowerCAmelCase  	=			model_normal_load(UpperCAmelCase_				,     UpperCAmelCase_  )['sample']

											assert torch_all_close(UpperCAmelCase_				,     UpperCAmelCase_				,     rtol=1E-3  )







				def 				__lowerCamelCase	(					self	:			Optional[int]  )		->  str:







											"""simple docstring"""




											_lowerCAmelCase  	=			UNetaDModel.from_pretrained('fusing/unet-ldm-dummy-update'  )
											model.eval()
											model.to(UpperCAmelCase_  )

											_lowerCAmelCase  	=			torch.randn(
											    1				,     model.config.in_channels				,     model.config.sample_size				,     model.config.sample_size				,     generator=torch.manual_seed(0  )				,     )
											_lowerCAmelCase  	=			noise.to(UpperCAmelCase_  )
											_lowerCAmelCase  	=			torch.tensor([10] * noise.shape[0]  ).to(UpperCAmelCase_  )

											with torch.no_grad():
																		_lowerCAmelCase  	=			model(UpperCAmelCase_				,     UpperCAmelCase_  ).sample

											_lowerCAmelCase  	=			output[0, -1, -3:, -3:].flatten().cpu()
											# fmt: off
											_lowerCAmelCase  	=			torch.tensor([-13.3258, -20.1100, -15.9873, -17.6617, -23.0596, -17.9419, -13.3675, -16.1889, -12.3800]  )
											# fmt: on

											self.assertTrue(torch_all_close(UpperCAmelCase_				,     UpperCAmelCase_				,     rtol=1E-3  )  )






class        _SCREAMING_SNAKE_CASE  (  UpperCAmelCase      ,				UpperCAmelCase      ,				unittest.TestCase							):






				'''simple docstring'''



				SCREAMING_SNAKE_CASE_:							int							=					UNetaDModel
				SCREAMING_SNAKE_CASE_:							Dict							=					"sample"


				@property
				def 				__lowerCamelCase	(					self	:			Any				,     UpperCAmelCase_	:			Optional[Any]=(32, 32)  )		->  List[Any]:







											"""simple docstring"""




											_lowerCAmelCase  	=			4
											_lowerCAmelCase  	=			3

											_lowerCAmelCase  	=			floats_tensor((batch_size, num_channels) + sizes  ).to(UpperCAmelCase_  )
											_lowerCAmelCase  	=			torch.tensor(batch_size * [10]  ).to(dtype=torch.intaa				,     device=UpperCAmelCase_  )

											return {"sample": noise, "timestep": time_step}


				@property
				def 				__lowerCamelCase	(					self	:			List[Any]  )		->  str:







											"""simple docstring"""




											return (3, 32, 32)


				@property
				def 				__lowerCamelCase	(					self	:			List[Any]  )		->  Dict:







											"""simple docstring"""




											return (3, 32, 32)


				def 				__lowerCamelCase	(					self	:			Optional[int]  )		->  int:







											"""simple docstring"""




											_lowerCAmelCase  	=			{
											    'block_out_channels': [32, 64, 64, 64],
											    'in_channels': 3,
											    'layers_per_block': 1,
											    'out_channels': 3,
											    'time_embedding_type': 'fourier',
											    'norm_eps': 1E-6,
											    'mid_block_scale_factor': math.sqrt(2.0  ),
											    'norm_num_groups': None,
											    'down_block_types': [
											        'SkipDownBlock2D',
											        'AttnSkipDownBlock2D',
											        'SkipDownBlock2D',
											        'SkipDownBlock2D',
											    ],
											    'up_block_types': [
											        'SkipUpBlock2D',
											        'SkipUpBlock2D',
											        'AttnSkipUpBlock2D',
											        'SkipUpBlock2D',
											    ],
											}
											_lowerCAmelCase  	=			self.dummy_input
											return init_dict, inputs_dict


				@slow
				def 				__lowerCamelCase	(					self	:			Optional[Any]  )		->  Union[str, Any]:







											"""simple docstring"""




											_lowerCAmelCase	,		_lowerCAmelCase  	=			UNetaDModel.from_pretrained('google/ncsnpp-celebahq-256'				,     output_loading_info=UpperCAmelCase_  )
											self.assertIsNotNone(UpperCAmelCase_  )
											self.assertEqual(len(loading_info['missing_keys']  )				,     0  )

											model.to(UpperCAmelCase_  )
											_lowerCAmelCase  	=			self.dummy_input
											_lowerCAmelCase  	=			floats_tensor((4, 3) + (256, 256)  ).to(UpperCAmelCase_  )
											_lowerCAmelCase  	=			noise
											_lowerCAmelCase  	=			model(**UpperCAmelCase_  )

											assert image is not None, "Make sure output is not None"


				@slow
				def 				__lowerCamelCase	(					self	:			Optional[int]  )		->  str:







											"""simple docstring"""




											_lowerCAmelCase  	=			UNetaDModel.from_pretrained('google/ncsnpp-celebahq-256'  )
											model.to(UpperCAmelCase_  )

											_lowerCAmelCase  	=			4
											_lowerCAmelCase  	=			3
											_lowerCAmelCase  	=			(256, 256)

											_lowerCAmelCase  	=			torch.ones((batch_size, num_channels) + sizes  ).to(UpperCAmelCase_  )
											_lowerCAmelCase  	=			torch.tensor(batch_size * [1E-4]  ).to(UpperCAmelCase_  )

											with torch.no_grad():
																		_lowerCAmelCase  	=			model(UpperCAmelCase_				,     UpperCAmelCase_  ).sample

											_lowerCAmelCase  	=			output[0, -3:, -3:, -1].flatten().cpu()
											# fmt: off
											_lowerCAmelCase  	=			torch.tensor([-4842.8691, -6499.6631, -3800.1953, -7978.2686, -10980.7129, -20028.8535, 8148.2822, 2342.2905, 567.7608]  )
											# fmt: on

											self.assertTrue(torch_all_close(UpperCAmelCase_				,     UpperCAmelCase_				,     rtol=1E-2  )  )


				def 				__lowerCamelCase	(					self	:			Optional[Any]  )		->  Dict:







											"""simple docstring"""




											_lowerCAmelCase  	=			UNetaDModel.from_pretrained('fusing/ncsnpp-ffhq-ve-dummy-update'  )
											model.to(UpperCAmelCase_  )

											_lowerCAmelCase  	=			4
											_lowerCAmelCase  	=			3
											_lowerCAmelCase  	=			(32, 32)

											_lowerCAmelCase  	=			torch.ones((batch_size, num_channels) + sizes  ).to(UpperCAmelCase_  )
											_lowerCAmelCase  	=			torch.tensor(batch_size * [1E-4]  ).to(UpperCAmelCase_  )

											with torch.no_grad():
																		_lowerCAmelCase  	=			model(UpperCAmelCase_				,     UpperCAmelCase_  ).sample

											_lowerCAmelCase  	=			output[0, -3:, -3:, -1].flatten().cpu()
											# fmt: off
											_lowerCAmelCase  	=			torch.tensor([-0.0325, -0.0900, -0.0869, -0.0332, -0.0725, -0.0270, -0.0101, 0.0227, 0.0256]  )
											# fmt: on

											self.assertTrue(torch_all_close(UpperCAmelCase_				,     UpperCAmelCase_				,     rtol=1E-2  )  )







				def 				__lowerCamelCase	(					self	:			Optional[Any]  )		->  Tuple:







											"""simple docstring"""




											pass








"""simple docstring"""



def 	__snake_case  (    SCREAMING_SNAKE_CASE:					int						):






							"""simple docstring"""



							_lowerCAmelCase  	=			generate_pascal_triangle(SCREAMING_SNAKE_CASE						)
							for row_idx in range(SCREAMING_SNAKE_CASE						):
														# Print left spaces
														for _ in range(num_rows - row_idx - 1						):
																					print(end=' '						)
														# Print row values
														for col_idx in range(row_idx + 1						):
																					if col_idx != row_idx:
																												print(triangle[row_idx][col_idx]     ,  end=' '						)
																					else:
																												print(triangle[row_idx][col_idx]     ,  end=''						)
														print()






def 	__snake_case  (    SCREAMING_SNAKE_CASE:					int						):






							"""simple docstring"""



							if not isinstance(SCREAMING_SNAKE_CASE     ,  SCREAMING_SNAKE_CASE						):
														raise TypeError('The input value of \'num_rows\' should be \'int\''						)

							if num_rows == 0:
														return []
							elif num_rows < 0:
														raise ValueError(
														    'The input value of \'num_rows\' should be greater than or equal to 0'						)

							_lowerCAmelCase  	=			[]
							for current_row_idx in range(SCREAMING_SNAKE_CASE						):
														_lowerCAmelCase  	=			populate_current_row(SCREAMING_SNAKE_CASE     ,  SCREAMING_SNAKE_CASE						)
														triangle.append(SCREAMING_SNAKE_CASE						)
							return triangle






def 	__snake_case  (    SCREAMING_SNAKE_CASE:					list[list[int]]     ,  SCREAMING_SNAKE_CASE:					int						):






							"""simple docstring"""



							_lowerCAmelCase  	=			[-1] * (current_row_idx + 1)
							# first and last elements of current row are equal to 1
							_lowerCAmelCase	,		_lowerCAmelCase  	=			1, 1
							for current_col_idx in range(1     ,  SCREAMING_SNAKE_CASE						):
														calculate_current_element(
														    SCREAMING_SNAKE_CASE     ,  SCREAMING_SNAKE_CASE     ,  SCREAMING_SNAKE_CASE     ,  SCREAMING_SNAKE_CASE						)
							return current_row






def 	__snake_case  (    SCREAMING_SNAKE_CASE:					list[list[int]]     ,  SCREAMING_SNAKE_CASE:					list[int]     ,  SCREAMING_SNAKE_CASE:					int     ,  SCREAMING_SNAKE_CASE:					int     ,  ):






							"""simple docstring"""



							_lowerCAmelCase  	=			triangle[current_row_idx - 1][current_col_idx - 1]
							_lowerCAmelCase  	=			triangle[current_row_idx - 1][current_col_idx]
							_lowerCAmelCase  	=			above_to_left_elt + above_to_right_elt






def 	__snake_case  (    SCREAMING_SNAKE_CASE:					int						):






							"""simple docstring"""



							if not isinstance(SCREAMING_SNAKE_CASE     ,  SCREAMING_SNAKE_CASE						):
														raise TypeError('The input value of \'num_rows\' should be \'int\''						)

							if num_rows == 0:
														return []
							elif num_rows < 0:
														raise ValueError(
														    'The input value of \'num_rows\' should be greater than or equal to 0'						)

							_lowerCAmelCase  	=			[[1]]

							for row_index in range(1     ,  SCREAMING_SNAKE_CASE						):
														_lowerCAmelCase  	=			[0] + result[-1] + [0]
														_lowerCAmelCase  	=			row_index + 1
														# Calculate the number of distinct elements in a row
														_lowerCAmelCase  	=			sum(divmod(SCREAMING_SNAKE_CASE     ,  2						)						)
														_lowerCAmelCase  	=			[
														    temp_row[i - 1] + temp_row[i] for i in range(1     ,  distinct_elements + 1						)
														]
														_lowerCAmelCase  	=			row_first_half[: (row_index + 1) // 2]
														row_second_half.reverse()
														_lowerCAmelCase  	=			row_first_half + row_second_half
														result.append(SCREAMING_SNAKE_CASE						)

							return result






def 	__snake_case  (    ):






							"""simple docstring"""



							from collections.abc import Callable
							from timeit import timeit

							def benchmark_a_function(SCREAMING_SNAKE_CASE:					Callable     ,  SCREAMING_SNAKE_CASE:					int						) -> None:
														_lowerCAmelCase  	=			f"""{func.__name__}({value})"""
														_lowerCAmelCase  	=			timeit(f"""__main__.{call}"""     ,  setup='import __main__'						)
														# print(f"{call:38} = {func(value)} -- {timing:.4f} seconds")
														print(f"""{call:38} -- {timing:.4f} seconds"""						)

							for value in range(15						):  # (1, 7, 14):
														for func in (generate_pascal_triangle, generate_pascal_triangle_optimized):
																					benchmark_a_function(SCREAMING_SNAKE_CASE     ,  SCREAMING_SNAKE_CASE						)
														print()


if __name__ == "__main__":
	import doctest

	doctest.testmod()
	benchmark()






