






"""simple docstring"""







from __future__ import annotations

import unittest

from transformers import is_tf_available
from transformers.testing_utils import require_tf, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
	import numpy
	import tensorflow as tf

	from transformers import (
	    TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST,
	    TF_DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST,
	    TF_DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST,
	    BertConfig,
	    DPRConfig,
	    TFDPRContextEncoder,
	    TFDPRQuestionEncoder,
	    TFDPRReader,
	)







class        _SCREAMING_SNAKE_CASE						:





							"""simple docstring"""





							def __init__(   self:     Union[str, Any]				,		__A:     Dict				,		__A:     Tuple=13				,		__A:     Union[str, Any]=7				,		__A:     Dict=True				,		__A:     Dict=True				,		__A:     Dict=True				,		__A:     Tuple=True				,		__A:     List[str]=99				,		__A:     Tuple=32				,		__A:     Union[str, Any]=2				,		__A:     List[str]=4				,		__A:     Tuple=37				,		__A:     int="gelu"				,		__A:     Union[str, Any]=0.1				,		__A:     int=0.1				,		__A:     Any=512				,		__A:     int=16				,		__A:     str=2				,		__A:     int=0.0_2				,		__A:     int=3				,		__A:     Dict=4				,		__A:     Union[str, Any]=None				,		__A:     int=0				,		):

												'''simple docstring'''

												a__          =  parent
												a__          =  batch_size
												a__          =  seq_length
												a__          =  is_training
												a__          =  use_input_mask
												a__          =  use_token_type_ids
												a__          =  use_labels
												a__          =  vocab_size
												a__          =  hidden_size
												a__          =  num_hidden_layers
												a__          =  num_attention_heads
												a__          =  intermediate_size
												a__          =  hidden_act
												a__          =  hidden_dropout_prob
												a__          =  attention_probs_dropout_prob
												a__          =  max_position_embeddings
												a__          =  type_vocab_size
												a__          =  type_sequence_label_size
												a__          =  initializer_range
												a__          =  num_labels
												a__          =  num_choices
												a__          =  scope
												a__          =  projection_dim




							def  lowercase		(   self:     Union[str, Any]    ):

												'''simple docstring'''

												a__          =  ids_tensor([self.batch_size, self.seq_length]				,		self.vocab_size    )

												a__          =  None
												if self.use_input_mask:
																	# follow test_modeling_tf_ctrl.py
																	a__          =  random_attention_mask([self.batch_size, self.seq_length]    )

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

												a__          =  None
												a__          =  None
												a__          =  None
												if self.use_labels:
																	a__          =  ids_tensor([self.batch_size]				,		self.type_sequence_label_size    )
																	a__          =  ids_tensor([self.batch_size, self.seq_length]				,		self.num_labels    )
																	a__          =  ids_tensor([self.batch_size]				,		self.num_choices    )

												a__          =  BertConfig(
												    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=__A				,		initializer_range=self.initializer_range				,		)
												a__          =  DPRConfig(projection_dim=self.projection_dim				,		**config.to_dict()    )

												return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels




							def  lowercase		(   self:     Optional[int]				,		__A:     str				,		__A:     Tuple				,		__A:     List[Any]				,		__A:     Any				,		__A:     int				,		__A:     List[str]				,		__A:     Optional[Any]    ):

												'''simple docstring'''

												a__          =  TFDPRContextEncoder(config=__A    )
												a__          =  model(__A				,		attention_mask=__A				,		token_type_ids=__A    )
												a__          =  model(__A				,		token_type_ids=__A    )
												a__          =  model(__A    )
												self.parent.assertEqual(result.pooler_output.shape				,		(self.batch_size, self.projection_dim or self.hidden_size)    )




							def  lowercase		(   self:     Tuple				,		__A:     str				,		__A:     int				,		__A:     Tuple				,		__A:     Union[str, Any]				,		__A:     Any				,		__A:     List[str]				,		__A:     Optional[int]    ):

												'''simple docstring'''

												a__          =  TFDPRQuestionEncoder(config=__A    )
												a__          =  model(__A				,		attention_mask=__A				,		token_type_ids=__A    )
												a__          =  model(__A				,		token_type_ids=__A    )
												a__          =  model(__A    )
												self.parent.assertEqual(result.pooler_output.shape				,		(self.batch_size, self.projection_dim or self.hidden_size)    )




							def  lowercase		(   self:     str				,		__A:     List[str]				,		__A:     Union[str, Any]				,		__A:     Any				,		__A:     Optional[Any]				,		__A:     int				,		__A:     Dict				,		__A:     int    ):

												'''simple docstring'''

												a__          =  TFDPRReader(config=__A    )
												a__          =  model(__A				,		attention_mask=__A    )

												self.parent.assertEqual(result.start_logits.shape				,		(self.batch_size, self.seq_length)    )
												self.parent.assertEqual(result.end_logits.shape				,		(self.batch_size, self.seq_length)    )
												self.parent.assertEqual(result.relevance_logits.shape				,		(self.batch_size,)    )





							def  lowercase		(   self:     Tuple    ):

												'''simple docstring'''

												a__          =  self.prepare_config_and_inputs()
												(
												    (
												    a__
												)       ,(
												    a__
												)       ,(
												    a__
												)       ,(
												    a__
												)       ,(
												    a__
												)       ,(
												    a__
												)       ,(
												    a__
												)       ,
												)          =  config_and_inputs
												a__          =  {'''input_ids''': input_ids}
												return config, inputs_dict







