


'''simple docstring'''






import os
import re
import shutil
import sys
import tempfile
import unittest

import black


lowercase				=os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(__file__))))
sys.path.append(os.path.join(git_repo_path, 'utils'))

import check_copies  # noqa: E402


# This is the reference code that will be used in the tests.
# If BertLMPredictionHead is changed in modeling_bert.py, this code needs to be manually updated.
lowercase				="    def __init__(self, config):\n        super().__init__()\n        self.transform = BertPredictionHeadTransform(config)\n\n        # The output weights are the same as the input embeddings, but there is\n        # an output-only bias for each token.\n        self.decoder = nn.Linear(config.hidden_size, config.vocab_size, bias=False)\n\n        self.bias = nn.Parameter(torch.zeros(config.vocab_size))\n\n        # Need a link between the two variables so that the bias is correctly resized with `resize_token_embeddings`\n        self.decoder.bias = self.bias\n\n    def forward(self, hidden_states):\n        hidden_states = self.transform(hidden_states)\n        hidden_states = self.decoder(hidden_states)\n        return hidden_states\n"






class    __magic_name__				(						unittest.TestCase							):






       def      lowerCAmelCase						(     self)		->    Union[str, Any]:




          '''simple docstring'''
          _UpperCAmelCase  :  Union[str, Any]				       =tempfile.mkdtemp()
          os.makedirs(os.path.join(self.transformer_dir    ,				'models/bert/'))
          _UpperCAmelCase  :  List[str]				       =self.transformer_dir
          shutil.copy(
              os.path.join(snake_case__    ,				'src/transformers/models/bert/modeling_bert.py')    ,				os.path.join(self.transformer_dir    ,				'models/bert/modeling_bert.py')    ,				)






       def      lowerCAmelCase						(     self)		->    Tuple:




          '''simple docstring'''
          _UpperCAmelCase  :  List[Any]				       ='src/transformers'
          shutil.rmtree(self.transformer_dir)






       def      lowerCAmelCase						(     self    ,				snake_case    ,				snake_case    ,				snake_case    ,				snake_case=None)		->    Dict:




          '''simple docstring'''
          _UpperCAmelCase  :  Optional[Any]				       =comment + f"\nclass {class_name}(nn.Module):\n" + class_code
          if overwrite_result is not None:
             _UpperCAmelCase  :  Optional[int]				       =comment + f"\nclass {class_name}(nn.Module):\n" + overwrite_result
          _UpperCAmelCase  :  Tuple				       =black.Mode(target_versions={black.TargetVersion.PYaa}    ,				line_length=1_1_9)
          _UpperCAmelCase  :  Optional[Any]				       =black.format_str(snake_case__    ,				mode=snake_case__)
          _UpperCAmelCase  :  int				       =os.path.join(self.transformer_dir    ,				'new_code.py')
          with open(snake_case__    ,				'w'    ,				newline='\n') as f:
             f.write(snake_case__)
          if overwrite_result is None:
             self.assertTrue(len(check_copies.is_copy_consistent(snake_case__)) == 0)
          else:
             check_copies.is_copy_consistent(f.name    ,				overwrite=snake_case__)
             with open(snake_case__    ,				'r') as f:
                self.assertTrue(f.read()    ,				snake_case__)






       def      lowerCAmelCase						(     self)		->    Tuple:




          '''simple docstring'''
          _UpperCAmelCase  :  Dict				       =check_copies.find_code_in_transformers('models.bert.modeling_bert.BertLMPredictionHead')
          self.assertEqual(snake_case__    ,				snake_case__)






       def      lowerCAmelCase						(     self)		->    List[Any]:




          '''simple docstring'''
          # Base copy consistency
          self.check_copy_consistency(
              '# Copied from transformers.models.bert.modeling_bert.BertLMPredictionHead'    ,				'BertLMPredictionHead'    ,				REFERENCE_CODE + '\n'    ,				)

          # With no empty line at the end
          self.check_copy_consistency(
              '# Copied from transformers.models.bert.modeling_bert.BertLMPredictionHead'    ,				'BertLMPredictionHead'    ,				snake_case__    ,				)

          # Copy consistency with rename
          self.check_copy_consistency(
              '# Copied from transformers.models.bert.modeling_bert.BertLMPredictionHead with Bert->TestModel'    ,				'TestModelLMPredictionHead'    ,				re.sub('Bert'    ,				'TestModel'    ,				snake_case__)    ,				)

          # Copy consistency with a really long name
          _UpperCAmelCase  :  Optional[Any]				       ='TestModelWithAReallyLongNameBecauseSomePeopleLikeThatForSomeReason'
          self.check_copy_consistency(
              f"# Copied from transformers.models.bert.modeling_bert.BertLMPredictionHead with Bert->{long_class_name}"    ,				f"{long_class_name}LMPredictionHead"    ,				re.sub('Bert'    ,				snake_case__    ,				snake_case__)    ,				)

          # Copy consistency with overwrite
          self.check_copy_consistency(
              '# Copied from transformers.models.bert.modeling_bert.BertLMPredictionHead with Bert->TestModel'    ,				'TestModelLMPredictionHead'    ,				snake_case__    ,				overwrite_result=re.sub('Bert'    ,				'TestModel'    ,				snake_case__)    ,				)






       def      lowerCAmelCase						(     self)		->    Tuple:




          '''simple docstring'''
          _UpperCAmelCase  :  int				       =check_copies.LOCALIZED_READMES['README_zh-hans.md']

          _UpperCAmelCase  :  int				       =(
              '1. **[ALBERT](https://huggingface.co/transformers/model_doc/albert.html)** (from Google Research and the'
              ' Toyota Technological Institute at Chicago) released with the paper [ALBERT: A Lite BERT for'
              ' Self-supervised Learning of Language Representations](https://arxiv.org/abs/1909.11942), by Zhenzhong'
              ' Lan, Mingda Chen, Sebastian Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut.\n1.'
              ' **[DistilBERT](https://huggingface.co/transformers/model_doc/distilbert.html)** (from HuggingFace),'
              ' released together with the paper [DistilBERT, a distilled version of BERT: smaller, faster, cheaper and'
              ' lighter](https://arxiv.org/abs/1910.01108) by Victor Sanh, Lysandre Debut and Thomas Wolf. The same'
              ' method has been applied to compress GPT2 into'
              ' [DistilGPT2](https://github.com/huggingface/transformers/tree/main/examples/distillation), RoBERTa into'
              ' [DistilRoBERTa](https://github.com/huggingface/transformers/tree/main/examples/distillation),'
              ' Multilingual BERT into'
              ' [DistilmBERT](https://github.com/huggingface/transformers/tree/main/examples/distillation) and a German'
              ' version of DistilBERT.\n1. **[ELECTRA](https://huggingface.co/transformers/model_doc/electra.html)**'
              ' (from Google Research/Stanford University) released with the paper [ELECTRA: Pre-training text encoders'
              ' as discriminators rather than generators](https://arxiv.org/abs/2003.10555) by Kevin Clark, Minh-Thang'
              ' Luong, Quoc V. Le, Christopher D. Manning.'
          )
          _UpperCAmelCase  :  List[Any]				       =(
              '1. **[ALBERT](https://huggingface.co/transformers/model_doc/albert.html)** (来自 Google Research and the'
              ' Toyota Technological Institute at Chicago) 伴随论文 [ALBERT: A Lite BERT for Self-supervised Learning of'
              ' Language Representations](https://arxiv.org/abs/1909.11942), 由 Zhenzhong Lan, Mingda Chen, Sebastian'
              ' Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut 发布。\n'
          )
          _UpperCAmelCase  :  Dict				       =(
              '1. **[ALBERT](https://huggingface.co/transformers/model_doc/albert.html)** (来自 Google Research and the'
              ' Toyota Technological Institute at Chicago) 伴随论文 [ALBERT: A Lite BERT for Self-supervised Learning of'
              ' Language Representations](https://arxiv.org/abs/1909.11942), 由 Zhenzhong Lan, Mingda Chen, Sebastian'
              ' Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut 发布。\n1.'
              ' **[DistilBERT](https://huggingface.co/transformers/model_doc/distilbert.html)** (来自 HuggingFace) 伴随论文'
              ' [DistilBERT, a distilled version of BERT: smaller, faster, cheaper and'
              ' lighter](https://arxiv.org/abs/1910.01108) 由 Victor Sanh, Lysandre Debut and Thomas Wolf 发布。 The same'
              ' method has been applied to compress GPT2 into'
              ' [DistilGPT2](https://github.com/huggingface/transformers/tree/main/examples/distillation), RoBERTa into'
              ' [DistilRoBERTa](https://github.com/huggingface/transformers/tree/main/examples/distillation),'
              ' Multilingual BERT into'
              ' [DistilmBERT](https://github.com/huggingface/transformers/tree/main/examples/distillation) and a German'
              ' version of DistilBERT.\n1. **[ELECTRA](https://huggingface.co/transformers/model_doc/electra.html)** (来自'
              ' Google Research/Stanford University) 伴随论文 [ELECTRA: Pre-training text encoders as discriminators rather'
              ' than generators](https://arxiv.org/abs/2003.10555) 由 Kevin Clark, Minh-Thang Luong, Quoc V. Le,'
              ' Christopher D. Manning 发布。\n'
          )

          _UpperCAmelCase       ,			_UpperCAmelCase  :  List[Any]				       =check_copies.convert_to_localized_md(
              snake_case__    ,				snake_case__    ,				localized_readme['format_model_list'])

          self.assertFalse(snake_case__)
          self.assertEqual(snake_case__    ,				snake_case__)

          _UpperCAmelCase       ,			_UpperCAmelCase  :  Any				       =check_copies.convert_to_localized_md(
              snake_case__    ,				snake_case__    ,				localized_readme['format_model_list'])

          # Check whether the number of models is equal to README.md after conversion.
          self.assertTrue(snake_case__)

          _UpperCAmelCase  :  Optional[int]				       =(
              '1. **[ALBERT](https://huggingface.co/transformers/model_doc/albert.html)** (from Google Research and the'
              ' Toyota Technological Institute at Chicago) released with the paper [ALBERT: A Lite BERT for'
              ' Self-supervised Learning of Language Representations](https://arxiv.org/abs/1909.11942), by Zhenzhong'
              ' Lan, Mingda Chen, Sebastian Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut.'
          )
          _UpperCAmelCase  :  Optional[int]				       =(
              '1. **[ALBERT](https://huggingface.co/transformers/main/model_doc/albert.html)** (来自 Google Research and'
              ' the Toyota Technological Institute at Chicago) 伴随论文 [ALBERT: A Lite BERT for Self-supervised Learning of'
              ' Language Representations](https://arxiv.org/abs/1909.11942), 由 Zhenzhong Lan, Mingda Chen, Sebastian'
              ' Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut 发布。\n'
          )
          _UpperCAmelCase  :  Optional[int]				       =(
              '1. **[ALBERT](https://huggingface.co/transformers/model_doc/albert.html)** (来自 Google Research and the'
              ' Toyota Technological Institute at Chicago) 伴随论文 [ALBERT: A Lite BERT for Self-supervised Learning of'
              ' Language Representations](https://arxiv.org/abs/1909.11942), 由 Zhenzhong Lan, Mingda Chen, Sebastian'
              ' Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut 发布。\n'
          )

          _UpperCAmelCase       ,			_UpperCAmelCase  :  Optional[int]				       =check_copies.convert_to_localized_md(
              snake_case__    ,				snake_case__    ,				localized_readme['format_model_list'])

          # Check if the model link is synchronized.
          self.assertEqual(snake_case__    ,				snake_case__)


