

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






      '''simple docstring'''






      def __init__(       self			:		Optional[int]  ,       snake_case			:		Optional[Any]  ,       snake_case			:		List[Any]=13  ,       snake_case			:		str=30  ,       snake_case			:		Union[str, Any]=2  ,       snake_case			:		Dict=3  ,       snake_case			:		Any=True  ,       snake_case			:		str=True  ,       snake_case			:		List[Any]=32  ,       snake_case			:		str=2  ,       snake_case			:		Any=4  ,       snake_case			:		List[Any]=37  ,       snake_case			:		Dict="gelu"  ,       snake_case			:		int=0.1  ,       snake_case			:		Optional[Any]=0.1  ,       snake_case			:		Any=10  ,       snake_case			:		str=0.02  ,       snake_case			:		List[Any]=3  ,       snake_case			:		List[str]=None  ,       snake_case			:		Union[str, Any]=2  ,       ):



          """simple docstring"""





          _snake_case			:			Tuple				       =		parent
          _snake_case			:			Optional[int]				       =		batch_size
          _snake_case			:			Dict				       =		image_size
          _snake_case			:			Optional[Any]				       =		patch_size
          _snake_case			:			int				       =		num_channels
          _snake_case			:			Any				       =		is_training
          _snake_case			:			List[str]				       =		use_labels
          _snake_case			:			Tuple				       =		hidden_size
          _snake_case			:			Optional[int]				       =		num_hidden_layers
          _snake_case			:			List[Any]				       =		num_attention_heads
          _snake_case			:			Any				       =		intermediate_size
          _snake_case			:			Optional[Any]				       =		hidden_act
          _snake_case			:			str				       =		hidden_dropout_prob
          _snake_case			:			Union[str, Any]				       =		attention_probs_dropout_prob
          _snake_case			:			Optional[int]				       =		type_sequence_label_size
          _snake_case			:			str				       =		initializer_range
          _snake_case			:			Optional[Any]				       =		scope
          _snake_case			:			List[str]				       =		encoder_stride

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




      def 						__UpperCAmelCase      (       self			:		Union[str, Any] ):



          """simple docstring"""





          _snake_case			:			List[Any]				       =		floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] )

          _snake_case			:			Union[str, Any]				       =		None
          if self.use_labels:
              _snake_case			:			Optional[Any]				       =		ids_tensor([self.batch_size]  ,       self.type_sequence_label_size )

          _snake_case			:			int				       =		self.get_config()

          return config, pixel_values, labels




      def 						__UpperCAmelCase      (       self			:		Union[str, Any] ):



          """simple docstring"""





          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			:		Union[str, Any]  ,       snake_case			:		Any  ,       snake_case			:		List[str]  ,       snake_case			:		Union[str, Any] ):



          """simple docstring"""





          _snake_case			:			Tuple				       =		TFDeiTModel(config=_A )
          _snake_case			:			Any				       =		model(_A )
          self.parent.assertEqual(result.last_hidden_state.shape  ,       (self.batch_size, self.seq_length, self.hidden_size) )




      def 						__UpperCAmelCase      (       self			:		Optional[int]  ,       snake_case			:		Dict  ,       snake_case			:		Dict  ,       snake_case			:		Optional[Any] ):



          """simple docstring"""





          _snake_case			:			Union[str, Any]				       =		TFDeiTForMaskedImageModeling(config=_A )
          _snake_case			:			Tuple				       =		model(_A )
          self.parent.assertEqual(
              result.reconstruction.shape  ,       (self.batch_size, self.num_channels, self.image_size, self.image_size) )

          # test greyscale images
          _snake_case			:			Optional[int]				       =		1
          _snake_case			:			int				       =		TFDeiTForMaskedImageModeling(_A )

          _snake_case			:			List[str]				       =		floats_tensor([self.batch_size, 1, self.image_size, self.image_size] )
          _snake_case			:			Tuple				       =		model(_A )
          self.parent.assertEqual(result.reconstruction.shape  ,       (self.batch_size, 1, self.image_size, self.image_size) )




      def 						__UpperCAmelCase      (       self			:		str  ,       snake_case			:		Optional[int]  ,       snake_case			:		List[Any]  ,       snake_case			:		Any ):



          """simple docstring"""





          _snake_case			:			Optional[int]				       =		self.type_sequence_label_size
          _snake_case			:			str				       =		TFDeiTForImageClassification(_A )
          _snake_case			:			Optional[Any]				       =		model(_A  ,       labels=_A )
          self.parent.assertEqual(result.logits.shape  ,       (self.batch_size, self.type_sequence_label_size) )

          # test greyscale images
          _snake_case			:			List[Any]				       =		1
          _snake_case			:			List[Any]				       =		TFDeiTForImageClassification(_A )

          _snake_case			:			int				       =		floats_tensor([self.batch_size, 1, self.image_size, self.image_size] )
          _snake_case			:			List[str]				       =		model(_A  ,       labels=_A )
          self.parent.assertEqual(result.logits.shape  ,       (self.batch_size, self.type_sequence_label_size) )




      def 						__UpperCAmelCase      (       self			:		Dict ):



          """simple docstring"""





          _snake_case			:			Tuple				       =		self.prepare_config_and_inputs()
          _snake_case			:			int				       =		config_and_inputs
          _snake_case			:			Dict				       =		{'''pixel_values''': pixel_values}
          return config, inputs_dict



