






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


lowerCamelCase__							=     logging.get_logger(__name__)

enable_full_determinism()




class 			snake_case__   (							snake_case__	,    snake_case__	,    unittest.TestCase):

       '''simple docstring'''
       lowerCamelCase							:				List[Any] 				= UNetaDModel
       lowerCamelCase							:				List[str] 				= "sample"



       @property
       def 					__lowercase						(							self    ) ->      str:






            '''simple docstring'''





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

            __snake_case			:str          =						floats_tensor((batch_size, num_channels) + sizes    ).to(lowercase_    )
            __snake_case			:Union[str, Any]          =						torch.tensor([10]    ).to(lowercase_    )

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



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






            '''simple docstring'''





            return (3, 32, 32)



       @property
       def 					__lowercase						(							self    ) ->      int:






            '''simple docstring'''





            return (3, 32, 32)



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






            '''simple docstring'''





            __snake_case			:str          =						{
                "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			:List[Any]          =						self.dummy_input
            return init_dict, inputs_dict




class 			snake_case__   (							snake_case__	,    snake_case__	,    unittest.TestCase):

       '''simple docstring'''
       lowerCamelCase							:				Optional[Any] 				= UNetaDModel
       lowerCamelCase							:				Optional[int] 				= "sample"



       @property
       def 					__lowercase						(							self    ) ->      Union[str, Any]:






            '''simple docstring'''





            __snake_case			:List[Any]          =						4
            __snake_case			:Dict          =						4
            __snake_case			:Dict          =						(32, 32)

            __snake_case			:Dict          =						floats_tensor((batch_size, num_channels) + sizes    ).to(lowercase_    )
            __snake_case			:Any          =						torch.tensor([10]    ).to(lowercase_    )

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



       @property
       def 					__lowercase						(							self    ) ->      Union[str, Any]:






            '''simple docstring'''





            return (4, 32, 32)



       @property
       def 					__lowercase						(							self    ) ->      Union[str, Any]:






            '''simple docstring'''





            return (4, 32, 32)



       def 					__lowercase						(							self    ) ->      int:






            '''simple docstring'''





            __snake_case			:Any          =						{
                "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			:Any          =						self.dummy_input
            return init_dict, inputs_dict



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






            '''simple docstring'''





            __snake_case			:Tuple          =						UNetaDModel.from_pretrained("""fusing/unet-ldm-dummy-update"""			,						output_loading_info=lowercase_    )

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

            model.to(lowercase_    )
            __snake_case			:int          =						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    ) ->      Dict:






            '''simple docstring'''





            __snake_case			:Optional[Any]          =						UNetaDModel.from_pretrained("""fusing/unet-ldm-dummy-update"""			,						output_loading_info=lowercase_    )
            model.to(lowercase_    )
            __snake_case			:str          =						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    ) ->      Dict:






            '''simple docstring'''





            __snake_case			:Any          =						UNetaDModel.from_pretrained("""fusing/unet-ldm-dummy-update"""			,						output_loading_info=lowercase_    )
            model_accelerate.to(lowercase_    )
            model_accelerate.eval()

            __snake_case			:Optional[int]          =						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			:List[str]          =						noise.to(lowercase_    )
            __snake_case			:Any          =						torch.tensor([10] * noise.shape[0]    ).to(lowercase_    )

            __snake_case			:Optional[int]          =						model_accelerate(lowercase_			,						lowercase_    )["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			:Union[str, Any]          =						UNetaDModel.from_pretrained(
                """fusing/unet-ldm-dummy-update"""			,						output_loading_info=lowercase_			,						low_cpu_mem_usage=lowercase_    )
            model_normal_load.to(lowercase_    )
            model_normal_load.eval()
            __snake_case			:Optional[Any]          =						model_normal_load(lowercase_			,						lowercase_    )["sample"]

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



       def 					__lowercase						(							self    ) ->      str:






            '''simple docstring'''





            __snake_case			:Any          =						UNetaDModel.from_pretrained("""fusing/unet-ldm-dummy-update"""    )
            model.eval()
            model.to(lowercase_    )

            __snake_case			:List[str]          =						torch.randn(
                1			,						model.config.in_channels			,						model.config.sample_size			,						model.config.sample_size			,						generator=torch.manual_seed(0    )			,						)
            __snake_case			:List[str]          =						noise.to(lowercase_    )
            __snake_case			:str          =						torch.tensor([10] * noise.shape[0]    ).to(lowercase_    )

            with torch.no_grad():
                 __snake_case			:Tuple          =						model(lowercase_			,						lowercase_    ).sample

            __snake_case			:Optional[int]          =						output[0, -1, -3:, -3:].flatten().cpu()
            # fmt: off
            __snake_case			:Tuple          =						torch.tensor([-13.32_58, -20.11_00, -15.98_73, -17.66_17, -23.05_96, -17.94_19, -13.36_75, -16.18_89, -12.38_00]    )
            # fmt: on

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




