





"""simple docstring"""
import gc
import unittest

import torch
from parameterized import parameterized

from diffusers import AutoencoderKL
from diffusers.utils import floats_tensor, load_hf_numpy, require_torch_gpu, slow, torch_all_close, torch_device
from diffusers.utils.import_utils import is_xformers_available
from diffusers.utils.testing_utils import enable_full_determinism

from .test_modeling_common import ModelTesterMixin, UNetTesterMixin


enable_full_determinism()






class 					_SCREAMING_SNAKE_CASE(   A    ,	A    ,	unittest.TestCase  ):
       SCREAMING_SNAKE_CASE_   :      List[Any]							=  AutoencoderKL
       SCREAMING_SNAKE_CASE_   :      str							=  '''sample'''
       SCREAMING_SNAKE_CASE_   :      int							=  1E-2




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





             """simple docstring"""






             __SCREAMING_SNAKE_CASE   :Optional[Any]						=  4
             __SCREAMING_SNAKE_CASE   :Optional[int]						=  3
             __SCREAMING_SNAKE_CASE   :Optional[Any]						=  (32, 32)

             __SCREAMING_SNAKE_CASE   :Union[str, Any]						=  floats_tensor((batch_size, num_channels) + sizes							).to(SCREAMING_SNAKE_CASE__							)

             return {"sample": image}




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





             """simple docstring"""






             return (3, 32, 32)




       @property
       def     _UpperCamelCase     (						self							)			->      str:





             """simple docstring"""






             return (3, 32, 32)




       def     _UpperCamelCase     (						self							)			->      Tuple:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE   :Any						=  {
                 '''block_out_channels''': [32, 64],
                 '''in_channels''': 3,
                 '''out_channels''': 3,
                 '''down_block_types''': ['''DownEncoderBlock2D''', '''DownEncoderBlock2D'''],
                 '''up_block_types''': ['''UpDecoderBlock2D''', '''UpDecoderBlock2D'''],
                 '''latent_channels''': 4,
             }
             __SCREAMING_SNAKE_CASE   :Optional[int]						=  self.dummy_input
             return init_dict, inputs_dict




       def     _UpperCamelCase     (						self							)			->      str:





             """simple docstring"""






             pass




       def     _UpperCamelCase     (						self							)			->      List[Any]:





             """simple docstring"""






             pass




       @unittest.skipIf(torch_device == '''mps'''		,'''Gradient checkpointing skipped on MPS'''							)
       def     _UpperCamelCase     (						self							)			->      Dict:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE				,__SCREAMING_SNAKE_CASE   :Dict						=  self.prepare_init_args_and_inputs_for_common()
             __SCREAMING_SNAKE_CASE   :Optional[Any]						=  self.model_class(**SCREAMING_SNAKE_CASE__							)
             model.to(SCREAMING_SNAKE_CASE__							)

             assert not model.is_gradient_checkpointing and model.training

             __SCREAMING_SNAKE_CASE   :Union[str, Any]						=  model(**SCREAMING_SNAKE_CASE__							).sample
             # run the backwards pass on the model. For backwards pass, for simplicity purpose,
             # we won't calculate the loss and rather backprop on out.sum()
             model.zero_grad()

             __SCREAMING_SNAKE_CASE   :Union[str, Any]						=  torch.randn_like(SCREAMING_SNAKE_CASE__							)
             __SCREAMING_SNAKE_CASE   :Optional[Any]						=  (out - labels).mean()
             loss.backward()

             # re-instantiate the model now enabling gradient checkpointing
             __SCREAMING_SNAKE_CASE   :Optional[Any]						=  self.model_class(**SCREAMING_SNAKE_CASE__							)
             # clone model
             model_a.load_state_dict(model.state_dict()							)
             model_a.to(SCREAMING_SNAKE_CASE__							)
             model_a.enable_gradient_checkpointing()

             assert model_a.is_gradient_checkpointing and model_a.training

             __SCREAMING_SNAKE_CASE   :int						=  model_a(**SCREAMING_SNAKE_CASE__							).sample
             # run the backwards pass on the model. For backwards pass, for simplicity purpose,
             # we won't calculate the loss and rather backprop on out.sum()
             model_a.zero_grad()
             __SCREAMING_SNAKE_CASE   :Union[str, Any]						=  (out_a - labels).mean()
             loss_a.backward()

             # compare the output and parameters gradients
             self.assertTrue((loss - loss_a).abs() < 1E-5							)
             __SCREAMING_SNAKE_CASE   :Tuple						=  dict(model.named_parameters()							)
             __SCREAMING_SNAKE_CASE   :Any						=  dict(model_a.named_parameters()							)
             for name, param in named_params.items():
                   self.assertTrue(torch_all_close(param.grad.data		,named_params_a[name].grad.data		,atol=5E-5							)							)




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





             """simple docstring"""






             __SCREAMING_SNAKE_CASE				,__SCREAMING_SNAKE_CASE   :List[Any]						=  AutoencoderKL.from_pretrained('''fusing/autoencoder-kl-dummy'''		,output_loading_info=SCREAMING_SNAKE_CASE__							)
             self.assertIsNotNone(SCREAMING_SNAKE_CASE__							)
             self.assertEqual(len(loading_info['''missing_keys''']							)		,0							)

             model.to(SCREAMING_SNAKE_CASE__							)
             __SCREAMING_SNAKE_CASE   :List[str]						=  model(**self.dummy_input							)

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




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





             """simple docstring"""






             __SCREAMING_SNAKE_CASE   :Optional[int]						=  AutoencoderKL.from_pretrained('''fusing/autoencoder-kl-dummy'''							)
             __SCREAMING_SNAKE_CASE   :int						=  model.to(SCREAMING_SNAKE_CASE__							)
             model.eval()

             if torch_device == "mps":
                   __SCREAMING_SNAKE_CASE   :Optional[int]						=  torch.manual_seed(0							)
             else:
                   __SCREAMING_SNAKE_CASE   :int						=  torch.Generator(device=SCREAMING_SNAKE_CASE__							).manual_seed(0							)

             __SCREAMING_SNAKE_CASE   :Dict						=  torch.randn(
                 1		,model.config.in_channels		,model.config.sample_size		,model.config.sample_size		,generator=torch.manual_seed(0							)		,)
             __SCREAMING_SNAKE_CASE   :Tuple						=  image.to(SCREAMING_SNAKE_CASE__							)
             with torch.no_grad():
                   __SCREAMING_SNAKE_CASE   :int						=  model(SCREAMING_SNAKE_CASE__		,sample_posterior=SCREAMING_SNAKE_CASE__		,generator=SCREAMING_SNAKE_CASE__							).sample

             __SCREAMING_SNAKE_CASE   :Union[str, Any]						=  output[0, -1, -3:, -3:].flatten().cpu()

             # Since the VAE Gaussian prior's generator is seeded on the appropriate device,
             # the expected output slices are not the same for CPU and GPU.
             if torch_device == "mps":
                   __SCREAMING_SNAKE_CASE   :Dict						=  torch.tensor(
                       [
                           -4.0_078E-01,
                           -3.8_323E-04,
                           -1.2_681E-01,
                           -1.1_462E-01,
                           2.0_095E-01,
                           1.0_893E-01,
                           -8.8_247E-02,
                           -3.0_361E-01,
                           -9.8_644E-03,
                       ]							)
             elif torch_device == "cpu":
                   __SCREAMING_SNAKE_CASE   :Optional[Any]						=  torch.tensor(
                       [-0.1_3_5_2, 0.0_8_7_8, 0.0_4_1_9, -0.0_8_1_8, -0.1_0_6_9, 0.0_6_8_8, -0.1_4_5_8, -0.4_4_4_6, -0.0_0_2_6]							)
             else:
                   __SCREAMING_SNAKE_CASE   :int						=  torch.tensor(
                       [-0.2_4_2_1, 0.4_6_4_2, 0.2_5_0_7, -0.0_4_3_8, 0.0_6_8_2, 0.3_1_6_0, -0.2_0_1_8, -0.0_7_2_7, 0.2_4_8_5]							)

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






