
"""simple docstring"""





import unittest

from transformers import DebertaConfig, is_torch_available
from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
							import torch

							from transformers import (
							    DebertaForMaskedLM,
							    DebertaForQuestionAnswering,
							    DebertaForSequenceClassification,
							    DebertaForTokenClassification,
							    DebertaModel,
							)
							from transformers.models.deberta.modeling_deberta import DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST






class    _UpperCAmelCase(   lowerCamelCase     ):



		def __init__( self    ,		__a    ,		__a=13    ,		__a=7    ,		__a=True    ,		__a=True    ,		__a=True    ,		__a=True    ,		__a=99    ,		__a=32    ,		__a=5    ,		__a=4    ,		__a=37    ,		__a="gelu"    ,		__a=0.1    ,		__a=0.1    ,		__a=5_12    ,		__a=16    ,		__a=2    ,		__a=0.02    ,		__a=False    ,		__a=True    ,		__a="None"    ,		__a=3    ,		__a=4    ,		__a=None    ,		)	->	Any:






							'''simple docstring'''
							_UpperCamelCase										=  parent
							_UpperCamelCase										=  batch_size
							_UpperCamelCase										=  seq_length
							_UpperCamelCase										=  is_training
							_UpperCamelCase										=  use_input_mask
							_UpperCamelCase										=  use_token_type_ids
							_UpperCamelCase										=  use_labels
							_UpperCamelCase										=  vocab_size
							_UpperCamelCase										=  hidden_size
							_UpperCamelCase										=  num_hidden_layers
							_UpperCamelCase										=  num_attention_heads
							_UpperCamelCase										=  intermediate_size
							_UpperCamelCase										=  hidden_act
							_UpperCamelCase										=  hidden_dropout_prob
							_UpperCamelCase										=  attention_probs_dropout_prob
							_UpperCamelCase										=  max_position_embeddings
							_UpperCamelCase										=  type_vocab_size
							_UpperCamelCase										=  type_sequence_label_size
							_UpperCamelCase										=  initializer_range
							_UpperCamelCase										=  num_labels
							_UpperCamelCase										=  num_choices
							_UpperCamelCase										=  relative_attention
							_UpperCamelCase										=  position_biased_input
							_UpperCamelCase										=  pos_att_type
							_UpperCamelCase										=  scope



		def 						UpperCAmelCase							( self)	->	Any:






							'''simple docstring'''
							_UpperCamelCase										=  ids_tensor([self.batch_size, self.seq_length]    ,		self.vocab_size)

							_UpperCamelCase										=  None
							if self.use_input_mask:
												_UpperCamelCase										=  ids_tensor([self.batch_size, self.seq_length]    ,		vocab_size=2)

							_UpperCamelCase										=  None
							if self.use_token_type_ids:
												_UpperCamelCase										=  ids_tensor([self.batch_size, self.seq_length]    ,		self.type_vocab_size)

							_UpperCamelCase										=  None
							_UpperCamelCase										=  None
							_UpperCamelCase										=  None
							if self.use_labels:
												_UpperCamelCase										=  ids_tensor([self.batch_size]    ,		self.type_sequence_label_size)
												_UpperCamelCase										=  ids_tensor([self.batch_size, self.seq_length]    ,		self.num_labels)
												_UpperCamelCase										=  ids_tensor([self.batch_size]    ,		self.num_choices)

							_UpperCamelCase										=  self.get_config()

							return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels



		def 						UpperCAmelCase							( self)	->	Any:






							'''simple docstring'''
							return DebertaConfig(
							    vocab_size=self.vocab_size    ,		hidden_size=self.hidden_size    ,		num_hidden_layers=self.num_hidden_layers    ,		num_attention_heads=self.num_attention_heads    ,		intermediate_size=self.intermediate_size    ,		hidden_act=self.hidden_act    ,		hidden_dropout_prob=self.hidden_dropout_prob    ,		attention_probs_dropout_prob=self.attention_probs_dropout_prob    ,		max_position_embeddings=self.max_position_embeddings    ,		type_vocab_size=self.type_vocab_size    ,		initializer_range=self.initializer_range    ,		relative_attention=self.relative_attention    ,		position_biased_input=self.position_biased_input    ,		pos_att_type=self.pos_att_type    ,		)



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






							'''simple docstring'''
							_UpperCamelCase										=  self.get_config()
							_UpperCamelCase										=  3_00
							return config



		def 						UpperCAmelCase							( self    ,		__a)	->	Tuple:






							'''simple docstring'''
							self.parent.assertListEqual(list(result.loss.size())    ,		[])



		def 						UpperCAmelCase							( self    ,		__a    ,		__a    ,		__a    ,		__a    ,		__a    ,		__a    ,		__a)	->	Any:






							'''simple docstring'''
							_UpperCamelCase										=  DebertaModel(config=__a)
							model.to(__a)
							model.eval()
							_UpperCamelCase										=  model(__a    ,		attention_mask=__a    ,		token_type_ids=__a)[0]
							_UpperCamelCase										=  model(__a    ,		token_type_ids=__a)[0]
							_UpperCamelCase										=  model(__a)[0]

							self.parent.assertListEqual(list(sequence_output.size())    ,		[self.batch_size, self.seq_length, self.hidden_size])



		def 						UpperCAmelCase							( self    ,		__a    ,		__a    ,		__a    ,		__a    ,		__a    ,		__a    ,		__a)	->	Optional[int]:






							'''simple docstring'''
							_UpperCamelCase										=  DebertaForMaskedLM(config=__a)
							model.to(__a)
							model.eval()
							_UpperCamelCase										=  model(__a    ,		attention_mask=__a    ,		token_type_ids=__a    ,		labels=__a)

							self.parent.assertEqual(result.logits.shape    ,		(self.batch_size, self.seq_length, self.vocab_size))



		def 						UpperCAmelCase							( self    ,		__a    ,		__a    ,		__a    ,		__a    ,		__a    ,		__a    ,		__a)	->	Optional[Any]:






							'''simple docstring'''
							_UpperCamelCase										=  self.num_labels
							_UpperCamelCase										=  DebertaForSequenceClassification(__a)
							model.to(__a)
							model.eval()
							_UpperCamelCase										=  model(__a    ,		attention_mask=__a    ,		token_type_ids=__a    ,		labels=__a)
							self.parent.assertListEqual(list(result.logits.size())    ,		[self.batch_size, self.num_labels])
							self.check_loss_output(__a)



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






							'''simple docstring'''
							_UpperCamelCase										=  self.num_labels
							_UpperCamelCase										=  DebertaForTokenClassification(config=__a)
							model.to(__a)
							model.eval()
							_UpperCamelCase										=  model(__a    ,		attention_mask=__a    ,		token_type_ids=__a    ,		labels=__a)
							self.parent.assertEqual(result.logits.shape    ,		(self.batch_size, self.seq_length, self.num_labels))



		def 						UpperCAmelCase							( self    ,		__a    ,		__a    ,		__a    ,		__a    ,		__a    ,		__a    ,		__a)	->	Optional[Any]:






							'''simple docstring'''
							_UpperCamelCase										=  DebertaForQuestionAnswering(config=__a)
							model.to(__a)
							model.eval()
							_UpperCamelCase										=  model(
							    __a    ,		attention_mask=__a    ,		token_type_ids=__a    ,		start_positions=__a    ,		end_positions=__a    ,		)
							self.parent.assertEqual(result.start_logits.shape    ,		(self.batch_size, self.seq_length))
							self.parent.assertEqual(result.end_logits.shape    ,		(self.batch_size, self.seq_length))




		def 						UpperCAmelCase							( self)	->	str:






							'''simple docstring'''
							_UpperCamelCase										=  self.prepare_config_and_inputs()
							(
							    (
							    _UpperCamelCase  
							)			,  (
							    _UpperCamelCase  
							)			,  (
							    _UpperCamelCase  
							)			,  (
							    _UpperCamelCase  
							)			,  (
							    _UpperCamelCase  
							)			,  (
							    _UpperCamelCase  
							)			,  (
							    _UpperCamelCase  
							)			,    
							)										=  config_and_inputs
							_UpperCamelCase										=  {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
							return config, inputs_dict






@require_torch
class    _UpperCAmelCase(   lowerCamelCase				,					lowerCamelCase				,					unittest.TestCase     ):
		lowercase__      =				(
		    (
		        DebertaModel,
		        DebertaForMaskedLM,
		        DebertaForSequenceClassification,
		        DebertaForTokenClassification,
		        DebertaForQuestionAnswering,
		    )
		    if is_torch_available()
		    else ()
		)
		lowercase__      =				(
		    {
		        'feature-extraction': DebertaModel,
		        'fill-mask': DebertaForMaskedLM,
		        'question-answering': DebertaForQuestionAnswering,
		        'text-classification': DebertaForSequenceClassification,
		        'token-classification': DebertaForTokenClassification,
		        'zero-shot': DebertaForSequenceClassification,
		    }
		    if is_torch_available()
		    else {}
		)

		lowercase__      =				True
		lowercase__      =				False
		lowercase__      =				False
		lowercase__      =				False
		lowercase__      =				False



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






							'''simple docstring'''
							_UpperCamelCase										=  DebertaModelTester(self)
							_UpperCamelCase										=  ConfigTester(self    ,		config_class=__a    ,		hidden_size=37)



		def 						UpperCAmelCase							( self)	->	Dict:






							'''simple docstring'''
							self.config_tester.run_common_tests()



		def 						UpperCAmelCase							( self)	->	Any:






							'''simple docstring'''
							_UpperCamelCase										=  self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_deberta_model(*__a)



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






							'''simple docstring'''
							_UpperCamelCase										=  self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_deberta_for_sequence_classification(*__a)



		def 						UpperCAmelCase							( self)	->	Dict:






							'''simple docstring'''
							_UpperCamelCase										=  self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_deberta_for_masked_lm(*__a)



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






							'''simple docstring'''
							_UpperCamelCase										=  self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_deberta_for_question_answering(*__a)



		def 						UpperCAmelCase							( self)	->	str:






							'''simple docstring'''
							_UpperCamelCase										=  self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_deberta_for_token_classification(*__a)




		@slow
		def 						UpperCAmelCase							( self)	->	Optional[int]:






							'''simple docstring'''
							for model_name in DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
												_UpperCamelCase										=  DebertaModel.from_pretrained(__a)
												self.assertIsNotNone(__a)






@require_torch
@require_sentencepiece
@require_tokenizers
class    _UpperCAmelCase(   unittest.TestCase     ):



		@unittest.skip(reason='''Model not available yet''')
		def 						UpperCAmelCase							( self)	->	Tuple:






							'''simple docstring'''
							pass




		@slow
		def 						UpperCAmelCase							( self)	->	Union[str, Any]:






							'''simple docstring'''
							_UpperCamelCase										=  DebertaModel.from_pretrained('''microsoft/deberta-base''')

							_UpperCamelCase										=  torch.tensor([[0, 3_14_14, 2_32, 3_28, 7_40, 11_40, 1_26_95, 69, 4_60_78, 15_88, 2]])
							_UpperCamelCase										=  torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]])
							with torch.no_grad():
												_UpperCamelCase										=  model(__a    ,		attention_mask=__a)[0]
							# compare the actual values for a slice.
							_UpperCamelCase										=  torch.tensor(
							    [[[-0.5986, -0.8055, -0.8462], [1.4484, -0.9348, -0.8059], [0.3123, 0.0032, -1.4131]]])
							self.assertTrue(torch.allclose(output[:, 1:4, 1:4]    ,		__a    ,		atol=1e-4)    ,		F'''{output[:, 1:4, 1:4]}''')








