

from __future__ import annotations

import inspect
import unittest
from math import floor

import numpy as np

from transformers import CvtConfig
from transformers.testing_utils import require_tf, require_vision, slow
from transformers.utils import cached_property, is_tf_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
							import tensorflow as tf

							from transformers import TFCvtForImageClassification, TFCvtModel
							from transformers.models.cvt.modeling_tf_cvt import TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
							from PIL import Image

							from transformers import AutoImageProcessor



class       __snake_case  (    lowercase_       ):

		def 						lowerCAmelCase__     (					self):
						SCREAMING_SNAKE_CASE_									=						self.config_class(**self.inputs_dict)
						self.parent.assertTrue(hasattr(_A						,		'embed_dim'))
						self.parent.assertTrue(hasattr(_A						,		'num_heads'))






class       __snake_case  :

		def __init__(					self						,		_A						,		_A=13						,		_A=64						,		_A=3						,		_A=[16, 48, 96]						,		_A=[1, 3, 6]						,		_A=[1, 2, 10]						,		_A=[7, 3, 3]						,		_A=[4, 2, 2]						,		_A=[2, 1, 1]						,		_A=[2, 2, 2]						,		_A=[False, False, True]						,		_A=[0.0, 0.0, 0.0]						,		_A=0.0_2						,		_A=1E-12						,		_A=True						,		_A=True						,		_A=2						,		):
						SCREAMING_SNAKE_CASE_									=						parent
						SCREAMING_SNAKE_CASE_									=						batch_size
						SCREAMING_SNAKE_CASE_									=						image_size
						SCREAMING_SNAKE_CASE_									=						patch_sizes
						SCREAMING_SNAKE_CASE_									=						patch_stride
						SCREAMING_SNAKE_CASE_									=						patch_padding
						SCREAMING_SNAKE_CASE_									=						is_training
						SCREAMING_SNAKE_CASE_									=						use_labels
						SCREAMING_SNAKE_CASE_									=						num_labels
						SCREAMING_SNAKE_CASE_									=						num_channels
						SCREAMING_SNAKE_CASE_									=						embed_dim
						SCREAMING_SNAKE_CASE_									=						num_heads
						SCREAMING_SNAKE_CASE_									=						stride_kv
						SCREAMING_SNAKE_CASE_									=						depth
						SCREAMING_SNAKE_CASE_									=						cls_token
						SCREAMING_SNAKE_CASE_									=						attention_drop_rate
						SCREAMING_SNAKE_CASE_									=						initializer_range
						SCREAMING_SNAKE_CASE_									=						layer_norm_eps

		def 						lowerCAmelCase__     (					self):
						SCREAMING_SNAKE_CASE_									=						floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size])

						SCREAMING_SNAKE_CASE_									=						None
						if self.use_labels:
										# create a random int32 tensor of given shape
										SCREAMING_SNAKE_CASE_									=						ids_tensor([self.batch_size]						,		self.num_labels)

						SCREAMING_SNAKE_CASE_									=						self.get_config()
						return config, pixel_values, labels

		def 						lowerCAmelCase__     (					self):
						return CvtConfig(
						    image_size=self.image_size						,		num_labels=self.num_labels						,		num_channels=self.num_channels						,		embed_dim=self.embed_dim						,		num_heads=self.num_heads						,		patch_sizes=self.patch_sizes						,		patch_padding=self.patch_padding						,		patch_stride=self.patch_stride						,		stride_kv=self.stride_kv						,		depth=self.depth						,		cls_token=self.cls_token						,		attention_drop_rate=self.attention_drop_rate						,		initializer_range=self.initializer_range						,		)

		def 						lowerCAmelCase__     (					self						,		_A						,		_A						,		_A):
						SCREAMING_SNAKE_CASE_									=						TFCvtModel(config=_A)
						SCREAMING_SNAKE_CASE_									=						model(_A						,		training=_A)
						SCREAMING_SNAKE_CASE_									=						(self.image_size, self.image_size)
						SCREAMING_SNAKE_CASE_							,			SCREAMING_SNAKE_CASE_									=						image_size[0], image_size[1]
						for i in range(len(self.depth)):
										SCREAMING_SNAKE_CASE_									=						floor(((height + 2 * self.patch_padding[i] - self.patch_sizes[i]) / self.patch_stride[i]) + 1)
										SCREAMING_SNAKE_CASE_									=						floor(((width + 2 * self.patch_padding[i] - self.patch_sizes[i]) / self.patch_stride[i]) + 1)
						self.parent.assertEqual(result.last_hidden_state.shape						,		(self.batch_size, self.embed_dim[-1], height, width))

		def 						lowerCAmelCase__     (					self						,		_A						,		_A						,		_A):
						SCREAMING_SNAKE_CASE_									=						self.num_labels
						SCREAMING_SNAKE_CASE_									=						TFCvtForImageClassification(_A)
						SCREAMING_SNAKE_CASE_									=						model(_A						,		labels=_A						,		training=_A)
						self.parent.assertEqual(result.logits.shape						,		(self.batch_size, self.num_labels))


		def 						lowerCAmelCase__     (					self):
						SCREAMING_SNAKE_CASE_									=						self.prepare_config_and_inputs()
						SCREAMING_SNAKE_CASE_							,			SCREAMING_SNAKE_CASE_							,			SCREAMING_SNAKE_CASE_									=						config_and_inputs
						SCREAMING_SNAKE_CASE_									=						{'pixel_values': pixel_values}
						return config, inputs_dict






