





import unittest

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

from diffusers import DDIMScheduler, LDMPipeline, UNetaDModel, VQModel
from diffusers.utils.testing_utils import enable_full_determinism, require_torch, slow, torch_device


enable_full_determinism()






class 					__A    (    unittest.TestCase ):

			'''simple docstring'''







			@property
			def 		__lowerCamelCase				(    self							):



				'''simple docstring'''







				torch.manual_seed(0							)
				lowerCamelCase__								=		UNetaDModel(
				    block_out_channels=(3_2, 6_4)       ,  layers_per_block=2       ,  sample_size=3_2       ,  in_channels=3       ,  out_channels=3       ,  down_block_types=('''DownBlock2D''', '''AttnDownBlock2D''')       ,  up_block_types=('''AttnUpBlock2D''', '''UpBlock2D''')       ,  )
				return model





			@property
			def 		__lowerCamelCase				(    self							):



				'''simple docstring'''







				torch.manual_seed(0							)
				lowerCamelCase__								=		VQModel(
				    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=3       ,  )
				return model





			@property
			def 		__lowerCamelCase				(    self							):



				'''simple docstring'''







				torch.manual_seed(0							)
				lowerCamelCase__								=		CLIPTextConfig(
				    bos_token_id=0       ,  eos_token_id=2       ,  hidden_size=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=1_0_0_0       ,  )
				return CLIPTextModel(lowercase_							)





			def 		__lowerCamelCase				(    self							):



				'''simple docstring'''







				lowerCamelCase__								=		self.dummy_uncond_unet
				lowerCamelCase__								=		DDIMScheduler()
				lowerCamelCase__								=		self.dummy_vq_model

				lowerCamelCase__								=		LDMPipeline(unet=lowercase_       ,  vqvae=lowercase_       ,  scheduler=lowercase_							)
				ldm.to(lowercase_							)
				ldm.set_progress_bar_config(disable=lowercase_							)

				lowerCamelCase__								=		torch.manual_seed(0							)
				lowerCamelCase__								=		ldm(generator=lowercase_       ,  num_inference_steps=2       ,  output_type='''numpy'''							).images

				lowerCamelCase__								=		torch.manual_seed(0							)
				lowerCamelCase__								=		ldm(generator=lowercase_       ,  num_inference_steps=2       ,  output_type='''numpy'''       ,  return_dict=lowercase_							)[0]

				lowerCamelCase__								=		image[0, -3:, -3:, -1]
				lowerCamelCase__								=		image_from_tuple[0, -3:, -3:, -1]

				assert image.shape == (1, 6_4, 6_4, 3)
				lowerCamelCase__								=		np.array([0.8512, 0.818, 0.6411, 0.6808, 0.4465, 0.5618, 0.46, 0.6231, 0.5172]							)
				lowerCamelCase__								=		1E-2 if torch_device != """mps""" else 3E-2

				assert np.abs(image_slice.flatten() - expected_slice							).max() < tolerance
				assert np.abs(image_from_tuple_slice.flatten() - expected_slice							).max() < tolerance






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

			'''simple docstring'''







			def 		__lowerCamelCase				(    self							):



				'''simple docstring'''







				lowerCamelCase__								=		LDMPipeline.from_pretrained('''CompVis/ldm-celebahq-256'''							)
				ldm.to(lowercase_							)
				ldm.set_progress_bar_config(disable=lowercase_							)

				lowerCamelCase__								=		torch.manual_seed(0							)
				lowerCamelCase__								=		ldm(generator=lowercase_       ,  num_inference_steps=5       ,  output_type='''numpy'''							).images

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

				assert image.shape == (1, 2_5_6, 2_5_6, 3)
				lowerCamelCase__								=		np.array([0.4399, 0.4_4975, 0.4_6825, 0.474, 0.4359, 0.4581, 0.4_5095, 0.4341, 0.4447]							)
				lowerCamelCase__								=		1E-2 if torch_device != """mps""" else 3E-2

				assert np.abs(image_slice.flatten() - expected_slice							).max() < tolerance




'''simple docstring'''




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

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


if is_sentencepiece_available():
						from .tokenization_albert import AlbertTokenizer
else:
						_lowercase						: int						=						None

_lowercase						: Union[str, Any]						=						logging.get_logger(__name__)
_lowercase						: Tuple						=						{"vocab_file": "spiece.model", "tokenizer_file": "tokenizer.json"}

