
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 			A_  :




	'''simple docstring'''


	def __init__(			self						,       snake_case						,       snake_case=13						,       snake_case=2						,       snake_case=24						,       snake_case=16						,       snake_case=True						,       snake_case=True						,       snake_case=32						,       snake_case=5						,       snake_case=4						,       snake_case=37						,       snake_case="gelu"						,       snake_case=0.1						,       snake_case=0.1						,       snake_case=10						,       snake_case=0.02						,       snake_case=None						,       snake_case=2						,       snake_case=2						,       ):
		lowercase	    =       parent
		lowercase	    =       batch_size
		lowercase	    =       patch_size
		lowercase	    =       max_length
		lowercase	    =       num_mel_bins
		lowercase	    =       is_training
		lowercase	    =       use_labels
		lowercase	    =       hidden_size
		lowercase	    =       num_hidden_layers
		lowercase	    =       num_attention_heads
		lowercase	    =       intermediate_size
		lowercase	    =       hidden_act
		lowercase	    =       hidden_dropout_prob
		lowercase	    =       attention_probs_dropout_prob
		lowercase	    =       type_sequence_label_size
		lowercase	    =       initializer_range
		lowercase	    =       scope
		lowercase	    =       frequency_stride
		lowercase	    =       time_stride

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





	def       SCREAMING_SNAKE_CASE__		(			self			):
		lowercase	    =       floats_tensor([self.batch_size, self.max_length, self.num_mel_bins]			)

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

		lowercase	    =       self.get_config()

		return config, input_values, labels





	def       SCREAMING_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=snake_case						,       initializer_range=self.initializer_range						,       frequency_stride=self.frequency_stride						,       time_stride=self.time_stride						,       )





	def       SCREAMING_SNAKE_CASE__		(			self						,       snake_case						,       snake_case						,       snake_case			):
		lowercase	    =       ASTModel(config=snake_case			)
		model.to(snake_case			)
		model.eval()
		lowercase	    =       model(snake_case			)
		self.parent.assertEqual(result.last_hidden_state.shape						,       (self.batch_size, self.seq_length, self.hidden_size)			)





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







@require_torch
class 			A_  (     __lowerCamelCase ,      __lowerCamelCase ,      unittest.TestCase  ):




	'''simple docstring'''


	_UpperCamelCase	:	str									=			(
	    (
	        ASTModel,
	        ASTForAudioClassification,
	    )
	    if is_torch_available()
	    else ()
	)
	_UpperCamelCase	:	Optional[int]									=			(
	    {"""audio-classification""": ASTForAudioClassification, """feature-extraction""": ASTModel}
	    if is_torch_available()
	    else {}
	)
	_UpperCamelCase	:	Union[str, Any]									=			False
	_UpperCamelCase	:	List[Any]									=			False
	_UpperCamelCase	:	Any									=			False
	_UpperCamelCase	:	Optional[int]									=			False





	def       SCREAMING_SNAKE_CASE__		(			self						,       snake_case						,       snake_case						,       snake_case						,       snake_case						,       snake_case			):
		if pipeline_test_casse_name == "AudioClassificationPipelineTests":
			return True

		return False





	def       SCREAMING_SNAKE_CASE__		(			self			):
		lowercase	    =       ASTModelTester(self			)
		lowercase	    =       ConfigTester(self						,       config_class=snake_case						,       has_text_modality=snake_case						,       hidden_size=37			)





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





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





	def       SCREAMING_SNAKE_CASE__		(			self			):
		lowercase    ,   lowercase	    =       self.model_tester.prepare_config_and_inputs_for_common()

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





	def       SCREAMING_SNAKE_CASE__		(			self			):
		lowercase    ,   lowercase	    =       self.model_tester.prepare_config_and_inputs_for_common()

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

			lowercase	    =       ['input_values']
			self.assertListEqual(arg_names[:1]						,       snake_case			)





	def       SCREAMING_SNAKE_CASE__		(			self			):
		lowercase	    =       self.model_tester.prepare_config_and_inputs()
		self.model_tester.create_and_check_model(*snake_case			)





	@slow
	def       SCREAMING_SNAKE_CASE__		(			self			):
		for model_name in AUDIO_SPECTROGRAM_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
			lowercase	    =       ASTModel.from_pretrained(snake_case			)
			self.assertIsNotNone(snake_case			)






def 				UpperCAmelCase_						(				):
	lowercase	    =       hf_hub_download(
	    repo_id='nielsr/audio-spectogram-transformer-checkpoint'					,				filename='sample_audio.flac'					,				repo_type='dataset'				)

	lowercase    ,   lowercase	    =       torchaudio.load(__SCREAMING_SNAKE_CASE				)

	return audio, sampling_rate







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




	'''simple docstring'''


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





	@slow
	def       SCREAMING_SNAKE_CASE__		(			self			):
		lowercase	    =       self.default_feature_extractor
		lowercase	    =       ASTForAudioClassification.from_pretrained('MIT/ast-finetuned-audioset-10-10-0.4593'			).to(snake_case			)

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

		# forward pass
		with torch.no_grad():
			lowercase	    =       model(**snake_case			)

		# verify the logits
		lowercase	    =       torch.Size((1, 527)			)
		self.assertEqual(outputs.logits.shape						,       snake_case			)

		lowercase	    =       torch.tensor([-0.8_760, -7.0_042, -8.6_602]			).to(snake_case			)

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





import copy

