





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__   :




   """simple docstring"""



   def __init__( self ,  lowercase ,  lowercase=13 ,  lowercase=30 ,  lowercase=2 ,  lowercase=3 ,  lowercase=True ,  lowercase=True ,  lowercase=32 ,  lowercase=2 ,  lowercase=4 ,  lowercase=37 ,  lowercase="gelu" ,  lowercase=0.1 ,  lowercase=0.1 ,  lowercase=10 ,  lowercase=0.02 ,  lowercase=3 ,  lowercase=None ,  lowercase=2 ,  )      ->						Tuple:







          '''simple docstring'''

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

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

   def 							__lowercase		( self)      ->						Tuple:







          '''simple docstring'''

          a__						:     int			=    floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size])

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

          a__						:     Dict			=    self.get_config()

          return config, pixel_values, labels

   def 							__lowercase		( self)      ->						Optional[int]:







          '''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=lowercase ,  initializer_range=self.initializer_range ,  encoder_stride=self.encoder_stride ,  )

   def 							__lowercase		( self ,  lowercase ,  lowercase ,  lowercase)      ->						List[str]:







          '''simple docstring'''

          a__						:     Tuple			=    TFDeiTModel(config=lowercase)
          a__						:     int			=    model(lowercase)
          self.parent.assertEqual(result.last_hidden_state.shape ,  (self.batch_size, self.seq_length, self.hidden_size))

   def 							__lowercase		( self ,  lowercase ,  lowercase ,  lowercase)      ->						Tuple:







          '''simple docstring'''

          a__						:     int			=    TFDeiTForMaskedImageModeling(config=lowercase)
          a__						:     Union[str, Any]			=    model(lowercase)
          self.parent.assertEqual(
              result.reconstruction.shape ,  (self.batch_size, self.num_channels, self.image_size, self.image_size))

          # test greyscale images
          a__						:     List[str]			=    1
          a__						:     List[str]			=    TFDeiTForMaskedImageModeling(lowercase)

          a__						:     Tuple			=    floats_tensor([self.batch_size, 1, self.image_size, self.image_size])
          a__						:     Tuple			=    model(lowercase)
          self.parent.assertEqual(result.reconstruction.shape ,  (self.batch_size, 1, self.image_size, self.image_size))

   def 							__lowercase		( self ,  lowercase ,  lowercase ,  lowercase)      ->						Tuple:







          '''simple docstring'''

          a__						:     List[str]			=    self.type_sequence_label_size
          a__						:     Optional[Any]			=    TFDeiTForImageClassification(lowercase)
          a__						:     Optional[Any]			=    model(lowercase ,  labels=lowercase)
          self.parent.assertEqual(result.logits.shape ,  (self.batch_size, self.type_sequence_label_size))

          # test greyscale images
          a__						:     str			=    1
          a__						:     Optional[int]			=    TFDeiTForImageClassification(lowercase)

          a__						:     int			=    floats_tensor([self.batch_size, 1, self.image_size, self.image_size])
          a__						:     Optional[int]			=    model(lowercase ,  labels=lowercase)
          self.parent.assertEqual(result.logits.shape ,  (self.batch_size, self.type_sequence_label_size))

   def 							__lowercase		( self)      ->						List[str]:







          '''simple docstring'''

          a__						:     Optional[Any]			=    self.prepare_config_and_inputs()
          a__			,							a__			,							a__						:     str			=    config_and_inputs
          a__						:     Optional[int]			=    {'pixel_values': pixel_values}
          return config, inputs_dict





@require_tf
class 			A__   (							__UpperCAmelCase			,  __UpperCAmelCase			,  unittest.TestCase     ):




   """simple docstring"""



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

   __A       :    List[Any]	  =  False
   __A       :    Any	  =  False
   __A       :    List[Any]	  =  False
   __A       :    Union[str, Any]	  =  False

   def 							__lowercase		( self)      ->						Optional[Any]:







          '''simple docstring'''

          a__						:     Union[str, Any]			=    TFDeiTModelTester(self)
          a__						:     Any			=    ConfigTester(self ,  config_class=lowercase ,  has_text_modality=lowercase ,  hidden_size=37)

   def 							__lowercase		( self)      ->						List[str]:







          '''simple docstring'''

          self.config_tester.run_common_tests()

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







          '''simple docstring'''

          pass

   def 							__lowercase		( self)      ->						Optional[Any]:







          '''simple docstring'''

          a__			,							a__						:     List[Any]			=    self.model_tester.prepare_config_and_inputs_for_common()

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

   def 							__lowercase		( self)      ->						Optional[int]:







          '''simple docstring'''

          a__			,							a__						:     List[Any]			=    self.model_tester.prepare_config_and_inputs_for_common()

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

                 a__						:     Tuple			=    ['pixel_values']
                 self.assertListEqual(arg_names[:1] ,  lowercase)

   def 							__lowercase		( self)      ->						Tuple:







          '''simple docstring'''

          a__						:     Dict			=    self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_model(*lowercase)

   def 							__lowercase		( self)      ->						Union[str, Any]:







          '''simple docstring'''

          a__						:     Any			=    self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_masked_image_modeling(*lowercase)

   def 							__lowercase		( self)      ->						Any:







          '''simple docstring'''

          a__						:     List[Any]			=    self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_image_classification(*lowercase)

   def 							__lowercase		( self ,  lowercase ,  lowercase ,  lowercase=False)      ->						Dict:







          '''simple docstring'''

          a__						:     List[Any]			=    super()._prepare_for_class(lowercase ,  lowercase ,  return_labels=lowercase)

          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)      ->						Optional[Any]:







          '''simple docstring'''

          for model_name in TF_DEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                 a__						:     Union[str, Any]			=    TFDeiTModel.from_pretrained(lowercase)
                 self.assertIsNotNone(lowercase)



def A_     (		)  ->		Any:
       a__						:     List[Any]			=    Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'    )
       return image





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




   """simple docstring"""



   @cached_property
   def 							__lowercase		( self)      ->						Union[str, Any]:







          '''simple docstring'''

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

   @slow
   def 							__lowercase		( self)      ->						str:







          '''simple docstring'''

          a__						:     int			=    TFDeiTForImageClassificationWithTeacher.from_pretrained('facebook/deit-base-distilled-patch16-224')

          a__						:     List[str]			=    self.default_image_processor
          a__						:     str			=    prepare_img()
          a__						:     str			=    image_processor(images=lowercase ,  return_tensors='tf')

          # forward pass
          a__						:     Tuple			=    model(**lowercase)

          # verify the logits
          a__						:     Optional[Any]			=    tf.TensorShape((1, 1000))
          self.assertEqual(outputs.logits.shape ,  lowercase)

          a__						:     Optional[int]			=    tf.constant([-1.02_66, 0.19_12, -1.28_61])

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









import unittest

from transformers import is_tf_available
from transformers.testing_utils import require_tf


if is_tf_available():
    import tensorflow as tf
    from tensorflow.python.eager import context
    from tensorflow.python.framework import ops

    from transformers import GradientAccumulator, create_optimizer





@require_tf
class 			A__   (							unittest.TestCase     ):




   """simple docstring"""



   def 							__lowercase		( self ,  lowercase ,  lowercase ,  lowercase)      ->						List[str]:







          '''simple docstring'''

          self.assertEqual(len(lowercase) ,  len(lowercase))
          for a, b in zip(lowercase ,  lowercase):
                 self.assertAlmostEqual(lowercase ,  lowercase ,  delta=lowercase)

   def 							__lowercase		( self)      ->						Any:







          '''simple docstring'''

          a__						:     Tuple			=    GradientAccumulator()
          accumulator([tf.constant([1.0, 2.0])])
          accumulator([tf.constant([-2.0, 1.0])])
          accumulator([tf.constant([-1.0, 2.0])])
          with self.assertRaises(lowercase):
                 accumulator([tf.constant([1.0, 1.0]), tf.constant([2.0, 2.0])])
          self.assertEqual(accumulator.step ,  3)
          self.assertEqual(len(accumulator.gradients) ,  1)
          self.assertListAlmostEqual(accumulator.gradients[0].numpy().tolist() ,  [-2.0, 5.0] ,  tol=1e-2)
          accumulator.reset()
          self.assertEqual(accumulator.step ,  0)
          self.assertListAlmostEqual(accumulator.gradients[0].numpy().tolist() ,  [0.0, 0.0] ,  tol=1e-2)

   def 							__lowercase		( self)      ->						List[Any]:







          '''simple docstring'''

          a__						:     Tuple			=    None
          ops.enable_eager_execution_internal()
          a__						:     Optional[int]			=    tf.config.list_physical_devices('CPU')
          if len(lowercase) == 1:
                 tf.config.set_logical_device_configuration(
                     physical_devices[0] ,  [tf.config.LogicalDeviceConfiguration(), tf.config.LogicalDeviceConfiguration()])
          a__						:     int			=    tf.config.list_logical_devices(device_type='CPU')
          a__						:     Union[str, Any]			=    tf.distribute.MirroredStrategy(devices=devices[:2])

          with strategy.scope():
                 a__						:     str			=    GradientAccumulator()
                 a__						:     Tuple			=    tf.Variable([4.0, 3.0])
                 a__			,							a__						:     Tuple			=    create_optimizer(5e-5 ,  10 ,  5)
                 a__						:     Tuple			=    tf.Variable([0.0, 0.0] ,  trainable=lowercase)

          def accumulate_on_replica(lowercase):
                 accumulator([gradient])

          def apply_on_replica():
                 optimizer.apply_gradients(list(zip(accumulator.gradients ,  [variable])))

          @tf.function
          def accumulate(lowercase ,  lowercase):
                 with strategy.scope():
                        a__						:     Union[str, Any]			=    strategy.experimental_local_results(lowercase)
                        local_variables[0].assign(lowercase)
                        local_variables[1].assign(lowercase)
                        strategy.run(lowercase ,  args=(gradient_placeholder,))

          @tf.function
          def apply_grad():
                 with strategy.scope():
                        strategy.run(lowercase)

          def _check_local_values(lowercase ,  lowercase):
                 a__						:     Union[str, Any]			=    strategy.experimental_local_results(accumulator._gradients[0])
                 self.assertListAlmostEqual(values[0].value() ,  lowercase ,  tol=1e-2)
                 self.assertListAlmostEqual(values[1].value() ,  lowercase ,  tol=1e-2)

          accumulate([1.0, 2.0] ,  [-1.0, 1.0])
          accumulate([3.0, -1.0] ,  [-1.0, -1.0])
          accumulate([-2.0, 2.0] ,  [3.0, -2.0])
          self.assertEqual(accumulator.step ,  3)
          _check_local_values([2.0, 3.0] ,  [1.0, -2.0])
          apply_grad()
          self.assertListAlmostEqual(variable.value() ,  [4.0, 3.0] ,  tol=1e-2)
          accumulator.reset()
          self.assertEqual(accumulator.step ,  0)
          _check_local_values([0.0, 0.0] ,  [0.0, 0.0])


