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

     """simple docstring"""

     def __init__(      self							,		lowerCAmelCase_							,		lowerCAmelCase_=13							,		lowerCAmelCase_=7							,		lowerCAmelCase_=True							,		lowerCAmelCase_=True							,		lowerCAmelCase_=True							,		lowerCAmelCase_=True							,		lowerCAmelCase_=99							,		lowerCAmelCase_=32							,		lowerCAmelCase_=2							,		lowerCAmelCase_=4							,		lowerCAmelCase_=37							,		lowerCAmelCase_="gelu"							,		lowerCAmelCase_=0.1							,		lowerCAmelCase_=0.1							,		lowerCAmelCase_=5_12							,		lowerCAmelCase_=16							,		lowerCAmelCase_=2							,		lowerCAmelCase_=0.02							,		lowerCAmelCase_=3							,		lowerCAmelCase_=4							,		lowerCAmelCase_=None							,		lowerCAmelCase_=0							,		):

       '''simple docstring'''

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


     def 							_lowerCAmelCase  (      self ):

       '''simple docstring'''

       a_      :	Union[str, Any]      =							ids_tensor([self.batch_size, self.seq_length]							,		self.vocab_size )

       a_      :	int      =							None
       if self.use_input_mask:
         # follow test_modeling_tf_ctrl.py
         a_      :	List[str]      =							random_attention_mask([self.batch_size, self.seq_length] )

       a_      :	Dict      =							None
       if self.use_token_type_ids:
         a_      :	List[str]      =							ids_tensor([self.batch_size, self.seq_length]							,		self.type_vocab_size )

       a_      :	int      =							None
       a_      :	List[Any]      =							None
       a_      :	int      =							None
       if self.use_labels:
         a_      :	List[str]      =							ids_tensor([self.batch_size]							,		self.type_sequence_label_size )
         a_      :	Union[str, Any]      =							ids_tensor([self.batch_size, self.seq_length]							,		self.num_labels )
         a_      :	int      =							ids_tensor([self.batch_size]							,		self.num_choices )

       a_      :	List[Any]      =							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							,		)
       a_      :	int      =							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 							_lowerCAmelCase  (      self							,		lowerCAmelCase_							,		lowerCAmelCase_							,		lowerCAmelCase_							,		lowerCAmelCase_							,		lowerCAmelCase_							,		lowerCAmelCase_							,		lowerCAmelCase_ ):

       '''simple docstring'''

       a_      :	Optional[int]      =							TFDPRContextEncoder(config=lowerCAmelCase_ )
       a_      :	List[str]      =							model(lowerCAmelCase_							,		attention_mask=lowerCAmelCase_							,		token_type_ids=lowerCAmelCase_ )
       a_      :	List[str]      =							model(lowerCAmelCase_							,		token_type_ids=lowerCAmelCase_ )
       a_      :	Optional[Any]      =							model(lowerCAmelCase_ )
       self.parent.assertEqual(result.pooler_output.shape							,		(self.batch_size, self.projection_dim or self.hidden_size) )


     def 							_lowerCAmelCase  (      self							,		lowerCAmelCase_							,		lowerCAmelCase_							,		lowerCAmelCase_							,		lowerCAmelCase_							,		lowerCAmelCase_							,		lowerCAmelCase_							,		lowerCAmelCase_ ):

       '''simple docstring'''

       a_      :	Dict      =							TFDPRQuestionEncoder(config=lowerCAmelCase_ )
       a_      :	int      =							model(lowerCAmelCase_							,		attention_mask=lowerCAmelCase_							,		token_type_ids=lowerCAmelCase_ )
       a_      :	List[str]      =							model(lowerCAmelCase_							,		token_type_ids=lowerCAmelCase_ )
       a_      :	Optional[int]      =							model(lowerCAmelCase_ )
       self.parent.assertEqual(result.pooler_output.shape							,		(self.batch_size, self.projection_dim or self.hidden_size) )


     def 							_lowerCAmelCase  (      self							,		lowerCAmelCase_							,		lowerCAmelCase_							,		lowerCAmelCase_							,		lowerCAmelCase_							,		lowerCAmelCase_							,		lowerCAmelCase_							,		lowerCAmelCase_ ):

       '''simple docstring'''

       a_      :	Dict      =							TFDPRReader(config=lowerCAmelCase_ )
       a_      :	str      =							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 							_lowerCAmelCase  (      self ):

       '''simple docstring'''

       a_      :	Tuple      =							self.prepare_config_and_inputs()
       (
           (
           a_
       )      , (
           a_
       )      , (
           a_
       )      , (
           a_
       )      , (
           a_
       )      , (
           a_
       )      , (
           a_
       )      , 
       )      :	List[Any]      =							config_and_inputs
       a_      :	Optional[Any]      =							{"""input_ids""": input_ids}
       return config, inputs_dict







