




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







   def __init__(						self   ,				UpperCamelCase   ,				UpperCamelCase=13   ,				UpperCamelCase=7   ,				UpperCamelCase=True   ,				UpperCamelCase=True   ,				UpperCamelCase=True   ,				UpperCamelCase=True   ,				UpperCamelCase=99   ,				UpperCamelCase=32   ,				UpperCamelCase=2   ,				UpperCamelCase=4   ,				UpperCamelCase=37   ,				UpperCamelCase="gelu"   ,				UpperCamelCase=0.1   ,				UpperCamelCase=0.1   ,				UpperCamelCase=512   ,				UpperCamelCase=16   ,				UpperCamelCase=2   ,				UpperCamelCase=0.02   ,				UpperCamelCase=3   ,				UpperCamelCase=4   ,				UpperCamelCase=None   ,				UpperCamelCase=0   ,				)					->     List[str]:
     __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 		UpperCamelCase__      (						self   )					->     int:
     __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=UpperCamelCase   ,				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 		UpperCamelCase__      (						self   ,				UpperCamelCase   ,				UpperCamelCase   ,				UpperCamelCase   ,				UpperCamelCase   ,				UpperCamelCase   ,				UpperCamelCase   ,				UpperCamelCase   )					->     List[str]:
     __a								=       TFDPRContextEncoder(config=UpperCamelCase   )
     __a								=       model(UpperCamelCase   ,				attention_mask=UpperCamelCase   ,				token_type_ids=UpperCamelCase   )
     __a								=       model(UpperCamelCase   ,				token_type_ids=UpperCamelCase   )
     __a								=       model(UpperCamelCase   )
     self.parent.assertEqual(result.pooler_output.shape   ,				(self.batch_size, self.projection_dim or self.hidden_size)   )







   def 		UpperCamelCase__      (						self   ,				UpperCamelCase   ,				UpperCamelCase   ,				UpperCamelCase   ,				UpperCamelCase   ,				UpperCamelCase   ,				UpperCamelCase   ,				UpperCamelCase   )					->     Optional[int]:
     __a								=       TFDPRQuestionEncoder(config=UpperCamelCase   )
     __a								=       model(UpperCamelCase   ,				attention_mask=UpperCamelCase   ,				token_type_ids=UpperCamelCase   )
     __a								=       model(UpperCamelCase   ,				token_type_ids=UpperCamelCase   )
     __a								=       model(UpperCamelCase   )
     self.parent.assertEqual(result.pooler_output.shape   ,				(self.batch_size, self.projection_dim or self.hidden_size)   )







   def 		UpperCamelCase__      (						self   ,				UpperCamelCase   ,				UpperCamelCase   ,				UpperCamelCase   ,				UpperCamelCase   ,				UpperCamelCase   ,				UpperCamelCase   ,				UpperCamelCase   )					->     Tuple:
     __a								=       TFDPRReader(config=UpperCamelCase   )
     __a								=       model(UpperCamelCase   ,				attention_mask=UpperCamelCase   )

     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   )					->     Tuple:
     __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  __lowercase    (	__magic_name__							,				__magic_name__							,				unittest.TestCase			):
   _a    						=   (
       (
           TFDPRContextEncoder,
           TFDPRQuestionEncoder,
           TFDPRReader,
       )
       if is_tf_available()
       else ()
   )
   _a    						=   {"""feature-extraction""": TFDPRQuestionEncoder} if is_tf_available() else {}

   _a    						=   False
   _a    						=   False
   _a    						=   False
   _a    						=   False
   _a    						=   False







   def 		UpperCamelCase__      (						self   )					->     Optional[Any]:
     __a								=       TFDPRModelTester(self   )
     __a								=       ConfigTester(self   ,				config_class=UpperCamelCase   ,				hidden_size=37   )







   def 		UpperCamelCase__      (						self   )					->     List[Any]:
     self.config_tester.run_common_tests()







   def 		UpperCamelCase__      (						self   )					->     int:
     __a								=       self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_dpr_context_encoder(*UpperCamelCase   )







   def 		UpperCamelCase__      (						self   )					->     Optional[int]:
     __a								=       self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_dpr_question_encoder(*UpperCamelCase   )







   def 		UpperCamelCase__      (						self   )					->     Tuple:
     __a								=       self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_dpr_reader(*UpperCamelCase   )







   @slow
   def 		UpperCamelCase__      (						self   )					->     Optional[int]:
     for model_name in TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
       __a								=       TFDPRContextEncoder.from_pretrained(UpperCamelCase   )
       self.assertIsNotNone(UpperCamelCase   )

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

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

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





@require_tf
class  __lowercase    (	unittest.TestCase			):







   @slow
   def 		UpperCamelCase__      (						self   )					->     List[Any]:
     __a								=       TFDPRQuestionEncoder.from_pretrained('facebook/dpr-question_encoder-single-nq-base'   )

     __a								=       tf.constant(
         [[101, 7592, 1010, 2003, 2026, 3899, 1_0140, 1029, 102]]   )  # [CLS] hello, is my dog cute? [SEP]
     __a								=       model(UpperCamelCase   )[0]  # embedding shape = (1, 768)
     # compare the actual values for a slice.
     __a								=       tf.constant(
         [
             [
                 0.03_236_253,
                 0.12_753_335,
                 0.16_818_509,
                 0.00_279_786,
                 0.3_896_933,
                 0.24_264_945,
                 0.2_178_971,
                 -0.02_335_227,
                 -0.08_481_959,
                 -0.14_324_117,
             ]
         ]   )
     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_sentencepiece_available,
    is_tokenizers_available,
    is_torch_available,
)


UpperCAmelCase_					=   {"configuration_fnet": ["FNET_PRETRAINED_CONFIG_ARCHIVE_MAP", "FNetConfig"]}

try:
  if not is_sentencepiece_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  UpperCAmelCase_					=   ["FNetTokenizer"]

try:
  if not is_tokenizers_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  UpperCAmelCase_					=   ["FNetTokenizerFast"]

try:
  if not is_torch_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  UpperCAmelCase_					=   [
      "FNET_PRETRAINED_MODEL_ARCHIVE_LIST",
      "FNetForMaskedLM",
      "FNetForMultipleChoice",
      "FNetForNextSentencePrediction",
      "FNetForPreTraining",
      "FNetForQuestionAnswering",
      "FNetForSequenceClassification",
      "FNetForTokenClassification",
      "FNetLayer",
      "FNetModel",
      "FNetPreTrainedModel",
  ]


if TYPE_CHECKING:
  from .configuration_fnet import FNET_PRETRAINED_CONFIG_ARCHIVE_MAP, FNetConfig

  try:
    if not is_sentencepiece_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .tokenization_fnet import FNetTokenizer

  try:
    if not is_tokenizers_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .tokenization_fnet_fast import FNetTokenizerFast

  try:
    if not is_torch_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_fnet import (
        FNET_PRETRAINED_MODEL_ARCHIVE_LIST,
        FNetForMaskedLM,
        FNetForMultipleChoice,
        FNetForNextSentencePrediction,
        FNetForPreTraining,
        FNetForQuestionAnswering,
        FNetForSequenceClassification,
        FNetForTokenClassification,
        FNetLayer,
        FNetModel,
        FNetPreTrainedModel,
    )


else:
  import sys

  UpperCAmelCase_					=   _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)



