
'''simple docstring'''


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






			'''simple docstring'''







			def __init__(			self ,   lowerCamelCase__ ,   lowerCamelCase__=1_3 ,   lowerCamelCase__=7 ,   lowerCamelCase__=True ,   lowerCamelCase__=True ,   lowerCamelCase__=True ,   lowerCamelCase__=True ,   lowerCamelCase__=9_9 ,   lowerCamelCase__=3_2 ,   lowerCamelCase__=2 ,   lowerCamelCase__=4 ,   lowerCamelCase__=3_7 ,   lowerCamelCase__="gelu" ,   lowerCamelCase__=0.1 ,   lowerCamelCase__=0.1 ,   lowerCamelCase__=5_1_2 ,   lowerCamelCase__=1_6 ,   lowerCamelCase__=2 ,   lowerCamelCase__=0.02 ,   lowerCamelCase__=3 ,   lowerCamelCase__=4 ,   lowerCamelCase__=None ,   lowerCamelCase__=0 ,   ):

						'''simple docstring'''







						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       UpperCAmelCase				(			self   ):

						'''simple docstring'''







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

						UpperCamelCase       =  None
						if self.use_input_mask:
									# follow test_modeling_tf_ctrl.py
									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       =  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=lowerCamelCase__ ,   initializer_range=self.initializer_range ,   )
						UpperCamelCase       =  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 ,   lowerCamelCase__ ,   lowerCamelCase__ ,   lowerCamelCase__ ,   lowerCamelCase__ ,   lowerCamelCase__ ,   lowerCamelCase__ ,   lowerCamelCase__   ):

						'''simple docstring'''







						UpperCamelCase       =  TFDPRContextEncoder(config=lowerCamelCase__   )
						UpperCamelCase       =  model(lowerCamelCase__ ,   attention_mask=lowerCamelCase__ ,   token_type_ids=lowerCamelCase__   )
						UpperCamelCase       =  model(lowerCamelCase__ ,   token_type_ids=lowerCamelCase__   )
						UpperCamelCase       =  model(lowerCamelCase__   )
						self.parent.assertEqual(result.pooler_output.shape ,   (self.batch_size, self.projection_dim or self.hidden_size)   )







			def       UpperCAmelCase				(			self ,   lowerCamelCase__ ,   lowerCamelCase__ ,   lowerCamelCase__ ,   lowerCamelCase__ ,   lowerCamelCase__ ,   lowerCamelCase__ ,   lowerCamelCase__   ):

						'''simple docstring'''







						UpperCamelCase       =  TFDPRQuestionEncoder(config=lowerCamelCase__   )
						UpperCamelCase       =  model(lowerCamelCase__ ,   attention_mask=lowerCamelCase__ ,   token_type_ids=lowerCamelCase__   )
						UpperCamelCase       =  model(lowerCamelCase__ ,   token_type_ids=lowerCamelCase__   )
						UpperCamelCase       =  model(lowerCamelCase__   )
						self.parent.assertEqual(result.pooler_output.shape ,   (self.batch_size, self.projection_dim or self.hidden_size)   )







			def       UpperCAmelCase				(			self ,   lowerCamelCase__ ,   lowerCamelCase__ ,   lowerCamelCase__ ,   lowerCamelCase__ ,   lowerCamelCase__ ,   lowerCamelCase__ ,   lowerCamelCase__   ):

						'''simple docstring'''







						UpperCamelCase       =  TFDPRReader(config=lowerCamelCase__   )
						UpperCamelCase       =  model(lowerCamelCase__ ,   attention_mask=lowerCamelCase__   )

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

						'''simple docstring'''







						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_tf
class 						lowercase__	(  snake_case_,					snake_case_,					unittest.TestCase   ):






			'''simple docstring'''







			_snake_case		   =							(
			    (
			        TFDPRContextEncoder,
			        TFDPRQuestionEncoder,
			        TFDPRReader,
			    )
			    if is_tf_available()
			    else ()
			)
			_snake_case		   =							{'''feature-extraction''': TFDPRQuestionEncoder} if is_tf_available() else {}

			_snake_case		   =							False
			_snake_case		   =							False
			_snake_case		   =							False
			_snake_case		   =							False
			_snake_case		   =							False







			def       UpperCAmelCase				(			self   ):

						'''simple docstring'''







						UpperCamelCase       =  TFDPRModelTester(self   )
						UpperCamelCase       =  ConfigTester(self ,   config_class=lowerCamelCase__ ,   hidden_size=3_7   )







			def       UpperCAmelCase				(			self   ):

						'''simple docstring'''







						self.config_tester.run_common_tests()







			def       UpperCAmelCase				(			self   ):

						'''simple docstring'''







						UpperCamelCase       =  self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_dpr_context_encoder(*lowerCamelCase__   )







			def       UpperCAmelCase				(			self   ):

						'''simple docstring'''







						UpperCamelCase       =  self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_dpr_question_encoder(*lowerCamelCase__   )







			def       UpperCAmelCase				(			self   ):

						'''simple docstring'''







						UpperCamelCase       =  self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_dpr_reader(*lowerCamelCase__   )







			@slow
			def       UpperCAmelCase				(			self   ):

						'''simple docstring'''







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

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

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

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




