




'''simple docstring'''


from __future__ import annotations

import unittest

from transformers import DebertaVaConfig, is_tf_available
from transformers.testing_utils import require_tf, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
		import tensorflow as tf

		from transformers import (
		    TFDebertaVaForMaskedLM,
		    TFDebertaVaForQuestionAnswering,
		    TFDebertaVaForSequenceClassification,
		    TFDebertaVaForTokenClassification,
		    TFDebertaVaModel,
		)

class       A   :







							def __init__(  self	:  List[Any]							,      lowerCAmelCase_	:  str							,      lowerCAmelCase_	:  List[Any]=13							,      lowerCAmelCase_	:  Any=7							,      lowerCAmelCase_	:  Union[str, Any]=True							,      lowerCAmelCase_	:  Optional[int]=True							,      lowerCAmelCase_	:  Union[str, Any]=True							,      lowerCAmelCase_	:  Union[str, Any]=True							,      lowerCAmelCase_	:  List[Any]=99							,      lowerCAmelCase_	:  Union[str, Any]=32							,      lowerCAmelCase_	:  Dict=2							,      lowerCAmelCase_	:  Tuple=4							,      lowerCAmelCase_	:  Tuple=37							,      lowerCAmelCase_	:  Union[str, Any]="gelu"							,      lowerCAmelCase_	:  Optional[Any]=0.1							,      lowerCAmelCase_	:  Optional[int]=0.1							,      lowerCAmelCase_	:  List[str]=5_12							,      lowerCAmelCase_	:  str=16							,      lowerCAmelCase_	:  str=2							,      lowerCAmelCase_	:  Dict=0.0_2							,      lowerCAmelCase_	:  Tuple=False							,      lowerCAmelCase_	:  Any=True							,      lowerCAmelCase_	:  Any="None"							,      lowerCAmelCase_	:  Any=3							,      lowerCAmelCase_	:  Any=4							,      lowerCAmelCase_	:  List[Any]=None							,      )      ->							List[Any]:


														"""simple docstring"""


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







							def        __lowerCAmelCase      (  self	:  Optional[int] )      ->							Any:


														"""simple docstring"""


														_a     					=    ids_tensor([self.batch_size, self.seq_length]							,      self.vocab_size )

														_a     					=    None
														if self.use_input_mask:
																					_a     					=    random_attention_mask([self.batch_size, self.seq_length] )

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

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

														_a     					=    DebertaVaConfig(
														    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							,      relative_attention=self.relative_attention							,      position_biased_input=self.position_biased_input							,      initializer_range=self.initializer_range							,      return_dict=lowerCAmelCase_							,      )

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







							def        __lowerCAmelCase      (  self	:  Optional[Any]							,      lowerCAmelCase_	:  Dict							,      lowerCAmelCase_	:  int							,      lowerCAmelCase_	:  Any							,      lowerCAmelCase_	:  Any							,      lowerCAmelCase_	:  str							,      lowerCAmelCase_	:  Tuple							,      lowerCAmelCase_	:  Any )      ->							Union[str, Any]:


														"""simple docstring"""


														_a     					=    TFDebertaVaModel(config=lowerCAmelCase_ )
														_a     					=    {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}

														_a     					=    [input_ids, input_mask]
														_a     					=    model(lowerCAmelCase_ )

														_a     					=    model(lowerCAmelCase_ )

														self.parent.assertEqual(result.last_hidden_state.shape							,      (self.batch_size, self.seq_length, self.hidden_size) )







							def        __lowerCAmelCase      (  self	:  int							,      lowerCAmelCase_	:  Union[str, Any]							,      lowerCAmelCase_	:  List[str]							,      lowerCAmelCase_	:  Union[str, Any]							,      lowerCAmelCase_	:  str							,      lowerCAmelCase_	:  List[str]							,      lowerCAmelCase_	:  Optional[Any]							,      lowerCAmelCase_	:  Optional[int] )      ->							str:


														"""simple docstring"""


														_a     					=    TFDebertaVaForMaskedLM(config=lowerCAmelCase_ )
														_a     					=    {
														    '''input_ids''': input_ids,
														    '''attention_mask''': input_mask,
														    '''token_type_ids''': token_type_ids,
														}
														_a     					=    model(lowerCAmelCase_ )
														self.parent.assertEqual(result.logits.shape							,      (self.batch_size, self.seq_length, self.vocab_size) )







							def        __lowerCAmelCase      (  self	:  str							,      lowerCAmelCase_	:  List[str]							,      lowerCAmelCase_	:  Optional[Any]							,      lowerCAmelCase_	:  List[str]							,      lowerCAmelCase_	:  Optional[int]							,      lowerCAmelCase_	:  Any							,      lowerCAmelCase_	:  Any							,      lowerCAmelCase_	:  Optional[Any] )      ->							str:


														"""simple docstring"""


														_a     					=    self.num_labels
														_a     					=    TFDebertaVaForSequenceClassification(config=lowerCAmelCase_ )
														_a     					=    {
														    '''input_ids''': input_ids,
														    '''attention_mask''': input_mask,
														    '''token_type_ids''': token_type_ids,
														}

														_a     					=    model(lowerCAmelCase_ )
														self.parent.assertEqual(result.logits.shape							,      (self.batch_size, self.num_labels) )







							def        __lowerCAmelCase      (  self	:  str							,      lowerCAmelCase_	:  List[Any]							,      lowerCAmelCase_	:  int							,      lowerCAmelCase_	:  Tuple							,      lowerCAmelCase_	:  Tuple							,      lowerCAmelCase_	:  Union[str, Any]							,      lowerCAmelCase_	:  Union[str, Any]							,      lowerCAmelCase_	:  Optional[Any] )      ->							Dict:


														"""simple docstring"""


														_a     					=    self.num_labels
														_a     					=    TFDebertaVaForTokenClassification(config=lowerCAmelCase_ )
														_a     					=    {
														    '''input_ids''': input_ids,
														    '''attention_mask''': input_mask,
														    '''token_type_ids''': token_type_ids,
														}
														_a     					=    model(lowerCAmelCase_ )
														self.parent.assertEqual(result.logits.shape							,      (self.batch_size, self.seq_length, self.num_labels) )







							def        __lowerCAmelCase      (  self	:  Optional[Any]							,      lowerCAmelCase_	:  List[Any]							,      lowerCAmelCase_	:  int							,      lowerCAmelCase_	:  Dict							,      lowerCAmelCase_	:  Optional[int]							,      lowerCAmelCase_	:  Union[str, Any]							,      lowerCAmelCase_	:  Tuple							,      lowerCAmelCase_	:  Dict )      ->							int:


														"""simple docstring"""


														_a     					=    TFDebertaVaForQuestionAnswering(config=lowerCAmelCase_ )
														_a     					=    {
														    '''input_ids''': input_ids,
														    '''attention_mask''': input_mask,
														    '''token_type_ids''': token_type_ids,
														}

														_a     					=    model(lowerCAmelCase_ )
														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        __lowerCAmelCase      (  self	:  Tuple )      ->							List[Any]:


														"""simple docstring"""


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