@require_tf
class        _SCREAMING_SNAKE_CASE						(  __snake_case       , __snake_case       , unittest.TestCase							):





							"""simple docstring"""





							_SCREAMING_SNAKE_CASE    =(
							    (
							        TFDPRContextEncoder,
							        TFDPRQuestionEncoder,
							        TFDPRReader,
							    )
							    if is_tf_available()
							    else ()
							)
							_SCREAMING_SNAKE_CASE    ={'feature-extraction': TFDPRQuestionEncoder} if is_tf_available() else {}

							_SCREAMING_SNAKE_CASE    =False
							_SCREAMING_SNAKE_CASE    =False
							_SCREAMING_SNAKE_CASE    =False
							_SCREAMING_SNAKE_CASE    =False
							_SCREAMING_SNAKE_CASE    =False




							def  lowercase		(   self:     List[str]    ):

												'''simple docstring'''

												a__          =  TFDPRModelTester(self    )
												a__          =  ConfigTester(self				,		config_class=__A				,		hidden_size=37    )




							def  lowercase		(   self:     str    ):

												'''simple docstring'''

												self.config_tester.run_common_tests()




							def  lowercase		(   self:     Dict    ):

												'''simple docstring'''

												a__          =  self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_dpr_context_encoder(*__A    )




							def  lowercase		(   self:     Optional[int]    ):

												'''simple docstring'''

												a__          =  self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_dpr_question_encoder(*__A    )




							def  lowercase		(   self:     Optional[Any]    ):

												'''simple docstring'''

												a__          =  self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_dpr_reader(*__A    )





							@slow
							def  lowercase		(   self:     Optional[int]    ):

												'''simple docstring'''

												for model_name in TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																	a__          =  TFDPRContextEncoder.from_pretrained(__A    )
																	self.assertIsNotNone(__A    )

												for model_name in TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																	a__          =  TFDPRContextEncoder.from_pretrained(__A    )
																	self.assertIsNotNone(__A    )

												for model_name in TF_DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																	a__          =  TFDPRQuestionEncoder.from_pretrained(__A    )
																	self.assertIsNotNone(__A    )

												for model_name in TF_DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																	a__          =  TFDPRReader.from_pretrained(__A    )
																	self.assertIsNotNone(__A    )







@require_tf
class        _SCREAMING_SNAKE_CASE						(  unittest.TestCase							):





							"""simple docstring"""





							@slow
							def  lowercase		(   self:     str    ):

												'''simple docstring'''

												a__          =  TFDPRQuestionEncoder.from_pretrained('''facebook/dpr-question_encoder-single-nq-base'''    )

												a__          =  tf.constant(
												    [[101, 7592, 1010, 2003, 2026, 3899, 10140, 1029, 102]]    )  # [CLS] hello, is my dog cute? [SEP]
												a__          =  model(__A    )[0]  # embedding shape = (1, 768)
												# compare the actual values for a slice.
												a__          =  tf.constant(
												    [
												        [
												            0.0_3_2_3_6_2_5_3,
												            0.1_2_7_5_3_3_3_5,
												            0.1_6_8_1_8_5_0_9,
												            0.0_0_2_7_9_7_8_6,
												            0.3_8_9_6_9_3_3,
												            0.2_4_2_6_4_9_4_5,
												            0.2_1_7_8_9_7_1,
												            -0.0_2_3_3_5_2_2_7,
												            -0.0_8_4_8_1_9_5_9,
												            -0.1_4_3_2_4_1_1_7,
												        ]
												    ]    )
												self.assertTrue(numpy.allclose(output[:, :10].numpy()				,		expected_slice.numpy()				,		atol=1e-4    )    )










"""simple docstring"""







import copy
import os
from typing import TYPE_CHECKING, List, Union


if TYPE_CHECKING:
	pass

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


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

__a       :	Tuple             = {
    'kakaobrain/align-base': 'https://huggingface.co/kakaobrain/align-base/resolve/main/config.json',
}







