


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




		'''simple docstring'''







		SCREAMING_SNAKE_CASE_						:     Optional[Any]								=     LDMTextToImagePipeline
		SCREAMING_SNAKE_CASE_						:     int								=     TEXT_TO_IMAGE_PARAMS - {
		    """negative_prompt""",
		    """negative_prompt_embeds""",
		    """cross_attention_kwargs""",
		    """prompt_embeds""",
		}
		SCREAMING_SNAKE_CASE_						:     Any								=     PipelineTesterMixin.required_optional_params - {
		    """num_images_per_prompt""",
		    """callback""",
		    """callback_steps""",
		}
		SCREAMING_SNAKE_CASE_						:     List[Any]								=     TEXT_TO_IMAGE_BATCH_PARAMS
		SCREAMING_SNAKE_CASE_						:     Optional[Any]								=     False







		def 							__A						(	self				)     ->							List[Any]:
								torch.manual_seed(0				)
								SCREAMING_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						,       )
								SCREAMING_SNAKE_CASE													=					DDIMScheduler(
								    beta_start=0.0_00_85						,       beta_end=0.0_12						,       beta_schedule='scaled_linear'						,       clip_sample=lowerCAmelCase__						,       set_alpha_to_one=lowerCAmelCase__						,       )
								torch.manual_seed(0				)
								SCREAMING_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						,       )
								torch.manual_seed(0				)
								SCREAMING_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=1_000						,       )
								SCREAMING_SNAKE_CASE													=					CLIPTextModel(lowerCAmelCase__				)
								SCREAMING_SNAKE_CASE													=					CLIPTokenizer.from_pretrained('hf-internal-testing/tiny-random-clip'				)

								SCREAMING_SNAKE_CASE													=					{
								    'unet': unet,
								    'scheduler': scheduler,
								    'vqvae': vae,
								    'bert': text_encoder,
								    'tokenizer': tokenizer,
								}
								return components







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

								SCREAMING_SNAKE_CASE													=					self.get_dummy_components()
								SCREAMING_SNAKE_CASE													=					LDMTextToImagePipeline(**lowerCAmelCase__				)
								pipe.to(lowerCAmelCase__				)
								pipe.set_progress_bar_config(disable=lowerCAmelCase__				)

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

								assert image.shape == (1, 16, 16, 3)
								SCREAMING_SNAKE_CASE													=					np.array([0.61_01, 0.61_56, 0.56_22, 0.48_95, 0.66_61, 0.38_04, 0.57_48, 0.61_36, 0.50_14]				)

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





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




		'''simple docstring'''







		def 							__A						(	self				)     ->							Dict:
								super().tearDown()
								gc.collect()
								torch.cuda.empty_cache()







		def 							__A						(	self						,       lowerCAmelCase__						,       lowerCAmelCase__=torch.floataa						,       lowerCAmelCase__=0				)     ->							Optional[int]:
								SCREAMING_SNAKE_CASE													=					torch.manual_seed(lowerCAmelCase__				)
								SCREAMING_SNAKE_CASE													=					np.random.RandomState(lowerCAmelCase__				).standard_normal((1, 4, 32, 32)				)
								SCREAMING_SNAKE_CASE													=					torch.from_numpy(lowerCAmelCase__				).to(device=lowerCAmelCase__						,       dtype=lowerCAmelCase__				)
								SCREAMING_SNAKE_CASE													=					{
								    '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 							__A						(	self				)     ->							Optional[Any]:
								SCREAMING_SNAKE_CASE													=					LDMTextToImagePipeline.from_pretrained('CompVis/ldm-text2im-large-256'				).to(lowerCAmelCase__				)
								pipe.set_progress_bar_config(disable=lowerCAmelCase__				)

								SCREAMING_SNAKE_CASE													=					self.get_inputs(lowerCAmelCase__				)
								SCREAMING_SNAKE_CASE													=					pipe(**lowerCAmelCase__				).images
								SCREAMING_SNAKE_CASE													=					image[0, -3:, -3:, -1].flatten()

								assert image.shape == (1, 256, 256, 3)
								SCREAMING_SNAKE_CASE													=					np.array([0.5_18_25, 0.5_28_50, 0.5_25_43, 0.5_42_58, 0.5_23_04, 0.5_25_69, 0.5_43_63, 0.5_52_76, 0.5_68_78]				)
								SCREAMING_SNAKE_CASE													=					np.abs(expected_slice - image_slice				).max()
								assert max_diff < 1e-3





@nightly
@require_torch_gpu
class 					lowerCAmelCase	(					unittest.TestCase    ):




		'''simple docstring'''







		def 							__A						(	self				)     ->							Union[str, Any]:
								super().tearDown()
								gc.collect()
								torch.cuda.empty_cache()







		def 							__A						(	self						,       lowerCAmelCase__						,       lowerCAmelCase__=torch.floataa						,       lowerCAmelCase__=0				)     ->							Dict:
								SCREAMING_SNAKE_CASE													=					torch.manual_seed(lowerCAmelCase__				)
								SCREAMING_SNAKE_CASE													=					np.random.RandomState(lowerCAmelCase__				).standard_normal((1, 4, 32, 32)				)
								SCREAMING_SNAKE_CASE													=					torch.from_numpy(lowerCAmelCase__				).to(device=lowerCAmelCase__						,       dtype=lowerCAmelCase__				)
								SCREAMING_SNAKE_CASE													=					{
								    '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 							__A						(	self				)     ->							Optional[Any]:
								SCREAMING_SNAKE_CASE													=					LDMTextToImagePipeline.from_pretrained('CompVis/ldm-text2im-large-256'				).to(lowerCAmelCase__				)
								pipe.set_progress_bar_config(disable=lowerCAmelCase__				)

								SCREAMING_SNAKE_CASE													=					self.get_inputs(lowerCAmelCase__				)
								SCREAMING_SNAKE_CASE													=					pipe(**lowerCAmelCase__				).images[0]

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






"""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 					lowerCAmelCase	(					lowerCamelCase_    ):




		'''simple docstring'''







		SCREAMING_SNAKE_CASE_						:     int								=     VOCAB_FILES_NAMES
		SCREAMING_SNAKE_CASE_						:     List[str]								=     PRETRAINED_VOCAB_FILES_MAP
		SCREAMING_SNAKE_CASE_						:     List[Any]								=     PRETRAINED_INIT_CONFIGURATION
		SCREAMING_SNAKE_CASE_						:     str								=     FunnelTokenizer
		SCREAMING_SNAKE_CASE_						:     Dict								=     PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
		SCREAMING_SNAKE_CASE_						:     int								=     2







		def __init__(	self						,       lowerCAmelCase__=None						,       lowerCAmelCase__=None						,       lowerCAmelCase__=True						,       lowerCAmelCase__="<unk>"						,       lowerCAmelCase__="<sep>"						,       lowerCAmelCase__="<pad>"						,       lowerCAmelCase__="<cls>"						,       lowerCAmelCase__="<mask>"						,       lowerCAmelCase__="<s>"						,       lowerCAmelCase__="</s>"						,       lowerCAmelCase__=True						,       lowerCAmelCase__=True						,       lowerCAmelCase__=None						,       lowerCAmelCase__="##"						,       **lowerCAmelCase__						,       )     ->							Tuple:
								super().__init__(
								    lowerCAmelCase__						,       tokenizer_file=lowerCAmelCase__						,       do_lower_case=lowerCAmelCase__						,       unk_token=lowerCAmelCase__						,       sep_token=lowerCAmelCase__						,       pad_token=lowerCAmelCase__						,       cls_token=lowerCAmelCase__						,       mask_token=lowerCAmelCase__						,       bos_token=lowerCAmelCase__						,       eos_token=lowerCAmelCase__						,       clean_text=lowerCAmelCase__						,       tokenize_chinese_chars=lowerCAmelCase__						,       strip_accents=lowerCAmelCase__						,       wordpieces_prefix=lowerCAmelCase__						,       **lowerCAmelCase__						,       )

								SCREAMING_SNAKE_CASE													=					json.loads(self.backend_tokenizer.normalizer.__getstate__()				)
								if (
								    normalizer_state.get('lowercase'						,       lowerCAmelCase__				) != do_lower_case
								    or normalizer_state.get('strip_accents'						,       lowerCAmelCase__				) != strip_accents
								    or normalizer_state.get('handle_chinese_chars'						,       lowerCAmelCase__				) != tokenize_chinese_chars
								):
														SCREAMING_SNAKE_CASE													=					getattr(lowerCAmelCase__						,       normalizer_state.pop('type'				)				)
														SCREAMING_SNAKE_CASE													=					do_lower_case
														SCREAMING_SNAKE_CASE													=					strip_accents
														SCREAMING_SNAKE_CASE													=					tokenize_chinese_chars
														SCREAMING_SNAKE_CASE													=					normalizer_class(**lowerCAmelCase__				)

								SCREAMING_SNAKE_CASE													=					do_lower_case







		def 							__A						(	self						,       lowerCAmelCase__						,       lowerCAmelCase__=None				)     ->							Optional[Any]:
								SCREAMING_SNAKE_CASE													=					[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 							__A						(	self						,       lowerCAmelCase__						,       lowerCAmelCase__ = None				)     ->							List[int]:
								SCREAMING_SNAKE_CASE													=					[self.sep_token_id]
								SCREAMING_SNAKE_CASE													=					[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 							__A						(	self						,       lowerCAmelCase__						,       lowerCAmelCase__ = None				)     ->							Tuple[str]:
								SCREAMING_SNAKE_CASE													=					self._tokenizer.model.save(lowerCAmelCase__						,       name=lowerCAmelCase__				)
								return tuple(lowerCAmelCase__				)


