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   snake_case_       :






   def __init__(      self				:     List[str]	,						lowercase_				:     Optional[Any]	,						lowercase_				:     Dict=13	,						lowercase_				:     str=7	,						lowercase_				:     Union[str, Any]=True	,						lowercase_				:     List[Any]=True	,						lowercase_				:     Optional[int]=True	,						lowercase_				:     List[str]=True	,						lowercase_				:     Optional[Any]=99	,						lowercase_				:     Tuple=32	,						lowercase_				:     Dict=2	,						lowercase_				:     Dict=4	,						lowercase_				:     Optional[Any]=37	,						lowercase_				:     Union[str, Any]="gelu"	,						lowercase_				:     List[Any]=0.1	,						lowercase_				:     Tuple=0.1	,						lowercase_				:     int=5_12	,						lowercase_				:     str=16	,						lowercase_				:     Optional[Any]=2	,						lowercase_				:     List[Any]=0.02	,						lowercase_				:     int=3	,						lowercase_				:     Optional[Any]=4	,						lowercase_				:     str=None	,						lowercase_				:     Dict=0	,						)					->						List[str]:
       lowercase__ :   Optional[Any]    					=				parent
       lowercase__ :   str    					=				batch_size
       lowercase__ :   int    					=				seq_length
       lowercase__ :   List[Any]    					=				is_training
       lowercase__ :   Any    					=				use_input_mask
       lowercase__ :   int    					=				use_token_type_ids
       lowercase__ :   List[str]    					=				use_labels
       lowercase__ :   Optional[int]    					=				vocab_size
       lowercase__ :   Union[str, Any]    					=				hidden_size
       lowercase__ :   int    					=				num_hidden_layers
       lowercase__ :   Tuple    					=				num_attention_heads
       lowercase__ :   int    					=				intermediate_size
       lowercase__ :   List[str]    					=				hidden_act
       lowercase__ :   Any    					=				hidden_dropout_prob
       lowercase__ :   Any    					=				attention_probs_dropout_prob
       lowercase__ :   List[Any]    					=				max_position_embeddings
       lowercase__ :   List[str]    					=				type_vocab_size
       lowercase__ :   int    					=				type_sequence_label_size
       lowercase__ :   Dict    					=				initializer_range
       lowercase__ :   Dict    					=				num_labels
       lowercase__ :   Tuple    					=				num_choices
       lowercase__ :   List[Any]    					=				scope
       lowercase__ :   List[Any]    					=				projection_dim






   def       __UpperCamelCase     (      self				:     List[Any]  )					->						List[Any]:
       lowercase__ :   Optional[int]    					=				ids_tensor([self.batch_size, self.seq_length]	,						self.vocab_size  )

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

       lowercase__ :   Tuple    					=				None
       if self.use_token_type_ids:
           lowercase__ :   Union[str, Any]    					=				ids_tensor([self.batch_size, self.seq_length]	,						self.type_vocab_size  )

       lowercase__ :   Any    					=				None
       lowercase__ :   Optional[int]    					=				None
       lowercase__ :   Tuple    					=				None
       if self.use_labels:
           lowercase__ :   List[str]    					=				ids_tensor([self.batch_size]	,						self.type_sequence_label_size  )
           lowercase__ :   Union[str, Any]    					=				ids_tensor([self.batch_size, self.seq_length]	,						self.num_labels  )
           lowercase__ :   str    					=				ids_tensor([self.batch_size]	,						self.num_choices  )

       lowercase__ :   Union[str, Any]    					=				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=lowerCAmelCase_	,						initializer_range=self.initializer_range	,						)
       lowercase__ :   Optional[int]    					=				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       __UpperCamelCase     (      self				:     Dict	,						lowercase_				:     Tuple	,						lowercase_				:     List[Any]	,						lowercase_				:     Tuple	,						lowercase_				:     Tuple	,						lowercase_				:     Optional[int]	,						lowercase_				:     Tuple	,						lowercase_				:     str  )					->						List[str]:
       lowercase__ :   List[Any]    					=				TFDPRContextEncoder(config=lowerCAmelCase_  )
       lowercase__ :   Any    					=				model(lowerCAmelCase_	,						attention_mask=lowerCAmelCase_	,						token_type_ids=lowerCAmelCase_  )
       lowercase__ :   int    					=				model(lowerCAmelCase_	,						token_type_ids=lowerCAmelCase_  )
       lowercase__ :   Tuple    					=				model(lowerCAmelCase_  )
       self.parent.assertEqual(result.pooler_output.shape	,						(self.batch_size, self.projection_dim or self.hidden_size)  )






   def       __UpperCamelCase     (      self				:     Optional[Any]	,						lowercase_				:     Optional[Any]	,						lowercase_				:     str	,						lowercase_				:     Any	,						lowercase_				:     Optional[Any]	,						lowercase_				:     str	,						lowercase_				:     Optional[Any]	,						lowercase_				:     Optional[int]  )					->						Optional[Any]:
       lowercase__ :   Optional[int]    					=				TFDPRQuestionEncoder(config=lowerCAmelCase_  )
       lowercase__ :   Optional[Any]    					=				model(lowerCAmelCase_	,						attention_mask=lowerCAmelCase_	,						token_type_ids=lowerCAmelCase_  )
       lowercase__ :   List[Any]    					=				model(lowerCAmelCase_	,						token_type_ids=lowerCAmelCase_  )
       lowercase__ :   Optional[Any]    					=				model(lowerCAmelCase_  )
       self.parent.assertEqual(result.pooler_output.shape	,						(self.batch_size, self.projection_dim or self.hidden_size)  )






   def       __UpperCamelCase     (      self				:     List[Any]	,						lowercase_				:     Optional[int]	,						lowercase_				:     str	,						lowercase_				:     Any	,						lowercase_				:     Tuple	,						lowercase_				:     Dict	,						lowercase_				:     Dict	,						lowercase_				:     str  )					->						List[str]:
       lowercase__ :   Optional[int]    					=				TFDPRReader(config=lowerCAmelCase_  )
       lowercase__ :   Optional[Any]    					=				model(lowerCAmelCase_	,						attention_mask=lowerCAmelCase_  )

       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       __UpperCamelCase     (      self				:     Optional[int]  )					->						List[Any]:
       lowercase__ :   Optional[Any]    					=				self.prepare_config_and_inputs()
       (
           (
           lowercase__ 
       )					,   (
           lowercase__ 
       )					,   (
           lowercase__ 
       )					,   (
           lowercase__ 
       )					,   (
           lowercase__ 
       )					,   (
           lowercase__ 
       )					,   (
           lowercase__ 
       )					,    
       ) :   Optional[Any]    					=				config_and_inputs
       lowercase__ :   Union[str, Any]    					=				{"input_ids": input_ids}
       return config, inputs_dict



