


'''simple docstring'''







import tempfile
import unittest

from transformers import DPRConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
  import torch

  from transformers import DPRContextEncoder, DPRQuestionEncoder, DPRReader, DPRReaderTokenizer
  from transformers.models.dpr.modeling_dpr import (
      DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST,
      DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST,
      DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST,
  )
class  lowercase_					:


    def __init__(					self  , a  , a=13  , a=7  , a=False  , a=True  , a=True  , a=True  , a=99  , a=32  , a=5  , a=4  , a=37  , a="gelu"  , a=0.1  , a=0.1  , a=5_12  , a=16  , a=2  , a=0.02  , a=3  , a=4  , a=None  , a=0  , ):
          UpperCamelCase__  =    parent
          UpperCamelCase__  =    batch_size
          UpperCamelCase__  =    seq_length
          UpperCamelCase__  =    is_training
          UpperCamelCase__  =    use_input_mask
          UpperCamelCase__  =    use_token_type_ids
          UpperCamelCase__  =    use_labels
          UpperCamelCase__  =    vocab_size
          UpperCamelCase__  =    hidden_size
          UpperCamelCase__  =    num_hidden_layers
          UpperCamelCase__  =    num_attention_heads
          UpperCamelCase__  =    intermediate_size
          UpperCamelCase__  =    hidden_act
          UpperCamelCase__  =    hidden_dropout_prob
          UpperCamelCase__  =    attention_probs_dropout_prob
          UpperCamelCase__  =    max_position_embeddings
          UpperCamelCase__  =    type_vocab_size
          UpperCamelCase__  =    type_sequence_label_size
          UpperCamelCase__  =    initializer_range
          UpperCamelCase__  =    num_labels
          UpperCamelCase__  =    num_choices
          UpperCamelCase__  =    scope
          UpperCamelCase__  =    projection_dim


    def        __a							(					self   ):
          UpperCamelCase__  =    ids_tensor([self.batch_size, self.seq_length]  , self.vocab_size   )

          UpperCamelCase__  =    None
          if self.use_input_mask:
                UpperCamelCase__  =    random_attention_mask([self.batch_size, self.seq_length]   )

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

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

          UpperCamelCase__  =    self.get_config()

          return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels


    def        __a							(					self   ):
          return DPRConfig(
              projection_dim=self.projection_dim  , 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  , initializer_range=self.initializer_range  , )


    def        __a							(					self  , a  , a  , a  , a  , a  , a  , a   ):
          UpperCamelCase__  =    DPRContextEncoder(config=__UpperCamelCase   )
          model.to(__UpperCamelCase   )
          model.eval()
          UpperCamelCase__  =    model(__UpperCamelCase  , attention_mask=__UpperCamelCase  , token_type_ids=__UpperCamelCase   )
          UpperCamelCase__  =    model(__UpperCamelCase  , token_type_ids=__UpperCamelCase   )
          UpperCamelCase__  =    model(__UpperCamelCase   )
          self.parent.assertEqual(result.pooler_output.shape  , (self.batch_size, self.projection_dim or self.hidden_size)   )


    def        __a							(					self  , a  , a  , a  , a  , a  , a  , a   ):
          UpperCamelCase__  =    DPRQuestionEncoder(config=__UpperCamelCase   )
          model.to(__UpperCamelCase   )
          model.eval()
          UpperCamelCase__  =    model(__UpperCamelCase  , attention_mask=__UpperCamelCase  , token_type_ids=__UpperCamelCase   )
          UpperCamelCase__  =    model(__UpperCamelCase  , token_type_ids=__UpperCamelCase   )
          UpperCamelCase__  =    model(__UpperCamelCase   )
          self.parent.assertEqual(result.pooler_output.shape  , (self.batch_size, self.projection_dim or self.hidden_size)   )


    def        __a							(					self  , a  , a  , a  , a  , a  , a  , a   ):
          UpperCamelCase__  =    DPRReader(config=__UpperCamelCase   )
          model.to(__UpperCamelCase   )
          model.eval()
          UpperCamelCase__  =    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        __a							(					self   ):
          UpperCamelCase__  =    self.prepare_config_and_inputs()
          (
              (
              UpperCamelCase__     
          ) ,     (
              UpperCamelCase__     
          ) ,     (
              UpperCamelCase__     
          ) ,     (
              UpperCamelCase__     
          ) ,     (
              UpperCamelCase__     
          ) ,     (
              UpperCamelCase__     
          ) ,     (
              UpperCamelCase__     
          ) ,          
          )  =    config_and_inputs
          UpperCamelCase__  =    {"input_ids": input_ids}
          return config, inputs_dict

