



'''simple docstring'''

import unittest

import numpy as np
import requests

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

							from transformers.pytorch_utils import is_torch_greater_or_equal_than_1_11
else:
							__lowerCAmelCase									=   False

if is_vision_available():
							from PIL import Image

							from transformers import PixaStructImageProcessor

class 	__magic_name__						(    unittest.TestCase   ):







							def __init__(							self					:       Tuple	,_UpperCAmelCase					:       str	,_UpperCAmelCase					:       List[str]=7	,_UpperCAmelCase					:       int=3	,_UpperCAmelCase					:       Dict=18	,_UpperCAmelCase					:       List[str]=30	,_UpperCAmelCase					:       Dict=400	,_UpperCAmelCase					:       Tuple=None	,_UpperCAmelCase					:       str=True	,_UpperCAmelCase					:       List[Any]=True	,_UpperCAmelCase					:       Tuple=None	,):
									_a     :      int        =							size if size is not None else {'height': 20, 'width': 20}
									_a     :      List[str]        =							parent
									_a     :      Any        =							batch_size
									_a     :      Optional[int]        =							num_channels
									_a     :      Tuple        =							image_size
									_a     :      int        =							min_resolution
									_a     :      List[Any]        =							max_resolution
									_a     :      Union[str, Any]        =							size
									_a     :      Dict        =							do_normalize
									_a     :      List[str]        =							do_convert_rgb
									_a     :      str        =							[512, 1024, 2048, 4096]
									_a     :      Union[str, Any]        =							patch_size if patch_size is not None else {'height': 16, 'width': 16}







							def 							__lowercase			(							self					:       str		):
									return {"do_normalize": self.do_normalize, "do_convert_rgb": self.do_convert_rgb}







							def 							__lowercase			(							self					:       Optional[Any]		):
									_a     :      List[str]        =							'https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/australia.jpg'
									_a     :      Union[str, Any]        =							Image.open(requests.get(__lowerCAmelCase	,stream=__lowerCAmelCase		).raw		).convert('RGB'		)
									return raw_image

@unittest.skipIf(
    not is_torch_greater_or_equal_than_1_11			,						reason='`Pix2StructImageProcessor` requires `torch>=1.11.0`.'			,						)
