



import copy
import os
from typing import Union

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


__snake_case						:List[Any]						=					logging.get_logger(__name__)

__snake_case						:Union[str, Any]						=					{
    '''BAAI/AltCLIP''': '''https://huggingface.co/BAAI/AltCLIP/resolve/main/config.json''',
    # See all AltCLIP models at https://huggingface.co/models?filter=altclip
}

class    _A	(     lowerCamelCase_   ):
							UpperCamelCase__  :   Tuple						 =						"""altclip_text_model"""



							def __init__(		self							:					Tuple					,		__SCREAMING_SNAKE_CASE							:					List[Any]=250_002					,		__SCREAMING_SNAKE_CASE							:					Union[str, Any]=1_024					,		__SCREAMING_SNAKE_CASE							:					Any=24					,		__SCREAMING_SNAKE_CASE							:					List[str]=16					,		__SCREAMING_SNAKE_CASE							:					Optional[int]=4_096					,		__SCREAMING_SNAKE_CASE							:					Optional[int]="gelu"					,		__SCREAMING_SNAKE_CASE							:					Tuple=0.1					,		__SCREAMING_SNAKE_CASE							:					List[str]=0.1					,		__SCREAMING_SNAKE_CASE							:					Tuple=514					,		__SCREAMING_SNAKE_CASE							:					List[Any]=1					,		__SCREAMING_SNAKE_CASE							:					List[str]=0.02					,		__SCREAMING_SNAKE_CASE							:					Dict=0.02					,		__SCREAMING_SNAKE_CASE							:					Optional[int]=1E-05					,		__SCREAMING_SNAKE_CASE							:					Optional[Any]=1					,		__SCREAMING_SNAKE_CASE							:					int=0					,		__SCREAMING_SNAKE_CASE							:					Dict=2					,		__SCREAMING_SNAKE_CASE							:					List[Any]="absolute"					,		__SCREAMING_SNAKE_CASE							:					List[str]=True					,		__SCREAMING_SNAKE_CASE							:					Optional[Any]=768					,		**__SCREAMING_SNAKE_CASE							:					Union[str, Any]					,		):


											'''simple docstring'''

											super().__init__(pad_token_id=_UpperCAmelCase					,		bos_token_id=_UpperCAmelCase					,		eos_token_id=_UpperCAmelCase					,		**_UpperCAmelCase)

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


class    _A	(     lowerCamelCase_   ):
							UpperCamelCase__  :   str						 =						"""altclip_vision_model"""



							def __init__(		self							:					Optional[Any]					,		__SCREAMING_SNAKE_CASE							:					Union[str, Any]=768					,		__SCREAMING_SNAKE_CASE							:					Tuple=3_072					,		__SCREAMING_SNAKE_CASE							:					Optional[Any]=512					,		__SCREAMING_SNAKE_CASE							:					Tuple=12					,		__SCREAMING_SNAKE_CASE							:					str=12					,		__SCREAMING_SNAKE_CASE							:					Union[str, Any]=3					,		__SCREAMING_SNAKE_CASE							:					Tuple=224					,		__SCREAMING_SNAKE_CASE							:					Union[str, Any]=32					,		__SCREAMING_SNAKE_CASE							:					List[Any]="quick_gelu"					,		__SCREAMING_SNAKE_CASE							:					Optional[Any]=1E-5					,		__SCREAMING_SNAKE_CASE							:					Tuple=0.0					,		__SCREAMING_SNAKE_CASE							:					List[str]=0.02					,		__SCREAMING_SNAKE_CASE							:					Optional[Any]=1.0					,		**__SCREAMING_SNAKE_CASE							:					Optional[int]					,		):


											'''simple docstring'''

											super().__init__(**_UpperCAmelCase)

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





							@classmethod
							def 		_lowerCamelCase      (		cls							:					Union[str, Any]					,		__SCREAMING_SNAKE_CASE							:					Union[str, os.PathLike]					,		**__SCREAMING_SNAKE_CASE							:					List[Any]):


											'''simple docstring'''

											cls._set_token_in_kwargs(_UpperCAmelCase)

											__a	,       __a							=    cls.get_config_dict(_UpperCAmelCase					,		**_UpperCAmelCase)

											# get the vision config dict if we are loading from AltCLIPConfig
											if config_dict.get('''model_type''') == "altclip":
															__a							=    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(_UpperCAmelCase					,		**_UpperCAmelCase)


