
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


if is_torch_available():
			import torch

if is_vision_available():
			from PIL import Image

			from transformers import ChineseCLIPImageProcessor






class       __A					(					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.4814_5466, 0.457_8275, 0.4082_1073] ,						__lowerCAmelCase=[0.2686_2954, 0.2613_0258, 0.2757_7711] ,						__lowerCAmelCase=True ,						):



				'''simple docstring'''







				lowerCamelCase__              =	size if size is not None else {"height": 2_2_4, "width": 2_2_4}
				lowerCamelCase__              =	crop_size if crop_size is not None else {"height": 1_8, "width": 1_8}
				lowerCamelCase__              =	parent
				lowerCamelCase__              =	batch_size
				lowerCamelCase__              =	num_channels
				lowerCamelCase__              =	image_size
				lowerCamelCase__              =	min_resolution
				lowerCamelCase__              =	max_resolution
				lowerCamelCase__              =	do_resize
				lowerCamelCase__              =	size
				lowerCamelCase__              =	do_center_crop
				lowerCamelCase__              =	crop_size
				lowerCamelCase__              =	do_normalize
				lowerCamelCase__              =	image_mean
				lowerCamelCase__              =	image_std
				lowerCamelCase__              =	do_convert_rgb





			def 	__lowerCamelCase       (					self							):



				'''simple docstring'''







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





			def 	__lowerCamelCase       (					self ,						__lowerCAmelCase=False ,						__lowerCAmelCase=False ,						__lowerCAmelCase=False							):



				'''simple docstring'''







				assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time"

				if equal_resolution:
					lowerCamelCase__              =	[]
					for i in range(self.batch_size							):
						image_inputs.append(
						    np.random.randint(
						        2_5_5 ,						size=(self.num_channels, self.max_resolution, self.max_resolution) ,						dtype=np.uinta							)							)
				else:
					lowerCamelCase__              =	[]
					for i in range(self.batch_size							):
						lowerCamelCase__              =	np.random.choice(np.arange(self.min_resolution ,						self.max_resolution							) ,						2							)
						image_inputs.append(np.random.randint(2_5_5 ,						size=(self.num_channels, width, height) ,						dtype=np.uinta							)							)

				if not numpify and not torchify:
					# PIL expects the channel dimension as last dimension
					lowerCamelCase__              =	[Image.fromarray(np.moveaxis(lowercase__ ,						0 ,						-1							)							) for x in image_inputs]

				if torchify:
					lowerCamelCase__              =	[torch.from_numpy(lowercase__							) for x in image_inputs]

				return image_inputs






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

			'''simple docstring'''







			lowerCAmelCase_							  =		ChineseCLIPImageProcessor if is_vision_available() else None





			def 	__lowerCamelCase       (					self							):



				'''simple docstring'''







				lowerCamelCase__              =	ChineseCLIPImageProcessingTester(self ,						do_center_crop=lowercase__							)





			@property
			def 	__lowerCamelCase       (					self							):



				'''simple docstring'''







				return self.image_processor_tester.prepare_image_processor_dict()





			def 	__lowerCamelCase       (					self							):



				'''simple docstring'''







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





			def 	__lowerCamelCase       (					self							):



				'''simple docstring'''







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

				lowerCamelCase__              =	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 	__lowerCamelCase       (					self							):



				'''simple docstring'''







				pass





			def 	__lowerCamelCase       (					self							):



				'''simple docstring'''







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

				# Test not batched input
				lowerCamelCase__              =	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
				lowerCamelCase__              =	image_processing(lowercase__ ,						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 	__lowerCamelCase       (					self							):



				'''simple docstring'''







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

				# Test not batched input
				lowerCamelCase__              =	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
				lowerCamelCase__              =	image_processing(lowercase__ ,						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 	__lowerCamelCase       (					self							):



				'''simple docstring'''







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

				# Test not batched input
				lowerCamelCase__              =	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
				lowerCamelCase__              =	image_processing(lowercase__ ,						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'''],
				    ) ,						)






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

			'''simple docstring'''







			lowerCAmelCase_							  =		ChineseCLIPImageProcessor if is_vision_available() else None





			def 	__lowerCamelCase       (					self							):



				'''simple docstring'''







				lowerCamelCase__              =	ChineseCLIPImageProcessingTester(self ,						num_channels=4 ,						do_center_crop=lowercase__							)
				lowerCamelCase__              =	3





			@property
			def 	__lowerCamelCase       (					self							):



				'''simple docstring'''







				return self.image_processor_tester.prepare_image_processor_dict()





			def 	__lowerCamelCase       (					self							):



				'''simple docstring'''







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





			def 	__lowerCamelCase       (					self							):



				'''simple docstring'''







				pass





			def 	__lowerCamelCase       (					self							):



				'''simple docstring'''







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

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

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










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

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







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





			def 	__lowerCamelCase       (					self							):



				'''simple docstring'''







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

				lowerCamelCase__              =	None
				if self.use_input_mask:
					# follow test_modeling_tf_ctrl.py
					lowerCamelCase__              =	random_attention_mask([self.batch_size, self.seq_length]							)

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

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

				lowerCamelCase__              =	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 ,						)
				lowerCamelCase__              =	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'''







				lowerCamelCase__              =	TFDPRContextEncoder(config=__lowerCAmelCase							)
				lowerCamelCase__              =	model(__lowerCAmelCase ,						attention_mask=__lowerCAmelCase ,						token_type_ids=__lowerCAmelCase							)
				lowerCamelCase__              =	model(__lowerCAmelCase ,						token_type_ids=__lowerCAmelCase							)
				lowerCamelCase__              =	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'''







				lowerCamelCase__              =	TFDPRQuestionEncoder(config=__lowerCAmelCase							)
				lowerCamelCase__              =	model(__lowerCAmelCase ,						attention_mask=__lowerCAmelCase ,						token_type_ids=__lowerCAmelCase							)
				lowerCamelCase__              =	model(__lowerCAmelCase ,						token_type_ids=__lowerCAmelCase							)
				lowerCamelCase__              =	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'''







				lowerCamelCase__              =	TFDPRReader(config=__lowerCAmelCase							)
				lowerCamelCase__              =	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'''







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






@require_tf
class       __A					(					lowerCAmelCase    ,       lowerCAmelCase    ,       unittest.TestCase				):

			'''simple docstring'''







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

			lowerCAmelCase_							  =		False
			lowerCAmelCase_							  =		False
			lowerCAmelCase_							  =		False
			lowerCAmelCase_							  =		False
			lowerCAmelCase_							  =		False





			def 	__lowerCamelCase       (					self							):



				'''simple docstring'''







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





			def 	__lowerCamelCase       (					self							):



				'''simple docstring'''







				self.config_tester.run_common_tests()





			def 	__lowerCamelCase       (					self							):



				'''simple docstring'''







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





			def 	__lowerCamelCase       (					self							):



				'''simple docstring'''







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





			def 	__lowerCamelCase       (					self							):



				'''simple docstring'''







				lowerCamelCase__              =	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]:
					lowerCamelCase__              =	TFDPRContextEncoder.from_pretrained(__lowerCAmelCase							)
					self.assertIsNotNone(__lowerCAmelCase							)

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

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

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






@require_tf
class       __A					(					unittest.TestCase				):

			'''simple docstring'''







			@slow
			def 	__lowerCamelCase       (					self							):



				'''simple docstring'''







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

				lowerCamelCase__              =	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]
				lowerCamelCase__              =	model(__lowerCAmelCase							)[0]  # embedding shape = (1, 768)
				# compare the actual values for a slice.
				lowerCamelCase__              =	tf.constant(
				    [
				        [
				            0.0323_6253,
				            0.1275_3335,
				            0.1681_8509,
				            0.0027_9786,
				            0.389_6933,
				            0.2426_4945,
				            0.217_8971,
				            -0.0233_5227,
				            -0.0848_1959,
				            -0.1432_4117,
				        ]
				    ]							)
				self.assertTrue(numpy.allclose(output[:, :1_0].numpy() ,						expected_slice.numpy() ,						atol=1E-4							)							)



