





'''simple docstring'''




import copy
import os
from typing import Union

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


a__					:			Tuple          =     logging.get_logger(__name__)

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






class 				__snake_case     (					__magic_name__							):
	__lowerCAmelCase       						=  '''altclip_text_model'''





	def __init__(		self      ,       UpperCamelCase_=25_0002      ,       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_      ,       )      ->		Tuple:
		super().__init__(pad_token_id=UpperCamelCase_      ,       bos_token_id=UpperCamelCase_      ,       eos_token_id=UpperCamelCase_      ,       **UpperCamelCase_		)

		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 				__snake_case     (					__magic_name__							):
	__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_      ,       )      ->		Any:
		super().__init__(**UpperCamelCase_		)

		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   _snake_case   (		cls      ,       UpperCamelCase_      ,       **UpperCamelCase_		)      ->		"PretrainedConfig":
		cls._set_token_in_kwargs(UpperCamelCase_		)

		snake_case__       ,  snake_case__	       =   cls.get_config_dict(UpperCamelCase_      ,       **UpperCamelCase_		)

		# 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(UpperCamelCase_      ,       **UpperCamelCase_		)






class 				__snake_case     (					__magic_name__							):
	__lowerCAmelCase       						=  '''altclip'''
	__lowerCAmelCase       						=  True





	def __init__(		self      ,       UpperCamelCase_=None      ,       UpperCamelCase_=None      ,       UpperCamelCase_=768      ,       UpperCamelCase_=2.6_5_9_2      ,       **UpperCamelCase_		)      ->		Union[str, 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'      ,       UpperCamelCase_		)
		snake_case__	       =   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:
				snake_case__	       =   {}

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

			# This is the complete result when using `vision_config_dict`.
			snake_case__	       =   AltCLIPVisionConfig(**UpperCamelCase_		).to_dict()
			# convert keys to string instead of integer
			if "id2label" in _vision_config_dict:
				snake_case__	       =   {
				    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:
						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(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:
			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(**UpperCamelCase_		)
		snake_case__	       =   AltCLIPVisionConfig(**UpperCamelCase_		)

		snake_case__	       =   projection_dim
		snake_case__	       =   logit_scale_init_value
		snake_case__	       =   1.0





	@classmethod
	def   _snake_case   (		cls      ,       UpperCamelCase_      ,       UpperCamelCase_      ,       **UpperCamelCase_		)      ->		Any:
		return cls(text_config=text_config.to_dict()      ,       vision_config=vision_config.to_dict()      ,       **UpperCamelCase_		)





	def   _snake_case   (		self		)      ->		Tuple:
		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










'''simple docstring'''




import unittest

import numpy as np

from transformers import RobertaConfig, is_flax_available
from transformers.testing_utils import require_flax, slow

from ...test_modeling_flax_common import FlaxModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask


if is_flax_available():
			from transformers.models.roberta.modeling_flax_roberta import (
			    FlaxRobertaForCausalLM,
			    FlaxRobertaForMaskedLM,
			    FlaxRobertaForMultipleChoice,
			    FlaxRobertaForQuestionAnswering,
			    FlaxRobertaForSequenceClassification,
			    FlaxRobertaForTokenClassification,
			    FlaxRobertaModel,
			)






class 				__snake_case     (					unittest.TestCase							):





	def __init__(		self      ,       UpperCamelCase_      ,       UpperCamelCase_=13      ,       UpperCamelCase_=7      ,       UpperCamelCase_=True      ,       UpperCamelCase_=True      ,       UpperCamelCase_=True      ,       UpperCamelCase_=True      ,       UpperCamelCase_=99      ,       UpperCamelCase_=32      ,       UpperCamelCase_=5      ,       UpperCamelCase_=4      ,       UpperCamelCase_=37      ,       UpperCamelCase_="gelu"      ,       UpperCamelCase_=0.1      ,       UpperCamelCase_=0.1      ,       UpperCamelCase_=512      ,       UpperCamelCase_=16      ,       UpperCamelCase_=2      ,       UpperCamelCase_=0.0_2      ,       UpperCamelCase_=4      ,       )      ->		List[Any]:
		snake_case__	       =   parent
		snake_case__	       =   batch_size
		snake_case__	       =   seq_length
		snake_case__	       =   is_training
		snake_case__	       =   use_attention_mask
		snake_case__	       =   use_token_type_ids
		snake_case__	       =   use_labels
		snake_case__	       =   vocab_size
		snake_case__	       =   hidden_size
		snake_case__	       =   num_hidden_layers
		snake_case__	       =   num_attention_heads
		snake_case__	       =   intermediate_size
		snake_case__	       =   hidden_act
		snake_case__	       =   hidden_dropout_prob
		snake_case__	       =   attention_probs_dropout_prob
		snake_case__	       =   max_position_embeddings
		snake_case__	       =   type_vocab_size
		snake_case__	       =   type_sequence_label_size
		snake_case__	       =   initializer_range
		snake_case__	       =   num_choices





	def   _snake_case   (		self		)      ->		Any:
		snake_case__	       =   ids_tensor([self.batch_size, self.seq_length]      ,       self.vocab_size		)

		snake_case__	       =   None
		if self.use_attention_mask:
			snake_case__	       =   random_attention_mask([self.batch_size, self.seq_length]		)

		snake_case__	       =   None
		if self.use_token_type_ids:
			snake_case__	       =   ids_tensor([self.batch_size, self.seq_length]      ,       self.type_vocab_size		)

		snake_case__	       =   RobertaConfig(
		    vocab_size=self.vocab_size      ,       hidden_size=self.hidden_size      ,       num_hidden_layers=self.num_hidden_layers      ,       num_attention_heads=self.num_attention_heads      ,       intermediate_size=self.intermediate_size      ,       hidden_act=self.hidden_act      ,       hidden_dropout_prob=self.hidden_dropout_prob      ,       attention_probs_dropout_prob=self.attention_probs_dropout_prob      ,       max_position_embeddings=self.max_position_embeddings      ,       type_vocab_size=self.type_vocab_size      ,       is_decoder=UpperCamelCase_      ,       initializer_range=self.initializer_range      ,       )

		return config, input_ids, token_type_ids, attention_mask





	def   _snake_case   (		self		)      ->		Tuple:
		snake_case__	       =   self.prepare_config_and_inputs()
		snake_case__       ,  snake_case__       ,  snake_case__       ,  snake_case__	       =   config_and_inputs
		snake_case__	       =   {'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': attention_mask}
		return config, inputs_dict





	def   _snake_case   (		self		)      ->		Union[str, Any]:
		snake_case__	       =   self.prepare_config_and_inputs()
		snake_case__       ,  snake_case__       ,  snake_case__       ,  snake_case__	       =   config_and_inputs

		snake_case__	       =   True
		snake_case__	       =   floats_tensor([self.batch_size, self.seq_length, self.hidden_size]		)
		snake_case__	       =   ids_tensor([self.batch_size, self.seq_length]      ,       vocab_size=2		)

		return (
		    config,
		    input_ids,
		    token_type_ids,
		    encoder_hidden_states,
		    encoder_attention_mask,
		)






@require_flax
class 				__snake_case     (					__magic_name__       ,       unittest.TestCase							):
	__lowerCAmelCase       						=  True

	__lowerCAmelCase       						=  (
	    (
	        FlaxRobertaModel,
	        FlaxRobertaForCausalLM,
	        FlaxRobertaForMaskedLM,
	        FlaxRobertaForSequenceClassification,
	        FlaxRobertaForTokenClassification,
	        FlaxRobertaForMultipleChoice,
	        FlaxRobertaForQuestionAnswering,
	    )
	    if is_flax_available()
	    else ()
	)





	def   _snake_case   (		self		)      ->		List[Any]:
		snake_case__	       =   FlaxRobertaModelTester(self		)





	@slow
	def   _snake_case   (		self		)      ->		Tuple:
		for model_class_name in self.all_model_classes:
			snake_case__	       =   model_class_name.from_pretrained('roberta-base'      ,       from_pt=UpperCamelCase_		)
			snake_case__	       =   model(np.ones((1, 1)		)		)
			self.assertIsNotNone(UpperCamelCase_		)



