





'''simple docstring'''



import unittest

import numpy as np
import timeout_decorator  # noqa

from transformers import OPTConfig, is_flax_available
from transformers.testing_utils import require_flax, require_sentencepiece, slow

from ...generation.test_flax_utils import FlaxGenerationTesterMixin
from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor


if is_flax_available():
     import os

     # The slow tests are often failing with OOM error on GPU
     # This makes JAX allocate exactly what is needed on demand, and deallocate memory that is no longer needed
     # but will be slower as stated here https://jax.readthedocs.io/en/latest/gpu_memory_allocation.html
     UpperCAmelCase_    :					Optional[Any]					      =						'''platform'''

     import jax
     import jax.numpy as jnp

     from transformers import FlaxOPTForCausalLM, FlaxOPTModel, GPTaTokenizer







def      _UpperCamelCase	(_lowerCamelCase							:       Optional[Any]				,		_lowerCamelCase							:       Tuple				,		_lowerCamelCase							:       Optional[Any]=None				,		_lowerCamelCase							:       Tuple=None		)->				List[Any]:




     '''simple docstring'''





     if attention_mask is None:
          __snake_case         =					np.where(input_ids != config.pad_token_id				,		1				,		0		)
     return {
         "input_ids": input_ids,
         "attention_mask": attention_mask,
     }





