






"""simple docstring"""
from __future__ import annotations

import inspect
import unittest

import numpy as np

from transformers import DeiTConfig
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 (
			    TFDeiTForImageClassification,
			    TFDeiTForImageClassificationWithTeacher,
			    TFDeiTForMaskedImageModeling,
			    TFDeiTModel,
			)
			from transformers.models.deit.modeling_tf_deit import TF_DEIT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
			from PIL import Image

			from transformers import DeiTImageProcessor


class _lowerCAmelCase						:







						def __init__(    self   ,				lowerCAmelCase_   ,				lowerCAmelCase_=1_3   ,				lowerCAmelCase_=3_0   ,				lowerCAmelCase_=2   ,				lowerCAmelCase_=3   ,				lowerCAmelCase_=True   ,				lowerCAmelCase_=True   ,				lowerCAmelCase_=3_2   ,				lowerCAmelCase_=2   ,				lowerCAmelCase_=4   ,				lowerCAmelCase_=3_7   ,				lowerCAmelCase_="gelu"   ,				lowerCAmelCase_=0.1   ,				lowerCAmelCase_=0.1   ,				lowerCAmelCase_=1_0   ,				lowerCAmelCase_=0.02   ,				lowerCAmelCase_=3   ,				lowerCAmelCase_=None   ,				lowerCAmelCase_=2   ,				)		->							Dict:
										_SCREAMING_SNAKE_CASE   :		int       		=    parent
										_SCREAMING_SNAKE_CASE   :		str       		=    batch_size
										_SCREAMING_SNAKE_CASE   :		str       		=    image_size
										_SCREAMING_SNAKE_CASE   :		int       		=    patch_size
										_SCREAMING_SNAKE_CASE   :		List[str]       		=    num_channels
										_SCREAMING_SNAKE_CASE   :		Tuple       		=    is_training
										_SCREAMING_SNAKE_CASE   :		Optional[Any]       		=    use_labels
										_SCREAMING_SNAKE_CASE   :		Any       		=    hidden_size
										_SCREAMING_SNAKE_CASE   :		Tuple       		=    num_hidden_layers
										_SCREAMING_SNAKE_CASE   :		Union[str, Any]       		=    num_attention_heads
										_SCREAMING_SNAKE_CASE   :		Optional[Any]       		=    intermediate_size
										_SCREAMING_SNAKE_CASE   :		List[Any]       		=    hidden_act
										_SCREAMING_SNAKE_CASE   :		Any       		=    hidden_dropout_prob
										_SCREAMING_SNAKE_CASE   :		Optional[Any]       		=    attention_probs_dropout_prob
										_SCREAMING_SNAKE_CASE   :		Optional[int]       		=    type_sequence_label_size
										_SCREAMING_SNAKE_CASE   :		Optional[Any]       		=    initializer_range
										_SCREAMING_SNAKE_CASE   :		Optional[Any]       		=    scope
										_SCREAMING_SNAKE_CASE   :		int       		=    encoder_stride

										# in DeiT, the seq length equals the number of patches + 2 (we add 2 for the [CLS] and distilation tokens)
										_SCREAMING_SNAKE_CASE   :		List[str]       		=    (image_size // patch_size) ** 2
										_SCREAMING_SNAKE_CASE   :		List[Any]       		=    num_patches + 2







						def 			A      (    self							)		->							List[Any]:
										_SCREAMING_SNAKE_CASE   :		Optional[Any]       		=    floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]							)

										_SCREAMING_SNAKE_CASE   :		int       		=    None
										if self.use_labels:
														_SCREAMING_SNAKE_CASE   :		Optional[Any]       		=    ids_tensor([self.batch_size]   ,				self.type_sequence_label_size							)

										_SCREAMING_SNAKE_CASE   :		int       		=    self.get_config()

										return config, pixel_values, labels







						def 			A      (    self							)		->							List[str]:
										return DeiTConfig(
										    image_size=self.image_size   ,				patch_size=self.patch_size   ,				num_channels=self.num_channels   ,				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   ,				is_decoder=lowerCAmelCase_   ,				initializer_range=self.initializer_range   ,				encoder_stride=self.encoder_stride   ,				)







						def 			A      (    self   ,				lowerCAmelCase_   ,				lowerCAmelCase_   ,				lowerCAmelCase_							)		->							Optional[Any]:
										_SCREAMING_SNAKE_CASE   :		Tuple       		=    TFDeiTModel(config=lowerCAmelCase_							)
										_SCREAMING_SNAKE_CASE   :		Tuple       		=    model(lowerCAmelCase_							)
										self.parent.assertEqual(result.last_hidden_state.shape   ,				(self.batch_size, self.seq_length, self.hidden_size)							)







						def 			A      (    self   ,				lowerCAmelCase_   ,				lowerCAmelCase_   ,				lowerCAmelCase_							)		->							Optional[int]:
										_SCREAMING_SNAKE_CASE   :		Optional[Any]       		=    TFDeiTForMaskedImageModeling(config=lowerCAmelCase_							)
										_SCREAMING_SNAKE_CASE   :		Optional[Any]       		=    model(lowerCAmelCase_							)
										self.parent.assertEqual(
										    result.reconstruction.shape   ,				(self.batch_size, self.num_channels, self.image_size, self.image_size)							)

										# test greyscale images
										_SCREAMING_SNAKE_CASE   :		List[Any]       		=    1
										_SCREAMING_SNAKE_CASE   :		int       		=    TFDeiTForMaskedImageModeling(lowerCAmelCase_							)

										_SCREAMING_SNAKE_CASE   :		Optional[Any]       		=    floats_tensor([self.batch_size, 1, self.image_size, self.image_size]							)
										_SCREAMING_SNAKE_CASE   :		Tuple       		=    model(lowerCAmelCase_							)
										self.parent.assertEqual(result.reconstruction.shape   ,				(self.batch_size, 1, self.image_size, self.image_size)							)







						def 			A      (    self   ,				lowerCAmelCase_   ,				lowerCAmelCase_   ,				lowerCAmelCase_							)		->							Optional[Any]:
										_SCREAMING_SNAKE_CASE   :		List[str]       		=    self.type_sequence_label_size
										_SCREAMING_SNAKE_CASE   :		List[Any]       		=    TFDeiTForImageClassification(lowerCAmelCase_							)
										_SCREAMING_SNAKE_CASE   :		str       		=    model(lowerCAmelCase_   ,				labels=lowerCAmelCase_							)
										self.parent.assertEqual(result.logits.shape   ,				(self.batch_size, self.type_sequence_label_size)							)

										# test greyscale images
										_SCREAMING_SNAKE_CASE   :		int       		=    1
										_SCREAMING_SNAKE_CASE   :		Union[str, Any]       		=    TFDeiTForImageClassification(lowerCAmelCase_							)

										_SCREAMING_SNAKE_CASE   :		Union[str, Any]       		=    floats_tensor([self.batch_size, 1, self.image_size, self.image_size]							)
										_SCREAMING_SNAKE_CASE   :		Dict       		=    model(lowerCAmelCase_   ,				labels=lowerCAmelCase_							)
										self.parent.assertEqual(result.logits.shape   ,				(self.batch_size, self.type_sequence_label_size)							)







						def 			A      (    self							)		->							List[Any]:
										_SCREAMING_SNAKE_CASE   :		int       		=    self.prepare_config_and_inputs()
										_SCREAMING_SNAKE_CASE		,						_SCREAMING_SNAKE_CASE		,						_SCREAMING_SNAKE_CASE   :		Union[str, Any]       		=    config_and_inputs
										_SCREAMING_SNAKE_CASE   :		Tuple       		=    {'pixel_values': pixel_values}
										return config, inputs_dict







