
"""simple docstring"""





import gc
import unittest

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

from diffusers import (
    AutoencoderKL,
    DDIMScheduler,
    StableDiffusionAttendAndExcitePipeline,
    UNetaDConditionModel,
)
from diffusers.utils import load_numpy, skip_mps, slow
from diffusers.utils.testing_utils import 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


UpperCamelCase_					:						Any				= False



@skip_mps
class __lowerCAmelCase				(					__SCREAMING_SNAKE_CASE     ,				__SCREAMING_SNAKE_CASE     ,				__SCREAMING_SNAKE_CASE     ,				unittest.TestCase      ):




   """simple docstring"""







   snake_case	      =				StableDiffusionAttendAndExcitePipeline
   snake_case	      =				False
   snake_case	      =				TEXT_TO_IMAGE_PARAMS
   snake_case	      =				TEXT_TO_IMAGE_BATCH_PARAMS.union({"token_indices"}      )
   snake_case	      =				TEXT_TO_IMAGE_IMAGE_PARAMS
   snake_case	      =				TEXT_TO_IMAGE_IMAGE_PARAMS




   @classmethod
   def      lowerCamelCase__     (				cls						:					Optional[int]	)      -> Tuple:







        """simple docstring"""
        super().setUpClass()
        torch.use_deterministic_algorithms(_a	)




   @classmethod
   def      lowerCamelCase__     (				cls						:					int	)      -> Optional[int]:







        """simple docstring"""
        super().tearDownClass()
        torch.use_deterministic_algorithms(_a	)




   def      lowerCamelCase__     (				self						:					Dict	)      -> Dict:







        """simple docstring"""
        torch.manual_seed(0	)
        A_				 =						UNetaDConditionModel(
            block_out_channels=(32, 64)       ,   layers_per_block=1       ,   sample_size=32       ,   in_channels=4       ,   out_channels=4       ,   down_block_types=("DownBlock2D", "CrossAttnDownBlock2D")       ,   up_block_types=("CrossAttnUpBlock2D", "UpBlock2D")       ,   cross_attention_dim=32       ,   attention_head_dim=(2, 4)       ,   use_linear_projection=_a       ,   )
        A_				 =						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	)
        A_				 =						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       ,   sample_size=128       ,   )
        torch.manual_seed(0	)
        A_				 =						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       ,   hidden_act="gelu"       ,   projection_dim=512       ,   )
        A_				 =						CLIPTextModel(_a	)
        A_				 =						CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip"	)

        A_				 =						{
            "unet": unet,
            "scheduler": scheduler,
            "vae": vae,
            "text_encoder": text_encoder,
            "tokenizer": tokenizer,
            "safety_checker": None,
            "feature_extractor": None,
        }

        return components




   def      lowerCamelCase__     (				self						:					int       ,   _snake_case						:					Optional[Any]       ,   _snake_case						:					Optional[int]=0	)      -> Tuple:







        """simple docstring"""
        if str(_a	).startswith("mps"	):
             A_				 =						torch.manual_seed(_a	)
        else:
             A_				 =						torch.Generator(device=_a	).manual_seed(_a	)
        A_				 =						A_				 =						{
            "prompt": "a cat and a frog",
            "token_indices": [2, 5],
            "generator": generator,
            "num_inference_steps": 1,
            "guidance_scale": 6.0,
            "output_type": "numpy",
            "max_iter_to_alter": 2,
            "thresholds": {0: 0.7},
        }
        return inputs




   def      lowerCamelCase__     (				self						:					List[str]	)      -> Any:







        """simple docstring"""
        A_				 =						"cpu"

        A_				 =						self.get_dummy_components()
        A_				 =						self.pipeline_class(**_a	)
        pipe.to(_a	)
        pipe.set_progress_bar_config(disable=_a	)

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

        self.assertEqual(image.shape       ,   (1, 64, 64, 3)	)
        A_				 =						np.array(
            [0.6_3_9_0_5_3_6_4, 0.6_2_8_9_7_3_0_7, 0.4_8_5_9_9_0_1_7, 0.5_1_3_3_6_2_4, 0.5_5_5_0_0_4_8, 0.4_5_7_6_9_5_1_6, 0.5_0_3_2_6_9_7_3, 0.5_0_2_3_1_3_9, 0.4_5_3_8_4_4_9_6]	)
        A_				 =						np.abs(image_slice.flatten() - expected_slice	).max()
        self.assertLessEqual(_a       ,   1e-3	)




   def      lowerCamelCase__     (				self						:					str	)      -> List[Any]:







        """simple docstring"""
        super().test_cpu_offload_forward_pass(expected_max_diff=5e-4	)




   def      lowerCamelCase__     (				self						:					Dict	)      -> Tuple:







        """simple docstring"""
        # NOTE: Larger batch sizes cause this test to timeout, only test on smaller batches
        self._test_inference_batch_consistent(batch_sizes=[1, 2]	)




   def      lowerCamelCase__     (				self						:					Any	)      -> str:







        """simple docstring"""
        self._test_inference_batch_single_identical(batch_size=2       ,   expected_max_diff=7e-4	)




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







        """simple docstring"""
        super().test_dict_tuple_outputs_equivalent(expected_max_difference=3e-3	)




   def      lowerCamelCase__     (				self						:					List[str]	)      -> Tuple:







        """simple docstring"""
        super().test_pt_np_pil_outputs_equivalent(expected_max_diff=5e-4	)




   def      lowerCamelCase__     (				self						:					Optional[int]	)      -> Dict:







        """simple docstring"""
        super().test_save_load_local(expected_max_difference=5e-4	)






   def      lowerCamelCase__     (				self						:					int	)      -> Tuple:







        """simple docstring"""
        super().test_save_load_optional_components(expected_max_difference=4e-4	)





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




   """simple docstring"""







   @classmethod
   def      lowerCamelCase__     (				cls						:					Optional[Any]	)      -> Dict:







        """simple docstring"""
        super().setUpClass()
        torch.use_deterministic_algorithms(_a	)




   @classmethod
   def      lowerCamelCase__     (				cls						:					List[str]	)      -> Union[str, Any]:







        """simple docstring"""
        super().tearDownClass()
        torch.use_deterministic_algorithms(_a	)




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







        """simple docstring"""
        super().tearDown()
        gc.collect()
        torch.cuda.empty_cache()






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







        """simple docstring"""
        A_				 =						torch.manual_seed(51	)

        A_				 =						StableDiffusionAttendAndExcitePipeline.from_pretrained(
            "CompVis/stable-diffusion-v1-4"       ,   safety_checker=_a       ,   torch_dtype=torch.floataa	)
        pipe.to("cuda"	)

        A_				 =						"a painting of an elephant with glasses"
        A_				 =						[5, 7]

        A_				 =						pipe(
            prompt=_a       ,   token_indices=_a       ,   guidance_scale=7.5       ,   generator=_a       ,   num_inference_steps=5       ,   max_iter_to_alter=5       ,   output_type="numpy"       ,   ).images[0]

        A_				 =						load_numpy(
            "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/attend-and-excite/elephant_glasses.npy"	)
        assert np.abs((expected_image - image).max()	) < 5e-1






