


'''simple docstring'''

import random
import unittest

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

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

from ..test_pipelines_common import PipelineTesterMixin


enable_full_determinism()
class 						__lowerCAmelCase		(a_						,  unittest.TestCase  ):





      '''simple docstring'''






      lowerCAmelCase__      :  str						      =							KandinskyVaaPriorEmbaEmbPipeline
      lowerCAmelCase__      :  Dict						      =							['prompt', 'image']
      lowerCAmelCase__      :  List[Any]						      =							['prompt', 'image']
      lowerCAmelCase__      :  List[Any]						      =							[
          'num_images_per_prompt',
          'strength',
          'generator',
          'num_inference_steps',
          'latents',
          'negative_prompt',
          'guidance_scale',
          'output_type',
          'return_dict',
      ]
      lowerCAmelCase__      :  List[str]						      =							False



      @property
      def    UpperCamelCase__	(self					:					Any				):

             '''simple docstring'''







             return 32



      @property
      def    UpperCamelCase__	(self					:					Dict				):

             '''simple docstring'''







             return 32



      @property
      def    UpperCamelCase__	(self					:					Dict				):

             '''simple docstring'''







             return self.time_input_dim



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

             '''simple docstring'''







             return self.time_input_dim * 4



      @property
      def    UpperCamelCase__	(self					:					Dict				):

             '''simple docstring'''







             return 100



      @property
      def    UpperCamelCase__	(self					:					Dict				):

             '''simple docstring'''







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



      @property
      def    UpperCamelCase__	(self					:					Any				):

             '''simple docstring'''







             torch.manual_seed(0				)
             lowercase__       =				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(_snake_case				)



      @property
      def    UpperCamelCase__	(self					:					str				):

             '''simple docstring'''







             torch.manual_seed(0				)

             lowercase__       =				{
                 '''num_attention_heads''': 2,
                 '''attention_head_dim''': 12,
                 '''embedding_dim''': self.text_embedder_hidden_size,
                 '''num_layers''': 1,
             }

             lowercase__       =				PriorTransformer(**_snake_case				)
             # 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
             lowercase__       =				nn.Parameter(torch.ones(model.clip_std.shape				)				)
             return model



      @property
      def    UpperCamelCase__	(self					:					int				):

             '''simple docstring'''







             torch.manual_seed(0				)
             lowercase__       =				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     ,		)

             lowercase__       =				CLIPVisionModelWithProjection(_snake_case				)
             return model



      @property
      def    UpperCamelCase__	(self					:					Optional[Any]				):

             '''simple docstring'''







             lowercase__       =				CLIPImageProcessor(
                 crop_size=224     ,		do_center_crop=_snake_case     ,		do_normalize=_snake_case     ,		do_resize=_snake_case     ,		image_mean=[0.48_14_54_66, 0.4_57_82_75, 0.40_82_10_73]     ,		image_std=[0.26_86_29_54, 0.26_13_02_58, 0.27_57_77_11]     ,		resample=3     ,		size=224     ,		)

             return image_processor



      def    UpperCamelCase__	(self					:					Optional[Any]				):

             '''simple docstring'''







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

             lowercase__       =				UnCLIPScheduler(
                 variance_type='''fixed_small_log'''     ,		prediction_type='''sample'''     ,		num_train_timesteps=1000     ,		clip_sample=_snake_case     ,		clip_sample_range=10.0     ,		)

             lowercase__       =				{
                 '''prior''': prior,
                 '''image_encoder''': image_encoder,
                 '''text_encoder''': text_encoder,
                 '''tokenizer''': tokenizer,
                 '''scheduler''': scheduler,
                 '''image_processor''': image_processor,
             }

             return components



      def    UpperCamelCase__	(self					:					Optional[int]     ,		UpperCamelCase					:					Any     ,		UpperCamelCase					:					Tuple=0				):

             '''simple docstring'''







             if str(_snake_case				).startswith('''mps'''				):
                    lowercase__       =				torch.manual_seed(_snake_case				)
             else:
                    lowercase__       =				torch.Generator(device=_snake_case				).manual_seed(_snake_case				)

             lowercase__       =				floats_tensor((1, 3, 64, 64)     ,		rng=random.Random(_snake_case				)				).to(_snake_case				)
             lowercase__       =				image.cpu().permute(0     ,		2     ,		3     ,		1				)[0]
             lowercase__       =				Image.fromarray(np.uinta(_snake_case				)				).convert('''RGB'''				).resize((256, 256)				)

             lowercase__       =				{
                 '''prompt''': '''horse''',
                 '''image''': init_image,
                 '''strength''': 0.5,
                 '''generator''': generator,
                 '''guidance_scale''': 4.0,
                 '''num_inference_steps''': 2,
                 '''output_type''': '''np''',
             }
             return inputs



      def    UpperCamelCase__	(self					:					int				):

             '''simple docstring'''







             lowercase__       =				'''cpu'''

             lowercase__       =				self.get_dummy_components()

             lowercase__       =				self.pipeline_class(**_snake_case				)
             lowercase__       =				pipe.to(_snake_case				)

             pipe.set_progress_bar_config(disable=_snake_case				)

             lowercase__       =				pipe(**self.get_dummy_inputs(_snake_case				)				)
             lowercase__       =				output.image_embeds

             lowercase__       =				pipe(
                 **self.get_dummy_inputs(_snake_case				)     ,		return_dict=_snake_case     ,		)[0]

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

             assert image.shape == (1, 32)

             lowercase__       =				np.array(
                 [
                     0.1_07_12_84,
                     1.3_33_02_71,
                     0.61_26_02_23,
                     -0.6_69_10_65,
                     -0.3_84_68_52,
                     -1.0_30_36_61,
                     0.22_71_61_11,
                     0.03_34_89_01,
                     0.30_04_06_75,
                     -0.24_80_50_29,
                 ]				)

             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    UpperCamelCase__	(self					:					Optional[Any]				):

             '''simple docstring'''







             lowercase__       =				torch_device == '''cpu'''
             lowercase__       =				True
             lowercase__       =				False

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






      @skip_mps
      def    UpperCamelCase__	(self					:					Tuple				):

             '''simple docstring'''







             lowercase__       =				torch_device == '''cpu'''
             lowercase__       =				False

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