@require_tf
class       A   (		_a					,_a					,unittest.TestCase   ):
							lowercase_          =    (
							    (
							        TFDebertaVaModel,
							        TFDebertaVaForMaskedLM,
							        TFDebertaVaForQuestionAnswering,
							        TFDebertaVaForSequenceClassification,
							        TFDebertaVaForTokenClassification,
							    )
							    if is_tf_available()
							    else ()
							)
							lowercase_          =    (
							    {
							        'feature-extraction': TFDebertaVaModel,
							        'fill-mask': TFDebertaVaForMaskedLM,
							        'question-answering': TFDebertaVaForQuestionAnswering,
							        'text-classification': TFDebertaVaForSequenceClassification,
							        'token-classification': TFDebertaVaForTokenClassification,
							        'zero-shot': TFDebertaVaForSequenceClassification,
							    }
							    if is_tf_available()
							    else {}
							)

							lowercase_          =    False
							lowercase_          =    False







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


														"""simple docstring"""


														_a     					=    TFDebertaVaModelTester(self )
														_a     					=    ConfigTester(self							,      config_class=lowerCAmelCase_							,      hidden_size=37 )







							def        __lowerCAmelCase      (  self	:  Dict )      ->							Tuple:


														"""simple docstring"""


														self.config_tester.run_common_tests()







							def        __lowerCAmelCase      (  self	:  List[Any] )      ->							Optional[Any]:


														"""simple docstring"""


														_a     					=    self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_model(*lowerCAmelCase_ )







							def        __lowerCAmelCase      (  self	:  Tuple )      ->							List[str]:


														"""simple docstring"""


														_a     					=    self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_for_masked_lm(*lowerCAmelCase_ )







							def        __lowerCAmelCase      (  self	:  str )      ->							Optional[Any]:


														"""simple docstring"""


														_a     					=    self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_for_question_answering(*lowerCAmelCase_ )







							def        __lowerCAmelCase      (  self	:  Any )      ->							Dict:


														"""simple docstring"""


														_a     					=    self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_for_sequence_classification(*lowerCAmelCase_ )







							def        __lowerCAmelCase      (  self	:  List[Any] )      ->							int:


														"""simple docstring"""


														_a     					=    self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_for_token_classification(*lowerCAmelCase_ )







							@slow
							def        __lowerCAmelCase      (  self	:  int )      ->							str:


														"""simple docstring"""


														_a     					=    TFDebertaVaModel.from_pretrained('''kamalkraj/deberta-v2-xlarge''' )
														self.assertIsNotNone(lowerCAmelCase_ )



