






import itertools
import os
import random
import tempfile
import unittest

import numpy as np
from datasets import load_dataset

from transformers import is_speech_available
from transformers.testing_utils import check_json_file_has_correct_format, require_torch, require_torchaudio
from transformers.utils.import_utils import is_torch_available

from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin


if is_speech_available():
	from transformers import WhisperFeatureExtractor

if is_torch_available():
	import torch

lowercase    				=   random.Random()





def 		__UpperCAmelCase     (	a_				,	a_=1.0				,	a_=None				,	a_=None):
							if rng is None:
														snake_case_           =				global_rng

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

							return values




@require_torch
@require_torchaudio
class 					UpperCamelCase_ (     unittest.TestCase							):



	'''simple docstring'''




	def __init__(			self			,     a			,     a=7			,     a=4_00			,     a=20_00			,     a=10			,     a=1_60			,     a=8			,     a=0.0			,     a=40_00			,     a=False			,     a=True			,     )			->       Union[str, Any]:
								snake_case_           =				parent
								snake_case_           =				batch_size
								snake_case_           =				min_seq_length
								snake_case_           =				max_seq_length
								snake_case_           =				(self.max_seq_length - self.min_seq_length) // (self.batch_size - 1)
								snake_case_           =				padding_value
								snake_case_           =				sampling_rate
								snake_case_           =				return_attention_mask
								snake_case_           =				do_normalize
								snake_case_           =				feature_size
								snake_case_           =				chunk_length
								snake_case_           =				hop_length




	def 				_UpperCamelCase     (			self   )			->       Dict:
								return {
								    "feature_size": self.feature_size,
								    "hop_length": self.hop_length,
								    "chunk_length": self.chunk_length,
								    "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			,     a=False			,     a=False   )			->       Dict:
								def _flatten(a   ):
															return list(itertools.chain(*a   )   )

								if equal_length:
															snake_case_           =				[floats_list((self.max_seq_length, self.feature_size)   ) for _ in range(self.batch_size   )]
								else:
															# make sure that inputs increase in size
															snake_case_           =				[
															    floats_list((x, self.feature_size)   )
															    for x in range(self.min_seq_length			,     self.max_seq_length			,     self.seq_length_diff   )
															]
								if numpify:
															snake_case_           =				[np.asarray(a   ) for x in speech_inputs]
								return speech_inputs






@require_torch
@require_torchaudio
class 					UpperCamelCase_ (     snake_case_			,   unittest.TestCase							):



	'''simple docstring'''




	lowerCAmelCase						=					WhisperFeatureExtractor if is_speech_available() else None




	def 				_UpperCamelCase     (			self   )			->       str:
								snake_case_           =				WhisperFeatureExtractionTester(self   )




	def 				_UpperCamelCase     (			self   )			->       Any:
								snake_case_           =				self.feature_extraction_class(**self.feat_extract_dict   )

								with tempfile.TemporaryDirectory() as tmpdirname:
															snake_case_           =				feat_extract_first.save_pretrained(a   )[0]
															check_json_file_has_correct_format(a   )
															snake_case_           =				self.feature_extraction_class.from_pretrained(a   )

								snake_case_           =				feat_extract_first.to_dict()
								snake_case_           =				feat_extract_second.to_dict()
								snake_case_           =				feat_extract_first.mel_filters
								snake_case_           =				feat_extract_second.mel_filters
								self.assertTrue(np.allclose(a			,     a   )   )
								self.assertEqual(a			,     a   )




	def 				_UpperCamelCase     (			self   )			->       Tuple:
								snake_case_           =				self.feature_extraction_class(**self.feat_extract_dict   )

								with tempfile.TemporaryDirectory() as tmpdirname:
															snake_case_           =				os.path.join(a			,     'feat_extract.json'   )
															feat_extract_first.to_json_file(a   )
															snake_case_           =				self.feature_extraction_class.from_json_file(a   )

								snake_case_           =				feat_extract_first.to_dict()
								snake_case_           =				feat_extract_second.to_dict()
								snake_case_           =				feat_extract_first.mel_filters
								snake_case_           =				feat_extract_second.mel_filters
								self.assertTrue(np.allclose(a			,     a   )   )
								self.assertEqual(a			,     a   )




	def 				_UpperCamelCase     (			self   )			->       Any:
								# Tests that all call wrap to encode_plus and batch_encode_plus
								snake_case_           =				self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()   )
								# create three inputs of length 800, 1000, and 1200
								snake_case_           =				[floats_list((1, x)   )[0] for x in range(8_00			,     14_00			,     2_00   )]
								snake_case_           =				[np.asarray(a   ) for speech_input in speech_inputs]

								# Test feature size
								snake_case_           =				feature_extractor(a			,     padding='max_length'			,     return_tensors='np'   ).input_features
								self.assertTrue(input_features.ndim == 3   )
								self.assertTrue(input_features.shape[-1] == feature_extractor.nb_max_frames   )
								self.assertTrue(input_features.shape[-2] == feature_extractor.feature_size   )

								# Test not batched input
								snake_case_           =				feature_extractor(speech_inputs[0]			,     return_tensors='np'   ).input_features
								snake_case_           =				feature_extractor(np_speech_inputs[0]			,     return_tensors='np'   ).input_features
								self.assertTrue(np.allclose(a			,     a			,     atol=1E-3   )   )

								# Test batched
								snake_case_           =				feature_extractor(a			,     return_tensors='np'   ).input_features
								snake_case_           =				feature_extractor(a			,     return_tensors='np'   ).input_features
								for enc_seq_a, enc_seq_a in zip(a			,     a   ):
															self.assertTrue(np.allclose(a			,     a			,     atol=1E-3   )   )

								# Test 2-D numpy arrays are batched.
								snake_case_           =				[floats_list((1, x)   )[0] for x in (8_00, 8_00, 8_00)]
								snake_case_           =				np.asarray(a   )
								snake_case_           =				feature_extractor(a			,     return_tensors='np'   ).input_features
								snake_case_           =				feature_extractor(a			,     return_tensors='np'   ).input_features
								for enc_seq_a, enc_seq_a in zip(a			,     a   ):
															self.assertTrue(np.allclose(a			,     a			,     atol=1E-3   )   )

								# Test truncation required
								snake_case_           =				[floats_list((1, x)   )[0] for x in range(2_00			,     (feature_extractor.n_samples + 5_00)			,     2_00   )]
								snake_case_           =				[np.asarray(a   ) for speech_input in speech_inputs]

								snake_case_           =				[x[: feature_extractor.n_samples] for x in speech_inputs]
								snake_case_           =				[np.asarray(a   ) for speech_input in speech_inputs_truncated]

								snake_case_           =				feature_extractor(a			,     return_tensors='np'   ).input_features
								snake_case_           =				feature_extractor(a			,     return_tensors='np'   ).input_features
								for enc_seq_a, enc_seq_a in zip(a			,     a   ):
															self.assertTrue(np.allclose(a			,     a			,     atol=1E-3   )   )




	def 				_UpperCamelCase     (			self   )			->       Tuple:
								import torch

								snake_case_           =				self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()   )
								snake_case_           =				np.random.rand(1_00			,     32   ).astype(np.floataa   )
								snake_case_           =				np_speech_inputs.tolist()

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




	def 				_UpperCamelCase     (			self			,     a   )			->       str:
								snake_case_           =				load_dataset('hf-internal-testing/librispeech_asr_dummy'			,     'clean'			,     split='validation'   )
								# automatic decoding with librispeech
								snake_case_           =				ds.sort('id'   ).select(range(a   )   )[:num_samples]['audio']

								return [x["array"] for x in speech_samples]




	def 				_UpperCamelCase     (			self   )			->       Dict:
								# fmt: off
								snake_case_           =				torch.tensor(
								    [
								        0.1_193, -0.0_946, -0.1_098, -0.0_196, 0.0_225, -0.0_690, -0.1_736, 0.0_951,
								        0.0_971, -0.0_817, -0.0_702, 0.0_162, 0.0_260, 0.0_017, -0.0_192, -0.1_678,
								        0.0_709, -0.1_867, -0.0_655, -0.0_274, -0.0_234, -0.1_884, -0.0_516, -0.0_554,
								        -0.0_274, -0.1_425, -0.1_423, 0.0_837, 0.0_377, -0.0_854
								    ]   )
								# fmt: on

								snake_case_           =				self._load_datasamples(1   )
								snake_case_           =				WhisperFeatureExtractor()
								snake_case_           =				feature_extractor(a			,     return_tensors='pt'   ).input_features
								self.assertEqual(input_features.shape			,     (1, 80, 30_00)   )
								self.assertTrue(torch.allclose(input_features[0, 0, :30]			,     a			,     atol=1E-4   )   )






	def 				_UpperCamelCase     (			self   )			->       int:
								snake_case_           =				self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()   )
								snake_case_           =				self._load_datasamples(1   )[0]
								snake_case_           =				((audio - audio.min()) / (audio.max() - audio.min())) * 6_55_35  # Rescale to [0, 65535] to show issue
								snake_case_           =				feat_extract.zero_mean_unit_var_norm([audio]			,     attention_mask=a   )[0]

								self.assertTrue(np.all(np.mean(a   ) < 1E-3   )   )
								self.assertTrue(np.all(np.abs(np.var(a   ) - 1   ) < 1E-3   )   )