@require_tf
class       SCREAMING_SNAKE_CASE       (				lowercase_   ,lowercase_   ,unittest.TestCase       ):






      '''simple docstring'''






      SCREAMING_SNAKE_CASE__			:					List[Any]     =					(
          (
              TFDeiTModel,
              TFDeiTForImageClassification,
              TFDeiTForImageClassificationWithTeacher,
              TFDeiTForMaskedImageModeling,
          )
          if is_tf_available()
          else ()
      )
      SCREAMING_SNAKE_CASE__			:					Optional[int]     =					(
          {
              '''feature-extraction''': TFDeiTModel,
              '''image-classification''': (TFDeiTForImageClassification, TFDeiTForImageClassificationWithTeacher),
          }
          if is_tf_available()
          else {}
      )

      SCREAMING_SNAKE_CASE__			:					Any     =					False
      SCREAMING_SNAKE_CASE__			:					int     =					False
      SCREAMING_SNAKE_CASE__			:					List[Any]     =					False
      SCREAMING_SNAKE_CASE__			:					Tuple     =					False




      def 						__UpperCAmelCase      (       self			:		Optional[Any] ):



          """simple docstring"""





          _snake_case			:			List[str]				       =		TFDeiTModelTester(self )
          _snake_case			:			Dict				       =		ConfigTester(self  ,       config_class=_A  ,       has_text_modality=_A  ,       hidden_size=37 )




      def 						__UpperCAmelCase      (       self			:		str ):



          """simple docstring"""





          self.config_tester.run_common_tests()




      @unittest.skip(reason='DeiT does not use inputs_embeds' )
      def 						__UpperCAmelCase      (       self			:		List[str] ):



          """simple docstring"""





          pass




      def 						__UpperCAmelCase      (       self			:		Dict ):



          """simple docstring"""





          _snake_case			:			Union[str, Any]				       =		self.model_tester.prepare_config_and_inputs_for_common()

          for model_class in self.all_model_classes:
              _snake_case			:			Union[str, Any]				       =		model_class(_A )
              self.assertIsInstance(model.get_input_embeddings()  ,       (tf.keras.layers.Layer) )
              _snake_case			:			List[Any]				       =		model.get_output_embeddings()
              self.assertTrue(x is None or isinstance(_A  ,       tf.keras.layers.Dense ) )




      def 						__UpperCAmelCase      (       self			:		Union[str, Any] ):



          """simple docstring"""





          _snake_case			:			Tuple				       =		self.model_tester.prepare_config_and_inputs_for_common()

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

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




      def 						__UpperCAmelCase      (       self			:		Union[str, Any] ):



          """simple docstring"""





          _snake_case			:			Union[str, Any]				       =		self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_model(*_A )




      def 						__UpperCAmelCase      (       self			:		Any ):



          """simple docstring"""





          _snake_case			:			Optional[int]				       =		self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_masked_image_modeling(*_A )




      def 						__UpperCAmelCase      (       self			:		Union[str, Any] ):



          """simple docstring"""





          _snake_case			:			Any				       =		self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_image_classification(*_A )




      def 						__UpperCAmelCase      (       self			:		Optional[Any]  ,       snake_case			:		Tuple  ,       snake_case			:		str  ,       snake_case			:		Any=False ):



          """simple docstring"""





          _snake_case			:			Optional[int]				       =		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			:		List[Any] ):



          """simple docstring"""





          for model_name in TF_DEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
              _snake_case			:			Optional[int]				       =		TFDeiTModel.from_pretrained(_A )
              self.assertIsNotNone(_A )



