



"""simple docstring"""
import inspect
import unittest

from huggingface_hub import hf_hub_download

from transformers import ASTConfig
from transformers.testing_utils import require_torch, require_torchaudio, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_torchaudio_available

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


if is_torch_available():
			import torch
			from torch import nn

			from transformers import ASTForAudioClassification, ASTModel
			from transformers.models.audio_spectrogram_transformer.modeling_audio_spectrogram_transformer import (
			    AUDIO_SPECTROGRAM_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST,
			)


if is_torchaudio_available():
			import torchaudio

			from transformers import ASTFeatureExtractor


class 							lowerCamelCase_:





				'''simple docstring'''



				def __init__(    self				,       lowerCamelCase__				,       lowerCamelCase__=1_3				,       lowerCamelCase__=2				,       lowerCamelCase__=2_4				,       lowerCamelCase__=1_6				,       lowerCamelCase__=True				,       lowerCamelCase__=True				,       lowerCamelCase__=3_2				,       lowerCamelCase__=5				,       lowerCamelCase__=4				,       lowerCamelCase__=3_7				,       lowerCamelCase__="gelu"				,       lowerCamelCase__=0.1				,       lowerCamelCase__=0.1				,       lowerCamelCase__=1_0				,       lowerCamelCase__=0.0_2				,       lowerCamelCase__=None				,       lowerCamelCase__=2				,       lowerCamelCase__=2				,       ):
								_lowerCamelCase        =				parent
								_lowerCamelCase        =				batch_size
								_lowerCamelCase        =				patch_size
								_lowerCamelCase        =				max_length
								_lowerCamelCase        =				num_mel_bins
								_lowerCamelCase        =				is_training
								_lowerCamelCase        =				use_labels
								_lowerCamelCase        =				hidden_size
								_lowerCamelCase        =				num_hidden_layers
								_lowerCamelCase        =				num_attention_heads
								_lowerCamelCase        =				intermediate_size
								_lowerCamelCase        =				hidden_act
								_lowerCamelCase        =				hidden_dropout_prob
								_lowerCamelCase        =				attention_probs_dropout_prob
								_lowerCamelCase        =				type_sequence_label_size
								_lowerCamelCase        =				initializer_range
								_lowerCamelCase        =				scope
								_lowerCamelCase        =				frequency_stride
								_lowerCamelCase        =				time_stride

								# in AST, the seq length equals the number of patches + 2 (we add 2 for the [CLS] and distillation tokens)
								_lowerCamelCase        =				(self.num_mel_bins - self.patch_size) // self.frequency_stride + 1
								_lowerCamelCase        =				(self.max_length - self.patch_size) // self.time_stride + 1
								_lowerCamelCase        =				frequency_out_dimension * time_out_dimension
								_lowerCamelCase        =				num_patches + 2

				def 			snake_case__      (    self   ):
								_lowerCamelCase        =				floats_tensor([self.batch_size, self.max_length, self.num_mel_bins]   )

								_lowerCamelCase        =				None
								if self.use_labels:
												_lowerCamelCase        =				ids_tensor([self.batch_size]				,       self.type_sequence_label_size   )

								_lowerCamelCase        =				self.get_config()

								return config, input_values, labels

				def 			snake_case__      (    self   ):
								return ASTConfig(
								    patch_size=self.patch_size				,       max_length=self.max_length				,       num_mel_bins=self.num_mel_bins				,       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				,       is_decoder=lowerCamelCase__				,       initializer_range=self.initializer_range				,       frequency_stride=self.frequency_stride				,       time_stride=self.time_stride				,       )

				def 			snake_case__      (    self				,       lowerCamelCase__				,       lowerCamelCase__				,       lowerCamelCase__   ):
								_lowerCamelCase        =				ASTModel(config=lowerCamelCase__   )
								model.to(lowerCamelCase__   )
								model.eval()
								_lowerCamelCase        =				model(lowerCamelCase__   )
								self.parent.assertEqual(result.last_hidden_state.shape				,       (self.batch_size, self.seq_length, self.hidden_size)   )





				def 			snake_case__      (    self   ):
								_lowerCamelCase        =				self.prepare_config_and_inputs()
								(
								    (
								    _lowerCamelCase     
								)  ,     (
								    _lowerCamelCase     
								)  ,     (
								    _lowerCamelCase     
								)  ,          
								)        =				config_and_inputs
								_lowerCamelCase        =				{'''input_values''': input_values}
								return config, inputs_dict


