

"""simple docstring"""





import gc
import unittest

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

from diffusers import AltDiffusionPipeline, AutoencoderKL, DDIMScheduler, PNDMScheduler, UNetaDConditionModel
from diffusers.pipelines.alt_diffusion.modeling_roberta_series import (
    RobertaSeriesConfig,
    RobertaSeriesModelWithTransformation,
)
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 PipelineKarrasSchedulerTesterMixin, PipelineLatentTesterMixin, PipelineTesterMixin


enable_full_determinism()







class 							SCREAMING_SNAKE_CASE   (						a_       ,				a_       ,				a_       ,				unittest.TestCase			):






					"""simple docstring"""



					lowercase__												=			AltDiffusionPipeline
					lowercase__												=			TEXT_TO_IMAGE_PARAMS
					lowercase__												=			TEXT_TO_IMAGE_BATCH_PARAMS
					lowercase__												=			TEXT_TO_IMAGE_IMAGE_PARAMS
					lowercase__												=			TEXT_TO_IMAGE_IMAGE_PARAMS







					def 			__lowerCAmelCase					(			self     :							str						):
						torch.manual_seed(0						)
						lowerCAmelCase__      :		Dict    							=      UNetaDConditionModel(
						    block_out_channels=(3_2, 6_4)  ,layers_per_block=2  ,sample_size=3_2  ,in_channels=4  ,out_channels=4  ,down_block_types=('''DownBlock2D''', '''CrossAttnDownBlock2D''')  ,up_block_types=('''CrossAttnUpBlock2D''', '''UpBlock2D''')  ,cross_attention_dim=3_2  ,)
						lowerCAmelCase__      :		Tuple    							=      DDIMScheduler(
						    beta_start=0.0_0085  ,beta_end=0.012  ,beta_schedule='''scaled_linear'''  ,clip_sample=lowercase_  ,set_alpha_to_one=lowercase_  ,)
						torch.manual_seed(0						)
						lowerCAmelCase__      :		str    							=      AutoencoderKL(
						    block_out_channels=[3_2, 6_4]  ,in_channels=3  ,out_channels=3  ,down_block_types=['''DownEncoderBlock2D''', '''DownEncoderBlock2D''']  ,up_block_types=['''UpDecoderBlock2D''', '''UpDecoderBlock2D''']  ,latent_channels=4  ,)

						# TODO: address the non-deterministic text encoder (fails for save-load tests)
						# torch.manual_seed(0)
						# text_encoder_config = RobertaSeriesConfig(
						#     hidden_size=32,
						#     project_dim=32,
						#     intermediate_size=37,
						#     layer_norm_eps=1e-05,
						#     num_attention_heads=4,
						#     num_hidden_layers=5,
						#     vocab_size=5002,
						# )
						# text_encoder = RobertaSeriesModelWithTransformation(text_encoder_config)

						torch.manual_seed(0						)
						lowerCAmelCase__      :		int    							=      CLIPTextConfig(
						    bos_token_id=0  ,eos_token_id=2  ,hidden_size=3_2  ,projection_dim=3_2  ,intermediate_size=3_7  ,layer_norm_eps=1E-05  ,num_attention_heads=4  ,num_hidden_layers=5  ,pad_token_id=1  ,vocab_size=5_0_0_2  ,)
						lowerCAmelCase__      :		List[str]    							=      CLIPTextModel(lowercase_						)

						lowerCAmelCase__      :		List[str]    							=      XLMRobertaTokenizer.from_pretrained('''hf-internal-testing/tiny-xlm-roberta'''						)
						lowerCAmelCase__      :		int    							=      7_7

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







					def 			__lowerCAmelCase					(			self     :							Any  ,lowercase_     :							str  ,lowercase_     :							Any=0						):
						if str(lowercase_						).startswith('''mps'''						):
							lowerCAmelCase__      :		Optional[Any]    							=      torch.manual_seed(lowercase_						)
						else:
							lowerCAmelCase__      :		List[str]    							=      torch.Generator(device=lowercase_						).manual_seed(lowercase_						)
						lowerCAmelCase__      :		int    							=      {
						    '''prompt''': '''A painting of a squirrel eating a burger''',
						    '''generator''': generator,
						    '''num_inference_steps''': 2,
						    '''guidance_scale''': 6.0,
						    '''output_type''': '''numpy''',
						}
						return inputs







					def 			__lowerCAmelCase					(			self     :							List[str]						):
						super().test_attention_slicing_forward_pass(expected_max_diff=3E-3						)







					def 			__lowerCAmelCase					(			self     :							Tuple						):
						super().test_inference_batch_single_identical(expected_max_diff=3E-3						)







					def 			__lowerCAmelCase					(			self     :							Any						):
						lowerCAmelCase__      :		Dict    							=      '''cpu'''  # ensure determinism for the device-dependent torch.Generator

						lowerCAmelCase__      :		int    							=      self.get_dummy_components()
						torch.manual_seed(0						)
						lowerCAmelCase__      :		Tuple    							=      RobertaSeriesConfig(
						    hidden_size=3_2  ,project_dim=3_2  ,intermediate_size=3_7  ,layer_norm_eps=1E-05  ,num_attention_heads=4  ,num_hidden_layers=5  ,vocab_size=5_0_0_2  ,)
						# TODO: remove after fixing the non-deterministic text encoder
						lowerCAmelCase__      :		Union[str, Any]    							=      RobertaSeriesModelWithTransformation(lowercase_						)
						lowerCAmelCase__      :		str    							=      text_encoder

						lowerCAmelCase__      :		Any    							=      AltDiffusionPipeline(**lowercase_						)
						lowerCAmelCase__      :		str    							=      alt_pipe.to(lowercase_						)
						alt_pipe.set_progress_bar_config(disable=lowercase_						)

						lowerCAmelCase__      :		Tuple    							=      self.get_dummy_inputs(lowercase_						)
						lowerCAmelCase__      :		Tuple    							=      '''A photo of an astronaut'''
						lowerCAmelCase__      :		List[Any]    							=      alt_pipe(**lowercase_						)
						lowerCAmelCase__      :		Union[str, Any]    							=      output.images
						lowerCAmelCase__      :		Dict    							=      image[0, -3:, -3:, -1]

						assert image.shape == (1, 6_4, 6_4, 3)
						lowerCAmelCase__      :		Dict    							=      np.array(
						    [0.574_8162, 0.6044_7145, 0.4882_1217, 0.5010_0636, 0.543_1185, 0.4576_3683, 0.4965_7696, 0.4813_2733, 0.4757_3093]						)

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







					def 			__lowerCAmelCase					(			self     :							Optional[Any]						):
						lowerCAmelCase__      :		Dict    							=      '''cpu'''  # ensure determinism for the device-dependent torch.Generator

						lowerCAmelCase__      :		Union[str, Any]    							=      self.get_dummy_components()
						lowerCAmelCase__      :		List[str]    							=      PNDMScheduler(skip_prk_steps=lowercase_						)
						torch.manual_seed(0						)
						lowerCAmelCase__      :		Union[str, Any]    							=      RobertaSeriesConfig(
						    hidden_size=3_2  ,project_dim=3_2  ,intermediate_size=3_7  ,layer_norm_eps=1E-05  ,num_attention_heads=4  ,num_hidden_layers=5  ,vocab_size=5_0_0_2  ,)
						# TODO: remove after fixing the non-deterministic text encoder
						lowerCAmelCase__      :		List[Any]    							=      RobertaSeriesModelWithTransformation(lowercase_						)
						lowerCAmelCase__      :		Optional[int]    							=      text_encoder
						lowerCAmelCase__      :		Any    							=      AltDiffusionPipeline(**lowercase_						)
						lowerCAmelCase__      :		List[Any]    							=      alt_pipe.to(lowercase_						)
						alt_pipe.set_progress_bar_config(disable=lowercase_						)

						lowerCAmelCase__      :		Dict    							=      self.get_dummy_inputs(lowercase_						)
						lowerCAmelCase__      :		str    							=      alt_pipe(**lowercase_						)
						lowerCAmelCase__      :		Dict    							=      output.images
						lowerCAmelCase__      :		Dict    							=      image[0, -3:, -3:, -1]

						assert image.shape == (1, 6_4, 6_4, 3)
						lowerCAmelCase__      :		Tuple    							=      np.array(
						    [0.5160_5093, 0.570_7241, 0.4736_5507, 0.5057_8886, 0.563_3877, 0.464_2503, 0.518_2081, 0.4876_3484, 0.4908_4237]						)

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







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






					"""simple docstring"""



					def 			__lowerCAmelCase					(			self     :							str						):
						# clean up the VRAM after each test
						super().tearDown()
						gc.collect()
						torch.cuda.empty_cache()







					def 			__lowerCAmelCase					(			self     :							int						):
						# make sure here that pndm scheduler skips prk
						lowerCAmelCase__      :		int    							=      AltDiffusionPipeline.from_pretrained('''BAAI/AltDiffusion'''  ,safety_checker=lowercase_						)
						lowerCAmelCase__      :		List[Any]    							=      alt_pipe.to(lowercase_						)
						alt_pipe.set_progress_bar_config(disable=lowercase_						)

						lowerCAmelCase__      :		int    							=      '''A painting of a squirrel eating a burger'''
						lowerCAmelCase__      :		Optional[Any]    							=      torch.manual_seed(0						)
						lowerCAmelCase__      :		Dict    							=      alt_pipe([prompt]  ,generator=lowercase_  ,guidance_scale=6.0  ,num_inference_steps=2_0  ,output_type='''np'''						)

						lowerCAmelCase__      :		Any    							=      output.images

						lowerCAmelCase__      :		int    							=      image[0, -3:, -3:, -1]

						assert image.shape == (1, 5_1_2, 5_1_2, 3)
						lowerCAmelCase__      :		Optional[int]    							=      np.array([0.1010, 0.0800, 0.0794, 0.0885, 0.0843, 0.0762, 0.0769, 0.0729, 0.0586]						)

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







					def 			__lowerCAmelCase					(			self     :							Union[str, Any]						):
						lowerCAmelCase__      :		Optional[Any]    							=      DDIMScheduler.from_pretrained('''BAAI/AltDiffusion'''  ,subfolder='''scheduler'''						)

						lowerCAmelCase__      :		Dict    							=      AltDiffusionPipeline.from_pretrained('''BAAI/AltDiffusion'''  ,scheduler=lowercase_  ,safety_checker=lowercase_						)
						lowerCAmelCase__      :		Optional[int]    							=      alt_pipe.to(lowercase_						)
						alt_pipe.set_progress_bar_config(disable=lowercase_						)

						lowerCAmelCase__      :		List[Any]    							=      '''A painting of a squirrel eating a burger'''
						lowerCAmelCase__      :		Union[str, Any]    							=      torch.manual_seed(0						)

						lowerCAmelCase__      :		str    							=      alt_pipe([prompt]  ,generator=lowercase_  ,num_inference_steps=2  ,output_type='''numpy'''						)
						lowerCAmelCase__      :		Optional[Any]    							=      output.images

						lowerCAmelCase__      :		str    							=      image[0, -3:, -3:, -1]

						assert image.shape == (1, 5_1_2, 5_1_2, 3)
						lowerCAmelCase__      :		Union[str, Any]    							=      np.array([0.4019, 0.4052, 0.3810, 0.4119, 0.3916, 0.3982, 0.4651, 0.4195, 0.5323]						)

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