@require_flax
class       lowerCAmelCase				:


       def __init__(				self      ,					__SCREAMING_SNAKE_CASE      ,					__SCREAMING_SNAKE_CASE=13      ,					__SCREAMING_SNAKE_CASE=7      ,					__SCREAMING_SNAKE_CASE=True      ,					__SCREAMING_SNAKE_CASE=False      ,					__SCREAMING_SNAKE_CASE=99      ,					__SCREAMING_SNAKE_CASE=16      ,					__SCREAMING_SNAKE_CASE=2      ,					__SCREAMING_SNAKE_CASE=4      ,					__SCREAMING_SNAKE_CASE=4      ,					__SCREAMING_SNAKE_CASE="gelu"      ,					__SCREAMING_SNAKE_CASE=0.1      ,					__SCREAMING_SNAKE_CASE=0.1      ,					__SCREAMING_SNAKE_CASE=20      ,					__SCREAMING_SNAKE_CASE=2      ,					__SCREAMING_SNAKE_CASE=1      ,					__SCREAMING_SNAKE_CASE=0      ,					__SCREAMING_SNAKE_CASE=16      ,					__SCREAMING_SNAKE_CASE=16      ,					__SCREAMING_SNAKE_CASE=0.02      ,					) ->					List[str]:



            '''simple docstring'''




            __snake_case         =					parent
            __snake_case         =					batch_size
            __snake_case         =					seq_length
            __snake_case         =					is_training
            __snake_case         =					use_labels
            __snake_case         =					vocab_size
            __snake_case         =					hidden_size
            __snake_case         =					num_hidden_layers
            __snake_case         =					num_attention_heads
            __snake_case         =					intermediate_size
            __snake_case         =					hidden_act
            __snake_case         =					hidden_dropout_prob
            __snake_case         =					attention_probs_dropout_prob
            __snake_case         =					max_position_embeddings
            __snake_case         =					eos_token_id
            __snake_case         =					pad_token_id
            __snake_case         =					bos_token_id
            __snake_case         =					embed_dim
            __snake_case         =					word_embed_proj_dim
            __snake_case         =					initializer_range
            __snake_case         =					False


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



            '''simple docstring'''




            __snake_case         =					np.clip(ids_tensor([self.batch_size, self.seq_length - 1]      ,					self.vocab_size					)      ,					3      ,					self.vocab_size					)
            __snake_case         =					np.concatenate((input_ids, 2 * np.ones((self.batch_size, 1)      ,					dtype=np.intaa					))      ,					-1					)

            __snake_case         =					OPTConfig(
                vocab_size=self.vocab_size      ,					hidden_size=self.hidden_size      ,					num_hidden_layers=self.num_hidden_layers      ,					num_attention_heads=self.num_attention_heads      ,					ffn_dim=self.intermediate_size      ,					dropout=self.hidden_dropout_prob      ,					attention_dropout=self.attention_probs_dropout_prob      ,					max_position_embeddings=self.max_position_embeddings      ,					eos_token_id=self.eos_token_id      ,					bos_token_id=self.bos_token_id      ,					pad_token_id=self.pad_token_id      ,					embed_dim=self.embed_dim      ,					is_encoder_decoder=__SCREAMING_SNAKE_CASE      ,					word_embed_proj_dim=self.word_embed_proj_dim      ,					initializer_range=self.initializer_range      ,					use_cache=__SCREAMING_SNAKE_CASE      ,					)
            __snake_case         =					prepare_opt_inputs_dict(__SCREAMING_SNAKE_CASE      ,					__SCREAMING_SNAKE_CASE					)
            return config, inputs_dict


       def  lowerCAmelCase (				self					) ->					Dict:



            '''simple docstring'''




            __snake_case   ,      __snake_case         =					self.prepare_config_and_inputs()
            return config, inputs_dict


       def  lowerCAmelCase (				self      ,					__SCREAMING_SNAKE_CASE      ,					__SCREAMING_SNAKE_CASE      ,					__SCREAMING_SNAKE_CASE					) ->					Tuple:



            '''simple docstring'''




            __snake_case         =					20
            __snake_case         =					model_class_name(__SCREAMING_SNAKE_CASE					)

            __snake_case         =					inputs_dict['''input_ids''']
            __snake_case         =					inputs_dict['''attention_mask''']

            __snake_case         =					model.init_cache(input_ids.shape[0]      ,					__SCREAMING_SNAKE_CASE					)
            __snake_case         =					jnp.ones((input_ids.shape[0], max_length)      ,					dtype='''i4'''					)

            __snake_case         =					jnp.broadcast_to(
                jnp.arange(input_ids.shape[-1] - 1					)[None, :]      ,					(input_ids.shape[0], input_ids.shape[-1] - 1)      ,					)
            __snake_case         =					model(
                input_ids[:, :-1]      ,					attention_mask=__SCREAMING_SNAKE_CASE      ,					past_key_values=__SCREAMING_SNAKE_CASE      ,					position_ids=__SCREAMING_SNAKE_CASE      ,					)

            __snake_case         =					jnp.array(input_ids.shape[0] * [[input_ids.shape[-1] - 1]]      ,					dtype='''i4'''					)
            __snake_case         =					model(
                input_ids[:, -1:]      ,					attention_mask=__SCREAMING_SNAKE_CASE      ,					past_key_values=outputs_cache.past_key_values      ,					position_ids=__SCREAMING_SNAKE_CASE      ,					)

            __snake_case         =					model(__SCREAMING_SNAKE_CASE					)

            __snake_case         =					np.max(np.abs((outputs_cache_next[0][:, -1, :5] - outputs[0][:, -1, :5])					)					)
            self.parent.assertTrue(diff < 1E-3      ,					msg=F'''Max diff is {diff}'''					)




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



            '''simple docstring'''




            __snake_case         =					20
            __snake_case         =					model_class_name(__SCREAMING_SNAKE_CASE					)

            __snake_case   ,      __snake_case         =					(
                inputs_dict['''input_ids'''],
                inputs_dict['''attention_mask'''],
            )

            __snake_case         =					jnp.concatenate(
                [
                    attention_mask,
                    jnp.zeros((attention_mask.shape[0], max_length - attention_mask.shape[1])					),
                ]      ,					axis=-1      ,					)

            __snake_case         =					model.init_cache(input_ids.shape[0]      ,					__SCREAMING_SNAKE_CASE					)
            __snake_case         =					jnp.broadcast_to(
                jnp.arange(input_ids.shape[-1] - 1					)[None, :]      ,					(input_ids.shape[0], input_ids.shape[-1] - 1)      ,					)

            __snake_case         =					model(
                input_ids[:, :-1]      ,					attention_mask=__SCREAMING_SNAKE_CASE      ,					past_key_values=__SCREAMING_SNAKE_CASE      ,					position_ids=__SCREAMING_SNAKE_CASE      ,					)
            __snake_case         =					jnp.array(input_ids.shape[0] * [[input_ids.shape[-1] - 1]]      ,					dtype='''i4'''					)
            __snake_case         =					model(
                input_ids[:, -1:]      ,					past_key_values=outputs_cache.past_key_values      ,					attention_mask=__SCREAMING_SNAKE_CASE      ,					position_ids=__SCREAMING_SNAKE_CASE      ,					)

            __snake_case         =					model(__SCREAMING_SNAKE_CASE      ,					attention_mask=__SCREAMING_SNAKE_CASE					)

            __snake_case         =					np.max(np.abs((outputs_cache_next[0][:, -1, :5] - outputs[0][:, -1, :5])					)					)
            self.parent.assertTrue(diff < 1E-3      ,					msg=F'''Max diff is {diff}'''					)