from ...configuration_utils import PretrainedConfig
from ...utils import logging
from ..auto import CONFIG_MAPPING


SCREAMING_SNAKE_CASE			: List[str]				   =					logging.get_logger(__name__)

SCREAMING_SNAKE_CASE			: Any				   =					{
    "ut/deta": "https://huggingface.co/ut/deta/resolve/main/config.json",
}






class _lowerCamelCase(		_a  ):
       lowercase_					:      Any       					=     """deta"""
       lowercase_					:      Union[str, Any]       					=     {
           """hidden_size""": """d_model""",
           """num_attention_heads""": """encoder_attention_heads""",
       }
       def __init__(   self, lowerCamelCase=None, lowerCamelCase=9_00, lowerCamelCase=20_48, lowerCamelCase=6, lowerCamelCase=20_48, lowerCamelCase=8, lowerCamelCase=6, lowerCamelCase=10_24, lowerCamelCase=8, lowerCamelCase=0.0, lowerCamelCase=True, lowerCamelCase="relu", lowerCamelCase=2_56, lowerCamelCase=0.1, lowerCamelCase=0.0, lowerCamelCase=0.0, lowerCamelCase=0.0_2, lowerCamelCase=1.0, lowerCamelCase=True, lowerCamelCase=False, lowerCamelCase="sine", lowerCamelCase=5, lowerCamelCase=4, lowerCamelCase=4, lowerCamelCase=True, lowerCamelCase=3_00, lowerCamelCase=True, lowerCamelCase=True, lowerCamelCase=1, lowerCamelCase=5, lowerCamelCase=2, lowerCamelCase=1, lowerCamelCase=1, lowerCamelCase=5, lowerCamelCase=2, lowerCamelCase=0.1, lowerCamelCase=0.2_5, **lowerCamelCase, )			->  Any:




           """simple docstring"""

           if backbone_config is None:
               logger.info('`backbone_config` is `None`. Initializing the config with the default `ResNet` backbone.')
               _lowercase			:				List[Any] 						=						CONFIG_MAPPING['resnet'](out_features=['stage2', 'stage3', 'stage4'])
           else:
               if isinstance(lowerCamelCase, lowerCamelCase):
                   _lowercase			:				Dict 						=						backbone_config.pop('model_type')
                   _lowercase			:				int 						=						CONFIG_MAPPING[backbone_model_type]
                   _lowercase			:				Union[str, Any] 						=						config_class.from_dict(lowerCamelCase)

           _lowercase			:				Union[str, Any] 						=						backbone_config
           _lowercase			:				Any 						=						num_queries
           _lowercase			:				Union[str, Any] 						=						max_position_embeddings
           _lowercase			:				Union[str, Any] 						=						d_model
           _lowercase			:				Optional[int] 						=						encoder_ffn_dim
           _lowercase			:				Optional[int] 						=						encoder_layers
           _lowercase			:				Optional[Any] 						=						encoder_attention_heads
           _lowercase			:				Optional[Any] 						=						decoder_ffn_dim
           _lowercase			:				Dict 						=						decoder_layers
           _lowercase			:				Tuple 						=						decoder_attention_heads
           _lowercase			:				Union[str, Any] 						=						dropout
           _lowercase			:				Optional[Any] 						=						attention_dropout
           _lowercase			:				int 						=						activation_dropout
           _lowercase			:				Tuple 						=						activation_function
           _lowercase			:				List[Any] 						=						init_std
           _lowercase			:				Union[str, Any] 						=						init_xavier_std
           _lowercase			:				int 						=						encoder_layerdrop
           _lowercase			:				Optional[int] 						=						auxiliary_loss
           _lowercase			:				Dict 						=						position_embedding_type
           # deformable attributes
           _lowercase			:				Any 						=						num_feature_levels
           _lowercase			:				str 						=						encoder_n_points
           _lowercase			:				Any 						=						decoder_n_points
           _lowercase			:				List[str] 						=						two_stage
           _lowercase			:				Dict 						=						two_stage_num_proposals
           _lowercase			:				Any 						=						with_box_refine
           _lowercase			:				List[Any] 						=						assign_first_stage
           if two_stage is True and with_box_refine is False:
               raise ValueError('If two_stage is True, with_box_refine must be True.')
           # Hungarian matcher
           _lowercase			:				List[Any] 						=						class_cost
           _lowercase			:				Optional[int] 						=						bbox_cost
           _lowercase			:				str 						=						giou_cost
           # Loss coefficients
           _lowercase			:				Optional[int] 						=						mask_loss_coefficient
           _lowercase			:				int 						=						dice_loss_coefficient
           _lowercase			:				List[Any] 						=						bbox_loss_coefficient
           _lowercase			:				Optional[Any] 						=						giou_loss_coefficient
           _lowercase			:				str 						=						eos_coefficient
           _lowercase			:				int 						=						focal_alpha
           super().__init__(is_encoder_decoder=lowerCamelCase, **lowerCamelCase)
       @property
       def      UpperCamelCase	(   self)			->  int:




           """simple docstring"""

           return self.encoder_attention_heads
       @property
       def      UpperCamelCase	(   self)			->  int:




           """simple docstring"""

           return self.d_model




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




           """simple docstring"""

           _lowercase			:				int 						=						copy.deepcopy(self.__dict__)
           _lowercase			:				Optional[int] 						=						self.backbone_config.to_dict()
           _lowercase			:				Optional[Any] 						=						self.__class__.model_type
           return output