@require_torch
class  lowercase_					(   lowercase_  ,						lowercase_  ,						unittest.TestCase       ):
    __UpperCAmelCase             =     (
        (
            DPRContextEncoder,
            DPRQuestionEncoder,
            DPRReader,
        )
        if is_torch_available()
        else ()
    )
    __UpperCAmelCase             =     {'feature-extraction': DPRQuestionEncoder} if is_torch_available() else {}

    __UpperCAmelCase             =     False
    __UpperCAmelCase             =     False  # why?
    __UpperCAmelCase             =     False
    __UpperCAmelCase             =     False


    def        __a							(					self   ):
          UpperCamelCase__  =    DPRModelTester(self   )
          UpperCamelCase__  =    ConfigTester(self  , config_class=__UpperCamelCase  , hidden_size=37   )


    def        __a							(					self   ):
          self.config_tester.run_common_tests()


    def        __a							(					self   ):
          UpperCamelCase__  =    self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_context_encoder(*__UpperCamelCase   )


    def        __a							(					self   ):
          UpperCamelCase__  =    self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_question_encoder(*__UpperCamelCase   )


    def        __a							(					self   ):
          UpperCamelCase__  =    self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_reader(*__UpperCamelCase   )


    def        __a							(					self   ):
          UpperCamelCase__  =    self.model_tester.prepare_config_and_inputs()[0]

          UpperCamelCase__  =    DPRQuestionEncoder(config=__UpperCamelCase   )
          model.to(__UpperCamelCase   )
          model.eval()

          with tempfile.TemporaryDirectory() as tmp_dirname:
                model.save_pretrained(__UpperCamelCase   )
                UpperCamelCase__  =    DPRQuestionEncoder.from_pretrained(__UpperCamelCase  , projection_dim=5_12   )

          self.assertIsNotNone(__UpperCamelCase   )


    @slow
    def        __a							(					self   ):
          for model_name in DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                UpperCamelCase__  =    DPRContextEncoder.from_pretrained(__UpperCamelCase   )
                self.assertIsNotNone(__UpperCamelCase   )

          for model_name in DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                UpperCamelCase__  =    DPRContextEncoder.from_pretrained(__UpperCamelCase   )
                self.assertIsNotNone(__UpperCamelCase   )

          for model_name in DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                UpperCamelCase__  =    DPRQuestionEncoder.from_pretrained(__UpperCamelCase   )
                self.assertIsNotNone(__UpperCamelCase   )

          for model_name in DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                UpperCamelCase__  =    DPRReader.from_pretrained(__UpperCamelCase   )
                self.assertIsNotNone(__UpperCamelCase   )

