





"""simple docstring"""






import copy
import os
from typing import Union

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


A 			=  logging.get_logger(__name__)

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




class 				__lowercase			(			_UpperCamelCase							):







					'''simple docstring'''




					__lowerCAmelCase  =  '''altclip_text_model'''







					def __init__(						self	,			_UpperCAmelCase=250002	,			_UpperCAmelCase=1024	,			_UpperCAmelCase=24	,			_UpperCAmelCase=16	,			_UpperCAmelCase=4096	,			_UpperCAmelCase="gelu"	,			_UpperCAmelCase=0.1	,			_UpperCAmelCase=0.1	,			_UpperCAmelCase=514	,			_UpperCAmelCase=1	,			_UpperCAmelCase=0.0_2	,			_UpperCAmelCase=0.0_2	,			_UpperCAmelCase=1e-0_5	,			_UpperCAmelCase=1	,			_UpperCAmelCase=0	,			_UpperCAmelCase=2	,			_UpperCAmelCase="absolute"	,			_UpperCAmelCase=True	,			_UpperCAmelCase=768	,			**_UpperCAmelCase	,			):
									super().__init__(pad_token_id=_UpperCAmelCase	,			bos_token_id=_UpperCAmelCase	,			eos_token_id=_UpperCAmelCase	,			**_UpperCAmelCase     )

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






class 				__lowercase			(			_UpperCamelCase							):







					'''simple docstring'''




					__lowerCAmelCase  =  '''altclip_vision_model'''







					def __init__(						self	,			_UpperCAmelCase=768	,			_UpperCAmelCase=3072	,			_UpperCAmelCase=512	,			_UpperCAmelCase=12	,			_UpperCAmelCase=12	,			_UpperCAmelCase=3	,			_UpperCAmelCase=224	,			_UpperCAmelCase=32	,			_UpperCAmelCase="quick_gelu"	,			_UpperCAmelCase=1e-5	,			_UpperCAmelCase=0.0	,			_UpperCAmelCase=0.0_2	,			_UpperCAmelCase=1.0	,			**_UpperCAmelCase	,			):
									super().__init__(**_UpperCAmelCase     )

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







					@classmethod
					def  _lowerCamelCase		(						cls	,			_UpperCAmelCase	,			**_UpperCAmelCase     ):
									cls._set_token_in_kwargs(_UpperCAmelCase     )

									__a       ,						__a :						Any			       =  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 :						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(_UpperCAmelCase	,			**_UpperCAmelCase     )






