





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      :      Optional[int]		,   _A      :      List[Any]		,   _A      :      str=13		,   _A      :      List[str]=7		,   _A      :      Dict=True		,   _A      :      Optional[Any]=True		,   _A      :      Dict=True		,   _A      :      int=True		,   _A      :      str=99		,   _A      :      Optional[Any]=32		,   _A      :      Optional[Any]=2		,   _A      :      Optional[int]=4		,   _A      :      Tuple=37		,   _A      :      Tuple="gelu"		,   _A      :      List[str]=0.1		,   _A      :      Union[str, Any]=0.1		,   _A      :      Any=512		,   _A      :      List[Any]=16		,   _A      :      List[str]=2		,   _A      :      int=0.0_2		,   _A      :      Any=3		,   _A      :      Optional[Any]=4		,   _A      :      int=None		,   _A      :      Any=0		,   )							->    Dict:







       """simple docstring"""


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






      def UpperCAmelCase_				(      self      :      List[str]	)							->    List[Any]:







       """simple docstring"""


       snake_case_		:			Tuple											=	ids_tensor([self.batch_size, self.seq_length]		,   self.vocab_size	)

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

       snake_case_		:			List[str]											=	None
       if self.use_token_type_ids:
        snake_case_		:			Optional[Any]											=	ids_tensor([self.batch_size, self.seq_length]		,   self.type_vocab_size	)

       snake_case_		:			Any											=	None
       snake_case_		:			Optional[int]											=	None
       snake_case_		:			Dict											=	None
       if self.use_labels:
        snake_case_		:			Tuple											=	ids_tensor([self.batch_size]		,   self.type_sequence_label_size	)
        snake_case_		:			Optional[Any]											=	ids_tensor([self.batch_size, self.seq_length]		,   self.num_labels	)
        snake_case_		:			int											=	ids_tensor([self.batch_size]		,   self.num_choices	)

       snake_case_		:			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=_A		,   initializer_range=self.initializer_range		,   )
       snake_case_		:			Dict											=	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      :      Tuple		,   _A      :      Any		,   _A      :      str		,   _A      :      List[Any]		,   _A      :      str		,   _A      :      Dict		,   _A      :      Union[str, Any]		,   _A      :      int	)							->    List[Any]:







       """simple docstring"""


       snake_case_		:			str											=	TFDPRContextEncoder(config=_A	)
       snake_case_		:			Dict											=	model(_A		,   attention_mask=_A		,   token_type_ids=_A	)
       snake_case_		:			Optional[int]											=	model(_A		,   token_type_ids=_A	)
       snake_case_		:			str											=	model(_A	)
       self.parent.assertEqual(result.pooler_output.shape		,   (self.batch_size, self.projection_dim or self.hidden_size)	)






      def UpperCAmelCase_				(      self      :      Tuple		,   _A      :      List[str]		,   _A      :      Optional[Any]		,   _A      :      Union[str, Any]		,   _A      :      Tuple		,   _A      :      Dict		,   _A      :      str		,   _A      :      List[str]	)							->    Tuple:







       """simple docstring"""


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






      def UpperCAmelCase_				(      self      :      int		,   _A      :      List[Any]		,   _A      :      List[str]		,   _A      :      Dict		,   _A      :      Optional[Any]		,   _A      :      Any		,   _A      :      Union[str, Any]		,   _A      :      Optional[Any]	)							->    str:







       """simple docstring"""


       snake_case_		:			Union[str, Any]											=	TFDPRReader(config=_A	)
       snake_case_		:			Dict											=	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 UpperCAmelCase_				(      self      :      Optional[Any]	)							->    List[str]:







       """simple docstring"""


       snake_case_		:			int											=	self.prepare_config_and_inputs()
       (
           snake_case_
       )		:			Dict											=	config_and_inputs
       snake_case_		:			int											=	{'input_ids': input_ids}
       return config, inputs_dict
