




import random
import unittest

import numpy as np

from diffusers import (
    DPMSolverMultistepScheduler,
    EulerAncestralDiscreteScheduler,
    EulerDiscreteScheduler,
    LMSDiscreteScheduler,
    OnnxStableDiffusionImgaImgPipeline,
    PNDMScheduler,
)
from diffusers.utils import floats_tensor
from diffusers.utils.testing_utils import (
    is_onnx_available,
    load_image,
    nightly,
    require_onnxruntime,
    require_torch_gpu,
)

from ..test_pipelines_onnx_common import OnnxPipelineTesterMixin


if is_onnx_available():
  import onnxruntime as ort
class 			_UpperCamelCase      (				_lowercase	,       unittest.TestCase		):
      UpperCAmelCase_        =		'''hf-internal-testing/tiny-random-OnnxStableDiffusionPipeline'''
      def 	UpperCAmelCase_						(			self   :List[Any] ,      lowerCamelCase   :List[str]=0      )     ->			Dict:
       UpperCAmelCase__							      =						floats_tensor((1, 3, 128, 128) ,      rng=random.Random(A_      )      )
       UpperCAmelCase__							      =						np.random.RandomState(A_      )
       UpperCAmelCase__							      =						{
           "prompt": "A painting of a squirrel eating a burger",
           "image": image,
           "generator": generator,
           "num_inference_steps": 3,
           "strength": 0.75,
           "guidance_scale": 7.5,
           "output_type": "numpy",
       }
       return inputs
      def 	UpperCAmelCase_						(			self   :str      )     ->			Optional[int]:
       UpperCAmelCase__							      =						OnnxStableDiffusionImgaImgPipeline.from_pretrained(self.hub_checkpoint ,      provider="CPUExecutionProvider"      )
       pipe.set_progress_bar_config(disable=A_      )

       UpperCAmelCase__							      =						self.get_dummy_inputs()
       UpperCAmelCase__							      =						pipe(**A_      ).images
       UpperCAmelCase__							      =						image[0, -3:, -3:, -1].flatten()

       assert image.shape == (1, 128, 128, 3)
       UpperCAmelCase__							      =						np.array([0.6_96_43, 0.5_84_84, 0.5_03_14, 0.5_87_60, 0.5_53_68, 0.5_96_43, 0.5_15_29, 0.4_12_17, 0.4_90_87]      )
       assert np.abs(image_slice - expected_slice      ).max() < 1e-1
      def 	UpperCAmelCase_						(			self   :str      )     ->			Union[str, Any]:
       UpperCAmelCase__							      =						OnnxStableDiffusionImgaImgPipeline.from_pretrained(self.hub_checkpoint ,      provider="CPUExecutionProvider"      )
       UpperCAmelCase__							      =						PNDMScheduler.from_config(pipe.scheduler.config ,      skip_prk_steps=A_      )
       pipe.set_progress_bar_config(disable=A_      )

       UpperCAmelCase__							      =						self.get_dummy_inputs()
       UpperCAmelCase__							      =						pipe(**A_      ).images
       UpperCAmelCase__							      =						image[0, -3:, -3:, -1]

       assert image.shape == (1, 128, 128, 3)
       UpperCAmelCase__							      =						np.array([0.6_17_37, 0.5_46_42, 0.5_31_83, 0.5_44_65, 0.5_27_42, 0.6_05_25, 0.4_99_69, 0.4_06_55, 0.4_81_54]      )

       assert np.abs(image_slice.flatten() - expected_slice      ).max() < 1e-1
      def 	UpperCAmelCase_						(			self   :Optional[Any]      )     ->			Dict:
       UpperCAmelCase__							      =						OnnxStableDiffusionImgaImgPipeline.from_pretrained(self.hub_checkpoint ,      provider="CPUExecutionProvider"      )
       UpperCAmelCase__							      =						LMSDiscreteScheduler.from_config(pipe.scheduler.config      )
       pipe.set_progress_bar_config(disable=A_      )

       # warmup pass to apply optimizations
       UpperCAmelCase__							      =						pipe(**self.get_dummy_inputs()      )

       UpperCAmelCase__							      =						self.get_dummy_inputs()
       UpperCAmelCase__							      =						pipe(**A_      ).images
       UpperCAmelCase__							      =						image[0, -3:, -3:, -1]

       assert image.shape == (1, 128, 128, 3)
       UpperCAmelCase__							      =						np.array([0.5_27_61, 0.5_99_77, 0.4_90_33, 0.4_96_19, 0.5_42_82, 0.5_03_11, 0.4_76_00, 0.4_09_18, 0.4_52_03]      )

       assert np.abs(image_slice.flatten() - expected_slice      ).max() < 1e-1
      def 	UpperCAmelCase_						(			self   :int      )     ->			Optional[int]:
       UpperCAmelCase__							      =						OnnxStableDiffusionImgaImgPipeline.from_pretrained(self.hub_checkpoint ,      provider="CPUExecutionProvider"      )
       UpperCAmelCase__							      =						EulerDiscreteScheduler.from_config(pipe.scheduler.config      )
       pipe.set_progress_bar_config(disable=A_      )

       UpperCAmelCase__							      =						self.get_dummy_inputs()
       UpperCAmelCase__							      =						pipe(**A_      ).images
       UpperCAmelCase__							      =						image[0, -3:, -3:, -1]

       assert image.shape == (1, 128, 128, 3)
       UpperCAmelCase__							      =						np.array([0.5_29_11, 0.6_00_04, 0.4_92_29, 0.4_98_05, 0.5_45_02, 0.5_06_80, 0.4_77_77, 0.4_10_28, 0.4_53_04]      )

       assert np.abs(image_slice.flatten() - expected_slice      ).max() < 1e-1
      def 	UpperCAmelCase_						(			self   :List[str]      )     ->			Optional[int]:
       UpperCAmelCase__							      =						OnnxStableDiffusionImgaImgPipeline.from_pretrained(self.hub_checkpoint ,      provider="CPUExecutionProvider"      )
       UpperCAmelCase__							      =						EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config      )
       pipe.set_progress_bar_config(disable=A_      )

       UpperCAmelCase__							      =						self.get_dummy_inputs()
       UpperCAmelCase__							      =						pipe(**A_      ).images
       UpperCAmelCase__							      =						image[0, -3:, -3:, -1]

       assert image.shape == (1, 128, 128, 3)
       UpperCAmelCase__							      =						np.array([0.5_29_11, 0.6_00_04, 0.4_92_29, 0.4_98_05, 0.5_45_02, 0.5_06_80, 0.4_77_77, 0.4_10_28, 0.4_53_04]      )

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







      def 	UpperCAmelCase_						(			self   :str      )     ->			Optional[Any]:
       UpperCAmelCase__							      =						OnnxStableDiffusionImgaImgPipeline.from_pretrained(self.hub_checkpoint ,      provider="CPUExecutionProvider"      )
       UpperCAmelCase__							      =						DPMSolverMultistepScheduler.from_config(pipe.scheduler.config      )
       pipe.set_progress_bar_config(disable=A_      )

       UpperCAmelCase__							      =						self.get_dummy_inputs()
       UpperCAmelCase__							      =						pipe(**A_      ).images
       UpperCAmelCase__							      =						image[0, -3:, -3:, -1]

       assert image.shape == (1, 128, 128, 3)
       UpperCAmelCase__							      =						np.array([0.6_53_31, 0.5_82_77, 0.4_82_04, 0.5_60_59, 0.5_36_65, 0.5_62_35, 0.5_09_69, 0.4_00_09, 0.4_65_52]      )

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