from __future__ import annotations

import inspect
import unittest
from math import floor

import numpy as np

from transformers import CvtConfig
from transformers.testing_utils import require_tf, require_vision, slow
from transformers.utils import cached_property, is_tf_available, is_vision_available

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


if is_tf_available():
	import tensorflow as tf

	from transformers import TFCvtForImageClassification, TFCvtModel
	from transformers.models.cvt.modeling_tf_cvt import TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
	from PIL import Image

	from transformers import AutoImageProcessor




class 					UpperCamelCase_ (     snake_case_							):



	'''simple docstring'''




	def 				_UpperCamelCase     (			self   )			->       Any:
								snake_case_           =				self.config_class(**self.inputs_dict   )
								self.parent.assertTrue(hasattr(a			,     'embed_dim'   )   )
								self.parent.assertTrue(hasattr(a			,     'num_heads'   )   )






class 					UpperCamelCase_ :



	'''simple docstring'''




	def __init__(			self			,     a			,     a=13			,     a=64			,     a=3			,     a=[16, 48, 96]			,     a=[1, 3, 6]			,     a=[1, 2, 10]			,     a=[7, 3, 3]			,     a=[4, 2, 2]			,     a=[2, 1, 1]			,     a=[2, 2, 2]			,     a=[False, False, True]			,     a=[0.0, 0.0, 0.0]			,     a=0.02			,     a=1E-12			,     a=True			,     a=True			,     a=2			,     )			->       List[Any]:
								snake_case_           =				parent
								snake_case_           =				batch_size
								snake_case_           =				image_size
								snake_case_           =				patch_sizes
								snake_case_           =				patch_stride
								snake_case_           =				patch_padding
								snake_case_           =				is_training
								snake_case_           =				use_labels
								snake_case_           =				num_labels
								snake_case_           =				num_channels
								snake_case_           =				embed_dim
								snake_case_           =				num_heads
								snake_case_           =				stride_kv
								snake_case_           =				depth
								snake_case_           =				cls_token
								snake_case_           =				attention_drop_rate
								snake_case_           =				initializer_range
								snake_case_           =				layer_norm_eps




	def 				_UpperCamelCase     (			self   )			->       Dict:
								snake_case_           =				floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]   )

								snake_case_           =				None
								if self.use_labels:
															# create a random int32 tensor of given shape
															snake_case_           =				ids_tensor([self.batch_size]			,     self.num_labels   )

								snake_case_           =				self.get_config()
								return config, pixel_values, labels




	def 				_UpperCamelCase     (			self   )			->       Dict:
								return CvtConfig(
								    image_size=self.image_size			,     num_labels=self.num_labels			,     num_channels=self.num_channels			,     embed_dim=self.embed_dim			,     num_heads=self.num_heads			,     patch_sizes=self.patch_sizes			,     patch_padding=self.patch_padding			,     patch_stride=self.patch_stride			,     stride_kv=self.stride_kv			,     depth=self.depth			,     cls_token=self.cls_token			,     attention_drop_rate=self.attention_drop_rate			,     initializer_range=self.initializer_range			,     )




	def 				_UpperCamelCase     (			self			,     a			,     a			,     a   )			->       int:
								snake_case_           =				TFCvtModel(config=a   )
								snake_case_           =				model(a			,     training=a   )
								snake_case_           =				(self.image_size, self.image_size)
								snake_case_     ,						snake_case_           =				image_size[0], image_size[1]
								for i in range(len(self.depth   )   ):
															snake_case_           =				floor(((height + 2 * self.patch_padding[i] - self.patch_sizes[i]) / self.patch_stride[i]) + 1   )
															snake_case_           =				floor(((width + 2 * self.patch_padding[i] - self.patch_sizes[i]) / self.patch_stride[i]) + 1   )
								self.parent.assertEqual(result.last_hidden_state.shape			,     (self.batch_size, self.embed_dim[-1], height, width)   )




	def 				_UpperCamelCase     (			self			,     a			,     a			,     a   )			->       Dict:
								snake_case_           =				self.num_labels
								snake_case_           =				TFCvtForImageClassification(a   )
								snake_case_           =				model(a			,     labels=a			,     training=a   )
								self.parent.assertEqual(result.logits.shape			,     (self.batch_size, self.num_labels)   )






	def 				_UpperCamelCase     (			self   )			->       Tuple:
								snake_case_           =				self.prepare_config_and_inputs()
								snake_case_     ,						snake_case_     ,						snake_case_           =				config_and_inputs
								snake_case_           =				{'pixel_values': pixel_values}
								return config, inputs_dict