def 	lowerCamelCase__							(   )		->     Dict:



    """simple docstring"""







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




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






      '''simple docstring'''






      @cached_property
      def 						__UpperCAmelCase      (       self			:		int ):



          """simple docstring"""





          return (
              DeiTImageProcessor.from_pretrained('facebook/deit-base-distilled-patch16-224' )
              if is_vision_available()
              else None
          )




      @slow
      def 						__UpperCAmelCase      (       self			:		Any ):



          """simple docstring"""





          _snake_case			:			Union[str, Any]				       =		TFDeiTForImageClassificationWithTeacher.from_pretrained('facebook/deit-base-distilled-patch16-224' )

          _snake_case			:			Dict				       =		self.default_image_processor
          _snake_case			:			List[str]				       =		prepare_img()
          _snake_case			:			List[Any]				       =		image_processor(images=_A  ,       return_tensors='tf' )

          # forward pass
          _snake_case			:			List[Any]				       =		model(**_A )

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

          _snake_case			:			int				       =		tf.constant([-1.0266, 0.1912, -1.2861] )

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











'''simple docstring'''





_UpperCamelCase						:						Dict  =					range(2, 20 + 1)
_UpperCamelCase						:						str  =					[10**k for k in range(ks[-1] + 1)]
_UpperCamelCase						:						dict[int, dict[int, list[list[int]]]]  =					{}
def __UpperCAmelCase   (					A					:			List[Any]			,       A					:			str			,       A					:			Union[str, Any]			,       A					:			Union[str, Any]		)    ->   Optional[Any]:
			UpperCAmelCase_      :       Optional[Any]          =       sum(a_i[j] for j in range(A			,       len(A		)		)		)
			UpperCAmelCase_      :       Union[str, Any]          =       sum(a_i[j] * base[j] for j in range(min(len(A		)			,       A		)		)		)

			UpperCAmelCase_    ,      UpperCAmelCase_      :       Optional[int]          =       0, 0
			UpperCAmelCase_      :       str          =       n - i

			UpperCAmelCase_      :       Union[str, Any]          =       memo.get(A		)

			if sub_memo is not None:
						UpperCAmelCase_      :       Dict          =       sub_memo.get(A		)

						if jumps is not None and len(A		) > 0:
									# find and make the largest jump without going over
									UpperCAmelCase_      :       str          =       -1
									for _k in range(len(A		) - 1			,       -1			,       -1		):
												if jumps[_k][2] <= k and jumps[_k][1] <= max_dn:
															UpperCAmelCase_      :       Any          =       _k
															break

									if max_jump >= 0:
												UpperCAmelCase_    ,      UpperCAmelCase_    ,      UpperCAmelCase_      :       Any          =       jumps[max_jump]
												# since the difference between jumps is cached, add c
												UpperCAmelCase_      :       Union[str, Any]          =       diff + c
												for j in range(min(A			,       len(A		)		)		):
															UpperCAmelCase_    ,      UpperCAmelCase_      :       Tuple          =       divmod(A			,       1_0		)
												if new_c > 0:
															add(A			,       A			,       A		)

						else:
									UpperCAmelCase_      :       Tuple          =       []
			else:
						UpperCAmelCase_      :       List[str]          =       {c: []}
						UpperCAmelCase_      :       List[str]          =       sub_memo

			if dn >= max_dn or c + diff >= base[k]:
						return diff, dn

			if k > ks[0]:
						while True:
									# keep doing smaller jumps
									UpperCAmelCase_    ,      UpperCAmelCase_      :       Optional[Any]          =       next_term(A			,       k - 1			,       i + dn			,       A		)
									diff += _diff
									dn += terms_jumped

									if dn >= max_dn or c + diff >= base[k]:
												break
			else:
						# would be too small a jump, just compute sequential terms instead
						UpperCAmelCase_    ,      UpperCAmelCase_      :       int          =       compute(A			,       A			,       i + dn			,       A		)
						diff += _diff
						dn += terms_jumped

			UpperCAmelCase_      :       List[str]          =       sub_memo[c]

			# keep jumps sorted by # of terms skipped
			UpperCAmelCase_      :       List[Any]          =       0
			while j < len(A		):
						if jumps[j][1] > dn:
									break
						j += 1

			# cache the jump for this value digitsum(b) and c
			sub_memo[c].insert(A			,       (diff, dn, k)		)
			return (diff, dn)
