




'''simple docstring'''

import gc
import random
import unittest

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

import diffusers
from diffusers import (
    AutoencoderKL,
    EulerDiscreteScheduler,
    StableDiffusionLatentUpscalePipeline,
    StableDiffusionPipeline,
    UNetaDConditionModel,
)
from diffusers.schedulers import KarrasDiffusionSchedulers
from diffusers.utils import floats_tensor, load_image, load_numpy, slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu

from ..pipeline_params import TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS, TEXT_GUIDED_IMAGE_VARIATION_PARAMS
from ..test_pipelines_common import PipelineKarrasSchedulerTesterMixin, PipelineLatentTesterMixin, PipelineTesterMixin


enable_full_determinism()


def 		__magic_name__    (	__UpperCAmelCase	)		->					List[Any]:

					'''simple docstring'''

					snake_case_									=				[tensor.shape for tensor in tensor_list]
					return all(shape == shapes[0] for shape in shapes[1:]	)






class 						a				(   _lowerCamelCase   ,     _lowerCamelCase   ,     _lowerCamelCase   ,     unittest.TestCase     ):
				snake_case_      =		StableDiffusionLatentUpscalePipeline
				snake_case_      =		TEXT_GUIDED_IMAGE_VARIATION_PARAMS - {
				    "height",
				    "width",
				    "cross_attention_kwargs",
				    "negative_prompt_embeds",
				    "prompt_embeds",
				}
				snake_case_      =		PipelineTesterMixin.required_optional_params - {"num_images_per_prompt"}
				snake_case_      =		TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS
				snake_case_      =		frozenset(
				    []     )  # TO-DO: update image_params once pipeline is refactored with VaeImageProcessor.preprocess
				snake_case_      =		frozenset([]     )

				snake_case_      =		True


				@property
				def   A_ (		self    :					List[str]					):
									snake_case_									=				1
									snake_case_									=				4
									snake_case_									=				(16, 16)

									snake_case_									=				floats_tensor((batch_size, num_channels) + sizes       ,				rng=random.Random(0					)					).to(lowercase_					)
									return image


				def   A_ (		self    :					Optional[int]					):
									torch.manual_seed(0					)
									snake_case_									=				UNetaDConditionModel(
									    act_fn='''gelu'''       ,				attention_head_dim=8       ,				norm_num_groups=lowercase_       ,				block_out_channels=[32, 32, 64, 64]       ,				time_cond_proj_dim=160       ,				conv_in_kernel=1       ,				conv_out_kernel=1       ,				cross_attention_dim=32       ,				down_block_types=(
									        '''KDownBlock2D''',
									        '''KCrossAttnDownBlock2D''',
									        '''KCrossAttnDownBlock2D''',
									        '''KCrossAttnDownBlock2D''',
									    )       ,				in_channels=8       ,				mid_block_type=lowercase_       ,				only_cross_attention=lowercase_       ,				out_channels=5       ,				resnet_time_scale_shift='''scale_shift'''       ,				time_embedding_type='''fourier'''       ,				timestep_post_act='''gelu'''       ,				up_block_types=('''KCrossAttnUpBlock2D''', '''KCrossAttnUpBlock2D''', '''KCrossAttnUpBlock2D''', '''KUpBlock2D''')       ,				)
									snake_case_									=				AutoencoderKL(
									    block_out_channels=[32, 32, 64, 64]       ,				in_channels=3       ,				out_channels=3       ,				down_block_types=[
									        '''DownEncoderBlock2D''',
									        '''DownEncoderBlock2D''',
									        '''DownEncoderBlock2D''',
									        '''DownEncoderBlock2D''',
									    ]       ,				up_block_types=['''UpDecoderBlock2D''', '''UpDecoderBlock2D''', '''UpDecoderBlock2D''', '''UpDecoderBlock2D''']       ,				latent_channels=4       ,				)
									snake_case_									=				EulerDiscreteScheduler(prediction_type='''sample'''					)
									snake_case_									=				CLIPTextConfig(
									    bos_token_id=0       ,				eos_token_id=2       ,				hidden_size=32       ,				intermediate_size=37       ,				layer_norm_eps=1e-05       ,				num_attention_heads=4       ,				num_hidden_layers=5       ,				pad_token_id=1       ,				vocab_size=1000       ,				hidden_act='''quick_gelu'''       ,				projection_dim=512       ,				)
									snake_case_									=				CLIPTextModel(lowercase_					)
									snake_case_									=				CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip'''					)

									snake_case_									=				{
									    '''unet''': model.eval(),
									    '''vae''': vae.eval(),
									    '''scheduler''': scheduler,
									    '''text_encoder''': text_encoder,
									    '''tokenizer''': tokenizer,
									}

									return components


				def   A_ (		self    :					Any       ,				lowercase_    :					int       ,				lowercase_    :					Dict=0					):
									if str(lowercase_					).startswith('''mps'''					):
														snake_case_									=				torch.manual_seed(lowercase_					)
									else:
														snake_case_									=				torch.Generator(device=lowercase_					).manual_seed(lowercase_					)
									snake_case_									=				{
									    '''prompt''': '''A painting of a squirrel eating a burger''',
									    '''image''': self.dummy_image.cpu(),
									    '''generator''': generator,
									    '''num_inference_steps''': 2,
									    '''output_type''': '''numpy''',
									}
									return inputs


				def   A_ (		self    :					Tuple					):
									snake_case_									=				'''cpu'''

									snake_case_									=				self.get_dummy_components()
									snake_case_									=				self.pipeline_class(**lowercase_					)
									pipe.to(lowercase_					)
									pipe.set_progress_bar_config(disable=lowercase_					)

									snake_case_									=				self.get_dummy_inputs(lowercase_					)
									snake_case_									=				pipe(**lowercase_					).images
									snake_case_									=				image[0, -3:, -3:, -1]

									self.assertEqual(image.shape       ,				(1, 256, 256, 3)					)
									snake_case_									=				np.array(
									    [0.4722_2412, 0.4192_1633, 0.4471_7434, 0.4687_4192, 0.4258_8258, 0.4615_0726, 0.467_7534, 0.4558_3832, 0.4857_9055]					)
									snake_case_									=				np.abs(image_slice.flatten() - expected_slice					).max()
									self.assertLessEqual(lowercase_       ,				1e-3					)


				def   A_ (		self    :					Optional[int]					):
									super().test_attention_slicing_forward_pass(expected_max_diff=7e-3					)


				def   A_ (		self    :					str					):
									super().test_cpu_offload_forward_pass(expected_max_diff=3e-3					)


				def   A_ (		self    :					str					):
									super().test_dict_tuple_outputs_equivalent(expected_max_difference=3e-3					)


				def   A_ (		self    :					List[Any]					):
									super().test_inference_batch_single_identical(expected_max_diff=7e-3					)


				def   A_ (		self    :					List[str]					):
									super().test_pt_np_pil_outputs_equivalent(expected_max_diff=3e-3					)


				def   A_ (		self    :					Tuple					):
									super().test_save_load_local(expected_max_difference=3e-3					)


				def   A_ (		self    :					int					):
									super().test_save_load_optional_components(expected_max_difference=3e-3					)


				def   A_ (		self    :					int					):
									snake_case_									=				[
									    '''DDIMScheduler''',
									    '''DDPMScheduler''',
									    '''PNDMScheduler''',
									    '''HeunDiscreteScheduler''',
									    '''EulerAncestralDiscreteScheduler''',
									    '''KDPM2DiscreteScheduler''',
									    '''KDPM2AncestralDiscreteScheduler''',
									    '''DPMSolverSDEScheduler''',
									]
									snake_case_									=				self.get_dummy_components()
									snake_case_									=				self.pipeline_class(**lowercase_					)

									# make sure that PNDM does not need warm-up
									pipe.scheduler.register_to_config(skip_prk_steps=lowercase_					)

									pipe.to(lowercase_					)
									pipe.set_progress_bar_config(disable=lowercase_					)
									snake_case_									=				self.get_dummy_inputs(lowercase_					)
									snake_case_									=				2

									snake_case_									=				[]
									for scheduler_enum in KarrasDiffusionSchedulers:
														if scheduler_enum.name in skip_schedulers:
																			# no sigma schedulers are not supported
																			# no schedulers
																			continue

														snake_case_									=				getattr(lowercase_       ,				scheduler_enum.name					)
														snake_case_									=				scheduler_cls.from_config(pipe.scheduler.config					)
														snake_case_									=				pipe(**lowercase_					)[0]
														outputs.append(lowercase_					)

									assert check_same_shape(lowercase_					)







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


				def   A_ (		self    :					str					):
									super().tearDown()
									gc.collect()
									torch.cuda.empty_cache()


				def   A_ (		self    :					Optional[int]					):
									snake_case_									=				torch.manual_seed(33					)

									snake_case_									=				StableDiffusionPipeline.from_pretrained('''CompVis/stable-diffusion-v1-4'''       ,				torch_dtype=torch.floataa					)
									pipe.to('''cuda'''					)

									snake_case_									=				StableDiffusionLatentUpscalePipeline.from_pretrained(
									    '''stabilityai/sd-x2-latent-upscaler'''       ,				torch_dtype=torch.floataa					)
									upscaler.to('''cuda'''					)

									snake_case_									=				'''a photo of an astronaut high resolution, unreal engine, ultra realistic'''

									snake_case_									=				pipe(lowercase_       ,				generator=lowercase_       ,				output_type='''latent'''					).images

									snake_case_									=				upscaler(
									    prompt=lowercase_       ,				image=lowercase_       ,				num_inference_steps=20       ,				guidance_scale=0       ,				generator=lowercase_       ,				output_type='''np'''       ,				).images[0]

									snake_case_									=				load_numpy(
									    '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/latent-upscaler/astronaut_1024.npy'''					)
									assert np.abs((expected_image - image).mean()					) < 5e-2


				def   A_ (		self    :					List[str]					):
									snake_case_									=				torch.manual_seed(33					)

									snake_case_									=				StableDiffusionLatentUpscalePipeline.from_pretrained(
									    '''stabilityai/sd-x2-latent-upscaler'''       ,				torch_dtype=torch.floataa					)
									upscaler.to('''cuda'''					)

									snake_case_									=				'''the temple of fire by Ross Tran and Gerardo Dottori, oil on canvas'''

									snake_case_									=				load_image(
									    '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/latent-upscaler/fire_temple_512.png'''					)

									snake_case_									=				upscaler(
									    prompt=lowercase_       ,				image=lowercase_       ,				num_inference_steps=20       ,				guidance_scale=0       ,				generator=lowercase_       ,				output_type='''np'''       ,				).images[0]

									snake_case_									=				load_numpy(
									    '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/latent-upscaler/fire_temple_1024.npy'''					)
									assert np.abs((expected_image - image).max()					) < 5e-2