@require_tf
class       __snake_case  (    lowercase_     , lowercase_     , unittest.TestCase       ):
		__lowerCAmelCase     :	Union[str, Any]						 =  (TFCvtModel, TFCvtForImageClassification) if is_tf_available() else ()
		__lowerCAmelCase     :	Dict						 =  (
		    {'''feature-extraction''': TFCvtModel, '''image-classification''': TFCvtForImageClassification}
		    if is_tf_available()
		    else {}
		)
		__lowerCAmelCase     :	Union[str, Any]						 =  False
		__lowerCAmelCase     :	str						 =  False
		__lowerCAmelCase     :	Optional[int]						 =  False
		__lowerCAmelCase     :	Dict						 =  False
		__lowerCAmelCase     :	List[Any]						 =  False

		def 						lowerCAmelCase__     (					self):
						SCREAMING_SNAKE_CASE_									=						TFCvtModelTester(self)
						SCREAMING_SNAKE_CASE_									=						TFCvtConfigTester(self						,		config_class=_A						,		has_text_modality=_A						,		hidden_size=37)

		def 						lowerCAmelCase__     (					self):
						self.config_tester.create_and_test_config_common_properties()
						self.config_tester.create_and_test_config_to_json_string()
						self.config_tester.create_and_test_config_to_json_file()
						self.config_tester.create_and_test_config_from_and_save_pretrained()
						self.config_tester.create_and_test_config_with_num_labels()
						self.config_tester.check_config_can_be_init_without_params()
						self.config_tester.check_config_arguments_init()

		@unittest.skip(reason='Cvt does not output attentions')
		def 						lowerCAmelCase__     (					self):
						pass

		@unittest.skip(reason='Cvt does not use inputs_embeds')
		def 						lowerCAmelCase__     (					self):
						pass

		@unittest.skip(reason='Cvt does not support input and output embeddings')
		def 						lowerCAmelCase__     (					self):
						pass

		@unittest.skipIf(
		    not is_tf_available() or len(tf.config.list_physical_devices('GPU')) == 0						,		reason='TF does not support backprop for grouped convolutions on CPU.'						,		)
		def 						lowerCAmelCase__     (					self):
						super().test_dataset_conversion()

		@unittest.skipIf(
		    not is_tf_available() or len(tf.config.list_physical_devices('GPU')) == 0						,		reason='TF does not support backprop for grouped convolutions on CPU.'						,		)
		@slow
		def 						lowerCAmelCase__     (					self):
						super().test_keras_fit()

		@unittest.skip(reason='Get `Failed to determine best cudnn convolution algo.` error after using TF 2.12+cuda 11.8')
		def 						lowerCAmelCase__     (					self):
						SCREAMING_SNAKE_CASE_									=						tf.keras.mixed_precision.Policy('mixed_float16')
						tf.keras.mixed_precision.set_global_policy(_A)
						super().test_keras_fit()
						tf.keras.mixed_precision.set_global_policy('float32')

		def 						lowerCAmelCase__     (					self):
						SCREAMING_SNAKE_CASE_							,			SCREAMING_SNAKE_CASE_									=						self.model_tester.prepare_config_and_inputs_for_common()

						for model_class in self.all_model_classes:
										SCREAMING_SNAKE_CASE_									=						model_class(_A)
										SCREAMING_SNAKE_CASE_									=						inspect.signature(model.call)
										# signature.parameters is an OrderedDict => so arg_names order is deterministic
										SCREAMING_SNAKE_CASE_									=						[*signature.parameters.keys()]

										SCREAMING_SNAKE_CASE_									=						['pixel_values']
										self.assertListEqual(arg_names[:1]						,		_A)

		def 						lowerCAmelCase__     (					self):
						def check_hidden_states_output(_A						,		_A						,		_A):
										SCREAMING_SNAKE_CASE_									=						model_class(_A)

										SCREAMING_SNAKE_CASE_									=						model(**self._prepare_for_class(_A						,		_A))
										SCREAMING_SNAKE_CASE_									=						outputs.hidden_states

										SCREAMING_SNAKE_CASE_									=						len(self.model_tester.depth)
										self.assertEqual(len(_A)						,		_A)

										# verify the first hidden states (first block)
										self.assertListEqual(
										    list(hidden_states[0].shape[-3:])						,		[
										        self.model_tester.embed_dim[0],
										        self.model_tester.image_size // 4,
										        self.model_tester.image_size // 4,
										    ]						,		)

						SCREAMING_SNAKE_CASE_							,			SCREAMING_SNAKE_CASE_									=						self.model_tester.prepare_config_and_inputs_for_common()

						for model_class in self.all_model_classes:
										SCREAMING_SNAKE_CASE_									=						True
										check_hidden_states_output(_A						,		_A						,		_A)

										# check that output_hidden_states also work using config
										del inputs_dict["output_hidden_states"]
										SCREAMING_SNAKE_CASE_									=						True

										check_hidden_states_output(_A						,		_A						,		_A)

		def 						lowerCAmelCase__     (					self):
						SCREAMING_SNAKE_CASE_									=						self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_model(*_A)

		def 						lowerCAmelCase__     (					self):
						SCREAMING_SNAKE_CASE_									=						self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_image_classification(*_A)


		@slow
		def 						lowerCAmelCase__     (					self):
						for model_name in TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
										SCREAMING_SNAKE_CASE_									=						TFCvtModel.from_pretrained(_A)
										self.assertIsNotNone(_A)






