





'''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    _UpperCAmelCase							:
      def __init__(		self      ,       a__      ,       a__=13      ,       a__=2      ,       a__=24      ,       a__=16      ,       a__=True      ,       a__=True      ,       a__=32      ,       a__=5      ,       a__=4      ,       a__=37      ,       a__="gelu"      ,       a__=0.1      ,       a__=0.1      ,       a__=10      ,       a__=0.02      ,       a__=None      ,       a__=2      ,       a__=2      ,       ):
         A_     :				Tuple			      =	parent
         A_     :				Optional[int]			      =	batch_size
         A_     :				Any			      =	patch_size
         A_     :				Optional[Any]			      =	max_length
         A_     :				Optional[Any]			      =	num_mel_bins
         A_     :				List[str]			      =	is_training
         A_     :				Union[str, Any]			      =	use_labels
         A_     :				Tuple			      =	hidden_size
         A_     :				Union[str, Any]			      =	num_hidden_layers
         A_     :				Union[str, Any]			      =	num_attention_heads
         A_     :				Tuple			      =	intermediate_size
         A_     :				Optional[int]			      =	hidden_act
         A_     :				List[Any]			      =	hidden_dropout_prob
         A_     :				int			      =	attention_probs_dropout_prob
         A_     :				Union[str, Any]			      =	type_sequence_label_size
         A_     :				str			      =	initializer_range
         A_     :				int			      =	scope
         A_     :				List[Any]			      =	frequency_stride
         A_     :				str			      =	time_stride

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

         A_     :				Tuple			      =	None
         if self.use_labels:
            A_     :				Optional[Any]			      =	ids_tensor([self.batch_size]      ,       self.type_sequence_label_size       )

         A_     :				Any			      =	self.get_config()

         return config, input_values, labels
      def 		_lowerCamelCase      (		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=A_      ,       initializer_range=self.initializer_range      ,       frequency_stride=self.frequency_stride      ,       time_stride=self.time_stride      ,       )
      def 		_lowerCamelCase      (		self      ,       a__      ,       a__      ,       a__       ):
         A_     :				int			      =	ASTModel(config=A_       )
         model.to(A_       )
         model.eval()
         A_     :				Any			      =	model(A_       )
         self.parent.assertEqual(result.last_hidden_state.shape      ,       (self.batch_size, self.seq_length, self.hidden_size)       )






      def 		_lowerCamelCase      (		self       ):
         A_     :				List[Any]			      =	self.prepare_config_and_inputs()
         (
             (
             A_	
         )      ,				(
             A_	
         )      ,				(
             A_	
         )      ,					
         )     :				Any			      =	config_and_inputs
         A_     :				List[str]			      =	{"""input_values""": input_values}
         return config, inputs_dict






@require_torch
class    _UpperCAmelCase							(		_UpperCAmelCase			,       _UpperCAmelCase			,       unittest.TestCase     ):
      a						       =							(
          (
              ASTModel,
              ASTForAudioClassification,
          )
          if is_torch_available()
          else ()
      )
      a						       =							(
          {'''audio-classification''': ASTForAudioClassification, '''feature-extraction''': ASTModel}
          if is_torch_available()
          else {}
      )
      a						       =							False
      a						       =							False
      a						       =							False
      a						       =							False
      def 		_lowerCamelCase      (		self      ,       a__      ,       a__      ,       a__      ,       a__      ,       a__       ):
         if pipeline_test_casse_name == "AudioClassificationPipelineTests":
            return True

         return False
      def 		_lowerCamelCase      (		self       ):
         A_     :				Dict			      =	ASTModelTester(self       )
         A_     :				List[str]			      =	ConfigTester(self      ,       config_class=A_      ,       has_text_modality=A_      ,       hidden_size=37       )
      def 		_lowerCamelCase      (		self       ):
         self.config_tester.run_common_tests()
      @unittest.skip(reason="""AST does not use inputs_embeds"""       )
      def 		_lowerCamelCase      (		self       ):
         pass
      def 		_lowerCamelCase      (		self       ):
         A_      ,				A_     :				str			      =	self.model_tester.prepare_config_and_inputs_for_common()

         for model_class in self.all_model_classes:
            A_     :				List[Any]			      =	model_class(A_       )
            self.assertIsInstance(model.get_input_embeddings()      ,       (nn.Module)       )
            A_     :				Union[str, Any]			      =	model.get_output_embeddings()
            self.assertTrue(x is None or isinstance(A_      ,       nn.Linear       )       )
      def 		_lowerCamelCase      (		self       ):
         A_      ,				A_     :				Any			      =	self.model_tester.prepare_config_and_inputs_for_common()

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

            A_     :				List[Any]			      =	["""input_values"""]
            self.assertListEqual(arg_names[:1]      ,       A_       )
      def 		_lowerCamelCase      (		self       ):
         A_     :				str			      =	self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_model(*A_       )






      @slow
      def 		_lowerCamelCase      (		self       ):
         for model_name in AUDIO_SPECTROGRAM_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
            A_     :				Union[str, Any]			      =	ASTModel.from_pretrained(A_       )
            self.assertIsNotNone(A_       )






def     _lowerCAmelCase		(  ):
   '''simple docstring'''


   A_     :				Optional[int]			      =	hf_hub_download(
       repo_id="""nielsr/audio-spectogram-transformer-checkpoint"""  ,filename="""sample_audio.flac"""  ,repo_type="""dataset"""     )

   A_      ,				A_     :				List[str]			      =	torchaudio.load(_lowerCAmelCase     )

   return audio, sampling_rate







@require_torch
@require_torchaudio
class    _UpperCAmelCase							(		unittest.TestCase     ):
      @cached_property
      def 		_lowerCamelCase      (		self       ):
         return (
             ASTFeatureExtractor.from_pretrained("""MIT/ast-finetuned-audioset-10-10-0.4593"""       )
             if is_torchaudio_available()
             else None
         )






      @slow
      def 		_lowerCamelCase      (		self       ):
         A_     :				int			      =	self.default_feature_extractor
         A_     :				Any			      =	ASTForAudioClassification.from_pretrained("""MIT/ast-finetuned-audioset-10-10-0.4593"""       ).to(A_       )

         A_     :				Any			      =	self.default_feature_extractor
         A_      ,				A_     :				str			      =	prepare_audio()
         A_     :				Union[str, Any]			      =	audio.squeeze().numpy()
         A_     :				Any			      =	feature_extractor(A_      ,       sampling_rate=A_      ,       return_tensors="""pt"""       ).to(A_       )

         # forward pass
         with torch.no_grad():
            A_     :				Union[str, Any]			      =	model(**A_       )

         # verify the logits
         A_     :				List[str]			      =	torch.Size((1, 527)       )
         self.assertEqual(outputs.logits.shape      ,       A_       )

         A_     :				Tuple			      =	torch.tensor([-0.8760, -7.0042, -8.6602]       ).to(A_       )

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













import gc
import unittest

import numpy as np
import torch
from transformers import CLIPTextConfig, CLIPTextModel, XLMRobertaTokenizer

from diffusers import AltDiffusionPipeline, AutoencoderKL, DDIMScheduler, PNDMScheduler, UNetaDConditionModel
from diffusers.pipelines.alt_diffusion.modeling_roberta_series import (
    RobertaSeriesConfig,
    RobertaSeriesModelWithTransformation,
)
from diffusers.utils import slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu

from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_IMAGE_PARAMS, TEXT_TO_IMAGE_PARAMS
from ..test_pipelines_common import PipelineKarrasSchedulerTesterMixin, PipelineLatentTesterMixin, PipelineTesterMixin


enable_full_determinism()

class    _UpperCAmelCase							(		_lowerCamelCase			,       _lowerCamelCase			,       _lowerCamelCase			,       unittest.TestCase     ):
      a						       =							AltDiffusionPipeline
      a						       =							TEXT_TO_IMAGE_PARAMS
      a						       =							TEXT_TO_IMAGE_BATCH_PARAMS
      a						       =							TEXT_TO_IMAGE_IMAGE_PARAMS
      a						       =							TEXT_TO_IMAGE_IMAGE_PARAMS
      def 		_lowerCamelCase      (		self       ):
         torch.manual_seed(0       )
         A_     :				Optional[int]			      =	UNetaDConditionModel(
             block_out_channels=(32, 64)      ,       layers_per_block=2      ,       sample_size=32      ,       in_channels=4      ,       out_channels=4      ,       down_block_types=("""DownBlock2D""", """CrossAttnDownBlock2D""")      ,       up_block_types=("""CrossAttnUpBlock2D""", """UpBlock2D""")      ,       cross_attention_dim=32      ,       )
         A_     :				List[Any]			      =	DDIMScheduler(
             beta_start=0.0_0085      ,       beta_end=0.012      ,       beta_schedule="""scaled_linear"""      ,       clip_sample=a__      ,       set_alpha_to_one=a__      ,       )
         torch.manual_seed(0       )
         A_     :				int			      =	AutoencoderKL(
             block_out_channels=[32, 64]      ,       in_channels=3      ,       out_channels=3      ,       down_block_types=["""DownEncoderBlock2D""", """DownEncoderBlock2D"""]      ,       up_block_types=["""UpDecoderBlock2D""", """UpDecoderBlock2D"""]      ,       latent_channels=4      ,       )

         # TODO: address the non-deterministic text encoder (fails for save-load tests)
         # torch.manual_seed(0)
         # text_encoder_config = RobertaSeriesConfig(
         #     hidden_size=32,
         #     project_dim=32,
         #     intermediate_size=37,
         #     layer_norm_eps=1e-05,
         #     num_attention_heads=4,
         #     num_hidden_layers=5,
         #     vocab_size=5002,
         # )
         # text_encoder = RobertaSeriesModelWithTransformation(text_encoder_config)

         torch.manual_seed(0       )
         A_     :				Optional[int]			      =	CLIPTextConfig(
             bos_token_id=0      ,       eos_token_id=2      ,       hidden_size=32      ,       projection_dim=32      ,       intermediate_size=37      ,       layer_norm_eps=1E-05      ,       num_attention_heads=4      ,       num_hidden_layers=5      ,       pad_token_id=1      ,       vocab_size=5002      ,       )
         A_     :				Optional[Any]			      =	CLIPTextModel(a__       )

         A_     :				Union[str, Any]			      =	XLMRobertaTokenizer.from_pretrained("""hf-internal-testing/tiny-xlm-roberta"""       )
         A_     :				str			      =	77

         A_     :				Optional[Any]			      =	{
             """unet""": unet,
             """scheduler""": scheduler,
             """vae""": vae,
             """text_encoder""": text_encoder,
             """tokenizer""": tokenizer,
             """safety_checker""": None,
             """feature_extractor""": None,
         }
         return components
      def 		_lowerCamelCase      (		self      ,       a__      ,       a__=0       ):
         if str(a__       ).startswith("""mps"""       ):
            A_     :				Union[str, Any]			      =	torch.manual_seed(a__       )
         else:
            A_     :				Dict			      =	torch.Generator(device=a__       ).manual_seed(a__       )
         A_     :				Dict			      =	{
             """prompt""": """A painting of a squirrel eating a burger""",
             """generator""": generator,
             """num_inference_steps""": 2,
             """guidance_scale""": 6.0,
             """output_type""": """numpy""",
         }
         return inputs
      def 		_lowerCamelCase      (		self       ):
         super().test_attention_slicing_forward_pass(expected_max_diff=3E-3       )
      def 		_lowerCamelCase      (		self       ):
         super().test_inference_batch_single_identical(expected_max_diff=3E-3       )
      def 		_lowerCamelCase      (		self       ):
         A_     :				Optional[Any]			      =	"""cpu"""  # ensure determinism for the device-dependent torch.Generator

         A_     :				Union[str, Any]			      =	self.get_dummy_components()
         torch.manual_seed(0       )
         A_     :				List[str]			      =	RobertaSeriesConfig(
             hidden_size=32      ,       project_dim=32      ,       intermediate_size=37      ,       layer_norm_eps=1E-05      ,       num_attention_heads=4      ,       num_hidden_layers=5      ,       vocab_size=5002      ,       )
         # TODO: remove after fixing the non-deterministic text encoder
         A_     :				Dict			      =	RobertaSeriesModelWithTransformation(a__       )
         A_     :				Any			      =	text_encoder

         A_     :				Tuple			      =	AltDiffusionPipeline(**a__       )
         A_     :				int			      =	alt_pipe.to(a__       )
         alt_pipe.set_progress_bar_config(disable=a__       )

         A_     :				Union[str, Any]			      =	self.get_dummy_inputs(a__       )
         A_     :				Tuple			      =	"""A photo of an astronaut"""
         A_     :				Dict			      =	alt_pipe(**a__       )
         A_     :				List[str]			      =	output.images
         A_     :				List[Any]			      =	image[0, -3:, -3:, -1]

         assert image.shape == (1, 64, 64, 3)
         A_     :				Tuple			      =	np.array(
             [0.574_8162, 0.6044_7145, 0.4882_1217, 0.5010_0636, 0.543_1185, 0.4576_3683, 0.4965_7696, 0.4813_2733, 0.4757_3093]       )

         assert np.abs(image_slice.flatten() - expected_slice       ).max() < 1E-2






      def 		_lowerCamelCase      (		self       ):
         A_     :				Tuple			      =	"""cpu"""  # ensure determinism for the device-dependent torch.Generator

         A_     :				Union[str, Any]			      =	self.get_dummy_components()
         A_     :				str			      =	PNDMScheduler(skip_prk_steps=a__       )
         torch.manual_seed(0       )
         A_     :				Any			      =	RobertaSeriesConfig(
             hidden_size=32      ,       project_dim=32      ,       intermediate_size=37      ,       layer_norm_eps=1E-05      ,       num_attention_heads=4      ,       num_hidden_layers=5      ,       vocab_size=5002      ,       )
         # TODO: remove after fixing the non-deterministic text encoder
         A_     :				int			      =	RobertaSeriesModelWithTransformation(a__       )
         A_     :				Any			      =	text_encoder
         A_     :				Any			      =	AltDiffusionPipeline(**a__       )
         A_     :				int			      =	alt_pipe.to(a__       )
         alt_pipe.set_progress_bar_config(disable=a__       )

         A_     :				Union[str, Any]			      =	self.get_dummy_inputs(a__       )
         A_     :				Union[str, Any]			      =	alt_pipe(**a__       )
         A_     :				List[str]			      =	output.images
         A_     :				List[Any]			      =	image[0, -3:, -3:, -1]

         assert image.shape == (1, 64, 64, 3)
         A_     :				List[Any]			      =	np.array(
             [0.5160_5093, 0.570_7241, 0.4736_5507, 0.5057_8886, 0.563_3877, 0.464_2503, 0.518_2081, 0.4876_3484, 0.4908_4237]       )

         assert np.abs(image_slice.flatten() - expected_slice       ).max() < 1E-2






@slow
@require_torch_gpu
class    _UpperCAmelCase							(		unittest.TestCase     ):
      def 		_lowerCamelCase      (		self       ):
         # clean up the VRAM after each test
         super().tearDown()
         gc.collect()
         torch.cuda.empty_cache()
      def 		_lowerCamelCase      (		self       ):
         # make sure here that pndm scheduler skips prk
         A_     :				Any			      =	AltDiffusionPipeline.from_pretrained("""BAAI/AltDiffusion"""      ,       safety_checker=a__       )
         A_     :				Optional[int]			      =	alt_pipe.to(a__       )
         alt_pipe.set_progress_bar_config(disable=a__       )

         A_     :				Tuple			      =	"""A painting of a squirrel eating a burger"""
         A_     :				Tuple			      =	torch.manual_seed(0       )
         A_     :				int			      =	alt_pipe([prompt]      ,       generator=a__      ,       guidance_scale=6.0      ,       num_inference_steps=20      ,       output_type="""np"""       )

         A_     :				Optional[Any]			      =	output.images

         A_     :				int			      =	image[0, -3:, -3:, -1]

         assert image.shape == (1, 512, 512, 3)
         A_     :				str			      =	np.array([0.1010, 0.0800, 0.0794, 0.0885, 0.0843, 0.0762, 0.0769, 0.0729, 0.0586]       )

         assert np.abs(image_slice.flatten() - expected_slice       ).max() < 1E-2






      def 		_lowerCamelCase      (		self       ):
         A_     :				Dict			      =	DDIMScheduler.from_pretrained("""BAAI/AltDiffusion"""      ,       subfolder="""scheduler"""       )

         A_     :				int			      =	AltDiffusionPipeline.from_pretrained("""BAAI/AltDiffusion"""      ,       scheduler=a__      ,       safety_checker=a__       )
         A_     :				Dict			      =	alt_pipe.to(a__       )
         alt_pipe.set_progress_bar_config(disable=a__       )

         A_     :				Optional[int]			      =	"""A painting of a squirrel eating a burger"""
         A_     :				Any			      =	torch.manual_seed(0       )

         A_     :				List[str]			      =	alt_pipe([prompt]      ,       generator=a__      ,       num_inference_steps=2      ,       output_type="""numpy"""       )
         A_     :				List[Any]			      =	output.images

         A_     :				Tuple			      =	image[0, -3:, -3:, -1]

         assert image.shape == (1, 512, 512, 3)
         A_     :				Any			      =	np.array([0.4019, 0.4052, 0.3810, 0.4119, 0.3916, 0.3982, 0.4651, 0.4195, 0.5323]       )

         assert np.abs(image_slice.flatten() - expected_slice       ).max() < 1E-2






