
import gc
import unittest

import numpy as np
import torch

from diffusers import (
    AudioDiffusionPipeline,
    AutoencoderKL,
    DDIMScheduler,
    DDPMScheduler,
    DiffusionPipeline,
    Mel,
    UNetaDConditionModel,
    UNetaDModel,
)
from diffusers.utils import slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu


enable_full_determinism()





class       _lowerCAmelCase   (      unittest.TestCase			):




				def 	__a		(   self	)							-> Optional[Any]:
							# clean up the VRAM after each test
							super().tearDown()
							gc.collect()
							torch.cuda.empty_cache()




				@property
				def 	__a		(   self	)							-> Tuple:
							torch.manual_seed(0	)
							lowerCAmelCase_         =      UNetaDModel(
							    sample_size=(32, 64)							,							in_channels=1							,							out_channels=1							,							layers_per_block=2							,							block_out_channels=(128, 128)							,							down_block_types=("AttnDownBlock2D", "DownBlock2D")							,							up_block_types=("UpBlock2D", "AttnUpBlock2D")							,							)
							return model




				@property
				def 	__a		(   self	)							-> Optional[int]:
							torch.manual_seed(0	)
							lowerCAmelCase_         =      UNetaDConditionModel(
							    sample_size=(64, 32)							,							in_channels=1							,							out_channels=1							,							layers_per_block=2							,							block_out_channels=(128, 128)							,							down_block_types=("CrossAttnDownBlock2D", "DownBlock2D")							,							up_block_types=("UpBlock2D", "CrossAttnUpBlock2D")							,							cross_attention_dim=10							,							)
							return model




				@property
				def 	__a		(   self	)							-> Optional[int]:
							torch.manual_seed(0	)
							lowerCAmelCase_         =      AutoencoderKL(
							    sample_size=(128, 64)							,							in_channels=1							,							out_channels=1							,							latent_channels=1							,							layers_per_block=2							,							block_out_channels=(128, 128)							,							down_block_types=("DownEncoderBlock2D", "DownEncoderBlock2D")							,							up_block_types=("UpDecoderBlock2D", "UpDecoderBlock2D")							,							)
							lowerCAmelCase_         =      UNetaDModel(
							    sample_size=(64, 32)							,							in_channels=1							,							out_channels=1							,							layers_per_block=2							,							block_out_channels=(128, 128)							,							down_block_types=("AttnDownBlock2D", "DownBlock2D")							,							up_block_types=("UpBlock2D", "AttnUpBlock2D")							,							)
							return vqvae, unet




				@slow
				def 	__a		(   self	)							-> Any:
							lowerCAmelCase_         =      "cpu"  # ensure determinism for the device-dependent torch.Generator
							lowerCAmelCase_         =      Mel(
							    x_res=self.dummy_unet.config.sample_size[1]							,							y_res=self.dummy_unet.config.sample_size[0]							,							)

							lowerCAmelCase_         =      DDPMScheduler()
							lowerCAmelCase_         =      AudioDiffusionPipeline(vqvae=_UpperCamelCase							,							unet=self.dummy_unet							,							mel=_UpperCamelCase							,							scheduler=_UpperCamelCase	)
							lowerCAmelCase_         =      pipe.to(_UpperCamelCase	)
							pipe.set_progress_bar_config(disable=_UpperCamelCase	)

							lowerCAmelCase_         =      torch.Generator(device=_UpperCamelCase	).manual_seed(42	)
							lowerCAmelCase_         =      pipe(generator=_UpperCamelCase							,							steps=4	)
							lowerCAmelCase_         =      output.audios[0]
							lowerCAmelCase_         =      output.images[0]

							lowerCAmelCase_         =      torch.Generator(device=_UpperCamelCase	).manual_seed(42	)
							lowerCAmelCase_         =      pipe(generator=_UpperCamelCase							,							steps=4							,							return_dict=_UpperCamelCase	)
							lowerCAmelCase_         =      output[0][0]

							assert audio.shape == (1, (self.dummy_unet.config.sample_size[1] - 1) * mel.hop_length)
							assert (
							    image.height == self.dummy_unet.config.sample_size[0]
							    and image.width == self.dummy_unet.config.sample_size[1]
							)
							lowerCAmelCase_         =      np.frombuffer(image.tobytes()							,							dtype="uint8"	)[:10]
							lowerCAmelCase_         =      np.frombuffer(image_from_tuple.tobytes()							,							dtype="uint8"	)[:10]
							lowerCAmelCase_         =      np.array([69, 255, 255, 255, 0, 0, 77, 181, 12, 127]	)

							assert np.abs(image_slice.flatten() - expected_slice	).max() == 0
							assert np.abs(image_from_tuple_slice.flatten() - expected_slice	).max() == 0

							lowerCAmelCase_         =      Mel(
							    x_res=self.dummy_vqvae_and_unet[0].config.sample_size[1]							,							y_res=self.dummy_vqvae_and_unet[0].config.sample_size[0]							,							)

							lowerCAmelCase_         =      DDIMScheduler()
							lowerCAmelCase_         =      self.dummy_vqvae_and_unet
							lowerCAmelCase_         =      AudioDiffusionPipeline(
							    vqvae=self.dummy_vqvae_and_unet[0]							,							unet=dummy_vqvae_and_unet[1]							,							mel=_UpperCamelCase							,							scheduler=_UpperCamelCase	)
							lowerCAmelCase_         =      pipe.to(_UpperCamelCase	)
							pipe.set_progress_bar_config(disable=_UpperCamelCase	)

							np.random.seed(0	)
							lowerCAmelCase_         =      np.random.uniform(-1							,							1							,							((dummy_vqvae_and_unet[0].config.sample_size[1] - 1) * mel.hop_length,)	)
							lowerCAmelCase_         =      torch.Generator(device=_UpperCamelCase	).manual_seed(42	)
							lowerCAmelCase_         =      pipe(raw_audio=_UpperCamelCase							,							generator=_UpperCamelCase							,							start_step=5							,							steps=10	)
							lowerCAmelCase_         =      output.images[0]

							assert (
							    image.height == self.dummy_vqvae_and_unet[0].config.sample_size[0]
							    and image.width == self.dummy_vqvae_and_unet[0].config.sample_size[1]
							)
							lowerCAmelCase_         =      np.frombuffer(image.tobytes()							,							dtype="uint8"	)[:10]
							lowerCAmelCase_         =      np.array([120, 117, 110, 109, 138, 167, 138, 148, 132, 121]	)

							assert np.abs(image_slice.flatten() - expected_slice	).max() == 0

							lowerCAmelCase_         =      self.dummy_unet_condition
							lowerCAmelCase_         =      AudioDiffusionPipeline(
							    vqvae=self.dummy_vqvae_and_unet[0]							,							unet=_UpperCamelCase							,							mel=_UpperCamelCase							,							scheduler=_UpperCamelCase	)
							lowerCAmelCase_         =      pipe.to(_UpperCamelCase	)
							pipe.set_progress_bar_config(disable=_UpperCamelCase	)

							np.random.seed(0	)
							lowerCAmelCase_         =      torch.rand((1, 1, 10)	)
							lowerCAmelCase_         =      pipe(generator=_UpperCamelCase							,							encoding=_UpperCamelCase	)
							lowerCAmelCase_         =      output.images[0]
							lowerCAmelCase_         =      np.frombuffer(image.tobytes()							,							dtype="uint8"	)[:10]
							lowerCAmelCase_         =      np.array([107, 103, 120, 127, 142, 122, 113, 122, 97, 111]	)

							assert np.abs(image_slice.flatten() - expected_slice	).max() == 0