@nightly
@require_onnxruntime
@require_torch_gpu
class 			_UpperCamelCase      (				unittest.TestCase		):
      @property
      def 	UpperCAmelCase_						(			self   :Any      )     ->			Union[str, Any]:
       return (
           "CUDAExecutionProvider",
           {
               "gpu_mem_limit": "15000000000",  # 15GB
               "arena_extend_strategy": "kSameAsRequested",
           },
       )
      @property
      def 	UpperCAmelCase_						(			self   :Dict      )     ->			str:
       UpperCAmelCase__							      =						ort.SessionOptions()
       UpperCAmelCase__							      =						False
       return options
      def 	UpperCAmelCase_						(			self   :str      )     ->			Tuple:
       UpperCAmelCase__							      =						load_image(
           "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"
           "/img2img/sketch-mountains-input.jpg"      )
       UpperCAmelCase__							      =						init_image.resize((768, 512)      )
       # using the PNDM scheduler by default
       UpperCAmelCase__							      =						OnnxStableDiffusionImgaImgPipeline.from_pretrained(
           "CompVis/stable-diffusion-v1-4" ,      revision="onnx" ,      safety_checker=A_ ,      feature_extractor=A_ ,      provider=self.gpu_provider ,      sess_options=self.gpu_options ,      )
       pipe.set_progress_bar_config(disable=A_      )

       UpperCAmelCase__							      =						"A fantasy landscape, trending on artstation"

       UpperCAmelCase__							      =						np.random.RandomState(0      )
       UpperCAmelCase__							      =						pipe(
           prompt=A_ ,      image=A_ ,      strength=0.75 ,      guidance_scale=7.5 ,      num_inference_steps=10 ,      generator=A_ ,      output_type="np" ,      )
       UpperCAmelCase__							      =						output.images
       UpperCAmelCase__							      =						images[0, 255:258, 383:386, -1]

       assert images.shape == (1, 512, 768, 3)
       UpperCAmelCase__							      =						np.array([0.49_09, 0.50_59, 0.53_72, 0.46_23, 0.48_76, 0.50_49, 0.48_20, 0.49_56, 0.50_19]      )
       # TODO: lower the tolerance after finding the cause of onnxruntime reproducibility issues

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







      def 	UpperCAmelCase_						(			self   :Optional[Any]      )     ->			Any:
       UpperCAmelCase__							      =						load_image(
           "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"
           "/img2img/sketch-mountains-input.jpg"      )
       UpperCAmelCase__							      =						init_image.resize((768, 512)      )
       UpperCAmelCase__							      =						LMSDiscreteScheduler.from_pretrained(
           "runwayml/stable-diffusion-v1-5" ,      subfolder="scheduler" ,      revision="onnx"      )
       UpperCAmelCase__							      =						OnnxStableDiffusionImgaImgPipeline.from_pretrained(
           "runwayml/stable-diffusion-v1-5" ,      revision="onnx" ,      scheduler=A_ ,      safety_checker=A_ ,      feature_extractor=A_ ,      provider=self.gpu_provider ,      sess_options=self.gpu_options ,      )
       pipe.set_progress_bar_config(disable=A_      )

       UpperCAmelCase__							      =						"A fantasy landscape, trending on artstation"

       UpperCAmelCase__							      =						np.random.RandomState(0      )
       UpperCAmelCase__							      =						pipe(
           prompt=A_ ,      image=A_ ,      strength=0.75 ,      guidance_scale=7.5 ,      num_inference_steps=20 ,      generator=A_ ,      output_type="np" ,      )
       UpperCAmelCase__							      =						output.images
       UpperCAmelCase__							      =						images[0, 255:258, 383:386, -1]

       assert images.shape == (1, 512, 768, 3)
       UpperCAmelCase__							      =						np.array([0.80_43, 0.9_26, 0.95_81, 0.81_19, 0.89_54, 0.9_13, 0.72_09, 0.74_63, 0.74_31]      )
       # TODO: lower the tolerance after finding the cause of onnxruntime reproducibility issues

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











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

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

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

       UpperCAmelCase__							 =						self.get_config()

       return config, input_values, labels
      def 	UpperCAmelCase_						(			self   :List[Any]      )     ->			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=lowerCamelCase ,      initializer_range=self.initializer_range ,      frequency_stride=self.frequency_stride ,      time_stride=self.time_stride ,      )
      def 	UpperCAmelCase_						(			self   :List[str] ,      lowerCamelCase   :List[str] ,      lowerCamelCase   :List[str] ,      lowerCamelCase   :List[str]      )     ->			Optional[Any]:
       UpperCAmelCase__							 =						ASTModel(config=lowerCamelCase      )
       model.to(lowerCamelCase      )
       model.eval()
       UpperCAmelCase__							 =						model(lowerCamelCase      )
       self.parent.assertEqual(result.last_hidden_state.shape ,      (self.batch_size, self.seq_length, self.hidden_size)      )







      def 	UpperCAmelCase_						(			self   :List[Any]      )     ->			str:
       UpperCAmelCase__							 =						self.prepare_config_and_inputs()
       (
           (
           UpperCAmelCase__
       ) , (
           UpperCAmelCase__
       ) , (
           UpperCAmelCase__
       ) , 
       )							 =						config_and_inputs
       UpperCAmelCase__							 =						{"input_values": input_values}
       return config, inputs_dict



