





import itertools
import random
import unittest

import numpy as np

from transformers import BatchFeature, SpeechTaFeatureExtractor
from transformers.testing_utils import require_torch
from transformers.utils.import_utils import is_torch_available

from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin


if is_torch_available():
      import torch


snake_case        =       random.Random()

def 							SCREAMING_SNAKE_CASE__       (  snake_case__  :List[str]			,							snake_case__  :Dict=1.0			,							snake_case__  :Dict=None			,							snake_case__  :int=None     )      ->						Dict:
  if rng is None:
    _lowercase							= global_rng

  _lowercase							= []
  for batch_idx in range(shape[0]     ):
    values.append([]     )
    for _ in range(shape[1]     ):
      values[-1].append(rng.random() * scale     )

  return values





@require_torch
class 				A_				(      unittest.TestCase	):







       """simple docstring"""





       def __init__(    self	:	Optional[Any]						,__A	:	str						,__A	:	Dict=7						,__A	:	List[Any]=400						,__A	:	int=2000						,__A	:	Any=1						,__A	:	Dict=0.0						,__A	:	Union[str, Any]=1_6000						,__A	:	Dict=True						,__A	:	List[Any]=80						,__A	:	Tuple=16						,__A	:	str=64						,__A	:	Union[str, Any]="hann_window"						,__A	:	List[Any]=80						,__A	:	List[str]=7600						,__A	:	Tuple=1e-10						,__A	:	int=True						,)						->					int:
         _lowercase							= parent
         _lowercase							= batch_size
         _lowercase							= min_seq_length
         _lowercase							= max_seq_length
         _lowercase							= (self.max_seq_length - self.min_seq_length) // (self.batch_size - 1)
         _lowercase							= feature_size
         _lowercase							= padding_value
         _lowercase							= sampling_rate
         _lowercase							= do_normalize
         _lowercase							= num_mel_bins
         _lowercase							= hop_length
         _lowercase							= win_length
         _lowercase							= win_function
         _lowercase							= fmin
         _lowercase							= fmax
         _lowercase							= mel_floor
         _lowercase							= return_attention_mask







       def    __UpperCAmelCase (    self	:	Dict  )						->					str:
         return {
             "feature_size": self.feature_size,
             "padding_value": self.padding_value,
             "sampling_rate": self.sampling_rate,
             "do_normalize": self.do_normalize,
             "num_mel_bins": self.num_mel_bins,
             "hop_length": self.hop_length,
             "win_length": self.win_length,
             "win_function": self.win_function,
             "fmin": self.fmin,
             "fmax": self.fmax,
             "mel_floor": self.mel_floor,
             "return_attention_mask": self.return_attention_mask,
         }







       def    __UpperCAmelCase (    self	:	int						,__A	:	int=False						,__A	:	int=False  )						->					Tuple:
         def _flatten(__A	:	List[Any]  ):
           return list(itertools.chain(*__A  )  )

         if equal_length:
           _lowercase							= floats_list((self.batch_size, self.max_seq_length)  )
         else:
           # make sure that inputs increase in size
           _lowercase							= [
               _flatten(floats_list((x, self.feature_size)  )  )
               for x in range(self.min_seq_length						,self.max_seq_length						,self.seq_length_diff  )
           ]

         if numpify:
           _lowercase							= [np.asarray(__A  ) for x in speech_inputs]

         return speech_inputs







       def    __UpperCAmelCase (    self	:	Union[str, Any]						,__A	:	Optional[int]=False						,__A	:	Optional[int]=False  )						->					str:
         if equal_length:
           _lowercase							= [floats_list((self.max_seq_length, self.num_mel_bins)  ) for _ in range(self.batch_size  )]
         else:
           # make sure that inputs increase in size
           _lowercase							= [
               floats_list((x, self.num_mel_bins)  )
               for x in range(self.min_seq_length						,self.max_seq_length						,self.seq_length_diff  )
           ]

         if numpify:
           _lowercase							= [np.asarray(__A  ) for x in speech_inputs]

         return speech_inputs