@require_tf
class 					UpperCamelCase_ (     snake_case_			,   snake_case_			,   unittest.TestCase							):



	'''simple docstring'''




	lowerCAmelCase						=					(TFCvtModel, TFCvtForImageClassification) if is_tf_available() else ()
	lowerCAmelCase						=					(
	    {'''feature-extraction''': TFCvtModel, '''image-classification''': TFCvtForImageClassification}
	    if is_tf_available()
	    else {}
	)
	lowerCAmelCase						=					False
	lowerCAmelCase						=					False
	lowerCAmelCase						=					False
	lowerCAmelCase						=					False
	lowerCAmelCase						=					False




	def 				_UpperCamelCase     (			self   )			->       Optional[int]:
								snake_case_           =				TFCvtModelTester(self   )
								snake_case_           =				TFCvtConfigTester(self			,     config_class=a			,     has_text_modality=a			,     hidden_size=37   )




	def 				_UpperCamelCase     (			self   )			->       Optional[int]:
								self.config_tester.create_and_test_config_common_properties()
								self.config_tester.create_and_test_config_to_json_string()
								self.config_tester.create_and_test_config_to_json_file()
								self.config_tester.create_and_test_config_from_and_save_pretrained()
								self.config_tester.create_and_test_config_with_num_labels()
								self.config_tester.check_config_can_be_init_without_params()
								self.config_tester.check_config_arguments_init()




	@unittest.skip(reason='Cvt does not output attentions'   )
	def 				_UpperCamelCase     (			self   )			->       Dict:
								pass




	@unittest.skip(reason='Cvt does not use inputs_embeds'   )
	def 				_UpperCamelCase     (			self   )			->       List[str]:
								pass




	@unittest.skip(reason='Cvt does not support input and output embeddings'   )
	def 				_UpperCamelCase     (			self   )			->       Optional[int]:
								pass




	@unittest.skipIf(
	    not is_tf_available() or len(tf.config.list_physical_devices('GPU'   )   ) == 0			,     reason='TF does not support backprop for grouped convolutions on CPU.'			,     )
	def 				_UpperCamelCase     (			self   )			->       Dict:
								super().test_dataset_conversion()




	@unittest.skipIf(
	    not is_tf_available() or len(tf.config.list_physical_devices('GPU'   )   ) == 0			,     reason='TF does not support backprop for grouped convolutions on CPU.'			,     )
	@slow
	def 				_UpperCamelCase     (			self   )			->       Dict:
								super().test_keras_fit()




	@unittest.skip(reason='Get `Failed to determine best cudnn convolution algo.` error after using TF 2.12+cuda 11.8'   )
	def 				_UpperCamelCase     (			self   )			->       List[str]:
								snake_case_           =				tf.keras.mixed_precision.Policy('mixed_float16'   )
								tf.keras.mixed_precision.set_global_policy(a   )
								super().test_keras_fit()
								tf.keras.mixed_precision.set_global_policy('float32'   )




	def 				_UpperCamelCase     (			self   )			->       Optional[int]:
								snake_case_     ,						snake_case_           =				self.model_tester.prepare_config_and_inputs_for_common()

								for model_class in self.all_model_classes:
															snake_case_           =				model_class(a   )
															snake_case_           =				inspect.signature(model.call   )
															# signature.parameters is an OrderedDict => so arg_names order is deterministic
															snake_case_           =				[*signature.parameters.keys()]

															snake_case_           =				['pixel_values']
															self.assertListEqual(arg_names[:1]			,     a   )




	def 				_UpperCamelCase     (			self   )			->       Optional[Any]:
								def check_hidden_states_output(a			,     a			,     a   ):
															snake_case_           =				model_class(a   )

															snake_case_           =				model(**self._prepare_for_class(a			,     a   )   )
															snake_case_           =				outputs.hidden_states

															snake_case_           =				len(self.model_tester.depth   )
															self.assertEqual(len(a   )			,     a   )

															# verify the first hidden states (first block)
															self.assertListEqual(
															    list(hidden_states[0].shape[-3:]   )			,     [
															        self.model_tester.embed_dim[0],
															        self.model_tester.image_size // 4,
															        self.model_tester.image_size // 4,
															    ]			,     )

								snake_case_     ,						snake_case_           =				self.model_tester.prepare_config_and_inputs_for_common()

								for model_class in self.all_model_classes:
															snake_case_           =				True
															check_hidden_states_output(a			,     a			,     a   )

															# check that output_hidden_states also work using config
															del inputs_dict["output_hidden_states"]
															snake_case_           =				True

															check_hidden_states_output(a			,     a			,     a   )




	def 				_UpperCamelCase     (			self   )			->       List[Any]:
								snake_case_           =				self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_model(*a   )




	def 				_UpperCamelCase     (			self   )			->       List[str]:
								snake_case_           =				self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_image_classification(*a   )






	@slow
	def 				_UpperCamelCase     (			self   )			->       int:
								for model_name in TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
															snake_case_           =				TFCvtModel.from_pretrained(a   )
															self.assertIsNotNone(a   )






def 		__UpperCAmelCase     (	):
							snake_case_           =				Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png')
							return image




@require_tf
@require_vision
class 					UpperCamelCase_ (     unittest.TestCase							):



	'''simple docstring'''




	@cached_property
	def 				_UpperCamelCase     (			self   )			->       Any:
								return AutoImageProcessor.from_pretrained(TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0]   )






	@slow
	def 				_UpperCamelCase     (			self   )			->       Dict:
								snake_case_           =				TFCvtForImageClassification.from_pretrained(TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0]   )

								snake_case_           =				self.default_image_processor
								snake_case_           =				prepare_img()
								snake_case_           =				image_processor(images=a			,     return_tensors='tf'   )

								# forward pass
								snake_case_           =				model(**a   )

								# verify the logits
								snake_case_           =				tf.TensorShape((1, 10_00)   )
								self.assertEqual(outputs.logits.shape			,     a   )

								snake_case_           =				tf.constant([0.9_285, 0.9_015, -0.3_150]   )
								self.assertTrue(np.allclose(outputs.logits[0, :3].numpy()			,     a			,     atol=1E-4   )   )





