


"""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        __A       (snake_case__  ,						snake_case__  ,						snake_case__  ,						unittest.TestCase):





							'''simple docstring'''

							__lowercase:						Tuple							 =      AltDiffusionPipeline
							__lowercase:						Optional[int]							 =      TEXT_TO_IMAGE_PARAMS
							__lowercase:						List[Any]							 =      TEXT_TO_IMAGE_BATCH_PARAMS
							__lowercase:						List[Any]							 =      TEXT_TO_IMAGE_IMAGE_PARAMS
							__lowercase:						Union[str, Any]							 =      TEXT_TO_IMAGE_IMAGE_PARAMS



							def 						lowerCAmelCase    (			self							: Union[str, Any]    )   ->Optional[int]:



														"""simple docstring"""



														torch.manual_seed(0    )
														snake_case_          =						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						,    )
														snake_case_          =						DDIMScheduler(
														    beta_start=0.00_085						,    beta_end=0.012						,    beta_schedule="""scaled_linear"""						,    clip_sample=UpperCAmelCase_						,    set_alpha_to_one=UpperCAmelCase_						,    )
														torch.manual_seed(0    )
														snake_case_          =						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    )
														snake_case_          =						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=5_002						,    )
														snake_case_          =						CLIPTextModel(UpperCAmelCase_    )

														snake_case_          =						XLMRobertaTokenizer.from_pretrained("""hf-internal-testing/tiny-xlm-roberta"""    )
														snake_case_          =						77

														snake_case_          =						{
														    """unet""": unet,
														    """scheduler""": scheduler,
														    """vae""": vae,
														    """text_encoder""": text_encoder,
														    """tokenizer""": tokenizer,
														    """safety_checker""": None,
														    """feature_extractor""": None,
														}
														return components



							def 						lowerCAmelCase    (			self							: int						,    UpperCAmelCase_							: Any						,    UpperCAmelCase_							: Optional[int]=0    )   ->Union[str, Any]:



														"""simple docstring"""



														if str(UpperCAmelCase_    ).startswith("""mps"""    ):
																					snake_case_          =						torch.manual_seed(UpperCAmelCase_    )
														else:
																					snake_case_          =						torch.Generator(device=UpperCAmelCase_    ).manual_seed(UpperCAmelCase_    )
														snake_case_          =						{
														    """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							: Dict    )   ->List[Any]:



														"""simple docstring"""



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



							def 						lowerCAmelCase    (			self							: str    )   ->int:



														"""simple docstring"""



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



							def 						lowerCAmelCase    (			self							: Tuple    )   ->List[Any]:



														"""simple docstring"""



														snake_case_          =						"""cpu"""  # ensure determinism for the device-dependent torch.Generator

														snake_case_          =						self.get_dummy_components()
														torch.manual_seed(0    )
														snake_case_          =						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=5_002						,    )
														# TODO: remove after fixing the non-deterministic text encoder
														snake_case_          =						RobertaSeriesModelWithTransformation(UpperCAmelCase_    )
														snake_case_          =						text_encoder

														snake_case_          =						AltDiffusionPipeline(**UpperCAmelCase_    )
														snake_case_          =						alt_pipe.to(UpperCAmelCase_    )
														alt_pipe.set_progress_bar_config(disable=UpperCAmelCase_    )

														snake_case_          =						self.get_dummy_inputs(UpperCAmelCase_    )
														snake_case_          =						"""A photo of an astronaut"""
														snake_case_          =						alt_pipe(**UpperCAmelCase_    )
														snake_case_          =						output.images
														snake_case_          =						image[0, -3:, -3:, -1]

														assert image.shape == (1, 64, 64, 3)
														snake_case_          =						np.array(
														    [0.5_748_162, 0.60_447_145, 0.48_821_217, 0.50_100_636, 0.5_431_185, 0.45_763_683, 0.49_657_696, 0.48_132_733, 0.47_573_093]    )

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



							def 						lowerCAmelCase    (			self							: Union[str, Any]    )   ->Optional[int]:



														"""simple docstring"""



														snake_case_          =						"""cpu"""  # ensure determinism for the device-dependent torch.Generator

														snake_case_          =						self.get_dummy_components()
														snake_case_          =						PNDMScheduler(skip_prk_steps=UpperCAmelCase_    )
														torch.manual_seed(0    )
														snake_case_          =						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=5_002						,    )
														# TODO: remove after fixing the non-deterministic text encoder
														snake_case_          =						RobertaSeriesModelWithTransformation(UpperCAmelCase_    )
														snake_case_          =						text_encoder
														snake_case_          =						AltDiffusionPipeline(**UpperCAmelCase_    )
														snake_case_          =						alt_pipe.to(UpperCAmelCase_    )
														alt_pipe.set_progress_bar_config(disable=UpperCAmelCase_    )

														snake_case_          =						self.get_dummy_inputs(UpperCAmelCase_    )
														snake_case_          =						alt_pipe(**UpperCAmelCase_    )
														snake_case_          =						output.images
														snake_case_          =						image[0, -3:, -3:, -1]

														assert image.shape == (1, 64, 64, 3)
														snake_case_          =						np.array(
														    [0.51_605_093, 0.5_707_241, 0.47_365_507, 0.50_578_886, 0.5_633_877, 0.4_642_503, 0.5_182_081, 0.48_763_484, 0.49_084_237]    )

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





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





							'''simple docstring'''

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



														"""simple docstring"""



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



							def 						lowerCAmelCase    (			self							: str    )   ->List[Any]:



														"""simple docstring"""



														snake_case_          =						AltDiffusionPipeline.from_pretrained("""BAAI/AltDiffusion"""						,    safety_checker=UpperCAmelCase_    )
														snake_case_          =						alt_pipe.to(UpperCAmelCase_    )
														alt_pipe.set_progress_bar_config(disable=UpperCAmelCase_    )

														snake_case_          =						"""A painting of a squirrel eating a burger"""
														snake_case_          =						torch.manual_seed(0    )
														snake_case_          =						alt_pipe([prompt]						,    generator=UpperCAmelCase_						,    guidance_scale=6.0						,    num_inference_steps=20						,    output_type="""np"""    )

														snake_case_          =						output.images

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

														assert image.shape == (1, 512, 512, 3)
														snake_case_          =						np.array([0.1_010, 0.0_800, 0.0_794, 0.0_885, 0.0_843, 0.0_762, 0.0_769, 0.0_729, 0.0_586]    )

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



							def 						lowerCAmelCase    (			self							: str    )   ->List[Any]:



														"""simple docstring"""



														snake_case_          =						DDIMScheduler.from_pretrained("""BAAI/AltDiffusion"""						,    subfolder="""scheduler"""    )

														snake_case_          =						AltDiffusionPipeline.from_pretrained("""BAAI/AltDiffusion"""						,    scheduler=UpperCAmelCase_						,    safety_checker=UpperCAmelCase_    )
														snake_case_          =						alt_pipe.to(UpperCAmelCase_    )
														alt_pipe.set_progress_bar_config(disable=UpperCAmelCase_    )

														snake_case_          =						"""A painting of a squirrel eating a burger"""
														snake_case_          =						torch.manual_seed(0    )

														snake_case_          =						alt_pipe([prompt]						,    generator=UpperCAmelCase_						,    num_inference_steps=2						,    output_type="""numpy"""    )
														snake_case_          =						output.images

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

														assert image.shape == (1, 512, 512, 3)
														snake_case_          =						np.array([0.4_019, 0.4_052, 0.3_810, 0.4_119, 0.3_916, 0.3_982, 0.4_651, 0.4_195, 0.5_323]    )

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