@require_torch
class 				A_				(      UpperCAmelCase					,	unittest.TestCase	):







       """simple docstring"""





       SCREAMING_SNAKE_CASE_      : str      =  SpeechTaFeatureExtractor







       def    __UpperCAmelCase (    self	:	Optional[int]  )						->					Optional[Any]:
         _lowercase							= SpeechTaFeatureExtractionTester(self  )







       def    __UpperCAmelCase (    self	:	List[Any]						,__A	:	Union[str, Any]  )						->					List[str]:
         self.assertTrue(np.all(np.mean(__A						,axis=0  ) < 1e-3  )  )
         self.assertTrue(np.all(np.abs(np.var(__A						,axis=0  ) - 1  ) < 1e-3  )  )







       def    __UpperCAmelCase (    self	:	List[str]  )						->					Union[str, Any]:
         # Tests that all call wrap to encode_plus and batch_encode_plus
         _lowercase							= self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()  )
         # create three inputs of length 800, 1000, and 1200
         _lowercase							= [floats_list((1, x)  )[0] for x in range(800						,1400						,200  )]
         _lowercase							= [np.asarray(__A  ) for speech_input in speech_inputs]

         # Test not batched input
         _lowercase							= feat_extract(speech_inputs[0]						,return_tensors='np'  ).input_values
         _lowercase							= feat_extract(np_speech_inputs[0]						,return_tensors='np'  ).input_values
         self.assertTrue(np.allclose(__A						,__A						,atol=1e-3  )  )

         # Test batched
         _lowercase							= feat_extract(__A						,return_tensors='np'  ).input_values
         _lowercase							= feat_extract(__A						,return_tensors='np'  ).input_values
         for enc_seq_a, enc_seq_a in zip(__A						,__A  ):
           self.assertTrue(np.allclose(__A						,__A						,atol=1e-3  )  )







       def    __UpperCAmelCase (    self	:	List[Any]  )						->					Dict:
         _lowercase							= self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()  )
         _lowercase							= [floats_list((1, x)  )[0] for x in range(800						,1400						,200  )]

         _lowercase							= ['longest', 'max_length', 'do_not_pad']
         _lowercase							= [None, 1600, None]
         for max_length, padding in zip(__A						,__A  ):
           _lowercase							= feat_extract(__A						,padding=__A						,max_length=__A						,return_tensors='np'  )
           _lowercase							= processed.input_values

           self._check_zero_mean_unit_variance(input_values[0][:800]  )
           self.assertTrue(input_values[0][800:].sum() < 1e-6  )
           self._check_zero_mean_unit_variance(input_values[1][:1000]  )
           self.assertTrue(input_values[0][1000:].sum() < 1e-6  )
           self._check_zero_mean_unit_variance(input_values[2][:1200]  )







       def    __UpperCAmelCase (    self	:	Optional[Any]  )						->					List[str]:
         _lowercase							= self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()  )
         _lowercase							= range(800						,1400						,200  )
         _lowercase							= [floats_list((1, x)  )[0] for x in lengths]

         _lowercase							= ['longest', 'max_length', 'do_not_pad']
         _lowercase							= [None, 1600, None]

         for max_length, padding in zip(__A						,__A  ):
           _lowercase							= feat_extract(__A						,max_length=__A						,padding=__A  )
           _lowercase							= processed.input_values

           self._check_zero_mean_unit_variance(input_values[0][:800]  )
           self._check_zero_mean_unit_variance(input_values[1][:1000]  )
           self._check_zero_mean_unit_variance(input_values[2][:1200]  )







       def    __UpperCAmelCase (    self	:	int  )						->					List[Any]:
         _lowercase							= self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()  )
         _lowercase							= [floats_list((1, x)  )[0] for x in range(800						,1400						,200  )]
         _lowercase							= feat_extract(
             __A						,truncation=__A						,max_length=1000						,padding='max_length'						,return_tensors='np'  )
         _lowercase							= processed.input_values

         self._check_zero_mean_unit_variance(input_values[0, :800]  )
         self._check_zero_mean_unit_variance(input_values[1]  )
         self._check_zero_mean_unit_variance(input_values[2]  )







       def    __UpperCAmelCase (    self	:	Dict  )						->					Optional[int]:
         _lowercase							= self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()  )
         _lowercase							= [floats_list((1, x)  )[0] for x in range(800						,1400						,200  )]
         _lowercase							= feat_extract(
             __A						,truncation=__A						,max_length=1000						,padding='longest'						,return_tensors='np'  )
         _lowercase							= processed.input_values

         self._check_zero_mean_unit_variance(input_values[0, :800]  )
         self._check_zero_mean_unit_variance(input_values[1, :1000]  )
         self._check_zero_mean_unit_variance(input_values[2]  )

         # make sure that if max_length < longest -> then pad to max_length
         self.assertTrue(input_values.shape == (3, 1000)  )

         _lowercase							= [floats_list((1, x)  )[0] for x in range(800						,1400						,200  )]
         _lowercase							= feat_extract(
             __A						,truncation=__A						,max_length=2000						,padding='longest'						,return_tensors='np'  )
         _lowercase							= processed.input_values

         self._check_zero_mean_unit_variance(input_values[0, :800]  )
         self._check_zero_mean_unit_variance(input_values[1, :1000]  )
         self._check_zero_mean_unit_variance(input_values[2]  )

         # make sure that if max_length > longest -> then pad to longest
         self.assertTrue(input_values.shape == (3, 1200)  )







       def    __UpperCAmelCase (    self	:	Dict  )						->					str:
         _lowercase							= self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()  )
         _lowercase							= np.random.rand(100  ).astype(np.floataa  )
         _lowercase							= np_speech_inputs.tolist()

         for inputs in [py_speech_inputs, np_speech_inputs]:
           _lowercase							= feature_extractor.pad([{'input_values': inputs}]						,return_tensors='np'  )
           self.assertTrue(np_processed.input_values.dtype == np.floataa  )
           _lowercase							= feature_extractor.pad([{'input_values': inputs}]						,return_tensors='pt'  )
           self.assertTrue(pt_processed.input_values.dtype == torch.floataa  )







       def    __UpperCAmelCase (    self	:	Dict  )						->					int:
         # Tests that all call wrap to encode_plus and batch_encode_plus
         _lowercase							= self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()  )
         # create three inputs of length 800, 1000, and 1200
         _lowercase							= [floats_list((1, x)  )[0] for x in range(800						,1400						,200  )]
         _lowercase							= [np.asarray(__A  ) for speech_input in speech_inputs]

         # Test feature size
         _lowercase							= feature_extractor(audio_target=__A						,padding=__A						,return_tensors='np'  ).input_values
         self.assertTrue(input_values.ndim == 3  )
         self.assertTrue(input_values.shape[-1] == feature_extractor.num_mel_bins  )

         # Test not batched input
         _lowercase							= feature_extractor(speech_inputs[0]						,return_tensors='np'  ).input_values
         _lowercase							= feature_extractor(np_speech_inputs[0]						,return_tensors='np'  ).input_values
         self.assertTrue(np.allclose(__A						,__A						,atol=1e-3  )  )

         # Test batched
         _lowercase							= feature_extractor(__A						,return_tensors='np'  ).input_values
         _lowercase							= feature_extractor(__A						,return_tensors='np'  ).input_values
         for enc_seq_a, enc_seq_a in zip(__A						,__A  ):
           self.assertTrue(np.allclose(__A						,__A						,atol=1e-3  )  )

         # Test 2-D numpy arrays are batched.
         _lowercase							= [floats_list((1, x)  )[0] for x in (800, 800, 800)]
         _lowercase							= np.asarray(__A  )
         _lowercase							= feature_extractor(__A						,return_tensors='np'  ).input_values
         _lowercase							= feature_extractor(__A						,return_tensors='np'  ).input_values
         for enc_seq_a, enc_seq_a in zip(__A						,__A  ):
           self.assertTrue(np.allclose(__A						,__A						,atol=1e-3  )  )







       def    __UpperCAmelCase (    self	:	Optional[int]  )						->					Tuple:
         _lowercase							= self.feat_extract_tester.prepare_inputs_for_target()
         _lowercase							= self.feature_extraction_class(**self.feat_extract_dict  )
         _lowercase							= feat_extract.model_input_names[0]

         _lowercase							= BatchFeature({input_name: speech_inputs}  )

         self.assertTrue(all(len(__A  ) == len(__A  ) for x, y in zip(__A						,processed_features[input_name]  )  )  )

         _lowercase							= self.feat_extract_tester.prepare_inputs_for_target(equal_length=__A  )
         _lowercase							= BatchFeature({input_name: speech_inputs}						,tensor_type='np'  )

         _lowercase							= processed_features[input_name]

         if len(batch_features_input.shape  ) < 3:
           _lowercase							= batch_features_input[:, :, None]

         self.assertTrue(
             batch_features_input.shape
             == (self.feat_extract_tester.batch_size, len(speech_inputs[0]  ), self.feat_extract_tester.num_mel_bins)  )







       @require_torch
       def    __UpperCAmelCase (    self	:	Union[str, Any]  )						->					int:
         _lowercase							= self.feat_extract_tester.prepare_inputs_for_target(equal_length=__A  )
         _lowercase							= self.feature_extraction_class(**self.feat_extract_dict  )
         _lowercase							= feat_extract.model_input_names[0]

         _lowercase							= BatchFeature({input_name: speech_inputs}						,tensor_type='pt'  )

         _lowercase							= processed_features[input_name]

         if len(batch_features_input.shape  ) < 3:
           _lowercase							= batch_features_input[:, :, None]

         self.assertTrue(
             batch_features_input.shape
             == (self.feat_extract_tester.batch_size, len(speech_inputs[0]  ), self.feat_extract_tester.num_mel_bins)  )







       @require_torch
       def    __UpperCAmelCase (    self	:	int  )						->					Any:
         _lowercase							= self.feature_extraction_class(**self.feat_extract_dict  )
         _lowercase							= self.feat_extract_tester.prepare_inputs_for_target()
         _lowercase							= feat_extract.model_input_names[0]

         _lowercase							= BatchFeature({input_name: speech_inputs}  )

         _lowercase							= feat_extract.num_mel_bins  # hack!

         _lowercase							= feat_extract.pad(__A						,padding='longest'						,return_tensors='np'  )[input_name]
         _lowercase							= feat_extract.pad(__A						,padding='longest'						,return_tensors='pt'  )[input_name]

         self.assertTrue(abs(input_np.astype(np.floataa  ).sum() - input_pt.numpy().astype(np.floataa  ).sum()  ) < 1e-2  )







       def    __UpperCAmelCase (    self	:	Optional[int]  )						->					Tuple:
         _lowercase							= self.feat_extract_dict
         _lowercase							= True
         _lowercase							= self.feature_extraction_class(**__A  )
         _lowercase							= self.feat_extract_tester.prepare_inputs_for_target()
         _lowercase							= [len(__A  ) for x in speech_inputs]
         _lowercase							= feat_extract.model_input_names[0]

         _lowercase							= BatchFeature({input_name: speech_inputs}  )

         _lowercase							= feat_extract.num_mel_bins  # hack!

         _lowercase							= feat_extract.pad(__A						,padding='longest'						,return_tensors='np'  )
         self.assertIn('attention_mask'						,__A  )
         self.assertListEqual(list(processed.attention_mask.shape  )						,list(processed[input_name].shape[:2]  )  )
         self.assertListEqual(processed.attention_mask.sum(-1  ).tolist()						,__A  )







       def    __UpperCAmelCase (    self	:	int  )						->					Dict:
         _lowercase							= self.feat_extract_dict
         _lowercase							= True
         _lowercase							= self.feature_extraction_class(**__A  )
         _lowercase							= self.feat_extract_tester.prepare_inputs_for_target()
         _lowercase							= [len(__A  ) for x in speech_inputs]
         _lowercase							= feat_extract.model_input_names[0]

         _lowercase							= BatchFeature({input_name: speech_inputs}  )
         _lowercase							= min(__A  )

         _lowercase							= feat_extract.num_mel_bins  # hack!

         _lowercase							= feat_extract.pad(
             __A						,padding='max_length'						,max_length=__A						,truncation=__A						,return_tensors='np'  )
         self.assertIn('attention_mask'						,__A  )
         self.assertListEqual(
             list(processed_pad.attention_mask.shape  )						,[processed_pad[input_name].shape[0], max_length]  )
         self.assertListEqual(
             processed_pad.attention_mask[:, :max_length].sum(-1  ).tolist()						,[max_length for x in speech_inputs]  )







       def    __UpperCAmelCase (    self	:	Tuple						,__A	:	Optional[Any]  )						->					Any:
         from datasets import load_dataset

         _lowercase							= load_dataset('hf-internal-testing/librispeech_asr_dummy'						,'clean'						,split='validation'  )
         # automatic decoding with librispeech
         _lowercase							= ds.sort('id'  ).select(range(__A  )  )[:num_samples]['audio']

         return [x["array"] for x in speech_samples]







       def    __UpperCAmelCase (    self	:	List[Any]  )						->					List[str]:
         # fmt: off
         _lowercase							= torch.tensor(
             [2.38_04e-03, 2.07_52e-03, 1.98_36e-03, 2.10_57e-03, 1.61_74e-03,
              3.05_18e-04, 9.15_53e-05, 3.35_69e-04, 9.76_56e-04, 1.83_11e-03,
              2.01_42e-03, 2.10_57e-03, 1.73_95e-03, 4.57_76e-04, -3.96_73e-04,
              4.57_76e-04, 1.00_71e-03, 9.15_53e-05, 4.88_28e-04, 1.15_97e-03,
              7.32_42e-04, 9.46_04e-04, 1.80_05e-03, 1.83_11e-03, 8.85_01e-04,
              4.27_25e-04, 4.88_28e-04, 7.32_42e-04, 1.09_86e-03, 2.10_57e-03]  )
         # fmt: on

         _lowercase							= self._load_datasamples(1  )
         _lowercase							= SpeechTaFeatureExtractor()
         _lowercase							= feature_extractor(__A						,return_tensors='pt'  ).input_values
         self.assertEquals(input_values.shape						,(1, 9_3680)  )
         self.assertTrue(torch.allclose(input_values[0, :30]						,__A						,atol=1e-6  )  )







       def    __UpperCAmelCase (    self	:	Dict  )						->					Optional[int]:
         # fmt: off
         _lowercase							= torch.tensor(
             [-2.6870, -3.0104, -3.1356, -3.5352, -3.0044, -3.0353, -3.4719, -3.6777,
              -3.1520, -2.9435, -2.6553, -2.8795, -2.9944, -2.5921, -3.0279, -3.0386,
              -3.0864, -3.1291, -3.2353, -2.7444, -2.6831, -2.7287, -3.1761, -3.1571,
              -3.2726, -3.0582, -3.1007, -3.4533, -3.4695, -3.0998]  )
         # fmt: on

         _lowercase							= self._load_datasamples(1  )
         _lowercase							= SpeechTaFeatureExtractor()
         _lowercase							= feature_extractor(audio_target=__A						,return_tensors='pt'  ).input_values
         self.assertEquals(input_values.shape						,(1, 366, 80)  )
         self.assertTrue(torch.allclose(input_values[0, 0, :30]						,__A						,atol=1e-4  )  )






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







       """simple docstring"""





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







       def    __UpperCAmelCase (    self	:	List[Any]  )						->					List[str]:
         _lowercase							= ids_tensor([self.batch_size, self.seq_length]						,self.vocab_size  )

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

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

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

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







       def    __UpperCAmelCase (    self	:	List[Any]						,__A	:	Optional[Any]						,__A	:	Tuple						,__A	:	Optional[Any]						,__A	:	Union[str, Any]						,__A	:	Tuple						,__A	:	Tuple						,__A	:	Any  )						->					str:
         _lowercase							= TFDPRQuestionEncoder(config=__A  )
         _lowercase							= model(__A						,attention_mask=__A						,token_type_ids=__A  )
         _lowercase							= model(__A						,token_type_ids=__A  )
         _lowercase							= 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[str]						,__A	:	Optional[int]						,__A	:	List[str]						,__A	:	str						,__A	:	str						,__A	:	int						,__A	:	int  )						->					Optional[Any]:
         _lowercase							= TFDPRReader(config=__A  )
         _lowercase							= 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	:	str  )						->					str:
         _lowercase							= self.prepare_config_and_inputs()
         (
             (
             _lowercase
         )						,		(
             _lowercase
         )						,		(
             _lowercase
         )						,		(
             _lowercase
         )						,		(
             _lowercase
         )						,		(
             _lowercase
         )						,		(
             _lowercase
         )						,		
         )							= config_and_inputs
         _lowercase							= {'input_ids': input_ids}
         return config, inputs_dict