class 			snake_case__   (							snake_case__	,    snake_case__	,    unittest.TestCase):

       '''simple docstring'''
       lowerCamelCase							:				Any 				= UNetaDModel
       lowerCamelCase							:				Optional[int] 				= "sample"



       @property
       def 					__lowercase						(							self			,						a__=(32, 32)    ) ->      Dict:






            '''simple docstring'''





            __snake_case			:List[Any]          =						4
            __snake_case			:str          =						3

            __snake_case			:str          =						floats_tensor((batch_size, num_channels) + sizes    ).to(lowercase_    )
            __snake_case			:Dict          =						torch.tensor(batch_size * [10]    ).to(dtype=torch.intaa			,						device=lowercase_    )

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



       @property
       def 					__lowercase						(							self    ) ->      str:






            '''simple docstring'''





            return (3, 32, 32)



       @property
       def 					__lowercase						(							self    ) ->      Dict:






            '''simple docstring'''





            return (3, 32, 32)



       def 					__lowercase						(							self    ) ->      str:






            '''simple docstring'''





            __snake_case			:List[str]          =						{
                "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			:Optional[Any]          =						self.dummy_input
            return init_dict, inputs_dict



       @slow
       def 					__lowercase						(							self    ) ->      Any:






            '''simple docstring'''





            __snake_case			:Optional[int]          =						UNetaDModel.from_pretrained("""google/ncsnpp-celebahq-256"""			,						output_loading_info=lowercase_    )
            self.assertIsNotNone(lowercase_    )
            self.assertEqual(len(loading_info["""missing_keys"""]    )			,						0    )

            model.to(lowercase_    )
            __snake_case			:Dict          =						self.dummy_input
            __snake_case			:Tuple          =						floats_tensor((4, 3) + (2_56, 2_56)    ).to(lowercase_    )
            __snake_case			:Tuple          =						noise
            __snake_case			:Tuple          =						model(**lowercase_    )

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



       @slow
       def 					__lowercase						(							self    ) ->      str:






            '''simple docstring'''





            __snake_case			:Dict          =						UNetaDModel.from_pretrained("""google/ncsnpp-celebahq-256"""    )
            model.to(lowercase_    )

            __snake_case			:List[Any]          =						4
            __snake_case			:str          =						3
            __snake_case			:Dict          =						(2_56, 2_56)

            __snake_case			:Tuple          =						torch.ones((batch_size, num_channels) + sizes    ).to(lowercase_    )
            __snake_case			:List[Any]          =						torch.tensor(batch_size * [1e-4]    ).to(lowercase_    )

            with torch.no_grad():
                 __snake_case			:int          =						model(lowercase_			,						lowercase_    ).sample

            __snake_case			:List[str]          =						output[0, -3:, -3:, -1].flatten().cpu()
            # fmt: off
            __snake_case			:Union[str, Any]          =						torch.tensor([-48_42.86_91, -64_99.66_31, -38_00.19_53, -79_78.26_86, -1_09_80.71_29, -2_00_28.85_35, 81_48.28_22, 23_42.29_05, 5_67.76_08]    )
            # fmt: on

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



       def 					__lowercase						(							self    ) ->      Optional[Any]:






            '''simple docstring'''





            __snake_case			:List[Any]          =						UNetaDModel.from_pretrained("""fusing/ncsnpp-ffhq-ve-dummy-update"""    )
            model.to(lowercase_    )

            __snake_case			:Dict          =						4
            __snake_case			:str          =						3
            __snake_case			:List[Any]          =						(32, 32)

            __snake_case			:int          =						torch.ones((batch_size, num_channels) + sizes    ).to(lowercase_    )
            __snake_case			:List[Any]          =						torch.tensor(batch_size * [1e-4]    ).to(lowercase_    )

            with torch.no_grad():
                 __snake_case			:Optional[Any]          =						model(lowercase_			,						lowercase_    ).sample

            __snake_case			:List[Any]          =						output[0, -3:, -3:, -1].flatten().cpu()
            # fmt: off
            __snake_case			:Union[str, Any]          =						torch.tensor([-0.03_25, -0.09_00, -0.08_69, -0.03_32, -0.07_25, -0.02_70, -0.01_01, 0.02_27, 0.02_56]    )
            # fmt: on

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



       def 					__lowercase						(							self    ) ->      Tuple:






            '''simple docstring'''





            pass










import importlib
import json
import os
import sys
import tempfile
import unittest
from pathlib import Path

import transformers
import transformers.models.auto
from transformers.models.auto.configuration_auto import CONFIG_MAPPING, AutoConfig
from transformers.models.bert.configuration_bert import BertConfig
from transformers.models.roberta.configuration_roberta import RobertaConfig
from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER, get_tests_dir


sys.path.append(str(Path(__file__).parent.parent.parent.parent / """utils"""))

from test_module.custom_configuration import CustomConfig  # noqa E402


lowerCamelCase__							=     get_tests_dir("""fixtures/dummy-config.json""")




class 			snake_case__   (							unittest.TestCase):

       '''simple docstring'''



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






            '''simple docstring'''





            __snake_case			:Optional[int]          =						0



       def 					__lowercase						(							self    ) ->      int:






            '''simple docstring'''





            self.assertIsNotNone(transformers.models.auto.__spec__    )
            self.assertIsNotNone(importlib.util.find_spec("""transformers.models.auto"""    )    )



       def 					__lowercase						(							self    ) ->      List[str]:






            '''simple docstring'''





            __snake_case			:Optional[Any]          =						AutoConfig.from_pretrained("""bert-base-uncased"""    )
            self.assertIsInstance(a__			,						a__    )



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






            '''simple docstring'''





            __snake_case			:List[str]          =						AutoConfig.from_pretrained(a__    )
            self.assertIsInstance(a__			,						a__    )



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






            '''simple docstring'''





            __snake_case			:Tuple          =						AutoConfig.from_pretrained(a__    )
            self.assertIsInstance(a__			,						a__    )



       def 					__lowercase						(							self    ) ->      str:






            '''simple docstring'''





            __snake_case			:Optional[int]          =						AutoConfig.for_model("""roberta"""    )
            self.assertIsInstance(a__			,						a__    )



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






            '''simple docstring'''





            with tempfile.TemporaryDirectory() as tmp_dir:
                 # This model name contains bert and roberta, but roberta ends up being picked.
                 __snake_case			:List[Any]          =						os.path.join(a__			,						"""fake-roberta"""    )
                 os.makedirs(a__			,						exist_ok=a__    )
                 with open(os.path.join(a__			,						"""config.json"""    )			,						"""w"""    ) as f:
                      f.write(json.dumps({}    )    )
                 __snake_case			:Optional[Any]          =						AutoConfig.from_pretrained(a__    )
                 self.assertEqual(type(a__    )			,						a__    )



       def 					__lowercase						(							self    ) ->      Dict:






            '''simple docstring'''





            try:
                 AutoConfig.register("""custom"""			,						a__    )
                 # Wrong model type will raise an error
                 with self.assertRaises(a__    ):
                      AutoConfig.register("""model"""			,						a__    )
                 # Trying to register something existing in the Transformers library will raise an error
                 with self.assertRaises(a__    ):
                      AutoConfig.register("""bert"""			,						a__    )

                 # Now that the config is registered, it can be used as any other config with the auto-API
                 __snake_case			:Optional[int]          =						CustomConfig()
                 with tempfile.TemporaryDirectory() as tmp_dir:
                      config.save_pretrained(a__    )
                      __snake_case			:Tuple          =						AutoConfig.from_pretrained(a__    )
                      self.assertIsInstance(a__			,						a__    )

            finally:
                 if "custom" in CONFIG_MAPPING._extra_content:
                      del CONFIG_MAPPING._extra_content["custom"]



       def 					__lowercase						(							self    ) ->      int:






            '''simple docstring'''





            with self.assertRaisesRegex(
                a__			,						"""bert-base is not a local folder and is not a valid model identifier"""    ):
                 __snake_case			:str          =						AutoConfig.from_pretrained("""bert-base"""    )



       def 					__lowercase						(							self    ) ->      str:






            '''simple docstring'''





            with self.assertRaisesRegex(
                a__			,						R"""aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)"""    ):
                 __snake_case			:Dict          =						AutoConfig.from_pretrained(a__			,						revision="""aaaaaa"""    )



       def 					__lowercase						(							self    ) ->      Optional[Any]:






            '''simple docstring'''





            with self.assertRaisesRegex(
                a__			,						"""hf-internal-testing/no-config-test-repo does not appear to have a file named config.json."""			,						):
                 __snake_case			:Optional[int]          =						AutoConfig.from_pretrained("""hf-internal-testing/no-config-test-repo"""    )



       def 					__lowercase						(							self    ) ->      int:






            '''simple docstring'''





            with self.assertRaises(a__    ):
                 __snake_case			:Dict          =						AutoConfig.from_pretrained("""hf-internal-testing/test_dynamic_model"""    )
            # If remote code is disabled, we can't load this config.
            with self.assertRaises(a__    ):
                 __snake_case			:int          =						AutoConfig.from_pretrained("""hf-internal-testing/test_dynamic_model"""			,						trust_remote_code=a__    )

            __snake_case			:Tuple          =						AutoConfig.from_pretrained("""hf-internal-testing/test_dynamic_model"""			,						trust_remote_code=a__    )
            self.assertEqual(config.__class__.__name__			,						"""NewModelConfig"""    )

            # Test config can be reloaded.
            with tempfile.TemporaryDirectory() as tmp_dir:
                 config.save_pretrained(a__    )
                 __snake_case			:Union[str, Any]          =						AutoConfig.from_pretrained(a__			,						trust_remote_code=a__    )
            self.assertEqual(reloaded_config.__class__.__name__			,						"""NewModelConfig"""    )



       def 					__lowercase						(							self    ) ->      Dict:






            '''simple docstring'''





            class 			snake_case__   (							lowercase_):

                   '''simple docstring'''
                   lowerCamelCase							:				Optional[Any] 				= "new-model"

            try:
                 AutoConfig.register("""new-model"""			,						a__    )
                 # If remote code is not set, the default is to use local
                 __snake_case			:List[Any]          =						AutoConfig.from_pretrained("""hf-internal-testing/test_dynamic_model"""    )
                 self.assertEqual(config.__class__.__name__			,						"""NewModelConfigLocal"""    )

                 # If remote code is disabled, we load the local one.
                 __snake_case			:Tuple          =						AutoConfig.from_pretrained("""hf-internal-testing/test_dynamic_model"""			,						trust_remote_code=a__    )
                 self.assertEqual(config.__class__.__name__			,						"""NewModelConfigLocal"""    )

                 # If remote is enabled, we load from the Hub
                 __snake_case			:List[Any]          =						AutoConfig.from_pretrained("""hf-internal-testing/test_dynamic_model"""			,						trust_remote_code=a__    )
                 self.assertEqual(config.__class__.__name__			,						"""NewModelConfig"""    )

            finally:
                 if "new-model" in CONFIG_MAPPING._extra_content:
                      del CONFIG_MAPPING._extra_content["new-model"]


