import unittest

from transformers import AlbertConfig, is_torch_available
from transformers.models.auto import get_values
from transformers.testing_utils import require_torch, slow, torch_device

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


if is_torch_available():
				import torch

				from transformers import (
				    MODEL_FOR_PRETRAINING_MAPPING,
				    AlbertForMaskedLM,
				    AlbertForMultipleChoice,
				    AlbertForPreTraining,
				    AlbertForQuestionAnswering,
				    AlbertForSequenceClassification,
				    AlbertForTokenClassification,
				    AlbertModel,
				)
				from transformers.models.albert.modeling_albert import ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST



class        lowercase_   :



							def __init__(						self				,    lowercase_				,    lowercase_=13				,    lowercase_=7				,    lowercase_=True				,    lowercase_=True				,    lowercase_=True				,    lowercase_=True				,    lowercase_=99				,    lowercase_=16				,    lowercase_=36				,    lowercase_=6				,    lowercase_=6				,    lowercase_=6				,    lowercase_=37				,    lowercase_="gelu"				,    lowercase_=0.1				,    lowercase_=0.1				,    lowercase_=512				,    lowercase_=16				,    lowercase_=2				,    lowercase_=0.02				,    lowercase_=3				,    lowercase_=4				,    lowercase_=None				,    ):
													_snake_case						:					Optional[int]    =   parent
													_snake_case						:					List[str]    =   batch_size
													_snake_case						:					Optional[Any]    =   seq_length
													_snake_case						:					Any    =   is_training
													_snake_case						:					Tuple    =   use_input_mask
													_snake_case						:					int    =   use_token_type_ids
													_snake_case						:					Dict    =   use_labels
													_snake_case						:					List[Any]    =   vocab_size
													_snake_case						:					Any    =   embedding_size
													_snake_case						:					str    =   hidden_size
													_snake_case						:					List[str]    =   num_hidden_layers
													_snake_case						:					List[Any]    =   num_hidden_groups
													_snake_case						:					Optional[int]    =   num_attention_heads
													_snake_case						:					Optional[Any]    =   intermediate_size
													_snake_case						:					int    =   hidden_act
													_snake_case						:					Optional[Any]    =   hidden_dropout_prob
													_snake_case						:					Optional[Any]    =   attention_probs_dropout_prob
													_snake_case						:					List[Any]    =   max_position_embeddings
													_snake_case						:					Dict    =   type_vocab_size
													_snake_case						:					Optional[Any]    =   type_sequence_label_size
													_snake_case						:					Optional[Any]    =   initializer_range
													_snake_case						:					Any    =   num_labels
													_snake_case						:					int    =   num_choices
													_snake_case						:					Optional[int]    =   scope



							def 	UpperCamelCase	(						self    ):
													_snake_case						:					Any    =   ids_tensor([self.batch_size, self.seq_length]				,    self.vocab_size    )

													_snake_case						:					Dict    =   None
													if self.use_input_mask:
																			_snake_case						:					List[str]    =   random_attention_mask([self.batch_size, self.seq_length]    )

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

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

													_snake_case						:					Optional[Any]    =   self.get_config()

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



							def 	UpperCamelCase	(						self    ):
													return AlbertConfig(
													    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				,    num_hidden_groups=self.num_hidden_groups				,    )



							def 	UpperCamelCase	(						self				,    lowercase_				,    lowercase_				,    lowercase_				,    lowercase_				,    lowercase_				,    lowercase_				,    lowercase_    ):
													_snake_case						:					Any    =   AlbertModel(config=lowercase_    )
													model.to(lowercase_    )
													model.eval()
													_snake_case						:					Optional[int]    =   model(lowercase_				,    attention_mask=lowercase_				,    token_type_ids=lowercase_    )
													_snake_case						:					Optional[int]    =   model(lowercase_				,    token_type_ids=lowercase_    )
													_snake_case						:					Tuple    =   model(lowercase_    )
													self.parent.assertEqual(result.last_hidden_state.shape				,    (self.batch_size, self.seq_length, self.hidden_size)    )
													self.parent.assertEqual(result.pooler_output.shape				,    (self.batch_size, self.hidden_size)    )



							def 	UpperCamelCase	(						self				,    lowercase_				,    lowercase_				,    lowercase_				,    lowercase_				,    lowercase_				,    lowercase_				,    lowercase_    ):
													_snake_case						:					str    =   AlbertForPreTraining(config=lowercase_    )
													model.to(lowercase_    )
													model.eval()
													_snake_case						:					Dict    =   model(
													    lowercase_				,    attention_mask=lowercase_				,    token_type_ids=lowercase_				,    labels=lowercase_				,    sentence_order_label=lowercase_				,    )
													self.parent.assertEqual(result.prediction_logits.shape				,    (self.batch_size, self.seq_length, self.vocab_size)    )
													self.parent.assertEqual(result.sop_logits.shape				,    (self.batch_size, config.num_labels)    )



							def 	UpperCamelCase	(						self				,    lowercase_				,    lowercase_				,    lowercase_				,    lowercase_				,    lowercase_				,    lowercase_				,    lowercase_    ):
													_snake_case						:					List[Any]    =   AlbertForMaskedLM(config=lowercase_    )
													model.to(lowercase_    )
													model.eval()
													_snake_case						:					int    =   model(lowercase_				,    attention_mask=lowercase_				,    token_type_ids=lowercase_				,    labels=lowercase_    )
													self.parent.assertEqual(result.logits.shape				,    (self.batch_size, self.seq_length, self.vocab_size)    )



							def 	UpperCamelCase	(						self				,    lowercase_				,    lowercase_				,    lowercase_				,    lowercase_				,    lowercase_				,    lowercase_				,    lowercase_    ):
													_snake_case						:					Union[str, Any]    =   AlbertForQuestionAnswering(config=lowercase_    )
													model.to(lowercase_    )
													model.eval()
													_snake_case						:					Optional[int]    =   model(
													    lowercase_				,    attention_mask=lowercase_				,    token_type_ids=lowercase_				,    start_positions=lowercase_				,    end_positions=lowercase_				,    )
													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				,    lowercase_				,    lowercase_				,    lowercase_				,    lowercase_				,    lowercase_				,    lowercase_				,    lowercase_    ):
													_snake_case						:					int    =   self.num_labels
													_snake_case						:					str    =   AlbertForSequenceClassification(lowercase_    )
													model.to(lowercase_    )
													model.eval()
													_snake_case						:					List[Any]    =   model(lowercase_				,    attention_mask=lowercase_				,    token_type_ids=lowercase_				,    labels=lowercase_    )
													self.parent.assertEqual(result.logits.shape				,    (self.batch_size, self.num_labels)    )



							def 	UpperCamelCase	(						self				,    lowercase_				,    lowercase_				,    lowercase_				,    lowercase_				,    lowercase_				,    lowercase_				,    lowercase_    ):
													_snake_case						:					List[str]    =   self.num_labels
													_snake_case						:					str    =   AlbertForTokenClassification(config=lowercase_    )
													model.to(lowercase_    )
													model.eval()
													_snake_case						:					int    =   model(lowercase_				,    attention_mask=lowercase_				,    token_type_ids=lowercase_				,    labels=lowercase_    )
													self.parent.assertEqual(result.logits.shape				,    (self.batch_size, self.seq_length, self.num_labels)    )



							def 	UpperCamelCase	(						self				,    lowercase_				,    lowercase_				,    lowercase_				,    lowercase_				,    lowercase_				,    lowercase_				,    lowercase_    ):
													_snake_case						:					List[Any]    =   self.num_choices
													_snake_case						:					Union[str, Any]    =   AlbertForMultipleChoice(config=lowercase_    )
													model.to(lowercase_    )
													model.eval()
													_snake_case						:					int    =   input_ids.unsqueeze(1    ).expand(-1				,    self.num_choices				,    -1    ).contiguous()
													_snake_case						:					str    =   token_type_ids.unsqueeze(1    ).expand(-1				,    self.num_choices				,    -1    ).contiguous()
													_snake_case						:					int    =   input_mask.unsqueeze(1    ).expand(-1				,    self.num_choices				,    -1    ).contiguous()
													_snake_case						:					Optional[Any]    =   model(
													    lowercase_				,    attention_mask=lowercase_				,    token_type_ids=lowercase_				,    labels=lowercase_				,    )
													self.parent.assertEqual(result.logits.shape				,    (self.batch_size, self.num_choices)    )



							def 	UpperCamelCase	(						self    ):
													_snake_case						:					Optional[int]    =   self.prepare_config_and_inputs()
													(
													    (
													    _snake_case
													)    ,(
													    _snake_case
													)    ,(
													    _snake_case
													)    ,(
													    _snake_case
													)    ,(
													    _snake_case
													)    ,(
													    _snake_case
													)    ,(
													    _snake_case
													)    ,
													)						:					Any    =   config_and_inputs
													_snake_case						:					List[str]    =   {"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
													return config, inputs_dict




@require_torch
class        lowercase_   (					__snake_case						,			__snake_case						,			unittest.TestCase		):
							_lowerCamelCase						=		(
							    (
							        AlbertModel,
							        AlbertForPreTraining,
							        AlbertForMaskedLM,
							        AlbertForMultipleChoice,
							        AlbertForSequenceClassification,
							        AlbertForTokenClassification,
							        AlbertForQuestionAnswering,
							    )
							    if is_torch_available()
							    else ()
							)
							_lowerCamelCase						=		(
							    {
							        'feature-extraction': AlbertModel,
							        'fill-mask': AlbertForMaskedLM,
							        'question-answering': AlbertForQuestionAnswering,
							        'text-classification': AlbertForSequenceClassification,
							        'token-classification': AlbertForTokenClassification,
							        'zero-shot': AlbertForSequenceClassification,
							    }
							    if is_torch_available()
							    else {}
							)
							_lowerCamelCase						=		True



							def 	UpperCamelCase	(						self				,    lowercase_				,    lowercase_				,    lowercase_=False    ):
													_snake_case						:					int    =   super()._prepare_for_class(lowercase_				,    lowercase_				,    return_labels=lowercase_    )

													if return_labels:
																			if model_class in get_values(lowercase_    ):
																									_snake_case						:					Optional[int]    =   torch.zeros(
																									    (self.model_tester.batch_size, self.model_tester.seq_length)				,    dtype=torch.long				,    device=lowercase_    )
																									_snake_case						:					Tuple    =   torch.zeros(
																									    self.model_tester.batch_size				,    dtype=torch.long				,    device=lowercase_    )
													return inputs_dict



							def 	UpperCamelCase	(						self    ):
													_snake_case						:					List[Any]    =   AlbertModelTester(self    )
													_snake_case						:					Union[str, Any]    =   ConfigTester(self				,    config_class=lowercase_				,    hidden_size=37    )



							def 	UpperCamelCase	(						self    ):
													self.config_tester.run_common_tests()



							def 	UpperCamelCase	(						self    ):
													_snake_case						:					Any    =   self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_model(*lowercase_    )



							def 	UpperCamelCase	(						self    ):
													_snake_case						:					Union[str, Any]    =   self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_for_pretraining(*lowercase_    )



							def 	UpperCamelCase	(						self    ):
													_snake_case						:					Union[str, Any]    =   self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_for_masked_lm(*lowercase_    )



							def 	UpperCamelCase	(						self    ):
													_snake_case						:					Optional[Any]    =   self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_for_multiple_choice(*lowercase_    )



							def 	UpperCamelCase	(						self    ):
													_snake_case						:					Union[str, Any]    =   self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_for_question_answering(*lowercase_    )



							def 	UpperCamelCase	(						self    ):
													_snake_case						:					Tuple    =   self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_for_sequence_classification(*lowercase_    )



							def 	UpperCamelCase	(						self    ):
													_snake_case						:					Tuple    =   self.model_tester.prepare_config_and_inputs()
													for type in ["absolute", "relative_key", "relative_key_query"]:
																			_snake_case						:					Optional[int]    =   type
																			self.model_tester.create_and_check_model(*lowercase_    )



							@slow
							def 	UpperCamelCase	(						self    ):
													for model_name in ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																			_snake_case						:					List[Any]    =   AlbertModel.from_pretrained(lowercase_    )
																			self.assertIsNotNone(lowercase_    )




@require_torch
class        lowercase_   (					unittest.TestCase		):



							@slow
							def 	UpperCamelCase	(						self    ):
													_snake_case						:					Any    =   AlbertModel.from_pretrained("albert-base-v2"    )
													_snake_case						:					Tuple    =   torch.tensor([[0, 345, 232, 328, 740, 140, 1_695, 69, 6_078, 1_588, 2]]    )
													_snake_case						:					Optional[Any]    =   torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]    )
													with torch.no_grad():
																			_snake_case						:					List[str]    =   model(lowercase_				,    attention_mask=lowercase_    )[0]
													_snake_case						:					Union[str, Any]    =   torch.Size((1, 11, 768)    )
													self.assertEqual(output.shape				,    lowercase_    )
													_snake_case						:					Optional[int]    =   torch.tensor(
													    [[[-0.6_513, 1.5_035, -0.2_766], [-0.6_515, 1.5_046, -0.2_780], [-0.6_512, 1.5_049, -0.2_784]]]    )

													self.assertTrue(torch.allclose(output[:, 1:4, 1:4]				,    lowercase_				,    atol=1e-4    )    )
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        lowercase_   (					unittest.TestCase		):



							def 	UpperCamelCase	(						self    ):
													_snake_case						:					str    =   "laion/clap-htsat-unfused"
													_snake_case						:					Dict    =   tempfile.mkdtemp()



							def 	UpperCamelCase	(						self				,    **lowercase_    ):
													return RobertaTokenizer.from_pretrained(self.checkpoint				,    **lowercase_    )



							def 	UpperCamelCase	(						self				,    **lowercase_    ):
													return ClapFeatureExtractor.from_pretrained(self.checkpoint				,    **lowercase_    )



							def 	UpperCamelCase	(						self    ):
													shutil.rmtree(self.tmpdirname    )



							def 	UpperCamelCase	(						self    ):
													_snake_case						:					Optional[int]    =   self.get_tokenizer()
													_snake_case						:					List[Any]    =   self.get_feature_extractor()

													_snake_case						:					Optional[Any]    =   ClapProcessor(tokenizer=lowercase_				,    feature_extractor=lowercase_    )

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

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

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



							def 	UpperCamelCase	(						self    ):
													_snake_case						:					Any    =   ClapProcessor(tokenizer=self.get_tokenizer()				,    feature_extractor=self.get_feature_extractor()    )
													processor.save_pretrained(self.tmpdirname    )

													_snake_case						:					Any    =   self.get_tokenizer(bos_token="(BOS)"				,    eos_token="(EOS)"    )
													_snake_case						:					List[Any]    =   self.get_feature_extractor(do_normalize=lowercase_				,    padding_value=1.0    )

													_snake_case						:					List[Any]    =   ClapProcessor.from_pretrained(
													    self.tmpdirname				,    bos_token="(BOS)"				,    eos_token="(EOS)"				,    do_normalize=lowercase_				,    padding_value=1.0    )

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

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



							def 	UpperCamelCase	(						self    ):
													_snake_case						:					Tuple    =   self.get_feature_extractor()
													_snake_case						:					Union[str, Any]    =   self.get_tokenizer()

													_snake_case						:					Optional[Any]    =   ClapProcessor(tokenizer=lowercase_				,    feature_extractor=lowercase_    )

													_snake_case						:					List[str]    =   floats_list((3, 1_000)    )

													_snake_case						:					Union[str, Any]    =   feature_extractor(lowercase_				,    return_tensors="np"    )
													_snake_case						:					Any    =   processor(audios=lowercase_				,    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    ):
													_snake_case						:					str    =   self.get_feature_extractor()
													_snake_case						:					Optional[Any]    =   self.get_tokenizer()

													_snake_case						:					Dict    =   ClapProcessor(tokenizer=lowercase_				,    feature_extractor=lowercase_    )

													_snake_case						:					Any    =   "This is a test string"

													_snake_case						:					Optional[Any]    =   processor(text=lowercase_    )

													_snake_case						:					Optional[Any]    =   tokenizer(lowercase_    )

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



							def 	UpperCamelCase	(						self    ):
													_snake_case						:					Dict    =   self.get_feature_extractor()
													_snake_case						:					Dict    =   self.get_tokenizer()

													_snake_case						:					List[Any]    =   ClapProcessor(tokenizer=lowercase_				,    feature_extractor=lowercase_    )

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

													_snake_case						:					List[Any]    =   processor.batch_decode(lowercase_    )
													_snake_case						:					Optional[int]    =   tokenizer.batch_decode(lowercase_    )

													self.assertListEqual(lowercase_				,    lowercase_    )



							def 	UpperCamelCase	(						self    ):
													_snake_case						:					List[str]    =   self.get_feature_extractor()
													_snake_case						:					str    =   self.get_tokenizer()

													_snake_case						:					Optional[int]    =   ClapProcessor(tokenizer=lowercase_				,    feature_extractor=lowercase_    )

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