@require_flax
class       lowerCAmelCase				(	__lowerCAmelCase			,				unittest.TestCase			,				__lowerCAmelCase):
       __lowercase					: str	    =     (FlaxOPTModel, FlaxOPTForCausalLM) if is_flax_available() else ()
       __lowercase					: Dict	    =     () if is_flax_available() else ()


       def  lowerCAmelCase (				self					) ->					Tuple:



            '''simple docstring'''




            __snake_case         =					FlaxOPTModelTester(self					)


       def  lowerCAmelCase (				self					) ->					Optional[int]:



            '''simple docstring'''




            __snake_case   ,      __snake_case         =					self.model_tester.prepare_config_and_inputs()
            for model_class in self.all_model_classes:
                 self.model_tester.check_use_cache_forward(__SCREAMING_SNAKE_CASE      ,					__SCREAMING_SNAKE_CASE      ,					__SCREAMING_SNAKE_CASE					)


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



            '''simple docstring'''




            __snake_case   ,      __snake_case         =					self.model_tester.prepare_config_and_inputs()
            for model_class in self.all_model_classes:
                 self.model_tester.check_use_cache_forward_with_attn_mask(__SCREAMING_SNAKE_CASE      ,					__SCREAMING_SNAKE_CASE      ,					__SCREAMING_SNAKE_CASE					)




       @slow
       def  lowerCAmelCase (				self					) ->					str:



            '''simple docstring'''




            for model_class_name in self.all_model_classes:
                 __snake_case         =					model_class_name.from_pretrained('''facebook/opt-125m'''					)
                 __snake_case         =					np.ones((1, 1)					) * model.config.eos_token_id
                 __snake_case         =					model(__SCREAMING_SNAKE_CASE					)
                 self.assertIsNotNone(__SCREAMING_SNAKE_CASE					)





@require_sentencepiece
@require_flax
class       lowerCAmelCase				(	unittest.TestCase):


       @slow
       def  lowerCAmelCase (				self					) ->					str:



            '''simple docstring'''




            __snake_case         =					FlaxOPTModel.from_pretrained('''facebook/opt-350m'''					)
            __snake_case         =					jnp.array([[0, 3_1414, 232, 328, 740, 1140, 1_2695, 69, 4_6078, 1588, 2]]					)
            __snake_case         =					model(input_ids=__SCREAMING_SNAKE_CASE					).last_hidden_state
            __snake_case         =					(1, 11, 512)
            self.assertEqual(output.shape      ,					__SCREAMING_SNAKE_CASE					)
            __snake_case         =					jnp.array(
                [[-0.2_867, -1.9_256, -0.3_062], [-1.2_711, -0.1_337, -0.1_897], [0.4_109, 0.1_187, -1.3_142]]					)
            self.assertTrue(jnp.allclose(output[:, :3, :3]      ,					__SCREAMING_SNAKE_CASE      ,					atol=4E-2					)					)