@require_tf
class _lowerCAmelCase						(    __UpperCAmelCase   ,  __UpperCAmelCase   ,  unittest.TestCase       ):
						SCREAMING_SNAKE_CASE_:					List[Any] 					=				(
						    (
						        TFDeiTModel,
						        TFDeiTForImageClassification,
						        TFDeiTForImageClassificationWithTeacher,
						        TFDeiTForMaskedImageModeling,
						    )
						    if is_tf_available()
						    else ()
						)
						SCREAMING_SNAKE_CASE_:					List[str] 					=				(
						    {
						        'feature-extraction': TFDeiTModel,
						        'image-classification': (TFDeiTForImageClassification, TFDeiTForImageClassificationWithTeacher),
						    }
						    if is_tf_available()
						    else {}
						)

						SCREAMING_SNAKE_CASE_:					Tuple 					=				False
						SCREAMING_SNAKE_CASE_:					Union[str, Any] 					=				False
						SCREAMING_SNAKE_CASE_:					Dict 					=				False
						SCREAMING_SNAKE_CASE_:					int 					=				False







						def 			A      (    self							)		->							Any:
										_SCREAMING_SNAKE_CASE   :		Union[str, Any]       		=    TFDeiTModelTester(self							)
										_SCREAMING_SNAKE_CASE   :		List[str]       		=    ConfigTester(self   ,				config_class=lowerCAmelCase_   ,				has_text_modality=lowerCAmelCase_   ,				hidden_size=3_7							)







						def 			A      (    self							)		->							Optional[int]:
										self.config_tester.run_common_tests()







						@unittest.skip(reason='DeiT does not use inputs_embeds'							)
						def 			A      (    self							)		->							Any:
										pass







						def 			A      (    self							)		->							Union[str, Any]:
										_SCREAMING_SNAKE_CASE		,						_SCREAMING_SNAKE_CASE   :		Any       		=    self.model_tester.prepare_config_and_inputs_for_common()

										for model_class in self.all_model_classes:
														_SCREAMING_SNAKE_CASE   :		Any       		=    model_class(lowerCAmelCase_							)
														self.assertIsInstance(model.get_input_embeddings()   ,				(tf.keras.layers.Layer)							)
														_SCREAMING_SNAKE_CASE   :		int       		=    model.get_output_embeddings()
														self.assertTrue(x is None or isinstance(lowerCAmelCase_   ,				tf.keras.layers.Dense							)							)







						def 			A      (    self							)		->							List[Any]:
										_SCREAMING_SNAKE_CASE		,						_SCREAMING_SNAKE_CASE   :		Union[str, Any]       		=    self.model_tester.prepare_config_and_inputs_for_common()

										for model_class in self.all_model_classes:
														_SCREAMING_SNAKE_CASE   :		Optional[Any]       		=    model_class(lowerCAmelCase_							)
														_SCREAMING_SNAKE_CASE   :		Union[str, Any]       		=    inspect.signature(model.call							)
														# signature.parameters is an OrderedDict => so arg_names order is deterministic
														_SCREAMING_SNAKE_CASE   :		Tuple       		=    [*signature.parameters.keys()]

														_SCREAMING_SNAKE_CASE   :		List[str]       		=    ['pixel_values']
														self.assertListEqual(arg_names[:1]   ,				lowerCAmelCase_							)







						def 			A      (    self							)		->							Tuple:
										_SCREAMING_SNAKE_CASE   :		Optional[int]       		=    self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_model(*lowerCAmelCase_							)







						def 			A      (    self							)		->							List[Any]:
										_SCREAMING_SNAKE_CASE   :		Optional[Any]       		=    self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_masked_image_modeling(*lowerCAmelCase_							)







						def 			A      (    self							)		->							List[str]:
										_SCREAMING_SNAKE_CASE   :		Tuple       		=    self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_image_classification(*lowerCAmelCase_							)







						def 			A      (    self   ,				lowerCAmelCase_   ,				lowerCAmelCase_   ,				lowerCAmelCase_=False							)		->							Optional[int]:
										_SCREAMING_SNAKE_CASE   :		str       		=    super()._prepare_for_class(lowerCAmelCase_   ,				lowerCAmelCase_   ,				return_labels=lowerCAmelCase_							)

										if return_labels:
														if "labels" in inputs_dict and "labels" not in inspect.signature(model_class.call							).parameters:
																		del inputs_dict["labels"]

										return inputs_dict







						@slow
						def 			A      (    self							)		->							Dict:
										for model_name in TF_DEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
														_SCREAMING_SNAKE_CASE   :		Any       		=    TFDeiTModel.from_pretrained(lowerCAmelCase_							)
														self.assertIsNotNone(lowerCAmelCase_							)