"""simple docstring"""





import unittest

import numpy as np

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

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs


if is_torch_available():
							import torch

if is_vision_available():
							from PIL import Image

							from transformers import LevitImageProcessor







class 							SCREAMING_SNAKE_CASE   (						unittest.TestCase			):






					"""simple docstring"""



					def __init__(			self     :							Any  ,lowercase_     :							Dict  ,lowercase_     :							List[str]=7  ,lowercase_     :							Tuple=3  ,lowercase_     :							List[str]=1_8  ,lowercase_     :							Optional[Any]=3_0  ,lowercase_     :							List[Any]=4_0_0  ,lowercase_     :							List[Any]=True  ,lowercase_     :							Any=None  ,lowercase_     :							Optional[Any]=True  ,lowercase_     :							str=None  ,lowercase_     :							List[Any]=True  ,lowercase_     :							Dict=[0.5, 0.5, 0.5]  ,lowercase_     :							Dict=[0.5, 0.5, 0.5]  ,):
						lowerCAmelCase__      :		Any    							=      size if size is not None else {'''shortest_edge''': 1_8}
						lowerCAmelCase__      :		Union[str, Any]    							=      crop_size if crop_size is not None else {'''height''': 1_8, '''width''': 1_8}
						lowerCAmelCase__      :		Dict    							=      parent
						lowerCAmelCase__      :		Dict    							=      batch_size
						lowerCAmelCase__      :		List[str]    							=      num_channels
						lowerCAmelCase__      :		Any    							=      image_size
						lowerCAmelCase__      :		Union[str, Any]    							=      min_resolution
						lowerCAmelCase__      :		Dict    							=      max_resolution
						lowerCAmelCase__      :		List[str]    							=      do_resize
						lowerCAmelCase__      :		Optional[Any]    							=      size
						lowerCAmelCase__      :		Tuple    							=      do_center_crop
						lowerCAmelCase__      :		Optional[int]    							=      crop_size
						lowerCAmelCase__      :		List[str]    							=      do_normalize
						lowerCAmelCase__      :		Tuple    							=      image_mean
						lowerCAmelCase__      :		int    							=      image_std







					def 			__lowerCAmelCase					(			self     :							List[str]						):
						return {
						    "image_mean": self.image_mean,
						    "image_std": self.image_std,
						    "do_normalize": self.do_normalize,
						    "do_resize": self.do_resize,
						    "do_center_crop": self.do_center_crop,
						    "size": self.size,
						    "crop_size": self.crop_size,
						}







