
'''simple docstring'''

import unittest

import numpy as np
import torch
from torch import nn
from transformers import (
    CLIPImageProcessor,
    CLIPTextConfig,
    CLIPTextModelWithProjection,
    CLIPTokenizer,
    CLIPVisionConfig,
    CLIPVisionModelWithProjection,
)

from diffusers import KandinskyPriorPipeline, PriorTransformer, UnCLIPScheduler
from diffusers.utils import torch_device
from diffusers.utils.testing_utils import enable_full_determinism, skip_mps

from ..test_pipelines_common import PipelineTesterMixin


enable_full_determinism()






class    A     (	SCREAMING_SNAKE_CASE__  ,      unittest.TestCase    ):
	snake_case__      :List[str]        =     KandinskyPriorPipeline
	snake_case__      :Optional[Any]        =     ['prompt']
	snake_case__      :List[Any]        =     ['prompt', 'negative_prompt']
	snake_case__      :Tuple        =     [
	    'num_images_per_prompt',
	    'generator',
	    'num_inference_steps',
	    'latents',
	    'negative_prompt',
	    'guidance_scale',
	    'output_type',
	    'return_dict',
	]
	snake_case__      :List[Any]        =     False





	@property
	def 							__SCREAMING_SNAKE_CASE  (					self			:       int							):


				"""simple docstring"""

				return 32





	@property
	def 							__SCREAMING_SNAKE_CASE  (					self			:       Union[str, Any]							):


				"""simple docstring"""

				return 32





	@property
	def 							__SCREAMING_SNAKE_CASE  (					self			:       Any							):


				"""simple docstring"""

				return self.time_input_dim





	@property
	def 							__SCREAMING_SNAKE_CASE  (					self			:       Union[str, Any]							):


				"""simple docstring"""

				return self.time_input_dim * 4





	@property
	def 							__SCREAMING_SNAKE_CASE  (					self			:       Union[str, Any]							):


				"""simple docstring"""

				return 100





	@property
	def 							__SCREAMING_SNAKE_CASE  (					self			:       int							):


				"""simple docstring"""

				lowerCAmelCase__								=							CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip"							)
				return tokenizer





	@property
	def 							__SCREAMING_SNAKE_CASE  (					self			:       List[Any]							):


				"""simple docstring"""

				torch.manual_seed(0							)
				lowerCAmelCase__								=							CLIPTextConfig(
				    bos_token_id=0      ,							eos_token_id=2      ,							hidden_size=self.text_embedder_hidden_size      ,							projection_dim=self.text_embedder_hidden_size      ,							intermediate_size=37      ,							layer_norm_eps=1E-05      ,							num_attention_heads=4      ,							num_hidden_layers=5      ,							pad_token_id=1      ,							vocab_size=1000      ,							)
				return CLIPTextModelWithProjection(__magic_name__							)





	@property
	def 							__SCREAMING_SNAKE_CASE  (					self			:       Tuple							):


				"""simple docstring"""

				torch.manual_seed(0							)

				lowerCAmelCase__								=							{
				    "num_attention_heads": 2,
				    "attention_head_dim": 12,
				    "embedding_dim": self.text_embedder_hidden_size,
				    "num_layers": 1,
				}

				lowerCAmelCase__								=							PriorTransformer(**__magic_name__							)
				# clip_std and clip_mean is initialized to be 0 so PriorTransformer.post_process_latents will always return 0 - set clip_std to be 1 so it won't return 0
				lowerCAmelCase__								=							nn.Parameter(torch.ones(model.clip_std.shape							)							)
				return model





	@property
	def 							__SCREAMING_SNAKE_CASE  (					self			:       Optional[int]							):


				"""simple docstring"""

				torch.manual_seed(0							)
				lowerCAmelCase__								=							CLIPVisionConfig(
				    hidden_size=self.text_embedder_hidden_size      ,							image_size=224      ,							projection_dim=self.text_embedder_hidden_size      ,							intermediate_size=37      ,							num_attention_heads=4      ,							num_channels=3      ,							num_hidden_layers=5      ,							patch_size=14      ,							)

				lowerCAmelCase__								=							CLIPVisionModelWithProjection(__magic_name__							)
				return model





	@property
	def 							__SCREAMING_SNAKE_CASE  (					self			:       Optional[int]							):


				"""simple docstring"""

				lowerCAmelCase__								=							CLIPImageProcessor(
				    crop_size=224      ,							do_center_crop=__magic_name__      ,							do_normalize=__magic_name__      ,							do_resize=__magic_name__      ,							image_mean=[0.4814_5466, 0.457_8275, 0.4082_1073]      ,							image_std=[0.2686_2954, 0.2613_0258, 0.2757_7711]      ,							resample=3      ,							size=224      ,							)

				return image_processor





	def 							__SCREAMING_SNAKE_CASE  (					self			:       Any							):


				"""simple docstring"""

				lowerCAmelCase__								=							self.dummy_prior
				lowerCAmelCase__								=							self.dummy_image_encoder
				lowerCAmelCase__								=							self.dummy_text_encoder
				lowerCAmelCase__								=							self.dummy_tokenizer
				lowerCAmelCase__								=							self.dummy_image_processor

				lowerCAmelCase__								=							UnCLIPScheduler(
				    variance_type="fixed_small_log"      ,							prediction_type="sample"      ,							num_train_timesteps=1000      ,							clip_sample=__magic_name__      ,							clip_sample_range=10.0      ,							)

				lowerCAmelCase__								=							{
				    "prior": prior,
				    "image_encoder": image_encoder,
				    "text_encoder": text_encoder,
				    "tokenizer": tokenizer,
				    "scheduler": scheduler,
				    "image_processor": image_processor,
				}

				return components





	def 							__SCREAMING_SNAKE_CASE  (					self			:       List[str]      ,							__magic_name__			:       Tuple      ,							__magic_name__			:       Union[str, Any]=0							):


				"""simple docstring"""

				if str(__magic_name__							).startswith("mps"							):
							lowerCAmelCase__								=							torch.manual_seed(__magic_name__							)
				else:
							lowerCAmelCase__								=							torch.Generator(device=__magic_name__							).manual_seed(__magic_name__							)
				lowerCAmelCase__								=							{
				    "prompt": "horse",
				    "generator": generator,
				    "guidance_scale": 4.0,
				    "num_inference_steps": 2,
				    "output_type": "np",
				}
				return inputs





	def 							__SCREAMING_SNAKE_CASE  (					self			:       Optional[Any]							):


				"""simple docstring"""

				lowerCAmelCase__								=							"cpu"

				lowerCAmelCase__								=							self.get_dummy_components()

				lowerCAmelCase__								=							self.pipeline_class(**__magic_name__							)
				lowerCAmelCase__								=							pipe.to(__magic_name__							)

				pipe.set_progress_bar_config(disable=__magic_name__							)

				lowerCAmelCase__								=							pipe(**self.get_dummy_inputs(__magic_name__							)							)
				lowerCAmelCase__								=							output.image_embeds

				lowerCAmelCase__								=							pipe(
				    **self.get_dummy_inputs(__magic_name__							)      ,							return_dict=__magic_name__      ,							)[0]

				lowerCAmelCase__								=							image[0, -10:]
				lowerCAmelCase__								=							image_from_tuple[0, -10:]

				assert image.shape == (1, 32)

				lowerCAmelCase__								=							np.array(
				    [-0.0532, 1.7120, 0.3656, -1.0852, -0.8946, -1.1756, 0.4348, 0.2482, 0.5146, -0.1156]							)

				assert np.abs(image_slice.flatten() - expected_slice							).max() < 1E-2
				assert np.abs(image_from_tuple_slice.flatten() - expected_slice							).max() < 1E-2





	@skip_mps
	def 							__SCREAMING_SNAKE_CASE  (					self			:       Optional[Any]							):


				"""simple docstring"""

				lowerCAmelCase__								=							torch_device == "cpu"
				lowerCAmelCase__								=							True
				lowerCAmelCase__								=							False

				self._test_inference_batch_single_identical(
				    test_max_difference=__magic_name__      ,							relax_max_difference=__magic_name__      ,							test_mean_pixel_difference=__magic_name__      ,							)







	@skip_mps
	def 							__SCREAMING_SNAKE_CASE  (					self			:       Union[str, Any]							):


				"""simple docstring"""

				lowerCAmelCase__								=							torch_device == "cpu"
				lowerCAmelCase__								=							False

				self._test_attention_slicing_forward_pass(
				    test_max_difference=__magic_name__      ,							test_mean_pixel_difference=__magic_name__      ,							)