class        _SCREAMING_SNAKE_CASE						(  __snake_case							):





							"""simple docstring"""





							_SCREAMING_SNAKE_CASE    ='align_text_model'




							def __init__(   self:     Tuple				,		__A:     Optional[int]=30522				,		__A:     Tuple=768				,		__A:     Any=12				,		__A:     Any=12				,		__A:     Dict=3072				,		__A:     Tuple="gelu"				,		__A:     Union[str, Any]=0.1				,		__A:     List[str]=0.1				,		__A:     Optional[int]=512				,		__A:     Tuple=2				,		__A:     str=0.0_2				,		__A:     int=1e-12				,		__A:     Optional[int]=0				,		__A:     Optional[int]="absolute"				,		__A:     List[Any]=True				,		**__A:     Tuple				,		):

												'''simple docstring'''

												super().__init__(**__A    )

												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__          =  layer_norm_eps
												a__          =  position_embedding_type
												a__          =  use_cache
												a__          =  pad_token_id





							@classmethod
							def  lowercase		(   cls:     Any				,		__A:     Union[str, os.PathLike]				,		**__A:     Tuple    ):

												'''simple docstring'''

												cls._set_token_in_kwargs(__A    )

												a__       ,a__          =  cls.get_config_dict(__A				,		**__A    )

												# get the text config dict if we are loading from AlignConfig
												if config_dict.get('''model_type'''    ) == "align":
																	a__          =  config_dict['''text_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(__A				,		**__A    )







class        _SCREAMING_SNAKE_CASE						(  __snake_case							):





							"""simple docstring"""





							_SCREAMING_SNAKE_CASE    ='align_vision_model'




							def __init__(   self:     Dict				,		__A:     int = 3				,		__A:     int = 600				,		__A:     float = 2.0				,		__A:     float = 3.1				,		__A:     int = 8				,		__A:     List[int] = [3, 3, 5, 3, 5, 5, 3]				,		__A:     List[int] = [32, 16, 24, 40, 80, 112, 192]				,		__A:     List[int] = [16, 24, 40, 80, 112, 192, 320]				,		__A:     List[int] = []				,		__A:     List[int] = [1, 2, 2, 2, 1, 2, 1]				,		__A:     List[int] = [1, 2, 2, 3, 3, 4, 1]				,		__A:     List[int] = [1, 6, 6, 6, 6, 6, 6]				,		__A:     float = 0.2_5				,		__A:     str = "swish"				,		__A:     int = 2560				,		__A:     str = "mean"				,		__A:     float = 0.0_2				,		__A:     float = 0.0_0_1				,		__A:     float = 0.9_9				,		__A:     float = 0.2				,		**__A:     List[Any]				,		):

												'''simple docstring'''

												super().__init__(**__A    )

												a__          =  num_channels
												a__          =  image_size
												a__          =  width_coefficient
												a__          =  depth_coefficient
												a__          =  depth_divisor
												a__          =  kernel_sizes
												a__          =  in_channels
												a__          =  out_channels
												a__          =  depthwise_padding
												a__          =  strides
												a__          =  num_block_repeats
												a__          =  expand_ratios
												a__          =  squeeze_expansion_ratio
												a__          =  hidden_act
												a__          =  hidden_dim
												a__          =  pooling_type
												a__          =  initializer_range
												a__          =  batch_norm_eps
												a__          =  batch_norm_momentum
												a__          =  drop_connect_rate
												a__          =  sum(__A    ) * 4





							@classmethod
							def  lowercase		(   cls:     Dict				,		__A:     Union[str, os.PathLike]				,		**__A:     List[Any]    ):

												'''simple docstring'''

												cls._set_token_in_kwargs(__A    )

												a__       ,a__          =  cls.get_config_dict(__A				,		**__A    )

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







class        _SCREAMING_SNAKE_CASE						(  __snake_case							):





							"""simple docstring"""





							_SCREAMING_SNAKE_CASE    ='align'
							_SCREAMING_SNAKE_CASE    =True




							def __init__(   self:     Optional[int]				,		__A:     Optional[int]=None				,		__A:     Dict=None				,		__A:     List[str]=640				,		__A:     Optional[int]=1.0				,		__A:     str=0.0_2				,		**__A:     List[str]				,		):

												'''simple docstring'''

												super().__init__(**__A    )

												if text_config is None:
																	a__          =  {}
																	logger.info('''text_config is None. Initializing the AlignTextConfig with default values.'''    )

												if vision_config is None:
																	a__          =  {}
																	logger.info('''vision_config is None. Initializing the AlignVisionConfig with default values.'''    )

												a__          =  AlignTextConfig(**__A    )
												a__          =  AlignVisionConfig(**__A    )

												a__          =  projection_dim
												a__          =  temperature_init_value
												a__          =  initializer_range




							@classmethod
							def  lowercase		(   cls:     Dict				,		__A:     AlignTextConfig				,		__A:     AlignVisionConfig				,		**__A:     str    ):

												'''simple docstring'''

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





							def  lowercase		(   self:     Any    ):

												'''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


