





import copy
import os
from typing import Union

from ...configuration_utils import PretrainedConfig
from ...utils import logging


UpperCAmelCase_ 							=       logging.get_logger(__name__)

UpperCAmelCase_ 							=       {
    """BAAI/AltCLIP""": """https://huggingface.co/BAAI/AltCLIP/resolve/main/config.json""",
    # See all AltCLIP models at https://huggingface.co/models?filter=altclip
}
class      UpperCamelCase_						(					_lowerCamelCase   ):
					lowerCAmelCase_							=		'''altclip_text_model'''




					def __init__(						self			,  lowerCAmelCase_=25_0002			,  lowerCAmelCase_=1024			,  lowerCAmelCase_=24			,  lowerCAmelCase_=16			,  lowerCAmelCase_=4096			,  lowerCAmelCase_="gelu"			,  lowerCAmelCase_=0.1			,  lowerCAmelCase_=0.1			,  lowerCAmelCase_=514			,  lowerCAmelCase_=1			,  lowerCAmelCase_=0.02			,  lowerCAmelCase_=0.02			,  lowerCAmelCase_=1E-05			,  lowerCAmelCase_=1			,  lowerCAmelCase_=0			,  lowerCAmelCase_=2			,  lowerCAmelCase_="absolute"			,  lowerCAmelCase_=True			,  lowerCAmelCase_=768			,  **lowerCAmelCase_			,  )						->							Optional[int]:
										super().__init__(pad_token_id=lowerCAmelCase_			,  bos_token_id=lowerCAmelCase_			,  eos_token_id=lowerCAmelCase_			,  **lowerCAmelCase_					)

										_snake_case													=			vocab_size
										_snake_case													=			hidden_size
										_snake_case													=			num_hidden_layers
										_snake_case													=			num_attention_heads
										_snake_case													=			hidden_act
										_snake_case													=			intermediate_size
										_snake_case													=			hidden_dropout_prob
										_snake_case													=			attention_probs_dropout_prob
										_snake_case													=			max_position_embeddings
										_snake_case													=			type_vocab_size
										_snake_case													=			initializer_range
										_snake_case													=			initializer_factor
										_snake_case													=			layer_norm_eps
										_snake_case													=			position_embedding_type
										_snake_case													=			use_cache
										_snake_case													=			project_dim





class      UpperCamelCase_						(					_lowerCamelCase   ):
					lowerCAmelCase_							=		'''altclip_vision_model'''




					def __init__(						self			,  lowerCAmelCase_=768			,  lowerCAmelCase_=3072			,  lowerCAmelCase_=512			,  lowerCAmelCase_=12			,  lowerCAmelCase_=12			,  lowerCAmelCase_=3			,  lowerCAmelCase_=224			,  lowerCAmelCase_=32			,  lowerCAmelCase_="quick_gelu"			,  lowerCAmelCase_=1E-5			,  lowerCAmelCase_=0.0			,  lowerCAmelCase_=0.02			,  lowerCAmelCase_=1.0			,  **lowerCAmelCase_			,  )						->							int:
										super().__init__(**lowerCAmelCase_					)

										_snake_case													=			hidden_size
										_snake_case													=			intermediate_size
										_snake_case													=			projection_dim
										_snake_case													=			num_hidden_layers
										_snake_case													=			num_attention_heads
										_snake_case													=			num_channels
										_snake_case													=			patch_size
										_snake_case													=			image_size
										_snake_case													=			initializer_range
										_snake_case													=			initializer_factor
										_snake_case													=			attention_dropout
										_snake_case													=			layer_norm_eps
										_snake_case													=			hidden_act






					@classmethod
					def 		lowerCAmelCase     (						cls			,  lowerCAmelCase_			,  **lowerCAmelCase_					)						->							"PretrainedConfig":
										cls._set_token_in_kwargs(lowerCAmelCase_					)

										_snake_case							,       _snake_case													=			cls.get_config_dict(lowerCAmelCase_			,  **lowerCAmelCase_					)

										# get the vision config dict if we are loading from AltCLIPConfig
										if config_dict.get('model_type'					) == "altclip":
															_snake_case													=			config_dict['vision_config']

										if "model_type" in config_dict and hasattr(cls			,  'model_type'					) and config_dict["model_type"] != cls.model_type:
															logger.warning(
															    F'''You are using a model of type {config_dict["model_type"]} to instantiate a model of type '''
															    F'''{cls.model_type}. This is not supported for all configurations of models and can yield errors.'''					)

										return cls.from_dict(lowerCAmelCase_			,  **lowerCAmelCase_					)