'''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_layoutlm import LayoutLMTokenizer


UpperCAmelCase__   : Optional[Any]					=		logging.get_logger(__name__)

UpperCAmelCase__   : str					=		{"vocab_file": "vocab.txt", "tokenizer_file": "tokenizer.json"}

UpperCAmelCase__   : Union[str, Any]					=		{
    "vocab_file": {
        "microsoft/layoutlm-base-uncased": (
            "https://huggingface.co/microsoft/layoutlm-base-uncased/resolve/main/vocab.txt"
        ),
        "microsoft/layoutlm-large-uncased": (
            "https://huggingface.co/microsoft/layoutlm-large-uncased/resolve/main/vocab.txt"
        ),
    },
    "tokenizer_file": {
        "microsoft/layoutlm-base-uncased": (
            "https://huggingface.co/microsoft/layoutlm-base-uncased/resolve/main/tokenizer.json"
        ),
        "microsoft/layoutlm-large-uncased": (
            "https://huggingface.co/microsoft/layoutlm-large-uncased/resolve/main/tokenizer.json"
        ),
    },
}

UpperCAmelCase__   : List[Any]					=		{
    "microsoft/layoutlm-base-uncased": 5_12,
    "microsoft/layoutlm-large-uncased": 5_12,
}

UpperCAmelCase__   : List[str]					=		{
    "microsoft/layoutlm-base-uncased": {"do_lower_case": True},
    "microsoft/layoutlm-large-uncased": {"do_lower_case": True},
}






class    A     (	SCREAMING_SNAKE_CASE__    ):
	snake_case__      :Union[str, Any]        =     VOCAB_FILES_NAMES
	snake_case__      :List[str]        =     PRETRAINED_VOCAB_FILES_MAP
	snake_case__      :Union[str, Any]        =     PRETRAINED_INIT_CONFIGURATION
	snake_case__      :int        =     PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
	snake_case__      :int        =     LayoutLMTokenizer





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


				"""simple docstring"""

				super().__init__(
				    __magic_name__      ,							tokenizer_file=__magic_name__      ,							do_lower_case=__magic_name__      ,							unk_token=__magic_name__      ,							sep_token=__magic_name__      ,							pad_token=__magic_name__      ,							cls_token=__magic_name__      ,							mask_token=__magic_name__      ,							tokenize_chinese_chars=__magic_name__      ,							strip_accents=__magic_name__      ,							**__magic_name__      ,							)

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

				lowerCAmelCase__								=							do_lower_case





	def 							__SCREAMING_SNAKE_CASE  (					self			:       List[str]      ,							__magic_name__			:       Optional[int]      ,							__magic_name__			:       List[str]=None							):


				"""simple docstring"""

				lowerCAmelCase__								=							[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 							__SCREAMING_SNAKE_CASE  (					self			:       Tuple      ,							__magic_name__			:       List[int]      ,							__magic_name__			:       Optional[List[int]] = None							):


				"""simple docstring"""

				lowerCAmelCase__								=							[self.sep_token_id]
				lowerCAmelCase__								=							[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			:       Optional[Any]      ,							__magic_name__			:       str      ,							__magic_name__			:       Optional[str] = None							):


				"""simple docstring"""

				lowerCAmelCase__								=							self._tokenizer.model.save(__magic_name__      ,							name=__magic_name__							)
				return tuple(__magic_name__							)