"""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    a_   :




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




					def     _SCREAMING_SNAKE_CASE    (						self    :  Optional[int]  ):
							lowerCAmelCase__   = ids_tensor([self.batch_size, self.seq_length]	,    self.vocab_size  )

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

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

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

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




					def     _SCREAMING_SNAKE_CASE    (						self    :  Any	,    snake_case__    :  str	,    snake_case__    :  Dict	,    snake_case__    :  str	,    snake_case__    :  Any	,    snake_case__    :  Union[str, Any]	,    snake_case__    :  List[str]	,    snake_case__    :  Dict  ):
							lowerCAmelCase__   = TFDPRQuestionEncoder(config=snake_case__  )
							lowerCAmelCase__   = model(snake_case__	,    attention_mask=snake_case__	,    token_type_ids=snake_case__  )
							lowerCAmelCase__   = model(snake_case__	,    token_type_ids=snake_case__  )
							lowerCAmelCase__   = model(snake_case__  )
							self.parent.assertEqual(result.pooler_output.shape	,    (self.batch_size, self.projection_dim or self.hidden_size)  )




					def     _SCREAMING_SNAKE_CASE    (						self    :  Tuple	,    snake_case__    :  Tuple	,    snake_case__    :  Any	,    snake_case__    :  str	,    snake_case__    :  Tuple	,    snake_case__    :  List[str]	,    snake_case__    :  List[str]	,    snake_case__    :  Tuple  ):
							lowerCAmelCase__   = TFDPRReader(config=snake_case__  )
							lowerCAmelCase__   = model(snake_case__	,    attention_mask=snake_case__  )

							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    :  Any  ):
							lowerCAmelCase__   = self.prepare_config_and_inputs()
							(
							    (
							    lowerCAmelCase__
							) ,       (
							    lowerCAmelCase__
							) ,       (
							    lowerCAmelCase__
							) ,       (
							    lowerCAmelCase__
							) ,       (
							    lowerCAmelCase__
							) ,       (
							    lowerCAmelCase__
							) ,       (
							    lowerCAmelCase__
							) ,       
							)   = config_and_inputs
							lowerCAmelCase__   = {"""input_ids""": input_ids}
							return config, inputs_dict

@require_tf
class    a_   (  __UpperCamelCase			,		__UpperCamelCase			,		unittest.TestCase      ):
					UpperCamelCase_			:						Optional[Any]      =     (
					    (
					        TFDPRContextEncoder,
					        TFDPRQuestionEncoder,
					        TFDPRReader,
					    )
					    if is_tf_available()
					    else ()
					)
					UpperCamelCase_			:						Any      =     {"feature-extraction": TFDPRQuestionEncoder} if is_tf_available() else {}

					UpperCamelCase_			:						Optional[Any]      =     False
					UpperCamelCase_			:						Tuple      =     False
					UpperCamelCase_			:						Tuple      =     False
					UpperCamelCase_			:						Optional[int]      =     False
					UpperCamelCase_			:						Optional[Any]      =     False




					def     _SCREAMING_SNAKE_CASE    (						self    :  Any  ):
							lowerCAmelCase__   = TFDPRModelTester(self  )
							lowerCAmelCase__   = ConfigTester(self	,    config_class=snake_case__	,    hidden_size=37  )




					def     _SCREAMING_SNAKE_CASE    (						self    :  Optional[int]  ):
							self.config_tester.run_common_tests()




					def     _SCREAMING_SNAKE_CASE    (						self    :  Dict  ):
							lowerCAmelCase__   = self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_dpr_context_encoder(*snake_case__  )




					def     _SCREAMING_SNAKE_CASE    (						self    :  Tuple  ):
							lowerCAmelCase__   = self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_dpr_question_encoder(*snake_case__  )




					def     _SCREAMING_SNAKE_CASE    (						self    :  int  ):
							lowerCAmelCase__   = self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_dpr_reader(*snake_case__  )




					@slow
					def     _SCREAMING_SNAKE_CASE    (						self    :  List[Any]  ):
							for model_name in TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
									lowerCAmelCase__   = TFDPRContextEncoder.from_pretrained(snake_case__  )
									self.assertIsNotNone(snake_case__  )

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

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

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

@require_tf
class    a_   (  unittest.TestCase      ):




					@slow
					def     _SCREAMING_SNAKE_CASE    (						self    :  List[str]  ):
							lowerCAmelCase__   = TFDPRQuestionEncoder.from_pretrained("""facebook/dpr-question_encoder-single-nq-base"""  )

							lowerCAmelCase__   = tf.constant(
							    [[101, 7592, 1010, 2003, 2026, 3899, 10140, 1029, 102]]  )  # [CLS] hello, is my dog cute? [SEP]
							lowerCAmelCase__   = model(snake_case__  )[0]  # embedding shape = (1, 768)
							# compare the actual values for a slice.
							lowerCAmelCase__   = tf.constant(
							    [
							        [
							            0.0323_6253,
							            0.1275_3335,
							            0.1681_8509,
							            0.0027_9786,
							            0.389_6933,
							            0.2426_4945,
							            0.217_8971,
							            -0.0233_5227,
							            -0.0848_1959,
							            -0.1432_4117,
							        ]
							    ]  )
							self.assertTrue(numpy.allclose(output[:, :10].numpy()	,    expected_slice.numpy()	,    atol=1E-4  )  )




