





import contextlib
import copy
import random
from typing import Any, Dict, Iterable, Optional, Union

import numpy as np
import torch

from .utils import deprecate, is_transformers_available


if is_transformers_available():
    import transformers



def 		A_    (					_lowerCAmelCase					)						->			str:
 random.seed(_lowerCAmelCase					)
 np.random.seed(_lowerCAmelCase					)
 torch.manual_seed(_lowerCAmelCase					)
 torch.cuda.manual_seed_all(_lowerCAmelCase					)
 # ^^ safe to call this function even if cuda is not available



class  A__       :





      def __init__(			self	,							A_	,							A_ = 0.99_99	,							A_ = 0.0	,							A_ = 0	,							A_ = False	,							A_ = 1.0	,							A_ = 2 / 3	,							A_ = None	,							A_ = None	,							**A_	,							):
       '''simple docstring'''




       if isinstance(A_	,							torch.nn.Module       ):
        UpperCamelCase     : Optional[int]	     =       (
            "Passing a `torch.nn.Module` to `ExponentialMovingAverage` is deprecated. "
            "Please pass the parameters of the module instead."
        )
        deprecate(
            "passing a `torch.nn.Module` to `ExponentialMovingAverage`"	,							"1.0.0"	,							A_	,							standard_warn=A_	,							)
        UpperCamelCase     : List[str]	     =       parameters.parameters()

        # set use_ema_warmup to True if a torch.nn.Module is passed for backwards compatibility
        UpperCamelCase     : str	     =       True

       if kwargs.get("max_value"	,							A_       ) is not None:
        UpperCamelCase     : int	     =       "The `max_value` argument is deprecated. Please use `decay` instead."
        deprecate("max_value"	,							"1.0.0"	,							A_	,							standard_warn=A_       )
        UpperCamelCase     : Dict	     =       kwargs["max_value"]

       if kwargs.get("min_value"	,							A_       ) is not None:
        UpperCamelCase     : int	     =       "The `min_value` argument is deprecated. Please use `min_decay` instead."
        deprecate("min_value"	,							"1.0.0"	,							A_	,							standard_warn=A_       )
        UpperCamelCase     : Any	     =       kwargs["min_value"]

       UpperCamelCase     : Dict	     =       list(A_       )
       UpperCamelCase     : List[Any]	     =       [p.clone().detach() for p in parameters]

       if kwargs.get("device"	,							A_       ) is not None:
        UpperCamelCase     : str	     =       "The `device` argument is deprecated. Please use `to` instead."
        deprecate("device"	,							"1.0.0"	,							A_	,							standard_warn=A_       )
        self.to(device=kwargs["device"]       )

       UpperCamelCase     : Tuple	     =       None

       UpperCamelCase     : int	     =       decay
       UpperCamelCase     : Tuple	     =       min_decay
       UpperCamelCase     : Union[str, Any]	     =       update_after_step
       UpperCamelCase     : str	     =       use_ema_warmup
       UpperCamelCase     : Any	     =       inv_gamma
       UpperCamelCase     : str	     =       power
       UpperCamelCase     : Optional[int]	     =       0
       UpperCamelCase     : Optional[Any]	     =       None  # set in `step()`

       UpperCamelCase     : Tuple	     =       model_cls
       UpperCamelCase     : Dict	     =       model_config





      @classmethod
      def 	__UpperCamelCase(			cls	,							A_	,							A_       ):
       '''simple docstring'''




       UpperCamelCase     ,   UpperCamelCase     : List[str]	     =       model_cls.load_config(A_	,							return_unused_kwargs=A_       )
       UpperCamelCase     : Tuple	     =       model_cls.from_pretrained(A_       )

       UpperCamelCase     : Optional[Any]	     =       cls(model.parameters()	,							model_cls=A_	,							model_config=model.config       )

       ema_model.load_state_dict(A_       )
       return ema_model





      def 	__UpperCamelCase(			self	,							A_       ):
       '''simple docstring'''




       if self.model_cls is None:
        raise ValueError("`save_pretrained` can only be used if `model_cls` was defined at __init__."       )

       if self.model_config is None:
        raise ValueError("`save_pretrained` can only be used if `model_config` was defined at __init__."       )

       UpperCamelCase     : Any	     =       self.model_cls.from_config(self.model_config       )
       UpperCamelCase     : str	     =       self.state_dict()
       state_dict.pop("shadow_params"	,							A_       )

       model.register_to_config(**A_       )
       self.copy_to(model.parameters()       )
       model.save_pretrained(A_       )





      def 	__UpperCamelCase(			self	,							A_       ):
       '''simple docstring'''




       UpperCamelCase     : str	     =       max(0	,							optimization_step - self.update_after_step - 1       )

       if step <= 0:
        return 0.0

       if self.use_ema_warmup:
        UpperCamelCase     : str	     =       1 - (1 + step / self.inv_gamma) ** -self.power
       else:
        UpperCamelCase     : Optional[Any]	     =       (1 + step) / (10 + step)

       UpperCamelCase     : int	     =       min(A_	,							self.decay       )
       # make sure decay is not smaller than min_decay
       UpperCamelCase     : Optional[Any]	     =       max(A_	,							self.min_decay       )
       return cur_decay_value





      @torch.no_grad()
      def 	__UpperCamelCase(			self	,							A_       ):
       '''simple docstring'''




       if isinstance(A_	,							torch.nn.Module       ):
        UpperCamelCase     : Tuple	     =       (
            "Passing a `torch.nn.Module` to `ExponentialMovingAverage.step` is deprecated. "
            "Please pass the parameters of the module instead."
        )
        deprecate(
            "passing a `torch.nn.Module` to `ExponentialMovingAverage.step`"	,							"1.0.0"	,							A_	,							standard_warn=A_	,							)
        UpperCamelCase     : Dict	     =       parameters.parameters()

       UpperCamelCase     : Optional[int]	     =       list(A_       )

       self.optimization_step += 1

       # Compute the decay factor for the exponential moving average.
       UpperCamelCase     : str	     =       self.get_decay(self.optimization_step       )
       UpperCamelCase     : Optional[Any]	     =       decay
       UpperCamelCase     : Dict	     =       1 - decay

       UpperCamelCase     : List[Any]	     =       contextlib.nullcontext
       if is_transformers_available() and transformers.deepspeed.is_deepspeed_zeroa_enabled():
        import deepspeed

       for s_param, param in zip(self.shadow_params	,							A_       ):
        if is_transformers_available() and transformers.deepspeed.is_deepspeed_zeroa_enabled():
         UpperCamelCase     : Tuple	     =       deepspeed.zero.GatheredParameters(A_	,							modifier_rank=A_       )

        with context_manager():
         if param.requires_grad:
          s_param.sub_(one_minus_decay * (s_param - param)       )
         else:
          s_param.copy_(A_       )





      def 	__UpperCamelCase(			self	,							A_       ):
       '''simple docstring'''




       UpperCamelCase     : Optional[int]	     =       list(A_       )
       for s_param, param in zip(self.shadow_params	,							A_       ):
        param.data.copy_(s_param.to(param.device       ).data       )





      def 	__UpperCamelCase(			self	,							A_=None	,							A_=None       ):
       '''simple docstring'''




       UpperCamelCase     : int	     =       [
           p.to(device=A_	,							dtype=A_       ) if p.is_floating_point() else p.to(device=A_       )
           for p in self.shadow_params
       ]





      def 	__UpperCamelCase(			self       ):
       '''simple docstring'''




       return {
           "decay": self.decay,
           "min_decay": self.min_decay,
           "optimization_step": self.optimization_step,
           "update_after_step": self.update_after_step,
           "use_ema_warmup": self.use_ema_warmup,
           "inv_gamma": self.inv_gamma,
           "power": self.power,
           "shadow_params": self.shadow_params,
       }





      def 	__UpperCamelCase(			self	,							A_       ):
       '''simple docstring'''




       UpperCamelCase     : List[Any]	     =       [param.detach().cpu().clone() for param in parameters]





      def 	__UpperCamelCase(			self	,							A_       ):
       '''simple docstring'''




       if self.temp_stored_params is None:
        raise RuntimeError("This ExponentialMovingAverage has no `store()`ed weights " "to `restore()`"       )
       for c_param, param in zip(self.temp_stored_params	,							A_       ):
        param.data.copy_(c_param.data       )

       # Better memory-wise.
       UpperCamelCase     : int	     =       None





      def 	__UpperCamelCase(			self	,							A_       ):
       '''simple docstring'''




       UpperCamelCase     : Dict	     =       copy.deepcopy(A_       )

       UpperCamelCase     : Dict	     =       state_dict.get("decay"	,							self.decay       )
       if self.decay < 0.0 or self.decay > 1.0:
        raise ValueError("Decay must be between 0 and 1"       )

       UpperCamelCase     : List[Any]	     =       state_dict.get("min_decay"	,							self.min_decay       )
       if not isinstance(self.min_decay	,							A_       ):
        raise ValueError("Invalid min_decay"       )

       UpperCamelCase     : Tuple	     =       state_dict.get("optimization_step"	,							self.optimization_step       )
       if not isinstance(self.optimization_step	,							A_       ):
        raise ValueError("Invalid optimization_step"       )

       UpperCamelCase     : List[str]	     =       state_dict.get("update_after_step"	,							self.update_after_step       )
       if not isinstance(self.update_after_step	,							A_       ):
        raise ValueError("Invalid update_after_step"       )

       UpperCamelCase     : int	     =       state_dict.get("use_ema_warmup"	,							self.use_ema_warmup       )
       if not isinstance(self.use_ema_warmup	,							A_       ):
        raise ValueError("Invalid use_ema_warmup"       )

       UpperCamelCase     : Tuple	     =       state_dict.get("inv_gamma"	,							self.inv_gamma       )
       if not isinstance(self.inv_gamma	,							(float, int)       ):
        raise ValueError("Invalid inv_gamma"       )

       UpperCamelCase     : str	     =       state_dict.get("power"	,							self.power       )
       if not isinstance(self.power	,							(float, int)       ):
        raise ValueError("Invalid power"       )

       UpperCamelCase     : int	     =       state_dict.get("shadow_params"	,							A_       )
       if shadow_params is not None:
        UpperCamelCase     : int	     =       shadow_params
        if not isinstance(self.shadow_params	,							A_       ):
         raise ValueError("shadow_params must be a list"       )
        if not all(isinstance(A_	,							torch.Tensor       ) for p in self.shadow_params       ):
         raise ValueError("shadow_params must all be Tensors"       )







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	,							A_	,							A_=13	,							A_=30	,							A_=2	,							A_=3	,							A_=True	,							A_=True	,							A_=32	,							A_=2	,							A_=4	,							A_=37	,							A_="gelu"	,							A_=0.1	,							A_=0.1	,							A_=10	,							A_=0.02	,							A_=3	,							A_=None	,							A_=2	,							):
       '''simple docstring'''




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

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





      def 	__UpperCamelCase(			self       ):
       '''simple docstring'''




       UpperCamelCase     : Tuple	     =       floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]       )

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

       UpperCamelCase     : Union[str, Any]	     =       self.get_config()

       return config, pixel_values, labels





      def 	__UpperCamelCase(			self       ):
       '''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	,							A_	,							A_	,							A_       ):
       '''simple docstring'''




       UpperCamelCase     : Optional[Any]	     =       TFDeiTModel(config=A_       )
       UpperCamelCase     : Tuple	     =       model(A_       )
       self.parent.assertEqual(result.last_hidden_state.shape	,							(self.batch_size, self.seq_length, self.hidden_size)       )





      def 	__UpperCamelCase(			self	,							A_	,							A_	,							A_       ):
       '''simple docstring'''




       UpperCamelCase     : List[str]	     =       TFDeiTForMaskedImageModeling(config=A_       )
       UpperCamelCase     : Optional[Any]	     =       model(A_       )
       self.parent.assertEqual(
           result.reconstruction.shape	,							(self.batch_size, self.num_channels, self.image_size, self.image_size)       )

       # test greyscale images
       UpperCamelCase     : Dict	     =       1
       UpperCamelCase     : Optional[Any]	     =       TFDeiTForMaskedImageModeling(A_       )

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





      def 	__UpperCamelCase(			self	,							A_	,							A_	,							A_       ):
       '''simple docstring'''




       UpperCamelCase     : Optional[Any]	     =       self.type_sequence_label_size
       UpperCamelCase     : List[Any]	     =       TFDeiTForImageClassification(A_       )
       UpperCamelCase     : Optional[int]	     =       model(A_	,							labels=A_       )
       self.parent.assertEqual(result.logits.shape	,							(self.batch_size, self.type_sequence_label_size)       )

       # test greyscale images
       UpperCamelCase     : List[Any]	     =       1
       UpperCamelCase     : Optional[Any]	     =       TFDeiTForImageClassification(A_       )

       UpperCamelCase     : Optional[int]	     =       floats_tensor([self.batch_size, 1, self.image_size, self.image_size]       )
       UpperCamelCase     : List[Any]	     =       model(A_	,							labels=A_       )
       self.parent.assertEqual(result.logits.shape	,							(self.batch_size, self.type_sequence_label_size)       )





      def 	__UpperCamelCase(			self       ):
       '''simple docstring'''




       UpperCamelCase     : Union[str, Any]	     =       self.prepare_config_and_inputs()
       UpperCamelCase     ,   UpperCamelCase     ,   UpperCamelCase     : int	     =       config_and_inputs
       UpperCamelCase     : Union[str, Any]	     =       {"pixel_values": pixel_values}
       return config, inputs_dict



@require_tf
class  A__       (	__snake_case	,       __snake_case	,       unittest.TestCase      ):
      _UpperCAmelCase     :str						   =       (
          (
              TFDeiTModel,
              TFDeiTForImageClassification,
              TFDeiTForImageClassificationWithTeacher,
              TFDeiTForMaskedImageModeling,
          )
          if is_tf_available()
          else ()
      )
      _UpperCAmelCase     :Tuple						   =       (
          {
              'feature-extraction': TFDeiTModel,
              'image-classification': (TFDeiTForImageClassification, TFDeiTForImageClassificationWithTeacher),
          }
          if is_tf_available()
          else {}
      )

      _UpperCAmelCase     :Dict						   =       False
      _UpperCAmelCase     :List[str]						   =       False
      _UpperCAmelCase     :Optional[Any]						   =       False
      _UpperCAmelCase     :Optional[int]						   =       False





      def 	__UpperCamelCase(			self       ):
       '''simple docstring'''




       UpperCamelCase     : Union[str, Any]	     =       TFDeiTModelTester(self       )
       UpperCamelCase     : Optional[Any]	     =       ConfigTester(self	,							config_class=A_	,							has_text_modality=A_	,							hidden_size=37       )





      def 	__UpperCamelCase(			self       ):
       '''simple docstring'''




       self.config_tester.run_common_tests()





      @unittest.skip(reason="DeiT does not use inputs_embeds"       )
      def 	__UpperCamelCase(			self       ):
       '''simple docstring'''




       pass





      def 	__UpperCamelCase(			self       ):
       '''simple docstring'''




       UpperCamelCase     ,   UpperCamelCase     : str	     =       self.model_tester.prepare_config_and_inputs_for_common()

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





      def 	__UpperCamelCase(			self       ):
       '''simple docstring'''




       UpperCamelCase     ,   UpperCamelCase     : Optional[int]	     =       self.model_tester.prepare_config_and_inputs_for_common()

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

        UpperCamelCase     : Any	     =       ["pixel_values"]
        self.assertListEqual(arg_names[:1]	,							A_       )





      def 	__UpperCamelCase(			self       ):
       '''simple docstring'''




       UpperCamelCase     : Optional[Any]	     =       self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*A_       )





      def 	__UpperCamelCase(			self       ):
       '''simple docstring'''




       UpperCamelCase     : int	     =       self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_masked_image_modeling(*A_       )





      def 	__UpperCamelCase(			self       ):
       '''simple docstring'''




       UpperCamelCase     : List[str]	     =       self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_image_classification(*A_       )





      def 	__UpperCamelCase(			self	,							A_	,							A_	,							A_=False       ):
       '''simple docstring'''




       UpperCamelCase     : List[str]	     =       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       ):
       '''simple docstring'''




       for model_name in TF_DEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
        UpperCamelCase     : str	     =       TFDeiTModel.from_pretrained(A_       )
        self.assertIsNotNone(A_       )



def 		A_    (					)						->			str:
 UpperCamelCase     : Union[str, Any]	     =       Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"					)
 return image



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





      @cached_property
      def 	__UpperCamelCase(			self       ):
       '''simple docstring'''




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





      @slow
      def 	__UpperCamelCase(			self       ):
       '''simple docstring'''




       UpperCamelCase     : str	     =       TFDeiTForImageClassificationWithTeacher.from_pretrained("facebook/deit-base-distilled-patch16-224"       )

       UpperCamelCase     : List[Any]	     =       self.default_image_processor
       UpperCamelCase     : Union[str, Any]	     =       prepare_img()
       UpperCamelCase     : Union[str, Any]	     =       image_processor(images=A_	,							return_tensors="tf"       )

       # forward pass
       UpperCamelCase     : str	     =       model(**A_       )

       # verify the logits
       UpperCamelCase     : Dict	     =       tf.TensorShape((1, 1000)       )
       self.assertEqual(outputs.logits.shape	,							A_       )

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

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