



import shutil
import tempfile
import unittest

import numpy as np
import pytest

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available


if is_torch_available():
      from transformers.pytorch_utils import is_torch_greater_or_equal_than_1_11
else:
      _lowerCamelCase         =	False

if is_vision_available():
      from PIL import Image

      from transformers import (
          AutoProcessor,
          PixaStructImageProcessor,
          PixaStructProcessor,
          PreTrainedTokenizerFast,
          TaTokenizer,
      )

@unittest.skipIf(
    not is_torch_greater_or_equal_than_1_11      ,     reason="""`Pix2StructImageProcessor` requires `torch>=1.11.0`."""      ,     )
@require_vision
@require_torch
class    _SCREAMING_SNAKE_CASE						(unittest.TestCase			):





     def 						__snake_case     (							self			:				List[Any]					)->Tuple:
      __SCREAMING_SNAKE_CASE							:     Optional[int]							=						tempfile.mkdtemp()

      __SCREAMING_SNAKE_CASE							:     Optional[Any]							=						PixaStructImageProcessor()
      __SCREAMING_SNAKE_CASE							:     Optional[int]							=						TaTokenizer.from_pretrained("t5-small"					)

      __SCREAMING_SNAKE_CASE							:     str							=						PixaStructProcessor(UpperCamelCase  ,	UpperCamelCase					)

      processor.save_pretrained(self.tmpdirname					)





     def 						__snake_case     (							self			:				List[str]  ,	**UpperCamelCase			:				List[str]					)->List[str]:
      return AutoProcessor.from_pretrained(self.tmpdirname  ,	**UpperCamelCase					).tokenizer





     def 						__snake_case     (							self			:				Any  ,	**UpperCamelCase			:				List[Any]					)->Any:
      return AutoProcessor.from_pretrained(self.tmpdirname  ,	**UpperCamelCase					).image_processor





     def 						__snake_case     (							self			:				List[str]					)->List[Any]:
      shutil.rmtree(self.tmpdirname					)





     def 						__snake_case     (							self			:				Optional[int]					)->Any:
      __SCREAMING_SNAKE_CASE							:     Union[str, Any]							=						[np.random.randint(2_5_5  ,	size=(3, 3_0, 4_0_0)  ,	dtype=np.uinta					)]

      __SCREAMING_SNAKE_CASE							:     Dict							=						[Image.fromarray(np.moveaxis(UpperCamelCase  ,	0  ,	-1					)					) for x in image_inputs]

      return image_inputs





     def 						__snake_case     (							self			:				List[Any]					)->List[Any]:
      __SCREAMING_SNAKE_CASE							:     List[str]							=						PixaStructProcessor(tokenizer=self.get_tokenizer()  ,	image_processor=self.get_image_processor()					)
      processor.save_pretrained(self.tmpdirname					)

      __SCREAMING_SNAKE_CASE							:     Optional[int]							=						self.get_tokenizer(bos_token="(BOS)"  ,	eos_token="(EOS)"					)
      __SCREAMING_SNAKE_CASE							:     List[Any]							=						self.get_image_processor(do_normalize=UpperCamelCase  ,	padding_value=1.0					)

      __SCREAMING_SNAKE_CASE							:     Tuple							=						PixaStructProcessor.from_pretrained(
          self.tmpdirname  ,	bos_token="(BOS)"  ,	eos_token="(EOS)"  ,	do_normalize=UpperCamelCase  ,	padding_value=1.0					)

      self.assertEqual(processor.tokenizer.get_vocab()  ,	tokenizer_add_kwargs.get_vocab()					)
      self.assertIsInstance(processor.tokenizer  ,	UpperCamelCase					)

      self.assertEqual(processor.image_processor.to_json_string()  ,	image_processor_add_kwargs.to_json_string()					)
      self.assertIsInstance(processor.image_processor  ,	UpperCamelCase					)





     def 						__snake_case     (							self			:				Optional[int]					)->List[str]:
      __SCREAMING_SNAKE_CASE							:     int							=						self.get_image_processor()
      __SCREAMING_SNAKE_CASE							:     int							=						self.get_tokenizer()

      __SCREAMING_SNAKE_CASE							:     Dict							=						PixaStructProcessor(tokenizer=UpperCamelCase  ,	image_processor=UpperCamelCase					)

      __SCREAMING_SNAKE_CASE							:     Union[str, Any]							=						self.prepare_image_inputs()

      __SCREAMING_SNAKE_CASE							:     int							=						image_processor(UpperCamelCase  ,	return_tensors="np"					)
      __SCREAMING_SNAKE_CASE							:     Optional[Any]							=						processor(images=UpperCamelCase  ,	return_tensors="np"					)

      for key in input_feat_extract.keys():
       self.assertAlmostEqual(input_feat_extract[key].sum()  ,	input_processor[key].sum()  ,	delta=1E-2					)





     def 						__snake_case     (							self			:				Union[str, Any]					)->Any:
      __SCREAMING_SNAKE_CASE							:     Optional[int]							=						self.get_image_processor()
      __SCREAMING_SNAKE_CASE							:     List[str]							=						self.get_tokenizer()

      __SCREAMING_SNAKE_CASE							:     Optional[int]							=						PixaStructProcessor(tokenizer=UpperCamelCase  ,	image_processor=UpperCamelCase					)

      __SCREAMING_SNAKE_CASE							:     Any							=						"lower newer"

      __SCREAMING_SNAKE_CASE							:     Tuple							=						processor(text=UpperCamelCase					)

      __SCREAMING_SNAKE_CASE							:     str							=						tokenizer(UpperCamelCase  ,	return_token_type_ids=UpperCamelCase  ,	add_special_tokens=UpperCamelCase					)

      for key in encoded_tok.keys():
       self.assertListEqual(encoded_tok[key]  ,	encoded_processor[key]					)





     def 						__snake_case     (							self			:				Dict					)->str:
      __SCREAMING_SNAKE_CASE							:     Optional[Any]							=						self.get_image_processor()
      __SCREAMING_SNAKE_CASE							:     int							=						self.get_tokenizer()

      __SCREAMING_SNAKE_CASE							:     List[str]							=						PixaStructProcessor(tokenizer=UpperCamelCase  ,	image_processor=UpperCamelCase					)

      __SCREAMING_SNAKE_CASE							:     Any							=						"lower newer"
      __SCREAMING_SNAKE_CASE							:     str							=						self.prepare_image_inputs()

      __SCREAMING_SNAKE_CASE							:     Any							=						processor(text=UpperCamelCase  ,	images=UpperCamelCase					)

      self.assertListEqual(
          list(inputs.keys()					)  ,	["flattened_patches", "attention_mask", "decoder_attention_mask", "decoder_input_ids"]					)

      # test if it raises when no input is passed
      with pytest.raises(UpperCamelCase					):
       processor()





     def 						__snake_case     (							self			:				Union[str, Any]					)->int:
      __SCREAMING_SNAKE_CASE							:     Optional[int]							=						self.get_image_processor()
      __SCREAMING_SNAKE_CASE							:     int							=						self.get_tokenizer()

      __SCREAMING_SNAKE_CASE							:     Optional[Any]							=						PixaStructProcessor(tokenizer=UpperCamelCase  ,	image_processor=UpperCamelCase					)

      __SCREAMING_SNAKE_CASE							:     Optional[Any]							=						"lower newer"
      __SCREAMING_SNAKE_CASE							:     Optional[int]							=						self.prepare_image_inputs()

      __SCREAMING_SNAKE_CASE							:     Union[str, Any]							=						processor(text=UpperCamelCase  ,	images=UpperCamelCase					)

      __SCREAMING_SNAKE_CASE							:     Tuple							=						[5_1_2, 1_0_2_4, 2_0_4_8, 4_0_9_6]
      __SCREAMING_SNAKE_CASE							:     Tuple							=						[7_7_0, 7_7_0, 7_7_0, 7_7_0]
      # with text
      for i, max_patch in enumerate(UpperCamelCase					):
       __SCREAMING_SNAKE_CASE							:     List[str]							=						processor(text=UpperCamelCase  ,	images=UpperCamelCase  ,	max_patches=UpperCamelCase					)
       self.assertEqual(inputs["flattened_patches"][0].shape[0]  ,	UpperCamelCase					)
       self.assertEqual(inputs["flattened_patches"][0].shape[1]  ,	expected_hidden_size[i]					)

      # without text input
      for i, max_patch in enumerate(UpperCamelCase					):
       __SCREAMING_SNAKE_CASE							:     int							=						processor(images=UpperCamelCase  ,	max_patches=UpperCamelCase					)
       self.assertEqual(inputs["flattened_patches"][0].shape[0]  ,	UpperCamelCase					)
       self.assertEqual(inputs["flattened_patches"][0].shape[1]  ,	expected_hidden_size[i]					)





     def 						__snake_case     (							self			:				List[str]					)->Tuple:
      __SCREAMING_SNAKE_CASE							:     str							=						self.get_image_processor()
      __SCREAMING_SNAKE_CASE							:     Optional[Any]							=						self.get_tokenizer()

      __SCREAMING_SNAKE_CASE							:     Optional[int]							=						PixaStructProcessor(tokenizer=UpperCamelCase  ,	image_processor=UpperCamelCase					)

      __SCREAMING_SNAKE_CASE							:     str							=						[[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

      __SCREAMING_SNAKE_CASE							:     Optional[int]							=						processor.batch_decode(UpperCamelCase					)
      __SCREAMING_SNAKE_CASE							:     Optional[Any]							=						tokenizer.batch_decode(UpperCamelCase					)

      self.assertListEqual(UpperCamelCase  ,	UpperCamelCase					)






     def 						__snake_case     (							self			:				List[str]					)->int:
      __SCREAMING_SNAKE_CASE							:     Optional[Any]							=						self.get_image_processor()
      __SCREAMING_SNAKE_CASE							:     str							=						self.get_tokenizer()

      __SCREAMING_SNAKE_CASE							:     Dict							=						PixaStructProcessor(tokenizer=UpperCamelCase  ,	image_processor=UpperCamelCase					)

      __SCREAMING_SNAKE_CASE							:     Optional[int]							=						"lower newer"
      __SCREAMING_SNAKE_CASE							:     Optional[Any]							=						self.prepare_image_inputs()

      __SCREAMING_SNAKE_CASE							:     Optional[Any]							=						processor(text=UpperCamelCase  ,	images=UpperCamelCase					)

      # For now the processor supports only ["flattened_patches", "input_ids", "attention_mask", "decoder_attention_mask"]
      self.assertListEqual(
          list(inputs.keys()					)  ,	["flattened_patches", "attention_mask", "decoder_attention_mask", "decoder_input_ids"]					)

      __SCREAMING_SNAKE_CASE							:     Tuple							=						processor(text=UpperCamelCase					)

      # For now the processor supports only ["flattened_patches", "input_ids", "attention_mask", "decoder_attention_mask"]
      self.assertListEqual(list(inputs.keys()					)  ,	["input_ids", "attention_mask"]					)












'''simple docstring'''
import gc
import unittest

import numpy as np
import torch

from diffusers import DDPMScheduler, MidiProcessor, SpectrogramDiffusionPipeline
from diffusers.pipelines.spectrogram_diffusion import SpectrogramContEncoder, SpectrogramNotesEncoder, TaFilmDecoder
from diffusers.utils import require_torch_gpu, skip_mps, slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_note_seq, require_onnxruntime

from ..pipeline_params import TOKENS_TO_AUDIO_GENERATION_BATCH_PARAMS, TOKENS_TO_AUDIO_GENERATION_PARAMS
from ..test_pipelines_common import PipelineTesterMixin


enable_full_determinism()


A				: List[str]					      =			"""./tests/fixtures/elise_format0.mid"""






@unittest.skip("""The note-seq package currently throws an error on import"""	)
class 			SCREAMING_SNAKE_CASE(							__A  , unittest.TestCase	):
       snake_case_					:	List[str]       =   SpectrogramDiffusionPipeline
       snake_case_					:	Any       =   PipelineTesterMixin.required_optional_params - {
           """callback""",
           """latents""",
           """callback_steps""",
           """output_type""",
           """num_images_per_prompt""",
       }
       snake_case_					:	int       =   False
       snake_case_					:	Tuple       =   False
       snake_case_					:	Tuple       =   TOKENS_TO_AUDIO_GENERATION_PARAMS
       snake_case_					:	Optional[int]       =   TOKENS_TO_AUDIO_GENERATION_BATCH_PARAMS

       def        snake_case__     (       self	)       ->   Optional[Any]:

           """simple docstring"""



           torch.manual_seed(0	)
           __lowercase     							=		SpectrogramNotesEncoder(
               max_length=2048   ,						vocab_size=1536   ,						d_model=768   ,						dropout_rate=0.1   ,						num_layers=1   ,						num_heads=1   ,						d_kv=4   ,						d_ff=2048   ,						feed_forward_proj="""gated-gelu"""   ,						)

           __lowercase     							=		SpectrogramContEncoder(
               input_dims=128   ,						targets_context_length=256   ,						d_model=768   ,						dropout_rate=0.1   ,						num_layers=1   ,						num_heads=1   ,						d_kv=4   ,						d_ff=2048   ,						feed_forward_proj="""gated-gelu"""   ,						)

           __lowercase     							=		TaFilmDecoder(
               input_dims=128   ,						targets_length=256   ,						max_decoder_noise_time=2_00_00.0   ,						d_model=768   ,						num_layers=1   ,						num_heads=1   ,						d_kv=4   ,						d_ff=2048   ,						dropout_rate=0.1   ,						)

           __lowercase     							=		DDPMScheduler()

           __lowercase     							=		{
               """notes_encoder""": notes_encoder.eval(),
               """continuous_encoder""": continuous_encoder.eval(),
               """decoder""": decoder.eval(),
               """scheduler""": scheduler,
               """melgan""": None,
           }
           return components

       def        snake_case__     (       self   ,						lowerCamelCase__   ,						lowerCamelCase__=0	)       ->   Any:

           """simple docstring"""



           if str(lowerCamelCase__	).startswith("""mps"""	):
               __lowercase     							=		torch.manual_seed(lowerCamelCase__	)
           else:
               __lowercase     							=		torch.Generator(device=lowerCamelCase__	).manual_seed(lowerCamelCase__	)
           __lowercase     							=		{
               """input_tokens""": [
                   [1134, 90, 1135, 1133, 1080, 112, 1132, 1080, 1133, 1079, 133, 1132, 1079, 1133, 1] + [0] * 2033
               ],
               """generator""": generator,
               """num_inference_steps""": 4,
               """output_type""": """mel""",
           }
           return inputs

       def        snake_case__     (       self	)       ->   str:

           """simple docstring"""



           __lowercase     							=		"""cpu"""  # ensure determinism for the device-dependent torch.Generator
           __lowercase     							=		self.get_dummy_components()
           __lowercase     							=		SpectrogramDiffusionPipeline(**lowerCamelCase__	)
           __lowercase     							=		pipe.to(lowerCamelCase__	)
           pipe.set_progress_bar_config(disable=lowerCamelCase__	)

           __lowercase     							=		self.get_dummy_inputs(lowerCamelCase__	)
           __lowercase     							=		pipe(**lowerCamelCase__	)
           __lowercase     							=		output.audios

           __lowercase     							=		mel[0, -3:, -3:]

           assert mel_slice.shape == (3, 3)
           __lowercase     							=		np.array(
               [-11.51_29_25, -4.78_82_15, -0.46_17_29_05, -2.05_17_15, -10.53_91_47, -10.97_09_63, -9.09_16_34, 4.0, 4.0]	)
           assert np.abs(mel_slice.flatten() - expected_slice	).max() < 1E-2

       @skip_mps
       def        snake_case__     (       self	)       ->   Any:

           """simple docstring"""



           return super().test_save_load_local()

       @skip_mps
       def        snake_case__     (       self	)       ->   Tuple:

           """simple docstring"""



           return super().test_dict_tuple_outputs_equivalent()

       @skip_mps
       def        snake_case__     (       self	)       ->   Any:

           """simple docstring"""



           return super().test_save_load_optional_components()

       @skip_mps
       def        snake_case__     (       self	)       ->   List[str]:

           """simple docstring"""



           return super().test_attention_slicing_forward_pass()

       def        snake_case__     (       self	)       ->   Optional[Any]:

           """simple docstring"""



           pass

       def        snake_case__     (       self	)       ->   Union[str, Any]:

           """simple docstring"""



           pass




       @skip_mps
       def        snake_case__     (       self	)       ->   Optional[Any]:

           """simple docstring"""



           return super().test_progress_bar()






@slow
@require_torch_gpu
@require_onnxruntime
@require_note_seq
class 			SCREAMING_SNAKE_CASE(							unittest.TestCase	):

       def        snake_case__     (       self	)       ->   Tuple:

           """simple docstring"""



           super().tearDown()
           gc.collect()
           torch.cuda.empty_cache()

       def        snake_case__     (       self	)       ->   List[Any]:

           """simple docstring"""



           __lowercase     							=		torch_device

           __lowercase     							=		SpectrogramDiffusionPipeline.from_pretrained("""google/music-spectrogram-diffusion"""	)
           __lowercase     							=		pipe.melgan
           __lowercase     							=		None

           __lowercase     							=		pipe.to(lowerCamelCase__	)
           pipe.set_progress_bar_config(disable=lowerCamelCase__	)

           def callback(lowerCamelCase__   ,						lowerCamelCase__	):
               # decode mel to audio
               __lowercase     							=		melgan(input_features=mel_output.astype(np.floataa	)	)[0]
               assert len(audio[0]	) == 8_1920 * (step + 1)
               # simulate that audio is played
               return audio

           __lowercase     							=		MidiProcessor()
           __lowercase     							=		processor(lowerCamelCase__	)

           __lowercase     							=		input_tokens[:3]
           __lowercase     							=		torch.manual_seed(0	)
           pipe(lowerCamelCase__   ,						num_inference_steps=5   ,						generator=lowerCamelCase__   ,						callback=lowerCamelCase__   ,						output_type="""mel"""	)

       def        snake_case__     (       self	)       ->   Union[str, Any]:

           """simple docstring"""



           __lowercase     							=		torch_device

           __lowercase     							=		SpectrogramDiffusionPipeline.from_pretrained("""google/music-spectrogram-diffusion"""	)
           __lowercase     							=		pipe.to(lowerCamelCase__	)
           pipe.set_progress_bar_config(disable=lowerCamelCase__	)
           __lowercase     							=		MidiProcessor()

           __lowercase     							=		processor(lowerCamelCase__	)
           # just run two denoising loops
           __lowercase     							=		input_tokens[:2]

           __lowercase     							=		torch.manual_seed(0	)
           __lowercase     							=		pipe(lowerCamelCase__   ,						num_inference_steps=2   ,						generator=lowerCamelCase__	)

           __lowercase     							=		output.audios[0]

           assert abs(np.abs(lowerCamelCase__	).sum() - 36_12.8_41	) < 1E-1




       def        snake_case__     (       self	)       ->   List[str]:

           """simple docstring"""



           __lowercase     							=		torch_device

           __lowercase     							=		SpectrogramDiffusionPipeline.from_pretrained("""google/music-spectrogram-diffusion"""	)
           __lowercase     							=		pipe.to(lowerCamelCase__	)
           pipe.set_progress_bar_config(disable=lowerCamelCase__	)

           __lowercase     							=		MidiProcessor()

           __lowercase     							=		processor(lowerCamelCase__	)

           # just run 4 denoising loops
           __lowercase     							=		input_tokens[:4]

           __lowercase     							=		torch.manual_seed(0	)
           __lowercase     							=		pipe(lowerCamelCase__   ,						num_inference_steps=100   ,						generator=lowerCamelCase__	)

           __lowercase     							=		output.audios[0]
           assert abs(np.abs(lowerCamelCase__	).sum() - 93_89.11_11	) < 5E-2