@require_tf
class   snake_case_       (		_lowerCamelCase  ,_lowerCamelCase  ,unittest.TestCase       ):
   __A	:		Dict         =				(
       (
           TFDPRContextEncoder,
           TFDPRQuestionEncoder,
           TFDPRReader,
       )
       if is_tf_available()
       else ()
   )
   __A	:		Tuple         =				{"feature-extraction": TFDPRQuestionEncoder} if is_tf_available() else {}

   __A	:		Union[str, Any]         =				False
   __A	:		List[Any]         =				False
   __A	:		List[str]         =				False
   __A	:		Optional[int]         =				False
   __A	:		List[Any]         =				False






   def       __UpperCamelCase     (      self				:     Tuple  )					->						Union[str, Any]:
       lowercase__ :   int    					=				TFDPRModelTester(self  )
       lowercase__ :   str    					=				ConfigTester(self	,						config_class=lowerCAmelCase_	,						hidden_size=37  )






   def       __UpperCamelCase     (      self				:     Optional[Any]  )					->						Dict:
       self.config_tester.run_common_tests()






   def       __UpperCamelCase     (      self				:     Optional[Any]  )					->						Optional[Any]:
       lowercase__ :   Union[str, Any]    					=				self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_dpr_context_encoder(*lowerCAmelCase_  )






   def       __UpperCamelCase     (      self				:     Union[str, Any]  )					->						Any:
       lowercase__ :   List[str]    					=				self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_dpr_question_encoder(*lowerCAmelCase_  )






   def       __UpperCamelCase     (      self				:     Optional[int]  )					->						Optional[Any]:
       lowercase__ :   Union[str, Any]    					=				self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_dpr_reader(*lowerCAmelCase_  )







   @slow
   def       __UpperCamelCase     (      self				:     Dict  )					->						Any:






       for model_name in TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
           lowercase__ :   str    					=				TFDPRContextEncoder.from_pretrained(lowerCAmelCase_  )
           self.assertIsNotNone(lowerCAmelCase_  )

       for model_name in TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
           lowercase__ :   List[str]    					=				TFDPRContextEncoder.from_pretrained(lowerCAmelCase_  )
           self.assertIsNotNone(lowerCAmelCase_  )

       for model_name in TF_DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
           lowercase__ :   List[str]    					=				TFDPRQuestionEncoder.from_pretrained(lowerCAmelCase_  )
           self.assertIsNotNone(lowerCAmelCase_  )

       for model_name in TF_DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
           lowercase__ :   str    					=				TFDPRReader.from_pretrained(lowerCAmelCase_  )
           self.assertIsNotNone(lowerCAmelCase_  )