'''simple docstring'''

import os
import re
import warnings
from shutil import copyfile
from typing import List, Optional, Tuple

from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import is_sentencepiece_available, logging


if is_sentencepiece_available():
							from .tokenization_ta import TaTokenizer
else:
							a  :				Dict									=  None


a  :				List[Any]									=  logging.get_logger(__name__)

a  :				List[Any]									=  {'vocab_file': 'spiece.model', 'tokenizer_file': 'tokenizer.json'}

a  :				str									=  {
    'vocab_file': {
        't5-small': 'https://huggingface.co/t5-small/resolve/main/spiece.model',
        't5-base': 'https://huggingface.co/t5-base/resolve/main/spiece.model',
        't5-large': 'https://huggingface.co/t5-large/resolve/main/spiece.model',
        't5-3b': 'https://huggingface.co/t5-3b/resolve/main/spiece.model',
        't5-11b': 'https://huggingface.co/t5-11b/resolve/main/spiece.model',
    },
    'tokenizer_file': {
        't5-small': 'https://huggingface.co/t5-small/resolve/main/tokenizer.json',
        't5-base': 'https://huggingface.co/t5-base/resolve/main/tokenizer.json',
        't5-large': 'https://huggingface.co/t5-large/resolve/main/tokenizer.json',
        't5-3b': 'https://huggingface.co/t5-3b/resolve/main/tokenizer.json',
        't5-11b': 'https://huggingface.co/t5-11b/resolve/main/tokenizer.json',
    },
}


