

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






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

  def 				__lowercase    (					self						:     Any      ):






   '''simple docstring'''
   UpperCAmelCase__   :					Tuple   				=   ids_tensor([self.batch_size, self.seq_length]   ,self.vocab_size      )

   UpperCAmelCase__   :					Any   				=   None
   if self.use_input_mask:
    UpperCAmelCase__   :					int   				=   random_attention_mask([self.batch_size, self.seq_length]      )

   UpperCAmelCase__   :					int   				=   None
   if self.use_token_type_ids:
    UpperCAmelCase__   :					List[Any]   				=   ids_tensor([self.batch_size, self.seq_length]   ,self.type_vocab_size      )

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

   UpperCAmelCase__   :					Dict   				=   self.get_config()

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

  def 				__lowercase    (					self						:     Optional[int]      ):






   '''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 				__lowercase    (					self						:     str   ,A						:     Optional[Any]   ,A						:     str   ,A						:     Tuple   ,A						:     List[Any]   ,A						:     List[str]   ,A						:     Union[str, Any]   ,A						:     Tuple      ):






   '''simple docstring'''
   UpperCAmelCase__   :					Tuple   				=   DPRContextEncoder(config=A      )
   model.to(A      )
   model.eval()
   UpperCAmelCase__   :					int   				=   model(A   ,attention_mask=A   ,token_type_ids=A      )
   UpperCAmelCase__   :					List[Any]   				=   model(A   ,token_type_ids=A      )
   UpperCAmelCase__   :					Union[str, Any]   				=   model(A      )
   self.parent.assertEqual(result.pooler_output.shape   ,(self.batch_size, self.projection_dim or self.hidden_size)      )

  def 				__lowercase    (					self						:     Dict   ,A						:     List[str]   ,A						:     str   ,A						:     Any   ,A						:     Union[str, Any]   ,A						:     Dict   ,A						:     Tuple   ,A						:     Tuple      ):






   '''simple docstring'''
   UpperCAmelCase__   :					Union[str, Any]   				=   DPRQuestionEncoder(config=A      )
   model.to(A      )
   model.eval()
   UpperCAmelCase__   :					int   				=   model(A   ,attention_mask=A   ,token_type_ids=A      )
   UpperCAmelCase__   :					Tuple   				=   model(A   ,token_type_ids=A      )
   UpperCAmelCase__   :					Tuple   				=   model(A      )
   self.parent.assertEqual(result.pooler_output.shape   ,(self.batch_size, self.projection_dim or self.hidden_size)      )

  def 				__lowercase    (					self						:     str   ,A						:     Optional[Any]   ,A						:     Union[str, Any]   ,A						:     Optional[Any]   ,A						:     Optional[Any]   ,A						:     Any   ,A						:     str   ,A						:     Union[str, Any]      ):






   '''simple docstring'''
   UpperCAmelCase__   :					str   				=   DPRReader(config=A      )
   model.to(A      )
   model.eval()
   UpperCAmelCase__   :					Optional[int]   				=   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 				__lowercase    (					self						:     Optional[Any]      ):






   '''simple docstring'''
   UpperCAmelCase__   :					List[str]   				=   self.prepare_config_and_inputs()
   (
       (
       UpperCAmelCase__
   )				, (
       UpperCAmelCase__
   )				, (
       UpperCAmelCase__
   )				, (
       UpperCAmelCase__
   )				, (
       UpperCAmelCase__
   )				, (
       UpperCAmelCase__
   )				, (
       UpperCAmelCase__
   )				, 
   )   :					int   				=   config_and_inputs
   UpperCAmelCase__   :					int   				=   {"""input_ids""": input_ids}
   return config, inputs_dict







@require_torch
class        __lowercase     (      __lowerCamelCase		,     __lowerCamelCase		,     unittest.TestCase   ):
  snake_case_			    =    (
      (
          DPRContextEncoder,
          DPRQuestionEncoder,
          DPRReader,
      )
      if is_torch_available()
      else ()
  )
  snake_case_			    =    {"""feature-extraction""": DPRQuestionEncoder} if is_torch_available() else {}

  snake_case_			    =    False
  snake_case_			    =    False  # why?
  snake_case_			    =    False
  snake_case_			    =    False

  def 				__lowercase    (					self						:     Any      ):






   '''simple docstring'''
   UpperCAmelCase__   :					Dict   				=   DPRModelTester(self      )
   UpperCAmelCase__   :					Optional[int]   				=   ConfigTester(self   ,config_class=A   ,hidden_size=37      )

  def 				__lowercase    (					self						:     Any      ):






   '''simple docstring'''
   self.config_tester.run_common_tests()

  def 				__lowercase    (					self						:     str      ):






   '''simple docstring'''
   UpperCAmelCase__   :					List[str]   				=   self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_context_encoder(*A      )

  def 				__lowercase    (					self						:     int      ):






   '''simple docstring'''
   UpperCAmelCase__   :					Optional[Any]   				=   self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_question_encoder(*A      )

  def 				__lowercase    (					self						:     List[Any]      ):






   '''simple docstring'''
   UpperCAmelCase__   :					Optional[Any]   				=   self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_reader(*A      )

  def 				__lowercase    (					self						:     Dict      ):






   '''simple docstring'''
   UpperCAmelCase__   :					List[str]   				=   self.model_tester.prepare_config_and_inputs()[0]

   UpperCAmelCase__   :					Tuple   				=   DPRQuestionEncoder(config=A      )
   model.to(A      )
   model.eval()

   with tempfile.TemporaryDirectory() as tmp_dirname:
    model.save_pretrained(A      )
    UpperCAmelCase__   :					List[str]   				=   DPRQuestionEncoder.from_pretrained(A   ,projection_dim=512      )

   self.assertIsNotNone(A      )




  @slow
  def 				__lowercase    (					self						:     List[Any]      ):






   '''simple docstring'''
   for model_name in DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
    UpperCAmelCase__   :					int   				=   DPRContextEncoder.from_pretrained(A      )
    self.assertIsNotNone(A      )

   for model_name in DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
    UpperCAmelCase__   :					int   				=   DPRContextEncoder.from_pretrained(A      )
    self.assertIsNotNone(A      )

   for model_name in DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
    UpperCAmelCase__   :					Dict   				=   DPRQuestionEncoder.from_pretrained(A      )
    self.assertIsNotNone(A      )

   for model_name in DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
    UpperCAmelCase__   :					List[Any]   				=   DPRReader.from_pretrained(A      )
    self.assertIsNotNone(A      )







@require_torch
class        __lowercase     (      unittest.TestCase   ):

  @slow
  def 				__lowercase    (					self						:     Any      ):






   '''simple docstring'''
   UpperCAmelCase__   :					Optional[Any]   				=   DPRQuestionEncoder.from_pretrained("""facebook/dpr-question_encoder-single-nq-base"""   ,return_dict=A      )
   model.to(A      )

   UpperCAmelCase__   :					List[Any]   				=   torch.tensor(
       [[101, 7_592, 1_010, 2_003, 2_026, 3_899, 10_140, 1_029, 102]]   ,dtype=torch.long   ,device=A      )  # [CLS] hello, is my dog cute? [SEP]
   UpperCAmelCase__   :					List[Any]   				=   model(A      )[0]  # embedding shape = (1, 768)
   # compare the actual values for a slice.
   UpperCAmelCase__   :					str   				=   torch.tensor(
       [
           [
               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,
           ]
       ]   ,dtype=torch.float   ,device=A   ,)
   self.assertTrue(torch.allclose(output[:, :10]   ,A   ,atol=1e-4      )      )




  @slow
  def 				__lowercase    (					self						:     str      ):






   '''simple docstring'''
   UpperCAmelCase__   :					Any   				=   DPRReaderTokenizer.from_pretrained("""facebook/dpr-reader-single-nq-base"""      )
   UpperCAmelCase__   :					int   				=   DPRReader.from_pretrained("""facebook/dpr-reader-single-nq-base"""      )
   model.to(A      )

   UpperCAmelCase__   :					List[str]   				=   tokenizer(
       questions="""What is love ?"""   ,titles="""Haddaway"""   ,texts="""What Is Love is a song recorded by the artist Haddaway"""   ,padding=A   ,return_tensors="""pt"""   ,)
   encoded_inputs.to(A      )

   UpperCAmelCase__   :					Optional[Any]   				=   model(**A      )

   # compare the actual values for a slice.
   UpperCAmelCase__   :					int   				=   torch.tensor(
       [[-1_0.3_0_0_5, -1_0.7_7_6_5, -1_1.4_8_7_2, -1_1.6_8_4_1, -1_1.9_3_1_2, -1_0.3_0_0_2, -9.8_5_4_4, -1_1.7_3_7_8, -1_2.0_8_2_1, -1_0.2_9_7_5]]   ,dtype=torch.float   ,device=A   ,)

   UpperCAmelCase__   :					Optional[Any]   				=   torch.tensor(
       [[-1_1.0_6_8_4, -1_1.7_0_4_1, -1_1.5_3_9_7, -1_0.3_4_6_5, -1_0.8_7_9_1, -6.8_4_4_3, -1_1.9_9_5_9, -1_1.0_3_6_4, -1_0.0_0_9_6, -6.8_4_0_5]]   ,dtype=torch.float   ,device=A   ,)
   self.assertTrue(torch.allclose(outputs.start_logits[:, :10]   ,A   ,atol=1e-4      )      )
   self.assertTrue(torch.allclose(outputs.end_logits[:, :10]   ,A   ,atol=1e-4      )      )








"""simple docstring"""




from typing import Dict, List, Optional, Union

import numpy as np

from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict
from ...image_transforms import center_crop, normalize, rescale, resize, to_channel_dimension_format
from ...image_utils import (
    IMAGENET_STANDARD_MEAN,
    IMAGENET_STANDARD_STD,
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    make_list_of_images,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, is_vision_available, logging


if is_vision_available():
      import PIL


__UpperCAmelCase	=			logging.get_logger(__name__)







class        __lowercase     (      __lowerCamelCase   ):
  snake_case_			    =    ["""pixel_values"""]

  def __init__(					self						:     Tuple   ,A						:     bool = True   ,A						:     Dict[str, int] = None   ,A						:     PILImageResampling = PIL.Image.NEAREST   ,A						:     bool = False   ,A						:     Dict[str, int] = None   ,A						:     Union[int, float] = 1 / 255   ,A						:     bool = False   ,A						:     bool = True   ,A						:     bool = True   ,A						:     Optional[Union[float, List[float]]] = None   ,A						:     Optional[Union[float, List[float]]] = None   ,A						:     bool = True   ,**A						:     List[str]   ,):






   '''simple docstring'''
   super().__init__(**A      )
   UpperCAmelCase__   :					Optional[Any]   				=   size if size is not None else {"""height""": 346, """width""": 346}
   UpperCAmelCase__   :					Union[str, Any]   				=   get_size_dict(A      )
   UpperCAmelCase__   :					Union[str, Any]   				=   crop_size if crop_size is not None else {"""height""": 289, """width""": 289}
   UpperCAmelCase__   :					Optional[Any]   				=   get_size_dict(A   ,param_name="""crop_size"""      )

   UpperCAmelCase__   :					Optional[int]   				=   do_resize
   UpperCAmelCase__   :					Optional[int]   				=   size
   UpperCAmelCase__   :					Optional[int]   				=   resample
   UpperCAmelCase__   :					List[str]   				=   do_center_crop
   UpperCAmelCase__   :					int   				=   crop_size
   UpperCAmelCase__   :					List[Any]   				=   do_rescale
   UpperCAmelCase__   :					List[str]   				=   rescale_factor
   UpperCAmelCase__   :					List[str]   				=   rescale_offset
   UpperCAmelCase__   :					Any   				=   do_normalize
   UpperCAmelCase__   :					Tuple   				=   image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN
   UpperCAmelCase__   :					int   				=   image_std if image_std is not None else IMAGENET_STANDARD_STD
   UpperCAmelCase__   :					Optional[int]   				=   include_top

  def 				__lowercase    (					self						:     Tuple   ,A						:     np.ndarray   ,A						:     Dict[str, int]   ,A						:     PILImageResampling = PIL.Image.NEAREST   ,A						:     Optional[Union[str, ChannelDimension]] = None   ,**A						:     Optional[int]   ,):






   '''simple docstring'''
   UpperCAmelCase__   :					List[Any]   				=   get_size_dict(A      )
   if "height" not in size or "width" not in size:
    raise ValueError(f"The size dictionary must have keys 'height' and 'width'. Got {size.keys()}"      )
   return resize(
       A   ,size=(size["""height"""], size["""width"""])   ,resample=A   ,data_format=A   ,**A      )

  def 				__lowercase    (					self						:     Union[str, Any]   ,A						:     np.ndarray   ,A						:     Dict[str, int]   ,A						:     Optional[Union[str, ChannelDimension]] = None   ,**A						:     Union[str, Any]   ,):






   '''simple docstring'''
   UpperCAmelCase__   :					Optional[Any]   				=   get_size_dict(A      )
   if "height" not in size or "width" not in size:
    raise ValueError(f"The size dictionary must have keys 'height' and 'width'. Got {size.keys()}"      )
   return center_crop(A   ,size=(size["""height"""], size["""width"""])   ,data_format=A   ,**A      )

  def 				__lowercase    (					self						:     List[Any]   ,A						:     np.ndarray   ,A						:     Union[int, float]   ,A						:     bool = True   ,A						:     Optional[Union[str, ChannelDimension]] = None   ,**A						:     Any   ,):






   '''simple docstring'''
   if offset:
    UpperCAmelCase__   :					Tuple   				=   (image - 1_2_7.5) * scale
    if data_format is not None:
     UpperCAmelCase__   :					Any   				=   to_channel_dimension_format(A   ,A      )
    UpperCAmelCase__   :					Union[str, Any]   				=   rescaled_image.astype(np.floataa      )
   else:
    UpperCAmelCase__   :					List[str]   				=   rescale(A   ,scale=A   ,data_format=A   ,**A      )
   return rescaled_image

  def 				__lowercase    (					self						:     Optional[Any]   ,A						:     np.ndarray   ,A						:     Union[float, List[float]]   ,A						:     Union[float, List[float]]   ,A						:     Optional[Union[str, ChannelDimension]] = None   ,**A						:     List[Any]   ,):






   '''simple docstring'''
   return normalize(A   ,mean=A   ,std=A   ,data_format=A   ,**A      )




  def 				__lowercase    (					self						:     Optional[int]   ,A						:     ImageInput   ,A						:     bool = None   ,A						:     Dict[str, int] = None   ,A						:     str=None   ,A						:     bool = None   ,A						:     Dict[str, int] = None   ,A						:     bool = None   ,A						:     float = None   ,A						:     bool = None   ,A						:     bool = None   ,A						:     Optional[Union[float, List[float]]] = None   ,A						:     Optional[Union[float, List[float]]] = None   ,A						:     bool = None   ,A						:     Optional[Union[str, TensorType]] = None   ,A						:     ChannelDimension = ChannelDimension.FIRST   ,**A						:     Optional[int]   ,):






   '''simple docstring'''
   UpperCAmelCase__   :					List[str]   				=   do_resize if do_resize is not None else self.do_resize
   UpperCAmelCase__   :					Optional[int]   				=   resample if resample is not None else self.resample
   UpperCAmelCase__   :					List[str]   				=   do_center_crop if do_center_crop is not None else self.do_center_crop
   UpperCAmelCase__   :					List[str]   				=   do_rescale if do_rescale is not None else self.do_rescale
   UpperCAmelCase__   :					Any   				=   rescale_factor if rescale_factor is not None else self.rescale_factor
   UpperCAmelCase__   :					str   				=   rescale_offset if rescale_offset is not None else self.rescale_offset
   UpperCAmelCase__   :					Union[str, Any]   				=   do_normalize if do_normalize is not None else self.do_normalize
   UpperCAmelCase__   :					int   				=   image_mean if image_mean is not None else self.image_mean
   UpperCAmelCase__   :					Any   				=   image_std if image_std is not None else self.image_std
   UpperCAmelCase__   :					List[Any]   				=   include_top if include_top is not None else self.include_top

   UpperCAmelCase__   :					List[Any]   				=   size if size is not None else self.size
   UpperCAmelCase__   :					int   				=   get_size_dict(A      )
   UpperCAmelCase__   :					Any   				=   crop_size if crop_size is not None else self.crop_size
   UpperCAmelCase__   :					str   				=   get_size_dict(A   ,param_name="""crop_size"""      )

   UpperCAmelCase__   :					Any   				=   make_list_of_images(A      )

   if not valid_images(A      ):
    raise ValueError(
        """Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, """
        """torch.Tensor, tf.Tensor or jax.ndarray."""      )

   if do_resize and size is None or resample is None:
    raise ValueError("""Size and resample must be specified if do_resize is True."""      )

   if do_center_crop and crop_size is None:
    raise ValueError("""Crop size must be specified if do_center_crop is True."""      )

   if do_rescale and rescale_factor is None:
    raise ValueError("""Rescale factor must be specified if do_rescale is True."""      )

   if do_normalize and (image_mean is None or image_std is None):
    raise ValueError("""Image mean and std must be specified if do_normalize is True."""      )

   # All transformations expect numpy arrays.
   UpperCAmelCase__   :					List[str]   				=   [to_numpy_array(A      ) for image in images]

   if do_resize:
    UpperCAmelCase__   :					str   				=   [self.resize(image=A   ,size=A   ,resample=A      ) for image in images]

   if do_center_crop:
    UpperCAmelCase__   :					Tuple   				=   [self.center_crop(image=A   ,size=A      ) for image in images]

   if do_rescale:
    UpperCAmelCase__   :					int   				=   [self.rescale(image=A   ,scale=A   ,offset=A      ) for image in images]

   if do_normalize:
    UpperCAmelCase__   :					int   				=   [self.normalize(image=A   ,mean=A   ,std=A      ) for image in images]

   if include_top:
    UpperCAmelCase__   :					Dict   				=   [self.normalize(image=A   ,mean=[0, 0, 0]   ,std=A      ) for image in images]

   UpperCAmelCase__   :					Optional[Any]   				=   [to_channel_dimension_format(A   ,A      ) for image in images]

   UpperCAmelCase__   :					Dict   				=   {"""pixel_values""": images}
   return BatchFeature(data=A   ,tensor_type=A      )