_lowercase						: Dict						=						{
    "vocab_file": {
        "albert-base-v1": "https://huggingface.co/albert-base-v1/resolve/main/spiece.model",
        "albert-large-v1": "https://huggingface.co/albert-large-v1/resolve/main/spiece.model",
        "albert-xlarge-v1": "https://huggingface.co/albert-xlarge-v1/resolve/main/spiece.model",
        "albert-xxlarge-v1": "https://huggingface.co/albert-xxlarge-v1/resolve/main/spiece.model",
        "albert-base-v2": "https://huggingface.co/albert-base-v2/resolve/main/spiece.model",
        "albert-large-v2": "https://huggingface.co/albert-large-v2/resolve/main/spiece.model",
        "albert-xlarge-v2": "https://huggingface.co/albert-xlarge-v2/resolve/main/spiece.model",
        "albert-xxlarge-v2": "https://huggingface.co/albert-xxlarge-v2/resolve/main/spiece.model",
    },
    "tokenizer_file": {
        "albert-base-v1": "https://huggingface.co/albert-base-v1/resolve/main/tokenizer.json",
        "albert-large-v1": "https://huggingface.co/albert-large-v1/resolve/main/tokenizer.json",
        "albert-xlarge-v1": "https://huggingface.co/albert-xlarge-v1/resolve/main/tokenizer.json",
        "albert-xxlarge-v1": "https://huggingface.co/albert-xxlarge-v1/resolve/main/tokenizer.json",
        "albert-base-v2": "https://huggingface.co/albert-base-v2/resolve/main/tokenizer.json",
        "albert-large-v2": "https://huggingface.co/albert-large-v2/resolve/main/tokenizer.json",
        "albert-xlarge-v2": "https://huggingface.co/albert-xlarge-v2/resolve/main/tokenizer.json",
        "albert-xxlarge-v2": "https://huggingface.co/albert-xxlarge-v2/resolve/main/tokenizer.json",
    },
}

_lowercase						: int						=						{
    "albert-base-v1": 512,
    "albert-large-v1": 512,
    "albert-xlarge-v1": 512,
    "albert-xxlarge-v1": 512,
    "albert-base-v2": 512,
    "albert-large-v2": 512,
    "albert-xlarge-v2": 512,
    "albert-xxlarge-v2": 512,
}

_lowercase						: Tuple						=						"▁"





class     __magic_name__						(    _UpperCAmelCase):
			UpperCamelCase__				=       VOCAB_FILES_NAMES
			UpperCamelCase__				=       PRETRAINED_VOCAB_FILES_MAP
			UpperCamelCase__				=       PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
			UpperCamelCase__				=       AlbertTokenizer







			def __init__(	self				:  Optional[Any]				,			lowercase_				:  Union[str, Any]=None				,			lowercase_				:  str=None				,			lowercase_				:  Any=True				,			lowercase_				:  Optional[int]=True				,			lowercase_				:  List[str]=False				,			lowercase_				:  Optional[int]="[CLS]"				,			lowercase_				:  Any="[SEP]"				,			lowercase_				:  int="<unk>"				,			lowercase_				:  Any="[SEP]"				,			lowercase_				:  int="<pad>"				,			lowercase_				:  Tuple="[CLS]"				,			lowercase_				:  Dict="[MASK]"				,			**lowercase_				:  Optional[int]				,			):
										# Mask token behave like a normal word, i.e. include the space before it and
										# is included in the raw text, there should be a match in a non-normalized sentence.
										lowercase_					:			Tuple										=       (
										    AddedToken(lowercase_				,			lstrip=lowercase_				,			rstrip=lowercase_				,			normalized=lowercase_    )
										    if isinstance(lowercase_				,			lowercase_    )
										    else mask_token
										)

										super().__init__(
										    lowercase_				,			tokenizer_file=lowercase_				,			do_lower_case=lowercase_				,			remove_space=lowercase_				,			keep_accents=lowercase_				,			bos_token=lowercase_				,			eos_token=lowercase_				,			unk_token=lowercase_				,			sep_token=lowercase_				,			pad_token=lowercase_				,			cls_token=lowercase_				,			mask_token=lowercase_				,			**lowercase_				,			)

										lowercase_					:			Optional[int]										=       do_lower_case
										lowercase_					:			Any										=       remove_space
										lowercase_					:			Dict										=       keep_accents
										lowercase_					:			Optional[int]										=       vocab_file
										lowercase_					:			Any										=       False if not self.vocab_file else True







			def 	SCREAMING_SNAKE_CASE_     (	self				:  Tuple				,			lowercase_				:  List[int]				,			lowercase_				:  Optional[List[int]] = None    ):
										lowercase_					:			Tuple										=       [self.sep_token_id]
										lowercase_					:			str										=       [self.cls_token_id]
										if token_ids_a is None:
																	return cls + token_ids_a + sep
										return cls + token_ids_a + sep + token_ids_a + sep







			def 	SCREAMING_SNAKE_CASE_     (	self				:  Any				,			lowercase_				:  List[int]				,			lowercase_				:  Optional[List[int]] = None    ):
										lowercase_					:			Union[str, Any]										=       [self.sep_token_id]
										lowercase_					:			int										=       [self.cls_token_id]

										if token_ids_a is None:
																	return len(cls + token_ids_a + sep    ) * [0]
										return len(cls + token_ids_a + sep    ) * [0] + len(token_ids_a + sep    ) * [1]







			def 	SCREAMING_SNAKE_CASE_     (	self				:  Union[str, 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
										lowercase_					:			Optional[Any]										=       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_    )

										return (out_vocab_file,)