@slow
class 					_SCREAMING_SNAKE_CASE(   unittest.TestCase  ):




       def     _UpperCamelCase     (						self		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__							)			->      List[Any]:





             """simple docstring"""






             return f'''gaussian_noise_s={seed}_shape={"_".join([str(SCREAMING_SNAKE_CASE__							) for s in shape]							)}.npy'''




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





             """simple docstring"""






             super().tearDown()
             gc.collect()
             torch.cuda.empty_cache()




       def     _UpperCamelCase     (						self		,SCREAMING_SNAKE_CASE__=0		,SCREAMING_SNAKE_CASE__=(4, 3, 5_12, 5_12)		,SCREAMING_SNAKE_CASE__=False							)			->      Tuple:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE   :str						=  torch.floataa if fpaa else torch.floataa
             __SCREAMING_SNAKE_CASE   :Tuple						=  torch.from_numpy(load_hf_numpy(self.get_file_format(SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__							)							)							).to(SCREAMING_SNAKE_CASE__							).to(SCREAMING_SNAKE_CASE__							)
             return image




       def     _UpperCamelCase     (						self		,SCREAMING_SNAKE_CASE__="CompVis/stable-diffusion-v1-4"		,SCREAMING_SNAKE_CASE__=False							)			->      Tuple:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE   :List[Any]						=  '''fp16''' if fpaa else None
             __SCREAMING_SNAKE_CASE   :List[Any]						=  torch.floataa if fpaa else torch.floataa

             __SCREAMING_SNAKE_CASE   :int						=  AutoencoderKL.from_pretrained(
                 SCREAMING_SNAKE_CASE__		,subfolder='''vae'''		,torch_dtype=SCREAMING_SNAKE_CASE__		,revision=SCREAMING_SNAKE_CASE__		,)
             model.to(SCREAMING_SNAKE_CASE__							).eval()

             return model




       def     _UpperCamelCase     (						self		,SCREAMING_SNAKE_CASE__=0							)			->      int:





             """simple docstring"""






             if torch_device == "mps":
                   return torch.manual_seed(SCREAMING_SNAKE_CASE__							)
             return torch.Generator(device=SCREAMING_SNAKE_CASE__							).manual_seed(SCREAMING_SNAKE_CASE__							)




       @parameterized.expand(
           [
               # fmt: off
               [33, [-0.1_6_0_3, 0.9_8_7_8, -0.0_4_9_5, -0.0_7_9_0, -0.2_7_0_9, 0.8_3_7_5, -0.2_0_6_0, -0.0_8_2_4], [-0.2_3_9_5, 0.0_0_9_8, 0.0_1_0_2, -0.0_7_0_9, -0.2_8_4_0, -0.0_2_7_4, -0.0_7_1_8, -0.1_8_2_4]],
               [47, [-0.2_3_7_6, 0.1_1_6_8, 0.1_3_3_2, -0.4_8_4_0, -0.2_5_0_8, -0.0_7_9_1, -0.0_4_9_3, -0.4_0_8_9], [0.0_3_5_0, 0.0_8_4_7, 0.0_4_6_7, 0.0_3_4_4, -0.0_8_4_2, -0.0_5_4_7, -0.0_6_3_3, -0.1_1_3_1]],
               # fmt: on
           ]							)
       def     _UpperCamelCase     (						self		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__							)			->      Any:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE   :Union[str, Any]						=  self.get_sd_vae_model()
             __SCREAMING_SNAKE_CASE   :Optional[Any]						=  self.get_sd_image(SCREAMING_SNAKE_CASE__							)
             __SCREAMING_SNAKE_CASE   :Optional[Any]						=  self.get_generator(SCREAMING_SNAKE_CASE__							)

             with torch.no_grad():
                   __SCREAMING_SNAKE_CASE   :Tuple						=  model(SCREAMING_SNAKE_CASE__		,generator=SCREAMING_SNAKE_CASE__		,sample_posterior=SCREAMING_SNAKE_CASE__							).sample

             assert sample.shape == image.shape

             __SCREAMING_SNAKE_CASE   :Union[str, Any]						=  sample[-1, -2:, -2:, :2].flatten().float().cpu()
             __SCREAMING_SNAKE_CASE   :str						=  torch.tensor(expected_slice_mps if torch_device == '''mps''' else expected_slice							)

             assert torch_all_close(SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,atol=3E-3							)




       @parameterized.expand(
           [
               # fmt: off
               [33, [-0.0_5_1_3, 0.0_2_8_9, 1.3_7_9_9, 0.2_1_6_6, -0.2_5_7_3, -0.0_8_7_1, 0.5_1_0_3, -0.0_9_9_9]],
               [47, [-0.4_1_2_8, -0.1_3_2_0, -0.3_7_0_4, 0.1_9_6_5, -0.4_1_1_6, -0.2_3_3_2, -0.3_3_4_0, 0.2_2_4_7]],
               # fmt: on
           ]							)
       @require_torch_gpu
       def     _UpperCamelCase     (						self		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__							)			->      List[Any]:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE   :str						=  self.get_sd_vae_model(fpaa=SCREAMING_SNAKE_CASE__							)
             __SCREAMING_SNAKE_CASE   :Optional[Any]						=  self.get_sd_image(SCREAMING_SNAKE_CASE__		,fpaa=SCREAMING_SNAKE_CASE__							)
             __SCREAMING_SNAKE_CASE   :Union[str, Any]						=  self.get_generator(SCREAMING_SNAKE_CASE__							)

             with torch.no_grad():
                   __SCREAMING_SNAKE_CASE   :Tuple						=  model(SCREAMING_SNAKE_CASE__		,generator=SCREAMING_SNAKE_CASE__		,sample_posterior=SCREAMING_SNAKE_CASE__							).sample

             assert sample.shape == image.shape

             __SCREAMING_SNAKE_CASE   :Any						=  sample[-1, -2:, :2, -2:].flatten().float().cpu()
             __SCREAMING_SNAKE_CASE   :Union[str, Any]						=  torch.tensor(SCREAMING_SNAKE_CASE__							)

             assert torch_all_close(SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,atol=1E-2							)




       @parameterized.expand(
           [
               # fmt: off
               [33, [-0.1_6_0_9, 0.9_8_6_6, -0.0_4_8_7, -0.0_7_7_7, -0.2_7_1_6, 0.8_3_6_8, -0.2_0_5_5, -0.0_8_1_4], [-0.2_3_9_5, 0.0_0_9_8, 0.0_1_0_2, -0.0_7_0_9, -0.2_8_4_0, -0.0_2_7_4, -0.0_7_1_8, -0.1_8_2_4]],
               [47, [-0.2_3_7_7, 0.1_1_4_7, 0.1_3_3_3, -0.4_8_4_1, -0.2_5_0_6, -0.0_8_0_5, -0.0_4_9_1, -0.4_0_8_5], [0.0_3_5_0, 0.0_8_4_7, 0.0_4_6_7, 0.0_3_4_4, -0.0_8_4_2, -0.0_5_4_7, -0.0_6_3_3, -0.1_1_3_1]],
               # fmt: on
           ]							)
       def     _UpperCamelCase     (						self		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__							)			->      Dict:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE   :Union[str, Any]						=  self.get_sd_vae_model()
             __SCREAMING_SNAKE_CASE   :Dict						=  self.get_sd_image(SCREAMING_SNAKE_CASE__							)

             with torch.no_grad():
                   __SCREAMING_SNAKE_CASE   :List[Any]						=  model(SCREAMING_SNAKE_CASE__							).sample

             assert sample.shape == image.shape

             __SCREAMING_SNAKE_CASE   :int						=  sample[-1, -2:, -2:, :2].flatten().float().cpu()
             __SCREAMING_SNAKE_CASE   :Optional[Any]						=  torch.tensor(expected_slice_mps if torch_device == '''mps''' else expected_slice							)

             assert torch_all_close(SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,atol=3E-3							)




       @parameterized.expand(
           [
               # fmt: off
               [13, [-0.2_0_5_1, -0.1_8_0_3, -0.2_3_1_1, -0.2_1_1_4, -0.3_2_9_2, -0.3_5_7_4, -0.2_9_5_3, -0.3_3_2_3]],
               [37, [-0.2_6_3_2, -0.2_6_2_5, -0.2_1_9_9, -0.2_7_4_1, -0.4_5_3_9, -0.4_9_9_0, -0.3_7_2_0, -0.4_9_2_5]],
               # fmt: on
           ]							)
       @require_torch_gpu
       def     _UpperCamelCase     (						self		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__							)			->      List[Any]:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE   :Any						=  self.get_sd_vae_model()
             __SCREAMING_SNAKE_CASE   :Optional[int]						=  self.get_sd_image(SCREAMING_SNAKE_CASE__		,shape=(3, 4, 64, 64)							)

             with torch.no_grad():
                   __SCREAMING_SNAKE_CASE   :List[str]						=  model.decode(SCREAMING_SNAKE_CASE__							).sample

             assert list(sample.shape							) == [3, 3, 5_12, 5_12]

             __SCREAMING_SNAKE_CASE   :Optional[int]						=  sample[-1, -2:, :2, -2:].flatten().cpu()
             __SCREAMING_SNAKE_CASE   :Union[str, Any]						=  torch.tensor(SCREAMING_SNAKE_CASE__							)

             assert torch_all_close(SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,atol=1E-3							)




       @parameterized.expand(
           [
               # fmt: off
               [27, [-0.0_3_6_9, 0.0_2_0_7, -0.0_7_7_6, -0.0_6_8_2, -0.1_7_4_7, -0.1_9_3_0, -0.1_4_6_5, -0.2_0_3_9]],
               [16, [-0.1_6_2_8, -0.2_1_3_4, -0.2_7_4_7, -0.2_6_4_2, -0.3_7_7_4, -0.4_4_0_4, -0.3_6_8_7, -0.4_2_7_7]],
               # fmt: on
           ]							)
       @require_torch_gpu
       def     _UpperCamelCase     (						self		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__							)			->      int:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE   :List[str]						=  self.get_sd_vae_model(fpaa=SCREAMING_SNAKE_CASE__							)
             __SCREAMING_SNAKE_CASE   :int						=  self.get_sd_image(SCREAMING_SNAKE_CASE__		,shape=(3, 4, 64, 64)		,fpaa=SCREAMING_SNAKE_CASE__							)

             with torch.no_grad():
                   __SCREAMING_SNAKE_CASE   :List[Any]						=  model.decode(SCREAMING_SNAKE_CASE__							).sample

             assert list(sample.shape							) == [3, 3, 5_12, 5_12]

             __SCREAMING_SNAKE_CASE   :Optional[Any]						=  sample[-1, -2:, :2, -2:].flatten().float().cpu()
             __SCREAMING_SNAKE_CASE   :Tuple						=  torch.tensor(SCREAMING_SNAKE_CASE__							)

             assert torch_all_close(SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,atol=5E-3							)




       @parameterized.expand([(13,), (16,), (27,)]							)
       @require_torch_gpu
       @unittest.skipIf(not is_xformers_available()		,reason='''xformers is not required when using PyTorch 2.0.'''							)
       def     _UpperCamelCase     (						self		,SCREAMING_SNAKE_CASE__							)			->      Optional[Any]:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE   :str						=  self.get_sd_vae_model(fpaa=SCREAMING_SNAKE_CASE__							)
             __SCREAMING_SNAKE_CASE   :Tuple						=  self.get_sd_image(SCREAMING_SNAKE_CASE__		,shape=(3, 4, 64, 64)		,fpaa=SCREAMING_SNAKE_CASE__							)

             with torch.no_grad():
                   __SCREAMING_SNAKE_CASE   :Optional[Any]						=  model.decode(SCREAMING_SNAKE_CASE__							).sample

             model.enable_xformers_memory_efficient_attention()
             with torch.no_grad():
                   __SCREAMING_SNAKE_CASE   :Tuple						=  model.decode(SCREAMING_SNAKE_CASE__							).sample

             assert list(sample.shape							) == [3, 3, 5_12, 5_12]

             assert torch_all_close(SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,atol=1E-1							)




       @parameterized.expand([(13,), (16,), (37,)]							)
       @require_torch_gpu
       @unittest.skipIf(not is_xformers_available()		,reason='''xformers is not required when using PyTorch 2.0.'''							)
       def     _UpperCamelCase     (						self		,SCREAMING_SNAKE_CASE__							)			->      int:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE   :List[Any]						=  self.get_sd_vae_model()
             __SCREAMING_SNAKE_CASE   :Optional[int]						=  self.get_sd_image(SCREAMING_SNAKE_CASE__		,shape=(3, 4, 64, 64)							)

             with torch.no_grad():
                   __SCREAMING_SNAKE_CASE   :List[str]						=  model.decode(SCREAMING_SNAKE_CASE__							).sample

             model.enable_xformers_memory_efficient_attention()
             with torch.no_grad():
                   __SCREAMING_SNAKE_CASE   :Dict						=  model.decode(SCREAMING_SNAKE_CASE__							).sample

             assert list(sample.shape							) == [3, 3, 5_12, 5_12]

             assert torch_all_close(SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,atol=1E-2							)




       @parameterized.expand(
           [
               # fmt: off
               [33, [-0.3_0_0_1, 0.0_9_1_8, -2.6_9_8_4, -3.9_7_2_0, -3.2_0_9_9, -5.0_3_5_3, 1.7_3_3_8, -0.2_0_6_5, 3.4_2_6_7]],
               [47, [-1.5_0_3_0, -4.3_8_7_1, -6.0_3_5_5, -9.1_1_5_7, -1.6_6_6_1, -2.7_8_5_3, 2.1_6_0_7, -5.0_8_2_3, 2.5_6_3_3]],
               # fmt: on
           ]							)
       def     _UpperCamelCase     (						self		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__							)			->      int:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE   :List[Any]						=  self.get_sd_vae_model()
             __SCREAMING_SNAKE_CASE   :List[str]						=  self.get_sd_image(SCREAMING_SNAKE_CASE__							)
             __SCREAMING_SNAKE_CASE   :Tuple						=  self.get_generator(SCREAMING_SNAKE_CASE__							)

             with torch.no_grad():
                   __SCREAMING_SNAKE_CASE   :Optional[Any]						=  model.encode(SCREAMING_SNAKE_CASE__							).latent_dist
                   __SCREAMING_SNAKE_CASE   :str						=  dist.sample(generator=SCREAMING_SNAKE_CASE__							)

             assert list(sample.shape							) == [image.shape[0], 4] + [i // 8 for i in image.shape[2:]]

             __SCREAMING_SNAKE_CASE   :Any						=  sample[0, -1, -3:, -3:].flatten().cpu()
             __SCREAMING_SNAKE_CASE   :List[str]						=  torch.tensor(SCREAMING_SNAKE_CASE__							)

             __SCREAMING_SNAKE_CASE   :List[Any]						=  3E-3 if torch_device != '''mps''' else 1E-2
             assert torch_all_close(SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,atol=SCREAMING_SNAKE_CASE__							)






"""simple docstring"""
from manim import *






class 					_SCREAMING_SNAKE_CASE(   A  ):




       def     _UpperCamelCase     (						self							)			->      str:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE   :Optional[Any]						=  Rectangle(height=0.5		,width=0.5							)
             __SCREAMING_SNAKE_CASE   :List[str]						=  Rectangle(height=0.4_6		,width=0.4_6							).set_stroke(width=0							)

             __SCREAMING_SNAKE_CASE   :List[str]						=  [mem.copy() for i in range(6							)]
             __SCREAMING_SNAKE_CASE   :List[str]						=  [mem.copy() for i in range(6							)]
             __SCREAMING_SNAKE_CASE   :Optional[int]						=  VGroup(*SCREAMING_SNAKE_CASE__							).arrange(SCREAMING_SNAKE_CASE__		,buff=0							)
             __SCREAMING_SNAKE_CASE   :Optional[Any]						=  VGroup(*SCREAMING_SNAKE_CASE__							).arrange(SCREAMING_SNAKE_CASE__		,buff=0							)
             __SCREAMING_SNAKE_CASE   :Any						=  VGroup(SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__							).arrange(SCREAMING_SNAKE_CASE__		,buff=0							)
             __SCREAMING_SNAKE_CASE   :Tuple						=  Text('''CPU'''		,font_size=24							)
             __SCREAMING_SNAKE_CASE   :Optional[Any]						=  Group(SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__							).arrange(SCREAMING_SNAKE_CASE__		,buff=0.5		,aligned_edge=SCREAMING_SNAKE_CASE__							)
             cpu.move_to([-2.5, -0.5, 0]							)
             self.add(SCREAMING_SNAKE_CASE__							)

             __SCREAMING_SNAKE_CASE   :List[Any]						=  [mem.copy() for i in range(1							)]
             __SCREAMING_SNAKE_CASE   :str						=  VGroup(*SCREAMING_SNAKE_CASE__							).arrange(SCREAMING_SNAKE_CASE__		,buff=0							)
             __SCREAMING_SNAKE_CASE   :Union[str, Any]						=  Text('''GPU'''		,font_size=24							)
             __SCREAMING_SNAKE_CASE   :int						=  Group(SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__							).arrange(SCREAMING_SNAKE_CASE__		,buff=0.5		,aligned_edge=SCREAMING_SNAKE_CASE__							)
             gpu.align_to(SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__							)
             gpu.set_x(gpu.get_x() - 1							)

             self.add(SCREAMING_SNAKE_CASE__							)

             __SCREAMING_SNAKE_CASE   :str						=  [mem.copy() for i in range(6							)]
             __SCREAMING_SNAKE_CASE   :int						=  VGroup(*SCREAMING_SNAKE_CASE__							).arrange(SCREAMING_SNAKE_CASE__		,buff=0							)

             __SCREAMING_SNAKE_CASE   :List[Any]						=  Text('''Model'''		,font_size=24							)
             __SCREAMING_SNAKE_CASE   :int						=  Group(SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__							).arrange(SCREAMING_SNAKE_CASE__		,buff=0.5		,aligned_edge=SCREAMING_SNAKE_CASE__							)
             model.move_to([3, -1.0, 0]							)

             self.play(
                 Create(SCREAMING_SNAKE_CASE__		,run_time=1							)		,Create(SCREAMING_SNAKE_CASE__		,run_time=1							)		,Create(SCREAMING_SNAKE_CASE__		,run_time=1							)		,)

             __SCREAMING_SNAKE_CASE   :List[str]						=  MarkupText(
                 f'''First, an empty model skeleton is loaded\ninto <span fgcolor=\'{YELLOW}\'>memory</span> without using much RAM.'''		,font_size=24		,)

             __SCREAMING_SNAKE_CASE   :List[Any]						=  Square(side_length=2.2							)
             key.move_to([-5, 2, 0]							)

             __SCREAMING_SNAKE_CASE   :Optional[Any]						=  MarkupText(
                 f'''<b>Key:</b>\n\n<span fgcolor=\'{YELLOW}\'>●</span> Empty Model'''		,font_size=18		,)

             key_text.move_to([-5, 2.4, 0]							)

             step_a.move_to([2, 2, 0]							)
             self.play(Write(SCREAMING_SNAKE_CASE__		,run_time=2.5							)		,Write(SCREAMING_SNAKE_CASE__							)		,Write(SCREAMING_SNAKE_CASE__							)							)

             self.add(SCREAMING_SNAKE_CASE__							)

             __SCREAMING_SNAKE_CASE   :Union[str, Any]						=  []
             __SCREAMING_SNAKE_CASE   :int						=  []
             __SCREAMING_SNAKE_CASE   :List[Any]						=  []
             for i, rect in enumerate(SCREAMING_SNAKE_CASE__							):
                   __SCREAMING_SNAKE_CASE   :Any						=  Rectangle(height=0.4_6		,width=0.4_6							).set_stroke(width=0.0							).set_fill(SCREAMING_SNAKE_CASE__		,opacity=0.7							)
                   cpu_target.move_to(SCREAMING_SNAKE_CASE__							)
                   cpu_target.generate_target()
                   __SCREAMING_SNAKE_CASE   :Union[str, Any]						=  0.4_6 / 4
                   __SCREAMING_SNAKE_CASE   :Tuple						=  0.4_6 / 3

                   if i == 0:
                         cpu_target.target.next_to(cpu_left_col_base[0].get_corner(DOWN + LEFT							)		,buff=0.0_2		,direction=SCREAMING_SNAKE_CASE__							)
                         cpu_target.target.set_x(cpu_target.target.get_x() + 0.1							)
                   elif i == 3:
                         cpu_target.target.next_to(cpu_targs[0].target		,direction=SCREAMING_SNAKE_CASE__		,buff=0.0							)
                   else:
                         cpu_target.target.next_to(cpu_targs[i - 1].target		,direction=SCREAMING_SNAKE_CASE__		,buff=0.0							)
                   cpu_targs.append(SCREAMING_SNAKE_CASE__							)

                   first_animations.append(rect.animate(run_time=0.5							).set_stroke(SCREAMING_SNAKE_CASE__							)							)
                   second_animations.append(MoveToTarget(SCREAMING_SNAKE_CASE__		,run_time=1.5							)							)

             self.play(*SCREAMING_SNAKE_CASE__							)
             self.play(*SCREAMING_SNAKE_CASE__							)

             self.wait()