



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

import regex as re

from ...tokenization_utils import AddedToken, PreTrainedTokenizer
from ...utils import logging


__snake_case     :Tuple			       =logging.get_logger(__name__)


__snake_case     :Dict			       ={'vocab_file': 'vocab.json', 'merges_file': 'merges.txt'}

# See all BART models at https://huggingface.co/models?filter=bart
__snake_case     :Dict			       ={
    'vocab_file': {
        'facebook/bart-base': 'https://huggingface.co/facebook/bart-base/resolve/main/vocab.json',
        'facebook/bart-large': 'https://huggingface.co/facebook/bart-large/resolve/main/vocab.json',
        'facebook/bart-large-mnli': 'https://huggingface.co/facebook/bart-large-mnli/resolve/main/vocab.json',
        'facebook/bart-large-cnn': 'https://huggingface.co/facebook/bart-large-cnn/resolve/main/vocab.json',
        'facebook/bart-large-xsum': 'https://huggingface.co/facebook/bart-large-xsum/resolve/main/vocab.json',
        'yjernite/bart_eli5': 'https://huggingface.co/yjernite/bart_eli5/resolve/main/vocab.json',
    },
    'merges_file': {
        'facebook/bart-base': 'https://huggingface.co/facebook/bart-base/resolve/main/merges.txt',
        'facebook/bart-large': 'https://huggingface.co/facebook/bart-large/resolve/main/merges.txt',
        'facebook/bart-large-mnli': 'https://huggingface.co/facebook/bart-large-mnli/resolve/main/merges.txt',
        'facebook/bart-large-cnn': 'https://huggingface.co/facebook/bart-large-cnn/resolve/main/merges.txt',
        'facebook/bart-large-xsum': 'https://huggingface.co/facebook/bart-large-xsum/resolve/main/merges.txt',
        'yjernite/bart_eli5': 'https://huggingface.co/yjernite/bart_eli5/resolve/main/merges.txt',
    },
}

__snake_case     :Optional[int]			       ={
    'facebook/bart-base': 1024,
    'facebook/bart-large': 1024,
    'facebook/bart-large-mnli': 1024,
    'facebook/bart-large-cnn': 1024,
    'facebook/bart-large-xsum': 1024,
    'yjernite/bart_eli5': 1024,
}






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

							'''simple docstring'''






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






def  lowerCamelCase_	(      lowerCAmelCase__	:	Optional[Any]				)       ->							Dict:

							'''simple docstring'''






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