'''simple docstring'''

import json
import os
from functools import lru_cache
from typing import TYPE_CHECKING, List, Optional, Tuple, Union

import numpy as np
import regex as re

from ...utils import is_tf_available, is_torch_available, logging


if TYPE_CHECKING:
 if is_torch_available():
  import torch
 if is_tf_available():
  import tensorflow as tf

from ...tokenization_utils import AddedToken, PreTrainedTokenizer


lowerCamelCase							:     Union[str, Any]								=			logging.get_logger(__name__)

lowerCamelCase							:     Tuple								=			{
    'vocab_file': 'vocab.json',
    'merges_file': 'merges.txt',
}

lowerCamelCase							:     Tuple								=			{
    'vocab_file': {
        'Salesforce/codegen-350M-mono': 'https://huggingface.co/Salesforce/codegen-350M-mono/resolve/main/vocab.json',
    },
    'merges_file': {
        'Salesforce/codegen-350M-mono': 'https://huggingface.co/Salesforce/codegen-350M-mono/resolve/main/merges.txt',
    },
}

lowerCamelCase							:     Optional[int]								=			{
    'Salesforce/codegen-350M-mono': 2_048,
}

@lru_cache()
def _SCREAMING_SNAKE_CASE   () -> Optional[int]:






       """simple docstring"""







       lowercase__       =				(
           list(range(ord('''!''' )						,				ord('''~''' ) + 1 ) ) + list(range(ord('''¡''' )						,				ord('''¬''' ) + 1 ) ) + list(range(ord('''®''' )						,				ord('''ÿ''' ) + 1 ) )
       )
       lowercase__       =				bs[:]
       lowercase__       =				0
       for b in range(2**8 ):
              if b not in bs:
                     bs.append(A )
                     cs.append(2**8 + n )
                     n += 1
       lowercase__       =				[chr(A ) for n in cs]
       return dict(zip(A						,				A ) )