@require_tf
class  _UpperCAmelCase ( lowerCAmelCase__	,lowerCAmelCase__	,unittest.TestCase    ):

     """simple docstring"""

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

     a_    	=					False
     a_    	=					False
     a_    	=					False
     a_    	=					False
     a_    	=					False


     def 							_lowerCAmelCase  (      self ):

       '''simple docstring'''

       a_      :	Any      =							TFDPRModelTester(self )
       a_      :	Any      =							ConfigTester(self							,		config_class=lowerCAmelCase_							,		hidden_size=37 )


     def 							_lowerCAmelCase  (      self ):

       '''simple docstring'''

       self.config_tester.run_common_tests()


     def 							_lowerCAmelCase  (      self ):

       '''simple docstring'''

       a_      :	int      =							self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_dpr_context_encoder(*lowerCAmelCase_ )


     def 							_lowerCAmelCase  (      self ):

       '''simple docstring'''

       a_      :	Tuple      =							self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_dpr_question_encoder(*lowerCAmelCase_ )


     def 							_lowerCAmelCase  (      self ):

       '''simple docstring'''

       a_      :	Dict      =							self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_dpr_reader(*lowerCAmelCase_ )






     @slow
     def 							_lowerCAmelCase  (      self ):

       '''simple docstring'''

       for model_name in TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
         a_      :	List[Any]      =							TFDPRContextEncoder.from_pretrained(lowerCAmelCase_ )
         self.assertIsNotNone(lowerCAmelCase_ )

       for model_name in TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
         a_      :	str      =							TFDPRContextEncoder.from_pretrained(lowerCAmelCase_ )
         self.assertIsNotNone(lowerCAmelCase_ )

       for model_name in TF_DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
         a_      :	Any      =							TFDPRQuestionEncoder.from_pretrained(lowerCAmelCase_ )
         self.assertIsNotNone(lowerCAmelCase_ )

       for model_name in TF_DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
         a_      :	Optional[int]      =							TFDPRReader.from_pretrained(lowerCAmelCase_ )
         self.assertIsNotNone(lowerCAmelCase_ )







@require_tf
class  _UpperCAmelCase ( unittest.TestCase    ):

     """simple docstring"""

     @slow
     def 							_lowerCAmelCase  (      self ):

       '''simple docstring'''

       a_      :	Union[str, Any]      =							TFDPRQuestionEncoder.from_pretrained("""facebook/dpr-question_encoder-single-nq-base""" )

       a_      :	int      =							tf.constant(
           [[1_01, 75_92, 10_10, 20_03, 20_26, 38_99, 1_01_40, 10_29, 1_02]] )  # [CLS] hello, is my dog cute? [SEP]
       a_      :	str      =							model(lowerCAmelCase_ )[0]  # embedding shape = (1, 768)
       # compare the actual values for a slice.
       a_      :	Optional[Any]      =							tf.constant(
           [
               [
                   0.03236253,
                   0.12753335,
                   0.16818509,
                   0.00279786,
                   0.3896933,
                   0.24264945,
                   0.2178971,
                   -0.02335227,
                   -0.08481959,
                   -0.14324117,
               ]
           ] )
       self.assertTrue(numpy.allclose(output[:, :10].numpy()							,		expected_slice.numpy()							,		atol=1E-4 ) )




'''simple docstring'''

import unittest

import numpy as np

from transformers.testing_utils import is_flaky, 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 DonutImageProcessor


class  _UpperCAmelCase ( unittest.TestCase    ):

     """simple docstring"""

     def __init__(      self							,		lowerCAmelCase_							,		lowerCAmelCase_=7							,		lowerCAmelCase_=3							,		lowerCAmelCase_=18							,		lowerCAmelCase_=30							,		lowerCAmelCase_=4_00							,		lowerCAmelCase_=True							,		lowerCAmelCase_=None							,		lowerCAmelCase_=True							,		lowerCAmelCase_=False							,		lowerCAmelCase_=True							,		lowerCAmelCase_=True							,		lowerCAmelCase_=[0.5, 0.5, 0.5]							,		lowerCAmelCase_=[0.5, 0.5, 0.5]							,		):

       '''simple docstring'''

       a_      :	Optional[int]      =							parent
       a_      :	Union[str, Any]      =							batch_size
       a_      :	Any      =							num_channels
       a_      :	Tuple      =							image_size
       a_      :	Any      =							min_resolution
       a_      :	Dict      =							max_resolution
       a_      :	Optional[Any]      =							do_resize
       a_      :	Tuple      =							size if size is not None else {"""height""": 18, """width""": 20}
       a_      :	int      =							do_thumbnail
       a_      :	Optional[int]      =							do_align_axis
       a_      :	Tuple      =							do_pad
       a_      :	int      =							do_normalize
       a_      :	Tuple      =							image_mean
       a_      :	Optional[Any]      =							image_std






     def 							_lowerCAmelCase  (      self ):

       '''simple docstring'''

       return {
           "do_resize": self.do_resize,
           "size": self.size,
           "do_thumbnail": self.do_thumbnail,
           "do_align_long_axis": self.do_align_axis,
           "do_pad": self.do_pad,
           "do_normalize": self.do_normalize,
           "image_mean": self.image_mean,
           "image_std": self.image_std,
       }