"""simple docstring"""
import re
import warnings
from contextlib import contextmanager

from ...processing_utils import ProcessorMixin





class        __A       (snake_case__):





							'''simple docstring'''

							__lowercase:						Any							 =      ["""image_processor""", """tokenizer"""]
							__lowercase:						Dict							 =      """AutoImageProcessor"""
							__lowercase:						List[Any]							 =      """AutoTokenizer"""



							def __init__(			self							: Tuple						,    UpperCAmelCase_							: int=None						,    UpperCAmelCase_							: Tuple=None						,    **UpperCAmelCase_							: str    )   ->Optional[int]:



														"""simple docstring"""



														snake_case_          =						None
														if "feature_extractor" in kwargs:
																					warnings.warn(
																					    """The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`"""
																					    """ instead."""						,    UpperCAmelCase_						,    )
																					snake_case_          =						kwargs.pop("""feature_extractor"""    )

														snake_case_          =						image_processor if image_processor is not None else feature_extractor
														if image_processor is None:
																					raise ValueError("""You need to specify an `image_processor`."""    )
														if tokenizer is None:
																					raise ValueError("""You need to specify a `tokenizer`."""    )

														super().__init__(UpperCAmelCase_						,    UpperCAmelCase_    )
														snake_case_          =						self.image_processor
														snake_case_          =						False



							def __call__(			self							: Any						,    *UpperCAmelCase_							: Optional[int]						,    **UpperCAmelCase_							: str    )   ->List[str]:



														"""simple docstring"""



														if self._in_target_context_manager:
																					return self.current_processor(*UpperCAmelCase_						,    **UpperCAmelCase_    )

														snake_case_          =						kwargs.pop("""images"""						,    UpperCAmelCase_    )
														snake_case_          =						kwargs.pop("""text"""						,    UpperCAmelCase_    )
														if len(UpperCAmelCase_    ) > 0:
																					snake_case_          =						args[0]
																					snake_case_          =						args[1:]

														if images is None and text is None:
																					raise ValueError("""You need to specify either an `images` or `text` input to process."""    )

														if images is not None:
																					snake_case_          =						self.image_processor(UpperCAmelCase_						,    *UpperCAmelCase_						,    **UpperCAmelCase_    )
														if text is not None:
																					snake_case_          =						self.tokenizer(UpperCAmelCase_						,    **UpperCAmelCase_    )

														if text is None:
																					return inputs
														elif images is None:
																					return encodings
														else:
																					snake_case_          =						encodings["""input_ids"""]
																					return inputs



							def 						lowerCAmelCase    (			self							: Optional[Any]						,    *UpperCAmelCase_							: Optional[Any]						,    **UpperCAmelCase_							: Tuple    )   ->Optional[int]:



														"""simple docstring"""



														return self.tokenizer.batch_decode(*UpperCAmelCase_						,    **UpperCAmelCase_    )



							def 						lowerCAmelCase    (			self							: Optional[int]						,    *UpperCAmelCase_							: Union[str, Any]						,    **UpperCAmelCase_							: List[Any]    )   ->str:



														"""simple docstring"""



														return self.tokenizer.decode(*UpperCAmelCase_						,    **UpperCAmelCase_    )



							@contextmanager
							def 						lowerCAmelCase    (			self							: List[str]    )   ->Dict:



														"""simple docstring"""



														warnings.warn(
														    """`as_target_processor` is deprecated and will be removed in v5 of Transformers. You can process your """
														    """labels by using the argument `text` of the regular `__call__` method (either in the same call as """
														    """your images inputs, or in a separate call."""    )
														snake_case_          =						True
														snake_case_          =						self.tokenizer
														yield
														snake_case_          =						self.image_processor
														snake_case_          =						False



							def 						lowerCAmelCase    (			self							: int						,    UpperCAmelCase_							: Union[str, Any]						,    UpperCAmelCase_							: str=False						,    UpperCAmelCase_							: str=None    )   ->Tuple:



														"""simple docstring"""



														if added_vocab is None:
																					snake_case_          =						self.tokenizer.get_added_vocab()

														snake_case_          =						{}

														while tokens:
																					snake_case_          =						re.search(R"""<s_(.*?)>"""						,    UpperCAmelCase_						,    re.IGNORECASE    )
																					if start_token is None:
																												break
																					snake_case_          =						start_token.group(1    )
																					snake_case_          =						re.search(RF"""</s_{key}>"""						,    UpperCAmelCase_						,    re.IGNORECASE    )
																					snake_case_          =						start_token.group()
																					if end_token is None:
																												snake_case_          =						tokens.replace(UpperCAmelCase_						,    """"""    )
																					else:
																												snake_case_          =						end_token.group()
																												snake_case_          =						re.escape(UpperCAmelCase_    )
																												snake_case_          =						re.escape(UpperCAmelCase_    )
																												snake_case_          =						re.search(F"""{start_token_escaped}(.*?){end_token_escaped}"""						,    UpperCAmelCase_						,    re.IGNORECASE    )
																												if content is not None:
																																			snake_case_          =						content.group(1    ).strip()
																																			if r"<s_" in content and r"</s_" in content:  # non-leaf node
																																										snake_case_          =						self.tokenajson(UpperCAmelCase_						,    is_inner_value=UpperCAmelCase_						,    added_vocab=UpperCAmelCase_    )
																																										if value:
																																																	if len(UpperCAmelCase_    ) == 1:
																																																								snake_case_          =						value[0]
																																																	snake_case_          =						value
																																			else:  # leaf nodes
																																										snake_case_          =						[]
																																										for leaf in content.split(R"""<sep/>"""    ):
																																																	snake_case_          =						leaf.strip()
																																																	if leaf in added_vocab and leaf[0] == "<" and leaf[-2:] == "/>":
																																																								snake_case_          =						leaf[1:-2]  # for categorical special tokens
																																																	output[key].append(UpperCAmelCase_    )
																																										if len(output[key]    ) == 1:
																																																	snake_case_          =						output[key][0]

																												snake_case_          =						tokens[tokens.find(UpperCAmelCase_    ) + len(UpperCAmelCase_    ) :].strip()
																												if tokens[:6] == r"<sep/>":  # non-leaf nodes
																																			return [output] + self.tokenajson(tokens[6:]						,    is_inner_value=UpperCAmelCase_						,    added_vocab=UpperCAmelCase_    )

														if len(UpperCAmelCase_    ):
																					return [output] if is_inner_value else output
														else:
																					return [] if is_inner_value else {"text_sequence": tokens}



							@property
							def 						lowerCAmelCase    (			self							: List[Any]    )   ->Optional[int]:



														"""simple docstring"""



														warnings.warn(
														    """`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead."""						,    UpperCAmelCase_						,    )
														return self.image_processor_class



							@property
							def 						lowerCAmelCase    (			self							: List[str]    )   ->str:



														"""simple docstring"""



														warnings.warn(
														    """`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead."""						,    UpperCAmelCase_						,    )
														return self.image_processor