class    _A	(     lowerCamelCase_   ):
							UpperCamelCase__  :   Optional[Any]						 =						"""altclip"""
							UpperCamelCase__  :   Any						 =						True



							def __init__(		self							:					Optional[Any]					,		__SCREAMING_SNAKE_CASE							:					List[str]=None					,		__SCREAMING_SNAKE_CASE							:					Optional[Any]=None					,		__SCREAMING_SNAKE_CASE							:					Optional[int]=768					,		__SCREAMING_SNAKE_CASE							:					Dict=2.65_92					,		**__SCREAMING_SNAKE_CASE							:					int):


											'''simple docstring'''

											__a							=    kwargs.pop('''text_config_dict'''					,		_UpperCAmelCase)
											__a							=    kwargs.pop('''vision_config_dict'''					,		_UpperCAmelCase)

											super().__init__(**_UpperCAmelCase)

											# 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							=    {}

															# This is the complete result when using `text_config_dict`.
															__a							=    AltCLIPTextConfig(**_UpperCAmelCase).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							=    (
																											    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							=    (
																											    F'`text_config_dict` is provided which will be used to initialize `AltCLIPTextConfig`. The '
																											    F'value `text_config["{key}"]` will be overriden.'
																											)
																							logger.warning(_UpperCAmelCase)

																							      # 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							=    {}

															# This is the complete result when using `vision_config_dict`.
															__a							=    AltCLIPVisionConfig(**_UpperCAmelCase).to_dict()
															# convert keys to string instead of integer
															if "id2label" in _vision_config_dict:
																			__a							=    {
																			    str(_UpperCAmelCase): 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							=    (
																											    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							=    (
																											    F'`vision_config_dict` is provided which will be used to initialize `AltCLIPVisionConfig`. '
																											    F'The value `vision_config["{key}"]` will be overriden.'
																											)
																							logger.warning(_UpperCAmelCase)

																							      # 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							=    {}
															logger.info('''`text_config` is `None`. Initializing the `AltCLIPTextConfig` with default values.''')

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

											__a							=    AltCLIPTextConfig(**_UpperCAmelCase)
											__a							=    AltCLIPVisionConfig(**_UpperCAmelCase)

											__a							=    projection_dim
											__a							=    logit_scale_init_value
											__a							=    1.0



							@classmethod
							def 		_lowerCamelCase      (		cls							:					Dict					,		__SCREAMING_SNAKE_CASE							:					AltCLIPTextConfig					,		__SCREAMING_SNAKE_CASE							:					AltCLIPVisionConfig					,		**__SCREAMING_SNAKE_CASE							:					int):


											'''simple docstring'''

											return cls(text_config=text_config.to_dict()					,		vision_config=vision_config.to_dict()					,		**_UpperCAmelCase)





							def 		_lowerCamelCase      (		self							:					Optional[int]):


											'''simple docstring'''

											__a							=    copy.deepcopy(self.__dict__)
											__a							=    self.text_config.to_dict()
											__a							=    self.vision_config.to_dict()
											__a							=    self.__class__.model_type
											return output










def 					__snake_case     (      _UpperCAmelCase       ,			_UpperCAmelCase    ):

    def get_matched_characters(_UpperCAmelCase       ,			_UpperCAmelCase    ) -> str:
        __a							=    []
        __a							=    min(len(_stra    )       ,			len(_stra    )    ) // 2
        for i, l in enumerate(_stra    ):
            __a							=    int(max(0       ,			i - limit    )    )
            __a							=    int(min(i + limit + 1       ,			len(_stra    )    )    )
            if l in _stra[left:right]:
                matched.append(_UpperCAmelCase    )
                __a							=    f'{_stra[0:_stra.index(_UpperCAmelCase    )]} {_stra[_stra.index(_UpperCAmelCase    ) + 1:]}'

        return "".join(_UpperCAmelCase    )

    # matching characters
    __a							=    get_matched_characters(_UpperCAmelCase       ,			_UpperCAmelCase    )
    __a							=    get_matched_characters(_UpperCAmelCase       ,			_UpperCAmelCase    )
    __a							=    len(_UpperCAmelCase    )

    # transposition
    __a							=    (
        len([(ca, ca) for ca, ca in zip(_UpperCAmelCase       ,			_UpperCAmelCase    ) if ca != ca]    ) // 2
    )

    if not match_count:
        __a							=    0.0
    else:
        __a							=    (
            1
            / 3
            * (
                match_count / len(_UpperCAmelCase    )
                + match_count / len(_UpperCAmelCase    )
                + (match_count - transpositions) / match_count
            )
        )

    # common prefix up to 4 characters
    __a							=    0
    for ca, ca in zip(stra[:4]       ,			stra[:4]    ):
        if ca == ca:
            prefix_len += 1
        else:
            break

    return jaro + 0.1 * prefix_len * (1 - jaro)


if __name__ == "__main__":
 import doctest

 doctest.testmod()
 print(jaro_winkler('''hello''', '''world'''))