class 				__lowercase			(			_UpperCamelCase							):







					'''simple docstring'''




					__lowerCAmelCase  =  '''altclip'''
					__lowerCAmelCase  =  True







					def __init__(						self	,			_UpperCAmelCase=None	,			_UpperCAmelCase=None	,			_UpperCAmelCase=768	,			_UpperCAmelCase=2.6_5_9_2	,			**_UpperCAmelCase     ):
									# 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 :						List[Any]			       =  kwargs.pop('''text_config_dict'''	,			_UpperCAmelCase     )
									__a :						str			       =  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 :						int			       =  {}

													# This is the complete result when using `text_config_dict`.
													__a :						str			       =  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 :						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 :						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(_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 :						Tuple			       =  {}

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

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

									__a :						Optional[Any]			       =  AltCLIPTextConfig(**_UpperCAmelCase     )
									__a :						Dict			       =  AltCLIPVisionConfig(**_UpperCAmelCase     )

									__a :						Dict			       =  projection_dim
									__a :						int			       =  logit_scale_init_value
									__a :						int			       =  1.0







					@classmethod
					def  _lowerCamelCase		(						cls	,			_UpperCAmelCase	,			_UpperCAmelCase	,			**_UpperCAmelCase     ):
									return cls(text_config=text_config.to_dict()	,			vision_config=vision_config.to_dict()	,			**_UpperCAmelCase     )







					def  _lowerCamelCase		(						self     ):
									__a :						Dict			       =  copy.deepcopy(self.__dict__     )
									__a :						int			       =  self.text_config.to_dict()
									__a :						List[str]			       =  self.vision_config.to_dict()
									__a :						List[str]			       =  self.__class__.model_type
									return output






"""simple docstring"""






import json
import os
import unittest

from transformers import OpenAIGPTTokenizer, OpenAIGPTTokenizerFast
from transformers.models.openai.tokenization_openai import VOCAB_FILES_NAMES
from transformers.testing_utils import require_ftfy, require_spacy, require_tokenizers

from ...test_tokenization_common import TokenizerTesterMixin




@require_tokenizers
class 				__lowercase			(			_UpperCamelCase   ,      unittest.TestCase							):







					'''simple docstring'''




					__lowerCAmelCase  =  OpenAIGPTTokenizer
					__lowerCAmelCase  =  OpenAIGPTTokenizerFast
					__lowerCAmelCase  =  True
					__lowerCAmelCase  =  False







					def  _lowerCamelCase		(						self     ):
									super().setUp()

									# Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt
									__a :						List[str]			       =  [
									    '''l''',
									    '''o''',
									    '''w''',
									    '''e''',
									    '''r''',
									    '''s''',
									    '''t''',
									    '''i''',
									    '''d''',
									    '''n''',
									    '''w</w>''',
									    '''r</w>''',
									    '''t</w>''',
									    '''lo''',
									    '''low''',
									    '''er</w>''',
									    '''low</w>''',
									    '''lowest</w>''',
									    '''newer</w>''',
									    '''wider</w>''',
									    '''<unk>''',
									]
									__a :						str			       =  dict(zip(_UpperCAmelCase	,			range(len(_UpperCAmelCase     )     )     )     )
									__a :						List[Any]			       =  ['''#version: 0.2''', '''l o''', '''lo w''', '''e r</w>''', '''''']

									__a :						Union[str, Any]			       =  os.path.join(self.tmpdirname	,			VOCAB_FILES_NAMES['''vocab_file''']     )
									__a :						Dict			       =  os.path.join(self.tmpdirname	,			VOCAB_FILES_NAMES['''merges_file''']     )
									with open(self.vocab_file	,			'''w'''     ) as fp:
													fp.write(json.dumps(_UpperCAmelCase     )     )
									with open(self.merges_file	,			'''w'''     ) as fp:
													fp.write('''\n'''.join(_UpperCAmelCase     )     )







					def  _lowerCamelCase		(						self	,			_UpperCAmelCase     ):
									return "lower newer", "lower newer"







					def  _lowerCamelCase		(						self     ):
									__a :						Tuple			       =  OpenAIGPTTokenizer(self.vocab_file	,			self.merges_file     )

									__a :						Tuple			       =  '''lower'''
									__a :						Union[str, Any]			       =  ['''low''', '''er</w>''']
									__a :						str			       =  tokenizer.tokenize(_UpperCAmelCase     )
									self.assertListEqual(_UpperCAmelCase	,			_UpperCAmelCase     )

									__a :						List[Any]			       =  tokens + ['''<unk>''']
									__a :						Tuple			       =  [14, 15, 20]
									self.assertListEqual(tokenizer.convert_tokens_to_ids(_UpperCAmelCase     )	,			_UpperCAmelCase     )







					def  _lowerCamelCase		(						self	,			_UpperCAmelCase=15     ):
									for tokenizer, pretrained_name, kwargs in self.tokenizers_list:
													with self.subTest(f"""{tokenizer.__class__.__name__} ({pretrained_name})"""     ):
																	__a :						Any			       =  self.rust_tokenizer_class.from_pretrained(_UpperCAmelCase	,			**_UpperCAmelCase     )

																	# Simple input
																	__a :						str			       =  '''This is a simple input'''
																	__a :						Dict			       =  ['''This is a simple input 1''', '''This is a simple input 2''']
																	__a :						List[Any]			       =  ('''This is a simple input''', '''This is a pair''')
																	__a :						Optional[Any]			       =  [
																	    ('''This is a simple input 1''', '''This is a simple input 2'''),
																	    ('''This is a simple pair 1''', '''This is a simple pair 2'''),
																	]

																	# Simple input tests
																	self.assertRaises(_UpperCAmelCase	,			tokenizer_r.encode	,			_UpperCAmelCase	,			max_length=_UpperCAmelCase	,			padding='''max_length'''     )

																	# Simple input
																	self.assertRaises(_UpperCAmelCase	,			tokenizer_r.encode_plus	,			_UpperCAmelCase	,			max_length=_UpperCAmelCase	,			padding='''max_length'''     )

																	# Simple input
																	self.assertRaises(
																	    _UpperCAmelCase	,			tokenizer_r.batch_encode_plus	,			_UpperCAmelCase	,			max_length=_UpperCAmelCase	,			padding='''max_length'''	,			)

																	# Pair input
																	self.assertRaises(_UpperCAmelCase	,			tokenizer_r.encode	,			_UpperCAmelCase	,			max_length=_UpperCAmelCase	,			padding='''max_length'''     )

																	# Pair input
																	self.assertRaises(_UpperCAmelCase	,			tokenizer_r.encode_plus	,			_UpperCAmelCase	,			max_length=_UpperCAmelCase	,			padding='''max_length'''     )

																	# Pair input
																	self.assertRaises(
																	    _UpperCAmelCase	,			tokenizer_r.batch_encode_plus	,			_UpperCAmelCase	,			max_length=_UpperCAmelCase	,			padding='''max_length'''	,			)







					def  _lowerCamelCase		(						self     ):
									pass






@require_ftfy
@require_spacy
@require_tokenizers
class 				__lowercase			(			_UpperCamelCase							):







					'''simple docstring'''




					pass