

import itertools
import random
import unittest

import numpy as np

from transformers import WAV_2_VEC_2_PRETRAINED_MODEL_ARCHIVE_LIST, WavaVecaConfig, WavaVecaFeatureExtractor
from transformers.testing_utils import require_torch, slow

from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin


lowerCAmelCase__	   = random.Random()



def 					lowerCAmelCase__							(	SCREAMING_SNAKE_CASE_:							Tuple	,       SCREAMING_SNAKE_CASE_:							List[Any]=1.0	,       SCREAMING_SNAKE_CASE_:							Optional[int]=None	,       SCREAMING_SNAKE_CASE_:							Tuple=None      )			->      int:




							'''simple docstring'''





							if rng is None:
														A__        =			global_rng

							A__        =			[]
							for batch_idx in range(shape[0]      ):
														values.append([]      )
														for _ in range(shape[1]      ):
																					values[-1].append(rng.random() * scale      )

							return values

class 					a__						(					unittest.TestCase			):
			"""simple docstring"""







			def __init__(    self      ,      lowercase      ,      lowercase=7      ,      lowercase=400      ,      lowercase=2000      ,      lowercase=1      ,      lowercase=0.0      ,      lowercase=16000      ,      lowercase=True      ,      lowercase=True      ,      )  ->						List[Any]:
										'''simple docstring'''






										A__        =			parent
										A__        =			batch_size
										A__        =			min_seq_length
										A__        =			max_seq_length
										A__        =			(self.max_seq_length - self.min_seq_length) // (self.batch_size - 1)
										A__        =			feature_size
										A__        =			padding_value
										A__        =			sampling_rate
										A__        =			return_attention_mask
										A__        =			do_normalize


			def      UpperCamelCase		(    self       )  ->						str:
										'''simple docstring'''






										return {
										    "feature_size": self.feature_size,
										    "padding_value": self.padding_value,
										    "sampling_rate": self.sampling_rate,
										    "return_attention_mask": self.return_attention_mask,
										    "do_normalize": self.do_normalize,
										}


			def      UpperCamelCase		(    self      ,      lowercase=False      ,      lowercase=False       )  ->						List[str]:
										'''simple docstring'''






										def _flatten(lowercase       ):
																	return list(itertools.chain(*lowercase       )       )

										if equal_length:
																	A__        =			floats_list((self.batch_size, self.max_seq_length)       )
										else:
																	# make sure that inputs increase in size
																	A__        =			[
																	    _flatten(floats_list((x, self.feature_size)       )       )
																	    for x in range(self.min_seq_length      ,      self.max_seq_length      ,      self.seq_length_diff       )
																	]

										if numpify:
																	A__        =			[np.asarray(lowercase       ) for x in speech_inputs]

										return speech_inputs

