





"""simple docstring"""


import shutil
import tempfile
import unittest

import numpy as np
import pytest

from transformers import is_speech_available, is_vision_available
from transformers.testing_utils import require_torch


if is_vision_available():
		from transformers import TvltImageProcessor

if is_speech_available():
		from transformers import TvltFeatureExtractor

from transformers import TvltProcessor






@require_torch
class 							lowercase__			(   unittest.TestCase	):

			"""simple docstring"""







			def   _a      (      self    ):






									'''simple docstring'''







									UpperCamelCase				:		List[str]				   =			"""ZinengTang/tvlt-base"""
									UpperCamelCase				:		Union[str, Any]				   =			tempfile.mkdtemp()







			def   _a      (      self						,   **_A    ):






									'''simple docstring'''







									return TvltImageProcessor.from_pretrained(self.checkpoint						,   **_A    )







			def   _a      (      self						,   **_A    ):






									'''simple docstring'''







									return TvltFeatureExtractor.from_pretrained(self.checkpoint						,   **_A    )







			def   _a      (      self    ):






									'''simple docstring'''







									shutil.rmtree(self.tmpdirname    )







			def   _a      (      self    ):






									'''simple docstring'''







									UpperCamelCase				:		str				   =			self.get_image_processor()
									UpperCamelCase				:		Any				   =			self.get_feature_extractor()

									UpperCamelCase				:		str				   =			TvltProcessor(image_processor=_A						,   feature_extractor=_A    )
									processor.save_pretrained(self.tmpdirname    )
									UpperCamelCase				:		List[Any]				   =			TvltProcessor.from_pretrained(self.tmpdirname    )

									self.assertIsInstance(processor.feature_extractor						,   _A    )
									self.assertIsInstance(processor.image_processor						,   _A    )







			def   _a      (      self    ):






									'''simple docstring'''







									UpperCamelCase				:		int				   =			self.get_image_processor()
									UpperCamelCase				:		Optional[Any]				   =			self.get_feature_extractor()

									UpperCamelCase				:		Optional[int]				   =			TvltProcessor(image_processor=_A						,   feature_extractor=_A    )

									UpperCamelCase				:		Tuple				   =			np.ones([1_2_0_0_0]    )

									UpperCamelCase				:		Union[str, Any]				   =			feature_extractor(_A						,   return_tensors="""np"""    )
									UpperCamelCase				:		List[str]				   =			processor(audio=_A						,   return_tensors="""np"""    )

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







			def   _a      (      self    ):






									'''simple docstring'''







									UpperCamelCase				:		Union[str, Any]				   =			self.get_image_processor()
									UpperCamelCase				:		Tuple				   =			self.get_feature_extractor()

									UpperCamelCase				:		Optional[Any]				   =			TvltProcessor(image_processor=_A						,   feature_extractor=_A    )

									UpperCamelCase				:		List[Any]				   =			np.ones([3, 2_2_4, 2_2_4]    )

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

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







			def   _a      (      self    ):






									'''simple docstring'''







									UpperCamelCase				:		List[Any]				   =			self.get_image_processor()
									UpperCamelCase				:		Tuple				   =			self.get_feature_extractor()

									UpperCamelCase				:		Union[str, Any]				   =			TvltProcessor(image_processor=_A						,   feature_extractor=_A    )

									UpperCamelCase				:		Tuple				   =			np.ones([1_2_0_0_0]    )
									UpperCamelCase				:		Optional[int]				   =			np.ones([3, 2_2_4, 2_2_4]    )

									UpperCamelCase				:		Optional[int]				   =			processor(audio=_A						,   images=_A    )

									self.assertListEqual(list(inputs.keys()    )						,   ["""audio_values""", """audio_mask""", """pixel_values""", """pixel_mask"""]    )

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







			def   _a      (      self    ):






									'''simple docstring'''







									UpperCamelCase				:		str				   =			self.get_image_processor()
									UpperCamelCase				:		Dict				   =			self.get_feature_extractor()

									UpperCamelCase				:		List[Any]				   =			TvltProcessor(image_processor=_A						,   feature_extractor=_A    )

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






import gc
import unittest

import numpy as np
import torch

from diffusers import StableDiffusionKDiffusionPipeline
from diffusers.utils import slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu


enable_full_determinism()





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



       '''simple docstring'''







       def     __UpperCamelCase     (							self     ):







             '''simple docstring'''



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

       def     __UpperCamelCase     (							self     ):







             '''simple docstring'''



             __A    =StableDiffusionKDiffusionPipeline.from_pretrained('''CompVis/stable-diffusion-v1-4'''     )
             __A    =sd_pipe.to(lowercase__     )
             sd_pipe.set_progress_bar_config(disable=lowercase__     )

             sd_pipe.set_scheduler('''sample_euler'''     )

             __A    ='''A painting of a squirrel eating a burger'''
             __A    =torch.manual_seed(0     )
             __A    =sd_pipe([prompt] ,   generator=lowercase__ ,   guidance_scale=9.0 ,   num_inference_steps=2_0 ,   output_type='''np'''     )

             __A    =output.images

             __A    =image[0, -3:, -3:, -1]

             assert image.shape == (1, 5_1_2, 5_1_2, 3)
             __A    =np.array([0.0447, 0.0492, 0.0468, 0.0408, 0.0383, 0.0408, 0.0354, 0.0380, 0.0339]     )

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

       def     __UpperCamelCase     (							self     ):







             '''simple docstring'''



             __A    =StableDiffusionKDiffusionPipeline.from_pretrained('''stabilityai/stable-diffusion-2-1-base'''     )
             __A    =sd_pipe.to(lowercase__     )
             sd_pipe.set_progress_bar_config(disable=lowercase__     )

             sd_pipe.set_scheduler('''sample_euler'''     )

             __A    ='''A painting of a squirrel eating a burger'''
             __A    =torch.manual_seed(0     )
             __A    =sd_pipe([prompt] ,   generator=lowercase__ ,   guidance_scale=9.0 ,   num_inference_steps=2_0 ,   output_type='''np'''     )

             __A    =output.images

             __A    =image[0, -3:, -3:, -1]

             assert image.shape == (1, 5_1_2, 5_1_2, 3)
             __A    =np.array([0.1237, 0.1320, 0.1438, 0.1359, 0.1390, 0.1132, 0.1277, 0.1175, 0.1112]     )

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


       def     __UpperCamelCase     (							self     ):







             '''simple docstring'''



             __A    =StableDiffusionKDiffusionPipeline.from_pretrained('''stabilityai/stable-diffusion-2-1-base'''     )
             __A    =sd_pipe.to(lowercase__     )
             sd_pipe.set_progress_bar_config(disable=lowercase__     )

             sd_pipe.set_scheduler('''sample_dpmpp_2m'''     )

             __A    ='''A painting of a squirrel eating a burger'''
             __A    =torch.manual_seed(0     )
             __A    =sd_pipe(
                 [prompt] ,   generator=lowercase__ ,   guidance_scale=7.5 ,   num_inference_steps=1_5 ,   output_type='''np''' ,   use_karras_sigmas=lowercase__ ,   )

             __A    =output.images

             __A    =image[0, -3:, -3:, -1]

             assert image.shape == (1, 5_1_2, 5_1_2, 3)
             __A    =np.array(
                 [0.1138_1689, 0.1211_2921, 0.138_9457, 0.1254_9606, 0.124_4964, 0.1083_1517, 0.1156_2866, 0.1086_7816, 0.1049_9048]     )

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


