



'''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 		lowerCamelCase__      (						A	,   A	,   A	,   unittest.TestCase     ):




					'''simple docstring'''



					A_						    =						AltDiffusionPipeline
					A_						    =						TEXT_TO_IMAGE_PARAMS
					A_						    =						TEXT_TO_IMAGE_BATCH_PARAMS
					A_						    =						TEXT_TO_IMAGE_IMAGE_PARAMS
					A_						    =						TEXT_TO_IMAGE_IMAGE_PARAMS




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



						'''simple docstring'''





						torch.manual_seed(0					)
						_lowercase					:       Optional[int]       =		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     ,  )
						_lowercase					:       Optional[Any]       =		DDIMScheduler(
						    beta_start=0.0_00_85     ,  beta_end=0.0_12     ,  beta_schedule='scaled_linear'     ,  clip_sample=UpperCamelCase_     ,  set_alpha_to_one=UpperCamelCase_     ,  )
						torch.manual_seed(0					)
						_lowercase					:       Optional[int]       =		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     ,  )

						# 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					)
						_lowercase					:       Any       =		CLIPTextConfig(
						    bos_token_id=0     ,  eos_token_id=2     ,  hidden_size=32     ,  projection_dim=32     ,  intermediate_size=37     ,  layer_norm_eps=1E-05     ,  num_attention_heads=4     ,  num_hidden_layers=5     ,  pad_token_id=1     ,  vocab_size=5002     ,  )
						_lowercase					:       str       =		CLIPTextModel(UpperCamelCase_					)

						_lowercase					:       str       =		XLMRobertaTokenizer.from_pretrained('hf-internal-testing/tiny-xlm-roberta'					)
						_lowercase					:       Union[str, Any]       =		77

						_lowercase					:       Union[str, Any]       =		{
						    'unet': unet,
						    'scheduler': scheduler,
						    'vae': vae,
						    'text_encoder': text_encoder,
						    'tokenizer': tokenizer,
						    'safety_checker': None,
						    'feature_extractor': None,
						}
						return components




					def 						__UpperCAmelCase     (  self				:	Optional[int]     ,  UpperCamelCase_				:	Optional[Any]     ,  UpperCamelCase_				:	List[Any]=0					)  ->       Optional[int]:



						'''simple docstring'''





						if str(UpperCamelCase_					).startswith('mps'					):
							_lowercase					:       Union[str, Any]       =		torch.manual_seed(UpperCamelCase_					)
						else:
							_lowercase					:       Any       =		torch.Generator(device=UpperCamelCase_					).manual_seed(UpperCamelCase_					)
						_lowercase					:       List[Any]       =		{
						    '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 						__UpperCAmelCase     (  self				:	List[str]					)  ->       int:



						'''simple docstring'''





						super().test_attention_slicing_forward_pass(expected_max_diff=3E-3					)




					def 						__UpperCAmelCase     (  self				:	Optional[int]					)  ->       Any:



						'''simple docstring'''





						super().test_inference_batch_single_identical(expected_max_diff=3E-3					)




					def 						__UpperCAmelCase     (  self				:	Optional[Any]					)  ->       Dict:



						'''simple docstring'''





						_lowercase					:       Dict       =		'cpu'  # ensure determinism for the device-dependent torch.Generator

						_lowercase					:       List[Any]       =		self.get_dummy_components()
						torch.manual_seed(0					)
						_lowercase					:       List[Any]       =		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     ,  )
						# TODO: remove after fixing the non-deterministic text encoder
						_lowercase					:       Optional[Any]       =		RobertaSeriesModelWithTransformation(UpperCamelCase_					)
						_lowercase					:       str       =		text_encoder

						_lowercase					:       str       =		AltDiffusionPipeline(**UpperCamelCase_					)
						_lowercase					:       List[Any]       =		alt_pipe.to(UpperCamelCase_					)
						alt_pipe.set_progress_bar_config(disable=UpperCamelCase_					)

						_lowercase					:       Any       =		self.get_dummy_inputs(UpperCamelCase_					)
						_lowercase					:       int       =		'A photo of an astronaut'
						_lowercase					:       Optional[int]       =		alt_pipe(**UpperCamelCase_					)
						_lowercase					:       List[str]       =		output.images
						_lowercase					:       Dict       =		image[0, -3:, -3:, -1]

						assert image.shape == (1, 64, 64, 3)
						_lowercase					:       Union[str, Any]       =		np.array(
						    [0.5_74_81_62, 0.60_44_71_45, 0.48_82_12_17, 0.50_10_06_36, 0.5_43_11_85, 0.45_76_36_83, 0.49_65_76_96, 0.48_13_27_33, 0.47_57_30_93]					)

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




					def 						__UpperCAmelCase     (  self				:	Optional[int]					)  ->       List[str]:



						'''simple docstring'''





						_lowercase					:       List[Any]       =		'cpu'  # ensure determinism for the device-dependent torch.Generator

						_lowercase					:       List[Any]       =		self.get_dummy_components()
						_lowercase					:       int       =		PNDMScheduler(skip_prk_steps=UpperCamelCase_					)
						torch.manual_seed(0					)
						_lowercase					:       Dict       =		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     ,  )
						# TODO: remove after fixing the non-deterministic text encoder
						_lowercase					:       Optional[int]       =		RobertaSeriesModelWithTransformation(UpperCamelCase_					)
						_lowercase					:       Tuple       =		text_encoder
						_lowercase					:       str       =		AltDiffusionPipeline(**UpperCamelCase_					)
						_lowercase					:       List[Any]       =		alt_pipe.to(UpperCamelCase_					)
						alt_pipe.set_progress_bar_config(disable=UpperCamelCase_					)

						_lowercase					:       Dict       =		self.get_dummy_inputs(UpperCamelCase_					)
						_lowercase					:       List[str]       =		alt_pipe(**UpperCamelCase_					)
						_lowercase					:       Any       =		output.images
						_lowercase					:       int       =		image[0, -3:, -3:, -1]

						assert image.shape == (1, 64, 64, 3)
						_lowercase					:       str       =		np.array(
						    [0.51_60_50_93, 0.5_70_72_41, 0.47_36_55_07, 0.50_57_88_86, 0.5_63_38_77, 0.4_64_25_03, 0.5_18_20_81, 0.48_76_34_84, 0.49_08_42_37]					)

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






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




					'''simple docstring'''



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



						'''simple docstring'''





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




					def 						__UpperCAmelCase     (  self				:	str					)  ->       int:



						'''simple docstring'''





						_lowercase					:       int       =		AltDiffusionPipeline.from_pretrained('BAAI/AltDiffusion'     ,  safety_checker=UpperCamelCase_					)
						_lowercase					:       List[Any]       =		alt_pipe.to(UpperCamelCase_					)
						alt_pipe.set_progress_bar_config(disable=UpperCamelCase_					)

						_lowercase					:       Any       =		'A painting of a squirrel eating a burger'
						_lowercase					:       int       =		torch.manual_seed(0					)
						_lowercase					:       Dict       =		alt_pipe([prompt]     ,  generator=UpperCamelCase_     ,  guidance_scale=6.0     ,  num_inference_steps=20     ,  output_type='np'					)

						_lowercase					:       List[Any]       =		output.images

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

						assert image.shape == (1, 512, 512, 3)
						_lowercase					:       Optional[int]       =		np.array([0.10_10, 0.08_00, 0.07_94, 0.08_85, 0.08_43, 0.07_62, 0.07_69, 0.07_29, 0.05_86]					)

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




					def 						__UpperCAmelCase     (  self				:	List[str]					)  ->       int:



						'''simple docstring'''





						_lowercase					:       Union[str, Any]       =		DDIMScheduler.from_pretrained('BAAI/AltDiffusion'     ,  subfolder='scheduler'					)

						_lowercase					:       Optional[Any]       =		AltDiffusionPipeline.from_pretrained('BAAI/AltDiffusion'     ,  scheduler=UpperCamelCase_     ,  safety_checker=UpperCamelCase_					)
						_lowercase					:       str       =		alt_pipe.to(UpperCamelCase_					)
						alt_pipe.set_progress_bar_config(disable=UpperCamelCase_					)

						_lowercase					:       int       =		'A painting of a squirrel eating a burger'
						_lowercase					:       Any       =		torch.manual_seed(0					)

						_lowercase					:       Optional[Any]       =		alt_pipe([prompt]     ,  generator=UpperCamelCase_     ,  num_inference_steps=2     ,  output_type='numpy'					)
						_lowercase					:       List[Any]       =		output.images

						_lowercase					:       List[str]       =		image[0, -3:, -3:, -1]

						assert image.shape == (1, 512, 512, 3)
						_lowercase					:       str       =		np.array([0.40_19, 0.40_52, 0.38_10, 0.41_19, 0.39_16, 0.39_82, 0.46_51, 0.41_95, 0.53_23]					)

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





'''simple docstring'''

import unittest

from transformers import AutoTokenizer, is_flax_available
from transformers.testing_utils import require_flax, require_sentencepiece, require_tokenizers, slow


if is_flax_available():
		import jax.numpy as jnp

		from transformers import FlaxXLMRobertaModel



@require_sentencepiece
@require_tokenizers
@require_flax
class 		lowerCamelCase__      (						unittest.TestCase     ):




					'''simple docstring'''



					@slow
					def 						__UpperCAmelCase     (  self				:	int					)  ->       Union[str, Any]:



						'''simple docstring'''





						_lowercase					:       List[Any]       =		FlaxXLMRobertaModel.from_pretrained('xlm-roberta-base'					)
						_lowercase					:       str       =		AutoTokenizer.from_pretrained('xlm-roberta-base'					)
						_lowercase					:       List[Any]       =		'The dog is cute and lives in the garden house'
						_lowercase					:       Optional[int]       =		jnp.array([tokenizer.encode(UpperCamelCase_					)]					)

						_lowercase					:       int       =		(1, 12, 768)  # batch_size, sequence_length, embedding_vector_dim
						_lowercase					:       Tuple       =		jnp.array(
						    [[-0.01_01, 0.12_18, -0.08_03, 0.08_01, 0.13_27, 0.07_76, -0.12_15, 0.23_83, 0.33_38, 0.31_06, 0.03_00, 0.02_52]]					)

						_lowercase					:       List[str]       =		model(UpperCamelCase_					)['last_hidden_state']
						self.assertEqual(output.shape     ,  UpperCamelCase_					)
						# compare the actual values for a slice of last dim
						self.assertTrue(jnp.allclose(output[:, :, -1]     ,  UpperCamelCase_     ,  atol=1E-3					)					)