def 				lowercase__		(					):
				_SCREAMING_SNAKE_CASE   :		Any       		=    Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'       )
				return image






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







						@cached_property
						def 			A      (    self							)		->							List[str]:
										return (
										    DeiTImageProcessor.from_pretrained('facebook/deit-base-distilled-patch16-224'							)
										    if is_vision_available()
										    else None
										)







						@slow
						def 			A      (    self							)		->							Optional[Any]:
										_SCREAMING_SNAKE_CASE   :		List[str]       		=    TFDeiTForImageClassificationWithTeacher.from_pretrained('facebook/deit-base-distilled-patch16-224'							)

										_SCREAMING_SNAKE_CASE   :		str       		=    self.default_image_processor
										_SCREAMING_SNAKE_CASE   :		List[Any]       		=    prepare_img()
										_SCREAMING_SNAKE_CASE   :		Optional[Any]       		=    image_processor(images=lowerCAmelCase_   ,				return_tensors='tf'							)

										# forward pass
										_SCREAMING_SNAKE_CASE   :		Dict       		=    model(**lowerCAmelCase_							)

										# verify the logits
										_SCREAMING_SNAKE_CASE   :		int       		=    tf.TensorShape((1, 1_0_0_0)							)
										self.assertEqual(outputs.logits.shape   ,				lowerCAmelCase_							)

										_SCREAMING_SNAKE_CASE   :		int       		=    tf.constant([-1.0_266, 0.1_912, -1.2_861]							)

										self.assertTrue(np.allclose(outputs.logits[0, :3]   ,				lowerCAmelCase_   ,				atol=1e-4							)							)