@require_torch
@require_vision
class 							SCREAMING_SNAKE_CASE   (						a_       ,				unittest.TestCase			):






					"""simple docstring"""



					lowercase__												=			LevitImageProcessor if is_vision_available() else None







					def 			__lowerCAmelCase					(			self     :							List[str]						):
						lowerCAmelCase__      :		Optional[Any]    							=      LevitImageProcessingTester(self						)







					@property
					def 			__lowerCAmelCase					(			self     :							Any						):
						return self.image_processor_tester.prepare_image_processor_dict()







					def 			__lowerCAmelCase					(			self     :							str						):
						lowerCAmelCase__      :		Optional[int]    							=      self.image_processing_class(**self.image_processor_dict						)
						self.assertTrue(hasattr(lowercase_  ,'''image_mean'''						)						)
						self.assertTrue(hasattr(lowercase_  ,'''image_std'''						)						)
						self.assertTrue(hasattr(lowercase_  ,'''do_normalize'''						)						)
						self.assertTrue(hasattr(lowercase_  ,'''do_resize'''						)						)
						self.assertTrue(hasattr(lowercase_  ,'''do_center_crop'''						)						)
						self.assertTrue(hasattr(lowercase_  ,'''size'''						)						)







					def 			__lowerCAmelCase					(			self     :							Any						):
						lowerCAmelCase__      :		int    							=      self.image_processing_class.from_dict(self.image_processor_dict						)
						self.assertEqual(image_processor.size  ,{'''shortest_edge''': 1_8}						)
						self.assertEqual(image_processor.crop_size  ,{'''height''': 1_8, '''width''': 1_8}						)

						lowerCAmelCase__      :		List[str]    							=      self.image_processing_class.from_dict(self.image_processor_dict  ,size=4_2  ,crop_size=8_4						)
						self.assertEqual(image_processor.size  ,{'''shortest_edge''': 4_2}						)
						self.assertEqual(image_processor.crop_size  ,{'''height''': 8_4, '''width''': 8_4}						)







					def 			__lowerCAmelCase					(			self     :							Union[str, Any]						):
						pass







					def 			__lowerCAmelCase					(			self     :							Optional[int]						):
						# Initialize image_processing
						lowerCAmelCase__      :		List[str]    							=      self.image_processing_class(**self.image_processor_dict						)
						# create random PIL images
						lowerCAmelCase__      :		Union[str, Any]    							=      prepare_image_inputs(self.image_processor_tester  ,equal_resolution=lowercase_						)
						for image in image_inputs:
							self.assertIsInstance(lowercase_  ,Image.Image						)

						# Test not batched input
						lowerCAmelCase__      :		Any    							=      image_processing(image_inputs[0]  ,return_tensors='''pt'''						).pixel_values
						self.assertEqual(
						    encoded_images.shape  ,(
						        1,
						        self.image_processor_tester.num_channels,
						        self.image_processor_tester.crop_size['''height'''],
						        self.image_processor_tester.crop_size['''width'''],
						    )  ,)

						# Test batched
						lowerCAmelCase__      :		Optional[int]    							=      image_processing(lowercase_  ,return_tensors='''pt'''						).pixel_values
						self.assertEqual(
						    encoded_images.shape  ,(
						        self.image_processor_tester.batch_size,
						        self.image_processor_tester.num_channels,
						        self.image_processor_tester.crop_size['''height'''],
						        self.image_processor_tester.crop_size['''width'''],
						    )  ,)







					def 			__lowerCAmelCase					(			self     :							Union[str, Any]						):
						# Initialize image_processing
						lowerCAmelCase__      :		str    							=      self.image_processing_class(**self.image_processor_dict						)
						# create random numpy tensors
						lowerCAmelCase__      :		Optional[Any]    							=      prepare_image_inputs(self.image_processor_tester  ,equal_resolution=lowercase_  ,numpify=lowercase_						)
						for image in image_inputs:
							self.assertIsInstance(lowercase_  ,np.ndarray						)

						# Test not batched input
						lowerCAmelCase__      :		Tuple    							=      image_processing(image_inputs[0]  ,return_tensors='''pt'''						).pixel_values
						self.assertEqual(
						    encoded_images.shape  ,(
						        1,
						        self.image_processor_tester.num_channels,
						        self.image_processor_tester.crop_size['''height'''],
						        self.image_processor_tester.crop_size['''width'''],
						    )  ,)

						# Test batched
						lowerCAmelCase__      :		Union[str, Any]    							=      image_processing(lowercase_  ,return_tensors='''pt'''						).pixel_values
						self.assertEqual(
						    encoded_images.shape  ,(
						        self.image_processor_tester.batch_size,
						        self.image_processor_tester.num_channels,
						        self.image_processor_tester.crop_size['''height'''],
						        self.image_processor_tester.crop_size['''width'''],
						    )  ,)







					def 			__lowerCAmelCase					(			self     :							int						):
						# Initialize image_processing
						lowerCAmelCase__      :		Tuple    							=      self.image_processing_class(**self.image_processor_dict						)
						# create random PyTorch tensors
						lowerCAmelCase__      :		Dict    							=      prepare_image_inputs(self.image_processor_tester  ,equal_resolution=lowercase_  ,torchify=lowercase_						)
						for image in image_inputs:
							self.assertIsInstance(lowercase_  ,torch.Tensor						)

						# Test not batched input
						lowerCAmelCase__      :		Dict    							=      image_processing(image_inputs[0]  ,return_tensors='''pt'''						).pixel_values
						self.assertEqual(
						    encoded_images.shape  ,(
						        1,
						        self.image_processor_tester.num_channels,
						        self.image_processor_tester.crop_size['''height'''],
						        self.image_processor_tester.crop_size['''width'''],
						    )  ,)

						# Test batched
						lowerCAmelCase__      :		Optional[int]    							=      image_processing(lowercase_  ,return_tensors='''pt'''						).pixel_values
						self.assertEqual(
						    encoded_images.shape  ,(
						        self.image_processor_tester.batch_size,
						        self.image_processor_tester.num_channels,
						        self.image_processor_tester.crop_size['''height'''],
						        self.image_processor_tester.crop_size['''width'''],
						    )  ,)