@require_tf
class 						lowercase__	(  unittest.TestCase   ):






			'''simple docstring'''







			@slow
			def       UpperCAmelCase				(			self   ):

						'''simple docstring'''







						UpperCamelCase       =  TFDPRQuestionEncoder.from_pretrained('''facebook/dpr-question_encoder-single-nq-base'''   )

						UpperCamelCase       =  tf.constant(
						    [[1_0_1, 7_5_9_2, 1_0_1_0, 2_0_0_3, 2_0_2_6, 3_8_9_9, 1_0_1_4_0, 1_0_2_9, 1_0_2]]   )  # [CLS] hello, is my dog cute? [SEP]
						UpperCamelCase       =  model(lowerCamelCase__   )[0]  # embedding shape = (1, 768)
						# compare the actual values for a slice.
						UpperCamelCase       =  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[:, :1_0].numpy() ,   expected_slice.numpy() ,   atol=1e-4   )   )







'''simple docstring'''


import unittest

import numpy as np

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs


if is_torch_available():
   import torch

if is_vision_available():
   from PIL import Image

   from transformers import LevitImageProcessor


class 						lowercase__	(  unittest.TestCase   ):






   '''simple docstring'''







   def __init__(			self ,   lowerCamelCase__ ,   lowerCamelCase__=7 ,   lowerCamelCase__=3 ,   lowerCamelCase__=1_8 ,   lowerCamelCase__=3_0 ,   lowerCamelCase__=4_0_0 ,   lowerCamelCase__=True ,   lowerCamelCase__=None ,   lowerCamelCase__=True ,   lowerCamelCase__=None ,   lowerCamelCase__=True ,   lowerCamelCase__=[0.5, 0.5, 0.5] ,   lowerCamelCase__=[0.5, 0.5, 0.5] ,   ):

      '''simple docstring'''







      UpperCamelCase       =  size if size is not None else {'''shortest_edge''': 1_8}
      UpperCamelCase       =  crop_size if crop_size is not None else {'''height''': 1_8, '''width''': 1_8}
      UpperCamelCase       =  parent
      UpperCamelCase       =  batch_size
      UpperCamelCase       =  num_channels
      UpperCamelCase       =  image_size
      UpperCamelCase       =  min_resolution
      UpperCamelCase       =  max_resolution
      UpperCamelCase       =  do_resize
      UpperCamelCase       =  size
      UpperCamelCase       =  do_center_crop
      UpperCamelCase       =  crop_size
      UpperCamelCase       =  do_normalize
      UpperCamelCase       =  image_mean
      UpperCamelCase       =  image_std







   def       UpperCAmelCase				(			self   ):

      '''simple docstring'''







      return {
          "image_mean": self.image_mean,
          "image_std": self.image_std,
          "do_normalize": self.do_normalize,
          "do_resize": self.do_resize,
          "do_center_crop": self.do_center_crop,
          "size": self.size,
          "crop_size": self.crop_size,
      }