@require_tf
class 				A_				(      UpperCAmelCase					,	UpperCAmelCase					,	unittest.TestCase	):







       """simple docstring"""





       SCREAMING_SNAKE_CASE_      : Union[str, Any]      =  (
           (
               TFDPRContextEncoder,
               TFDPRQuestionEncoder,
               TFDPRReader,
           )
           if is_tf_available()
           else ()
       )
       SCREAMING_SNAKE_CASE_      : Optional[Any]      =  {'''feature-extraction''': TFDPRQuestionEncoder} if is_tf_available() else {}

       SCREAMING_SNAKE_CASE_      : List[Any]      =  False
       SCREAMING_SNAKE_CASE_      : Dict      =  False
       SCREAMING_SNAKE_CASE_      : int      =  False
       SCREAMING_SNAKE_CASE_      : List[Any]      =  False
       SCREAMING_SNAKE_CASE_      : Optional[int]      =  False







       def    __UpperCAmelCase (    self	:	List[str]  )						->					Optional[int]:
         _lowercase							= TFDPRModelTester(self  )
         _lowercase							= ConfigTester(self						,config_class=__A						,hidden_size=37  )







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







       def    __UpperCAmelCase (    self	:	Dict  )						->					Optional[int]:
         _lowercase							= self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_dpr_context_encoder(*__A  )







       def    __UpperCAmelCase (    self	:	Optional[Any]  )						->					Optional[Any]:
         _lowercase							= self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_dpr_question_encoder(*__A  )







       def    __UpperCAmelCase (    self	:	Dict  )						->					List[Any]:
         _lowercase							= self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_dpr_reader(*__A  )







       @slow
       def    __UpperCAmelCase (    self	:	Optional[Any]  )						->					str:
         for model_name in TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
           _lowercase							= TFDPRContextEncoder.from_pretrained(__A  )
           self.assertIsNotNone(__A  )

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

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

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





@require_tf
class 				A_				(      unittest.TestCase	):







       """simple docstring"""





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

         _lowercase							= tf.constant(
             [[101, 7592, 1010, 2003, 2026, 3899, 1_0140, 1029, 102]]  )  # [CLS] hello, is my dog cute? [SEP]
         _lowercase							= model(__A  )[0]  # embedding shape = (1, 768)
         # compare the actual values for a slice.
         _lowercase							= 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  )  )