@require_tf
class       A   (		unittest.TestCase   ):







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


														"""simple docstring"""


														pass







							@slow
							def        __lowerCAmelCase      (  self	:  List[Any] )      ->							List[Any]:


														"""simple docstring"""


														_a     					=    TFDebertaVaModel.from_pretrained('''kamalkraj/deberta-v2-xlarge''' )
														_a     					=    tf.constant([[0, 3_14_14, 2_32, 3_28, 7_40, 11_40, 1_26_95, 69, 4_60_78, 15_88, 2]] )
														_a     					=    tf.constant([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]] )
														_a     					=    model(lowerCAmelCase_							,      attention_mask=lowerCAmelCase_ )[0]

														_a     					=    tf.constant(
														    [[[0.2_3_5_6, 0.1_9_4_8, 0.0_3_6_9], [-0.1_0_6_3, 0.3_5_8_6, -0.5_1_5_2], [-0.6_3_9_9, -0.0_2_5_9, -0.2_5_2_5]]] )
														tf.debugging.assert_near(output[:, 1:4, 1:4]							,      lowerCAmelCase_							,      atol=1e-4 )







'''simple docstring'''


import tempfile
import unittest

import numpy as np
from huggingface_hub import HfFolder, delete_repo
from requests.exceptions import HTTPError

from transformers import BertConfig, is_flax_available
from transformers.testing_utils import TOKEN, USER, is_staging_test, require_flax


if is_flax_available():
				import os

				from flax.core.frozen_dict import unfreeze
				from flax.traverse_util import flatten_dict

				from transformers import FlaxBertModel

				_snake_case				:				Optional[Any]       							=		'0.12'  # assumed parallelism: 8

@require_flax
@is_staging_test
class       A   (		unittest.TestCase   ):







							@classmethod
							def        __lowerCAmelCase      (  cls	:  Tuple )      ->							int:


														"""simple docstring"""


														_a     					=    TOKEN
														HfFolder.save_token(lowerCAmelCase_ )







							@classmethod
							def        __lowerCAmelCase      (  cls	:  Tuple )      ->							Optional[int]:


														"""simple docstring"""


														try:
																					delete_repo(token=cls._token							,      repo_id='''test-model-flax''' )
														except HTTPError:
																					pass

														try:
																					delete_repo(token=cls._token							,      repo_id='''valid_org/test-model-flax-org''' )
														except HTTPError:
																					pass







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


														"""simple docstring"""


														_a     					=    BertConfig(
														    vocab_size=99							,      hidden_size=32							,      num_hidden_layers=5							,      num_attention_heads=4							,      intermediate_size=37 )
														_a     					=    FlaxBertModel(lowerCAmelCase_ )
														model.push_to_hub('''test-model-flax'''							,      use_auth_token=self._token )

														_a     					=    FlaxBertModel.from_pretrained(F'{USER}/test-model-flax' )

														_a     					=    flatten_dict(unfreeze(model.params ) )
														_a     					=    flatten_dict(unfreeze(new_model.params ) )

														for key in base_params.keys():
																					_a     					=    (base_params[key] - new_params[key]).sum().item()
																					self.assertLessEqual(lowerCAmelCase_							,      1e-3							,      msg=F'{key} not identical' )

														# Reset repo
														delete_repo(token=self._token							,      repo_id='''test-model-flax''' )

														# Push to hub via save_pretrained
														with tempfile.TemporaryDirectory() as tmp_dir:
																					model.save_pretrained(lowerCAmelCase_							,      repo_id='''test-model-flax'''							,      push_to_hub=lowerCAmelCase_							,      use_auth_token=self._token )

														_a     					=    FlaxBertModel.from_pretrained(F'{USER}/test-model-flax' )

														_a     					=    flatten_dict(unfreeze(model.params ) )
														_a     					=    flatten_dict(unfreeze(new_model.params ) )

														for key in base_params.keys():
																					_a     					=    (base_params[key] - new_params[key]).sum().item()
																					self.assertLessEqual(lowerCAmelCase_							,      1e-3							,      msg=F'{key} not identical' )







							def        __lowerCAmelCase      (  self	:  int )      ->							Tuple:


														"""simple docstring"""


														_a     					=    BertConfig(
														    vocab_size=99							,      hidden_size=32							,      num_hidden_layers=5							,      num_attention_heads=4							,      intermediate_size=37 )
														_a     					=    FlaxBertModel(lowerCAmelCase_ )
														model.push_to_hub('''valid_org/test-model-flax-org'''							,      use_auth_token=self._token )

														_a     					=    FlaxBertModel.from_pretrained('''valid_org/test-model-flax-org''' )

														_a     					=    flatten_dict(unfreeze(model.params ) )
														_a     					=    flatten_dict(unfreeze(new_model.params ) )

														for key in base_params.keys():
																					_a     					=    (base_params[key] - new_params[key]).sum().item()
																					self.assertLessEqual(lowerCAmelCase_							,      1e-3							,      msg=F'{key} not identical' )

														# Reset repo
														delete_repo(token=self._token							,      repo_id='''valid_org/test-model-flax-org''' )

														# Push to hub via save_pretrained
														with tempfile.TemporaryDirectory() as tmp_dir:
																					model.save_pretrained(
																					    lowerCAmelCase_							,      repo_id='''valid_org/test-model-flax-org'''							,      push_to_hub=lowerCAmelCase_							,      use_auth_token=self._token )

														_a     					=    FlaxBertModel.from_pretrained('''valid_org/test-model-flax-org''' )

														_a     					=    flatten_dict(unfreeze(model.params ) )
														_a     					=    flatten_dict(unfreeze(new_model.params ) )

														for key in base_params.keys():
																					_a     					=    (base_params[key] - new_params[key]).sum().item()
																					self.assertLessEqual(lowerCAmelCase_							,      1e-3							,      msg=F'{key} not identical' )