def __UpperCAmelCase   (					A					:			List[str]			,       A					:			Dict			,       A					:			List[str]			,       A					:			Union[str, Any]		)    ->   str:
			if i >= n:
						return 0, i
			if k > len(A		):
						a_i.extend([0 for _ in range(k - len(A		)		)]		)

			# note: a_i -> b * 10^k + c
			# ds_b -> digitsum(b)
			# ds_c -> digitsum(c)
			UpperCAmelCase_      :       int          =       i
			UpperCAmelCase_    ,      UpperCAmelCase_    ,      UpperCAmelCase_      :       List[Any]          =       0, 0, 0
			for j in range(len(A		)		):
						if j >= k:
									ds_b += a_i[j]
						else:
									ds_c += a_i[j]

			while i < n:
						i += 1
						UpperCAmelCase_      :       Optional[int]          =       ds_c + ds_b
						diff += addend
						UpperCAmelCase_      :       Any          =       0
						for j in range(A		):
									UpperCAmelCase_      :       Dict          =       a_i[j] + addend
									UpperCAmelCase_    ,      UpperCAmelCase_      :       Tuple          =       divmod(A			,       1_0		)

									ds_c += a_i[j]

						if addend > 0:
									break

			if addend > 0:
						add(A			,       A			,       A		)
			return diff, i - start_i
def __UpperCAmelCase   (					A					:			Dict			,       A					:			Union[str, Any]			,       A					:			str		)    ->   List[Any]:
			for j in range(A			,       len(A		)		):
						UpperCAmelCase_      :       List[str]          =       digits[j] + addend
						if s >= 1_0:
									UpperCAmelCase_    ,      UpperCAmelCase_      :       Any          =       divmod(A			,       1_0		)
									UpperCAmelCase_      :       str          =       addend // 1_0 + quotient
						else:
									UpperCAmelCase_      :       Any          =       s
									UpperCAmelCase_      :       Any          =       addend // 1_0

						if addend == 0:
									break

			while addend > 0:
						UpperCAmelCase_    ,      UpperCAmelCase_      :       Union[str, Any]          =       divmod(A			,       1_0		)
						digits.append(A		)
def __UpperCAmelCase   (					A					:			int = 1_0**1_5		)    ->   int:
			UpperCAmelCase_      :       Any          =       [1]
			UpperCAmelCase_      :       Union[str, Any]          =       1
			UpperCAmelCase_      :       List[Any]          =       0
			while True:
						UpperCAmelCase_    ,      UpperCAmelCase_      :       str          =       next_term(A			,       2_0			,       i + dn			,       A		)
						dn += terms_jumped
						if dn == n - i:
									break

			UpperCAmelCase_      :       Any          =       0
			for j in range(len(A		)		):
						a_n += digits[j] * 1_0**j
			return a_n


if __name__ == "__main__":
						print(f'''{solution() = }''')


