


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


a__    :							int            =					get_tests_dir('''fixtures/dummy-config.json''')




class 			UpperCamelCase_     (			unittest.TestCase):

	"""simple docstring"""





	def   UpperCAmelCase_				(			self      :    str       )	->     int:
						__SCREAMING_SNAKE_CASE						       =    0




	def   UpperCAmelCase_				(			self      :    int       )	->     List[Any]:
						self.assertIsNotNone(transformers.models.auto.__spec__       )
						self.assertIsNotNone(importlib.util.find_spec("transformers.models.auto"       )       )




	def   UpperCAmelCase_				(			self      :    Optional[int]       )	->     List[Any]:
						__SCREAMING_SNAKE_CASE						       =    AutoConfig.from_pretrained("bert-base-uncased"       )
						self.assertIsInstance(UpperCAmelCase__      ,      UpperCAmelCase__       )




	def   UpperCAmelCase_				(			self      :    Dict       )	->     str:
						__SCREAMING_SNAKE_CASE						       =    AutoConfig.from_pretrained(UpperCAmelCase__       )
						self.assertIsInstance(UpperCAmelCase__      ,      UpperCAmelCase__       )




	def   UpperCAmelCase_				(			self      :    int       )	->     Tuple:
						__SCREAMING_SNAKE_CASE						       =    AutoConfig.from_pretrained(UpperCAmelCase__       )
						self.assertIsInstance(UpperCAmelCase__      ,      UpperCAmelCase__       )




	def   UpperCAmelCase_				(			self      :    str       )	->     int:
						__SCREAMING_SNAKE_CASE						       =    AutoConfig.for_model("roberta"       )
						self.assertIsInstance(UpperCAmelCase__      ,      UpperCAmelCase__       )




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




	def   UpperCAmelCase_				(			self      :    Optional[int]       )	->     Any:
						try:
											AutoConfig.register("custom"      ,      UpperCAmelCase__       )
											# Wrong model type will raise an error
											with self.assertRaises(UpperCAmelCase__       ):
																AutoConfig.register("model"      ,      UpperCAmelCase__       )
											# Trying to register something existing in the Transformers library will raise an error
											with self.assertRaises(UpperCAmelCase__       ):
																AutoConfig.register("bert"      ,      UpperCAmelCase__       )

											# Now that the config is registered, it can be used as any other config with the auto-API
											__SCREAMING_SNAKE_CASE						       =    CustomConfig()
											with tempfile.TemporaryDirectory() as tmp_dir:
																config.save_pretrained(UpperCAmelCase__       )
																__SCREAMING_SNAKE_CASE						       =    AutoConfig.from_pretrained(UpperCAmelCase__       )
																self.assertIsInstance(UpperCAmelCase__      ,      UpperCAmelCase__       )

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




	def   UpperCAmelCase_				(			self      :    List[str]       )	->     Any:
						with self.assertRaisesRegex(
						    UpperCAmelCase__      ,      "bert-base is not a local folder and is not a valid model identifier"       ):
											__SCREAMING_SNAKE_CASE						       =    AutoConfig.from_pretrained("bert-base"       )




	def   UpperCAmelCase_				(			self      :    Dict       )	->     List[Any]:
						with self.assertRaisesRegex(
						    UpperCAmelCase__      ,      R"aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)"       ):
											__SCREAMING_SNAKE_CASE						       =    AutoConfig.from_pretrained(UpperCAmelCase__      ,      revision="aaaaaa"       )




	def   UpperCAmelCase_				(			self      :    List[Any]       )	->     str:
						with self.assertRaisesRegex(
						    UpperCAmelCase__      ,      "hf-internal-testing/no-config-test-repo does not appear to have a file named config.json."      ,      ):
											__SCREAMING_SNAKE_CASE						       =    AutoConfig.from_pretrained("hf-internal-testing/no-config-test-repo"       )




	def   UpperCAmelCase_				(			self      :    List[str]       )	->     str:
						# If remote code is not set, we will time out when asking whether to load the model.
						with self.assertRaises(UpperCAmelCase__       ):
											__SCREAMING_SNAKE_CASE						       =    AutoConfig.from_pretrained("hf-internal-testing/test_dynamic_model"       )
						# If remote code is disabled, we can't load this config.
						with self.assertRaises(UpperCAmelCase__       ):
											__SCREAMING_SNAKE_CASE						       =    AutoConfig.from_pretrained("hf-internal-testing/test_dynamic_model"      ,      trust_remote_code=UpperCAmelCase__       )

						__SCREAMING_SNAKE_CASE						       =    AutoConfig.from_pretrained("hf-internal-testing/test_dynamic_model"      ,      trust_remote_code=UpperCAmelCase__       )
						self.assertEqual(config.__class__.__name__      ,      "NewModelConfig"       )

						# Test config can be reloaded.
						with tempfile.TemporaryDirectory() as tmp_dir:
											config.save_pretrained(UpperCAmelCase__       )
											__SCREAMING_SNAKE_CASE						       =    AutoConfig.from_pretrained(UpperCAmelCase__      ,      trust_remote_code=UpperCAmelCase__       )
						self.assertEqual(reloaded_config.__class__.__name__      ,      "NewModelConfig"       )




	def   UpperCAmelCase_				(			self      :    List[Any]       )	->     Tuple:




						class 			UpperCamelCase_     (			UpperCamelCase):

							"""simple docstring"""





							snake_case__    :  int    =      "new-model"

						try:
											AutoConfig.register("new-model"      ,      UpperCAmelCase__       )
											# If remote code is not set, the default is to use local
											__SCREAMING_SNAKE_CASE						       =    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.
											__SCREAMING_SNAKE_CASE						       =    AutoConfig.from_pretrained("hf-internal-testing/test_dynamic_model"      ,      trust_remote_code=UpperCAmelCase__       )
											self.assertEqual(config.__class__.__name__      ,      "NewModelConfigLocal"       )

											# If remote is enabled, we load from the Hub
											__SCREAMING_SNAKE_CASE						       =    AutoConfig.from_pretrained("hf-internal-testing/test_dynamic_model"      ,      trust_remote_code=UpperCAmelCase__       )
											self.assertEqual(config.__class__.__name__      ,      "NewModelConfig"       )

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




