



'''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 							a__:







				def __init__(				self			:  Any						,      __snake_case			:  Dict						,      __snake_case			:  Dict=13						,      __snake_case			:  Dict=2						,      __snake_case			:  Optional[Any]=24						,      __snake_case			:  int=16						,      __snake_case			:  Any=True						,      __snake_case			:  List[str]=True						,      __snake_case			:  Any=32						,      __snake_case			:  str=5						,      __snake_case			:  str=4						,      __snake_case			:  Any=37						,      __snake_case			:  Optional[int]="gelu"						,      __snake_case			:  List[str]=0.1						,      __snake_case			:  Tuple=0.1						,      __snake_case			:  Optional[int]=10						,      __snake_case			:  List[str]=0.02						,      __snake_case			:  Tuple=None						,      __snake_case			:  Optional[int]=2						,      __snake_case			:  Union[str, Any]=2						,      ):
						a						: Tuple					       =					parent
						a						: Optional[int]					       =					batch_size
						a						: Union[str, Any]					       =					patch_size
						a						: Tuple					       =					max_length
						a						: Optional[Any]					       =					num_mel_bins
						a						: str					       =					is_training
						a						: Optional[Any]					       =					use_labels
						a						: Optional[Any]					       =					hidden_size
						a						: str					       =					num_hidden_layers
						a						: Optional[Any]					       =					num_attention_heads
						a						: Tuple					       =					intermediate_size
						a						: Optional[Any]					       =					hidden_act
						a						: Any					       =					hidden_dropout_prob
						a						: List[str]					       =					attention_probs_dropout_prob
						a						: Optional[int]					       =					type_sequence_label_size
						a						: Tuple					       =					initializer_range
						a						: List[str]					       =					scope
						a						: Dict					       =					frequency_stride
						a						: Union[str, Any]					       =					time_stride

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







				def       lowercase_	(				self			:  Any     ):
						a						: Union[str, Any]					       =					floats_tensor([self.batch_size, self.max_length, self.num_mel_bins]     )

						a						: Any					       =					None
						if self.use_labels:
								a						: str					       =					ids_tensor([self.batch_size]						,      self.type_sequence_label_size     )

						a						: Tuple					       =					self.get_config()

						return config, input_values, labels







				def       lowercase_	(				self			:  Optional[Any]     ):
						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=_UpperCAmelCase						,      initializer_range=self.initializer_range						,      frequency_stride=self.frequency_stride						,      time_stride=self.time_stride						,      )







				def       lowercase_	(				self			:  Any						,      __snake_case			:  List[Any]						,      __snake_case			:  Tuple						,      __snake_case			:  Optional[Any]     ):
						a						: Any					       =					ASTModel(config=_UpperCAmelCase     )
						model.to(_UpperCAmelCase     )
						model.eval()
						a						: int					       =					model(_UpperCAmelCase     )
						self.parent.assertEqual(result.last_hidden_state.shape						,      (self.batch_size, self.seq_length, self.hidden_size)     )







				def       lowercase_	(				self			:  Optional[Any]     ):
						a						: Tuple					       =					self.prepare_config_and_inputs()
						(
						    (
						    a     
						)       ,     (
						    a     
						)       ,     (
						    a     
						)       ,          
						)						: Tuple					       =					config_and_inputs
						a						: str					       =					{'input_values': input_values}
						return config, inputs_dict

@require_torch
class 							a__( lowerCamelCase_		,			lowerCamelCase_		,			unittest.TestCase     ):
				lowercase__			=   (
				    (
				        ASTModel,
				        ASTForAudioClassification,
				    )
				    if is_torch_available()
				    else ()
				)
				lowercase__			=   (
				    {"""audio-classification""": ASTForAudioClassification, """feature-extraction""": ASTModel}
				    if is_torch_available()
				    else {}
				)
				lowercase__			=   False
				lowercase__			=   False
				lowercase__			=   False
				lowercase__			=   False







				def       lowercase_	(				self			:  str						,      __snake_case			:  Optional[Any]						,      __snake_case			:  int						,      __snake_case			:  Union[str, Any]						,      __snake_case			:  Tuple						,      __snake_case			:  List[str]     ):






						if pipeline_test_casse_name == "AudioClassificationPipelineTests":
								return True

						return False







				def       lowercase_	(				self			:  int     ):
						a						: List[Any]					       =					ASTModelTester(self     )
						a						: Any					       =					ConfigTester(self						,      config_class=_UpperCAmelCase						,      has_text_modality=_UpperCAmelCase						,      hidden_size=37     )







				def       lowercase_	(				self			:  Optional[int]     ):
						self.config_tester.run_common_tests()







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







				def       lowercase_	(				self			:  Dict     ):
						a       ,     a						: Any					       =					self.model_tester.prepare_config_and_inputs_for_common()

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







				def       lowercase_	(				self			:  Optional[int]     ):
						a       ,     a						: List[str]					       =					self.model_tester.prepare_config_and_inputs_for_common()

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

								a						: Optional[int]					       =					['input_values']
								self.assertListEqual(arg_names[:1]						,      _UpperCAmelCase     )







				def       lowercase_	(				self			:  List[Any]     ):
						a						: Dict					       =					self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_model(*_UpperCAmelCase     )







				@slow
				def       lowercase_	(				self			:  Dict     ):






						for model_name in AUDIO_SPECTROGRAM_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
								a						: Any					       =					ASTModel.from_pretrained(_UpperCAmelCase     )
								self.assertIsNotNone(_UpperCAmelCase     )
def 				lowerCamelCase__	(						):
		a						: Union[str, Any]					       =					hf_hub_download(
		    repo_id='nielsr/audio-spectogram-transformer-checkpoint'			,      filename='sample_audio.flac'			,      repo_type='dataset'    )

		a       ,     a						: Optional[int]					       =					torchaudio.load(SCREAMING_SNAKE_CASE__    )

		return audio, sampling_rate

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







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







				@slow
				def       lowercase_	(				self			:  str     ):
						a						: List[str]					       =					self.default_feature_extractor
						a						: List[str]					       =					ASTForAudioClassification.from_pretrained('MIT/ast-finetuned-audioset-10-10-0.4593'     ).to(_UpperCAmelCase     )

						a						: List[str]					       =					self.default_feature_extractor
						a       ,     a						: Tuple					       =					prepare_audio()
						a						: Tuple					       =					audio.squeeze().numpy()
						a						: str					       =					feature_extractor(_UpperCAmelCase						,      sampling_rate=_UpperCAmelCase						,      return_tensors='pt'     ).to(_UpperCAmelCase     )

						# forward pass
						with torch.no_grad():
								a						: List[str]					       =					model(**_UpperCAmelCase     )

						# verify the logits
						a						: Union[str, Any]					       =					torch.Size((1, 5_27)     )
						self.assertEqual(outputs.logits.shape						,      _UpperCAmelCase     )

						a						: Optional[int]					       =					torch.tensor([-0.8760, -7.0042, -8.6602]     ).to(_UpperCAmelCase     )

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




'''simple docstring'''






import torch

from transformers import AutoModel

class 							a__( torch.nn.Module     ):







    def __init__(				self			:  Any						,      __snake_case			:  str="sayef/fsner-bert-base-uncased"     ):
      super(__snake_case						,      self     ).__init__()

      a						: List[str]					       =					AutoModel.from_pretrained(__snake_case						,      return_dict=__snake_case     )
      a						: Optional[Any]					       =					torch.nn.CosineSimilarity(3						,      1e-0_8     )
      a						: Tuple					       =					torch.nn.Softmax(dim=1     )







    def       lowercase_	(				self			:  List[str]						,      **__snake_case			:  int     ):
      return self.bert(**__snake_case     ).last_hidden_state







    def       lowercase_	(				self			:  Optional[int]						,      __snake_case			:  Union[str, Any]     ):
      return token_embeddings.sum(2						,      keepdim=__snake_case     )







    def       lowercase_	(				self			:  str						,      __snake_case			:  Tuple						,      __snake_case			:  List[Any]						,      __snake_case			:  Optional[Any]=1     ):
      return self.softmax(T * self.cos(__snake_case						,      __snake_case     )     )







    def       lowercase_	(				self			:  int						,      __snake_case			:  int						,      __snake_case			:  Tuple     ):
      a						: List[Any]					       =					W_supports['sizes'].tolist()
      a						: Any					       =					W_supports['start_token_id'].item()
      a						: int					       =					W_supports['end_token_id'].item()

      del W_supports["sizes"]
      del W_supports["start_token_id"]
      del W_supports["end_token_id"]

      a						: Optional[Any]					       =					self.BERT(**__snake_case     )
      a						: Tuple					       =					self.BERT(**__snake_case     )

      a						: Dict					       =					None
      a						: Optional[Any]					       =					None

      a						: Union[str, Any]					       =					W_supports['input_ids'] == start_token_id
      a						: str					       =					W_supports['input_ids'] == end_token_id

      for i, size in enumerate(__snake_case     ):
        if i == 0:
          a						: Optional[int]					       =					0
        else:
          a						: Tuple					       =					support_sizes[i - 1]

        a						: Tuple					       =					S[s : s + size][start_token_masks[s : s + size]]
        a						: int					       =					S[s : s + size][end_token_masks[s : s + size]]

        a						: Any					       =					torch.matmul(q[i]						,      s_start.T     ).sum(1     ).softmax(0     )
        a						: List[str]					       =					torch.matmul(q[i]						,      s_end.T     ).sum(1     ).softmax(0     )

        if p_starts is not None:
          a						: List[Any]					       =					torch.vstack((p_starts, p_start)     )
          a						: List[Any]					       =					torch.vstack((p_ends, p_end)     )
        else:
          a						: List[Any]					       =					p_start
          a						: Optional[int]					       =					p_end

      return p_starts, p_ends