





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


     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						,     ):

          '''simple docstring'''






          __A						:			Tuple								=	parent
          __A						:			Optional[Any]								=	batch_size
          __A						:			Optional[Any]								=	seq_length
          __A						:			Optional[Any]								=	is_training
          __A						:			List[str]								=	use_input_mask
          __A						:			Tuple								=	use_token_type_ids
          __A						:			Dict								=	use_labels
          __A						:			str								=	vocab_size
          __A						:			Optional[int]								=	hidden_size
          __A						:			int								=	num_hidden_layers
          __A						:			List[Any]								=	num_attention_heads
          __A						:			Any								=	intermediate_size
          __A						:			int								=	hidden_act
          __A						:			str								=	hidden_dropout_prob
          __A						:			Dict								=	attention_probs_dropout_prob
          __A						:			Optional[int]								=	max_position_embeddings
          __A						:			List[Any]								=	type_vocab_size
          __A						:			Dict								=	type_sequence_label_size
          __A						:			int								=	initializer_range
          __A						:			int								=	num_labels
          __A						:			Optional[int]								=	num_choices
          __A						:			int								=	scope
          __A						:			int								=	projection_dim


     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






          __A						:			List[Any]								=	ids_tensor([self.batch_size, self.seq_length]						,     self.vocab_size)

          __A						:			Dict								=	None
          if self.use_input_mask:
               # follow test_modeling_tf_ctrl.py
               __A						:			List[Any]								=	random_attention_mask([self.batch_size, self.seq_length])

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

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

          __A						:			List[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=_UpperCAmelCase						,     initializer_range=self.initializer_range						,     )
          __A						:			str								=	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 							SCREAMING_SNAKE_CASE  (						self						,     _UpperCAmelCase						,     _UpperCAmelCase						,     _UpperCAmelCase						,     _UpperCAmelCase						,     _UpperCAmelCase						,     _UpperCAmelCase						,     _UpperCAmelCase):

          '''simple docstring'''






          __A						:			Union[str, Any]								=	TFDPRContextEncoder(config=_UpperCAmelCase)
          __A						:			Dict								=	model(_UpperCAmelCase						,     attention_mask=_UpperCAmelCase						,     token_type_ids=_UpperCAmelCase)
          __A						:			Tuple								=	model(_UpperCAmelCase						,     token_type_ids=_UpperCAmelCase)
          __A						:			Dict								=	model(_UpperCAmelCase)
          self.parent.assertEqual(result.pooler_output.shape						,     (self.batch_size, self.projection_dim or self.hidden_size))


     def 							SCREAMING_SNAKE_CASE  (						self						,     _UpperCAmelCase						,     _UpperCAmelCase						,     _UpperCAmelCase						,     _UpperCAmelCase						,     _UpperCAmelCase						,     _UpperCAmelCase						,     _UpperCAmelCase):

          '''simple docstring'''






          __A						:			Any								=	TFDPRQuestionEncoder(config=_UpperCAmelCase)
          __A						:			List[Any]								=	model(_UpperCAmelCase						,     attention_mask=_UpperCAmelCase						,     token_type_ids=_UpperCAmelCase)
          __A						:			Optional[int]								=	model(_UpperCAmelCase						,     token_type_ids=_UpperCAmelCase)
          __A						:			int								=	model(_UpperCAmelCase)
          self.parent.assertEqual(result.pooler_output.shape						,     (self.batch_size, self.projection_dim or self.hidden_size))


     def 							SCREAMING_SNAKE_CASE  (						self						,     _UpperCAmelCase						,     _UpperCAmelCase						,     _UpperCAmelCase						,     _UpperCAmelCase						,     _UpperCAmelCase						,     _UpperCAmelCase						,     _UpperCAmelCase):

          '''simple docstring'''






          __A						:			Dict								=	TFDPRReader(config=_UpperCAmelCase)
          __A						:			Tuple								=	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 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






          __A						:			Union[str, Any]								=	self.prepare_config_and_inputs()
          (
              (
              __A
          )			,(
              __A
          )			,(
              __A
          )			,(
              __A
          )			,(
              __A
          )			,(
              __A
          )			,(
              __A
          )			,
          )						:			int								=	config_and_inputs
          __A						:			Dict								=	{'input_ids': input_ids}
          return config, inputs_dict