def _SCREAMING_SNAKE_CASE   (A ) -> Tuple:






       """simple docstring"""







       lowercase__       =				set()
       lowercase__       =				word[0]
       for char in word[1:]:
              pairs.add((prev_char, char) )
              lowercase__       =				char
       return pairs

class 						__lowerCAmelCase		(lowercase_  ):





      '''simple docstring'''






      lowerCAmelCase__      :  int						      =							VOCAB_FILES_NAMES
      lowerCAmelCase__      :  Union[str, Any]						      =							PRETRAINED_VOCAB_FILES_MAP
      lowerCAmelCase__      :  Optional[int]						      =							PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
      lowerCAmelCase__      :  Tuple						      =							["""input_ids""", """attention_mask"""]



      def __init__(self					:					List[Any]     ,		UpperCamelCase					:					List[str]     ,		UpperCamelCase					:					List[Any]     ,		UpperCamelCase					:					Optional[int]="replace"     ,		UpperCamelCase					:					Optional[Any]="<|endoftext|>"     ,		UpperCamelCase					:					str="<|endoftext|>"     ,		UpperCamelCase					:					Optional[int]="<|endoftext|>"     ,		UpperCamelCase					:					Optional[int]=None     ,		UpperCamelCase					:					str=False     ,		UpperCamelCase					:					Any=False     ,		**UpperCamelCase					:					Dict     ,		):

             '''simple docstring'''







             lowercase__       =				AddedToken(UpperCamelCase     ,		lstrip=UpperCamelCase     ,		rstrip=UpperCamelCase				) if isinstance(UpperCamelCase     ,		UpperCamelCase				) else bos_token
             lowercase__       =				AddedToken(UpperCamelCase     ,		lstrip=UpperCamelCase     ,		rstrip=UpperCamelCase				) if isinstance(UpperCamelCase     ,		UpperCamelCase				) else eos_token
             lowercase__       =				AddedToken(UpperCamelCase     ,		lstrip=UpperCamelCase     ,		rstrip=UpperCamelCase				) if isinstance(UpperCamelCase     ,		UpperCamelCase				) else unk_token
             lowercase__       =				AddedToken(UpperCamelCase     ,		lstrip=UpperCamelCase     ,		rstrip=UpperCamelCase				) if isinstance(UpperCamelCase     ,		UpperCamelCase				) else pad_token
             super().__init__(
                 errors=UpperCamelCase     ,		unk_token=UpperCamelCase     ,		bos_token=UpperCamelCase     ,		eos_token=UpperCamelCase     ,		pad_token=UpperCamelCase     ,		add_prefix_space=UpperCamelCase     ,		add_bos_token=UpperCamelCase     ,		**UpperCamelCase     ,		)
             lowercase__       =				add_bos_token

             with open(UpperCamelCase     ,		encoding='''utf-8'''				) as vocab_handle:
                    lowercase__       =				json.load(UpperCamelCase				)
             lowercase__       =				{v: k for k, v in self.encoder.items()}
             lowercase__       =				errors  # how to handle errors in decoding
             lowercase__       =				bytes_to_unicode()
             lowercase__       =				{v: k for k, v in self.byte_encoder.items()}
             with open(UpperCamelCase     ,		encoding='''utf-8'''				) as merges_handle:
                    lowercase__       =				merges_handle.read().split('''\n'''				)[1:-1]
             lowercase__       =				[tuple(merge.split()				) for merge in bpe_merges]
             lowercase__       =				dict(zip(UpperCamelCase     ,		range(len(UpperCamelCase				)				)				)				)
             lowercase__       =				{}
             lowercase__       =				add_prefix_space

             # Should have added re.IGNORECASE so BPE merges can happen for capitalized versions of contractions
             lowercase__       =				re.compile(R'''\'s|\'t|\'re|\'ve|\'m|\'ll|\'d| ?\p{L}+| ?\p{N}+| ?[^\s\p{L}\p{N}]+|\s+(?!\S)|\s+'''				)



      @property
      def    UpperCamelCase__	(self					:					Any				):

             '''simple docstring'''







             return len(self.encoder				)



      def    UpperCamelCase__	(self					:					Optional[Any]				):

             '''simple docstring'''







             return dict(self.encoder     ,		**self.added_tokens_encoder				)



      def    UpperCamelCase__	(self					:					Union[str, Any]     ,		UpperCamelCase					:					Optional[int]				):

             '''simple docstring'''







             if token in self.cache:
                    return self.cache[token]
             lowercase__       =				tuple(UpperCamelCase				)
             lowercase__       =				get_pairs(UpperCamelCase				)

             if not pairs:
                    return token

             while True:
                    lowercase__       =				min(UpperCamelCase     ,		key=lambda UpperCamelCase				: self.bpe_ranks.get(UpperCamelCase     ,		float('''inf'''				)				)				)
                    if bigram not in self.bpe_ranks:
                           break
                    lowercase__    ,lowercase__       =				bigram
                    lowercase__       =				[]
                    lowercase__       =				0
                    while i < len(UpperCamelCase				):
                           try:
                                  lowercase__       =				word.index(UpperCamelCase     ,		UpperCamelCase				)
                           except ValueError:
                                  new_word.extend(word[i:]				)
                                  break
                           else:
                                  new_word.extend(word[i:j]				)
                                  lowercase__       =				j

                           if word[i] == first and i < len(UpperCamelCase				) - 1 and word[i + 1] == second:
                                  new_word.append(first + second				)
                                  i += 2
                           else:
                                  new_word.append(word[i]				)
                                  i += 1
                    lowercase__       =				tuple(UpperCamelCase				)
                    lowercase__       =				new_word
                    if len(UpperCamelCase				) == 1:
                           break
                    else:
                           lowercase__       =				get_pairs(UpperCamelCase				)
             lowercase__       =				''' '''.join(UpperCamelCase				)
             lowercase__       =				word
             return word



      def    UpperCamelCase__	(self					:					Any     ,		UpperCamelCase					:					str     ,		UpperCamelCase					:					str=None				):

             '''simple docstring'''







             if self.add_bos_token:
                    lowercase__       =				[self.bos_token_id]
             else:
                    lowercase__       =				[]

             lowercase__       =				bos_token_ids + token_ids_a

             if token_ids_a is None:
                    return output

             return output + bos_token_ids + token_ids_a



      def    UpperCamelCase__	(self					:					Optional[Any]     ,		UpperCamelCase					:					str				):

             '''simple docstring'''







             lowercase__       =				[]
             for token in re.findall(self.pat     ,		UpperCamelCase				):
                    lowercase__       =				''''''.join(
                        self.byte_encoder[b] for b in token.encode('''utf-8'''				)				)  # Maps all our bytes to unicode strings, avoiding control tokens of the BPE (spaces in our case)
                    bpe_tokens.extend(bpe_token for bpe_token in self.bpe(UpperCamelCase				).split(''' '''				)				)
             return bpe_tokens



      def    UpperCamelCase__	(self					:					str     ,		UpperCamelCase					:					str				):

             '''simple docstring'''







             return self.encoder.get(UpperCamelCase     ,		self.encoder.get(self.unk_token				)				)



      def    UpperCamelCase__	(self					:					Any     ,		UpperCamelCase					:					Optional[int]				):

             '''simple docstring'''







             return self.decoder.get(UpperCamelCase				)



      def    UpperCamelCase__	(self					:					List[Any]     ,		UpperCamelCase					:					Optional[int]				):

             '''simple docstring'''







             lowercase__       =				''''''.join(UpperCamelCase				)
             lowercase__       =				bytearray([self.byte_decoder[c] for c in text]				).decode('''utf-8'''     ,		errors=self.errors				)
             return text



      def    UpperCamelCase__	(self					:					int     ,		UpperCamelCase					:					str     ,		UpperCamelCase					:					Optional[str] = None				):

             '''simple docstring'''







             if not os.path.isdir(UpperCamelCase				):
                    logger.error(f"Vocabulary path ({save_directory}) should be a directory"				)
                    return
             lowercase__       =				os.path.join(
                 UpperCamelCase     ,		(filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file''']				)
             lowercase__       =				os.path.join(
                 UpperCamelCase     ,		(filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''merges_file''']				)

             with open(UpperCamelCase     ,		'''w'''     ,		encoding='''utf-8'''				) as f:
                    f.write(json.dumps(self.encoder     ,		indent=2     ,		sort_keys=UpperCamelCase     ,		ensure_ascii=UpperCamelCase				) + '''\n'''				)

             lowercase__       =				0
             with open(UpperCamelCase     ,		'''w'''     ,		encoding='''utf-8'''				) as writer:
                    writer.write('''#version: 0.2\n'''				)
                    for bpe_tokens, token_index in sorted(self.bpe_ranks.items()     ,		key=lambda UpperCamelCase				: kv[1]				):
                           if index != token_index:
                                  logger.warning(
                                      f"Saving vocabulary to {merge_file}: BPE merge indices are not consecutive."
                                      ''' Please check that the tokenizer is not corrupted!'''				)
                                  lowercase__       =				token_index
                           writer.write(''' '''.join(UpperCamelCase				) + '''\n'''				)
                           index += 1

             return vocab_file, merge_file



      def    UpperCamelCase__	(self					:					str     ,		UpperCamelCase					:					List[Any]     ,		UpperCamelCase					:					int=False     ,		**UpperCamelCase					:					Optional[int]				):

             '''simple docstring'''







             lowercase__       =				kwargs.pop('''add_prefix_space'''     ,		self.add_prefix_space				)
             if is_split_into_words or add_prefix_space:
                    lowercase__       =				''' ''' + text
             return (text, kwargs)



      def    UpperCamelCase__	(self					:					Tuple     ,		UpperCamelCase					:					Union[int, List[int], "np.ndarray", "torch.Tensor", "tf.Tensor"]     ,		UpperCamelCase					:					bool = False     ,		UpperCamelCase					:					bool = None     ,		UpperCamelCase					:					Optional[List[str]] = None     ,		**UpperCamelCase					:					Optional[Any]     ,		):

             '''simple docstring'''







             lowercase__       =				super()._decode(
                 token_ids=UpperCamelCase     ,		skip_special_tokens=UpperCamelCase     ,		clean_up_tokenization_spaces=UpperCamelCase     ,		**UpperCamelCase     ,		)

             if truncate_before_pattern is not None and len(UpperCamelCase				) > 0:
                    lowercase__       =				self.truncate(UpperCamelCase     ,		UpperCamelCase				)

             return decoded_text






      def    UpperCamelCase__	(self					:					str     ,		UpperCamelCase					:					List[str]     ,		UpperCamelCase					:					Union[str, Any]				):

             '''simple docstring'''







             def find_re(UpperCamelCase					:					Dict     ,		UpperCamelCase					:					Dict     ,		UpperCamelCase					:					Optional[Any]				):
                    lowercase__       =				pattern.search(UpperCamelCase     ,		UpperCamelCase				)
                    return m.start() if m else -1

             lowercase__       =				[re.compile(UpperCamelCase     ,		re.MULTILINE				) for pattern in truncate_before_pattern]

             lowercase__       =				list(re.finditer('''^print'''     ,		UpperCamelCase     ,		re.MULTILINE				)				)

             if len(UpperCamelCase				) > 1:
                    lowercase__       =				completion[: prints[1].start()]

             lowercase__       =				list(re.finditer('''^def'''     ,		UpperCamelCase     ,		re.MULTILINE				)				)

             if len(UpperCamelCase				) > 1:
                    lowercase__       =				completion[: defs[1].start()]

             lowercase__       =				0

             lowercase__       =				[
                 pos for pos in [find_re(UpperCamelCase     ,		UpperCamelCase     ,		UpperCamelCase				) for terminal in terminals] if pos != -1
             ]

             if len(UpperCamelCase				) > 0:
                    return completion[: min(UpperCamelCase				)]
             else:
                    return completion