@require_torch
class 							lowerCamelCase_(   A__,				A__,				unittest.TestCase    ):





				'''simple docstring'''



				lowercase__   :       Dict										= (
				    (
				        ASTModel,
				        ASTForAudioClassification,
				    )
				    if is_torch_available()
				    else ()
				)
				lowercase__   :       str										= (
				    {'audio-classification': ASTForAudioClassification, 'feature-extraction': ASTModel}
				    if is_torch_available()
				    else {}
				)
				lowercase__   :       str										= False
				lowercase__   :       List[str]										= False
				lowercase__   :       List[str]										= False
				lowercase__   :       Dict										= False

				def 			snake_case__      (    self				,       lowerCamelCase__				,       lowerCamelCase__				,       lowerCamelCase__				,       lowerCamelCase__				,       lowerCamelCase__   ):
								if pipeline_test_casse_name == "AudioClassificationPipelineTests":
												return True

								return False

				def 			snake_case__      (    self   ):
								_lowerCamelCase        =				ASTModelTester(self   )
								_lowerCamelCase        =				ConfigTester(self				,       config_class=lowerCamelCase__				,       has_text_modality=lowerCamelCase__				,       hidden_size=3_7   )

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

				@unittest.skip(reason='''AST does not use inputs_embeds'''   )
				def 			snake_case__      (    self   ):
								pass

				def 			snake_case__      (    self   ):
								_lowerCamelCase  ,     _lowerCamelCase        =				self.model_tester.prepare_config_and_inputs_for_common()

								for model_class in self.all_model_classes:
												_lowerCamelCase        =				model_class(lowerCamelCase__   )
												self.assertIsInstance(model.get_input_embeddings()				,       (nn.Module)   )
												_lowerCamelCase        =				model.get_output_embeddings()
												self.assertTrue(x is None or isinstance(lowerCamelCase__				,       nn.Linear   )   )

				def 			snake_case__      (    self   ):
								_lowerCamelCase  ,     _lowerCamelCase        =				self.model_tester.prepare_config_and_inputs_for_common()

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

												_lowerCamelCase        =				['''input_values''']
												self.assertListEqual(arg_names[:1]				,       lowerCamelCase__   )

				def 			snake_case__      (    self   ):
								_lowerCamelCase        =				self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_model(*lowerCamelCase__   )





				@slow
				def 			snake_case__      (    self   ):
								for model_name in AUDIO_SPECTROGRAM_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
												_lowerCamelCase        =				ASTModel.from_pretrained(lowerCamelCase__   )
												self.assertIsNotNone(lowerCamelCase__   )



def        lowerCAmelCase_(   )	->       Optional[Any]:
				_lowerCamelCase        =				hf_hub_download(
				    repo_id='''nielsr/audio-spectogram-transformer-checkpoint'''						,   filename='''sample_audio.flac'''						,   repo_type='''dataset'''							)

				_lowerCamelCase  ,     _lowerCamelCase        =				torchaudio.load(lowercase_							)

				return audio, sampling_rate




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





				'''simple docstring'''



				@cached_property
				def 			snake_case__      (    self   ):
								return (
								    ASTFeatureExtractor.from_pretrained('''MIT/ast-finetuned-audioset-10-10-0.4593'''   )
								    if is_torchaudio_available()
								    else None
								)





				@slow
				def 			snake_case__      (    self   ):
								_lowerCamelCase        =				self.default_feature_extractor
								_lowerCamelCase        =				ASTForAudioClassification.from_pretrained('''MIT/ast-finetuned-audioset-10-10-0.4593'''   ).to(lowerCamelCase__   )

								_lowerCamelCase        =				self.default_feature_extractor
								_lowerCamelCase  ,     _lowerCamelCase        =				prepare_audio()
								_lowerCamelCase        =				audio.squeeze().numpy()
								_lowerCamelCase        =				feature_extractor(lowerCamelCase__				,       sampling_rate=lowerCamelCase__				,       return_tensors='''pt'''   ).to(lowerCamelCase__   )

								# forward pass
								with torch.no_grad():
												_lowerCamelCase        =				model(**lowerCamelCase__   )

								# verify the logits
								_lowerCamelCase        =				torch.Size((1, 5_2_7)   )
								self.assertEqual(outputs.logits.shape				,       lowerCamelCase__   )

								_lowerCamelCase        =				torch.tensor([-0.8_7_6_0, -7.0_0_4_2, -8.6_6_0_2]   ).to(lowerCamelCase__   )

								self.assertTrue(torch.allclose(outputs.logits[0, :3]				,       lowerCamelCase__				,       atol=1e-4   )   )