@require_tf
class   snake_case_       (		unittest.TestCase       ):






   @slow
   def       __UpperCamelCase     (      self				:     int  )					->						Any:
       lowercase__ :   List[Any]    					=				TFDPRQuestionEncoder.from_pretrained("facebook/dpr-question_encoder-single-nq-base"  )

       lowercase__ :   Any    					=				tf.constant(
           [[1_01, 75_92, 10_10, 20_03, 20_26, 38_99, 1_01_40, 10_29, 1_02]]  )  # [CLS] hello, is my dog cute? [SEP]
       lowercase__ :   List[Any]    					=				model(lowerCAmelCase_  )[0]  # embedding shape = (1, 768)
       # compare the actual values for a slice.
       lowercase__ :   str    					=				tf.constant(
           [
               [
                   0.03_23_62_53,
                   0.12_75_33_35,
                   0.16_81_85_09,
                   0.00_27_97_86,
                   0.3_89_69_33,
                   0.24_26_49_45,
                   0.2_17_89_71,
                   -0.02_33_52_27,
                   -0.08_48_19_59,
                   -0.14_32_41_17,
               ]
           ]  )
       self.assertTrue(numpy.allclose(output[:, :10].numpy()	,						expected_slice.numpy()	,						atol=1E-4  )  )





'''simple docstring'''







from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available


lowercase    :							Tuple      =					{
    "configuration_xlm": ["XLM_PRETRAINED_CONFIG_ARCHIVE_MAP", "XLMConfig", "XLMOnnxConfig"],
    "tokenization_xlm": ["XLMTokenizer"],
}

try:
					if not is_torch_available():
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					pass
else:
					lowercase    :							int      =					[
					    "XLM_PRETRAINED_MODEL_ARCHIVE_LIST",
					    "XLMForMultipleChoice",
					    "XLMForQuestionAnswering",
					    "XLMForQuestionAnsweringSimple",
					    "XLMForSequenceClassification",
					    "XLMForTokenClassification",
					    "XLMModel",
					    "XLMPreTrainedModel",
					    "XLMWithLMHeadModel",
					]

try:
					if not is_tf_available():
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					pass
else:
					lowercase    :							Tuple      =					[
					    "TF_XLM_PRETRAINED_MODEL_ARCHIVE_LIST",
					    "TFXLMForMultipleChoice",
					    "TFXLMForQuestionAnsweringSimple",
					    "TFXLMForSequenceClassification",
					    "TFXLMForTokenClassification",
					    "TFXLMMainLayer",
					    "TFXLMModel",
					    "TFXLMPreTrainedModel",
					    "TFXLMWithLMHeadModel",
					]


if TYPE_CHECKING:
					from .configuration_xlm import XLM_PRETRAINED_CONFIG_ARCHIVE_MAP, XLMConfig, XLMOnnxConfig
					from .tokenization_xlm import XLMTokenizer

					try:
										if not is_torch_available():
															raise OptionalDependencyNotAvailable()
					except OptionalDependencyNotAvailable:
										pass
					else:
										from .modeling_xlm import (
										    XLM_PRETRAINED_MODEL_ARCHIVE_LIST,
										    XLMForMultipleChoice,
										    XLMForQuestionAnswering,
										    XLMForQuestionAnsweringSimple,
										    XLMForSequenceClassification,
										    XLMForTokenClassification,
										    XLMModel,
										    XLMPreTrainedModel,
										    XLMWithLMHeadModel,
										)

					try:
										if not is_tf_available():
															raise OptionalDependencyNotAvailable()
					except OptionalDependencyNotAvailable:
										pass
					else:
										from .modeling_tf_xlm import (
										    TF_XLM_PRETRAINED_MODEL_ARCHIVE_LIST,
										    TFXLMForMultipleChoice,
										    TFXLMForQuestionAnsweringSimple,
										    TFXLMForSequenceClassification,
										    TFXLMForTokenClassification,
										    TFXLMMainLayer,
										    TFXLMModel,
										    TFXLMPreTrainedModel,
										    TFXLMWithLMHeadModel,
										)

else:
					import sys

					lowercase    :							Union[str, Any]      =					_LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)