@require_torch
class  lowercase_					(   unittest.TestCase       ):


    @slow
    def        __a							(					self   ):
          UpperCamelCase__  =    DPRQuestionEncoder.from_pretrained("facebook/dpr-question_encoder-single-nq-base"  , return_dict=__UpperCamelCase   )
          model.to(__UpperCamelCase   )

          UpperCamelCase__  =    torch.tensor(
              [[1_01, 75_92, 10_10, 20_03, 20_26, 38_99, 1_01_40, 10_29, 1_02]]  , dtype=torch.long  , device=__UpperCamelCase   )  # [CLS] hello, is my dog cute? [SEP]
          UpperCamelCase__  =    model(__UpperCamelCase   )[0]  # embedding shape = (1, 768)
          # compare the actual values for a slice.
          UpperCamelCase__  =    torch.tensor(
              [
                  [
                      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,
                  ]
              ]  , dtype=torch.float  , device=__UpperCamelCase  , )
          self.assertTrue(torch.allclose(output[:, :10]  , __UpperCamelCase  , atol=1e-4   )   )


    @slow
    def        __a							(					self   ):
          UpperCamelCase__  =    DPRReaderTokenizer.from_pretrained("facebook/dpr-reader-single-nq-base"   )
          UpperCamelCase__  =    DPRReader.from_pretrained("facebook/dpr-reader-single-nq-base"   )
          model.to(__UpperCamelCase   )

          UpperCamelCase__  =    tokenizer(
              questions="What is love ?"  , titles="Haddaway"  , texts="What Is Love is a song recorded by the artist Haddaway"  , padding=__UpperCamelCase  , return_tensors="pt"  , )
          encoded_inputs.to(__UpperCamelCase   )

          UpperCamelCase__  =    model(**__UpperCamelCase   )

          # compare the actual values for a slice.
          UpperCamelCase__  =    torch.tensor(
              [[-10.3005, -10.7765, -11.4872, -11.6841, -11.9312, -10.3002, -9.8544, -11.7378, -12.0821, -10.2975]]  , dtype=torch.float  , device=__UpperCamelCase  , )

          UpperCamelCase__  =    torch.tensor(
              [[-11.0684, -11.7041, -11.5397, -10.3465, -10.8791, -6.8443, -11.9959, -11.0364, -10.0096, -6.8405]]  , dtype=torch.float  , device=__UpperCamelCase  , )
          self.assertTrue(torch.allclose(outputs.start_logits[:, :10]  , __UpperCamelCase  , atol=1e-4   )   )
          self.assertTrue(torch.allclose(outputs.end_logits[:, :10]  , __UpperCamelCase  , atol=1e-4   )   )









'''simple docstring'''







import json
import os
import shutil
import tempfile
import unittest

from transformers import is_speech_available, is_torch_available
from transformers.models.speechta import SpeechTaTokenizer
from transformers.testing_utils import get_tests_dir, require_torch
from transformers.utils import FEATURE_EXTRACTOR_NAME


if is_speech_available() and is_torch_available():
  from transformers import SpeechTaFeatureExtractor, SpeechTaProcessor

  from .test_feature_extraction_speechta import floats_list