@slow
@require_torch_gpu
class       _lowerCAmelCase   (      unittest.TestCase			):




				def 	__a		(   self	)							-> Union[str, Any]:
							# clean up the VRAM after each test
							super().tearDown()
							gc.collect()
							torch.cuda.empty_cache()




				def 	__a		(   self	)							-> Tuple:
							lowerCAmelCase_         =      torch_device

							lowerCAmelCase_         =      DiffusionPipeline.from_pretrained("teticio/audio-diffusion-ddim-256"	)
							lowerCAmelCase_         =      pipe.to(_UpperCamelCase	)
							pipe.set_progress_bar_config(disable=_UpperCamelCase	)

							lowerCAmelCase_         =      torch.Generator(device=_UpperCamelCase	).manual_seed(42	)
							lowerCAmelCase_         =      pipe(generator=_UpperCamelCase	)
							lowerCAmelCase_         =      output.audios[0]
							lowerCAmelCase_         =      output.images[0]

							assert audio.shape == (1, (pipe.unet.config.sample_size[1] - 1) * pipe.mel.hop_length)
							assert image.height == pipe.unet.config.sample_size[0] and image.width == pipe.unet.config.sample_size[1]
							lowerCAmelCase_         =      np.frombuffer(image.tobytes()							,							dtype="uint8"	)[:10]
							lowerCAmelCase_         =      np.array([151, 167, 154, 144, 122, 134, 121, 105, 70, 26]	)

							assert np.abs(image_slice.flatten() - expected_slice	).max() == 0