@require_torch
class 			_UpperCamelCase      (				lowerCAmelCase	,       lowerCAmelCase	,       unittest.TestCase		):
      UpperCAmelCase_        =		(
          (
              ASTModel,
              ASTForAudioClassification,
          )
          if is_torch_available()
          else ()
      )
      UpperCAmelCase_        =		(
          {"""audio-classification""": ASTForAudioClassification, """feature-extraction""": ASTModel}
          if is_torch_available()
          else {}
      )
      UpperCAmelCase_        =		False
      UpperCAmelCase_        =		False
      UpperCAmelCase_        =		False
      UpperCAmelCase_        =		False
      def 	UpperCAmelCase_						(			self   :List[str] ,      lowerCamelCase   :Optional[int] ,      lowerCamelCase   :List[Any] ,      lowerCamelCase   :str ,      lowerCamelCase   :List[Any] ,      lowerCamelCase   :int      )     ->			str:
       if pipeline_test_casse_name == "AudioClassificationPipelineTests":
        return True

       return False
      def 	UpperCAmelCase_						(			self   :List[str]      )     ->			int:
       UpperCAmelCase__							 =						ASTModelTester(self      )
       UpperCAmelCase__							 =						ConfigTester(self ,      config_class=lowerCamelCase ,      has_text_modality=lowerCamelCase ,      hidden_size=37      )
      def 	UpperCAmelCase_						(			self   :Tuple      )     ->			str:
       self.config_tester.run_common_tests()
      @unittest.skip(reason="AST does not use inputs_embeds"      )
      def 	UpperCAmelCase_						(			self   :List[str]      )     ->			Optional[Any]:
       pass
      def 	UpperCAmelCase_						(			self   :Optional[int]      )     ->			Any:
       UpperCAmelCase__ , UpperCAmelCase__							 =						self.model_tester.prepare_config_and_inputs_for_common()

       for model_class in self.all_model_classes:
        UpperCAmelCase__							 =						model_class(lowerCamelCase      )
        self.assertIsInstance(model.get_input_embeddings() ,      (nn.Module)      )
        UpperCAmelCase__							 =						model.get_output_embeddings()
        self.assertTrue(x is None or isinstance(lowerCamelCase ,      nn.Linear      )      )
      def 	UpperCAmelCase_						(			self   :Tuple      )     ->			List[str]:
       UpperCAmelCase__ , UpperCAmelCase__							 =						self.model_tester.prepare_config_and_inputs_for_common()

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

        UpperCAmelCase__							 =						["input_values"]
        self.assertListEqual(arg_names[:1] ,      lowerCamelCase      )
      def 	UpperCAmelCase_						(			self   :int      )     ->			Any:
       UpperCAmelCase__							 =						self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*lowerCamelCase      )







      @slow
      def 	UpperCAmelCase_						(			self   :int      )     ->			Optional[Any]:
       for model_name in AUDIO_SPECTROGRAM_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
        UpperCAmelCase__							 =						ASTModel.from_pretrained(lowerCamelCase      )
        self.assertIsNotNone(lowerCamelCase      )