@require_torch
@require_vision
class 	__magic_name__						(    _UpperCamelCase			,						unittest.TestCase   ):
							lowerCAmelCase       :    Any   				=					PixaStructImageProcessor if is_vision_available() else None







							def 							__lowercase			(							self					:       Optional[Any]		):
									_a     :      Optional[int]        =							PixaStructImageProcessingTester(self		)







							@property
							def 							__lowercase			(							self					:       int		):
									return self.image_processor_tester.prepare_image_processor_dict()







							def 							__lowercase			(							self					:       str		):
									_a     :      Tuple        =							self.image_processing_class(**self.image_processor_dict		)
									self.assertTrue(hasattr(__lowerCAmelCase	,'do_normalize'		)		)
									self.assertTrue(hasattr(__lowerCAmelCase	,'do_convert_rgb'		)		)







							def 							__lowercase			(							self					:       str		):
									_a     :      Tuple        =							self.image_processor_tester.prepare_dummy_image()

									_a     :      int        =							self.image_processing_class(**self.image_processor_dict		)
									_a     :      Optional[int]        =							2048

									_a     :      int        =							image_processor(__lowerCAmelCase	,return_tensors='pt'	,max_patches=__lowerCAmelCase		)
									self.assertTrue(torch.allclose(inputs.flattened_patches.mean()	,torch.tensor(0.06_06		)	,atol=1E-3	,rtol=1E-3		)		)







							def 							__lowercase			(							self					:       Optional[int]		):
									_a     :      Optional[Any]        =							self.image_processing_class(**self.image_processor_dict		)
									# create random PIL images
									_a     :      int        =							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     :      Tuple        =							(
									    (self.image_processor_tester.patch_size['height'] * self.image_processor_tester.patch_size['width'])
									    * self.image_processor_tester.num_channels
									) + 2

									for max_patch in self.image_processor_tester.max_patches:
											# Test not batched input
											_a     :      Dict        =							image_processor(
											    image_inputs[0]	,return_tensors='pt'	,max_patches=__lowerCAmelCase		).flattened_patches
											self.assertEqual(
											    encoded_images.shape	,(1, max_patch, expected_hidden_dim)	,)

											# Test batched
											_a     :      List[str]        =							image_processor(
											    __lowerCAmelCase	,return_tensors='pt'	,max_patches=__lowerCAmelCase		).flattened_patches
											self.assertEqual(
											    encoded_images.shape	,(self.image_processor_tester.batch_size, max_patch, expected_hidden_dim)	,)







							def 							__lowercase			(							self					:       str		):
									_a     :      List[str]        =							self.image_processing_class(**self.image_processor_dict		)
									# create random PIL images
									_a     :      List[Any]        =							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     :      str        =							(
									    (self.image_processor_tester.patch_size['height'] * self.image_processor_tester.patch_size['width'])
									    * self.image_processor_tester.num_channels
									) + 2

									_a     :      Optional[int]        =							True

									for max_patch in self.image_processor_tester.max_patches:
											# Test not batched input
											with self.assertRaises(__lowerCAmelCase		):
													_a     :      Optional[Any]        =							image_processor(
													    image_inputs[0]	,return_tensors='pt'	,max_patches=__lowerCAmelCase		).flattened_patches

											_a     :      Optional[int]        =							'Hello'

											_a     :      Tuple        =							image_processor(
											    image_inputs[0]	,return_tensors='pt'	,max_patches=__lowerCAmelCase	,header_text=__lowerCAmelCase		).flattened_patches
											self.assertEqual(
											    encoded_images.shape	,(1, max_patch, expected_hidden_dim)	,)

											# Test batched
											_a     :      Dict        =							image_processor(
											    __lowerCAmelCase	,return_tensors='pt'	,max_patches=__lowerCAmelCase	,header_text=__lowerCAmelCase		).flattened_patches
											self.assertEqual(
											    encoded_images.shape	,(self.image_processor_tester.batch_size, max_patch, expected_hidden_dim)	,)







							def 							__lowercase			(							self					:       List[Any]		):
									_a     :      List[Any]        =							self.image_processing_class(**self.image_processor_dict		)
									# create random numpy tensors
									_a     :      List[str]        =							prepare_image_inputs(self.image_processor_tester	,equal_resolution=__lowerCAmelCase	,numpify=__lowerCAmelCase		)
									for image in image_inputs:
											self.assertIsInstance(__lowerCAmelCase	,np.ndarray		)

									_a     :      Any        =							(
									    (self.image_processor_tester.patch_size['height'] * self.image_processor_tester.patch_size['width'])
									    * self.image_processor_tester.num_channels
									) + 2

									for max_patch in self.image_processor_tester.max_patches:
											# Test not batched input
											_a     :      Dict        =							image_processor(
											    image_inputs[0]	,return_tensors='pt'	,max_patches=__lowerCAmelCase		).flattened_patches
											self.assertEqual(
											    encoded_images.shape	,(1, max_patch, expected_hidden_dim)	,)

											# Test batched
											_a     :      str        =							image_processor(
											    __lowerCAmelCase	,return_tensors='pt'	,max_patches=__lowerCAmelCase		).flattened_patches
											self.assertEqual(
											    encoded_images.shape	,(self.image_processor_tester.batch_size, max_patch, expected_hidden_dim)	,)







							def 							__lowercase			(							self					:       Dict		):
									_a     :      Optional[int]        =							self.image_processing_class(**self.image_processor_dict		)
									# create random PyTorch tensors
									_a     :      str        =							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[Any]        =							(
									    (self.image_processor_tester.patch_size['height'] * self.image_processor_tester.patch_size['width'])
									    * self.image_processor_tester.num_channels
									) + 2

									for max_patch in self.image_processor_tester.max_patches:
											# Test not batched input
											_a     :      str        =							image_processor(
											    image_inputs[0]	,return_tensors='pt'	,max_patches=__lowerCAmelCase		).flattened_patches
											self.assertEqual(
											    encoded_images.shape	,(1, max_patch, expected_hidden_dim)	,)

											# Test batched
											_a     :      Dict        =							image_processor(
											    __lowerCAmelCase	,return_tensors='pt'	,max_patches=__lowerCAmelCase		).flattened_patches
											self.assertEqual(
											    encoded_images.shape	,(self.image_processor_tester.batch_size, max_patch, expected_hidden_dim)	,)

@unittest.skipIf(
    not is_torch_greater_or_equal_than_1_11			,						reason='`Pix2StructImageProcessor` requires `torch>=1.11.0`.'			,						)
