


'''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


_UpperCAmelCase					:			str				     =					logging.get_logger(__name__)

_UpperCAmelCase					:			Optional[Any]				     =					{
    """vocab_file""": """vocab.json""",
    """merges_file""": """merges.txt""",
}

_UpperCAmelCase					:			List[str]				     =					{
    """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""",
    },
}

_UpperCAmelCase					:			Optional[Any]				     =					{
    """Salesforce/codegen-350M-mono""": 2_0_4_8,
}

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

def 				__magic_name__(    lowerCamelCase):
						__lowerCAmelCase											=					set()
						__lowerCAmelCase											=					word[0]
						for char in word[1:]:
												pairs.add((prev_char, char))
												__lowerCAmelCase											=					char
						return pairs




class    a__					(			__A   ):







				"""simple docstring"""



				__UpperCamelCase     :		Union[str, Any]						=						VOCAB_FILES_NAMES
				__UpperCamelCase     :		Dict						=						PRETRAINED_VOCAB_FILES_MAP
				__UpperCamelCase     :		str						=						PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
				__UpperCamelCase     :		Dict						=						['input_ids', 'attention_mask']




				def __init__(self							,      __lowercase							,      __lowercase							,      __lowercase="replace"							,      __lowercase="<|endoftext|>"							,      __lowercase="<|endoftext|>"							,      __lowercase="<|endoftext|>"							,      __lowercase=None							,      __lowercase=False							,      __lowercase=False							,      **__lowercase							,      ):
										__lowerCAmelCase											=					AddedToken(__lowercase							,      lstrip=__lowercase							,      rstrip=__lowercase		) if isinstance(__lowercase							,      __lowercase		) else bos_token
										__lowerCAmelCase											=					AddedToken(__lowercase							,      lstrip=__lowercase							,      rstrip=__lowercase		) if isinstance(__lowercase							,      __lowercase		) else eos_token
										__lowerCAmelCase											=					AddedToken(__lowercase							,      lstrip=__lowercase							,      rstrip=__lowercase		) if isinstance(__lowercase							,      __lowercase		) else unk_token
										__lowerCAmelCase											=					AddedToken(__lowercase							,      lstrip=__lowercase							,      rstrip=__lowercase		) if isinstance(__lowercase							,      __lowercase		) else pad_token
										super().__init__(
										    errors=__lowercase							,      unk_token=__lowercase							,      bos_token=__lowercase							,      eos_token=__lowercase							,      pad_token=__lowercase							,      add_prefix_space=__lowercase							,      add_bos_token=__lowercase							,      **__lowercase							,      )
										__lowerCAmelCase											=					add_bos_token

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

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




				@property
				def        _snake_case	(self		):
										return len(self.encoder		)




				def        _snake_case	(self		):
										return dict(self.encoder							,      **self.added_tokens_encoder		)




				def        _snake_case	(self							,      __lowercase		):
										if token in self.cache:
																return self.cache[token]
										__lowerCAmelCase											=					tuple(__lowercase		)
										__lowerCAmelCase											=					get_pairs(__lowercase		)

										if not pairs:
																return token

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

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




				def        _snake_case	(self							,      __lowercase							,      __lowercase=None		):
										if self.add_bos_token:
																__lowerCAmelCase											=					[self.bos_token_id]
										else:
																__lowerCAmelCase											=					[]

										__lowerCAmelCase											=					bos_token_ids + token_ids_a

										if token_ids_a is None:
																return output

										return output + bos_token_ids + token_ids_a




				def        _snake_case	(self							,      __lowercase		):
										__lowerCAmelCase											=					[]
										for token in re.findall(self.pat							,      __lowercase		):
																__lowerCAmelCase											=					''''''.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(__lowercase		).split(''' '''		)		)
										return bpe_tokens




				def        _snake_case	(self							,      __lowercase		):
										return self.encoder.get(__lowercase							,      self.encoder.get(self.unk_token		)		)




				def        _snake_case	(self							,      __lowercase		):
										return self.decoder.get(__lowercase		)




				def        _snake_case	(self							,      __lowercase		):
										__lowerCAmelCase											=					''''''.join(__lowercase		)
										__lowerCAmelCase											=					bytearray([self.byte_decoder[c] for c in text]		).decode('''utf-8'''							,      errors=self.errors		)
										return text




				def        _snake_case	(self							,      __lowercase							,      __lowercase = None		):
										if not os.path.isdir(__lowercase		):
																logger.error(F"""Vocabulary path ({save_directory}) should be a directory"""		)
																return
										__lowerCAmelCase											=					os.path.join(
										    __lowercase							,      (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file''']		)
										__lowerCAmelCase											=					os.path.join(
										    __lowercase							,      (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''merges_file''']		)

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

										__lowerCAmelCase											=					0
										with open(__lowercase							,      '''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 __lowercase		: 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!'''		)
																												__lowerCAmelCase											=					token_index
																						writer.write(''' '''.join(__lowercase		) + '''\n'''		)
																						index += 1

										return vocab_file, merge_file




				def        _snake_case	(self							,      __lowercase							,      __lowercase=False							,      **__lowercase		):
										__lowerCAmelCase											=					kwargs.pop('''add_prefix_space'''							,      self.add_prefix_space		)
										if is_split_into_words or add_prefix_space:
																__lowerCAmelCase											=					''' ''' + text
										return (text, kwargs)




				def        _snake_case	(self							,      __lowercase							,      __lowercase = False							,      __lowercase = None							,      __lowercase = None							,      **__lowercase							,      ):
										__lowerCAmelCase											=					super()._decode(
										    token_ids=__lowercase							,      skip_special_tokens=__lowercase							,      clean_up_tokenization_spaces=__lowercase							,      **__lowercase							,      )

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

										return decoded_text




				def        _snake_case	(self							,      __lowercase							,      __lowercase		):
										def find_re(__lowercase							,      __lowercase							,      __lowercase		):
																__lowerCAmelCase											=					pattern.search(__lowercase							,      __lowercase		)
																return m.start() if m else -1

										__lowerCAmelCase											=					[re.compile(__lowercase							,      re.MULTILINE		) for pattern in truncate_before_pattern]

										__lowerCAmelCase											=					list(re.finditer('''^print'''							,      __lowercase							,      re.MULTILINE		)		)

										if len(__lowercase		) > 1:
																__lowerCAmelCase											=					completion[: prints[1].start()]

										__lowerCAmelCase											=					list(re.finditer('''^def'''							,      __lowercase							,      re.MULTILINE		)		)

										if len(__lowercase		) > 1:
																__lowerCAmelCase											=					completion[: defs[1].start()]

										__lowerCAmelCase											=					0

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

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





'''simple docstring'''

from dataclasses import dataclass
from enum import Enum
from typing import List, Optional, Union

import numpy as np
import PIL
from PIL import Image

from ...utils import BaseOutput, is_torch_available, is_transformers_available


@dataclass
class    a__					(			__A   ):







				"""simple docstring"""



				__UpperCamelCase     :		Tuple						=						{
				    'sld_warmup_steps': 15,
				    'sld_guidance_scale': 20,
				    'sld_threshold': 0.0,
				    'sld_momentum_scale': 0.0,
				    'sld_mom_beta': 0.0,
				}
				__UpperCamelCase     :		Optional[Any]						=						{
				    'sld_warmup_steps': 10,
				    'sld_guidance_scale': 1_000,
				    'sld_threshold': 0.01,
				    'sld_momentum_scale': 0.3,
				    'sld_mom_beta': 0.4,
				}
				__UpperCamelCase     :		Tuple						=						{
				    'sld_warmup_steps': 7,
				    'sld_guidance_scale': 2_000,
				    'sld_threshold': 0.025,
				    'sld_momentum_scale': 0.5,
				    'sld_mom_beta': 0.7,
				}
				__UpperCamelCase     :		str						=						{
				    'sld_warmup_steps': 0,
				    'sld_guidance_scale': 5_000,
				    'sld_threshold': 1.0,
				    'sld_momentum_scale': 0.5,
				    'sld_mom_beta': 0.7,
				}


@dataclass
class    a__					(			__A   ):







				"""simple docstring"""



				__UpperCamelCase     :		Union[List[PIL.Image.Image], np.ndarray]
				__UpperCamelCase     :		Optional[List[bool]]
				__UpperCamelCase     :		Optional[Union[List[PIL.Image.Image], np.ndarray]]
				__UpperCamelCase     :		Optional[str]


if is_transformers_available() and is_torch_available():
	from .pipeline_stable_diffusion_safe import StableDiffusionPipelineSafe
	from .safety_checker import SafeStableDiffusionSafetyChecker