class 					a__						(					snake_case							,	unittest.TestCase			):
			"""simple docstring"""







			__lowerCamelCase        =       WavaVecaFeatureExtractor


			def      UpperCamelCase		(    self       )  ->						Tuple:
										'''simple docstring'''






										A__        =			WavaVecaFeatureExtractionTester(self       )


			def      UpperCamelCase		(    self      ,      lowercase       )  ->						Optional[int]:
										'''simple docstring'''






										self.assertTrue(np.all(np.mean(lowercase      ,      axis=0       ) < 1e-3       )       )
										self.assertTrue(np.all(np.abs(np.var(lowercase      ,      axis=0       ) - 1       ) < 1e-3       )       )


			def      UpperCamelCase		(    self       )  ->						List[Any]:
										'''simple docstring'''






										A__        =			self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()       )
										# create three inputs of length 800, 1000, and 1200
										A__        =			[floats_list((1, x)       )[0] for x in range(800      ,      1400      ,      200       )]
										A__        =			[np.asarray(lowercase       ) for speech_input in speech_inputs]

										# Test not batched input
										A__        =			feat_extract(speech_inputs[0]      ,      return_tensors="np"       ).input_values
										A__        =			feat_extract(np_speech_inputs[0]      ,      return_tensors="np"       ).input_values
										self.assertTrue(np.allclose(lowercase      ,      lowercase      ,      atol=1e-3       )       )

										# Test batched
										A__        =			feat_extract(lowercase      ,      return_tensors="np"       ).input_values
										A__        =			feat_extract(lowercase      ,      return_tensors="np"       ).input_values
										for enc_seq_a, enc_seq_a in zip(lowercase      ,      lowercase       ):
																	self.assertTrue(np.allclose(lowercase      ,      lowercase      ,      atol=1e-3       )       )

										# Test 2-D numpy arrays are batched.
										A__        =			[floats_list((1, x)       )[0] for x in (800, 800, 800)]
										A__        =			np.asarray(lowercase       )
										A__        =			feat_extract(lowercase      ,      return_tensors="np"       ).input_values
										A__        =			feat_extract(lowercase      ,      return_tensors="np"       ).input_values
										for enc_seq_a, enc_seq_a in zip(lowercase      ,      lowercase       ):
																	self.assertTrue(np.allclose(lowercase      ,      lowercase      ,      atol=1e-3       )       )


			def      UpperCamelCase		(    self       )  ->						str:
										'''simple docstring'''






										A__        =			self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()       )
										A__        =			[floats_list((1, x)       )[0] for x in range(800      ,      1400      ,      200       )]

										A__        =			["longest", "max_length", "do_not_pad"]
										A__        =			[None, 1600, None]
										for max_length, padding in zip(lowercase      ,      lowercase       ):
																	A__        =			feat_extract(lowercase      ,      padding=lowercase      ,      max_length=lowercase      ,      return_tensors="np"       )
																	A__        =			processed.input_values

																	self._check_zero_mean_unit_variance(input_values[0][:800]       )
																	self.assertTrue(input_values[0][800:].sum() < 1e-6       )
																	self._check_zero_mean_unit_variance(input_values[1][:1000]       )
																	self.assertTrue(input_values[0][1000:].sum() < 1e-6       )
																	self._check_zero_mean_unit_variance(input_values[2][:1200]       )


			def      UpperCamelCase		(    self       )  ->						Union[str, Any]:
										'''simple docstring'''






										A__        =			self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()       )
										A__        =			range(800      ,      1400      ,      200       )
										A__        =			[floats_list((1, x)       )[0] for x in lengths]

										A__        =			["longest", "max_length", "do_not_pad"]
										A__        =			[None, 1600, None]

										for max_length, padding in zip(lowercase      ,      lowercase       ):
																	A__        =			feat_extract(lowercase      ,      max_length=lowercase      ,      padding=lowercase       )
																	A__        =			processed.input_values

																	self._check_zero_mean_unit_variance(input_values[0][:800]       )
																	self._check_zero_mean_unit_variance(input_values[1][:1000]       )
																	self._check_zero_mean_unit_variance(input_values[2][:1200]       )


			def      UpperCamelCase		(    self       )  ->						List[Any]:
										'''simple docstring'''






										A__        =			self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()       )
										A__        =			[floats_list((1, x)       )[0] for x in range(800      ,      1400      ,      200       )]
										A__        =			feat_extract(
										    lowercase      ,      truncation=lowercase      ,      max_length=1000      ,      padding="max_length"      ,      return_tensors="np"       )
										A__        =			processed.input_values

										self._check_zero_mean_unit_variance(input_values[0, :800]       )
										self._check_zero_mean_unit_variance(input_values[1]       )
										self._check_zero_mean_unit_variance(input_values[2]       )


			def      UpperCamelCase		(    self       )  ->						List[Any]:
										'''simple docstring'''






										A__        =			self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()       )
										A__        =			[floats_list((1, x)       )[0] for x in range(800      ,      1400      ,      200       )]
										A__        =			feat_extract(
										    lowercase      ,      truncation=lowercase      ,      max_length=1000      ,      padding="longest"      ,      return_tensors="np"       )
										A__        =			processed.input_values

										self._check_zero_mean_unit_variance(input_values[0, :800]       )
										self._check_zero_mean_unit_variance(input_values[1, :1000]       )
										self._check_zero_mean_unit_variance(input_values[2]       )

										# make sure that if max_length < longest -> then pad to max_length
										self.assertTrue(input_values.shape == (3, 1000)       )

										A__        =			[floats_list((1, x)       )[0] for x in range(800      ,      1400      ,      200       )]
										A__        =			feat_extract(
										    lowercase      ,      truncation=lowercase      ,      max_length=2000      ,      padding="longest"      ,      return_tensors="np"       )
										A__        =			processed.input_values

										self._check_zero_mean_unit_variance(input_values[0, :800]       )
										self._check_zero_mean_unit_variance(input_values[1, :1000]       )
										self._check_zero_mean_unit_variance(input_values[2]       )

										# make sure that if max_length > longest -> then pad to longest
										self.assertTrue(input_values.shape == (3, 1200)       )


			@require_torch
			def      UpperCamelCase		(    self       )  ->						str:
										'''simple docstring'''






										import torch

										A__        =			self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()       )
										A__        =			np.random.rand(100       ).astype(np.floataa       )
										A__        =			np_speech_inputs.tolist()

										for inputs in [py_speech_inputs, np_speech_inputs]:
																	A__        =			feature_extractor.pad([{"input_values": inputs}]      ,      return_tensors="np"       )
																	self.assertTrue(np_processed.input_values.dtype == np.floataa       )
																	A__        =			feature_extractor.pad([{"input_values": inputs}]      ,      return_tensors="pt"       )
																	self.assertTrue(pt_processed.input_values.dtype == torch.floataa       )


			@slow
			@require_torch
			def      UpperCamelCase		(    self       )  ->						List[Any]:
										'''simple docstring'''






										for model_id in WAV_2_VEC_2_PRETRAINED_MODEL_ARCHIVE_LIST:
																	A__        =			WavaVecaConfig.from_pretrained(lowercase       )
																	A__        =			WavaVecaFeatureExtractor.from_pretrained(lowercase       )

																	# only "layer" feature extraction norm should make use of
																	# attention_mask
																	self.assertEqual(feat_extract.return_attention_mask      ,      config.feat_extract_norm == "layer"       )




