



import copy
import os
from typing import Union

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


SCREAMING_SNAKE_CASE_:Optional[int]					=						logging.get_logger(__name__)

SCREAMING_SNAKE_CASE_:Dict					=						{
    """BAAI/AltCLIP""": """https://huggingface.co/BAAI/AltCLIP/resolve/main/config.json""",
    # See all AltCLIP models at https://huggingface.co/models?filter=altclip
}






class SCREAMING_SNAKE_CASE__					(		SCREAMING_SNAKE_CASE__   ):




    '''simple docstring'''






    __lowerCamelCase      :			int =				"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__,		):
        super().__init__(pad_token_id=lowerCamelCase__,		bos_token_id=lowerCamelCase__,		eos_token_id=lowerCamelCase__,		**lowerCamelCase__		)

        A			:	Union[str, Any]										=       vocab_size
        A			:	Dict										=       hidden_size
        A			:	Union[str, Any]										=       num_hidden_layers
        A			:	List[str]										=       num_attention_heads
        A			:	str										=       hidden_act
        A			:	Dict										=       intermediate_size
        A			:	List[str]										=       hidden_dropout_prob
        A			:	Optional[Any]										=       attention_probs_dropout_prob
        A			:	Tuple										=       max_position_embeddings
        A			:	Optional[Any]										=       type_vocab_size
        A			:	Optional[Any]										=       initializer_range
        A			:	Optional[int]										=       initializer_factor
        A			:	Tuple										=       layer_norm_eps
        A			:	List[str]										=       position_embedding_type
        A			:	int										=       use_cache
        A			:	int										=       project_dim






