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



    def __init__(self    :    Any   ,			__a    :    str   ,			__a    :    List[str]=13   ,			__a    :    Optional[int]=30   ,			__a    :    Tuple=2   ,			__a    :    str=3   ,			__a    :    Tuple=True   ,			__a    :    List[Any]=True   ,			__a    :    Optional[int]=32   ,			__a    :    Optional[int]=2   ,			__a    :    int=4   ,			__a    :    Optional[Any]=37   ,			__a    :    Optional[Any]="gelu"   ,			__a    :    Optional[Any]=0.1   ,			__a    :    int=0.1   ,			__a    :    int=10   ,			__a    :    Optional[int]=0.02   ,			__a    :    Dict=3   ,			__a    :    Optional[int]=None   ,			__a    :    List[str]=2   ,			):
         UpperCAmelCase_           =   parent
         UpperCAmelCase_           =   batch_size
         UpperCAmelCase_           =   image_size
         UpperCAmelCase_           =   patch_size
         UpperCAmelCase_           =   num_channels
         UpperCAmelCase_           =   is_training
         UpperCAmelCase_           =   use_labels
         UpperCAmelCase_           =   hidden_size
         UpperCAmelCase_           =   num_hidden_layers
         UpperCAmelCase_           =   num_attention_heads
         UpperCAmelCase_           =   intermediate_size
         UpperCAmelCase_           =   hidden_act
         UpperCAmelCase_           =   hidden_dropout_prob
         UpperCAmelCase_           =   attention_probs_dropout_prob
         UpperCAmelCase_           =   type_sequence_label_size
         UpperCAmelCase_           =   initializer_range
         UpperCAmelCase_           =   scope
         UpperCAmelCase_           =   encoder_stride

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



    def 					_lowercase						(self    :    Optional[Any] ):
         UpperCAmelCase_           =   floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] )

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

         UpperCAmelCase_           =   self.get_config()

         return config, pixel_values, labels



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



    def 					_lowercase						(self    :    Union[str, Any]   ,			__a    :    str   ,			__a    :    Any   ,			__a    :    Dict ):
         UpperCAmelCase_           =   TFDeiTModel(config=__a )
         UpperCAmelCase_           =   model(__a )
         self.parent.assertEqual(result.last_hidden_state.shape   ,			(self.batch_size, self.seq_length, self.hidden_size) )



    def 					_lowercase						(self    :    str   ,			__a    :    Optional[int]   ,			__a    :    List[str]   ,			__a    :    Tuple ):
         UpperCAmelCase_           =   TFDeiTForMaskedImageModeling(config=__a )
         UpperCAmelCase_           =   model(__a )
         self.parent.assertEqual(
             result.reconstruction.shape   ,			(self.batch_size, self.num_channels, self.image_size, self.image_size) )

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

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



    def 					_lowercase						(self    :    List[Any]   ,			__a    :    List[str]   ,			__a    :    int   ,			__a    :    int ):
         UpperCAmelCase_           =   self.type_sequence_label_size
         UpperCAmelCase_           =   TFDeiTForImageClassification(__a )
         UpperCAmelCase_           =   model(__a   ,			labels=__a )
         self.parent.assertEqual(result.logits.shape   ,			(self.batch_size, self.type_sequence_label_size) )

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

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






    def 					_lowercase						(self    :    Dict ):
         UpperCAmelCase_           =   self.prepare_config_and_inputs()
         UpperCAmelCase_    , UpperCAmelCase_    , UpperCAmelCase_           =   config_and_inputs
         UpperCAmelCase_           =   {"pixel_values": pixel_values}
         return config, inputs_dict




@require_tf
class        __A      ( UpperCamelCase__		,	UpperCamelCase__		,	unittest.TestCase	):
    a__       :						Tuple          =  (
        (
            TFDeiTModel,
            TFDeiTForImageClassification,
            TFDeiTForImageClassificationWithTeacher,
            TFDeiTForMaskedImageModeling,
        )
        if is_tf_available()
        else ()
    )
    a__       :						Union[str, Any]          =  (
        {
            """feature-extraction""": TFDeiTModel,
            """image-classification""": (TFDeiTForImageClassification, TFDeiTForImageClassificationWithTeacher),
        }
        if is_tf_available()
        else {}
    )

    a__       :						Tuple          =  False
    a__       :						List[Any]          =  False
    a__       :						Any          =  False
    a__       :						Dict          =  False



    def 					_lowercase						(self    :    List[Any] ):
         UpperCAmelCase_           =   TFDeiTModelTester(self )
         UpperCAmelCase_           =   ConfigTester(self   ,			config_class=__a   ,			has_text_modality=__a   ,			hidden_size=37 )



    def 					_lowercase						(self    :    Union[str, Any] ):
         self.config_tester.run_common_tests()



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



    def 					_lowercase						(self    :    List[Any] ):
         UpperCAmelCase_    , UpperCAmelCase_           =   self.model_tester.prepare_config_and_inputs_for_common()

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



    def 					_lowercase						(self    :    Optional[Any] ):
         UpperCAmelCase_    , UpperCAmelCase_           =   self.model_tester.prepare_config_and_inputs_for_common()

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

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



    def 					_lowercase						(self    :    Dict ):
         UpperCAmelCase_           =   self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_model(*__a )



    def 					_lowercase						(self    :    Any ):
         UpperCAmelCase_           =   self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_masked_image_modeling(*__a )



    def 					_lowercase						(self    :    Any ):
         UpperCAmelCase_           =   self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_image_classification(*__a )



    def 					_lowercase						(self    :    Optional[Any]   ,			__a    :    Tuple   ,			__a    :    Tuple   ,			__a    :    Union[str, Any]=False ):
         UpperCAmelCase_           =   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 					_lowercase						(self    :    int ):
         for model_name in TF_DEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
              UpperCAmelCase_           =   TFDeiTModel.from_pretrained(__a )
              self.assertIsNotNone(__a )





def 	lowerCAmelCase_				(					)    ->      Dict:


     '''simple docstring'''




     UpperCAmelCase_           =   Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"			)
     return image






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



    @cached_property
    def 					_lowercase						(self    :    Union[str, Any] ):
         return (
             DeiTImageProcessor.from_pretrained("facebook/deit-base-distilled-patch16-224" )
             if is_vision_available()
             else None
         )






    @slow
    def 					_lowercase						(self    :    Optional[Any] ):
         UpperCAmelCase_           =   TFDeiTForImageClassificationWithTeacher.from_pretrained("facebook/deit-base-distilled-patch16-224" )

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

         # forward pass
         UpperCAmelCase_           =   model(**__a )

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

         UpperCAmelCase_           =   tf.constant([-1.02_66, 0.19_12, -1.28_61] )

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








'''simple docstring'''







from math import isqrt


def 	SCREAMING_SNAKE_CASE_					(  _UpperCAmelCase	:	int  )  ->			bool:
       return all(number % divisor != 0 for divisor in range(2    ,isqrt(_UpperCAmelCase  ) + 1  )  )


def 	SCREAMING_SNAKE_CASE_					(  _UpperCAmelCase	:	int = 10**6  )  ->			int:
       _a    : List[Any]     					=0
       _a    : str     					=1
       _a    : Optional[Any]     					=7
       while prime_candidate < max_prime:
              primes_count += is_prime(_UpperCAmelCase  )

              cube_index += 1
              prime_candidate += 6 * cube_index

       return primes_count


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