from __future__ import annotations

import unittest

from transformers import DebertaVaConfig, is_tf_available
from transformers.testing_utils import require_tf, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
			import tensorflow as tf

			from transformers import (
			    TFDebertaVaForMaskedLM,
			    TFDebertaVaForQuestionAnswering,
			    TFDebertaVaForSequenceClassification,
			    TFDebertaVaForTokenClassification,
			    TFDebertaVaModel,
			)





class       _lowerCAmelCase   :




				def __init__(   self							,							_UpperCamelCase							,							_UpperCamelCase=13							,							_UpperCamelCase=7							,							_UpperCamelCase=True							,							_UpperCamelCase=True							,							_UpperCamelCase=True							,							_UpperCamelCase=True							,							_UpperCamelCase=99							,							_UpperCamelCase=32							,							_UpperCamelCase=2							,							_UpperCamelCase=4							,							_UpperCamelCase=37							,							_UpperCamelCase="gelu"							,							_UpperCamelCase=0.1							,							_UpperCamelCase=0.1							,							_UpperCamelCase=512							,							_UpperCamelCase=16							,							_UpperCamelCase=2							,							_UpperCamelCase=0.02							,							_UpperCamelCase=False							,							_UpperCamelCase=True							,							_UpperCamelCase="None"							,							_UpperCamelCase=3							,							_UpperCamelCase=4							,							_UpperCamelCase=None							,							)							-> Tuple:
							lowerCAmelCase_         =      parent
							lowerCAmelCase_         =      batch_size
							lowerCAmelCase_         =      seq_length
							lowerCAmelCase_         =      is_training
							lowerCAmelCase_         =      use_input_mask
							lowerCAmelCase_         =      use_token_type_ids
							lowerCAmelCase_         =      use_labels
							lowerCAmelCase_         =      vocab_size
							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_         =      max_position_embeddings
							lowerCAmelCase_         =      type_vocab_size
							lowerCAmelCase_         =      type_sequence_label_size
							lowerCAmelCase_         =      initializer_range
							lowerCAmelCase_         =      num_labels
							lowerCAmelCase_         =      num_choices
							lowerCAmelCase_         =      relative_attention
							lowerCAmelCase_         =      position_biased_input
							lowerCAmelCase_         =      pos_att_type
							lowerCAmelCase_         =      scope




				def 	__a		(   self	)							-> List[str]:
							lowerCAmelCase_         =      ids_tensor([self.batch_size, self.seq_length]							,							self.vocab_size	)

							lowerCAmelCase_         =      None
							if self.use_input_mask:
										lowerCAmelCase_         =      random_attention_mask([self.batch_size, self.seq_length]	)

							lowerCAmelCase_         =      None
							if self.use_token_type_ids:
										lowerCAmelCase_         =      ids_tensor([self.batch_size, self.seq_length]							,							self.type_vocab_size	)

							lowerCAmelCase_         =      None
							lowerCAmelCase_         =      None
							lowerCAmelCase_         =      None
							if self.use_labels:
										lowerCAmelCase_         =      ids_tensor([self.batch_size]							,							self.type_sequence_label_size	)
										lowerCAmelCase_         =      ids_tensor([self.batch_size, self.seq_length]							,							self.num_labels	)

							lowerCAmelCase_         =      DebertaVaConfig(
							    vocab_size=self.vocab_size							,							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							,							max_position_embeddings=self.max_position_embeddings							,							type_vocab_size=self.type_vocab_size							,							relative_attention=self.relative_attention							,							position_biased_input=self.position_biased_input							,							initializer_range=self.initializer_range							,							return_dict=_UpperCamelCase							,							)

							return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels




				def 	__a		(   self							,							_UpperCamelCase							,							_UpperCamelCase							,							_UpperCamelCase							,							_UpperCamelCase							,							_UpperCamelCase							,							_UpperCamelCase							,							_UpperCamelCase	)							-> Dict:
							lowerCAmelCase_         =      TFDebertaVaModel(config=_UpperCamelCase	)
							lowerCAmelCase_         =      {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}

							lowerCAmelCase_         =      [input_ids, input_mask]
							lowerCAmelCase_         =      model(_UpperCamelCase	)

							lowerCAmelCase_         =      model(_UpperCamelCase	)

							self.parent.assertEqual(result.last_hidden_state.shape							,							(self.batch_size, self.seq_length, self.hidden_size)	)




				def 	__a		(   self							,							_UpperCamelCase							,							_UpperCamelCase							,							_UpperCamelCase							,							_UpperCamelCase							,							_UpperCamelCase							,							_UpperCamelCase							,							_UpperCamelCase	)							-> Any:
							lowerCAmelCase_         =      TFDebertaVaForMaskedLM(config=_UpperCamelCase	)
							lowerCAmelCase_         =      {
							    "input_ids": input_ids,
							    "attention_mask": input_mask,
							    "token_type_ids": token_type_ids,
							}
							lowerCAmelCase_         =      model(_UpperCamelCase	)
							self.parent.assertEqual(result.logits.shape							,							(self.batch_size, self.seq_length, self.vocab_size)	)




				def 	__a		(   self							,							_UpperCamelCase							,							_UpperCamelCase							,							_UpperCamelCase							,							_UpperCamelCase							,							_UpperCamelCase							,							_UpperCamelCase							,							_UpperCamelCase	)							-> str:
							lowerCAmelCase_         =      self.num_labels
							lowerCAmelCase_         =      TFDebertaVaForSequenceClassification(config=_UpperCamelCase	)
							lowerCAmelCase_         =      {
							    "input_ids": input_ids,
							    "attention_mask": input_mask,
							    "token_type_ids": token_type_ids,
							}

							lowerCAmelCase_         =      model(_UpperCamelCase	)
							self.parent.assertEqual(result.logits.shape							,							(self.batch_size, self.num_labels)	)




				def 	__a		(   self							,							_UpperCamelCase							,							_UpperCamelCase							,							_UpperCamelCase							,							_UpperCamelCase							,							_UpperCamelCase							,							_UpperCamelCase							,							_UpperCamelCase	)							-> int:
							lowerCAmelCase_         =      self.num_labels
							lowerCAmelCase_         =      TFDebertaVaForTokenClassification(config=_UpperCamelCase	)
							lowerCAmelCase_         =      {
							    "input_ids": input_ids,
							    "attention_mask": input_mask,
							    "token_type_ids": token_type_ids,
							}
							lowerCAmelCase_         =      model(_UpperCamelCase	)
							self.parent.assertEqual(result.logits.shape							,							(self.batch_size, self.seq_length, self.num_labels)	)




				def 	__a		(   self							,							_UpperCamelCase							,							_UpperCamelCase							,							_UpperCamelCase							,							_UpperCamelCase							,							_UpperCamelCase							,							_UpperCamelCase							,							_UpperCamelCase	)							-> Dict:
							lowerCAmelCase_         =      TFDebertaVaForQuestionAnswering(config=_UpperCamelCase	)
							lowerCAmelCase_         =      {
							    "input_ids": input_ids,
							    "attention_mask": input_mask,
							    "token_type_ids": token_type_ids,
							}

							lowerCAmelCase_         =      model(_UpperCamelCase	)
							self.parent.assertEqual(result.start_logits.shape							,							(self.batch_size, self.seq_length)	)
							self.parent.assertEqual(result.end_logits.shape							,							(self.batch_size, self.seq_length)	)




				def 	__a		(   self	)							-> Tuple:
							lowerCAmelCase_         =      self.prepare_config_and_inputs()
							(
							    (
							    lowerCAmelCase_
							)      ,    (
							    lowerCAmelCase_
							)      ,    (
							    lowerCAmelCase_
							)      ,    (
							    lowerCAmelCase_
							)      ,    (
							    lowerCAmelCase_
							)      ,    (
							    lowerCAmelCase_
							)      ,    (
							    lowerCAmelCase_
							)      ,    
							)         =      config_and_inputs
							lowerCAmelCase_         =      {"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
							return config, inputs_dict





@require_tf
class       _lowerCAmelCase   (      __a   , __a   , unittest.TestCase			):
				_lowercase     						=(
				    (
				        TFDebertaVaModel,
				        TFDebertaVaForMaskedLM,
				        TFDebertaVaForQuestionAnswering,
				        TFDebertaVaForSequenceClassification,
				        TFDebertaVaForTokenClassification,
				    )
				    if is_tf_available()
				    else ()
				)
				_lowercase     						=(
				    {
				        '''feature-extraction''': TFDebertaVaModel,
				        '''fill-mask''': TFDebertaVaForMaskedLM,
				        '''question-answering''': TFDebertaVaForQuestionAnswering,
				        '''text-classification''': TFDebertaVaForSequenceClassification,
				        '''token-classification''': TFDebertaVaForTokenClassification,
				        '''zero-shot''': TFDebertaVaForSequenceClassification,
				    }
				    if is_tf_available()
				    else {}
				)

				_lowercase     						=False
				_lowercase     						=False




				def 	__a		(   self	)							-> Optional[int]:
							lowerCAmelCase_         =      TFDebertaVaModelTester(self	)
							lowerCAmelCase_         =      ConfigTester(self							,							config_class=_UpperCamelCase							,							hidden_size=37	)




				def 	__a		(   self	)							-> str:
							self.config_tester.run_common_tests()




				def 	__a		(   self	)							-> int:
							lowerCAmelCase_         =      self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*_UpperCamelCase	)




				def 	__a		(   self	)							-> List[Any]:
							lowerCAmelCase_         =      self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_masked_lm(*_UpperCamelCase	)




				def 	__a		(   self	)							-> List[str]:
							lowerCAmelCase_         =      self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_question_answering(*_UpperCamelCase	)




				def 	__a		(   self	)							-> List[Any]:
							lowerCAmelCase_         =      self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_sequence_classification(*_UpperCamelCase	)




				def 	__a		(   self	)							-> int:
							lowerCAmelCase_         =      self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_token_classification(*_UpperCamelCase	)




				@slow
				def 	__a		(   self	)							-> Union[str, Any]:
							lowerCAmelCase_         =      TFDebertaVaModel.from_pretrained("kamalkraj/deberta-v2-xlarge"	)
							self.assertIsNotNone(_UpperCamelCase	)





@require_tf
class       _lowerCAmelCase   (      unittest.TestCase			):




				@unittest.skip(reason="Model not available yet"	)
				def 	__a		(   self	)							-> List[str]:
							pass




				@slow
				def 	__a		(   self	)							-> Dict:
							lowerCAmelCase_         =      TFDebertaVaModel.from_pretrained("kamalkraj/deberta-v2-xlarge"	)
							lowerCAmelCase_         =      tf.constant([[0, 31_414, 232, 328, 740, 1_140, 12_695, 69, 46_078, 1_588, 2]]	)
							lowerCAmelCase_         =      tf.constant([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]	)
							lowerCAmelCase_         =      model(_UpperCamelCase							,							attention_mask=_UpperCamelCase	)[0]

							lowerCAmelCase_         =      tf.constant(
							    [[[0.2356, 0.1948, 0.0369], [-0.1063, 0.3586, -0.5152], [-0.6399, -0.0259, -0.2525]]]	)
							tf.debugging.assert_near(output[:, 1:4, 1:4]							,							_UpperCamelCase							,							atol=1e-4	)



