





import json
import os
import unittest

from transformers import DebertaTokenizer, DebertaTokenizerFast
from transformers.models.deberta.tokenization_deberta import VOCAB_FILES_NAMES
from transformers.testing_utils import slow

from ...test_tokenization_common import TokenizerTesterMixin
class SCREAMING_SNAKE_CASE_  (   __A    ,			unittest.TestCase   ):
						__magic_name__:     str  =    DebertaTokenizer
						__magic_name__:     str  =    True
						__magic_name__:     Any  =    DebertaTokenizerFast






						def UpperCAmelCase_			( self : Any		)							->      str:







							"""simple docstring"""


							super().setUp()

							# Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt
							snake_case_						:     str     =		[
							    'l',
							    'o',
							    'w',
							    'e',
							    'r',
							    's',
							    't',
							    'i',
							    'd',
							    'n',
							    '\u0120',
							    '\u0120l',
							    '\u0120n',
							    '\u0120lo',
							    '\u0120low',
							    'er',
							    '\u0120lowest',
							    '\u0120newer',
							    '\u0120wider',
							    '[UNK]',
							]
							snake_case_						:     int     =		dict(zip(__lowercase						,				range(len(__lowercase		)		)		)		)
							snake_case_						:     List[str]     =		['#version: 0.2', '\u0120 l', '\u0120l o', '\u0120lo w', 'e r', '']
							snake_case_						:     int     =		{'unk_token': '[UNK]'}

							snake_case_						:     Tuple     =		os.path.join(self.tmpdirname						,				VOCAB_FILES_NAMES['vocab_file']		)
							snake_case_						:     List[Any]     =		os.path.join(self.tmpdirname						,				VOCAB_FILES_NAMES['merges_file']		)
							with open(self.vocab_file						,				'w'						,				encoding='utf-8'		) as fp:
								fp.write(json.dumps(__lowercase		) + '\n'		)
							with open(self.merges_file						,				'w'						,				encoding='utf-8'		) as fp:
								fp.write('\n'.join(__lowercase		)		)






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







							"""simple docstring"""


							kwargs.update(self.special_tokens_map		)
							return self.tokenizer_class.from_pretrained(self.tmpdirname						,				**__lowercase		)






						def UpperCAmelCase_			( self : Any						,				_A : str		)							->      str:







							"""simple docstring"""


							snake_case_						:     Any     =		'lower newer'
							snake_case_						:     Tuple     =		'lower newer'
							return input_text, output_text






						def UpperCAmelCase_			( self : int		)							->      Any:







							"""simple docstring"""


							snake_case_						:     List[str]     =		self.get_tokenizer()
							snake_case_						:     Tuple     =		'lower newer'
							snake_case_						:     Tuple     =		['l', 'o', 'w', 'er', '\u0120', 'n', 'e', 'w', 'er']
							snake_case_						:     Any     =		tokenizer.tokenize(__lowercase		)
							self.assertListEqual(__lowercase						,				__lowercase		)

							snake_case_						:     str     =		tokens + [tokenizer.unk_token]
							snake_case_						:     List[str]     =		[0, 1, 2, 15, 10, 9, 3, 2, 15, 19]
							self.assertListEqual(tokenizer.convert_tokens_to_ids(__lowercase		)						,				__lowercase		)






						def UpperCAmelCase_			( self : Any		)							->      Tuple:







							"""simple docstring"""


							snake_case_						:     Any     =		self.get_tokenizer()
							snake_case_						:     Optional[Any]     =		tokenizer('Hello'						,				'World'		)
							snake_case_						:     Union[str, Any]     =		[0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1]
							self.assertListEqual(tokd['token_type_ids']						,				__lowercase		)






						@slow
						def UpperCAmelCase_			( self : Tuple		)							->      int:







							"""simple docstring"""


							snake_case_						:     List[str]     =		self.tokenizer_class.from_pretrained('microsoft/deberta-base'		)

							snake_case_						:     str     =		tokenizer.encode('sequence builders'						,				add_special_tokens=__lowercase		)
							snake_case_						:     Dict     =		tokenizer.encode('multi-sequence build'						,				add_special_tokens=__lowercase		)

							snake_case_						:     str     =		tokenizer.encode(
							    'sequence builders'						,				add_special_tokens=__lowercase						,				add_prefix_space=__lowercase		)
							snake_case_						:     List[str]     =		tokenizer.encode(
							    'sequence builders'						,				'multi-sequence build'						,				add_special_tokens=__lowercase						,				add_prefix_space=__lowercase		)

							snake_case_						:     Optional[Any]     =		tokenizer.build_inputs_with_special_tokens(__lowercase		)
							snake_case_						:     Tuple     =		tokenizer.build_inputs_with_special_tokens(__lowercase						,				__lowercase		)

							assert encoded_sentence == encoded_text_from_decode
							assert encoded_pair == encoded_pair_from_decode






						@slow
						def UpperCAmelCase_			( self : int		)							->      List[Any]:







							"""simple docstring"""


							snake_case_						:     Union[str, Any]     =		[self.tokenizer_class]
							if self.test_rust_tokenizer:
								tokenizer_classes.append(self.rust_tokenizer_class		)

							for tokenizer_class in tokenizer_classes:
								snake_case_						:     List[Any]     =		tokenizer_class.from_pretrained('microsoft/deberta-base'		)

								snake_case_						:     int     =		[
								    'ALBERT: A Lite BERT for Self-supervised Learning of Language Representations',
								    'ALBERT incorporates two parameter reduction techniques',
								    'The first one is a factorized embedding parameterization. By decomposing the large vocabulary'
								    ' embedding matrix into two small matrices, we separate the size of the hidden layers from the size of'
								    ' vocabulary embedding.',
								]

								snake_case_						:     List[Any]     =		tokenizer(__lowercase						,				padding=__lowercase		)
								snake_case_						:     Dict     =		[tokenizer.decode(__lowercase						,				skip_special_tokens=__lowercase		) for seq in encoding['input_ids']]

								# fmt: off
								snake_case_						:     Any     =		{
								    'input_ids': [
								        [1, 2118, 11126, 565, 35, 83, 25191, 163, 18854, 13, 12156, 12, 16101, 25376, 13807, 9, 22205, 27893, 1635, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
								        [1, 2118, 11126, 565, 24536, 80, 43797, 4878, 7373, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
								        [1, 133, 78, 65, 16, 10, 3724, 1538, 33183, 11303, 43797, 1938, 4, 870, 24165, 29105, 5, 739, 32644, 33183, 11303, 36173, 88, 80, 650, 7821, 45940, 6, 52, 2559, 5, 1836, 9, 5, 7397, 13171, 31, 5, 1836, 9, 32644, 33183, 11303, 4, 2]
								    ],
								    'token_type_ids': [
								        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
								        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
								        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
								    ],
								    'attention_mask': [
								        [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
								        [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
								        [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
								    ]
								}
								# fmt: on

								snake_case_						:     Union[str, Any]     =		[
								    'ALBERT: A Lite BERT for Self-supervised Learning of Language Representations',
								    'ALBERT incorporates two parameter reduction techniques',
								    'The first one is a factorized embedding parameterization. By decomposing the large vocabulary'
								    ' embedding matrix into two small matrices, we separate the size of the hidden layers from the size of'
								    ' vocabulary embedding.',
								]

								self.assertDictEqual(encoding.data						,				__lowercase		)

								for expected, decoded in zip(__lowercase						,				__lowercase		):
									self.assertEqual(__lowercase						,				__lowercase		)








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_  (   snake_case_    ,			snake_case_    ,			snake_case_    ,			unittest.TestCase   ):
						__magic_name__:     int	 =    AltDiffusionPipeline
						__magic_name__:     Any	 =    TEXT_TO_IMAGE_PARAMS
						__magic_name__:     Any	 =    TEXT_TO_IMAGE_BATCH_PARAMS
						__magic_name__:     Any	 =    TEXT_TO_IMAGE_IMAGE_PARAMS
						__magic_name__:     Union[str, Any]	 =    TEXT_TO_IMAGE_IMAGE_PARAMS






						def UpperCAmelCase_			( self : List[Any]		)							->      int:







							"""simple docstring"""


							torch.manual_seed(0		)
							snake_case_						:     List[str]     =		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_						:     Optional[Any]     =		DDIMScheduler(
							    beta_start=0.0_0_0_8_5						,				beta_end=0.0_1_2						,				beta_schedule='scaled_linear'						,				clip_sample=_A						,				set_alpha_to_one=_A						,				)
							torch.manual_seed(0		)
							snake_case_						:     Union[str, Any]     =		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_						:     Optional[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						,				)
							snake_case_						:     Any     =		CLIPTextModel(_A		)

							snake_case_						:     Any     =		XLMRobertaTokenizer.from_pretrained('hf-internal-testing/tiny-xlm-roberta'		)
							snake_case_						:     Dict     =		77

							snake_case_						:     List[Any]     =		{
							    'unet': unet,
							    'scheduler': scheduler,
							    'vae': vae,
							    'text_encoder': text_encoder,
							    'tokenizer': tokenizer,
							    'safety_checker': None,
							    'feature_extractor': None,
							}
							return components






						def UpperCAmelCase_			( self : int						,				_A : Optional[int]						,				_A : int=0		)							->      Dict:







							"""simple docstring"""


							if str(_A		).startswith('mps'		):
								snake_case_						:     Union[str, Any]     =		torch.manual_seed(_A		)
							else:
								snake_case_						:     Union[str, Any]     =		torch.Generator(device=_A		).manual_seed(_A		)
							snake_case_						:     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 : Optional[Any]		)							->      Any:







							"""simple docstring"""


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






						def UpperCAmelCase_			( self : List[Any]		)							->      Dict:







							"""simple docstring"""


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






						def UpperCAmelCase_			( self : Dict		)							->      Any:







							"""simple docstring"""


							snake_case_						:     Tuple     =		'cpu'  # ensure determinism for the device-dependent torch.Generator

							snake_case_						:     Any     =		self.get_dummy_components()
							torch.manual_seed(0		)
							snake_case_						:     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
							snake_case_						:     Optional[Any]     =		RobertaSeriesModelWithTransformation(_A		)
							snake_case_						:     Optional[Any]     =		text_encoder

							snake_case_						:     Optional[Any]     =		AltDiffusionPipeline(**_A		)
							snake_case_						:     List[Any]     =		alt_pipe.to(_A		)
							alt_pipe.set_progress_bar_config(disable=_A		)

							snake_case_						:     Optional[Any]     =		self.get_dummy_inputs(_A		)
							snake_case_						:     int     =		'A photo of an astronaut'
							snake_case_						:     Tuple     =		alt_pipe(**_A		)
							snake_case_						:     Any     =		output.images
							snake_case_						:     Optional[int]     =		image[0, -3:, -3:, -1]

							assert image.shape == (1, 64, 64, 3)
							snake_case_						:     Any     =		np.array(
							    [0.5_7_4_8_1_6_2, 0.6_0_4_4_7_1_4_5, 0.4_8_8_2_1_2_1_7, 0.5_0_1_0_0_6_3_6, 0.5_4_3_1_1_8_5, 0.4_5_7_6_3_6_8_3, 0.4_9_6_5_7_6_9_6, 0.4_8_1_3_2_7_3_3, 0.4_7_5_7_3_0_9_3]		)

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






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







							"""simple docstring"""


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

							snake_case_						:     Any     =		self.get_dummy_components()
							snake_case_						:     List[str]     =		PNDMScheduler(skip_prk_steps=_A		)
							torch.manual_seed(0		)
							snake_case_						:     Optional[int]     =		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
							snake_case_						:     Tuple     =		RobertaSeriesModelWithTransformation(_A		)
							snake_case_						:     Any     =		text_encoder
							snake_case_						:     Tuple     =		AltDiffusionPipeline(**_A		)
							snake_case_						:     Dict     =		alt_pipe.to(_A		)
							alt_pipe.set_progress_bar_config(disable=_A		)

							snake_case_						:     Dict     =		self.get_dummy_inputs(_A		)
							snake_case_						:     Tuple     =		alt_pipe(**_A		)
							snake_case_						:     int     =		output.images
							snake_case_						:     List[Any]     =		image[0, -3:, -3:, -1]

							assert image.shape == (1, 64, 64, 3)
							snake_case_						:     Optional[int]     =		np.array(
							    [0.5_1_6_0_5_0_9_3, 0.5_7_0_7_2_4_1, 0.4_7_3_6_5_5_0_7, 0.5_0_5_7_8_8_8_6, 0.5_6_3_3_8_7_7, 0.4_6_4_2_5_0_3, 0.5_1_8_2_0_8_1, 0.4_8_7_6_3_4_8_4, 0.4_9_0_8_4_2_3_7]		)

							assert np.abs(image_slice.flatten() - expected_slice		).max() < 1E-2
@slow
@require_torch_gpu
class SCREAMING_SNAKE_CASE_  (   unittest.TestCase   ):






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







							"""simple docstring"""


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






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







							"""simple docstring"""


							snake_case_						:     Optional[int]     =		AltDiffusionPipeline.from_pretrained('BAAI/AltDiffusion'						,				safety_checker=_A		)
							snake_case_						:     Optional[int]     =		alt_pipe.to(_A		)
							alt_pipe.set_progress_bar_config(disable=_A		)

							snake_case_						:     str     =		'A painting of a squirrel eating a burger'
							snake_case_						:     Tuple     =		torch.manual_seed(0		)
							snake_case_						:     str     =		alt_pipe([prompt]						,				generator=_A						,				guidance_scale=6.0						,				num_inference_steps=20						,				output_type='np'		)

							snake_case_						:     Any     =		output.images

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

							assert image.shape == (1, 512, 512, 3)
							snake_case_						:     Union[str, Any]     =		np.array([0.1_0_1_0, 0.0_8_0_0, 0.0_7_9_4, 0.0_8_8_5, 0.0_8_4_3, 0.0_7_6_2, 0.0_7_6_9, 0.0_7_2_9, 0.0_5_8_6]		)

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






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







							"""simple docstring"""


							snake_case_						:     Optional[Any]     =		DDIMScheduler.from_pretrained('BAAI/AltDiffusion'						,				subfolder='scheduler'		)

							snake_case_						:     Union[str, Any]     =		AltDiffusionPipeline.from_pretrained('BAAI/AltDiffusion'						,				scheduler=_A						,				safety_checker=_A		)
							snake_case_						:     List[str]     =		alt_pipe.to(_A		)
							alt_pipe.set_progress_bar_config(disable=_A		)

							snake_case_						:     List[Any]     =		'A painting of a squirrel eating a burger'
							snake_case_						:     int     =		torch.manual_seed(0		)

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

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

							assert image.shape == (1, 512, 512, 3)
							snake_case_						:     List[Any]     =		np.array([0.4_0_1_9, 0.4_0_5_2, 0.3_8_1_0, 0.4_1_1_9, 0.3_9_1_6, 0.3_9_8_2, 0.4_6_5_1, 0.4_1_9_5, 0.5_3_2_3]		)

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