class      UpperCamelCase_						(					_lowerCamelCase   ):
					lowerCAmelCase_							=		'''altclip'''
					lowerCAmelCase_							=		True




					def __init__(						self			,  lowerCAmelCase_=None			,  lowerCAmelCase_=None			,  lowerCAmelCase_=768			,  lowerCAmelCase_=2.65_92			,  **lowerCAmelCase_					)						->							List[Any]:
										# If `_config_dict` exist, we use them for the backward compatibility.
										# We pop out these 2 attributes before calling `super().__init__` to avoid them being saved (which causes a lot
										# of confusion!).
										_snake_case													=			kwargs.pop('text_config_dict'			,  lowerCAmelCase_					)
										_snake_case													=			kwargs.pop('vision_config_dict'			,  lowerCAmelCase_					)

										super().__init__(**lowerCAmelCase_					)

										# Instead of simply assigning `[text|vision]_config_dict` to `[text|vision]_config`, we use the values in
										# `[text|vision]_config_dict` to update the values in `[text|vision]_config`. The values should be same in most
										# cases, but we don't want to break anything regarding `_config_dict` that existed before commit `8827e1b2`.
										if text_config_dict is not None:
															if text_config is None:
																				_snake_case													=			{}

															# This is the complete result when using `text_config_dict`.
															_snake_case													=			AltCLIPTextConfig(**lowerCAmelCase_					).to_dict()

															# Give a warning if the values exist in both `_text_config_dict` and `text_config` but being different.
															for key, value in _text_config_dict.items():
																				if key in text_config and value != text_config[key] and key not in ["transformers_version"]:
																									# If specified in `text_config_dict`
																									if key in text_config_dict:
																														_snake_case													=			(
																														    F'''`{key}` is found in both `text_config_dict` and `text_config` but with different values. '''
																														    F'''The value `text_config_dict["{key}"]` will be used instead.'''
																														)
																									# If inferred from default argument values (just to be super careful)
																									else:
																														_snake_case													=			(
																														    F'''`text_config_dict` is provided which will be used to initialize `AltCLIPTextConfig`. The '''
																														    F'''value `text_config["{key}"]` will be overriden.'''
																														)
																									logger.warning(lowerCAmelCase_					)

            # Update all values in `text_config` with the ones in `_text_config_dict`.
															text_config.update(_text_config_dict					)

										if vision_config_dict is not None:
															if vision_config is None:
																				_snake_case													=			{}

															# This is the complete result when using `vision_config_dict`.
															_snake_case													=			AltCLIPVisionConfig(**lowerCAmelCase_					).to_dict()
															# convert keys to string instead of integer
															if "id2label" in _vision_config_dict:
																				_snake_case													=			{
																				    str(lowerCAmelCase_					): value for key, value in _vision_config_dict['id2label'].items()
																				}

															# Give a warning if the values exist in both `_vision_config_dict` and `vision_config` but being different.
															for key, value in _vision_config_dict.items():
																				if key in vision_config and value != vision_config[key] and key not in ["transformers_version"]:
																									# If specified in `vision_config_dict`
																									if key in vision_config_dict:
																														_snake_case													=			(
																														    F'''`{key}` is found in both `vision_config_dict` and `vision_config` but with different '''
																														    F'''values. The value `vision_config_dict["{key}"]` will be used instead.'''
																														)
																									# If inferred from default argument values (just to be super careful)
																									else:
																														_snake_case													=			(
																														    F'''`vision_config_dict` is provided which will be used to initialize `AltCLIPVisionConfig`. '''
																														    F'''The value `vision_config["{key}"]` will be overriden.'''
																														)
																									logger.warning(lowerCAmelCase_					)

            # Update all values in `vision_config` with the ones in `_vision_config_dict`.
															vision_config.update(_vision_config_dict					)

										if text_config is None:
															_snake_case													=			{}
															logger.info('`text_config` is `None`. Initializing the `AltCLIPTextConfig` with default values.'					)

										if vision_config is None:
															_snake_case													=			{}
															logger.info('`vision_config` is `None`. initializing the `AltCLIPVisionConfig` with default values.'					)

										_snake_case													=			AltCLIPTextConfig(**lowerCAmelCase_					)
										_snake_case													=			AltCLIPVisionConfig(**lowerCAmelCase_					)

										_snake_case													=			projection_dim
										_snake_case													=			logit_scale_init_value
										_snake_case													=			1.0




					@classmethod
					def 		lowerCAmelCase     (						cls			,  lowerCAmelCase_			,  lowerCAmelCase_			,  **lowerCAmelCase_					)						->							Union[str, Any]:
										return cls(text_config=text_config.to_dict()			,  vision_config=vision_config.to_dict()			,  **lowerCAmelCase_					)






					def 		lowerCAmelCase     (						self					)						->							List[Any]:
										_snake_case													=			copy.deepcopy(self.__dict__					)
										_snake_case													=			self.text_config.to_dict()
										_snake_case													=			self.vision_config.to_dict()
										_snake_case													=			self.__class__.model_type
										return output










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


UpperCAmelCase_ 							=       logging.get_logger(__name__)


UpperCAmelCase_ 							=       {"""vocab_file""": """vocab.json""", """merges_file""": """merges.txt"""}

# See all BART models at https://huggingface.co/models?filter=bart
UpperCAmelCase_ 							=       {
    """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""",
    },
}