from __future__ import annotations

import copy
import tempfile
import unittest

from transformers import CONFIG_MAPPING, AutoConfig, BertConfig, GPTaConfig, TaConfig, TapasConfig, is_tf_available
from transformers.testing_utils import (
    DUMMY_UNKNOWN_IDENTIFIER,
    SMALL_MODEL_IDENTIFIER,
    RequestCounter,
    require_tensorflow_probability,
    require_tf,
    slow,
)

from ..bert.test_modeling_bert import BertModelTester


if is_tf_available():
	from transformers import (
	    TFAutoModel,
	    TFAutoModelForCausalLM,
	    TFAutoModelForMaskedLM,
	    TFAutoModelForPreTraining,
	    TFAutoModelForQuestionAnswering,
	    TFAutoModelForSeqaSeqLM,
	    TFAutoModelForSequenceClassification,
	    TFAutoModelForTableQuestionAnswering,
	    TFAutoModelForTokenClassification,
	    TFAutoModelWithLMHead,
	    TFBertForMaskedLM,
	    TFBertForPreTraining,
	    TFBertForQuestionAnswering,
	    TFBertForSequenceClassification,
	    TFBertModel,
	    TFFunnelBaseModel,
	    TFFunnelModel,
	    TFGPTaLMHeadModel,
	    TFRobertaForMaskedLM,
	    TFTaForConditionalGeneration,
	    TFTapasForQuestionAnswering,
	)
	from transformers.models.auto.modeling_tf_auto import (
	    TF_MODEL_FOR_CAUSAL_LM_MAPPING,
	    TF_MODEL_FOR_MASKED_LM_MAPPING,
	    TF_MODEL_FOR_PRETRAINING_MAPPING,
	    TF_MODEL_FOR_QUESTION_ANSWERING_MAPPING,
	    TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING,
	    TF_MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING,
	    TF_MODEL_MAPPING,
	)
	from transformers.models.bert.modeling_tf_bert import TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST
	from transformers.models.gpta.modeling_tf_gpta import TF_GPT2_PRETRAINED_MODEL_ARCHIVE_LIST
	from transformers.models.ta.modeling_tf_ta import TF_T5_PRETRAINED_MODEL_ARCHIVE_LIST
	from transformers.models.tapas.modeling_tf_tapas import TF_TAPAS_PRETRAINED_MODEL_ARCHIVE_LIST

