






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




		'''simple docstring'''





		def __init__(   self ,_lowerCAmelCase ,_lowerCAmelCase=13 ,_lowerCAmelCase=30 ,_lowerCAmelCase=2 ,_lowerCAmelCase=3 ,_lowerCAmelCase=True ,_lowerCAmelCase=True ,_lowerCAmelCase=32 ,_lowerCAmelCase=2 ,_lowerCAmelCase=4 ,_lowerCAmelCase=37 ,_lowerCAmelCase="gelu" ,_lowerCAmelCase=0.1 ,_lowerCAmelCase=0.1 ,_lowerCAmelCase=10 ,_lowerCAmelCase=0.02 ,_lowerCAmelCase=3 ,_lowerCAmelCase=None ,_lowerCAmelCase=2 ,):
								lowerCamelCase__								=	parent
								lowerCamelCase__								=	batch_size
								lowerCamelCase__								=	image_size
								lowerCamelCase__								=	patch_size
								lowerCamelCase__								=	num_channels
								lowerCamelCase__								=	is_training
								lowerCamelCase__								=	use_labels
								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__								=	type_sequence_label_size
								lowerCamelCase__								=	initializer_range
								lowerCamelCase__								=	scope
								lowerCamelCase__								=	encoder_stride

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






		def       UpperCamelCase_       (   self							):
								lowerCamelCase__								=	floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]							)

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

								lowerCamelCase__								=	self.get_config()

								return config, pixel_values, labels






		def       UpperCamelCase_       (   self							):
								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=__a ,initializer_range=self.initializer_range ,encoder_stride=self.encoder_stride ,)






		def       UpperCamelCase_       (   self ,_lowerCAmelCase ,_lowerCAmelCase ,_lowerCAmelCase							):
								lowerCamelCase__								=	TFDeiTModel(config=__a							)
								lowerCamelCase__								=	model(__a							)
								self.parent.assertEqual(result.last_hidden_state.shape ,(self.batch_size, self.seq_length, self.hidden_size)							)






		def       UpperCamelCase_       (   self ,_lowerCAmelCase ,_lowerCAmelCase ,_lowerCAmelCase							):
								lowerCamelCase__								=	TFDeiTForMaskedImageModeling(config=__a							)
								lowerCamelCase__								=	model(__a							)
								self.parent.assertEqual(
								    result.reconstruction.shape ,(self.batch_size, self.num_channels, self.image_size, self.image_size)							)

								# test greyscale images
								lowerCamelCase__								=	1
								lowerCamelCase__								=	TFDeiTForMaskedImageModeling(__a							)

								lowerCamelCase__								=	floats_tensor([self.batch_size, 1, self.image_size, self.image_size]							)
								lowerCamelCase__								=	model(__a							)
								self.parent.assertEqual(result.reconstruction.shape ,(self.batch_size, 1, self.image_size, self.image_size)							)






		def       UpperCamelCase_       (   self ,_lowerCAmelCase ,_lowerCAmelCase ,_lowerCAmelCase							):
								lowerCamelCase__								=	self.type_sequence_label_size
								lowerCamelCase__								=	TFDeiTForImageClassification(__a							)
								lowerCamelCase__								=	model(__a ,labels=__a							)
								self.parent.assertEqual(result.logits.shape ,(self.batch_size, self.type_sequence_label_size)							)

								# test greyscale images
								lowerCamelCase__								=	1
								lowerCamelCase__								=	TFDeiTForImageClassification(__a							)

								lowerCamelCase__								=	floats_tensor([self.batch_size, 1, self.image_size, self.image_size]							)
								lowerCamelCase__								=	model(__a ,labels=__a							)
								self.parent.assertEqual(result.logits.shape ,(self.batch_size, self.type_sequence_label_size)							)






		def       UpperCamelCase_       (   self							):
								lowerCamelCase__								=	self.prepare_config_and_inputs()
								lowerCamelCase__								=	config_and_inputs
								lowerCamelCase__								=	{"""pixel_values""": pixel_values}
								return config, inputs_dict