@require_tf
class 			SCREAMING_SNAKE_CASE   (a__ ,		a__ ,		unittest.TestCase							):
     lowerCAmelCase		      =	(
         (
             TFDPRContextEncoder,
             TFDPRQuestionEncoder,
             TFDPRReader,
         )
         if is_tf_available()
         else ()
     )
     lowerCAmelCase		      =	{'''feature-extraction''': TFDPRQuestionEncoder} if is_tf_available() else {}

     lowerCAmelCase		      =	False
     lowerCAmelCase		      =	False
     lowerCAmelCase		      =	False
     lowerCAmelCase		      =	False
     lowerCAmelCase		      =	False


     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






          __A						:			Optional[int]								=	TFDPRModelTester(self)
          __A						:			List[Any]								=	ConfigTester(self						,     config_class=_UpperCAmelCase						,     hidden_size=37)


     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






          self.config_tester.run_common_tests()


     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






          __A						:			int								=	self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_dpr_context_encoder(*_UpperCAmelCase)


     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






          __A						:			Optional[int]								=	self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_dpr_question_encoder(*_UpperCAmelCase)


     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






          __A						:			str								=	self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_dpr_reader(*_UpperCAmelCase)


     @slow
     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






          for model_name in TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
               __A						:			Union[str, Any]								=	TFDPRContextEncoder.from_pretrained(_UpperCAmelCase)
               self.assertIsNotNone(_UpperCAmelCase)

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

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

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




@require_tf
class 			SCREAMING_SNAKE_CASE   (unittest.TestCase							):


     @slow
     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






          __A						:			Any								=	TFDPRQuestionEncoder.from_pretrained('facebook/dpr-question_encoder-single-nq-base')

          __A						:			Union[str, Any]								=	tf.constant(
              [[101, 7592, 1010, 2003, 2026, 3899, 1_0140, 1029, 102]])  # [CLS] hello, is my dog cute? [SEP]
          __A						:			List[Any]								=	model(_UpperCAmelCase)[0]  # embedding shape = (1, 768)
          # compare the actual values for a slice.
          __A						:			Any								=	tf.constant(
              [
                  [
                      0.03236253,
                      0.12753335,
                      0.16818509,
                      0.00279786,
                      0.3896933,
                      0.24264945,
                      0.2178971,
                      -0.02335227,
                      -0.08481959,
                      -0.14324117,
                  ]
              ])
          self.assertTrue(numpy.allclose(output[:, :10].numpy()						,     expected_slice.numpy()						,     atol=1e-4))






'''simple docstring'''




import argparse


lowercase__   :				Any      =   '''docs/source/_static/js/custom.js'''






def 	_lowerCAmelCase      (  __snake_case  :			Union[str, Any]      )					->					str:
     with open(__snake_case			,							encoding='utf-8'			,							newline='\n'      ) as f:
          __A						:			Optional[Any]								=	f.readlines()
     __A						:			List[str]								=	0

     # First let's put the right version
     while not lines[index].startswith('const stableVersion ='      ):
          index += 1
     __A						:			Tuple								=	f'const stableVersion = "v{version}"\n'

     # Then update the dictionary
     while not lines[index].startswith('const versionMapping = {'      ):
          index += 1

     # We go until the end
     while not lines[index].startswith('}'      ):
          index += 1
     # We add the new version at the end
     lines[index - 1] += f'    "v{version}": "v{version}",\n'

     with open(__snake_case			,							'w'			,							encoding='utf-8'			,							newline='\n'      ) as f:
          f.writelines(__snake_case      )


if __name__ == "__main__":
  lowercase__   :				Optional[Any]      =   argparse.ArgumentParser()
  parser.add_argument('''--version''', help='''Release version.''')
  lowercase__   :				List[str]      =   parser.parse_args()
  update_custom_js(args.version)