class 					a__						(					snake_case			):
			"""simple docstring"""







			__lowerCamelCase        =       'new-model'


if is_tf_available():

	class 					a__						(					snake_case			):
				"""simple docstring"""







				__lowerCamelCase        =       NewModelConfig

@require_tf
class 					a__						(					unittest.TestCase			):
			"""simple docstring"""







			@slow
			def      UpperCamelCase		(    self       )  ->						List[Any]:
							'''simple docstring'''






							A__        =			"bert-base-cased"
							A__        =			AutoConfig.from_pretrained(lowercase       )
							self.assertIsNotNone(lowercase       )
							self.assertIsInstance(lowercase      ,      lowercase       )

							A__        =			TFAutoModel.from_pretrained(lowercase       )
							self.assertIsNotNone(lowercase       )
							self.assertIsInstance(lowercase      ,      lowercase       )


			@slow
			def      UpperCamelCase		(    self       )  ->						Optional[Any]:
							'''simple docstring'''






							A__        =			"bert-base-cased"
							A__        =			AutoConfig.from_pretrained(lowercase       )
							self.assertIsNotNone(lowercase       )
							self.assertIsInstance(lowercase      ,      lowercase       )

							A__        =			TFAutoModelForPreTraining.from_pretrained(lowercase       )
							self.assertIsNotNone(lowercase       )
							self.assertIsInstance(lowercase      ,      lowercase       )


			@slow
			def      UpperCamelCase		(    self       )  ->						Dict:
							'''simple docstring'''






							for model_name in TF_GPT2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
											A__        =			AutoConfig.from_pretrained(lowercase       )
											self.assertIsNotNone(lowercase       )
											self.assertIsInstance(lowercase      ,      lowercase       )

											A__        =			TFAutoModelForCausalLM.from_pretrained(lowercase       )
											A__    ,				A__        =			TFAutoModelForCausalLM.from_pretrained(lowercase      ,      output_loading_info=lowercase       )
											self.assertIsNotNone(lowercase       )
											self.assertIsInstance(lowercase      ,      lowercase       )


			@slow
			def      UpperCamelCase		(    self       )  ->						Optional[int]:
							'''simple docstring'''






							for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
											A__        =			AutoConfig.from_pretrained(lowercase       )
											self.assertIsNotNone(lowercase       )
											self.assertIsInstance(lowercase      ,      lowercase       )

											A__        =			TFAutoModelWithLMHead.from_pretrained(lowercase       )
											self.assertIsNotNone(lowercase       )
											self.assertIsInstance(lowercase      ,      lowercase       )


			@slow
			def      UpperCamelCase		(    self       )  ->						int:
							'''simple docstring'''






							for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
											A__        =			AutoConfig.from_pretrained(lowercase       )
											self.assertIsNotNone(lowercase       )
											self.assertIsInstance(lowercase      ,      lowercase       )

											A__        =			TFAutoModelForMaskedLM.from_pretrained(lowercase       )
											A__    ,				A__        =			TFAutoModelForMaskedLM.from_pretrained(lowercase      ,      output_loading_info=lowercase       )
											self.assertIsNotNone(lowercase       )
											self.assertIsInstance(lowercase      ,      lowercase       )


			@slow
			def      UpperCamelCase		(    self       )  ->						str:
							'''simple docstring'''






							for model_name in TF_T5_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
											A__        =			AutoConfig.from_pretrained(lowercase       )
											self.assertIsNotNone(lowercase       )
											self.assertIsInstance(lowercase      ,      lowercase       )

											A__        =			TFAutoModelForSeqaSeqLM.from_pretrained(lowercase       )
											A__    ,				A__        =			TFAutoModelForSeqaSeqLM.from_pretrained(lowercase      ,      output_loading_info=lowercase       )
											self.assertIsNotNone(lowercase       )
											self.assertIsInstance(lowercase      ,      lowercase       )


			@slow
			def      UpperCamelCase		(    self       )  ->						Dict:
							'''simple docstring'''






							for model_name in ["bert-base-uncased"]:
											A__        =			AutoConfig.from_pretrained(lowercase       )
											self.assertIsNotNone(lowercase       )
											self.assertIsInstance(lowercase      ,      lowercase       )

											A__        =			TFAutoModelForSequenceClassification.from_pretrained(lowercase       )
											self.assertIsNotNone(lowercase       )
											self.assertIsInstance(lowercase      ,      lowercase       )


			@slow
			def      UpperCamelCase		(    self       )  ->						int:
							'''simple docstring'''






							for model_name in ["bert-base-uncased"]:
											A__        =			AutoConfig.from_pretrained(lowercase       )
											self.assertIsNotNone(lowercase       )
											self.assertIsInstance(lowercase      ,      lowercase       )

											A__        =			TFAutoModelForQuestionAnswering.from_pretrained(lowercase       )
											self.assertIsNotNone(lowercase       )
											self.assertIsInstance(lowercase      ,      lowercase       )


			@slow
			@require_tensorflow_probability
			def      UpperCamelCase		(    self       )  ->						List[str]:
							'''simple docstring'''






							for model_name in TF_TAPAS_PRETRAINED_MODEL_ARCHIVE_LIST[5:6]:
											A__        =			AutoConfig.from_pretrained(lowercase       )
											self.assertIsNotNone(lowercase       )
											self.assertIsInstance(lowercase      ,      lowercase       )

											A__        =			TFAutoModelForTableQuestionAnswering.from_pretrained(lowercase       )
											A__    ,				A__        =			TFAutoModelForTableQuestionAnswering.from_pretrained(
											    lowercase      ,      output_loading_info=lowercase       )
											self.assertIsNotNone(lowercase       )
											self.assertIsInstance(lowercase      ,      lowercase       )


			def      UpperCamelCase		(    self       )  ->						Dict:
							'''simple docstring'''






							A__        =			TFAutoModelWithLMHead.from_pretrained(lowercase       )
							self.assertIsInstance(lowercase      ,      lowercase       )
							self.assertEqual(model.num_parameters()      ,      14410       )
							self.assertEqual(model.num_parameters(only_trainable=lowercase       )      ,      14410       )


			def      UpperCamelCase		(    self       )  ->						Any:
							'''simple docstring'''






							A__        =			TFAutoModelWithLMHead.from_pretrained(lowercase       )
							self.assertIsInstance(lowercase      ,      lowercase       )
							self.assertEqual(model.num_parameters()      ,      14410       )
							self.assertEqual(model.num_parameters(only_trainable=lowercase       )      ,      14410       )


			def      UpperCamelCase		(    self       )  ->						int:
							'''simple docstring'''






							A__        =			TFAutoModel.from_pretrained("sgugger/funnel-random-tiny"       )
							self.assertIsInstance(lowercase      ,      lowercase       )

							A__        =			copy.deepcopy(model.config       )
							A__        =			["FunnelBaseModel"]
							A__        =			TFAutoModel.from_config(lowercase       )
							self.assertIsInstance(lowercase      ,      lowercase       )

							with tempfile.TemporaryDirectory() as tmp_dir:
											model.save_pretrained(lowercase       )
											A__        =			TFAutoModel.from_pretrained(lowercase       )
											self.assertIsInstance(lowercase      ,      lowercase       )


			def      UpperCamelCase		(    self       )  ->						Optional[Any]:
							'''simple docstring'''






							try:
											AutoConfig.register("new-model"      ,      lowercase       )

											A__        =			[
											    TFAutoModel,
											    TFAutoModelForCausalLM,
											    TFAutoModelForMaskedLM,
											    TFAutoModelForPreTraining,
											    TFAutoModelForQuestionAnswering,
											    TFAutoModelForSequenceClassification,
											    TFAutoModelForTokenClassification,
											]

											for auto_class in auto_classes:
															with self.subTest(auto_class.__name__       ):
																			# Wrong config class will raise an error
																			with self.assertRaises(lowercase       ):
																							auto_class.register(lowercase      ,      lowercase       )
																			auto_class.register(lowercase      ,      lowercase       )
																			# Trying to register something existing in the Transformers library will raise an error
																			with self.assertRaises(lowercase       ):
																							auto_class.register(lowercase      ,      lowercase       )

																			# Now that the config is registered, it can be used as any other config with the auto-API
																			A__        =			BertModelTester(self       ).get_config()
																			A__        =			NewModelConfig(**tiny_config.to_dict()       )
																			A__        =			auto_class.from_config(lowercase       )
																			self.assertIsInstance(lowercase      ,      lowercase       )

																			with tempfile.TemporaryDirectory() as tmp_dir:
																							model.save_pretrained(lowercase       )
																							A__        =			auto_class.from_pretrained(lowercase       )
																							self.assertIsInstance(lowercase      ,      lowercase       )

							finally:
											if "new-model" in CONFIG_MAPPING._extra_content:
															del CONFIG_MAPPING._extra_content["new-model"]
											for mapping in (
											    TF_MODEL_MAPPING,
											    TF_MODEL_FOR_PRETRAINING_MAPPING,
											    TF_MODEL_FOR_QUESTION_ANSWERING_MAPPING,
											    TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING,
											    TF_MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING,
											    TF_MODEL_FOR_CAUSAL_LM_MAPPING,
											    TF_MODEL_FOR_MASKED_LM_MAPPING,
											):
															if NewModelConfig in mapping._extra_content:
																			del mapping._extra_content[NewModelConfig]


			def      UpperCamelCase		(    self       )  ->						str:
							'''simple docstring'''






							with self.assertRaisesRegex(
							    lowercase      ,      "bert-base is not a local folder and is not a valid model identifier"       ):
											A__        =			TFAutoModel.from_pretrained("bert-base"       )


			def      UpperCamelCase		(    self       )  ->						Optional[Any]:
							'''simple docstring'''






							with self.assertRaisesRegex(
							    lowercase      ,      R"aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)"       ):
											A__        =			TFAutoModel.from_pretrained(lowercase      ,      revision="aaaaaa"       )


			def      UpperCamelCase		(    self       )  ->						Union[str, Any]:
							'''simple docstring'''






							with self.assertRaisesRegex(
							    lowercase      ,      "hf-internal-testing/config-no-model does not appear to have a file named pytorch_model.bin"      ,      ):
											A__        =			TFAutoModel.from_pretrained("hf-internal-testing/config-no-model"       )


			def      UpperCamelCase		(    self       )  ->						Union[str, Any]:
							'''simple docstring'''






							with self.assertRaisesRegex(lowercase      ,      "Use `from_pt=True` to load this model"       ):
											A__        =			TFAutoModel.from_pretrained("hf-internal-testing/tiny-bert-pt-only"       )


			def      UpperCamelCase		(    self       )  ->						Any:
							'''simple docstring'''






							A__        =			TFAutoModel.from_pretrained("hf-internal-testing/tiny-random-bert"       )
							with RequestCounter() as counter:
											A__        =			TFAutoModel.from_pretrained("hf-internal-testing/tiny-random-bert"       )
											self.assertEqual(counter.get_request_count      ,      0       )
											self.assertEqual(counter.head_request_count      ,      1       )
											self.assertEqual(counter.other_request_count      ,      0       )

							# With a sharded checkpoint
							A__        =			TFAutoModel.from_pretrained("ArthurZ/tiny-random-bert-sharded"       )
							with RequestCounter() as counter:
											A__        =			TFAutoModel.from_pretrained("ArthurZ/tiny-random-bert-sharded"       )
											self.assertEqual(counter.get_request_count      ,      0       )
											self.assertEqual(counter.head_request_count      ,      1       )
											self.assertEqual(counter.other_request_count      ,      0       )