"""simple docstring"""
from ...configuration_utils import PretrainedConfig
from ...utils import logging


lowerCAmelCase__    						=			logging.get_logger(__name__)

lowerCAmelCase__    						=			{
    'caidas/swin2sr-classicalsr-x2-64': (
        'https://huggingface.co/caidas/swin2sr-classicalsr-x2-64/resolve/main/config.json'
    ),
}


class _lowerCAmelCase						(    __UpperCAmelCase       ):
						SCREAMING_SNAKE_CASE_:					Any 					=				'swin2sr'

						SCREAMING_SNAKE_CASE_:					Dict 					=				{
						    'hidden_size': 'embed_dim',
						    'num_attention_heads': 'num_heads',
						    'num_hidden_layers': 'num_layers',
						}







						def __init__(    self   ,				lowerCAmelCase_=6_4   ,				lowerCAmelCase_=1   ,				lowerCAmelCase_=3   ,				lowerCAmelCase_=1_8_0   ,				lowerCAmelCase_=[6, 6, 6, 6, 6, 6]   ,				lowerCAmelCase_=[6, 6, 6, 6, 6, 6]   ,				lowerCAmelCase_=8   ,				lowerCAmelCase_=2.0   ,				lowerCAmelCase_=True   ,				lowerCAmelCase_=0.0   ,				lowerCAmelCase_=0.0   ,				lowerCAmelCase_=0.1   ,				lowerCAmelCase_="gelu"   ,				lowerCAmelCase_=False   ,				lowerCAmelCase_=0.02   ,				lowerCAmelCase_=1e-5   ,				lowerCAmelCase_=2   ,				lowerCAmelCase_=1.0   ,				lowerCAmelCase_="1conv"   ,				lowerCAmelCase_="pixelshuffle"   ,				**lowerCAmelCase_   ,				)		->							Dict:
										super().__init__(**lowerCAmelCase_							)

										_SCREAMING_SNAKE_CASE   :		Optional[Any]       		=    image_size
										_SCREAMING_SNAKE_CASE   :		Optional[Any]       		=    patch_size
										_SCREAMING_SNAKE_CASE   :		str       		=    num_channels
										_SCREAMING_SNAKE_CASE   :		Any       		=    embed_dim
										_SCREAMING_SNAKE_CASE   :		Optional[int]       		=    depths
										_SCREAMING_SNAKE_CASE   :		List[Any]       		=    len(lowerCAmelCase_							)
										_SCREAMING_SNAKE_CASE   :		Tuple       		=    num_heads
										_SCREAMING_SNAKE_CASE   :		Optional[Any]       		=    window_size
										_SCREAMING_SNAKE_CASE   :		List[str]       		=    mlp_ratio
										_SCREAMING_SNAKE_CASE   :		List[str]       		=    qkv_bias
										_SCREAMING_SNAKE_CASE   :		Optional[int]       		=    hidden_dropout_prob
										_SCREAMING_SNAKE_CASE   :		Optional[int]       		=    attention_probs_dropout_prob
										_SCREAMING_SNAKE_CASE   :		Optional[int]       		=    drop_path_rate
										_SCREAMING_SNAKE_CASE   :		int       		=    hidden_act
										_SCREAMING_SNAKE_CASE   :		Optional[Any]       		=    use_absolute_embeddings
										_SCREAMING_SNAKE_CASE   :		str       		=    layer_norm_eps
										_SCREAMING_SNAKE_CASE   :		List[Any]       		=    initializer_range
										_SCREAMING_SNAKE_CASE   :		List[Any]       		=    upscale
										_SCREAMING_SNAKE_CASE   :		str       		=    img_range
										_SCREAMING_SNAKE_CASE   :		Any       		=    resi_connection
										_SCREAMING_SNAKE_CASE   :		Tuple       		=    upsampler