def 					_UpperCAmelCase       (  ):





				"""simple docstring"""


				SCREAMING_SNAKE_CASE_									=						Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'       )
				return image





@require_tf
@require_vision
class       __snake_case  (    unittest.TestCase       ):

		@cached_property
		def 						lowerCAmelCase__     (					self):
						return AutoImageProcessor.from_pretrained(TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0])


		@slow
		def 						lowerCAmelCase__     (					self):
						SCREAMING_SNAKE_CASE_									=						TFCvtForImageClassification.from_pretrained(TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0])

						SCREAMING_SNAKE_CASE_									=						self.default_image_processor
						SCREAMING_SNAKE_CASE_									=						prepare_img()
						SCREAMING_SNAKE_CASE_									=						image_processor(images=_A						,		return_tensors='tf')

						# forward pass
						SCREAMING_SNAKE_CASE_									=						model(**_A)

						# verify the logits
						SCREAMING_SNAKE_CASE_									=						tf.TensorShape((1, 1000))
						self.assertEqual(outputs.logits.shape						,		_A)

						SCREAMING_SNAKE_CASE_									=						tf.constant([0.9_2_8_5, 0.9_0_1_5, -0.3_1_5_0])
						self.assertTrue(np.allclose(outputs.logits[0, :3].numpy()						,		_A						,		atol=1E-4))





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


UpperCamelCase__  :      str			=    logging.get_logger(__name__)

UpperCamelCase__  :      Optional[int]			=    {
    "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       __snake_case  (    lowerCAmelCase__       ):
  __lowerCAmelCase     :	Optional[int]						 =  'dpr'

  def __init__(					self						,		_A=30522						,		_A=768						,		_A=12						,		_A=12						,		_A=3072						,		_A="gelu"						,		_A=0.1						,		_A=0.1						,		_A=512						,		_A=2						,		_A=0.0_2						,		_A=1E-12						,		_A=0						,		_A="absolute"						,		_A = 0						,		**_A						,		):
      super().__init__(pad_token_id=_A						,		**_A)

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