@require_torch
@require_vision
class 						lowercase__	(  snake_case_,					unittest.TestCase   ):






   '''simple docstring'''







   _snake_case		   =							LevitImageProcessor if is_vision_available() else None







   def       UpperCAmelCase				(			self   ):

      '''simple docstring'''







      UpperCamelCase       =  LevitImageProcessingTester(self   )







   @property
   def       UpperCAmelCase				(			self   ):

      '''simple docstring'''







      return self.image_processor_tester.prepare_image_processor_dict()







   def       UpperCAmelCase				(			self   ):

      '''simple docstring'''







      UpperCamelCase       =  self.image_processing_class(**self.image_processor_dict   )
      self.assertTrue(hasattr(lowerCamelCase__ ,   '''image_mean'''   )   )
      self.assertTrue(hasattr(lowerCamelCase__ ,   '''image_std'''   )   )
      self.assertTrue(hasattr(lowerCamelCase__ ,   '''do_normalize'''   )   )
      self.assertTrue(hasattr(lowerCamelCase__ ,   '''do_resize'''   )   )
      self.assertTrue(hasattr(lowerCamelCase__ ,   '''do_center_crop'''   )   )
      self.assertTrue(hasattr(lowerCamelCase__ ,   '''size'''   )   )







   def       UpperCAmelCase				(			self   ):

      '''simple docstring'''







      UpperCamelCase       =  self.image_processing_class.from_dict(self.image_processor_dict   )
      self.assertEqual(image_processor.size ,   {'''shortest_edge''': 1_8}   )
      self.assertEqual(image_processor.crop_size ,   {'''height''': 1_8, '''width''': 1_8}   )

      UpperCamelCase       =  self.image_processing_class.from_dict(self.image_processor_dict ,   size=4_2 ,   crop_size=8_4   )
      self.assertEqual(image_processor.size ,   {'''shortest_edge''': 4_2}   )
      self.assertEqual(image_processor.crop_size ,   {'''height''': 8_4, '''width''': 8_4}   )







   def       UpperCAmelCase				(			self   ):

      '''simple docstring'''







      pass







   def       UpperCAmelCase				(			self   ):

      '''simple docstring'''







      UpperCamelCase       =  self.image_processing_class(**self.image_processor_dict   )
      # create random PIL images
      UpperCamelCase       =  prepare_image_inputs(self.image_processor_tester ,   equal_resolution=lowerCamelCase__   )
      for image in image_inputs:
         self.assertIsInstance(lowerCamelCase__ ,   Image.Image   )

      # Test not batched input
      UpperCamelCase       =  image_processing(image_inputs[0] ,   return_tensors='''pt'''   ).pixel_values
      self.assertEqual(
          encoded_images.shape ,   (
              1,
              self.image_processor_tester.num_channels,
              self.image_processor_tester.crop_size['''height'''],
              self.image_processor_tester.crop_size['''width'''],
          ) ,   )

      # Test batched
      UpperCamelCase       =  image_processing(lowerCamelCase__ ,   return_tensors='''pt'''   ).pixel_values
      self.assertEqual(
          encoded_images.shape ,   (
              self.image_processor_tester.batch_size,
              self.image_processor_tester.num_channels,
              self.image_processor_tester.crop_size['''height'''],
              self.image_processor_tester.crop_size['''width'''],
          ) ,   )







   def       UpperCAmelCase				(			self   ):

      '''simple docstring'''







      UpperCamelCase       =  self.image_processing_class(**self.image_processor_dict   )
      # create random numpy tensors
      UpperCamelCase       =  prepare_image_inputs(self.image_processor_tester ,   equal_resolution=lowerCamelCase__ ,   numpify=lowerCamelCase__   )
      for image in image_inputs:
         self.assertIsInstance(lowerCamelCase__ ,   np.ndarray   )

      # Test not batched input
      UpperCamelCase       =  image_processing(image_inputs[0] ,   return_tensors='''pt'''   ).pixel_values
      self.assertEqual(
          encoded_images.shape ,   (
              1,
              self.image_processor_tester.num_channels,
              self.image_processor_tester.crop_size['''height'''],
              self.image_processor_tester.crop_size['''width'''],
          ) ,   )

      # Test batched
      UpperCamelCase       =  image_processing(lowerCamelCase__ ,   return_tensors='''pt'''   ).pixel_values
      self.assertEqual(
          encoded_images.shape ,   (
              self.image_processor_tester.batch_size,
              self.image_processor_tester.num_channels,
              self.image_processor_tester.crop_size['''height'''],
              self.image_processor_tester.crop_size['''width'''],
          ) ,   )







   def       UpperCAmelCase				(			self   ):

      '''simple docstring'''







      UpperCamelCase       =  self.image_processing_class(**self.image_processor_dict   )
      # create random PyTorch tensors
      UpperCamelCase       =  prepare_image_inputs(self.image_processor_tester ,   equal_resolution=lowerCamelCase__ ,   torchify=lowerCamelCase__   )
      for image in image_inputs:
         self.assertIsInstance(lowerCamelCase__ ,   torch.Tensor   )

      # Test not batched input
      UpperCamelCase       =  image_processing(image_inputs[0] ,   return_tensors='''pt'''   ).pixel_values
      self.assertEqual(
          encoded_images.shape ,   (
              1,
              self.image_processor_tester.num_channels,
              self.image_processor_tester.crop_size['''height'''],
              self.image_processor_tester.crop_size['''width'''],
          ) ,   )

      # Test batched
      UpperCamelCase       =  image_processing(lowerCamelCase__ ,   return_tensors='''pt'''   ).pixel_values
      self.assertEqual(
          encoded_images.shape ,   (
              self.image_processor_tester.batch_size,
              self.image_processor_tester.num_channels,
              self.image_processor_tester.crop_size['''height'''],
              self.image_processor_tester.crop_size['''width'''],
          ) ,   )