class        lowerCAmelCase__					(  _lowerCamelCase       ):
				A_   :     Optional[Any]								=     VOCAB_FILES_NAMES
				A_   :     List[str]								=     PRETRAINED_VOCAB_FILES_MAP
				A_   :     Optional[Any]								=     PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
				A_   :     str								=     ['input_ids', 'attention_mask']


				def __init__(							self      :  Optional[int]						, __UpperCamelCase      :  Any						, __UpperCamelCase      :  Optional[Any]						, __UpperCamelCase      :  Tuple="replace"						, __UpperCamelCase      :  Any="<s>"						, __UpperCamelCase      :  Dict="</s>"						, __UpperCamelCase      :  List[Any]="</s>"						, __UpperCamelCase      :  List[Any]="<s>"						, __UpperCamelCase      :  str="<unk>"						, __UpperCamelCase      :  Optional[Any]="<pad>"						, __UpperCamelCase      :  Union[str, Any]="<mask>"						, __UpperCamelCase      :  Any=False						, **__UpperCamelCase      :  Any						, )		->     Optional[Any]:
											A      						=    AddedToken(__UpperCamelCase						, lstrip=__UpperCamelCase						, rstrip=__UpperCamelCase       ) if isinstance(__UpperCamelCase						, __UpperCamelCase       ) else bos_token
											A      						=    AddedToken(__UpperCamelCase						, lstrip=__UpperCamelCase						, rstrip=__UpperCamelCase       ) if isinstance(__UpperCamelCase						, __UpperCamelCase       ) else eos_token
											A      						=    AddedToken(__UpperCamelCase						, lstrip=__UpperCamelCase						, rstrip=__UpperCamelCase       ) if isinstance(__UpperCamelCase						, __UpperCamelCase       ) else sep_token
											A      						=    AddedToken(__UpperCamelCase						, lstrip=__UpperCamelCase						, rstrip=__UpperCamelCase       ) if isinstance(__UpperCamelCase						, __UpperCamelCase       ) else cls_token
											A      						=    AddedToken(__UpperCamelCase						, lstrip=__UpperCamelCase						, rstrip=__UpperCamelCase       ) if isinstance(__UpperCamelCase						, __UpperCamelCase       ) else unk_token
											A      						=    AddedToken(__UpperCamelCase						, lstrip=__UpperCamelCase						, rstrip=__UpperCamelCase       ) if isinstance(__UpperCamelCase						, __UpperCamelCase       ) else pad_token

											# Mask token behave like a normal word, i.e. include the space before it
											A      						=    AddedToken(__UpperCamelCase						, lstrip=__UpperCamelCase						, rstrip=__UpperCamelCase       ) if isinstance(__UpperCamelCase						, __UpperCamelCase       ) else mask_token

											super().__init__(
											    errors=__UpperCamelCase						, bos_token=__UpperCamelCase						, eos_token=__UpperCamelCase						, unk_token=__UpperCamelCase						, sep_token=__UpperCamelCase						, cls_token=__UpperCamelCase						, pad_token=__UpperCamelCase						, mask_token=__UpperCamelCase						, add_prefix_space=__UpperCamelCase						, **__UpperCamelCase						, )

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

											# Should have added re.IGNORECASE so BPE merges can happen for capitalized versions of contractions
											A      						=    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       )		->     Any:
											return len(self.encoder       )


				def __UpperCamelCase						(							self      :  Optional[Any]       )		->     str:
											return dict(self.encoder						, **self.added_tokens_encoder       )


				def __UpperCamelCase						(							self      :  List[Any]						, __UpperCamelCase      :  int       )		->     Any:
											if token in self.cache:
																		return self.cache[token]
											A      						=    tuple(__UpperCamelCase       )
											A      						=    get_pairs(__UpperCamelCase       )

											if not pairs:
																		return token

											while True:
																		A      						=    min(__UpperCamelCase						, key=lambda __UpperCamelCase       : self.bpe_ranks.get(__UpperCamelCase						, float('inf'       )       )       )
																		if bigram not in self.bpe_ranks:
																									break
																		A						,   A      						=    bigram
																		A      						=    []
																		A      						=    0
																		while i < len(__UpperCamelCase       ):
																									try:
																																A      						=    word.index(__UpperCamelCase						, __UpperCamelCase       )
																									except ValueError:
																																new_word.extend(word[i:]       )
																																break
																									else:
																																new_word.extend(word[i:j]       )
																																A      						=    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
																		A      						=    tuple(__UpperCamelCase       )
																		A      						=    new_word
																		if len(__UpperCamelCase       ) == 1:
																									break
																		else:
																									A      						=    get_pairs(__UpperCamelCase       )
											A      						=    ' '.join(__UpperCamelCase       )
											A      						=    word
											return word


				def __UpperCamelCase						(							self      :  Optional[int]						, __UpperCamelCase      :  int       )		->     Optional[int]:
											A      						=    []
											for token in re.findall(self.pat						, __UpperCamelCase       ):
																		A      						=    ''.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      :  List[Any]						, __UpperCamelCase      :  List[Any]       )		->     int:
											return self.encoder.get(__UpperCamelCase						, self.encoder.get(self.unk_token       )       )


				def __UpperCamelCase						(							self      :  Optional[Any]						, __UpperCamelCase      :  Optional[Any]       )		->     Optional[Any]:
											return self.decoder.get(__UpperCamelCase       )


				def __UpperCamelCase						(							self      :  Any						, __UpperCamelCase      :  Optional[Any]       )		->     Any:
											A      						=    ''.join(__UpperCamelCase       )
											A      						=    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       )		->     Tuple[str]:
											if not os.path.isdir(__UpperCamelCase       ):
																		logger.error(f'''Vocabulary path ({save_directory}) should be a directory'''       )
																		return
											A      						=    os.path.join(
											    __UpperCamelCase						, (filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['vocab_file']       )
											A      						=    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'       )

											A      						=    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!'       )
																																A      						=    token_index
																									writer.write(' '.join(__UpperCamelCase       ) + '\n'       )
																									index += 1

											return vocab_file, merge_file


				def __UpperCamelCase						(							self      :  Optional[Any]						, __UpperCamelCase      :  List[int]						, __UpperCamelCase      :  Optional[List[int]] = None       )		->     List[int]:
											if token_ids_a is None:
																		return [self.cls_token_id] + token_ids_a + [self.sep_token_id]
											A      						=    [self.cls_token_id]
											A      						=    [self.sep_token_id]
											return cls + token_ids_a + sep + sep + token_ids_a + sep


				def __UpperCamelCase						(							self      :  Optional[int]						, __UpperCamelCase      :  List[int]						, __UpperCamelCase      :  Optional[List[int]] = None						, __UpperCamelCase      :  bool = False       )		->     List[int]:
											if already_has_special_tokens:
																		return super().get_special_tokens_mask(
																		    token_ids_a=__UpperCamelCase						, token_ids_a=__UpperCamelCase						, already_has_special_tokens=__UpperCamelCase       )

											if token_ids_a is None:
																		return [1] + ([0] * len(__UpperCamelCase       )) + [1]
											return [1] + ([0] * len(__UpperCamelCase       )) + [1, 1] + ([0] * len(__UpperCamelCase       )) + [1]


				def __UpperCamelCase						(							self      :  int						, __UpperCamelCase      :  List[int]						, __UpperCamelCase      :  Optional[List[int]] = None       )		->     List[int]:
											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 + sep + token_ids_a + sep       ) * [0]


				def __UpperCamelCase						(							self      :  int						, __UpperCamelCase      :  str						, __UpperCamelCase      :  Optional[Any]=False						, **__UpperCamelCase      :  Tuple       )		->     Any:
											A      						=    kwargs.pop('add_prefix_space'						, self.add_prefix_space       )
											if (is_split_into_words or add_prefix_space) and (len(__UpperCamelCase       ) > 0 and not text[0].isspace()):
																		A      						=    ' ' + text
											return (text, kwargs)