UpperCAmelCase_ 							=       {
    """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__       (       )     ->	Tuple:



					'''simple docstring'''




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





def   lowerCamelCase__       (       UpperCamelCase__     :    Tuple  )     ->	int:



					'''simple docstring'''




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




class      UpperCamelCase_						(					_lowerCamelCase   ):
					lowerCAmelCase_							=		VOCAB_FILES_NAMES
					lowerCAmelCase_							=		PRETRAINED_VOCAB_FILES_MAP
					lowerCAmelCase_							=		PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
					lowerCAmelCase_							=		['''input_ids''', '''attention_mask''']




					def __init__(						self			,  lowerCAmelCase_			,  lowerCAmelCase_			,  lowerCAmelCase_="replace"			,  lowerCAmelCase_="<s>"			,  lowerCAmelCase_="</s>"			,  lowerCAmelCase_="</s>"			,  lowerCAmelCase_="<s>"			,  lowerCAmelCase_="<unk>"			,  lowerCAmelCase_="<pad>"			,  lowerCAmelCase_="<mask>"			,  lowerCAmelCase_=False			,  **lowerCAmelCase_			,  )						->							Tuple:
										_snake_case													=			AddedToken(lowerCAmelCase_			,  lstrip=lowerCAmelCase_			,  rstrip=lowerCAmelCase_					) if isinstance(lowerCAmelCase_			,  lowerCAmelCase_					) else bos_token
										_snake_case													=			AddedToken(lowerCAmelCase_			,  lstrip=lowerCAmelCase_			,  rstrip=lowerCAmelCase_					) if isinstance(lowerCAmelCase_			,  lowerCAmelCase_					) else eos_token
										_snake_case													=			AddedToken(lowerCAmelCase_			,  lstrip=lowerCAmelCase_			,  rstrip=lowerCAmelCase_					) if isinstance(lowerCAmelCase_			,  lowerCAmelCase_					) else sep_token
										_snake_case													=			AddedToken(lowerCAmelCase_			,  lstrip=lowerCAmelCase_			,  rstrip=lowerCAmelCase_					) if isinstance(lowerCAmelCase_			,  lowerCAmelCase_					) else cls_token
										_snake_case													=			AddedToken(lowerCAmelCase_			,  lstrip=lowerCAmelCase_			,  rstrip=lowerCAmelCase_					) if isinstance(lowerCAmelCase_			,  lowerCAmelCase_					) else unk_token
										_snake_case													=			AddedToken(lowerCAmelCase_			,  lstrip=lowerCAmelCase_			,  rstrip=lowerCAmelCase_					) if isinstance(lowerCAmelCase_			,  lowerCAmelCase_					) else pad_token

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

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

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

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




					@property
					def 		lowerCAmelCase     (						self					)						->							Any:
										return len(self.encoder					)




					def 		lowerCAmelCase     (						self					)						->							Dict:
										return dict(self.encoder			,  **self.added_tokens_encoder					)




					def 		lowerCAmelCase     (						self			,  lowerCAmelCase_					)						->							Dict:
										if token in self.cache:
															return self.cache[token]
										_snake_case													=			tuple(lowerCAmelCase_					)
										_snake_case													=			get_pairs(lowerCAmelCase_					)

										if not pairs:
															return token

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

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




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




					def 		lowerCAmelCase     (						self			,  lowerCAmelCase_					)						->							str:
										return self.encoder.get(lowerCAmelCase_			,  self.encoder.get(self.unk_token					)					)




					def 		lowerCAmelCase     (						self			,  lowerCAmelCase_					)						->							Optional[int]:
										return self.decoder.get(lowerCAmelCase_					)




					def 		lowerCAmelCase     (						self			,  lowerCAmelCase_					)						->							Union[str, Any]:
										_snake_case													=			''.join(lowerCAmelCase_					)
										_snake_case													=			bytearray([self.byte_decoder[c] for c in text]					).decode('utf-8'			,  errors=self.errors					)
										return text




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

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

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

										return vocab_file, merge_file




					def 		lowerCAmelCase     (						self			,  lowerCAmelCase_			,  lowerCAmelCase_ = None					)						->							List[int]:
										if token_ids_a is None:
															return [self.cls_token_id] + token_ids_a + [self.sep_token_id]
										_snake_case													=			[self.cls_token_id]
										_snake_case													=			[self.sep_token_id]
										return cls + token_ids_a + sep + sep + token_ids_a + sep




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

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




					def 		lowerCAmelCase     (						self			,  lowerCAmelCase_			,  lowerCAmelCase_ = None					)						->							List[int]:
										_snake_case													=			[self.sep_token_id]
										_snake_case													=			[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 		lowerCAmelCase     (						self			,  lowerCAmelCase_			,  lowerCAmelCase_=False			,  **lowerCAmelCase_					)						->							str:
										_snake_case													=			kwargs.pop('add_prefix_space'			,  self.add_prefix_space					)
										if (is_split_into_words or add_prefix_space) and (len(lowerCAmelCase_					) > 0 and not text[0].isspace()):
															_snake_case													=			' ' + text
										return (text, kwargs)



