




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





				'''simple docstring'''



				def __init__( self       , UpperCAmelCase_       , UpperCAmelCase_=13       , UpperCAmelCase_=2       , UpperCAmelCase_=24       , UpperCAmelCase_=16       , UpperCAmelCase_=True       , UpperCAmelCase_=True       , UpperCAmelCase_=32       , UpperCAmelCase_=5       , UpperCAmelCase_=4       , UpperCAmelCase_=37       , UpperCAmelCase_="gelu"       , UpperCAmelCase_=0.1       , UpperCAmelCase_=0.1       , UpperCAmelCase_=10       , UpperCAmelCase_=0.02       , UpperCAmelCase_=None       , UpperCAmelCase_=2       , UpperCAmelCase_=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=UpperCAmelCase_       , initializer_range=self.initializer_range       , frequency_stride=self.frequency_stride       , time_stride=self.time_stride       , )



				def       __snake_case		( self       , UpperCAmelCase_       , UpperCAmelCase_       , UpperCAmelCase_						):
											lowerCAmelCase   =		ASTModel(config=UpperCAmelCase_						)
											model.to(UpperCAmelCase_						)
											model.eval()
											lowerCAmelCase   =		model(UpperCAmelCase_						)
											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 							__UpperCamelCase			( __UpperCAmelCase     ,					__UpperCAmelCase     ,					unittest.TestCase  ):





				'''simple docstring'''



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



				def       __snake_case		( self       , UpperCAmelCase_       , UpperCAmelCase_       , UpperCAmelCase_       , UpperCAmelCase_       , UpperCAmelCase_						):
											if pipeline_test_casse_name == "AudioClassificationPipelineTests":
																		return True

											return False



				def       __snake_case		( self						):
											lowerCAmelCase   =		ASTModelTester(self						)
											lowerCAmelCase   =		ConfigTester(self       , config_class=UpperCAmelCase_       , has_text_modality=UpperCAmelCase_       , hidden_size=37						)



				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(UpperCAmelCase_						)
																		self.assertIsInstance(model.get_input_embeddings()       , (nn.Module)						)
																		lowerCAmelCase   =		model.get_output_embeddings()
																		self.assertTrue(x is None or isinstance(UpperCAmelCase_       , 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(UpperCAmelCase_						)
																		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]       , UpperCAmelCase_						)



				def       __snake_case		( self						):
											lowerCAmelCase   =		self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_model(*UpperCAmelCase_						)






				@slow
				def       __snake_case		( self						):
											for model_name in AUDIO_SPECTROGRAM_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																		lowerCAmelCase   =		ASTModel.from_pretrained(UpperCAmelCase_						)
																		self.assertIsNotNone(UpperCAmelCase_						)




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

							lowerCAmelCase   ,     lowerCAmelCase   =		torchaudio.load(_snake_case							)

							return audio, sampling_rate


@require_torch
@require_torchaudio
class 							__UpperCamelCase			( 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(UpperCAmelCase_						)

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

											# forward pass
											with torch.no_grad():
																		lowerCAmelCase   =		model(**UpperCAmelCase_						)

											# verify the logits
											lowerCAmelCase   =		torch.Size((1, 5_27)						)
											self.assertEqual(outputs.logits.shape       , UpperCAmelCase_						)

											lowerCAmelCase   =		torch.tensor([-0.8760, -7.0042, -8.6602]						).to(UpperCAmelCase_						)

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









import random
import unittest

import torch

from diffusers import IFInpaintingSuperResolutionPipeline
from diffusers.utils import floats_tensor
from diffusers.utils.import_utils import is_xformers_available
from diffusers.utils.testing_utils import skip_mps, torch_device

from ..pipeline_params import (
    TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS,
    TEXT_GUIDED_IMAGE_INPAINTING_PARAMS,
)
from ..test_pipelines_common import PipelineTesterMixin
from . import IFPipelineTesterMixin


@skip_mps
class 							__UpperCamelCase			( __UpperCAmelCase     ,					__UpperCAmelCase     ,					unittest.TestCase  ):





				'''simple docstring'''



				__a							: Tuple										=IFInpaintingSuperResolutionPipeline
				__a							: Dict										=TEXT_GUIDED_IMAGE_INPAINTING_PARAMS - {"""width""", """height"""}
				__a							: int										=TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS.union({"""original_image"""}  )
				__a							: Union[str, Any]										=PipelineTesterMixin.required_optional_params - {"""latents"""}



				def       __snake_case		( self						):
											return self._get_superresolution_dummy_components()



				def       __snake_case		( self       , UpperCAmelCase_       , UpperCAmelCase_=0						):
											if str(UpperCAmelCase_						).startswith('''mps'''						):
																		lowerCAmelCase   =		torch.manual_seed(UpperCAmelCase_						)
											else:
																		lowerCAmelCase   =		torch.Generator(device=UpperCAmelCase_						).manual_seed(UpperCAmelCase_						)

											lowerCAmelCase   =		floats_tensor((1, 3, 16, 16)       , rng=random.Random(UpperCAmelCase_						)						).to(UpperCAmelCase_						)
											lowerCAmelCase   =		floats_tensor((1, 3, 32, 32)       , rng=random.Random(UpperCAmelCase_						)						).to(UpperCAmelCase_						)
											lowerCAmelCase   =		floats_tensor((1, 3, 32, 32)       , rng=random.Random(UpperCAmelCase_						)						).to(UpperCAmelCase_						)

											lowerCAmelCase   =		{
											    '''prompt''': '''A painting of a squirrel eating a burger''',
											    '''image''': image,
											    '''original_image''': original_image,
											    '''mask_image''': mask_image,
											    '''generator''': generator,
											    '''num_inference_steps''': 2,
											    '''output_type''': '''numpy''',
											}

											return inputs



				@unittest.skipIf(
				    torch_device != '''cuda''' or not is_xformers_available()       , reason='''XFormers attention is only available with CUDA and `xformers` installed'''       , )
				def       __snake_case		( self						):
											self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1E-3						)



				def       __snake_case		( self						):
											self._test_save_load_optional_components()



				@unittest.skipIf(torch_device != '''cuda'''       , reason='''float16 requires CUDA'''						)
				def       __snake_case		( self						):
											# Due to non-determinism in save load of the hf-internal-testing/tiny-random-t5 text encoder
											super().test_save_load_floataa(expected_max_diff=1E-1						)



				def       __snake_case		( self						):
											self._test_attention_slicing_forward_pass(expected_max_diff=1E-2						)



				def       __snake_case		( self						):
											self._test_save_load_local()






				def       __snake_case		( self						):
											self._test_inference_batch_single_identical(
											    expected_max_diff=1E-2       , )



