




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


UpperCamelCase__         =      get_tests_dir("""fixtures/dummy-config.json""")
class       a__   ( unittest.TestCase				):






						def     __SCREAMING_SNAKE_CASE(       self      ):




											"""simple docstring"""



											__lowerCAmelCase           =	0






						def     __SCREAMING_SNAKE_CASE(       self      ):




											"""simple docstring"""



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






						def     __SCREAMING_SNAKE_CASE(       self      ):




											"""simple docstring"""



											__lowerCAmelCase           =	AutoConfig.from_pretrained("bert-base-uncased"      )
											self.assertIsInstance(_A		,      _A      )






						def     __SCREAMING_SNAKE_CASE(       self      ):




											"""simple docstring"""



											__lowerCAmelCase           =	AutoConfig.from_pretrained(_A      )
											self.assertIsInstance(_A		,      _A      )






						def     __SCREAMING_SNAKE_CASE(       self      ):




											"""simple docstring"""



											__lowerCAmelCase           =	AutoConfig.from_pretrained(_A      )
											self.assertIsInstance(_A		,      _A      )






						def     __SCREAMING_SNAKE_CASE(       self      ):




											"""simple docstring"""



											__lowerCAmelCase           =	AutoConfig.for_model("roberta"      )
											self.assertIsInstance(_A		,      _A      )






						def     __SCREAMING_SNAKE_CASE(       self      ):




											"""simple docstring"""



											with tempfile.TemporaryDirectory() as tmp_dir:
																# This model name contains bert and roberta, but roberta ends up being picked.
																__lowerCAmelCase           =	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({}      )      )
																__lowerCAmelCase           =	AutoConfig.from_pretrained(_A      )
																self.assertEqual(type(_A      )		,      _A      )






						def     __SCREAMING_SNAKE_CASE(       self      ):




											"""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
																__lowerCAmelCase           =	CustomConfig()
																with tempfile.TemporaryDirectory() as tmp_dir:
																					config.save_pretrained(_A      )
																					__lowerCAmelCase           =	AutoConfig.from_pretrained(_A      )
																					self.assertIsInstance(_A		,      _A      )

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






						def     __SCREAMING_SNAKE_CASE(       self      ):




											"""simple docstring"""



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






						def     __SCREAMING_SNAKE_CASE(       self      ):




											"""simple docstring"""



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






						def     __SCREAMING_SNAKE_CASE(       self      ):




											"""simple docstring"""



											with self.assertRaisesRegex(
											    _A		,      "hf-internal-testing/no-config-test-repo does not appear to have a file named config.json."		,      ):
																__lowerCAmelCase           =	AutoConfig.from_pretrained("hf-internal-testing/no-config-test-repo"      )






						def     __SCREAMING_SNAKE_CASE(       self      ):




											"""simple docstring"""



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

											__lowerCAmelCase           =	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      )
																__lowerCAmelCase           =	AutoConfig.from_pretrained(_A		,      trust_remote_code=_A      )
											self.assertEqual(reloaded_config.__class__.__name__		,      "NewModelConfig"      )






						def     __SCREAMING_SNAKE_CASE(       self      ):




											"""simple docstring"""



											class       a__   ( snake_case__				):
																	_a							:   List[str] =   """new-model"""

											try:
																AutoConfig.register("new-model"		,      _A      )
																# If remote code is not set, the default is to use local
																__lowerCAmelCase           =	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.
																__lowerCAmelCase           =	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
																__lowerCAmelCase           =	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"]










import unittest

import numpy as np
import torch

from diffusers import ScoreSdeVePipeline, ScoreSdeVeScheduler, UNetaDModel
from diffusers.utils.testing_utils import enable_full_determinism, require_torch, slow, torch_device


enable_full_determinism()
class       a__   ( unittest.TestCase				):






						@property
						def     __SCREAMING_SNAKE_CASE(       self      ):




											"""simple docstring"""



											torch.manual_seed(0      )
											__lowerCAmelCase           =	UNetaDModel(
											    block_out_channels=(3_2, 6_4)		,      layers_per_block=2		,      sample_size=3_2		,      in_channels=3		,      out_channels=3		,      down_block_types=("DownBlock2D", "AttnDownBlock2D")		,      up_block_types=("AttnUpBlock2D", "UpBlock2D")		,      )
											return model






						def     __SCREAMING_SNAKE_CASE(       self      ):




											"""simple docstring"""



											__lowerCAmelCase           =	self.dummy_uncond_unet
											__lowerCAmelCase           =	ScoreSdeVeScheduler()

											__lowerCAmelCase           =	ScoreSdeVePipeline(unet=_A		,      scheduler=_A      )
											sde_ve.to(_A      )
											sde_ve.set_progress_bar_config(disable=_A      )

											__lowerCAmelCase           =	torch.manual_seed(0      )
											__lowerCAmelCase           =	sde_ve(num_inference_steps=2		,      output_type="numpy"		,      generator=_A      ).images

											__lowerCAmelCase           =	torch.manual_seed(0      )
											__lowerCAmelCase           =	sde_ve(num_inference_steps=2		,      output_type="numpy"		,      generator=_A		,      return_dict=_A      )[
											    0
											]

											__lowerCAmelCase           =	image[0, -3:, -3:, -1]
											__lowerCAmelCase           =	image_from_tuple[0, -3:, -3:, -1]

											assert image.shape == (1, 3_2, 3_2, 3)
											__lowerCAmelCase           =	np.array([0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0]      )

											assert np.abs(image_slice.flatten() - expected_slice      ).max() < 1E-2
											assert np.abs(image_from_tuple_slice.flatten() - expected_slice      ).max() < 1E-2






@slow
@require_torch
class       a__   ( unittest.TestCase				):






						def     __SCREAMING_SNAKE_CASE(       self      ):




											"""simple docstring"""



											__lowerCAmelCase           =	"google/ncsnpp-church-256"
											__lowerCAmelCase           =	UNetaDModel.from_pretrained(_A      )

											__lowerCAmelCase           =	ScoreSdeVeScheduler.from_pretrained(_A      )

											__lowerCAmelCase           =	ScoreSdeVePipeline(unet=_A		,      scheduler=_A      )
											sde_ve.to(_A      )
											sde_ve.set_progress_bar_config(disable=_A      )

											__lowerCAmelCase           =	torch.manual_seed(0      )
											__lowerCAmelCase           =	sde_ve(num_inference_steps=1_0		,      output_type="numpy"		,      generator=_A      ).images

											__lowerCAmelCase           =	image[0, -3:, -3:, -1]

											assert image.shape == (1, 2_5_6, 2_5_6, 3)

											__lowerCAmelCase           =	np.array([0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0]      )

											assert np.abs(image_slice.flatten() - expected_slice      ).max() < 1E-2