def 		snake_case_   (UpperCamelCase	:  List[str]			,						UpperCamelCase	:  Optional[Any]				):





							'''simple docstring'''


							_a     					=    True
							_a     					=    flatten_dict(modela.params				)
							_a     					=    flatten_dict(modela.params				)
							for key in flat_params_a.keys():
														if np.sum(np.abs(flat_params_a[key] - flat_params_a[key]				)				) > 1e-4:
																					_a     					=    False

							return models_are_equal

@require_flax
class       A   (		unittest.TestCase   ):







							def        __lowerCAmelCase      (  self	:  Dict )      ->							int:


														"""simple docstring"""


														_a     					=    BertConfig.from_pretrained('''hf-internal-testing/tiny-bert-flax-only''' )
														_a     					=    FlaxBertModel(lowerCAmelCase_ )

														_a     					=    '''bert'''
														with tempfile.TemporaryDirectory() as tmp_dir:
																					model.save_pretrained(os.path.join(lowerCAmelCase_							,      lowerCAmelCase_ ) )

																					with self.assertRaises(lowerCAmelCase_ ):
																												_a     					=    FlaxBertModel.from_pretrained(lowerCAmelCase_ )

																					_a     					=    FlaxBertModel.from_pretrained(lowerCAmelCase_							,      subfolder=lowerCAmelCase_ )

														self.assertTrue(check_models_equal(lowerCAmelCase_							,      lowerCAmelCase_ ) )







							def        __lowerCAmelCase      (  self	:  Union[str, Any] )      ->							Optional[int]:


														"""simple docstring"""


														_a     					=    BertConfig.from_pretrained('''hf-internal-testing/tiny-bert-flax-only''' )
														_a     					=    FlaxBertModel(lowerCAmelCase_ )

														_a     					=    '''bert'''
														with tempfile.TemporaryDirectory() as tmp_dir:
																					model.save_pretrained(os.path.join(lowerCAmelCase_							,      lowerCAmelCase_ )							,      max_shard_size='''10KB''' )

																					with self.assertRaises(lowerCAmelCase_ ):
																												_a     					=    FlaxBertModel.from_pretrained(lowerCAmelCase_ )

																					_a     					=    FlaxBertModel.from_pretrained(lowerCAmelCase_							,      subfolder=lowerCAmelCase_ )

														self.assertTrue(check_models_equal(lowerCAmelCase_							,      lowerCAmelCase_ ) )







							def        __lowerCAmelCase      (  self	:  Optional[Any] )      ->							List[str]:


														"""simple docstring"""


														_a     					=    '''bert'''
														_a     					=    '''hf-internal-testing/tiny-random-bert-subfolder'''

														with self.assertRaises(lowerCAmelCase_ ):
																					_a     					=    FlaxBertModel.from_pretrained(lowerCAmelCase_ )

														_a     					=    FlaxBertModel.from_pretrained(lowerCAmelCase_							,      subfolder=lowerCAmelCase_ )

														self.assertIsNotNone(lowerCAmelCase_ )







							def        __lowerCAmelCase      (  self	:  Tuple )      ->							Tuple:


														"""simple docstring"""


														_a     					=    '''bert'''
														_a     					=    '''hf-internal-testing/tiny-random-bert-sharded-subfolder'''
														with self.assertRaises(lowerCAmelCase_ ):
																					_a     					=    FlaxBertModel.from_pretrained(lowerCAmelCase_ )

														_a     					=    FlaxBertModel.from_pretrained(lowerCAmelCase_							,      subfolder=lowerCAmelCase_ )

														self.assertIsNotNone(lowerCAmelCase_ )

