


'''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 __lowerCamelCase			:
    """simple docstring"""


    def __init__(  self      :							Tuple				,							SCREAMING_SNAKE_CASE      :							Tuple				,							SCREAMING_SNAKE_CASE      :							Any=13				,							SCREAMING_SNAKE_CASE      :							Optional[int]=2				,							SCREAMING_SNAKE_CASE      :							List[str]=24				,							SCREAMING_SNAKE_CASE      :							Union[str, Any]=16				,							SCREAMING_SNAKE_CASE      :							Tuple=True				,							SCREAMING_SNAKE_CASE      :							Union[str, Any]=True				,							SCREAMING_SNAKE_CASE      :							Dict=32				,							SCREAMING_SNAKE_CASE      :							List[Any]=5				,							SCREAMING_SNAKE_CASE      :							List[str]=4				,							SCREAMING_SNAKE_CASE      :							Optional[int]=37				,							SCREAMING_SNAKE_CASE      :							Any="gelu"				,							SCREAMING_SNAKE_CASE      :							Optional[int]=0.1				,							SCREAMING_SNAKE_CASE      :							Optional[int]=0.1				,							SCREAMING_SNAKE_CASE      :							Optional[Any]=10				,							SCREAMING_SNAKE_CASE      :							Any=0.02				,							SCREAMING_SNAKE_CASE      :							int=None				,							SCREAMING_SNAKE_CASE      :							List[str]=2				,							SCREAMING_SNAKE_CASE      :							Union[str, Any]=2				,							):
           _A					:     Any     = parent
           _A					:     str     = batch_size
           _A					:     List[str]     = patch_size
           _A					:     Union[str, Any]     = max_length
           _A					:     Union[str, Any]     = num_mel_bins
           _A					:     Optional[Any]     = is_training
           _A					:     Any     = use_labels
           _A					:     Optional[Any]     = hidden_size
           _A					:     List[str]     = num_hidden_layers
           _A					:     List[str]     = num_attention_heads
           _A					:     Union[str, Any]     = intermediate_size
           _A					:     str     = hidden_act
           _A					:     Union[str, Any]     = hidden_dropout_prob
           _A					:     str     = attention_probs_dropout_prob
           _A					:     Optional[Any]     = type_sequence_label_size
           _A					:     List[Any]     = initializer_range
           _A					:     int     = scope
           _A					:     Union[str, Any]     = 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					:     List[str]     = (self.num_mel_bins - self.patch_size) // self.frequency_stride + 1
           _A					:     List[Any]     = (self.max_length - self.patch_size) // self.time_stride + 1
           _A					:     Optional[int]     = frequency_out_dimension * time_out_dimension
           _A					:     List[str]     = num_patches + 2


    def        A   (  self      :							str):
           _A					:     str     = floats_tensor([self.batch_size, self.max_length, self.num_mel_bins])

           _A					:     int     = None
           if self.use_labels:
                  _A					:     Tuple     = ids_tensor([self.batch_size]				,							self.type_sequence_label_size)

           _A					:     str     = self.get_config()

           return config, input_values, labels


    def        A   (  self      :							Tuple):
           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=SCREAMING_SNAKE_CASE				,							initializer_range=self.initializer_range				,							frequency_stride=self.frequency_stride				,							time_stride=self.time_stride				,							)


    def        A   (  self      :							Optional[int]				,							SCREAMING_SNAKE_CASE      :							Any				,							SCREAMING_SNAKE_CASE      :							Dict				,							SCREAMING_SNAKE_CASE      :							Optional[Any]):
           _A					:     int     = ASTModel(config=SCREAMING_SNAKE_CASE)
           model.to(SCREAMING_SNAKE_CASE)
           model.eval()
           _A					:     Dict     = model(SCREAMING_SNAKE_CASE)
           self.parent.assertEqual(result.last_hidden_state.shape				,							(self.batch_size, self.seq_length, self.hidden_size))


    def        A   (  self      :							Tuple):
           _A					:     Any     = self.prepare_config_and_inputs()
           (
               _A			
           )					:     Optional[Any]     = config_and_inputs
           _A					:     Dict     = {'input_values': input_values}
           return config, inputs_dict




@require_torch
class __lowerCamelCase			(	a_    , a_    , unittest.TestCase	):
    """simple docstring"""
    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        A   (  self      :							Optional[int]				,							SCREAMING_SNAKE_CASE      :							str				,							SCREAMING_SNAKE_CASE      :							Any				,							SCREAMING_SNAKE_CASE      :							int				,							SCREAMING_SNAKE_CASE      :							Any				,							SCREAMING_SNAKE_CASE      :							Dict):
           if pipeline_test_casse_name == "AudioClassificationPipelineTests":
                  return True

           return False


    def        A   (  self      :							Tuple):
           _A					:     Optional[Any]     = ASTModelTester(self)
           _A					:     List[str]     = ConfigTester(self				,							config_class=SCREAMING_SNAKE_CASE				,							has_text_modality=SCREAMING_SNAKE_CASE				,							hidden_size=37)


    def        A   (  self      :							List[str]):
           self.config_tester.run_common_tests()


    @unittest.skip(reason='AST does not use inputs_embeds')
    def        A   (  self      :							List[str]):
           pass


    def        A   (  self      :							Optional[Any]):
           _A					:     List[Any]     = self.model_tester.prepare_config_and_inputs_for_common()

           for model_class in self.all_model_classes:
                  _A					:     Any     = model_class(SCREAMING_SNAKE_CASE)
                  self.assertIsInstance(model.get_input_embeddings()				,							(nn.Module))
                  _A					:     Optional[Any]     = model.get_output_embeddings()
                  self.assertTrue(x is None or isinstance(SCREAMING_SNAKE_CASE				,							nn.Linear))


    def        A   (  self      :							Optional[Any]):
           _A					:     Tuple     = self.model_tester.prepare_config_and_inputs_for_common()

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

                  _A					:     Any     = ['input_values']
                  self.assertListEqual(arg_names[:1]				,							SCREAMING_SNAKE_CASE)


    def        A   (  self      :							str):
           _A					:     Union[str, Any]     = self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_model(*SCREAMING_SNAKE_CASE)


    @slow
    def        A   (  self      :							Tuple):
           for model_name in AUDIO_SPECTROGRAM_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                  _A					:     Dict     = ASTModel.from_pretrained(SCREAMING_SNAKE_CASE)
                  self.assertIsNotNone(SCREAMING_SNAKE_CASE)






def 				lowerCAmelCase__	(    ):
       _A					:     List[str]     = hf_hub_download(
           repo_id='nielsr/audio-spectogram-transformer-checkpoint'  ,filename='sample_audio.flac'  ,repo_type='dataset'       )

       _A					:     Dict     = torchaudio.load(lowerCamelCase       )

       return audio, sampling_rate


@require_torch
@require_torchaudio
class __lowerCamelCase			(	unittest.TestCase	):
    """simple docstring"""


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


    @slow
    def        A   (  self      :							Tuple):
           _A					:     Optional[Any]     = self.default_feature_extractor
           _A					:     Tuple     = ASTForAudioClassification.from_pretrained('MIT/ast-finetuned-audioset-10-10-0.4593').to(SCREAMING_SNAKE_CASE)

           _A					:     List[Any]     = self.default_feature_extractor
           _A					:     Dict     = prepare_audio()
           _A					:     int     = audio.squeeze().numpy()
           _A					:     str     = feature_extractor(SCREAMING_SNAKE_CASE				,							sampling_rate=SCREAMING_SNAKE_CASE				,							return_tensors='pt').to(SCREAMING_SNAKE_CASE)

           # forward pass
           with torch.no_grad():
                  _A					:     Tuple     = model(**SCREAMING_SNAKE_CASE)

           # verify the logits
           _A					:     Optional[Any]     = torch.Size((1, 527))
           self.assertEqual(outputs.logits.shape				,							SCREAMING_SNAKE_CASE)

           _A					:     Union[str, Any]     = torch.tensor([-0.8760, -7.0042, -8.6602]).to(SCREAMING_SNAKE_CASE)

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










'''simple docstring'''







# DISCLAIMER: This code is strongly influenced by https://github.com/pesser/pytorch_diffusion
# and https://github.com/hojonathanho/diffusion
import math
from dataclasses import dataclass
from typing import List, Optional, Tuple, Union

import numpy as np
import torch

from diffusers.configuration_utils import ConfigMixin, register_to_config
from diffusers.schedulers.scheduling_utils import SchedulerMixin
from diffusers.utils import BaseOutput, deprecate


@dataclass
# Copied from diffusers.schedulers.scheduling_ddpm.DDPMSchedulerOutput with DDPM->DDIM
class 			__lowerCamelCase			(	a_	):
    """simple docstring"""
    a													=   42
    a													=   None






def 				lowerCAmelCase__	(    lowerCamelCase					:      int  ,lowerCamelCase					:      Tuple=0.999  ,lowerCamelCase					:      int="cosine"  ,):
       if alpha_transform_type == "cosine":

              def alpha_bar_fn(lowerCamelCase					:      str       ):
                     return math.cos((t + 0.008) / 1.008 * math.pi / 2       ) ** 2

       elif alpha_transform_type == "exp":

              def alpha_bar_fn(lowerCamelCase					:      Dict       ):
                     return math.exp(t * -12.0       )

       else:
              raise ValueError(F'Unsupported alpha_tranform_type: {alpha_transform_type}'       )

       _A					:     Tuple     = []
       for i in range(lowerCamelCase       ):
              _A					:     Optional[Any]     = i / num_diffusion_timesteps
              _A					:     Tuple     = (i + 1) / num_diffusion_timesteps
              betas.append(min(1 - alpha_bar_fn(lowerCamelCase       ) / alpha_bar_fn(lowerCamelCase       )  ,lowerCamelCase       )       )
       return torch.tensor(lowerCamelCase  ,dtype=torch.floataa       )


class 			__lowerCamelCase			(	a_    , a_	):
    """simple docstring"""
    a													=   1


    @register_to_config
    def __init__(  self      :							Tuple				,							SCREAMING_SNAKE_CASE      :							int = 1000				,							SCREAMING_SNAKE_CASE      :							float = 0.0001				,							SCREAMING_SNAKE_CASE      :							float = 0.02				,							SCREAMING_SNAKE_CASE      :							str = "linear"				,							SCREAMING_SNAKE_CASE      :							Optional[Union[np.ndarray, List[float]]] = None				,							SCREAMING_SNAKE_CASE      :							bool = True				,							SCREAMING_SNAKE_CASE      :							bool = True				,							SCREAMING_SNAKE_CASE      :							int = 0				,							SCREAMING_SNAKE_CASE      :							str = "epsilon"				,							SCREAMING_SNAKE_CASE      :							float = 1.0				,							**SCREAMING_SNAKE_CASE      :							List[str]				,							):
           if kwargs.get('set_alpha_to_one'				,							SCREAMING_SNAKE_CASE) is not None:
                  _A					:     Tuple     = (
                      'The `set_alpha_to_one` argument is deprecated. Please use `set_alpha_to_zero` instead.'
                  )
                  deprecate('set_alpha_to_one'				,							'1.0.0'				,							SCREAMING_SNAKE_CASE				,							standard_warn=SCREAMING_SNAKE_CASE)
                  _A					:     Tuple     = kwargs['set_alpha_to_one']
           if trained_betas is not None:
                  _A					:     Any     = torch.tensor(SCREAMING_SNAKE_CASE				,							dtype=torch.floataa)
           elif beta_schedule == "linear":
                  _A					:     List[Any]     = torch.linspace(SCREAMING_SNAKE_CASE				,							SCREAMING_SNAKE_CASE				,							SCREAMING_SNAKE_CASE				,							dtype=torch.floataa)
           elif beta_schedule == "scaled_linear":
                  # this schedule is very specific to the latent diffusion model.
                  _A					:     List[str]     = (
                      torch.linspace(beta_start**0.5				,							beta_end**0.5				,							SCREAMING_SNAKE_CASE				,							dtype=torch.floataa) ** 2
                  )
           elif beta_schedule == "squaredcos_cap_v2":
                  # Glide cosine schedule
                  _A					:     List[Any]     = betas_for_alpha_bar(SCREAMING_SNAKE_CASE)
           else:
                  raise NotImplementedError(F'{beta_schedule} does is not implemented for {self.__class__}')

           _A					:     Optional[int]     = 1.0 - self.betas
           _A					:     Union[str, Any]     = torch.cumprod(self.alphas				,							dim=0)

           # At every step in inverted ddim, we are looking into the next alphas_cumprod
           # For the final step, there is no next alphas_cumprod, and the index is out of bounds
           # `set_alpha_to_zero` decides whether we set this parameter simply to zero
           # in this case, self.step() just output the predicted noise
           # or whether we use the final alpha of the "non-previous" one.
           _A					:     Optional[int]     = torch.tensor(0.0) if set_alpha_to_zero else self.alphas_cumprod[-1]

           # standard deviation of the initial noise distribution
           _A					:     Union[str, Any]     = 1.0

           # setable values
           _A					:     List[str]     = None
           _A					:     Dict     = torch.from_numpy(np.arange(0				,							SCREAMING_SNAKE_CASE).copy().astype(np.intaa))


    def        A   (  self      :							str				,							SCREAMING_SNAKE_CASE      :							torch.FloatTensor				,							SCREAMING_SNAKE_CASE      :							Optional[int] = None):
           return sample


    def        A   (  self      :							Dict				,							SCREAMING_SNAKE_CASE      :							int				,							SCREAMING_SNAKE_CASE      :							Union[str, torch.device] = None):

           if num_inference_steps > self.config.num_train_timesteps:
                  raise ValueError(
                      F'`num_inference_steps`: {num_inference_steps} cannot be larger than `self.config.train_timesteps`:'
                      F' {self.config.num_train_timesteps} as the unet model trained with this scheduler can only handle'
                      F' maximal {self.config.num_train_timesteps} timesteps.')

           _A					:     Optional[Any]     = num_inference_steps
           _A					:     List[Any]     = self.config.num_train_timesteps // self.num_inference_steps
           # creates integer timesteps by multiplying by ratio
           # casting to int to avoid issues when num_inference_step is power of 3
           _A					:     List[str]     = (np.arange(0				,							SCREAMING_SNAKE_CASE) * step_ratio).round().copy().astype(np.intaa)
           _A					:     int     = torch.from_numpy(SCREAMING_SNAKE_CASE).to(SCREAMING_SNAKE_CASE)
           self.timesteps += self.config.steps_offset


    def        A   (  self      :							List[Any]				,							SCREAMING_SNAKE_CASE      :							torch.FloatTensor				,							SCREAMING_SNAKE_CASE      :							int				,							SCREAMING_SNAKE_CASE      :							torch.FloatTensor				,							SCREAMING_SNAKE_CASE      :							float = 0.0				,							SCREAMING_SNAKE_CASE      :							bool = False				,							SCREAMING_SNAKE_CASE      :							Optional[torch.FloatTensor] = None				,							SCREAMING_SNAKE_CASE      :							bool = True				,							):
           # 1. get previous step value (=t+1)
           _A					:     Union[str, Any]     = timestep + self.config.num_train_timesteps // self.num_inference_steps

           # 2. compute alphas, betas
           # change original implementation to exactly match noise levels for analogous forward process
           _A					:     List[str]     = self.alphas_cumprod[timestep]
           _A					:     List[str]     = (
               self.alphas_cumprod[prev_timestep]
               if prev_timestep < self.config.num_train_timesteps
               else self.final_alpha_cumprod
           )

           _A					:     List[str]     = 1 - alpha_prod_t

           # 3. compute predicted original sample from predicted noise also called
           # "predicted x_0" of formula (12) from https://arxiv.org/pdf/2010.02502.pdf
           if self.config.prediction_type == "epsilon":
                  _A					:     Any     = (sample - beta_prod_t ** 0.5 * model_output) / alpha_prod_t ** 0.5
                  _A					:     List[Any]     = model_output
           elif self.config.prediction_type == "sample":
                  _A					:     List[Any]     = model_output
                  _A					:     Dict     = (sample - alpha_prod_t ** 0.5 * pred_original_sample) / beta_prod_t ** 0.5
           elif self.config.prediction_type == "v_prediction":
                  _A					:     List[str]     = (alpha_prod_t**0.5) * sample - (beta_prod_t**0.5) * model_output
                  _A					:     Optional[int]     = (alpha_prod_t**0.5) * model_output + (beta_prod_t**0.5) * sample
           else:
                  raise ValueError(
                      F'prediction_type given as {self.config.prediction_type} must be one of `epsilon`, `sample`, or'
                      ' `v_prediction`')

           # 4. Clip or threshold "predicted x_0"
           if self.config.clip_sample:
                  _A					:     str     = pred_original_sample.clamp(
                      -self.config.clip_sample_range				,							self.config.clip_sample_range)

           # 5. compute "direction pointing to x_t" of formula (12) from https://arxiv.org/pdf/2010.02502.pdf
           _A					:     Any     = (1 - alpha_prod_t_prev) ** 0.5 * pred_epsilon

           # 6. compute x_t without "random noise" of formula (12) from https://arxiv.org/pdf/2010.02502.pdf
           _A					:     Tuple     = alpha_prod_t_prev ** 0.5 * pred_original_sample + pred_sample_direction

           if not return_dict:
                  return (prev_sample, pred_original_sample)
           return DDIMSchedulerOutput(prev_sample=SCREAMING_SNAKE_CASE				,							pred_original_sample=SCREAMING_SNAKE_CASE)


    def __len__(  self      :							List[Any]):
           return self.config.num_train_timesteps






