




'''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


UpperCamelCase_						:      int       =						logging.get_logger(__name__)

enable_full_determinism()

class        _a    (						__lowerCAmelCase , __lowerCAmelCase , unittest.TestCase							):
					SCREAMING_SNAKE_CASE_							:   Tuple       =						UNetaDModel
					SCREAMING_SNAKE_CASE_							:   str       =						"""sample"""






					@property
					def     _lowercase     (			self   )							->      int:
										_snake_case							=				4
										_snake_case							=				3
										_snake_case							=				(32, 32)

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

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






					@property
					def     _lowercase     (			self   )							->      Optional[Any]:
										return (3, 32, 32)






					@property
					def     _lowercase     (			self   )							->      Dict:
										return (3, 32, 32)






					def     _lowercase     (			self   )							->      int:
										_snake_case							=				{
										    "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,
										}
										_snake_case							=				self.dummy_input
										return init_dict, inputs_dict







class        _a    (						__lowerCAmelCase , __lowerCAmelCase , unittest.TestCase							):
					SCREAMING_SNAKE_CASE_							:   Dict       =						UNetaDModel
					SCREAMING_SNAKE_CASE_							:   List[Any]       =						"""sample"""






					@property
					def     _lowercase     (			self   )							->      Any:
										_snake_case							=				4
										_snake_case							=				4
										_snake_case							=				(32, 32)

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

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






					@property
					def     _lowercase     (			self   )							->      int:
										return (4, 32, 32)






					@property
					def     _lowercase     (			self   )							->      int:
										return (4, 32, 32)






					def     _lowercase     (			self   )							->      str:
										_snake_case							=				{
										    "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"),
										}
										_snake_case							=				self.dummy_input
										return init_dict, inputs_dict






					def     _lowercase     (			self   )							->      List[str]:
										_snake_case							, _snake_case							=				UNetaDModel.from_pretrained("fusing/unet-ldm-dummy-update" ,output_loading_info=_SCREAMING_SNAKE_CASE   )

										self.assertIsNotNone(_SCREAMING_SNAKE_CASE   )
										self.assertEqual(len(loading_info["missing_keys"]   ) ,0   )

										model.to(_SCREAMING_SNAKE_CASE   )
										_snake_case							=				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     _lowercase     (			self   )							->      Any:
										_snake_case							, _snake_case							=				UNetaDModel.from_pretrained("fusing/unet-ldm-dummy-update" ,output_loading_info=_SCREAMING_SNAKE_CASE   )
										model.to(_SCREAMING_SNAKE_CASE   )
										_snake_case							=				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     _lowercase     (			self   )							->      Union[str, Any]:
										# by defautl model loading will use accelerate as `low_cpu_mem_usage=True`
										_snake_case							, _snake_case							=				UNetaDModel.from_pretrained("fusing/unet-ldm-dummy-update" ,output_loading_info=_SCREAMING_SNAKE_CASE   )
										model_accelerate.to(_SCREAMING_SNAKE_CASE   )
										model_accelerate.eval()

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

										_snake_case							=				model_accelerate(_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE   )["sample"]

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

										_snake_case							, _snake_case							=				UNetaDModel.from_pretrained(
										    "fusing/unet-ldm-dummy-update" ,output_loading_info=_SCREAMING_SNAKE_CASE ,low_cpu_mem_usage=_SCREAMING_SNAKE_CASE   )
										model_normal_load.to(_SCREAMING_SNAKE_CASE   )
										model_normal_load.eval()
										_snake_case							=				model_normal_load(_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE   )["sample"]

										assert torch_all_close(_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,rtol=1e-3   )






					def     _lowercase     (			self   )							->      Union[str, Any]:
										_snake_case							=				UNetaDModel.from_pretrained("fusing/unet-ldm-dummy-update"   )
										model.eval()
										model.to(_SCREAMING_SNAKE_CASE   )

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

										with torch.no_grad():
															_snake_case							=				model(_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE   ).sample

										_snake_case							=				output[0, -1, -3:, -3:].flatten().cpu()
										# fmt: off
										_snake_case							=				torch.tensor([-13.3_258, -20.1_100, -15.9_873, -17.6_617, -23.0_596, -17.9_419, -13.3_675, -16.1_889, -12.3_800]   )
										# fmt: on

										self.assertTrue(torch_all_close(_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,rtol=1e-3   )   )







class        _a    (						__lowerCAmelCase , __lowerCAmelCase , unittest.TestCase							):
					SCREAMING_SNAKE_CASE_							:   Optional[int]       =						UNetaDModel
					SCREAMING_SNAKE_CASE_							:   Dict       =						"""sample"""






					@property
					def     _lowercase     (			self ,_SCREAMING_SNAKE_CASE=(32, 32)   )							->      List[Any]:
										_snake_case							=				4
										_snake_case							=				3

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

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






					@property
					def     _lowercase     (			self   )							->      Union[str, Any]:
										return (3, 32, 32)






					@property
					def     _lowercase     (			self   )							->      List[Any]:
										return (3, 32, 32)






					def     _lowercase     (			self   )							->      Any:
										_snake_case							=				{
										    "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",
										    ],
										}
										_snake_case							=				self.dummy_input
										return init_dict, inputs_dict






					@slow
					def     _lowercase     (			self   )							->      Optional[Any]:
										_snake_case							, _snake_case							=				UNetaDModel.from_pretrained("google/ncsnpp-celebahq-256" ,output_loading_info=_SCREAMING_SNAKE_CASE   )
										self.assertIsNotNone(_SCREAMING_SNAKE_CASE   )
										self.assertEqual(len(loading_info["missing_keys"]   ) ,0   )

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

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






					@slow
					def     _lowercase     (			self   )							->      str:
										_snake_case							=				UNetaDModel.from_pretrained("google/ncsnpp-celebahq-256"   )
										model.to(_SCREAMING_SNAKE_CASE   )

										_snake_case							=				4
										_snake_case							=				3
										_snake_case							=				(256, 256)

										_snake_case							=				torch.ones((batch_size, num_channels) + sizes   ).to(_SCREAMING_SNAKE_CASE   )
										_snake_case							=				torch.tensor(batch_size * [1e-4]   ).to(_SCREAMING_SNAKE_CASE   )

										with torch.no_grad():
															_snake_case							=				model(_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE   ).sample

										_snake_case							=				output[0, -3:, -3:, -1].flatten().cpu()
										# fmt: off
										_snake_case							=				torch.tensor([-4_842.8_691, -6_499.6_631, -3_800.1_953, -7_978.2_686, -10_980.7_129, -20_028.8_535, 8_148.2_822, 2_342.2_905, 567.7_608]   )
										# fmt: on

										self.assertTrue(torch_all_close(_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,rtol=1e-2   )   )






					def     _lowercase     (			self   )							->      int:
										_snake_case							=				UNetaDModel.from_pretrained("fusing/ncsnpp-ffhq-ve-dummy-update"   )
										model.to(_SCREAMING_SNAKE_CASE   )

										_snake_case							=				4
										_snake_case							=				3
										_snake_case							=				(32, 32)

										_snake_case							=				torch.ones((batch_size, num_channels) + sizes   ).to(_SCREAMING_SNAKE_CASE   )
										_snake_case							=				torch.tensor(batch_size * [1e-4]   ).to(_SCREAMING_SNAKE_CASE   )

										with torch.no_grad():
															_snake_case							=				model(_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE   ).sample

										_snake_case							=				output[0, -3:, -3:, -1].flatten().cpu()
										# fmt: off
										_snake_case							=				torch.tensor([-0.0_3_2_5, -0.0_9_0_0, -0.0_8_6_9, -0.0_3_3_2, -0.0_7_2_5, -0.0_2_7_0, -0.0_1_0_1, 0.0_2_2_7, 0.0_2_5_6]   )
										# fmt: on

										self.assertTrue(torch_all_close(_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,rtol=1e-2   )   )






					def     _lowercase     (			self   )							->      List[Any]:
										# not required for this model
										pass








'''simple docstring'''





from decimal import Decimal, getcontext
from math import ceil, factorial





def      __a     (       _UpperCamelCase:   int   )						->			str:

     """simple docstring"""




     if not isinstance(_UpperCamelCase ,						_UpperCamelCase   ):
          raise TypeError("Undefined for non-integers"   )
     elif precision < 1:
          raise ValueError("Undefined for non-natural numbers"   )

     _snake_case							=				precision
     _snake_case							=				ceil(precision / 14   )
     _snake_case							=				426_880 * Decimal(10_005   ).sqrt()
     _snake_case							=				1
     _snake_case							=				13_591_409
     _snake_case							=				Decimal(_UpperCamelCase   )
     for k in range(1 ,						_UpperCamelCase   ):
          _snake_case							=				factorial(6 * k   ) // (factorial(3 * k   ) * factorial(_UpperCamelCase   ) ** 3)
          linear_term += 545_140_134
          exponential_term *= -262_537_412_640_768_000
          partial_sum += Decimal(multinomial_term * linear_term   ) / exponential_term
     return str(constant_term / partial_sum   )[:-1]


if __name__ == "__main__":
   UpperCamelCase_						:      Any       =						50
   print(F'The first {n} digits of pi is: {pi(n)}')