# TODO(PVP) - this should be removed in Transformers v5
a  :				List[Any]									=  {
    't5-small': 512,
    't5-base': 512,
    't5-large': 512,
    't5-3b': 512,
    't5-11b': 512,
}






class 						a				(   _lowerCamelCase     ):
				snake_case_      =		VOCAB_FILES_NAMES
				snake_case_      =		PRETRAINED_VOCAB_FILES_MAP
				snake_case_      =		PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
				snake_case_      =		["input_ids", "attention_mask"]
				snake_case_      =		TaTokenizer

				snake_case_      =		[]


				def __init__(		self    :					List[Any]       ,				lowercase_    :					int=None       ,				lowercase_    :					Dict=None       ,				lowercase_    :					Dict="</s>"       ,				lowercase_    :					List[Any]="<unk>"       ,				lowercase_    :					int="<pad>"       ,				lowercase_    :					int=100       ,				lowercase_    :					List[Any]=None       ,				**lowercase_    :					List[str]       ,				):
									# Add extra_ids to the special token list
									if extra_ids > 0 and additional_special_tokens is None:
														snake_case_									=				[F"<extra_id_{i}>" for i in range(lowercase_					)]
									elif extra_ids > 0 and additional_special_tokens is not None:
														# Check that we have the right number of extra special tokens
														snake_case_									=				len(set(filter(lambda lowercase_					: bool('''extra_id_''' in str(lowercase_					)					)       ,				lowercase_					)					)					)
														if extra_tokens != extra_ids:
																			raise ValueError(
																			    F"Both extra_ids ({extra_ids}) and additional_special_tokens ({additional_special_tokens}) are"
																			    ''' provided to T5Tokenizer. In this case the additional_special_tokens must include the extra_ids'''
																			    ''' tokens'''					)

									super().__init__(
									    lowercase_       ,				tokenizer_file=lowercase_       ,				eos_token=lowercase_       ,				unk_token=lowercase_       ,				pad_token=lowercase_       ,				extra_ids=lowercase_       ,				additional_special_tokens=lowercase_       ,				**lowercase_       ,				)

									snake_case_									=				vocab_file
									snake_case_									=				False if not self.vocab_file else True
									snake_case_									=				extra_ids


				@staticmethod
				def   A_ (		lowercase_    :					Optional[int]       ,				lowercase_    :					List[str]       ,				lowercase_    :					int					):
									if pretrained_model_name_or_path in TaTokenizerFast.max_model_input_sizes:
														snake_case_									=				TaTokenizerFast.max_model_input_sizes[pretrained_model_name_or_path]
														if init_max_model_length is not None and init_max_model_length != max_model_length:
																			return init_max_model_length
														elif init_max_model_length is None:
																			warnings.warn(
																			    '''This tokenizer was incorrectly instantiated with a model max length of'''
																			    F" {deprecated_max_model_length} which will be corrected in Transformers v5.\nFor now, this"
																			    ''' behavior is kept to avoid breaking backwards compatibility when padding/encoding with'''
																			    ''' `truncation is True`.\n- Be aware that you SHOULD NOT rely on'''
																			    F" {pretrained_model_name_or_path} automatically truncating your input to"
																			    F" {deprecated_max_model_length} when padding/encoding.\n- If you want to encode/pad to sequences"
																			    F" longer than {deprecated_max_model_length} you can either instantiate this tokenizer with"
																			    ''' `model_max_length` or pass `max_length` when encoding/padding.\n- To avoid this warning, please'''
																			    ''' instantiate this tokenizer with `model_max_length` set to your preferred value.'''       ,				lowercase_       ,				)

									return max_model_length


				def   A_ (		self    :					Optional[Any]       ,				lowercase_    :					str       ,				lowercase_    :					Optional[str] = None					):
									if not self.can_save_slow_tokenizer:
														raise ValueError(
														    '''Your fast tokenizer does not have the necessary information to save the vocabulary for a slow '''
														    '''tokenizer.'''					)

									if not os.path.isdir(lowercase_					):
														logger.error(F"Vocabulary path ({save_directory}) should be a directory"					)
														return
									snake_case_									=				os.path.join(
									    lowercase_       ,				(filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file''']					)

									if os.path.abspath(self.vocab_file					) != os.path.abspath(lowercase_					):
														copyfile(self.vocab_file       ,				lowercase_					)
														logger.info(F"Copy vocab file to {out_vocab_file}"					)

									return (out_vocab_file,)


				def   A_ (		self    :					Optional[int]       ,				lowercase_    :					List[int]       ,				lowercase_    :					Optional[List[int]] = None					):
									snake_case_									=				token_ids_a + [self.eos_token_id]
									if token_ids_a is None:
														return self.prefix_tokens + token_ids_a
									else:
														snake_case_									=				token_ids_a + [self.eos_token_id]
														return self.prefix_tokens + token_ids_a + token_ids_a


				def   A_ (		self    :					int       ,				lowercase_    :					List[int]       ,				lowercase_    :					Optional[List[int]] = None					):
									snake_case_									=				[self.eos_token_id]

									if token_ids_a is None:
														return len(token_ids_a + eos					) * [0]
									return len(token_ids_a + eos + token_ids_a + eos					) * [0]


				def   A_ (		self    :					Dict					):
									return list(
									    set(filter(lambda lowercase_					: bool(re.search(R'''<extra_id_\d+>'''       ,				lowercase_					)					) is not None       ,				self.additional_special_tokens					)					)					)


				def   A_ (		self    :					Any					):
									return [self.convert_tokens_to_ids(lowercase_					) for token in self.get_sentinel_tokens()]

