


"""simple docstring"""





import json
from typing import List, Optional, Tuple

from tokenizers import normalizers

from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import logging
from .tokenization_funnel import FunnelTokenizer


_UpperCamelCase							=      logging.get_logger(__name__)

_UpperCamelCase							=      {'vocab_file': 'vocab.txt', 'tokenizer_file': 'tokenizer.json'}

_UpperCamelCase							=      [
    'small',
    'small-base',
    'medium',
    'medium-base',
    'intermediate',
    'intermediate-base',
    'large',
    'large-base',
    'xlarge',
    'xlarge-base',
]

_UpperCamelCase							=      {
    'vocab_file': {
        'funnel-transformer/small': 'https://huggingface.co/funnel-transformer/small/resolve/main/vocab.txt',
        'funnel-transformer/small-base': 'https://huggingface.co/funnel-transformer/small-base/resolve/main/vocab.txt',
        'funnel-transformer/medium': 'https://huggingface.co/funnel-transformer/medium/resolve/main/vocab.txt',
        'funnel-transformer/medium-base': (
            'https://huggingface.co/funnel-transformer/medium-base/resolve/main/vocab.txt'
        ),
        'funnel-transformer/intermediate': (
            'https://huggingface.co/funnel-transformer/intermediate/resolve/main/vocab.txt'
        ),
        'funnel-transformer/intermediate-base': (
            'https://huggingface.co/funnel-transformer/intermediate-base/resolve/main/vocab.txt'
        ),
        'funnel-transformer/large': 'https://huggingface.co/funnel-transformer/large/resolve/main/vocab.txt',
        'funnel-transformer/large-base': 'https://huggingface.co/funnel-transformer/large-base/resolve/main/vocab.txt',
        'funnel-transformer/xlarge': 'https://huggingface.co/funnel-transformer/xlarge/resolve/main/vocab.txt',
        'funnel-transformer/xlarge-base': (
            'https://huggingface.co/funnel-transformer/xlarge-base/resolve/main/vocab.txt'
        ),
    },
    'tokenizer_file': {
        'funnel-transformer/small': 'https://huggingface.co/funnel-transformer/small/resolve/main/tokenizer.json',
        'funnel-transformer/small-base': (
            'https://huggingface.co/funnel-transformer/small-base/resolve/main/tokenizer.json'
        ),
        'funnel-transformer/medium': 'https://huggingface.co/funnel-transformer/medium/resolve/main/tokenizer.json',
        'funnel-transformer/medium-base': (
            'https://huggingface.co/funnel-transformer/medium-base/resolve/main/tokenizer.json'
        ),
        'funnel-transformer/intermediate': (
            'https://huggingface.co/funnel-transformer/intermediate/resolve/main/tokenizer.json'
        ),
        'funnel-transformer/intermediate-base': (
            'https://huggingface.co/funnel-transformer/intermediate-base/resolve/main/tokenizer.json'
        ),
        'funnel-transformer/large': 'https://huggingface.co/funnel-transformer/large/resolve/main/tokenizer.json',
        'funnel-transformer/large-base': (
            'https://huggingface.co/funnel-transformer/large-base/resolve/main/tokenizer.json'
        ),
        'funnel-transformer/xlarge': 'https://huggingface.co/funnel-transformer/xlarge/resolve/main/tokenizer.json',
        'funnel-transformer/xlarge-base': (
            'https://huggingface.co/funnel-transformer/xlarge-base/resolve/main/tokenizer.json'
        ),
    },
}
_UpperCamelCase							=      {f'''funnel-transformer/{name}''': 512 for name in _model_names}
_UpperCamelCase							=      {f'''funnel-transformer/{name}''': {'do_lower_case': True} for name in _model_names}
class 		SCREAMING_SNAKE_CASE_							(    snake_case__   ):
				"""simple docstring"""
				__snake_case   :       Optional[Any]	 =		VOCAB_FILES_NAMES
				__snake_case   :       str	 =		PRETRAINED_VOCAB_FILES_MAP
				__snake_case   :       Tuple	 =		PRETRAINED_INIT_CONFIGURATION
				__snake_case   :       Any	 =		FunnelTokenizer
				__snake_case   :       Optional[Any]	 =		PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
				__snake_case   :       int	 =		2




				def __init__(     self  :Union[str, Any]						,    __lowercase  :Optional[int]=None						,    __lowercase  :Optional[Any]=None						,    __lowercase  :int=True						,    __lowercase  :int="<unk>"						,    __lowercase  :int="<sep>"						,    __lowercase  :Optional[Any]="<pad>"						,    __lowercase  :str="<cls>"						,    __lowercase  :int="<mask>"						,    __lowercase  :str="<s>"						,    __lowercase  :Union[str, Any]="</s>"						,    __lowercase  :Tuple=True						,    __lowercase  :List[str]=True						,    __lowercase  :List[Any]=None						,    __lowercase  :Optional[Any]="##"						,    **__lowercase  :Dict						,    ):
											super().__init__(
											    __lowercase						,    tokenizer_file=__lowercase						,    do_lower_case=__lowercase						,    unk_token=__lowercase						,    sep_token=__lowercase						,    pad_token=__lowercase						,    cls_token=__lowercase						,    mask_token=__lowercase						,    bos_token=__lowercase						,    eos_token=__lowercase						,    clean_text=__lowercase						,    tokenize_chinese_chars=__lowercase						,    strip_accents=__lowercase						,    wordpieces_prefix=__lowercase						,    **__lowercase						,    )

											__lowerCamelCase      :      Any         =json.loads(self.backend_tokenizer.normalizer.__getstate__()			)
											if (
											    normalizer_state.get('''lowercase'''						,    __lowercase			) != do_lower_case
											    or normalizer_state.get('''strip_accents'''						,    __lowercase			) != strip_accents
											    or normalizer_state.get('''handle_chinese_chars'''						,    __lowercase			) != tokenize_chinese_chars
											):
																		__lowerCamelCase      :      Any         =getattr(__lowercase						,    normalizer_state.pop('''type'''			)			)
																		__lowerCamelCase      :      Union[str, Any]         =do_lower_case
																		__lowerCamelCase      :      Optional[int]         =strip_accents
																		__lowerCamelCase      :      Optional[Any]         =tokenize_chinese_chars
																		__lowerCamelCase      :      Optional[int]         =normalizer_class(**__lowercase			)

											__lowerCamelCase      :      Dict         =do_lower_case




				def 					__lowercase  (     self  :Union[str, Any]						,    __lowercase  :Tuple						,    __lowercase  :int=None			):
											__lowerCamelCase      :      Optional[int]         =[self.cls_token_id] + token_ids_a + [self.sep_token_id]

											if token_ids_a:
																		output += token_ids_a + [self.sep_token_id]

											return output




				def 					__lowercase  (     self  :List[Any]						,    __lowercase  :List[int]						,    __lowercase  :Optional[List[int]] = None			):
											__lowerCamelCase      :      Any         =[self.sep_token_id]
											__lowerCamelCase      :      str         =[self.cls_token_id]
											if token_ids_a is None:
																		return len(cls			) * [self.cls_token_type_id] + len(token_ids_a + sep			) * [0]
											return len(cls			) * [self.cls_token_type_id] + len(token_ids_a + sep			) * [0] + len(token_ids_a + sep			) * [1]




				def 					__lowercase  (     self  :Optional[int]						,    __lowercase  :str						,    __lowercase  :Optional[str] = None			):
											__lowerCamelCase      :      Optional[Any]         =self._tokenizer.model.save(__lowercase						,    name=__lowercase			)
											return tuple(__lowercase			)