"""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_retribert import RetriBertTokenizer


UpperCamelCase_					:						Any				= logging.get_logger(__name__)

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

UpperCamelCase_					:						List[str]				= {
    '''vocab_file''': {
        '''yjernite/retribert-base-uncased''': (
            '''https://huggingface.co/yjernite/retribert-base-uncased/resolve/main/vocab.txt'''
        ),
    },
    '''tokenizer_file''': {
        '''yjernite/retribert-base-uncased''': (
            '''https://huggingface.co/yjernite/retribert-base-uncased/resolve/main/tokenizer.json'''
        ),
    },
}

UpperCamelCase_					:						Union[str, Any]				= {
    '''yjernite/retribert-base-uncased''': 512,
}


UpperCamelCase_					:						str				= {
    '''yjernite/retribert-base-uncased''': {'''do_lower_case''': True},
}



class __lowerCAmelCase				(					_lowercase      ):




			"""simple docstring"""







			snake_case	      =				VOCAB_FILES_NAMES
			snake_case	      =				PRETRAINED_VOCAB_FILES_MAP
			snake_case	      =				PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
			snake_case	      =				PRETRAINED_INIT_CONFIGURATION
			snake_case	      =				RetriBertTokenizer
			snake_case	      =				["input_ids", "attention_mask"]




			def __init__(				self						:					Tuple       ,   _snake_case						:					int=None       ,   _snake_case						:					Any=None       ,   _snake_case						:					str=True       ,   _snake_case						:					List[Any]="[UNK]"       ,   _snake_case						:					Optional[Any]="[SEP]"       ,   _snake_case						:					str="[PAD]"       ,   _snake_case						:					List[str]="[CLS]"       ,   _snake_case						:					List[str]="[MASK]"       ,   _snake_case						:					Dict=True       ,   _snake_case						:					Optional[int]=None       ,   **_snake_case						:					Tuple       ,   )      -> Union[str, Any]:







								"""simple docstring"""
								super().__init__(
								    _snake_case       ,   tokenizer_file=_snake_case       ,   do_lower_case=_snake_case       ,   unk_token=_snake_case       ,   sep_token=_snake_case       ,   pad_token=_snake_case       ,   cls_token=_snake_case       ,   mask_token=_snake_case       ,   tokenize_chinese_chars=_snake_case       ,   strip_accents=_snake_case       ,   **_snake_case       ,   )

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

								A_				 =						do_lower_case




			def      lowerCamelCase__     (				self						:					Union[str, Any]       ,   _snake_case						:					List[Any]       ,   _snake_case						:					Union[str, Any]=None	)      -> List[Any]:







								"""simple docstring"""
								A_				 =						[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      lowerCamelCase__     (				self						:					Any       ,   _snake_case						:					List[int]       ,   _snake_case						:					Optional[List[int]] = None	)      -> List[int]:







								"""simple docstring"""
								A_				 =						[self.sep_token_id]
								A_				 =						[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      lowerCamelCase__     (				self						:					Union[str, Any]       ,   _snake_case						:					str       ,   _snake_case						:					Optional[str] = None	)      -> Tuple[str]:







								"""simple docstring"""
								A_				 =						self._tokenizer.model.save(_snake_case       ,   name=_snake_case	)
								return tuple(_snake_case	)