"""simple docstring"""







import random
import unittest

import torch

from diffusers import IFInpaintingSuperResolutionPipeline
from diffusers.utils import floats_tensor
from diffusers.utils.import_utils import is_xformers_available
from diffusers.utils.testing_utils import skip_mps, torch_device

from ..pipeline_params import (
    TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS,
    TEXT_GUIDED_IMAGE_INPAINTING_PARAMS,
)
from ..test_pipelines_common import PipelineTesterMixin
from . import IFPipelineTesterMixin




@skip_mps
class 			UpperCamelCase_     (			UpperCamelCase      ,  UpperCamelCase      ,  unittest.TestCase):

	"""simple docstring"""





	snake_case__    :  str    =      IFInpaintingSuperResolutionPipeline
	snake_case__    :  Optional[Any]    =      TEXT_GUIDED_IMAGE_INPAINTING_PARAMS - {"width", "height"}
	snake_case__    :  Union[str, Any]    =      TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS.union({"original_image"})
	snake_case__    :  Dict    =      PipelineTesterMixin.required_optional_params - {"latents"}




	def   UpperCAmelCase_				(			self      :    List[Any]       )	->     Optional[int]:
						return self._get_superresolution_dummy_components()




	def   UpperCAmelCase_				(			self      :    List[str]      ,      UpperCAmelCase__      :    List[Any]      ,      UpperCAmelCase__      :    List[str]=0       )	->     List[str]:
						if str(UpperCAmelCase__       ).startswith("mps"       ):
											__SCREAMING_SNAKE_CASE						       =    torch.manual_seed(UpperCAmelCase__       )
						else:
											__SCREAMING_SNAKE_CASE						       =    torch.Generator(device=UpperCAmelCase__       ).manual_seed(UpperCAmelCase__       )

						__SCREAMING_SNAKE_CASE						       =    floats_tensor((1, 3, 1_6, 1_6)      ,      rng=random.Random(UpperCAmelCase__       )       ).to(UpperCAmelCase__       )
						__SCREAMING_SNAKE_CASE						       =    floats_tensor((1, 3, 3_2, 3_2)      ,      rng=random.Random(UpperCAmelCase__       )       ).to(UpperCAmelCase__       )
						__SCREAMING_SNAKE_CASE						       =    floats_tensor((1, 3, 3_2, 3_2)      ,      rng=random.Random(UpperCAmelCase__       )       ).to(UpperCAmelCase__       )

						__SCREAMING_SNAKE_CASE						       =    {
						    "prompt": "A painting of a squirrel eating a burger",
						    "image": image,
						    "original_image": original_image,
						    "mask_image": mask_image,
						    "generator": generator,
						    "num_inference_steps": 2,
						    "output_type": "numpy",
						}

						return inputs




	@unittest.skipIf(
	    torch_device != "cuda" or not is_xformers_available()      ,      reason="XFormers attention is only available with CUDA and `xformers` installed"      ,      )
	def   UpperCAmelCase_				(			self      :    str       )	->     Dict:
						self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1E-3       )




	def   UpperCAmelCase_				(			self      :    str       )	->     Tuple:
						self._test_save_load_optional_components()




	@unittest.skipIf(torch_device != "cuda"      ,      reason="float16 requires CUDA"       )
	def   UpperCAmelCase_				(			self      :    Tuple       )	->     Tuple:
						# Due to non-determinism in save load of the hf-internal-testing/tiny-random-t5 text encoder
						super().test_save_load_floataa(expected_max_diff=1E-1       )




	def   UpperCAmelCase_				(			self      :    Optional[Any]       )	->     List[Any]:
						self._test_attention_slicing_forward_pass(expected_max_diff=1E-2       )




	def   UpperCAmelCase_				(			self      :    Any       )	->     Union[str, Any]:
						self._test_save_load_local()




	def   UpperCAmelCase_				(			self      :    Union[str, Any]       )	->     Optional[Any]:
						self._test_inference_batch_single_identical(
						    expected_max_diff=1E-2      ,      )