"""simple docstring"""





import gc
import unittest

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

from diffusers import AutoencoderKL, DDIMScheduler, LDMTextToImagePipeline, UNetaDConditionModel
from diffusers.utils.testing_utils import (
    enable_full_determinism,
    load_numpy,
    nightly,
    require_torch_gpu,
    slow,
    torch_device,
)

from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_PARAMS
from ..test_pipelines_common import PipelineTesterMixin


enable_full_determinism()
class 		SCREAMING_SNAKE_CASE_							(    snake_case__       ,  unittest.TestCase   ):
				"""simple docstring"""
				__snake_case   :       Union[str, Any]	 =		LDMTextToImagePipeline
				__snake_case   :       Optional[Any]	 =		TEXT_TO_IMAGE_PARAMS - {
				    """negative_prompt""",
				    """negative_prompt_embeds""",
				    """cross_attention_kwargs""",
				    """prompt_embeds""",
				}
				__snake_case   :       str	 =		PipelineTesterMixin.required_optional_params - {
				    """num_images_per_prompt""",
				    """callback""",
				    """callback_steps""",
				}
				__snake_case   :       Union[str, Any]	 =		TEXT_TO_IMAGE_BATCH_PARAMS
				__snake_case   :       Optional[Any]	 =		False




				def 					__lowercase  (     self  :List[str]			):
											torch.manual_seed(0			)
											__lowerCamelCase      :      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						,    )
											__lowerCamelCase      :      str         =DDIMScheduler(
											    beta_start=0.00085						,    beta_end=0.012						,    beta_schedule='''scaled_linear'''						,    clip_sample=__lowercase						,    set_alpha_to_one=__lowercase						,    )
											torch.manual_seed(0			)
											__lowerCamelCase      :      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						,    )
											torch.manual_seed(0			)
											__lowerCamelCase      :      Any         =CLIPTextConfig(
											    bos_token_id=0						,    eos_token_id=2						,    hidden_size=32						,    intermediate_size=37						,    layer_norm_eps=1e-0_5						,    num_attention_heads=4						,    num_hidden_layers=5						,    pad_token_id=1						,    vocab_size=1000						,    )
											__lowerCamelCase      :      Optional[int]         =CLIPTextModel(__lowercase			)
											__lowerCamelCase      :      Dict         =CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip'''			)

											__lowerCamelCase      :      Optional[int]         ={
											    '''unet''': unet,
											    '''scheduler''': scheduler,
											    '''vqvae''': vae,
											    '''bert''': text_encoder,
											    '''tokenizer''': tokenizer,
											}
											return components




				def 					__lowercase  (     self  :int						,    __lowercase  :Optional[int]						,    __lowercase  :Optional[Any]=0			):
											if str(__lowercase			).startswith('''mps'''			):
																		__lowerCamelCase      :      Any         =torch.manual_seed(__lowercase			)
											else:
																		__lowerCamelCase      :      str         =torch.Generator(device=__lowercase			).manual_seed(__lowercase			)
											__lowerCamelCase      :      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 					__lowercase  (     self  :List[str]			):
											__lowerCamelCase      :      List[str]         ='''cpu'''  # ensure determinism for the device-dependent torch.Generator

											__lowerCamelCase      :      str         =self.get_dummy_components()
											__lowerCamelCase      :      Optional[int]         =LDMTextToImagePipeline(**__lowercase			)
											pipe.to(__lowercase			)
											pipe.set_progress_bar_config(disable=__lowercase			)

											__lowerCamelCase      :      str         =self.get_dummy_inputs(__lowercase			)
											__lowerCamelCase      :      List[Any]         =pipe(**__lowercase			).images
											__lowerCamelCase      :      Optional[int]         =image[0, -3:, -3:, -1]

											assert image.shape == (1, 16, 16, 3)
											__lowerCamelCase      :      Optional[Any]         =np.array([0.6101, 0.6156, 0.5622, 0.4895, 0.6661, 0.3804, 0.5748, 0.6136, 0.5014]			)

											assert np.abs(image_slice.flatten() - expected_slice			).max() < 1e-3

@slow
@require_torch_gpu
class 		SCREAMING_SNAKE_CASE_							(    unittest.TestCase   ):
				"""simple docstring"""




				def 					__lowercase  (     self  :Any			):
											super().tearDown()
											gc.collect()
											torch.cuda.empty_cache()




				def 					__lowercase  (     self  :int						,    __lowercase  :Any						,    __lowercase  :Optional[int]=torch.floataa						,    __lowercase  :Dict=0			):
											__lowerCamelCase      :      List[str]         =torch.manual_seed(__lowercase			)
											__lowerCamelCase      :      List[str]         =np.random.RandomState(__lowercase			).standard_normal((1, 4, 32, 32)			)
											__lowerCamelCase      :      List[str]         =torch.from_numpy(__lowercase			).to(device=__lowercase						,    dtype=__lowercase			)
											__lowerCamelCase      :      Any         ={
											    '''prompt''': '''A painting of a squirrel eating a burger''',
											    '''latents''': latents,
											    '''generator''': generator,
											    '''num_inference_steps''': 3,
											    '''guidance_scale''': 6.0,
											    '''output_type''': '''numpy''',
											}
											return inputs




				def 					__lowercase  (     self  :Tuple			):
											__lowerCamelCase      :      int         =LDMTextToImagePipeline.from_pretrained('''CompVis/ldm-text2im-large-256'''			).to(__lowercase			)
											pipe.set_progress_bar_config(disable=__lowercase			)

											__lowerCamelCase      :      Tuple         =self.get_inputs(__lowercase			)
											__lowerCamelCase      :      Optional[Any]         =pipe(**__lowercase			).images
											__lowerCamelCase      :      Union[str, Any]         =image[0, -3:, -3:, -1].flatten()

											assert image.shape == (1, 256, 256, 3)
											__lowerCamelCase      :      Union[str, Any]         =np.array([0.51825, 0.52850, 0.52543, 0.54258, 0.52304, 0.52569, 0.54363, 0.55276, 0.56878]			)
											__lowerCamelCase      :      Dict         =np.abs(expected_slice - image_slice			).max()
											assert max_diff < 1e-3

@nightly
@require_torch_gpu
class 		SCREAMING_SNAKE_CASE_							(    unittest.TestCase   ):
				"""simple docstring"""




				def 					__lowercase  (     self  :Any			):
											super().tearDown()
											gc.collect()
											torch.cuda.empty_cache()




				def 					__lowercase  (     self  :Dict						,    __lowercase  :Optional[Any]						,    __lowercase  :int=torch.floataa						,    __lowercase  :Dict=0			):
											__lowerCamelCase      :      Any         =torch.manual_seed(__lowercase			)
											__lowerCamelCase      :      Dict         =np.random.RandomState(__lowercase			).standard_normal((1, 4, 32, 32)			)
											__lowerCamelCase      :      str         =torch.from_numpy(__lowercase			).to(device=__lowercase						,    dtype=__lowercase			)
											__lowerCamelCase      :      Dict         ={
											    '''prompt''': '''A painting of a squirrel eating a burger''',
											    '''latents''': latents,
											    '''generator''': generator,
											    '''num_inference_steps''': 50,
											    '''guidance_scale''': 6.0,
											    '''output_type''': '''numpy''',
											}
											return inputs




				def 					__lowercase  (     self  :Tuple			):
											__lowerCamelCase      :      Optional[int]         =LDMTextToImagePipeline.from_pretrained('''CompVis/ldm-text2im-large-256'''			).to(__lowercase			)
											pipe.set_progress_bar_config(disable=__lowercase			)

											__lowerCamelCase      :      List[Any]         =self.get_inputs(__lowercase			)
											__lowerCamelCase      :      Optional[int]         =pipe(**__lowercase			).images[0]

											__lowerCamelCase      :      Optional[int]         =load_numpy(
											    '''https://huggingface.co/datasets/diffusers/test-arrays/resolve/main/ldm_text2img/ldm_large_256_ddim.npy'''			)
											__lowerCamelCase      :      Dict         =np.abs(expected_image - image			).max()
											assert max_diff < 1e-3




