





import copy
import os
from typing import Union

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


UpperCAmelCase__						:			int									=logging.get_logger(__name__)

UpperCAmelCase__						:			List[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     (						_A    ):
				__A      =				"""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-05   , 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__							)

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




class       __A     (						_A    ):
				__A      =				"""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__							)

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


				@classmethod
				def _snake_case       (		cls   , UpperCAmelCase_   , **UpperCAmelCase_							):
								cls._set_token_in_kwargs(UpperCamelCase__							)

								lowerCamelCase ,  lowerCamelCase	 =cls.get_config_dict(UpperCamelCase__   , **UpperCamelCase__							)

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


				def __init__(		self   , UpperCAmelCase_=None   , UpperCAmelCase_=None   , UpperCAmelCase_=768   , UpperCAmelCase_=2.6_5_9_2   , **UpperCAmelCase_							):
								lowerCamelCase	 =kwargs.pop("""text_config_dict"""   , UpperCamelCase__							)
								lowerCamelCase	 =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:
																lowerCamelCase	 ={}

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

												# This is the complete result when using `vision_config_dict`.
												lowerCamelCase	 =AltCLIPVisionConfig(**UpperCamelCase__							).to_dict()
												# convert keys to string instead of integer
												if "id2label" in _vision_config_dict:
																lowerCamelCase	 ={
																    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:
																								lowerCamelCase	 =(
																								    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:
																								lowerCamelCase	 =(
																								    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:
												lowerCamelCase	 ={}
												logger.info("""`text_config` is `None`. Initializing the `AltCLIPTextConfig` with default values."""							)

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

								lowerCamelCase	 =AltCLIPTextConfig(**UpperCamelCase__							)
								lowerCamelCase	 =AltCLIPVisionConfig(**UpperCamelCase__							)

								lowerCamelCase	 =projection_dim
								lowerCamelCase	 =logit_scale_init_value
								lowerCamelCase	 =1.0


				@classmethod
				def _snake_case       (		cls   , UpperCAmelCase_   , UpperCAmelCase_   , **UpperCAmelCase_							):
								return cls(text_config=text_config.to_dict()   , vision_config=vision_config.to_dict()   , **UpperCamelCase__							)


				def _snake_case       (		self							):
								lowerCamelCase	 =copy.deepcopy(self.__dict__							)
								lowerCamelCase	 =self.text_config.to_dict()
								lowerCamelCase	 =self.vision_config.to_dict()
								lowerCamelCase	 =self.__class__.model_type
								return output












from tempfile import TemporaryDirectory
from unittest import TestCase
from unittest.mock import MagicMock, patch

from transformers import AutoModel, TFAutoModel
from transformers.onnx import FeaturesManager
from transformers.testing_utils import SMALL_MODEL_IDENTIFIER, require_tf, require_torch

@require_torch
@require_tf
class       __A     (						a    ):


				def _snake_case       (		self							):
								lowerCamelCase	 =SMALL_MODEL_IDENTIFIER
								lowerCamelCase	 ="""pt"""
								lowerCamelCase	 ="""tf"""


				def _snake_case       (		self   , UpperCAmelCase_							):
								lowerCamelCase	 =AutoModel.from_pretrained(self.test_model							)
								model_pt.save_pretrained(UpperCAmelCase_							)


				def _snake_case       (		self   , UpperCAmelCase_							):
								lowerCamelCase	 =TFAutoModel.from_pretrained(self.test_model   , from_pt=UpperCAmelCase_							)
								model_tf.save_pretrained(UpperCAmelCase_							)


				def _snake_case       (		self							):
								lowerCamelCase	 ="""mock_framework"""

								# Framework provided - return whatever the user provides
								lowerCamelCase	 =FeaturesManager.determine_framework(self.test_model   , UpperCAmelCase_							)
								self.assertEqual(UpperCAmelCase_   , UpperCAmelCase_							)

								# Local checkpoint and framework provided - return provided framework
								# PyTorch checkpoint
								with TemporaryDirectory() as local_pt_ckpt:
												self._setup_pt_ckpt(UpperCAmelCase_							)
												lowerCamelCase	 =FeaturesManager.determine_framework(UpperCAmelCase_   , UpperCAmelCase_							)
												self.assertEqual(UpperCAmelCase_   , UpperCAmelCase_							)

								# TensorFlow checkpoint
								with TemporaryDirectory() as local_tf_ckpt:
												self._setup_tf_ckpt(UpperCAmelCase_							)
												lowerCamelCase	 =FeaturesManager.determine_framework(UpperCAmelCase_   , UpperCAmelCase_							)
												self.assertEqual(UpperCAmelCase_   , UpperCAmelCase_							)


				def _snake_case       (		self							):
								# PyTorch checkpoint
								with TemporaryDirectory() as local_pt_ckpt:
												self._setup_pt_ckpt(UpperCAmelCase_							)
												lowerCamelCase	 =FeaturesManager.determine_framework(UpperCAmelCase_							)
												self.assertEqual(UpperCAmelCase_   , self.framework_pt							)

								# TensorFlow checkpoint
								with TemporaryDirectory() as local_tf_ckpt:
												self._setup_tf_ckpt(UpperCAmelCase_							)
												lowerCamelCase	 =FeaturesManager.determine_framework(UpperCAmelCase_							)
												self.assertEqual(UpperCAmelCase_   , self.framework_tf							)

								# Invalid local checkpoint
								with TemporaryDirectory() as local_invalid_ckpt:
												with self.assertRaises(UpperCAmelCase_							):
																lowerCamelCase	 =FeaturesManager.determine_framework(UpperCAmelCase_							)


				def _snake_case       (		self							):
								lowerCamelCase	 =MagicMock(return_value=UpperCAmelCase_							)
								with patch("""transformers.onnx.features.is_tf_available"""   , UpperCAmelCase_							):
												lowerCamelCase	 =FeaturesManager.determine_framework(self.test_model							)
												self.assertEqual(UpperCAmelCase_   , self.framework_pt							)

								# PyTorch not in environment -> use TensorFlow
								lowerCamelCase	 =MagicMock(return_value=UpperCAmelCase_							)
								with patch("""transformers.onnx.features.is_torch_available"""   , UpperCAmelCase_							):
												lowerCamelCase	 =FeaturesManager.determine_framework(self.test_model							)
												self.assertEqual(UpperCAmelCase_   , self.framework_tf							)

								# Both in environment -> use PyTorch
								lowerCamelCase	 =MagicMock(return_value=UpperCAmelCase_							)
								lowerCamelCase	 =MagicMock(return_value=UpperCAmelCase_							)
								with patch("""transformers.onnx.features.is_tf_available"""   , UpperCAmelCase_							), patch(
								    """transformers.onnx.features.is_torch_available"""   , UpperCAmelCase_							):
												lowerCamelCase	 =FeaturesManager.determine_framework(self.test_model							)
												self.assertEqual(UpperCAmelCase_   , self.framework_pt							)

								# Both not in environment -> raise error
								lowerCamelCase	 =MagicMock(return_value=UpperCAmelCase_							)
								lowerCamelCase	 =MagicMock(return_value=UpperCAmelCase_							)
								with patch("""transformers.onnx.features.is_tf_available"""   , UpperCAmelCase_							), patch(
								    """transformers.onnx.features.is_torch_available"""   , UpperCAmelCase_							):
												with self.assertRaises(UpperCAmelCase_							):
																lowerCamelCase	 =FeaturesManager.determine_framework(self.test_model							)