a__  : int				=	get_tests_dir('fixtures/test_sentencepiece_bpe_char.model')
@require_torch
class  lowercase_					(   unittest.TestCase       ):


    def        __a							(					self   ):
          UpperCamelCase__  =    tempfile.mkdtemp()

          UpperCamelCase__  =    SpeechTaTokenizer(a   )
          tokenizer.save_pretrained(self.tmpdirname   )

          UpperCamelCase__  =    {
              "feature_size": 1,
              "padding_value": 0.0,
              "sampling_rate": 1_60_00,
              "do_normalize": False,
              "num_mel_bins": 80,
              "hop_length": 16,
              "win_length": 64,
              "win_function": "hann_window",
              "fmin": 80,
              "fmax": 76_00,
              "mel_floor": 1e-10,
              "reduction_factor": 2,
              "return_attention_mask": True,
          }

          UpperCamelCase__  =    os.path.join(self.tmpdirname  , a   )
          with open(self.feature_extraction_file  , "w"  , encoding="utf-8"   ) as fp:
                fp.write(json.dumps(a   ) + "\n"   )


    def        __a							(					self  , **a   ):
          return SpeechTaTokenizer.from_pretrained(self.tmpdirname  , **a   )


    def        __a							(					self  , **a   ):
          return SpeechTaFeatureExtractor.from_pretrained(self.tmpdirname  , **a   )


    def        __a							(					self   ):
          shutil.rmtree(self.tmpdirname   )


    def        __a							(					self   ):
          UpperCamelCase__  =    self.get_tokenizer()
          UpperCamelCase__  =    self.get_feature_extractor()

          UpperCamelCase__  =    SpeechTaProcessor(tokenizer=a  , feature_extractor=a   )

          processor.save_pretrained(self.tmpdirname   )
          UpperCamelCase__  =    SpeechTaProcessor.from_pretrained(self.tmpdirname   )

          self.assertEqual(processor.tokenizer.get_vocab()  , tokenizer.get_vocab()   )
          self.assertIsInstance(processor.tokenizer  , a   )

          self.assertEqual(processor.feature_extractor.to_json_string()  , feature_extractor.to_json_string()   )
          self.assertIsInstance(processor.feature_extractor  , a   )


    def        __a							(					self   ):
          UpperCamelCase__  =    SpeechTaProcessor(tokenizer=self.get_tokenizer()  , feature_extractor=self.get_feature_extractor()   )
          processor.save_pretrained(self.tmpdirname   )

          UpperCamelCase__  =    self.get_tokenizer(bos_token="(BOS)"  , eos_token="(EOS)"   )
          UpperCamelCase__  =    self.get_feature_extractor(do_normalize=a  , padding_value=1.0   )

          UpperCamelCase__  =    SpeechTaProcessor.from_pretrained(
              self.tmpdirname  , bos_token="(BOS)"  , eos_token="(EOS)"  , do_normalize=a  , padding_value=1.0   )

          self.assertEqual(processor.tokenizer.get_vocab()  , tokenizer_add_kwargs.get_vocab()   )
          self.assertIsInstance(processor.tokenizer  , a   )

          self.assertEqual(processor.feature_extractor.to_json_string()  , feature_extractor_add_kwargs.to_json_string()   )
          self.assertIsInstance(processor.feature_extractor  , a   )


    def        __a							(					self   ):
          UpperCamelCase__  =    self.get_feature_extractor()
          UpperCamelCase__  =    self.get_tokenizer()

          UpperCamelCase__  =    SpeechTaProcessor(tokenizer=a  , feature_extractor=a   )

          UpperCamelCase__  =    floats_list((3, 10_00)   )

          UpperCamelCase__  =    feature_extractor(audio=a  , return_tensors="np"   )
          UpperCamelCase__  =    processor(audio=a  , return_tensors="np"   )

          for key in input_feat_extract.keys():
                self.assertAlmostEqual(input_feat_extract[key].sum()  , input_processor[key].sum()  , delta=1e-2   )


    def        __a							(					self   ):
          UpperCamelCase__  =    self.get_feature_extractor()
          UpperCamelCase__  =    self.get_tokenizer()

          UpperCamelCase__  =    SpeechTaProcessor(tokenizer=a  , feature_extractor=a   )

          UpperCamelCase__  =    floats_list((3, 10_00)   )

          UpperCamelCase__  =    feature_extractor(audio_target=a  , return_tensors="np"   )
          UpperCamelCase__  =    processor(audio_target=a  , return_tensors="np"   )

          for key in input_feat_extract.keys():
                self.assertAlmostEqual(input_feat_extract[key].sum()  , input_processor[key].sum()  , delta=1e-2   )


    def        __a							(					self   ):
          UpperCamelCase__  =    self.get_feature_extractor()
          UpperCamelCase__  =    self.get_tokenizer()

          UpperCamelCase__  =    SpeechTaProcessor(tokenizer=a  , feature_extractor=a   )

          UpperCamelCase__  =    "This is a test string"

          UpperCamelCase__  =    processor(text=a   )
          UpperCamelCase__  =    tokenizer(a   )

          for key in encoded_tok.keys():
                self.assertListEqual(encoded_tok[key]  , encoded_processor[key]   )


    def        __a							(					self   ):
          UpperCamelCase__  =    self.get_feature_extractor()
          UpperCamelCase__  =    self.get_tokenizer()

          UpperCamelCase__  =    SpeechTaProcessor(tokenizer=a  , feature_extractor=a   )

          UpperCamelCase__  =    "This is a test string"

          UpperCamelCase__  =    processor(text_target=a   )
          UpperCamelCase__  =    tokenizer(a   )

          for key in encoded_tok.keys():
                self.assertListEqual(encoded_tok[key]  , encoded_processor[key]   )


    def        __a							(					self   ):
          UpperCamelCase__  =    self.get_feature_extractor()
          UpperCamelCase__  =    self.get_tokenizer()

          UpperCamelCase__  =    SpeechTaProcessor(tokenizer=a  , feature_extractor=a   )

          UpperCamelCase__  =    [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

          UpperCamelCase__  =    processor.batch_decode(a   )
          UpperCamelCase__  =    tokenizer.batch_decode(a   )

          self.assertListEqual(a  , a   )


    def        __a							(					self   ):
          UpperCamelCase__  =    self.get_feature_extractor()
          UpperCamelCase__  =    self.get_tokenizer()

          UpperCamelCase__  =    SpeechTaProcessor(tokenizer=a  , feature_extractor=a   )

          self.assertListEqual(
              processor.model_input_names  , feature_extractor.model_input_names  , msg="`processor` and `feature_extractor` model input names do not match"  , )