"""simple docstring"""
import numpy as np



def        lowerCAmelCase_(   lowercase_     :							np.ndarray						,   lowercase_     :							np.ndarray						,   lowercase_     :							float = 1e-12						,   lowercase_     :							int = 1_00						,   )	->       tuple[float, np.ndarray]:
				assert np.shape(lowercase_							)[0] == np.shape(lowercase_							)[1]
				# Ensure proper dimensionality.
				assert np.shape(lowercase_							)[0] == np.shape(lowercase_							)[0]
				# Ensure inputs are either both complex or both real
				assert np.iscomplexobj(lowercase_							) == np.iscomplexobj(lowercase_							)
				_lowerCamelCase        =				np.iscomplexobj(lowercase_							)
				if is_complex:
								# Ensure complex input_matrix is Hermitian
								assert np.array_equal(lowercase_						,   input_matrix.conj().T							)

				# Set convergence to False. Will define convergence when we exceed max_iterations
				# or when we have small changes from one iteration to next.

				_lowerCamelCase        =				False
				_lowerCamelCase        =				0
				_lowerCamelCase        =				0
				_lowerCamelCase        =				1e12

				while not convergence:
								# Multiple matrix by the vector.
								_lowerCamelCase        =				np.dot(lowercase_						,   lowercase_							)
								# Normalize the resulting output vector.
								_lowerCamelCase        =				w / np.linalg.norm(lowercase_							)
								# Find rayleigh quotient
								# (faster than usual b/c we know vector is normalized already)
								_lowerCamelCase        =				vector.conj().T if is_complex else vector.T
								_lowerCamelCase        =				np.dot(lowercase_						,   np.dot(lowercase_						,   lowercase_							)							)

								# Check convergence.
								_lowerCamelCase        =				np.abs(lambda_ - lambda_previous							) / lambda_
								iterations += 1

								if error <= error_tol or iterations >= max_iterations:
												_lowerCamelCase        =				True

								_lowerCamelCase        =				lambda_

				if is_complex:
								_lowerCamelCase        =				np.real(lambda_							)

				return lambda_, vector



def        lowerCAmelCase_(   )	->       None:
				_lowerCamelCase        =				np.array([[41, 4, 20], [4, 26, 30], [20, 30, 50]]							)
				_lowerCamelCase        =				np.array([41, 4, 20]							)
				_lowerCamelCase        =				real_input_matrix.astype(np.complexaaa							)
				_lowerCamelCase        =				np.triu(1j * complex_input_matrix						,   1							)
				complex_input_matrix += imag_matrix
				complex_input_matrix += -1 * imag_matrix.T
				_lowerCamelCase        =				np.array([41, 4, 20]							).astype(np.complexaaa							)

				for problem_type in ["real", "complex"]:
								if problem_type == "real":
												_lowerCamelCase        =				real_input_matrix
												_lowerCamelCase        =				real_vector
								elif problem_type == "complex":
												_lowerCamelCase        =				complex_input_matrix
												_lowerCamelCase        =				complex_vector

								# Our implementation.
								_lowerCamelCase  ,     _lowerCamelCase        =				power_iteration(lowercase_						,   lowercase_							)

								# Numpy implementation.

								# Get eigenvalues and eigenvectors using built-in numpy
								# eigh (eigh used for symmetric or hermetian matrices).
								_lowerCamelCase  ,     _lowerCamelCase        =				np.linalg.eigh(lowercase_							)
								# Last eigenvalue is the maximum one.
								_lowerCamelCase        =				eigen_values[-1]
								# Last column in this matrix is eigenvector corresponding to largest eigenvalue.
								_lowerCamelCase        =				eigen_vectors[:, -1]

								# Check our implementation and numpy gives close answers.
								assert np.abs(eigen_value - eigen_value_max							) <= 1e-6
								# Take absolute values element wise of each eigenvector.
								# as they are only unique to a minus sign.
								assert np.linalg.norm(np.abs(lowercase_							) - np.abs(lowercase_							)							) <= 1e-6


if __name__ == "__main__":
			import doctest

			doctest.testmod()
			test_power_iteration()