@require_flax
@slow
class       lowerCAmelCase				(	unittest.TestCase):


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



            '''simple docstring'''




            super().setUp()
            __snake_case         =					'''facebook/opt-350m'''




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



            '''simple docstring'''




            __snake_case         =					FlaxOPTForCausalLM.from_pretrained(self.path_model					)
            __snake_case         =					GPTaTokenizer.from_pretrained(self.path_model					)

            __snake_case         =					[
                '''Today is a beautiful day and I want to''',
                '''In the city of''',
                '''Paris is the capital of France and''',
                '''Computers and mobile phones have taken''',
            ]
            # verify that prompt without BOS token is identical to Metaseq -> add_special_tokens=False
            __snake_case         =					tokenizer(__SCREAMING_SNAKE_CASE      ,					return_tensors='''jax'''      ,					padding=__SCREAMING_SNAKE_CASE      ,					add_special_tokens=__SCREAMING_SNAKE_CASE					)
            __snake_case         =					model(inputs.input_ids      ,					attention_mask=inputs.attention_mask					)[0].mean(axis=-1					)
            __snake_case         =					jnp.array(
                [
                    [1.3_851, -13.8_923, -10.5_229, -10.7_533, -0.2_309, -10.2_384, -0.5_365, -9.0_947, -5.1_670],
                    [-4.7_073, -10.6_276, -3.9_415, -21.5_242, -0.2_822, -0.2_822, -0.2_822, -0.2_822, -0.2_822],
                    [0.6_247, -3.4_229, -8.9_179, -1.4_297, -14.1_650, 1.4_146, -9.0_218, -0.2_703, -0.2_703],
                    [6.4_783, -1.9_913, -10.7_926, -2.3_336, 1.5_092, -0.9_974, -6.8_213, 1.3_477, 1.3_477],
                ]					)
            self.assertTrue(jnp.allclose(__SCREAMING_SNAKE_CASE      ,					__SCREAMING_SNAKE_CASE      ,					atol=4E-2					)					)

            __snake_case         =					jax.jit(__SCREAMING_SNAKE_CASE					)
            __snake_case         =					model(inputs.input_ids      ,					attention_mask=inputs.attention_mask					)[0].mean(axis=-1					)
            self.assertTrue(jnp.allclose(__SCREAMING_SNAKE_CASE      ,					__SCREAMING_SNAKE_CASE      ,					atol=4E-2					)					)