def 						lowerCAmelCase		(		):




 """simple docstring"""







 UpperCAmelCase__							 =						hf_hub_download(
     repo_id="nielsr/audio-spectogram-transformer-checkpoint"						,							filename="sample_audio.flac"						,							repo_type="dataset"	)

 UpperCAmelCase__ , UpperCAmelCase__							 =						torchaudio.load(_lowerCAmelCase	)

 return audio, sampling_rate


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







      @slow
      def 	UpperCAmelCase_						(			self   :str      )     ->			Optional[int]:
       UpperCAmelCase__							 =						self.default_feature_extractor
       UpperCAmelCase__							 =						ASTForAudioClassification.from_pretrained("MIT/ast-finetuned-audioset-10-10-0.4593"      ).to(lowerCamelCase      )

       UpperCAmelCase__							 =						self.default_feature_extractor
       UpperCAmelCase__ , UpperCAmelCase__							 =						prepare_audio()
       UpperCAmelCase__							 =						audio.squeeze().numpy()
       UpperCAmelCase__							 =						feature_extractor(lowerCamelCase ,      sampling_rate=lowerCamelCase ,      return_tensors="pt"      ).to(lowerCamelCase      )

       # forward pass
       with torch.no_grad():
        UpperCAmelCase__							 =						model(**lowerCamelCase      )

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

       UpperCAmelCase__							 =						torch.tensor([-0.87_60, -7.00_42, -8.66_02]      ).to(lowerCamelCase      )

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