@require_tf
class SCREAMING_SNAKE_CASE_						(					snake_case_       ,				snake_case_       ,				unittest.TestCase					):
      __magic_name__:			Optional[Any]       =       (
          (
              TFDPRContextEncoder,
              TFDPRQuestionEncoder,
              TFDPRReader,
          )
          if is_tf_available()
          else ()
      )
      __magic_name__:			Optional[Any]       =       {"feature-extraction": TFDPRQuestionEncoder} if is_tf_available() else {}

      __magic_name__:			List[Any]       =       False
      __magic_name__:			Any       =       False
      __magic_name__:			Any       =       False
      __magic_name__:			List[str]       =       False
      __magic_name__:			int       =       False






      def UpperCAmelCase_				(      self      :      int	)							->    Any:







       """simple docstring"""


       snake_case_		:			List[Any]											=	TFDPRModelTester(self	)
       snake_case_		:			str											=	ConfigTester(self		,   config_class=_A		,   hidden_size=37	)






      def UpperCAmelCase_				(      self      :      str	)							->    Any:







       """simple docstring"""


       self.config_tester.run_common_tests()






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







       """simple docstring"""


       snake_case_		:			int											=	self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_dpr_context_encoder(*_A	)






      def UpperCAmelCase_				(      self      :      Optional[int]	)							->    List[Any]:







       """simple docstring"""


       snake_case_		:			List[str]											=	self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_dpr_question_encoder(*_A	)






      def UpperCAmelCase_				(      self      :      Any	)							->    str:







       """simple docstring"""


       snake_case_		:			List[Any]											=	self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_dpr_reader(*_A	)






      @slow
      def UpperCAmelCase_				(      self      :      Union[str, Any]	)							->    Tuple:







       """simple docstring"""


       for model_name in TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
        snake_case_		:			List[Any]											=	TFDPRContextEncoder.from_pretrained(_A	)
        self.assertIsNotNone(_A	)

       for model_name in TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
        snake_case_		:			Optional[int]											=	TFDPRContextEncoder.from_pretrained(_A	)
        self.assertIsNotNone(_A	)

       for model_name in TF_DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
        snake_case_		:			int											=	TFDPRQuestionEncoder.from_pretrained(_A	)
        self.assertIsNotNone(_A	)

       for model_name in TF_DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
        snake_case_		:			List[Any]											=	TFDPRReader.from_pretrained(_A	)
        self.assertIsNotNone(_A	)
@require_tf
class SCREAMING_SNAKE_CASE_						(					unittest.TestCase					):






      @slow
      def UpperCAmelCase_				(      self      :      Union[str, Any]	)							->    Tuple:







       """simple docstring"""


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

       snake_case_		:			Union[str, Any]											=	tf.constant(
           [[101, 7592, 1010, 2003, 2026, 3899, 10140, 1029, 102]]	)  # [CLS] hello, is my dog cute? [SEP]
       snake_case_		:			Optional[Any]											=	model(_A	)[0]  # embedding shape = (1, 768)
       # compare the actual values for a slice.
       snake_case_		:			List[str]											=	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	)	)








import io
import json
import unittest

from parameterized import parameterized

from transformers import FSMTForConditionalGeneration, FSMTTokenizer
from transformers.testing_utils import get_tests_dir, require_torch, slow, torch_device
from utils import calculate_bleu


_SCREAMING_SNAKE_CASE      	=		get_tests_dir() + """/test_data/fsmt/fsmt_val_data.json"""
with io.open(filename, """r""", encoding="""utf-8""") as f:
						_SCREAMING_SNAKE_CASE      	=		json.load(f)
@require_torch
class SCREAMING_SNAKE_CASE_						(					unittest.TestCase					):






						def UpperCAmelCase_				(      self      :      Union[str, Any]		,   _A      :      Any	)							->    Optional[Any]:







							"""simple docstring"""


							return FSMTTokenizer.from_pretrained(_A	)






						def UpperCAmelCase_				(      self      :      Dict		,   _A      :      Union[str, Any]	)							->    Optional[Any]:







							"""simple docstring"""


							snake_case_		:			Tuple											=	FSMTForConditionalGeneration.from_pretrained(_A	).to(_A	)
							if torch_device == "cuda":
								model.half()
							return model






						@parameterized.expand(
						    [
						        ['en-ru', 2_6.0],
						        ['ru-en', 2_2.0],
						        ['en-de', 2_2.0],
						        ['de-en', 2_9.0],
						    ]	)
						@slow
						def UpperCAmelCase_				(      self      :      int		,   _A      :      Optional[Any]		,   _A      :      Optional[Any]	)							->    Union[str, Any]:







							"""simple docstring"""


							snake_case_		:			Tuple											=	F"""facebook/wmt19-{pair}"""
							snake_case_		:			List[Any]											=	self.get_tokenizer(_A	)
							snake_case_		:			List[str]											=	self.get_model(_A	)

							snake_case_		:			Union[str, Any]											=	bleu_data[pair]['src']
							snake_case_		:			List[str]											=	bleu_data[pair]['tgt']

							snake_case_		:			Optional[int]											=	tokenizer(_A		,   return_tensors='pt'		,   truncation=_A		,   padding='longest'	).to(_A	)
							snake_case_		:			List[Any]											=	model.generate(
							    input_ids=batch.input_ids		,   num_beams=8		,   )
							snake_case_		:			Dict											=	tokenizer.batch_decode(
							    _A		,   skip_special_tokens=_A		,   clean_up_tokenization_spaces=_A	)
							snake_case_		:			Tuple											=	calculate_bleu(_A		,   _A	)
							print(_A	)
							self.assertGreaterEqual(scores['bleu']		,   _A	)