"""simple docstring"""





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														=		get_tests_dir("""fixtures/dummy-config.json""")






class    _UpperCAmelCase(   unittest.TestCase     ):



		def 						UpperCAmelCase							( self)	->	str:






							'''simple docstring'''
							_UpperCamelCase										=  0



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






							'''simple docstring'''
							self.assertIsNotNone(transformers.models.auto.__spec__)
							self.assertIsNotNone(importlib.util.find_spec('''transformers.models.auto'''))



		def 						UpperCAmelCase							( self)	->	str:






							'''simple docstring'''
							_UpperCamelCase										=  AutoConfig.from_pretrained('''bert-base-uncased''')
							self.assertIsInstance(__a    ,		__a)



		def 						UpperCAmelCase							( self)	->	Tuple:






							'''simple docstring'''
							_UpperCamelCase										=  AutoConfig.from_pretrained(__a)
							self.assertIsInstance(__a    ,		__a)



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






							'''simple docstring'''
							_UpperCamelCase										=  AutoConfig.from_pretrained(__a)
							self.assertIsInstance(__a    ,		__a)



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






							'''simple docstring'''
							_UpperCamelCase										=  AutoConfig.for_model('''roberta''')
							self.assertIsInstance(__a    ,		__a)



		def 						UpperCAmelCase							( self)	->	Dict:






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



		def 						UpperCAmelCase							( self)	->	Tuple:






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

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



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






							'''simple docstring'''
							with self.assertRaisesRegex(
							    __a    ,		'''bert-base is not a local folder and is not a valid model identifier'''):
												_UpperCamelCase										=  AutoConfig.from_pretrained('''bert-base''')



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






							'''simple docstring'''
							with self.assertRaisesRegex(
							    __a    ,		R'''aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)'''):
												_UpperCamelCase										=  AutoConfig.from_pretrained(__a    ,		revision='''aaaaaa''')



		def 						UpperCAmelCase							( 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.'''    ,		):
												_UpperCamelCase										=  AutoConfig.from_pretrained('''hf-internal-testing/no-config-test-repo''')



		def 						UpperCAmelCase							( self)	->	int:






							'''simple docstring'''
							# If remote code is not set, we will time out when asking whether to load the model.
							with self.assertRaises(__a):
												_UpperCamelCase										=  AutoConfig.from_pretrained('''hf-internal-testing/test_dynamic_model''')
							# If remote code is disabled, we can't load this config.
							with self.assertRaises(__a):
												_UpperCamelCase										=  AutoConfig.from_pretrained('''hf-internal-testing/test_dynamic_model'''    ,		trust_remote_code=__a)

							_UpperCamelCase										=  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)
												_UpperCamelCase										=  AutoConfig.from_pretrained(__a    ,		trust_remote_code=__a)
							self.assertEqual(reloaded_config.__class__.__name__    ,		'''NewModelConfig''')




		def 						UpperCAmelCase							( self)	->	Any:






							'''simple docstring'''






							class    _UpperCAmelCase(   lowerCamelCase     ):
									lowercase__      =				'new-model'

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