@require_tf
class 		UpperCamelCase__     (a__ ,a__ ,unittest.TestCase      ):




		'''simple docstring'''





		_UpperCamelCase						=       (
		    (
		        TFDeiTModel,
		        TFDeiTForImageClassification,
		        TFDeiTForImageClassificationWithTeacher,
		        TFDeiTForMaskedImageModeling,
		    )
		    if is_tf_available()
		    else ()
		)
		_UpperCamelCase						=       (
		    {
		        'feature-extraction': TFDeiTModel,
		        'image-classification': (TFDeiTForImageClassification, TFDeiTForImageClassificationWithTeacher),
		    }
		    if is_tf_available()
		    else {}
		)

		_UpperCamelCase						=       False
		_UpperCamelCase						=       False
		_UpperCamelCase						=       False
		_UpperCamelCase						=       False






		def       UpperCamelCase_       (   self							):
								lowerCamelCase__								=	TFDeiTModelTester(self							)
								lowerCamelCase__								=	ConfigTester(self ,config_class=__a ,has_text_modality=__a ,hidden_size=37							)






		def       UpperCamelCase_       (   self							):
								self.config_tester.run_common_tests()






		@unittest.skip(reason="""DeiT does not use inputs_embeds"""							)
		def       UpperCamelCase_       (   self							):
								pass






		def       UpperCamelCase_       (   self							):
								lowerCamelCase__								=	self.model_tester.prepare_config_and_inputs_for_common()

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






		def       UpperCamelCase_       (   self							):
								lowerCamelCase__								=	self.model_tester.prepare_config_and_inputs_for_common()

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

														lowerCamelCase__								=	["""pixel_values"""]
														self.assertListEqual(arg_names[:1] ,__a							)






		def       UpperCamelCase_       (   self							):
								lowerCamelCase__								=	self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_model(*__a							)






		def       UpperCamelCase_       (   self							):
								lowerCamelCase__								=	self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_masked_image_modeling(*__a							)






		def       UpperCamelCase_       (   self							):
								lowerCamelCase__								=	self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_image_classification(*__a							)






		def       UpperCamelCase_       (   self ,_lowerCAmelCase ,_lowerCAmelCase ,_lowerCAmelCase=False							):
								lowerCamelCase__								=	super()._prepare_for_class(__a ,__a ,return_labels=__a							)

								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       UpperCamelCase_       (   self							):
								for model_name in TF_DEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
														lowerCamelCase__								=	TFDeiTModel.from_pretrained(__a							)
														self.assertIsNotNone(__a							)



def     A__      (					):
						lowerCamelCase__								=	Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""    )
						return image





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




		'''simple docstring'''





		@cached_property
		def       UpperCamelCase_       (   self							):
								return (
								    DeiTImageProcessor.from_pretrained("""facebook/deit-base-distilled-patch16-224"""							)
								    if is_vision_available()
								    else None
								)






		@slow
		def       UpperCamelCase_       (   self							):
								lowerCamelCase__								=	TFDeiTForImageClassificationWithTeacher.from_pretrained("""facebook/deit-base-distilled-patch16-224"""							)

								lowerCamelCase__								=	self.default_image_processor
								lowerCamelCase__								=	prepare_img()
								lowerCamelCase__								=	image_processor(images=__a ,return_tensors="""tf"""							)

								# forward pass
								lowerCamelCase__								=	model(**__a							)

								# verify the logits
								lowerCamelCase__								=	tf.TensorShape((1, 10_00)							)
								self.assertEqual(outputs.logits.shape ,__a							)

								lowerCamelCase__								=	tf.constant([-1.0266, 0.1912, -1.2861]							)

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





from __future__ import annotations

from collections import namedtuple


def    __lowerCamelCase   (      A__						: float						,						A__						: float						,						A__						: float					)     ->    tuple:
   lowerCamelCase_						:   Dict		= namedtuple("""result"""						,						"""name value"""					)
   if (voltage, current, power).count(0					) != 1:
      raise ValueError("""Only one argument must be 0"""					)
   elif power < 0:
      raise ValueError(
          """Power cannot be negative in any electrical/electronics system"""					)
   elif voltage == 0:
      return result("""voltage"""						,						power / current					)
   elif current == 0:
      return result("""current"""						,						power / voltage					)
   elif power == 0:
      return result("""power"""						,						float(round(abs(voltage * current					)						,						2					)					)					)
   else:
      raise ValueError("""Exactly one argument must be 0"""					)


if __name__ == "__main__":
      import doctest

      doctest.testmod()





