




"""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__				:


			def __init__(      self      :    Any      ,  UpperCamelCase_      :    int      ,  UpperCamelCase_      :    Any=13      ,  UpperCamelCase_      :    str=2      ,  UpperCamelCase_      :    Optional[Any]=24      ,  UpperCamelCase_      :    str=16      ,  UpperCamelCase_      :    str=True      ,  UpperCamelCase_      :    str=True      ,  UpperCamelCase_      :    List[str]=32      ,  UpperCamelCase_      :    Optional[Any]=5      ,  UpperCamelCase_      :    List[str]=4      ,  UpperCamelCase_      :    Any=37      ,  UpperCamelCase_      :    List[Any]="gelu"      ,  UpperCamelCase_      :    int=0.1      ,  UpperCamelCase_      :    List[Any]=0.1      ,  UpperCamelCase_      :    Tuple=10      ,  UpperCamelCase_      :    str=0.02      ,  UpperCamelCase_      :    Dict=None      ,  UpperCamelCase_      :    str=2      ,  UpperCamelCase_      :    str=2      ,  ):


						"""simple docstring"""




						__UpperCAmelCase	: int		=					parent
						__UpperCAmelCase	: Optional[Any]		=					batch_size
						__UpperCAmelCase	: Union[str, Any]		=					patch_size
						__UpperCAmelCase	: Optional[int]		=					max_length
						__UpperCAmelCase	: Optional[Any]		=					num_mel_bins
						__UpperCAmelCase	: Union[str, Any]		=					is_training
						__UpperCAmelCase	: Tuple		=					use_labels
						__UpperCAmelCase	: Optional[Any]		=					hidden_size
						__UpperCAmelCase	: Union[str, Any]		=					num_hidden_layers
						__UpperCAmelCase	: Any		=					num_attention_heads
						__UpperCAmelCase	: int		=					intermediate_size
						__UpperCAmelCase	: Optional[int]		=					hidden_act
						__UpperCAmelCase	: Optional[Any]		=					hidden_dropout_prob
						__UpperCAmelCase	: List[str]		=					attention_probs_dropout_prob
						__UpperCAmelCase	: List[str]		=					type_sequence_label_size
						__UpperCAmelCase	: Optional[Any]		=					initializer_range
						__UpperCAmelCase	: int		=					scope
						__UpperCAmelCase	: str		=					frequency_stride
						__UpperCAmelCase	: List[str]		=					time_stride

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


			def       a_     (      self      :    str):


						"""simple docstring"""




						__UpperCAmelCase	: Any		=					floats_tensor([self.batch_size, self.max_length, self.num_mel_bins])

						__UpperCAmelCase	: Optional[int]		=					None
						if self.use_labels:
									__UpperCAmelCase	: List[str]		=					ids_tensor([self.batch_size]      ,  self.type_sequence_label_size)

						__UpperCAmelCase	: Any		=					self.get_config()

						return config, input_values, labels


			def       a_     (      self      :    Union[str, Any]):


						"""simple docstring"""




						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       a_     (      self      :    Dict      ,  UpperCamelCase_      :    Optional[int]      ,  UpperCamelCase_      :    Optional[Any]      ,  UpperCamelCase_      :    Optional[int]):


						"""simple docstring"""




						__UpperCAmelCase	: str		=					ASTModel(config=UpperCamelCase_)
						model.to(UpperCamelCase_)
						model.eval()
						__UpperCAmelCase	: List[str]		=					model(UpperCamelCase_)
						self.parent.assertEqual(result.last_hidden_state.shape      ,  (self.batch_size, self.seq_length, self.hidden_size))


			def       a_     (      self      :    Union[str, Any]):


						"""simple docstring"""




						__UpperCAmelCase	: str		=					self.prepare_config_and_inputs()
						(
						    __UpperCAmelCase     
						)	: Any		=					config_and_inputs
						__UpperCAmelCase	: Optional[int]		=					{"input_values": input_values}
						return config, inputs_dict






@require_torch
class      lowerCamelCase__				(	__magic_name__							,    __magic_name__							,    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       a_     (      self      :    Union[str, Any]      ,  UpperCamelCase_      :    Union[str, Any]      ,  UpperCamelCase_      :    Dict      ,  UpperCamelCase_      :    Any      ,  UpperCamelCase_      :    Tuple      ,  UpperCamelCase_      :    Any):


						"""simple docstring"""




						if pipeline_test_casse_name == "AudioClassificationPipelineTests":
									return True

						return False


			def       a_     (      self      :    Optional[int]):


						"""simple docstring"""




						__UpperCAmelCase	: Dict		=					ASTModelTester(self)
						__UpperCAmelCase	: str		=					ConfigTester(self      ,  config_class=UpperCamelCase_      ,  has_text_modality=UpperCamelCase_      ,  hidden_size=37)


			def       a_     (      self      :    Tuple):


						"""simple docstring"""




						self.config_tester.run_common_tests()


			@unittest.skip(reason="AST does not use inputs_embeds")
			def       a_     (      self      :    Any):


						"""simple docstring"""




						pass


			def       a_     (      self      :    int):


						"""simple docstring"""




						__UpperCAmelCase	: List[str]		=					self.model_tester.prepare_config_and_inputs_for_common()

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


			def       a_     (      self      :    int):


						"""simple docstring"""




						__UpperCAmelCase	: Optional[Any]		=					self.model_tester.prepare_config_and_inputs_for_common()

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

									__UpperCAmelCase	: Optional[Any]		=					["input_values"]
									self.assertListEqual(arg_names[:1]      ,  UpperCamelCase_)


			def       a_     (      self      :    Dict):


						"""simple docstring"""




						__UpperCAmelCase	: List[str]		=					self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_model(*UpperCamelCase_)


			@slow
			def       a_     (      self      :    Union[str, Any]):


						"""simple docstring"""




						for model_name in AUDIO_SPECTROGRAM_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
									__UpperCAmelCase	: Optional[Any]		=					ASTModel.from_pretrained(UpperCamelCase_)
									self.assertIsNotNone(UpperCamelCase_)






def 					_UpperCamelCase    (			)			->			Any:


			"""simple docstring"""
			__UpperCAmelCase	: Union[str, Any]		=					hf_hub_download(
			    repo_id="nielsr/audio-spectogram-transformer-checkpoint"				,       filename="sample_audio.flac"				,       repo_type="dataset"							)

			__UpperCAmelCase	: Dict		=					torchaudio.load(UpperCamelCase							)

			return audio, sampling_rate





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


			@cached_property
			def       a_     (      self      :    List[Any]):


						"""simple docstring"""




						return (
						    ASTFeatureExtractor.from_pretrained("MIT/ast-finetuned-audioset-10-10-0.4593")
						    if is_torchaudio_available()
						    else None
						)


			@slow
			def       a_     (      self      :    int):


						"""simple docstring"""




						__UpperCAmelCase	: Tuple		=					self.default_feature_extractor
						__UpperCAmelCase	: str		=					ASTForAudioClassification.from_pretrained("MIT/ast-finetuned-audioset-10-10-0.4593").to(UpperCamelCase_)

						__UpperCAmelCase	: Optional[Any]		=					self.default_feature_extractor
						__UpperCAmelCase	: Optional[int]		=					prepare_audio()
						__UpperCAmelCase	: str		=					audio.squeeze().numpy()
						__UpperCAmelCase	: List[str]		=					feature_extractor(UpperCamelCase_      ,  sampling_rate=UpperCamelCase_      ,  return_tensors="pt").to(UpperCamelCase_)

						# forward pass
						with torch.no_grad():
									__UpperCAmelCase	: Dict		=					model(**UpperCamelCase_)

						# verify the logits
						__UpperCAmelCase	: Tuple		=					torch.Size((1, 527))
						self.assertEqual(outputs.logits.shape      ,  UpperCamelCase_)

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

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








"""simple docstring"""





# DISCLAIMER: This file is strongly influenced by https://github.com/ermongroup/ddim

from dataclasses import dataclass
from typing import Optional, Tuple, Union

import flax
import jax
import jax.numpy as jnp

from ..configuration_utils import ConfigMixin, register_to_config
from .scheduling_utils_flax import (
    CommonSchedulerState,
    FlaxKarrasDiffusionSchedulers,
    FlaxSchedulerMixin,
    FlaxSchedulerOutput,
    add_noise_common,
    get_velocity_common,
)



@flax.struct.dataclass
class      a__				:
			lowercase_    =	42

			# setable values
			lowercase_    =	42
			lowercase_    =	42
			lowercase_    =	None


			@classmethod
			def       a_     (      cls      :    List[str]      ,  UpperCamelCase_      :    CommonSchedulerState      ,  UpperCamelCase_      :    jnp.ndarray      ,  UpperCamelCase_      :    jnp.ndarray):


						"""simple docstring"""




						return cls(common=UpperCamelCase_      ,  init_noise_sigma=UpperCamelCase_      ,  timesteps=UpperCamelCase_)






@dataclass
class      a__				(	__magic_name__   ):
			lowercase_    =	42






class      a__				(	__magic_name__							,    __magic_name__   ):
			lowercase_    =	[e.name for e in FlaxKarrasDiffusionSchedulers]

			lowercase_    =	42


			@property
			def       a_     (      self      :    Optional[int]):


						"""simple docstring"""




						return True


			@register_to_config
			def __init__(      self      :    str      ,  UpperCamelCase_      :    int = 1000      ,  UpperCamelCase_      :    float = 0.0001      ,  UpperCamelCase_      :    float = 0.02      ,  UpperCamelCase_      :    str = "linear"      ,  UpperCamelCase_      :    Optional[jnp.ndarray] = None      ,  UpperCamelCase_      :    str = "fixed_small"      ,  UpperCamelCase_      :    bool = True      ,  UpperCamelCase_      :    str = "epsilon"      ,  UpperCamelCase_      :    jnp.dtype = jnp.floataa      ,  ):


						"""simple docstring"""




						__UpperCAmelCase	: Optional[int]		=					dtype


			def       a_     (      self      :    Optional[Any]      ,  UpperCamelCase_      :    Optional[CommonSchedulerState] = None):


						"""simple docstring"""




						if common is None:
									__UpperCAmelCase	: Tuple		=					CommonSchedulerState.create(self)

						# standard deviation of the initial noise distribution
						__UpperCAmelCase	: Tuple		=					jnp.array(1.0      ,  dtype=self.dtype)

						__UpperCAmelCase	: Any		=					jnp.arange(0      ,  self.config.num_train_timesteps).round()[::-1]

						return DDPMSchedulerState.create(
						    common=UpperCamelCase_      ,  init_noise_sigma=UpperCamelCase_      ,  timesteps=UpperCamelCase_      ,  )


			def       a_     (      self      :    Optional[Any]      ,  UpperCamelCase_      :    DDPMSchedulerState      ,  UpperCamelCase_      :    jnp.ndarray      ,  UpperCamelCase_      :    Optional[int] = None):


						"""simple docstring"""




						return sample


			def       a_     (      self      :    Any      ,  UpperCamelCase_      :    DDPMSchedulerState      ,  UpperCamelCase_      :    int      ,  UpperCamelCase_      :    Tuple = ()):


						"""simple docstring"""




						__UpperCAmelCase	: List[str]		=					self.config.num_train_timesteps // num_inference_steps
						# creates integer timesteps by multiplying by ratio
						# rounding to avoid issues when num_inference_step is power of 3
						__UpperCAmelCase	: List[str]		=					(jnp.arange(0      ,  UpperCamelCase_) * step_ratio).round()[::-1]

						return state.replace(
						    num_inference_steps=UpperCamelCase_      ,  timesteps=UpperCamelCase_      ,  )


			def       a_     (      self      :    Any      ,  UpperCamelCase_      :    DDPMSchedulerState      ,  UpperCamelCase_      :    Optional[Any]      ,  UpperCamelCase_      :    Union[str, Any]=None      ,  UpperCamelCase_      :    Optional[int]=None):


						"""simple docstring"""




						__UpperCAmelCase	: List[str]		=					state.common.alphas_cumprod[t]
						__UpperCAmelCase	: Optional[int]		=					jnp.where(t > 0      ,  state.common.alphas_cumprod[t - 1]      ,  jnp.array(1.0      ,  dtype=self.dtype))

						# For t > 0, compute predicted variance βt (see formula (6) and (7) from https://arxiv.org/pdf/2006.11239.pdf)
						# and sample from it to get previous sample
						# x_{t-1} ~ N(pred_prev_sample, variance) == add variance to pred_sample
						__UpperCAmelCase	: Tuple		=					(1 - alpha_prod_t_prev) / (1 - alpha_prod_t) * state.common.betas[t]

						if variance_type is None:
									__UpperCAmelCase	: Optional[Any]		=					self.config.variance_type

						# hacks - were probably added for training stability
						if variance_type == "fixed_small":
									__UpperCAmelCase	: str		=					jnp.clip(UpperCamelCase_      ,  a_min=1e-20)
						# for rl-diffuser https://arxiv.org/abs/2205.09991
						elif variance_type == "fixed_small_log":
									__UpperCAmelCase	: Optional[int]		=					jnp.log(jnp.clip(UpperCamelCase_      ,  a_min=1e-20))
						elif variance_type == "fixed_large":
									__UpperCAmelCase	: Tuple		=					state.common.betas[t]
						elif variance_type == "fixed_large_log":
									# Glide max_log
									__UpperCAmelCase	: str		=					jnp.log(state.common.betas[t])
						elif variance_type == "learned":
									return predicted_variance
						elif variance_type == "learned_range":
									__UpperCAmelCase	: Any		=					variance
									__UpperCAmelCase	: Union[str, Any]		=					state.common.betas[t]
									__UpperCAmelCase	: List[str]		=					(predicted_variance + 1) / 2
									__UpperCAmelCase	: int		=					frac * max_log + (1 - frac) * min_log

						return variance


			def       a_     (      self      :    Optional[int]      ,  UpperCamelCase_      :    DDPMSchedulerState      ,  UpperCamelCase_      :    jnp.ndarray      ,  UpperCamelCase_      :    int      ,  UpperCamelCase_      :    jnp.ndarray      ,  UpperCamelCase_      :    Optional[jax.random.KeyArray] = None      ,  UpperCamelCase_      :    bool = True      ,  ):


						"""simple docstring"""




						__UpperCAmelCase	: Dict		=					timestep

						if key is None:
									__UpperCAmelCase	: List[str]		=					jax.random.PRNGKey(0)

						if model_output.shape[1] == sample.shape[1] * 2 and self.config.variance_type in ["learned", "learned_range"]:
									__UpperCAmelCase	,     __UpperCAmelCase	: int		=					jnp.split(UpperCamelCase_      ,  sample.shape[1]      ,  axis=1)
						else:
									__UpperCAmelCase	: List[str]		=					None

						# 1. compute alphas, betas
						__UpperCAmelCase	: str		=					state.common.alphas_cumprod[t]
						__UpperCAmelCase	: str		=					jnp.where(t > 0      ,  state.common.alphas_cumprod[t - 1]      ,  jnp.array(1.0      ,  dtype=self.dtype))
						__UpperCAmelCase	: Tuple		=					1 - alpha_prod_t
						__UpperCAmelCase	: int		=					1 - alpha_prod_t_prev

						# 2. compute predicted original sample from predicted noise also called
						# "predicted x_0" of formula (15) from https://arxiv.org/pdf/2006.11239.pdf
						if self.config.prediction_type == "epsilon":
									__UpperCAmelCase	: Any		=					(sample - beta_prod_t ** 0.5 * model_output) / alpha_prod_t ** 0.5
						elif self.config.prediction_type == "sample":
									__UpperCAmelCase	: Optional[int]		=					model_output
						elif self.config.prediction_type == "v_prediction":
									__UpperCAmelCase	: Optional[int]		=					(alpha_prod_t**0.5) * sample - (beta_prod_t**0.5) * model_output
						else:
									raise ValueError(
									    F"prediction_type given as {self.config.prediction_type} must be one of `epsilon`, `sample` "
									    " for the FlaxDDPMScheduler.")

						# 3. Clip "predicted x_0"
						if self.config.clip_sample:
									__UpperCAmelCase	: List[Any]		=					jnp.clip(UpperCamelCase_      ,  -1      ,  1)

						# 4. Compute coefficients for pred_original_sample x_0 and current sample x_t
						# See formula (7) from https://arxiv.org/pdf/2006.11239.pdf
						__UpperCAmelCase	: Union[str, Any]		=					(alpha_prod_t_prev ** 0.5 * state.common.betas[t]) / beta_prod_t
						__UpperCAmelCase	: Union[str, Any]		=					state.common.alphas[t] ** 0.5 * beta_prod_t_prev / beta_prod_t

						# 5. Compute predicted previous sample µ_t
						# See formula (7) from https://arxiv.org/pdf/2006.11239.pdf
						__UpperCAmelCase	: Optional[Any]		=					pred_original_sample_coeff * pred_original_sample + current_sample_coeff * sample

						# 6. Add noise
						def random_variance():
									__UpperCAmelCase	: int		=					jax.random.split(UpperCamelCase_      ,  num=1)
									__UpperCAmelCase	: Any		=					jax.random.normal(UpperCamelCase_      ,  shape=model_output.shape      ,  dtype=self.dtype)
									return (self._get_variance(UpperCamelCase_      ,  UpperCamelCase_      ,  predicted_variance=UpperCamelCase_) ** 0.5) * noise

						__UpperCAmelCase	: Tuple		=					jnp.where(t > 0      ,  random_variance()      ,  jnp.zeros(model_output.shape      ,  dtype=self.dtype))

						__UpperCAmelCase	: Tuple		=					pred_prev_sample + variance

						if not return_dict:
									return (pred_prev_sample, state)

						return FlaxDDPMSchedulerOutput(prev_sample=UpperCamelCase_      ,  state=UpperCamelCase_)


			def       a_     (      self      :    Optional[Any]      ,  UpperCamelCase_      :    DDPMSchedulerState      ,  UpperCamelCase_      :    jnp.ndarray      ,  UpperCamelCase_      :    jnp.ndarray      ,  UpperCamelCase_      :    jnp.ndarray      ,  ):


						"""simple docstring"""




						return add_noise_common(state.common      ,  UpperCamelCase_      ,  UpperCamelCase_      ,  UpperCamelCase_)


			def       a_     (      self      :    Union[str, Any]      ,  UpperCamelCase_      :    DDPMSchedulerState      ,  UpperCamelCase_      :    jnp.ndarray      ,  UpperCamelCase_      :    jnp.ndarray      ,  UpperCamelCase_      :    jnp.ndarray      ,  ):


						"""simple docstring"""




						return get_velocity_common(state.common      ,  UpperCamelCase_      ,  UpperCamelCase_      ,  UpperCamelCase_)


			def __len__(      self      :    int):


						"""simple docstring"""




						return self.config.num_train_timesteps


