


'''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 							__UpperCamelCase    :




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

						'''simple docstring'''





						__a      :	str    =  parent
						__a      :	Dict    =  batch_size
						__a      :	str    =  seq_length
						__a      :	Optional[Any]    =  is_training
						__a      :	Union[str, Any]    =  use_input_mask
						__a      :	int    =  use_token_type_ids
						__a      :	Union[str, Any]    =  use_labels
						__a      :	int    =  vocab_size
						__a      :	Optional[Any]    =  hidden_size
						__a      :	Dict    =  num_hidden_layers
						__a      :	Optional[int]    =  num_attention_heads
						__a      :	List[Any]    =  intermediate_size
						__a      :	List[Any]    =  hidden_act
						__a      :	Tuple    =  hidden_dropout_prob
						__a      :	str    =  attention_probs_dropout_prob
						__a      :	int    =  max_position_embeddings
						__a      :	Tuple    =  type_vocab_size
						__a      :	List[Any]    =  type_sequence_label_size
						__a      :	Optional[int]    =  initializer_range
						__a      :	int    =  num_labels
						__a      :	str    =  num_choices
						__a      :	int    =  relative_attention
						__a      :	Dict    =  position_biased_input
						__a      :	Tuple    =  pos_att_type
						__a      :	Optional[Any]    =  scope




	def 				__UpperCAmelCase							(       self							):

						'''simple docstring'''





						__a      :	Union[str, Any]    =  ids_tensor([self.batch_size, self.seq_length]						,    self.vocab_size							)

						__a      :	int    =  None
						if self.use_input_mask:
											__a      :	Union[str, Any]    =  random_attention_mask([self.batch_size, self.seq_length]							)

						__a      :	List[Any]    =  None
						if self.use_token_type_ids:
											__a      :	int    =  ids_tensor([self.batch_size, self.seq_length]						,    self.type_vocab_size							)

						__a      :	Optional[Any]    =  None
						__a      :	str    =  None
						__a      :	Any    =  None
						if self.use_labels:
											__a      :	Optional[int]    =  ids_tensor([self.batch_size]						,    self.type_sequence_label_size							)
											__a      :	List[str]    =  ids_tensor([self.batch_size, self.seq_length]						,    self.num_labels							)

						__a      :	Tuple    =  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=_SCREAMING_SNAKE_CASE						,    )

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




	def 				__UpperCAmelCase							(       self						,    __a						,    __a						,    __a						,    __a						,    __a						,    __a						,    __a							):

						'''simple docstring'''





						__a      :	str    =  TFDebertaVaModel(config=_SCREAMING_SNAKE_CASE							)
						__a      :	Optional[Any]    =  {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}

						__a      :	Optional[Any]    =  [input_ids, input_mask]
						__a      :	Optional[int]    =  model(_SCREAMING_SNAKE_CASE							)

						__a      :	Optional[Any]    =  model(_SCREAMING_SNAKE_CASE							)

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




	def 				__UpperCAmelCase							(       self						,    __a						,    __a						,    __a						,    __a						,    __a						,    __a						,    __a							):

						'''simple docstring'''





						__a      :	Dict    =  TFDebertaVaForMaskedLM(config=_SCREAMING_SNAKE_CASE							)
						__a      :	Tuple    =  {
						    """input_ids""": input_ids,
						    """attention_mask""": input_mask,
						    """token_type_ids""": token_type_ids,
						}
						__a      :	Any    =  model(_SCREAMING_SNAKE_CASE							)
						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							):

						'''simple docstring'''





						__a      :	Optional[int]    =  self.num_labels
						__a      :	List[Any]    =  TFDebertaVaForSequenceClassification(config=_SCREAMING_SNAKE_CASE							)
						__a      :	Tuple    =  {
						    """input_ids""": input_ids,
						    """attention_mask""": input_mask,
						    """token_type_ids""": token_type_ids,
						}

						__a      :	Dict    =  model(_SCREAMING_SNAKE_CASE							)
						self.parent.assertEqual(result.logits.shape						,    (self.batch_size, self.num_labels)							)




	def 				__UpperCAmelCase							(       self						,    __a						,    __a						,    __a						,    __a						,    __a						,    __a						,    __a							):

						'''simple docstring'''





						__a      :	Optional[int]    =  self.num_labels
						__a      :	Optional[Any]    =  TFDebertaVaForTokenClassification(config=_SCREAMING_SNAKE_CASE							)
						__a      :	str    =  {
						    """input_ids""": input_ids,
						    """attention_mask""": input_mask,
						    """token_type_ids""": token_type_ids,
						}
						__a      :	Union[str, Any]    =  model(_SCREAMING_SNAKE_CASE							)
						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							):

						'''simple docstring'''





						__a      :	List[str]    =  TFDebertaVaForQuestionAnswering(config=_SCREAMING_SNAKE_CASE							)
						__a      :	Tuple    =  {
						    """input_ids""": input_ids,
						    """attention_mask""": input_mask,
						    """token_type_ids""": token_type_ids,
						}

						__a      :	Optional[int]    =  model(_SCREAMING_SNAKE_CASE							)
						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							):

						'''simple docstring'''





						__a      :	Tuple    =  self.prepare_config_and_inputs()
						(
						    __a		
						)      :	Optional[int]    =  config_and_inputs
						__a      :	int    =  {"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": input_mask}
						return config, inputs_dict





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

	A_           =   False
	A_           =   False




	def 				__UpperCAmelCase							(       self							):

						'''simple docstring'''





						__a      :	Tuple    =  TFDebertaVaModelTester(self							)
						__a      :	str    =  ConfigTester(self						,    config_class=_SCREAMING_SNAKE_CASE						,    hidden_size=37							)




	def 				__UpperCAmelCase							(       self							):

						'''simple docstring'''





						self.config_tester.run_common_tests()




	def 				__UpperCAmelCase							(       self							):

						'''simple docstring'''





						__a      :	Optional[int]    =  self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_model(*_SCREAMING_SNAKE_CASE							)




	def 				__UpperCAmelCase							(       self							):

						'''simple docstring'''





						__a      :	Optional[int]    =  self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_masked_lm(*_SCREAMING_SNAKE_CASE							)




	def 				__UpperCAmelCase							(       self							):

						'''simple docstring'''





						__a      :	Optional[Any]    =  self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_question_answering(*_SCREAMING_SNAKE_CASE							)




	def 				__UpperCAmelCase							(       self							):

						'''simple docstring'''





						__a      :	List[str]    =  self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_sequence_classification(*_SCREAMING_SNAKE_CASE							)




	def 				__UpperCAmelCase							(       self							):

						'''simple docstring'''





						__a      :	List[str]    =  self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_token_classification(*_SCREAMING_SNAKE_CASE							)





	@slow
	def 				__UpperCAmelCase							(       self							):

						'''simple docstring'''





						__a      :	str    =  TFDebertaVaModel.from_pretrained('kamalkraj/deberta-v2-xlarge'							)
						self.assertIsNotNone(_SCREAMING_SNAKE_CASE							)





@require_tf
class 							__UpperCamelCase    (		unittest.TestCase ):




	@unittest.skip(reason='Model not available yet'							)
	def 				__UpperCAmelCase							(       self							):

						'''simple docstring'''





						pass





	@slow
	def 				__UpperCAmelCase							(       self							):

						'''simple docstring'''





						__a      :	List[str]    =  TFDebertaVaModel.from_pretrained('kamalkraj/deberta-v2-xlarge'							)
						__a      :	Tuple    =  tf.constant([[0, 3_1414, 232, 328, 740, 1140, 1_2695, 69, 4_6078, 1588, 2]]							)
						__a      :	int    =  tf.constant([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]							)
						__a      :	Optional[int]    =  model(_SCREAMING_SNAKE_CASE						,    attention_mask=_SCREAMING_SNAKE_CASE							)[0]

						__a      :	List[str]    =  tf.constant(
						    [[[0.2356, 0.1948, 0.0369], [-0.1063, 0.3586, -0.5152], [-0.6399, -0.0259, -0.2525]]]							)
						tf.debugging.assert_near(output[:, 1:4, 1:4]						,    _SCREAMING_SNAKE_CASE						,    atol=1E-4							)







'''simple docstring'''



import shutil
import tempfile
import unittest

from transformers import ClapFeatureExtractor, ClapProcessor, RobertaTokenizer, RobertaTokenizerFast
from transformers.testing_utils import require_sentencepiece, require_torchaudio

from .test_feature_extraction_clap import floats_list





@require_torchaudio
@require_sentencepiece
class 							__UpperCamelCase    (		unittest.TestCase ):




	def 				__UpperCAmelCase							(       self							):

						'''simple docstring'''





						__a      :	Optional[Any]    =  'laion/clap-htsat-unfused'
						__a      :	Optional[Any]    =  tempfile.mkdtemp()




	def 				__UpperCAmelCase							(       self						,    **__a							):

						'''simple docstring'''





						return RobertaTokenizer.from_pretrained(self.checkpoint						,    **__a							)




	def 				__UpperCAmelCase							(       self						,    **__a							):

						'''simple docstring'''





						return ClapFeatureExtractor.from_pretrained(self.checkpoint						,    **__a							)




	def 				__UpperCAmelCase							(       self							):

						'''simple docstring'''





						shutil.rmtree(self.tmpdirname							)




	def 				__UpperCAmelCase							(       self							):

						'''simple docstring'''





						__a      :	Any    =  self.get_tokenizer()
						__a      :	List[str]    =  self.get_feature_extractor()

						__a      :	Any    =  ClapProcessor(tokenizer=__a						,    feature_extractor=__a							)

						processor.save_pretrained(self.tmpdirname							)
						__a      :	Tuple    =  ClapProcessor.from_pretrained(self.tmpdirname							)

						self.assertEqual(processor.tokenizer.get_vocab()						,    tokenizer.get_vocab()							)
						self.assertIsInstance(processor.tokenizer						,    __a							)

						self.assertEqual(processor.feature_extractor.to_json_string()						,    feature_extractor.to_json_string()							)
						self.assertIsInstance(processor.feature_extractor						,    __a							)




	def 				__UpperCAmelCase							(       self							):

						'''simple docstring'''





						__a      :	str    =  ClapProcessor(tokenizer=self.get_tokenizer()						,    feature_extractor=self.get_feature_extractor()							)
						processor.save_pretrained(self.tmpdirname							)

						__a      :	int    =  self.get_tokenizer(bos_token='(BOS)'						,    eos_token='(EOS)'							)
						__a      :	List[str]    =  self.get_feature_extractor(do_normalize=__a						,    padding_value=1.0							)

						__a      :	Tuple    =  ClapProcessor.from_pretrained(
						    self.tmpdirname						,    bos_token='(BOS)'						,    eos_token='(EOS)'						,    do_normalize=__a						,    padding_value=1.0							)

						self.assertEqual(processor.tokenizer.get_vocab()						,    tokenizer_add_kwargs.get_vocab()							)
						self.assertIsInstance(processor.tokenizer						,    __a							)

						self.assertEqual(processor.feature_extractor.to_json_string()						,    feature_extractor_add_kwargs.to_json_string()							)
						self.assertIsInstance(processor.feature_extractor						,    __a							)




	def 				__UpperCAmelCase							(       self							):

						'''simple docstring'''





						__a      :	str    =  self.get_feature_extractor()
						__a      :	int    =  self.get_tokenizer()

						__a      :	str    =  ClapProcessor(tokenizer=__a						,    feature_extractor=__a							)

						__a      :	int    =  floats_list((3, 1000)							)

						__a      :	str    =  feature_extractor(__a						,    return_tensors='np'							)
						__a      :	int    =  processor(audios=__a						,    return_tensors='np'							)

						for key in input_feat_extract.keys():
											self.assertAlmostEqual(input_feat_extract[key].sum()						,    input_processor[key].sum()						,    delta=1E-2							)




	def 				__UpperCAmelCase							(       self							):

						'''simple docstring'''





						__a      :	Union[str, Any]    =  self.get_feature_extractor()
						__a      :	Any    =  self.get_tokenizer()

						__a      :	Any    =  ClapProcessor(tokenizer=__a						,    feature_extractor=__a							)

						__a      :	Union[str, Any]    =  'This is a test string'

						__a      :	Union[str, Any]    =  processor(text=__a							)

						__a      :	Tuple    =  tokenizer(__a							)

						for key in encoded_tok.keys():
											self.assertListEqual(encoded_tok[key]						,    encoded_processor[key]							)




	def 				__UpperCAmelCase							(       self							):

						'''simple docstring'''





						__a      :	str    =  self.get_feature_extractor()
						__a      :	str    =  self.get_tokenizer()

						__a      :	List[str]    =  ClapProcessor(tokenizer=__a						,    feature_extractor=__a							)

						__a      :	Dict    =  [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

						__a      :	Optional[int]    =  processor.batch_decode(__a							)
						__a      :	Optional[Any]    =  tokenizer.batch_decode(__a							)

						self.assertListEqual(__a						,    __a							)





	def 				__UpperCAmelCase							(       self							):

						'''simple docstring'''





						__a      :	Optional[Any]    =  self.get_feature_extractor()
						__a      :	Optional[int]    =  self.get_tokenizer()

						__a      :	int    =  ClapProcessor(tokenizer=__a						,    feature_extractor=__a							)

						self.assertListEqual(
						    processor.model_input_names[2:]						,    feature_extractor.model_input_names						,    msg='`processor` and `feature_extractor` model input names do not match'						,    )



