
"""simple docstring"""




import json
import os
import shutil
import tempfile
import unittest

from transformers import is_speech_available, is_torch_available
from transformers.models.speechta import SpeechTaTokenizer
from transformers.testing_utils import get_tests_dir, require_torch
from transformers.utils import FEATURE_EXTRACTOR_NAME


if is_speech_available() and is_torch_available():
				from transformers import SpeechTaFeatureExtractor, SpeechTaProcessor

				from .test_feature_extraction_speechta import floats_list


lowercase__							:							List[Any]													=     get_tests_dir("fixtures/test_sentencepiece_bpe_char.model")






@require_torch
class     SCREAMING_SNAKE_CASE__      (							unittest.TestCase      ):

							"""simple docstring"""






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





													'''simple docstring'''






													__UpperCamelCase       					=     tempfile.mkdtemp()

													__UpperCamelCase       					=     SpeechTaTokenizer(SCREAMING_SNAKE_CASE_     )
													tokenizer.save_pretrained(self.tmpdirname     )

													__UpperCamelCase       					=     {
													    '''feature_size''': 1,
													    '''padding_value''': 0.0,
													    '''sampling_rate''': 16000,
													    '''do_normalize''': False,
													    '''num_mel_bins''': 80,
													    '''hop_length''': 16,
													    '''win_length''': 64,
													    '''win_function''': '''hann_window''',
													    '''fmin''': 80,
													    '''fmax''': 7600,
													    '''mel_floor''': 1E-10,
													    '''reduction_factor''': 2,
													    '''return_attention_mask''': True,
													}

													__UpperCamelCase       					=     os.path.join(self.tmpdirname       ,		SCREAMING_SNAKE_CASE_     )
													with open(self.feature_extraction_file       ,		'''w'''       ,		encoding='''utf-8'''     ) as fp:
																			fp.write(json.dumps(SCREAMING_SNAKE_CASE_     ) + '''\n'''     )



							def       A__						(					self       ,		**SCREAMING_SNAKE_CASE_     )->				Union[str, Any]:





													'''simple docstring'''






													return SpeechTaTokenizer.from_pretrained(self.tmpdirname       ,		**SCREAMING_SNAKE_CASE_     )



							def       A__						(					self       ,		**SCREAMING_SNAKE_CASE_     )->				Union[str, Any]:





													'''simple docstring'''






													return SpeechTaFeatureExtractor.from_pretrained(self.tmpdirname       ,		**SCREAMING_SNAKE_CASE_     )



							def       A__						(					self     )->				str:





													'''simple docstring'''






													shutil.rmtree(self.tmpdirname     )



							def       A__						(					self     )->				Optional[int]:





													'''simple docstring'''






													__UpperCamelCase       					=     self.get_tokenizer()
													__UpperCamelCase       					=     self.get_feature_extractor()

													__UpperCamelCase       					=     SpeechTaProcessor(tokenizer=SCREAMING_SNAKE_CASE_       ,		feature_extractor=SCREAMING_SNAKE_CASE_     )

													processor.save_pretrained(self.tmpdirname     )
													__UpperCamelCase       					=     SpeechTaProcessor.from_pretrained(self.tmpdirname     )

													self.assertEqual(processor.tokenizer.get_vocab()       ,		tokenizer.get_vocab()     )
													self.assertIsInstance(processor.tokenizer       ,		SCREAMING_SNAKE_CASE_     )

													self.assertEqual(processor.feature_extractor.to_json_string()       ,		feature_extractor.to_json_string()     )
													self.assertIsInstance(processor.feature_extractor       ,		SCREAMING_SNAKE_CASE_     )



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





													'''simple docstring'''






													__UpperCamelCase       					=     SpeechTaProcessor(tokenizer=self.get_tokenizer()       ,		feature_extractor=self.get_feature_extractor()     )
													processor.save_pretrained(self.tmpdirname     )

													__UpperCamelCase       					=     self.get_tokenizer(bos_token='''(BOS)'''       ,		eos_token='''(EOS)'''     )
													__UpperCamelCase       					=     self.get_feature_extractor(do_normalize=SCREAMING_SNAKE_CASE_       ,		padding_value=1.0     )

													__UpperCamelCase       					=     SpeechTaProcessor.from_pretrained(
													    self.tmpdirname       ,		bos_token='''(BOS)'''       ,		eos_token='''(EOS)'''       ,		do_normalize=SCREAMING_SNAKE_CASE_       ,		padding_value=1.0     )

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

													self.assertEqual(processor.feature_extractor.to_json_string()       ,		feature_extractor_add_kwargs.to_json_string()     )
													self.assertIsInstance(processor.feature_extractor       ,		SCREAMING_SNAKE_CASE_     )



							def       A__						(					self     )->				Dict:





													'''simple docstring'''






													__UpperCamelCase       					=     self.get_feature_extractor()
													__UpperCamelCase       					=     self.get_tokenizer()

													__UpperCamelCase       					=     SpeechTaProcessor(tokenizer=SCREAMING_SNAKE_CASE_       ,		feature_extractor=SCREAMING_SNAKE_CASE_     )

													__UpperCamelCase       					=     floats_list((3, 1000)     )

													__UpperCamelCase       					=     feature_extractor(audio=SCREAMING_SNAKE_CASE_       ,		return_tensors='''np'''     )
													__UpperCamelCase       					=     processor(audio=SCREAMING_SNAKE_CASE_       ,		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       A__						(					self     )->				Dict:





													'''simple docstring'''






													__UpperCamelCase       					=     self.get_feature_extractor()
													__UpperCamelCase       					=     self.get_tokenizer()

													__UpperCamelCase       					=     SpeechTaProcessor(tokenizer=SCREAMING_SNAKE_CASE_       ,		feature_extractor=SCREAMING_SNAKE_CASE_     )

													__UpperCamelCase       					=     floats_list((3, 1000)     )

													__UpperCamelCase       					=     feature_extractor(audio_target=SCREAMING_SNAKE_CASE_       ,		return_tensors='''np'''     )
													__UpperCamelCase       					=     processor(audio_target=SCREAMING_SNAKE_CASE_       ,		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       A__						(					self     )->				int:





													'''simple docstring'''






													__UpperCamelCase       					=     self.get_feature_extractor()
													__UpperCamelCase       					=     self.get_tokenizer()

													__UpperCamelCase       					=     SpeechTaProcessor(tokenizer=SCREAMING_SNAKE_CASE_       ,		feature_extractor=SCREAMING_SNAKE_CASE_     )

													__UpperCamelCase       					=     '''This is a test string'''

													__UpperCamelCase       					=     processor(text=SCREAMING_SNAKE_CASE_     )
													__UpperCamelCase       					=     tokenizer(SCREAMING_SNAKE_CASE_     )

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



							def       A__						(					self     )->				int:





													'''simple docstring'''






													__UpperCamelCase       					=     self.get_feature_extractor()
													__UpperCamelCase       					=     self.get_tokenizer()

													__UpperCamelCase       					=     SpeechTaProcessor(tokenizer=SCREAMING_SNAKE_CASE_       ,		feature_extractor=SCREAMING_SNAKE_CASE_     )

													__UpperCamelCase       					=     '''This is a test string'''

													__UpperCamelCase       					=     processor(text_target=SCREAMING_SNAKE_CASE_     )
													__UpperCamelCase       					=     tokenizer(SCREAMING_SNAKE_CASE_     )

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



							def       A__						(					self     )->				Dict:





													'''simple docstring'''






													__UpperCamelCase       					=     self.get_feature_extractor()
													__UpperCamelCase       					=     self.get_tokenizer()

													__UpperCamelCase       					=     SpeechTaProcessor(tokenizer=SCREAMING_SNAKE_CASE_       ,		feature_extractor=SCREAMING_SNAKE_CASE_     )

													__UpperCamelCase       					=     [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

													__UpperCamelCase       					=     processor.batch_decode(SCREAMING_SNAKE_CASE_     )
													__UpperCamelCase       					=     tokenizer.batch_decode(SCREAMING_SNAKE_CASE_     )

													self.assertListEqual(SCREAMING_SNAKE_CASE_       ,		SCREAMING_SNAKE_CASE_     )



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





													'''simple docstring'''






													__UpperCamelCase       					=     self.get_feature_extractor()
													__UpperCamelCase       					=     self.get_tokenizer()

													__UpperCamelCase       					=     SpeechTaProcessor(tokenizer=SCREAMING_SNAKE_CASE_       ,		feature_extractor=SCREAMING_SNAKE_CASE_     )

													self.assertListEqual(
													    processor.model_input_names       ,		feature_extractor.model_input_names       ,		msg='''`processor` and `feature_extractor` model input names do not match'''       ,		)






import gc
import unittest

import numpy as np
import torch
from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer

from diffusers import (
    AutoencoderKL,
    DDIMParallelScheduler,
    DDPMParallelScheduler,
    StableDiffusionParadigmsPipeline,
    UNetaDConditionModel,
)
from diffusers.utils import slow, torch_device
from diffusers.utils.testing_utils import (
    enable_full_determinism,
    require_torch_gpu,
)

from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_IMAGE_PARAMS, TEXT_TO_IMAGE_PARAMS
from ..test_pipelines_common import PipelineLatentTesterMixin, PipelineTesterMixin


enable_full_determinism()






class     SCREAMING_SNAKE_CASE__      (							SCREAMING_SNAKE_CASE_				,						SCREAMING_SNAKE_CASE_				,						unittest.TestCase      ):

							"""simple docstring"""






							_snake_case       						=					StableDiffusionParadigmsPipeline
							_snake_case       						=					TEXT_TO_IMAGE_PARAMS
							_snake_case       						=					TEXT_TO_IMAGE_BATCH_PARAMS
							_snake_case       						=					TEXT_TO_IMAGE_IMAGE_PARAMS
							_snake_case       						=					TEXT_TO_IMAGE_IMAGE_PARAMS



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





													'''simple docstring'''






													torch.manual_seed(0     )
													__UpperCamelCase       					=     UNetaDConditionModel(
													    block_out_channels=(32, 64)       ,		layers_per_block=2       ,		sample_size=32       ,		in_channels=4       ,		out_channels=4       ,		down_block_types=('''DownBlock2D''', '''CrossAttnDownBlock2D''')       ,		up_block_types=('''CrossAttnUpBlock2D''', '''UpBlock2D''')       ,		cross_attention_dim=32       ,		attention_head_dim=(2, 4)       ,		use_linear_projection=SCREAMING_SNAKE_CASE_       ,		)
													__UpperCamelCase       					=     DDIMParallelScheduler(
													    beta_start=0.0_0_0_8_5       ,		beta_end=0.0_1_2       ,		beta_schedule='''scaled_linear'''       ,		clip_sample=SCREAMING_SNAKE_CASE_       ,		set_alpha_to_one=SCREAMING_SNAKE_CASE_       ,		)
													torch.manual_seed(0     )
													__UpperCamelCase       					=     AutoencoderKL(
													    block_out_channels=[32, 64]       ,		in_channels=3       ,		out_channels=3       ,		down_block_types=['''DownEncoderBlock2D''', '''DownEncoderBlock2D''']       ,		up_block_types=['''UpDecoderBlock2D''', '''UpDecoderBlock2D''']       ,		latent_channels=4       ,		sample_size=128       ,		)
													torch.manual_seed(0     )
													__UpperCamelCase       					=     CLIPTextConfig(
													    bos_token_id=0       ,		eos_token_id=2       ,		hidden_size=32       ,		intermediate_size=37       ,		layer_norm_eps=1E-05       ,		num_attention_heads=4       ,		num_hidden_layers=5       ,		pad_token_id=1       ,		vocab_size=1000       ,		hidden_act='''gelu'''       ,		projection_dim=512       ,		)
													__UpperCamelCase       					=     CLIPTextModel(SCREAMING_SNAKE_CASE_     )
													__UpperCamelCase       					=     CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip'''     )

													__UpperCamelCase       					=     {
													    '''unet''': unet,
													    '''scheduler''': scheduler,
													    '''vae''': vae,
													    '''text_encoder''': text_encoder,
													    '''tokenizer''': tokenizer,
													    '''safety_checker''': None,
													    '''feature_extractor''': None,
													}
													return components



							def       A__						(					self       ,		SCREAMING_SNAKE_CASE_       ,		SCREAMING_SNAKE_CASE_=0     )->				List[Any]:





													'''simple docstring'''






													if str(SCREAMING_SNAKE_CASE_     ).startswith('''mps'''     ):
																			__UpperCamelCase       					=     torch.manual_seed(SCREAMING_SNAKE_CASE_     )
													else:
																			__UpperCamelCase       					=     torch.Generator(device=SCREAMING_SNAKE_CASE_     ).manual_seed(SCREAMING_SNAKE_CASE_     )
													__UpperCamelCase       					=     {
													    '''prompt''': '''a photograph of an astronaut riding a horse''',
													    '''generator''': generator,
													    '''num_inference_steps''': 10,
													    '''guidance_scale''': 6.0,
													    '''output_type''': '''numpy''',
													    '''parallel''': 3,
													    '''debug''': True,
													}
													return inputs



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





													'''simple docstring'''






													__UpperCamelCase       					=     '''cpu'''  # ensure determinism for the device-dependent torch.Generator
													__UpperCamelCase       					=     self.get_dummy_components()
													__UpperCamelCase       					=     StableDiffusionParadigmsPipeline(**SCREAMING_SNAKE_CASE_     )
													__UpperCamelCase       					=     sd_pipe.to(SCREAMING_SNAKE_CASE_     )
													sd_pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE_     )

													__UpperCamelCase       					=     self.get_dummy_inputs(SCREAMING_SNAKE_CASE_     )
													__UpperCamelCase       					=     sd_pipe(**SCREAMING_SNAKE_CASE_     ).images
													__UpperCamelCase       					=     image[0, -3:, -3:, -1]
													assert image.shape == (1, 64, 64, 3)

													__UpperCamelCase       					=     np.array([0.4_7_7_3, 0.5_4_1_7, 0.4_7_2_3, 0.4_9_2_5, 0.5_6_3_1, 0.4_7_5_2, 0.5_2_4_0, 0.4_9_3_5, 0.5_0_2_3]     )

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



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





													'''simple docstring'''






													__UpperCamelCase       					=     '''cpu'''  # ensure determinism for the device-dependent torch.Generator
													__UpperCamelCase       					=     self.get_dummy_components()
													torch.manual_seed(0     )
													__UpperCamelCase       					=     DDPMParallelScheduler()
													torch.manual_seed(0     )
													__UpperCamelCase       					=     StableDiffusionParadigmsPipeline(**SCREAMING_SNAKE_CASE_     )
													__UpperCamelCase       					=     sd_pipe.to(SCREAMING_SNAKE_CASE_     )
													sd_pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE_     )

													__UpperCamelCase       					=     self.get_dummy_inputs(SCREAMING_SNAKE_CASE_     )
													__UpperCamelCase       					=     sd_pipe(**SCREAMING_SNAKE_CASE_     ).images
													__UpperCamelCase       					=     image[0, -3:, -3:, -1]
													assert image.shape == (1, 64, 64, 3)

													__UpperCamelCase       					=     np.array([0.3_5_7_3, 0.4_4_2_0, 0.4_9_6_0, 0.4_7_9_9, 0.3_7_9_6, 0.3_8_7_9, 0.4_8_1_9, 0.4_3_6_5, 0.4_4_6_8]     )

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



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





													'''simple docstring'''






													super().test_inference_batch_consistent(batch_sizes=[1, 2]     )



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





													'''simple docstring'''






													super().test_inference_batch_single_identical(batch_size=2       ,		expected_max_diff=3E-3     )



							def       A__						(					self     )->				Optional[int]:





													'''simple docstring'''






													__UpperCamelCase       					=     '''cpu'''  # ensure determinism for the device-dependent torch.Generator
													__UpperCamelCase       					=     self.get_dummy_components()
													__UpperCamelCase       					=     StableDiffusionParadigmsPipeline(**SCREAMING_SNAKE_CASE_     )
													__UpperCamelCase       					=     sd_pipe.to(SCREAMING_SNAKE_CASE_     )
													sd_pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE_     )

													__UpperCamelCase       					=     self.get_dummy_inputs(SCREAMING_SNAKE_CASE_     )
													__UpperCamelCase       					=     '''french fries'''
													__UpperCamelCase       					=     sd_pipe(**SCREAMING_SNAKE_CASE_       ,		negative_prompt=SCREAMING_SNAKE_CASE_     )
													__UpperCamelCase       					=     output.images
													__UpperCamelCase       					=     image[0, -3:, -3:, -1]

													assert image.shape == (1, 64, 64, 3)

													__UpperCamelCase       					=     np.array([0.4_7_7_1, 0.5_4_2_0, 0.4_6_8_3, 0.4_9_1_8, 0.5_6_3_6, 0.4_7_2_5, 0.5_2_3_0, 0.4_9_2_3, 0.5_0_1_5]     )

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






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

							"""simple docstring"""






							def       A__						(					self     )->				Any:





													'''simple docstring'''






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



							def       A__						(					self       ,		SCREAMING_SNAKE_CASE_=0     )->				Optional[Any]:





													'''simple docstring'''






													__UpperCamelCase       					=     torch.Generator(device=SCREAMING_SNAKE_CASE_     ).manual_seed(SCREAMING_SNAKE_CASE_     )
													__UpperCamelCase       					=     {
													    '''prompt''': '''a photograph of an astronaut riding a horse''',
													    '''generator''': generator,
													    '''num_inference_steps''': 10,
													    '''guidance_scale''': 7.5,
													    '''output_type''': '''numpy''',
													    '''parallel''': 3,
													    '''debug''': True,
													}
													return inputs



							def       A__						(					self     )->				str:





													'''simple docstring'''






													__UpperCamelCase       					=     '''stabilityai/stable-diffusion-2-base'''
													__UpperCamelCase       					=     DDIMParallelScheduler.from_pretrained(SCREAMING_SNAKE_CASE_       ,		subfolder='''scheduler'''     )
													__UpperCamelCase       					=     StableDiffusionParadigmsPipeline.from_pretrained(SCREAMING_SNAKE_CASE_       ,		scheduler=SCREAMING_SNAKE_CASE_       ,		safety_checker=SCREAMING_SNAKE_CASE_     )
													pipe.to(SCREAMING_SNAKE_CASE_     )
													pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE_     )
													pipe.enable_attention_slicing()

													__UpperCamelCase       					=     self.get_inputs()
													__UpperCamelCase       					=     pipe(**SCREAMING_SNAKE_CASE_     ).images
													__UpperCamelCase       					=     image[0, -3:, -3:, -1].flatten()

													assert image.shape == (1, 512, 512, 3)

													__UpperCamelCase       					=     np.array([0.9_6_2_2, 0.9_6_0_2, 0.9_7_4_8, 0.9_5_9_1, 0.9_6_3_0, 0.9_6_9_1, 0.9_6_6_1, 0.9_6_3_1, 0.9_7_4_1]     )

													assert np.abs(expected_slice - image_slice     ).max() < 1E-2