class SCREAMING_SNAKE_CASE__					(		SCREAMING_SNAKE_CASE__   ):




    '''simple docstring'''






    __lowerCamelCase      :			Optional[Any] =				"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__,		):
        super().__init__(**lowerCamelCase__		)

        A			:	Optional[Any]										=       hidden_size
        A			:	Optional[int]										=       intermediate_size
        A			:	Union[str, Any]										=       projection_dim
        A			:	str										=       num_hidden_layers
        A			:	int										=       num_attention_heads
        A			:	Optional[Any]										=       num_channels
        A			:	Tuple										=       patch_size
        A			:	List[Any]										=       image_size
        A			:	Optional[int]										=       initializer_range
        A			:	Union[str, Any]										=       initializer_factor
        A			:	List[str]										=       attention_dropout
        A			:	int										=       layer_norm_eps
        A			:	str										=       hidden_act






    @classmethod
    def       _lowerCAmelCase (	cls,		lowerCamelCase__,		**lowerCamelCase__		):
        cls._set_token_in_kwargs(lowerCamelCase__		)

        A					,							A			:	Optional[Any]										=       cls.get_config_dict(lowerCamelCase__,		**lowerCamelCase__		)

        # get the vision config dict if we are loading from AltCLIPConfig
        if config_dict.get("""model_type"""		) == "altclip":
            A			:	Any										=       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 SCREAMING_SNAKE_CASE__					(		SCREAMING_SNAKE_CASE__   ):




    '''simple docstring'''






    __lowerCamelCase      :			List[Any] =				"altclip"
    __lowerCamelCase      :			List[Any] =				True






    def __init__(	self,		lowerCamelCase__=None,		lowerCamelCase__=None,		lowerCamelCase__=768,		lowerCamelCase__=2.6592,		**lowerCamelCase__		):
        # 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!).
        A			:	Dict										=       kwargs.pop("""text_config_dict""",		lowerCamelCase__		)
        A			:	str										=       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:
                A			:	Dict										=       {}

            # This is the complete result when using `text_config_dict`.
            A			:	str										=       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:
                        A			:	Optional[Any]										=       (
                            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:
                        A			:	Optional[int]										=       (
                            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:
                A			:	int										=       {}

            # This is the complete result when using `vision_config_dict`.
            A			:	Union[str, Any]										=       AltCLIPVisionConfig(**lowerCamelCase__		).to_dict()
            # convert keys to string instead of integer
            if "id2label" in _vision_config_dict:
                A			:	Optional[int]										=       {
                    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:
                        A			:	Optional[int]										=       (
                            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:
                        A			:	Any										=       (
                            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:
            A			:	Tuple										=       {}
            logger.info("""`text_config` is `None`. Initializing the `AltCLIPTextConfig` with default values."""		)

        if vision_config is None:
            A			:	Union[str, Any]										=       {}
            logger.info("""`vision_config` is `None`. initializing the `AltCLIPVisionConfig` with default values."""		)

        A			:	Dict										=       AltCLIPTextConfig(**lowerCamelCase__		)
        A			:	Optional[int]										=       AltCLIPVisionConfig(**lowerCamelCase__		)

        A			:	List[str]										=       projection_dim
        A			:	Any										=       logit_scale_init_value
        A			:	Tuple										=       1.0






    @classmethod
    def       _lowerCAmelCase (	cls,		lowerCamelCase__,		lowerCamelCase__,		**lowerCamelCase__		):
        return cls(text_config=text_config.to_dict(),		vision_config=vision_config.to_dict(),		**lowerCamelCase__		)






    def       _lowerCAmelCase (	self		):
        A			:	str										=       copy.deepcopy(self.__dict__		)
        A			:	Any										=       self.text_config.to_dict()
        A			:	List[str]										=       self.vision_config.to_dict()
        A			:	Union[str, Any]										=       self.__class__.model_type
        return output







import json
import os
from typing import Dict, List, Optional, Tuple

import regex as re

from ...tokenization_utils import PreTrainedTokenizer
from ...utils import logging


SCREAMING_SNAKE_CASE_:Dict					=						logging.get_logger(__name__)


SCREAMING_SNAKE_CASE_:int					=						{
    """vocab_file""": """vocab.json""",
    """merges_file""": """merges.txt""",
    """tokenizer_config_file""": """tokenizer_config.json""",
}

SCREAMING_SNAKE_CASE_:Tuple					=						{
    """vocab_file""": {
        """facebook/blenderbot_small-90M""": """https://huggingface.co/facebook/blenderbot_small-90M/resolve/main/vocab.json"""
    },
    """merges_file""": {
        """facebook/blenderbot_small-90M""": """https://huggingface.co/facebook/blenderbot_small-90M/resolve/main/merges.txt"""
    },
    """tokenizer_config_file""": {
        """facebook/blenderbot_small-90M""": (
            """https://huggingface.co/facebook/blenderbot_small-90M/resolve/main/tokenizer_config.json"""
        )
    },
}

SCREAMING_SNAKE_CASE_:Optional[int]					=						{"""facebook/blenderbot_small-90M""": 512}




def      __UpperCamelCase     (  _lowerCAmelCase		)	->  Dict:



    """simple docstring"""






    A			:	Optional[int]										=       set()
    A			:	Any										=       word[0]
    for char in word[1:]:
        pairs.add((prev_char, char)		)
        A			:	List[Any]										=       char

    A			:	Optional[int]										=       set(_lowerCAmelCase		)
    return pairs






class SCREAMING_SNAKE_CASE__					(		SCREAMING_SNAKE_CASE__   ):




    '''simple docstring'''






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






    def __init__(	self,		lowerCamelCase__,		lowerCamelCase__,		lowerCamelCase__="__start__",		lowerCamelCase__="__end__",		lowerCamelCase__="__unk__",		lowerCamelCase__="__null__",		**lowerCamelCase__,		):
        super().__init__(unk_token=lowerCamelCase__,		bos_token=lowerCamelCase__,		eos_token=lowerCamelCase__,		pad_token=lowerCamelCase__,		**lowerCamelCase__		)

        with open(lowerCamelCase__,		encoding="""utf-8"""		) as vocab_handle:
            A			:	Tuple										=       json.load(lowerCamelCase__		)
        A			:	Optional[Any]										=       {v: k for k, v in self.encoder.items()}
        with open(lowerCamelCase__,		encoding="""utf-8"""		) as merges_handle:
            A			:	str										=       merges_handle.read().split("""\n"""		)[1:-1]
        A			:	List[str]										=       [tuple(merge.split()		) for merge in merges]
        A			:	int										=       dict(zip(lowerCamelCase__,		range(len(lowerCamelCase__		)		)		)		)
        A			:	Optional[int]										=       {}






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






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






    def       _lowerCAmelCase (	self,		lowerCamelCase__		):
        if token in self.cache:
            return self.cache[token]
        A			:	Optional[int]										=       re.sub("""([.,!?()])""",		R""" \1""",		lowerCamelCase__		)
        A			:	List[Any]										=       re.sub("""(')""",		R""" \1 """,		lowerCamelCase__		)
        A			:	int										=       re.sub(R"""\s{2,}""",		""" """,		lowerCamelCase__		)
        if "\n" in token:
            A			:	Dict										=       token.replace("""\n""",		""" __newln__"""		)

        A			:	Tuple										=       token.split(""" """		)
        A			:	Union[str, Any]										=       []
        for token in tokens:
            if not len(lowerCamelCase__		):
                continue

            A			:	Optional[int]										=       token.lower()
            A			:	Optional[Any]										=       tuple(lowerCamelCase__		)
            A			:	int										=       tuple(list(word[:-1]		) + [word[-1] + """</w>"""]		)
            A			:	Tuple										=       get_pairs(lowerCamelCase__		)

            if not pairs:
                words.append(lowerCamelCase__		)
                continue

            while True:
                A			:	Any										=       min(lowerCamelCase__,		key=lambda lowerCamelCase__		: self.bpe_ranks.get(lowerCamelCase__,		float("""inf"""		)		)		)
                if bigram not in self.bpe_ranks:
                    break
                A					,							A			:	Any										=       bigram
                A			:	Optional[Any]										=       []
                A			:	Any										=       0

                while i < len(lowerCamelCase__		):
                    try:
                        A			:	List[str]										=       word.index(lowerCamelCase__,		lowerCamelCase__		)
                        new_word.extend(word[i:j]		)
                        A			:	Dict										=       j
                    except ValueError:
                        new_word.extend(word[i:]		)
                        break

                    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
                A			:	Tuple										=       tuple(lowerCamelCase__		)
                A			:	int										=       new_word
                if len(lowerCamelCase__		) == 1:
                    break
                else:
                    A			:	Dict										=       get_pairs(lowerCamelCase__		)
            A			:	Any										=       """@@ """.join(lowerCamelCase__		)
            A			:	Dict										=       word[:-4]

            A			:	Union[str, Any]										=       word
            words.append(lowerCamelCase__		)
        return " ".join(lowerCamelCase__		)






    def       _lowerCAmelCase (	self,		lowerCamelCase__		):
        A			:	int										=       []

        A			:	Optional[Any]										=       re.findall(R"""\S+\n?""",		lowerCamelCase__		)

        for token in words:
            split_tokens.extend(list(self.bpe(lowerCamelCase__		).split(""" """		)		)		)
        return split_tokens






    def       _lowerCAmelCase (	self,		lowerCamelCase__		):
        A			:	List[Any]										=       token.lower()
        return self.encoder.get(lowerCamelCase__,		self.encoder.get(self.unk_token		)		)






    def       _lowerCAmelCase (	self,		lowerCamelCase__		):
        return self.decoder.get(lowerCamelCase__,		self.unk_token		)






    def       _lowerCAmelCase (	self,		lowerCamelCase__		):
        A			:	Dict										=       """ """.join(lowerCamelCase__		).replace("""@@ """,		""""""		).strip()
        return out_string






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

        A			:	str										=       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!"""		)
                    A			:	List[Any]										=       token_index
                writer.write(""" """.join(lowerCamelCase__		) + """\n"""		)
                index += 1

        return vocab_file, merge_file