@require_flax
@slow
class       lowerCAmelCase				(	unittest.TestCase):


       @property
       def  lowerCAmelCase (				self					) ->					Optional[int]:



            '''simple docstring'''




            return [
                "Today is a beautiful day and I want",
                "In the city of",
                "Paris is the capital of France and",
                "Computers and mobile phones have taken",
            ]


       def  lowerCAmelCase (				self					) ->					Any:



            '''simple docstring'''




            __snake_case         =					'''facebook/opt-125m'''

            __snake_case         =					[
                '''Today is a beautiful day and I want to''',
                '''In the city of New York, the city''',
                '''Paris is the capital of France and the capital''',
                '''Computers and mobile phones have taken over the''',
            ]

            __snake_case         =					[]

            __snake_case         =					FlaxOPTForCausalLM.from_pretrained(__SCREAMING_SNAKE_CASE					)
            __snake_case         =					GPTaTokenizer.from_pretrained(__SCREAMING_SNAKE_CASE					)

            for prompt in self.prompts:
                 __snake_case         =					tokenizer(__SCREAMING_SNAKE_CASE      ,					return_tensors='''jax'''					).input_ids

                 __snake_case         =					model.generate(__SCREAMING_SNAKE_CASE      ,					max_length=10					)
                 __snake_case         =					generated_ids[0]

                 __snake_case         =					tokenizer.batch_decode(__SCREAMING_SNAKE_CASE      ,					skip_special_tokens=__SCREAMING_SNAKE_CASE					)
                 predicted_outputs += generated_string

            self.assertListEqual(__SCREAMING_SNAKE_CASE      ,					__SCREAMING_SNAKE_CASE					)


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



            '''simple docstring'''




            __snake_case         =					'''facebook/opt-350m'''

            __snake_case         =					[
                '''Today is a beautiful day and I want to''',
                '''In the city of San Francisco, the city''',
                '''Paris is the capital of France and the capital''',
                '''Computers and mobile phones have taken over the''',
            ]

            __snake_case         =					[]
            __snake_case         =					FlaxOPTForCausalLM.from_pretrained(__SCREAMING_SNAKE_CASE					)
            __snake_case         =					GPTaTokenizer.from_pretrained(__SCREAMING_SNAKE_CASE					)

            for prompt in self.prompts:
                 __snake_case         =					tokenizer(__SCREAMING_SNAKE_CASE      ,					return_tensors='''jax'''					).input_ids

                 __snake_case         =					model.generate(__SCREAMING_SNAKE_CASE      ,					max_length=10					)
                 __snake_case         =					generated_ids[0]

                 __snake_case         =					tokenizer.batch_decode(__SCREAMING_SNAKE_CASE      ,					skip_special_tokens=__SCREAMING_SNAKE_CASE					)
                 predicted_outputs += generated_string

            self.assertListEqual(__SCREAMING_SNAKE_CASE      ,					__SCREAMING_SNAKE_CASE					)


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



            '''simple docstring'''




            __snake_case         =					'''facebook/opt-125m'''
            __snake_case         =					[
                '''Today is a beautiful day and I want to thank''',
                '''In the city of Rome Canaver Canaver Canaver Canaver''',
            ]
            __snake_case         =					FlaxOPTForCausalLM.from_pretrained(__SCREAMING_SNAKE_CASE					)
            __snake_case         =					GPTaTokenizer.from_pretrained(__SCREAMING_SNAKE_CASE					)
            __snake_case         =					tokenizer(
                [
                    '''Today is a beautiful day and I want to''',
                    '''In the city of''',
                ]      ,					return_tensors='''jax'''      ,					padding=__SCREAMING_SNAKE_CASE      ,					)

            __snake_case         =					jax.jit(model.generate					)

            __snake_case         =					jit_generate(inputs['''input_ids''']      ,					attention_mask=inputs['''attention_mask''']					).sequences

            __snake_case         =					tokenizer.batch_decode(__SCREAMING_SNAKE_CASE      ,					skip_special_tokens=__SCREAMING_SNAKE_CASE					)

            self.assertIsNotNone(__SCREAMING_SNAKE_CASE      ,					__SCREAMING_SNAKE_CASE					)




       def  lowerCAmelCase (				self					) ->					Any:



            '''simple docstring'''




            __snake_case         =					'''facebook/opt-350m'''

            __snake_case         =					GPTaTokenizer.from_pretrained(__SCREAMING_SNAKE_CASE					)
            __snake_case         =					FlaxOPTForCausalLM.from_pretrained(__SCREAMING_SNAKE_CASE					)

            __snake_case         =					'''left'''

            # use different length sentences to test batching
            __snake_case         =					[
                '''Hello, my dog is a little''',
                '''Today, I''',
            ]

            __snake_case         =					tokenizer(__SCREAMING_SNAKE_CASE      ,					return_tensors='''jax'''      ,					padding=__SCREAMING_SNAKE_CASE					)
            __snake_case         =					inputs['''input_ids''']

            __snake_case         =					model.generate(input_ids=__SCREAMING_SNAKE_CASE      ,					attention_mask=inputs['''attention_mask''']      ,					trace=__SCREAMING_SNAKE_CASE					)

            __snake_case         =					tokenizer(sentences[0]      ,					return_tensors='''jax'''					).input_ids
            __snake_case         =					model.generate(input_ids=__SCREAMING_SNAKE_CASE					)

            __snake_case         =					inputs_non_padded.shape[-1] - inputs['''attention_mask'''][-1].sum()
            __snake_case         =					tokenizer(sentences[1]      ,					return_tensors='''jax'''					).input_ids
            __snake_case         =					model.generate(input_ids=__SCREAMING_SNAKE_CASE      ,					max_length=model.config.max_length - num_paddings					)

            __snake_case         =					tokenizer.batch_decode(outputs[0]      ,					skip_special_tokens=__SCREAMING_SNAKE_CASE					)
            __snake_case         =					tokenizer.decode(output_non_padded[0][0]      ,					skip_special_tokens=__SCREAMING_SNAKE_CASE					)
            __snake_case         =					tokenizer.decode(output_padded[0][0]      ,					skip_special_tokens=__SCREAMING_SNAKE_CASE					)

            __snake_case         =					[
                '''Hello, my dog is a little bit of a dork.\nI\'m a little bit''',
                '''Today, I was in the middle of a conversation with a friend about the''',
            ]
            self.assertListEqual(__SCREAMING_SNAKE_CASE      ,					__SCREAMING_SNAKE_CASE					)
            self.assertListEqual(__SCREAMING_SNAKE_CASE      ,					[non_padded_sentence, padded_sentence]					)












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


       def __init__(				self      ,					__SCREAMING_SNAKE_CASE      ,					__SCREAMING_SNAKE_CASE=13      ,					__SCREAMING_SNAKE_CASE=7      ,					__SCREAMING_SNAKE_CASE=False      ,					__SCREAMING_SNAKE_CASE=True      ,					__SCREAMING_SNAKE_CASE=True      ,					__SCREAMING_SNAKE_CASE=True      ,					__SCREAMING_SNAKE_CASE=99      ,					__SCREAMING_SNAKE_CASE=32      ,					__SCREAMING_SNAKE_CASE=5      ,					__SCREAMING_SNAKE_CASE=4      ,					__SCREAMING_SNAKE_CASE=37      ,					__SCREAMING_SNAKE_CASE="gelu"      ,					__SCREAMING_SNAKE_CASE=0.1      ,					__SCREAMING_SNAKE_CASE=0.1      ,					__SCREAMING_SNAKE_CASE=512      ,					__SCREAMING_SNAKE_CASE=16      ,					__SCREAMING_SNAKE_CASE=2      ,					__SCREAMING_SNAKE_CASE=0.02      ,					__SCREAMING_SNAKE_CASE=3      ,					__SCREAMING_SNAKE_CASE=4      ,					__SCREAMING_SNAKE_CASE=None      ,					__SCREAMING_SNAKE_CASE=0      ,					) ->					str:



            '''simple docstring'''




            __snake_case         =					parent
            __snake_case         =					batch_size
            __snake_case         =					seq_length
            __snake_case         =					is_training
            __snake_case         =					use_input_mask
            __snake_case         =					use_token_type_ids
            __snake_case         =					use_labels
            __snake_case         =					vocab_size
            __snake_case         =					hidden_size
            __snake_case         =					num_hidden_layers
            __snake_case         =					num_attention_heads
            __snake_case         =					intermediate_size
            __snake_case         =					hidden_act
            __snake_case         =					hidden_dropout_prob
            __snake_case         =					attention_probs_dropout_prob
            __snake_case         =					max_position_embeddings
            __snake_case         =					type_vocab_size
            __snake_case         =					type_sequence_label_size
            __snake_case         =					initializer_range
            __snake_case         =					num_labels
            __snake_case         =					num_choices
            __snake_case         =					scope
            __snake_case         =					projection_dim


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



            '''simple docstring'''




            __snake_case         =					ids_tensor([self.batch_size, self.seq_length]      ,					self.vocab_size					)

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

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

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

            __snake_case         =					self.get_config()

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


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



            '''simple docstring'''




            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  lowerCAmelCase (				self      ,					__SCREAMING_SNAKE_CASE      ,					__SCREAMING_SNAKE_CASE      ,					__SCREAMING_SNAKE_CASE      ,					__SCREAMING_SNAKE_CASE      ,					__SCREAMING_SNAKE_CASE      ,					__SCREAMING_SNAKE_CASE      ,					__SCREAMING_SNAKE_CASE					) ->					Any:



            '''simple docstring'''




            __snake_case         =					DPRContextEncoder(config=__SCREAMING_SNAKE_CASE					)
            model.to(__SCREAMING_SNAKE_CASE					)
            model.eval()
            __snake_case         =					model(__SCREAMING_SNAKE_CASE      ,					attention_mask=__SCREAMING_SNAKE_CASE      ,					token_type_ids=__SCREAMING_SNAKE_CASE					)
            __snake_case         =					model(__SCREAMING_SNAKE_CASE      ,					token_type_ids=__SCREAMING_SNAKE_CASE					)
            __snake_case         =					model(__SCREAMING_SNAKE_CASE					)
            self.parent.assertEqual(result.pooler_output.shape      ,					(self.batch_size, self.projection_dim or self.hidden_size)					)


       def  lowerCAmelCase (				self      ,					__SCREAMING_SNAKE_CASE      ,					__SCREAMING_SNAKE_CASE      ,					__SCREAMING_SNAKE_CASE      ,					__SCREAMING_SNAKE_CASE      ,					__SCREAMING_SNAKE_CASE      ,					__SCREAMING_SNAKE_CASE      ,					__SCREAMING_SNAKE_CASE					) ->					Any:



            '''simple docstring'''




            __snake_case         =					DPRQuestionEncoder(config=__SCREAMING_SNAKE_CASE					)
            model.to(__SCREAMING_SNAKE_CASE					)
            model.eval()
            __snake_case         =					model(__SCREAMING_SNAKE_CASE      ,					attention_mask=__SCREAMING_SNAKE_CASE      ,					token_type_ids=__SCREAMING_SNAKE_CASE					)
            __snake_case         =					model(__SCREAMING_SNAKE_CASE      ,					token_type_ids=__SCREAMING_SNAKE_CASE					)
            __snake_case         =					model(__SCREAMING_SNAKE_CASE					)
            self.parent.assertEqual(result.pooler_output.shape      ,					(self.batch_size, self.projection_dim or self.hidden_size)					)


       def  lowerCAmelCase (				self      ,					__SCREAMING_SNAKE_CASE      ,					__SCREAMING_SNAKE_CASE      ,					__SCREAMING_SNAKE_CASE      ,					__SCREAMING_SNAKE_CASE      ,					__SCREAMING_SNAKE_CASE      ,					__SCREAMING_SNAKE_CASE      ,					__SCREAMING_SNAKE_CASE					) ->					Tuple:



            '''simple docstring'''




            __snake_case         =					DPRReader(config=__SCREAMING_SNAKE_CASE					)
            model.to(__SCREAMING_SNAKE_CASE					)
            model.eval()
            __snake_case         =					model(
                __SCREAMING_SNAKE_CASE      ,					attention_mask=__SCREAMING_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  lowerCAmelCase (				self					) ->					str:



            '''simple docstring'''




            __snake_case         =					self.prepare_config_and_inputs()
            (
                (
                __snake_case
            )   ,      (
                __snake_case
            )   ,      (
                __snake_case
            )   ,      (
                __snake_case
            )   ,      (
                __snake_case
            )   ,      (
                __snake_case
            )   ,      (
                __snake_case
            )   ,      
            )         =					config_and_inputs
            __snake_case         =					{'''input_ids''': input_ids}
            return config, inputs_dict





@require_torch
class       lowerCAmelCase				(	__lowerCAmelCase			,				__lowerCAmelCase			,				unittest.TestCase):
       __lowercase					: Optional[int]	    =     (
           (
               DPRContextEncoder,
               DPRQuestionEncoder,
               DPRReader,
           )
           if is_torch_available()
           else ()
       )
       __lowercase					: Optional[Any]	    =     {'''feature-extraction''': DPRQuestionEncoder} if is_torch_available() else {}

       __lowercase					: Optional[Any]	    =     False
       __lowercase					: List[str]	    =     False  # why?
       __lowercase					: Optional[Any]	    =     False
       __lowercase					: Dict	    =     False


       def  lowerCAmelCase (				self					) ->					Tuple:



            '''simple docstring'''




            __snake_case         =					DPRModelTester(self					)
            __snake_case         =					ConfigTester(self      ,					config_class=__SCREAMING_SNAKE_CASE      ,					hidden_size=37					)


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



            '''simple docstring'''




            self.config_tester.run_common_tests()


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



            '''simple docstring'''




            __snake_case         =					self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_context_encoder(*__SCREAMING_SNAKE_CASE					)


       def  lowerCAmelCase (				self					) ->					str:



            '''simple docstring'''




            __snake_case         =					self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_question_encoder(*__SCREAMING_SNAKE_CASE					)


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



            '''simple docstring'''




            __snake_case         =					self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_reader(*__SCREAMING_SNAKE_CASE					)


       def  lowerCAmelCase (				self					) ->					Any:



            '''simple docstring'''




            __snake_case         =					self.model_tester.prepare_config_and_inputs()[0]

            __snake_case         =					DPRQuestionEncoder(config=__SCREAMING_SNAKE_CASE					)
            model.to(__SCREAMING_SNAKE_CASE					)
            model.eval()

            with tempfile.TemporaryDirectory() as tmp_dirname:
                 model.save_pretrained(__SCREAMING_SNAKE_CASE					)
                 __snake_case         =					DPRQuestionEncoder.from_pretrained(__SCREAMING_SNAKE_CASE      ,					projection_dim=512					)

            self.assertIsNotNone(__SCREAMING_SNAKE_CASE					)




       @slow
       def  lowerCAmelCase (				self					) ->					Tuple:



            '''simple docstring'''




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

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

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

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





@require_torch
class       lowerCAmelCase				(	unittest.TestCase):


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



            '''simple docstring'''




            __snake_case         =					DPRQuestionEncoder.from_pretrained('''facebook/dpr-question_encoder-single-nq-base'''      ,					return_dict=__SCREAMING_SNAKE_CASE					)
            model.to(__SCREAMING_SNAKE_CASE					)

            __snake_case         =					torch.tensor(
                [[101, 7592, 1010, 2003, 2026, 3899, 1_0140, 1029, 102]]      ,					dtype=torch.long      ,					device=__SCREAMING_SNAKE_CASE					)  # [CLS] hello, is my dog cute? [SEP]
            __snake_case         =					model(__SCREAMING_SNAKE_CASE					)[0]  # embedding shape = (1, 768)
            # compare the actual values for a slice.
            __snake_case         =					torch.tensor(
                [
                    [
                        0.03_236_253,
                        0.12_753_335,
                        0.16_818_509,
                        0.00_279_786,
                        0.3_896_933,
                        0.24_264_945,
                        0.2_178_971,
                        -0.02_335_227,
                        -0.08_481_959,
                        -0.14_324_117,
                    ]
                ]      ,					dtype=torch.float      ,					device=__SCREAMING_SNAKE_CASE      ,					)
            self.assertTrue(torch.allclose(output[:, :10]      ,					__SCREAMING_SNAKE_CASE      ,					atol=1E-4					)					)




       @slow
       def  lowerCAmelCase (				self					) ->					Any:



            '''simple docstring'''




            __snake_case         =					DPRReaderTokenizer.from_pretrained('''facebook/dpr-reader-single-nq-base'''					)
            __snake_case         =					DPRReader.from_pretrained('''facebook/dpr-reader-single-nq-base'''					)
            model.to(__SCREAMING_SNAKE_CASE					)

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

            __snake_case         =					model(**__SCREAMING_SNAKE_CASE					)

            # compare the actual values for a slice.
            __snake_case         =					torch.tensor(
                [[-10.3_005, -10.7_765, -11.4_872, -11.6_841, -11.9_312, -10.3_002, -9.8_544, -11.7_378, -12.0_821, -10.2_975]]      ,					dtype=torch.float      ,					device=__SCREAMING_SNAKE_CASE      ,					)

            __snake_case         =					torch.tensor(
                [[-11.0_684, -11.7_041, -11.5_397, -10.3_465, -10.8_791, -6.8_443, -11.9_959, -11.0_364, -10.0_096, -6.8_405]]      ,					dtype=torch.float      ,					device=__SCREAMING_SNAKE_CASE      ,					)
            self.assertTrue(torch.allclose(outputs.start_logits[:, :10]      ,					__SCREAMING_SNAKE_CASE      ,					atol=1E-4					)					)
            self.assertTrue(torch.allclose(outputs.end_logits[:, :10]      ,					__SCREAMING_SNAKE_CASE      ,					atol=1E-4					)					)






