






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      :    Union[str, Any]   		=   get_tests_dir("""fixtures/dummy-config.json""")



class 							SCREAMING_SNAKE_CASE__		(       unittest.TestCase		):





					"""simple docstring"""






					def  _lowercase  (      self  :						Optional[int] ):
						snake_case__				:   str     =						0



					def  _lowercase  (      self  :						int ):
						self.assertIsNotNone(transformers.models.auto.__spec__ )
						self.assertIsNotNone(importlib.util.find_spec("transformers.models.auto" ) )



					def  _lowercase  (      self  :						List[Any] ):
						snake_case__				:   str     =						AutoConfig.from_pretrained("bert-base-uncased" )
						self.assertIsInstance(__A							,	__A )



					def  _lowercase  (      self  :						Union[str, Any] ):
						snake_case__				:   Optional[int]     =						AutoConfig.from_pretrained(__A )
						self.assertIsInstance(__A							,	__A )



					def  _lowercase  (      self  :						List[Any] ):
						snake_case__				:   Any     =						AutoConfig.from_pretrained(__A )
						self.assertIsInstance(__A							,	__A )



					def  _lowercase  (      self  :						List[Any] ):
						snake_case__				:   Tuple     =						AutoConfig.for_model("roberta" )
						self.assertIsInstance(__A							,	__A )



					def  _lowercase  (      self  :						Tuple ):
						with tempfile.TemporaryDirectory() as tmp_dir:
							# This model name contains bert and roberta, but roberta ends up being picked.
							snake_case__				:   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__				:   List[Any]     =						AutoConfig.from_pretrained(__A )
							self.assertEqual(type(__A )							,	__A )



					def  _lowercase  (      self  :						List[str] ):
						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__				:   str     =						CustomConfig()
							with tempfile.TemporaryDirectory() as tmp_dir:
								config.save_pretrained(__A )
								snake_case__				:   List[str]     =						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  :						Union[str, Any] ):
						with self.assertRaisesRegex(
						    __A							,	"bert-base is not a local folder and is not a valid model identifier" ):
							snake_case__				:   Any     =						AutoConfig.from_pretrained("bert-base" )



					def  _lowercase  (      self  :						int ):
						with self.assertRaisesRegex(
						    __A							,	R"aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)" ):
							snake_case__				:   Optional[Any]     =						AutoConfig.from_pretrained(__A							,	revision="aaaaaa" )



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



					def  _lowercase  (      self  :						List[str] ):
						# If remote code is not set, we will time out when asking whether to load the model.
						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__				:   Union[str, Any]     =						AutoConfig.from_pretrained("hf-internal-testing/test_dynamic_model"							,	trust_remote_code=__A )

						snake_case__				:   Optional[int]     =						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  :						Any ):



						class 							SCREAMING_SNAKE_CASE__		(       UpperCamelCase_		):





											"""simple docstring"""






											a_			=	"new-model"

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

from transformers import is_torch_available
from transformers.testing_utils import require_torch


if is_torch_available():
	import torch

	from transformers.activations import gelu_new, gelu_python, get_activation



@require_torch
class 							SCREAMING_SNAKE_CASE__		(       unittest.TestCase		):





					"""simple docstring"""






					def  _lowercase  (      self  :						Tuple ):
						snake_case__				:   List[str]     =						torch.tensor([-1_0_0, -1, -0.1, 0, 0.1, 1.0, 1_0_0] )
						snake_case__				:   Tuple     =						get_activation("gelu" )
						self.assertTrue(torch.allclose(gelu_python(__A )							,	torch_builtin(__A ) ) )
						self.assertFalse(torch.allclose(gelu_python(__A )							,	gelu_new(__A ) ) )



					def  _lowercase  (      self  :						Dict ):
						snake_case__				:   str     =						torch.tensor([-1_0_0, -1, -0.1, 0, 0.1, 1.0, 1_0_0] )
						snake_case__				:   Union[str, Any]     =						get_activation("gelu" )
						snake_case__				:   int     =						get_activation("gelu_10" )

						snake_case__				:   Optional[int]     =						torch_builtin(__A )
						snake_case__				:   Dict     =						geluaa(__A )

						snake_case__				:   Optional[Any]     =						torch.where(y_gelu_aa < 1_0.0							,	1							,	0 )

						self.assertTrue(torch.max(__A ).item() == 1_0.0 )
						self.assertTrue(torch.allclose(y_gelu * clipped_mask							,	y_gelu_aa * clipped_mask ) )



					def  _lowercase  (      self  :						str ):
						get_activation("gelu" )
						get_activation("gelu_10" )
						get_activation("gelu_fast" )
						get_activation("gelu_new" )
						get_activation("gelu_python" )
						get_activation("gelu_pytorch_tanh" )
						get_activation("linear" )
						get_activation("mish" )
						get_activation("quick_gelu" )
						get_activation("relu" )
						get_activation("sigmoid" )
						get_activation("silu" )
						get_activation("swish" )
						get_activation("tanh" )
						with self.assertRaises(__A ):
							get_activation("bogus" )
						with self.assertRaises(__A ):
							get_activation(__A )






					def  _lowercase  (      self  :						List[str] ):
						snake_case__				:   List[str]     =						get_activation("gelu" )
						snake_case__				:   Any     =						1
						snake_case__				:   Union[str, Any]     =						get_activation("gelu" )
						self.assertEqual(acta.a							,	1 )
						with self.assertRaises(__A ):
							snake_case__				:   int     =						acta.a