import gc
import tempfile
import unittest

import numpy as np
import torch

from diffusers import VersatileDiffusionPipeline
from diffusers.utils.testing_utils import load_image, nightly, require_torch_gpu, torch_device


__snake_case     :str			       =False



class        lowerCAmelCase__					(  unittest.TestCase       ):
				pass





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


				def __UpperCamelCase						(							self      :  Dict       )		->     List[Any]:
											# clean up the VRAM after each test
											super().tearDown()
											gc.collect()
											torch.cuda.empty_cache()


				def __UpperCamelCase						(							self      :  Union[str, Any]       )		->     List[Any]:
											A      						=    VersatileDiffusionPipeline.from_pretrained('shi-labs/versatile-diffusion'						, torch_dtype=torch.floataa       )
											pipe.to(__UpperCamelCase       )
											pipe.set_progress_bar_config(disable=__UpperCamelCase       )

											A      						=    load_image(
											    'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg'       )

											A      						=    torch.manual_seed(0       )
											A      						=    pipe.dual_guided(
											    prompt='first prompt'						, image=__UpperCamelCase						, text_to_image_strength=0.7_5						, generator=__UpperCamelCase						, guidance_scale=7.5						, num_inference_steps=2						, output_type='numpy'						, ).images

											with tempfile.TemporaryDirectory() as tmpdirname:
																		pipe.save_pretrained(__UpperCamelCase       )
																		A      						=    VersatileDiffusionPipeline.from_pretrained(__UpperCamelCase						, torch_dtype=torch.floataa       )
											pipe.to(__UpperCamelCase       )
											pipe.set_progress_bar_config(disable=__UpperCamelCase       )

											A      						=    generator.manual_seed(0       )
											A      						=    pipe.dual_guided(
											    prompt='first prompt'						, image=__UpperCamelCase						, text_to_image_strength=0.7_5						, generator=__UpperCamelCase						, guidance_scale=7.5						, num_inference_steps=2						, output_type='numpy'						, ).images

											assert np.abs(image - new_image       ).sum() < 1e-5, "Models don't have the same forward pass"


				def __UpperCamelCase						(							self      :  Tuple       )		->     List[str]:
											A      						=    VersatileDiffusionPipeline.from_pretrained('shi-labs/versatile-diffusion'						, torch_dtype=torch.floataa       )
											pipe.to(__UpperCamelCase       )
											pipe.set_progress_bar_config(disable=__UpperCamelCase       )

											A      						=    'cyberpunk 2077'
											A      						=    load_image(
											    'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg'       )
											A      						=    torch.manual_seed(0       )
											A      						=    pipe.dual_guided(
											    prompt=__UpperCamelCase						, image=__UpperCamelCase						, text_to_image_strength=0.7_5						, generator=__UpperCamelCase						, guidance_scale=7.5						, num_inference_steps=50						, output_type='numpy'						, ).images

											A      						=    image[0, 253:256, 253:256, -1]

											assert image.shape == (1, 512, 512, 3)
											A      						=    np.array([0.1_4_4_8, 0.1_6_1_9, 0.1_7_4_1, 0.1_0_8_6, 0.1_1_4_7, 0.1_1_2_8, 0.1_1_9_9, 0.1_1_6_5, 0.1_0_0_1]       )

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

											A      						=    'A painting of a squirrel eating a burger '
											A      						=    torch.manual_seed(0       )
											A      						=    pipe.text_to_image(
											    prompt=__UpperCamelCase						, generator=__UpperCamelCase						, guidance_scale=7.5						, num_inference_steps=50						, output_type='numpy'       ).images

											A      						=    image[0, 253:256, 253:256, -1]

											assert image.shape == (1, 512, 512, 3)
											A      						=    np.array([0.3_3_6_7, 0.3_1_6_9, 0.2_6_5_6, 0.3_8_7_0, 0.4_7_9_0, 0.3_7_9_6, 0.4_0_0_9, 0.4_8_7_8, 0.4_7_7_8]       )

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

											A      						=    pipe.image_variation(__UpperCamelCase						, generator=__UpperCamelCase						, output_type='numpy'       ).images

											A      						=    image[0, 253:256, 253:256, -1]

											assert image.shape == (1, 512, 512, 3)
											A      						=    np.array([0.3_0_7_6, 0.3_1_2_3, 0.3_2_8_4, 0.3_7_8_2, 0.3_7_7_0, 0.3_8_9_4, 0.4_2_9_7, 0.4_3_3_1, 0.4_4_5_6]       )

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