@require_torch
@require_vision
class 	__magic_name__						(    _UpperCamelCase			,						unittest.TestCase   ):
							lowerCAmelCase       :    Tuple   				=					PixaStructImageProcessor if is_vision_available() else None







							def 							__lowercase			(							self					:       int		):
									_a     :      Any        =							PixaStructImageProcessingTester(self	,num_channels=4		)
									_a     :      List[str]        =							3







							@property
							def 							__lowercase			(							self					:       str		):
									return self.image_processor_tester.prepare_image_processor_dict()







							def 							__lowercase			(							self					:       Union[str, Any]		):
									_a     :      Dict        =							self.image_processing_class(**self.image_processor_dict		)
									self.assertTrue(hasattr(__lowerCAmelCase	,'do_normalize'		)		)
									self.assertTrue(hasattr(__lowerCAmelCase	,'do_convert_rgb'		)		)







							def 							__lowercase			(							self					:       Optional[int]		):
									_a     :      Dict        =							self.image_processing_class(**self.image_processor_dict		)
									# create random PIL images
									_a     :      Dict        =							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     :      Optional[Any]        =							(
									    (self.image_processor_tester.patch_size['height'] * self.image_processor_tester.patch_size['width'])
									    * (self.image_processor_tester.num_channels - 1)
									) + 2

									for max_patch in self.image_processor_tester.max_patches:
											# Test not batched input
											_a     :      Any        =							image_processor(
											    image_inputs[0]	,return_tensors='pt'	,max_patches=__lowerCAmelCase		).flattened_patches
											self.assertEqual(
											    encoded_images.shape	,(1, max_patch, expected_hidden_dim)	,)

											# Test batched
											_a     :      str        =							image_processor(
											    __lowerCAmelCase	,return_tensors='pt'	,max_patches=__lowerCAmelCase		).flattened_patches
											self.assertEqual(
											    encoded_images.shape	,(self.image_processor_tester.batch_size, max_patch, expected_hidden_dim)	,)





"""simple docstring"""






from ...configuration_utils import PretrainedConfig
from ...utils import logging


__lowerCAmelCase    :       List[str]					     =logging.get_logger(__name__)

__lowerCAmelCase    :       str					     ={
    """facebook/dpr-ctx_encoder-single-nq-base""": (
        """https://huggingface.co/facebook/dpr-ctx_encoder-single-nq-base/resolve/main/config.json"""
    ),
    """facebook/dpr-question_encoder-single-nq-base""": (
        """https://huggingface.co/facebook/dpr-question_encoder-single-nq-base/resolve/main/config.json"""
    ),
    """facebook/dpr-reader-single-nq-base""": (
        """https://huggingface.co/facebook/dpr-reader-single-nq-base/resolve/main/config.json"""
    ),
    """facebook/dpr-ctx_encoder-multiset-base""": (
        """https://huggingface.co/facebook/dpr-ctx_encoder-multiset-base/resolve/main/config.json"""
    ),
    """facebook/dpr-question_encoder-multiset-base""": (
        """https://huggingface.co/facebook/dpr-question_encoder-multiset-base/resolve/main/config.json"""
    ),
    """facebook/dpr-reader-multiset-base""": (
        """https://huggingface.co/facebook/dpr-reader-multiset-base/resolve/main/config.json"""
    ),
}





class  _A						(       lowerCAmelCase	):
      snake_case__			:   Tuple							  =	'dpr'

      def __init__(						self		,       __lowerCAmelCase=3_0522		,       __lowerCAmelCase=768		,       __lowerCAmelCase=12		,       __lowerCAmelCase=12		,       __lowerCAmelCase=3072		,       __lowerCAmelCase="gelu"		,       __lowerCAmelCase=0.1		,       __lowerCAmelCase=0.1		,       __lowerCAmelCase=512		,       __lowerCAmelCase=2		,       __lowerCAmelCase=0.0_2		,       __lowerCAmelCase=1E-12		,       __lowerCAmelCase=0		,       __lowerCAmelCase="absolute"		,       __lowerCAmelCase = 0		,       **__lowerCAmelCase		,       ):







            """simple docstring"""



            super().__init__(pad_token_id=__lowerCAmelCase		,       **__lowerCAmelCase						)

            lowercase														=	vocab_size
            lowercase														=	hidden_size
            lowercase														=	num_hidden_layers
            lowercase														=	num_attention_heads
            lowercase														=	hidden_act
            lowercase														=	intermediate_size
            lowercase														=	hidden_dropout_prob
            lowercase														=	attention_probs_dropout_prob
            lowercase														=	max_position_embeddings
            lowercase														=	type_vocab_size
            lowercase														=	initializer_range
            lowercase														=	layer_norm_eps
            lowercase														=	projection_dim
            lowercase														=	position_embedding_type