@require_torch
@require_vision
class  _UpperCAmelCase ( lowerCAmelCase__	,unittest.TestCase    ):

     """simple docstring"""

     a_    	=					DonutImageProcessor if is_vision_available() else None


     def 							_lowerCAmelCase  (      self ):

       '''simple docstring'''

       a_      :	Optional[int]      =							DonutImageProcessingTester(self )


     @property
     def 							_lowerCAmelCase  (      self ):

       '''simple docstring'''

       return self.image_processor_tester.prepare_image_processor_dict()


     def 							_lowerCAmelCase  (      self ):

       '''simple docstring'''

       a_      :	Any      =							self.image_processing_class(**self.image_processor_dict )
       self.assertTrue(hasattr(lowerCAmelCase_							,		"""do_resize""" ) )
       self.assertTrue(hasattr(lowerCAmelCase_							,		"""size""" ) )
       self.assertTrue(hasattr(lowerCAmelCase_							,		"""do_thumbnail""" ) )
       self.assertTrue(hasattr(lowerCAmelCase_							,		"""do_align_long_axis""" ) )
       self.assertTrue(hasattr(lowerCAmelCase_							,		"""do_pad""" ) )
       self.assertTrue(hasattr(lowerCAmelCase_							,		"""do_normalize""" ) )
       self.assertTrue(hasattr(lowerCAmelCase_							,		"""image_mean""" ) )
       self.assertTrue(hasattr(lowerCAmelCase_							,		"""image_std""" ) )


     def 							_lowerCAmelCase  (      self ):

       '''simple docstring'''

       a_      :	List[str]      =							self.image_processing_class.from_dict(self.image_processor_dict )
       self.assertEqual(image_processor.size							,		{"""height""": 18, """width""": 20} )

       a_      :	str      =							self.image_processing_class.from_dict(self.image_processor_dict							,		size=42 )
       self.assertEqual(image_processor.size							,		{"""height""": 42, """width""": 42} )

       # Previous config had dimensions in (width, height) order
       a_      :	Any      =							self.image_processing_class.from_dict(self.image_processor_dict							,		size=(42, 84) )
       self.assertEqual(image_processor.size							,		{"""height""": 84, """width""": 42} )


     def 							_lowerCAmelCase  (      self ):

       '''simple docstring'''

       pass


     @is_flaky()
     def 							_lowerCAmelCase  (      self ):

       '''simple docstring'''

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

       # Test not batched input
       a_      :	Dict      =							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.size["""height"""],
               self.image_processor_tester.size["""width"""],
           )							,		)

       # Test batched
       a_      :	int      =							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.size["""height"""],
               self.image_processor_tester.size["""width"""],
           )							,		)


     @is_flaky()
     def 							_lowerCAmelCase  (      self ):

       '''simple docstring'''

       a_      :	List[str]      =							self.image_processing_class(**self.image_processor_dict )
       # create random numpy tensors
       a_      :	int      =							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
       a_      :	Optional[Any]      =							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.size["""height"""],
               self.image_processor_tester.size["""width"""],
           )							,		)

       # Test batched
       a_      :	Dict      =							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.size["""height"""],
               self.image_processor_tester.size["""width"""],
           )							,		)






     @is_flaky()
     def 							_lowerCAmelCase  (      self ):

       '''simple docstring'''

       a_      :	Union[str, Any]      =							self.image_processing_class(**self.image_processor_dict )
       # create random PyTorch tensors
       a_      :	Optional[Any]      =							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
       a_      :	Optional[int]      =							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.size["""height"""],
               self.image_processor_tester.size["""width"""],
           )							,		)

       # Test batched
       a_      :	Any      =							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.size["""height"""],
               self.image_processor_tester.size["""width"""],
           )							,		)


