




import numpy as np



def 		A_    (					_lowerCAmelCase							,       _lowerCAmelCase							,       _lowerCAmelCase = 1e-12							,       _lowerCAmelCase = 100							,       )						->			List[str]:
 assert np.shape(A__					)[0] == np.shape(A__					)[1]
 # Ensure proper dimensionality.
 assert np.shape(A__					)[0] == np.shape(A__					)[0]
 # Ensure inputs are either both complex or both real
 assert np.iscomplexobj(A__					) == np.iscomplexobj(A__					)
 UpperCamelCase     : List[Any]	     =       np.iscomplexobj(A__					)
 if is_complex:
  # Ensure complex input_matrix is Hermitian
  assert np.array_equal(A__							,       input_matrix.conj().T					)

 # Set convergence to False. Will define convergence when we exceed max_iterations
 # or when we have small changes from one iteration to next.

 UpperCamelCase     : Dict	     =       False
 UpperCamelCase     : Optional[int]	     =       0
 UpperCamelCase     : List[str]	     =       0
 UpperCamelCase     : Any	     =       1e12

 while not convergence:
  # Multiple matrix by the vector.
  UpperCamelCase     : List[Any]	     =       np.dot(A__							,       A__					)
  # Normalize the resulting output vector.
  UpperCamelCase     : List[str]	     =       w / np.linalg.norm(A__					)
  # Find rayleigh quotient
  # (faster than usual b/c we know vector is normalized already)
  UpperCamelCase     : Union[str, Any]	     =       vector.conj().T if is_complex else vector.T
  UpperCamelCase     : List[str]	     =       np.dot(A__							,       np.dot(A__							,       A__					)					)

  # Check convergence.
  UpperCamelCase     : Tuple	     =       np.abs(lambda_ - lambda_previous					) / lambda_
  iterations += 1

  if error <= error_tol or iterations >= max_iterations:
   UpperCamelCase     : Dict	     =       True

  UpperCamelCase     : Tuple	     =       lambda_

 if is_complex:
  UpperCamelCase     : Tuple	     =       np.real(lambda_					)

 return lambda_, vector



def 		A_    (					)						->			Dict:
 UpperCamelCase     : Dict	     =       np.array([[41, 4, 20], [4, 26, 30], [20, 30, 50]]					)
 UpperCamelCase     : List[Any]	     =       np.array([41, 4, 20]					)
 UpperCamelCase     : Tuple	     =       real_input_matrix.astype(np.complexaaa					)
 UpperCamelCase     : Tuple	     =       np.triu(1j * complex_input_matrix							,       1					)
 complex_input_matrix += imag_matrix
 complex_input_matrix += -1 * imag_matrix.T
 UpperCamelCase     : str	     =       np.array([41, 4, 20]					).astype(np.complexaaa					)

 for problem_type in ["real", "complex"]:
  if problem_type == "real":
   UpperCamelCase     : Optional[int]	     =       real_input_matrix
   UpperCamelCase     : Dict	     =       real_vector
  elif problem_type == "complex":
   UpperCamelCase     : Optional[Any]	     =       complex_input_matrix
   UpperCamelCase     : Any	     =       complex_vector

  # Our implementation.
  UpperCamelCase     ,   UpperCamelCase     : Optional[Any]	     =       power_iteration(A__							,       A__					)

  # Numpy implementation.

  # Get eigenvalues and eigenvectors using built-in numpy
  # eigh (eigh used for symmetric or hermetian matrices).
  UpperCamelCase     ,   UpperCamelCase     : Any	     =       np.linalg.eigh(A__					)
  # Last eigenvalue is the maximum one.
  UpperCamelCase     : Dict	     =       eigen_values[-1]
  # Last column in this matrix is eigenvector corresponding to largest eigenvalue.
  UpperCamelCase     : str	     =       eigen_vectors[:, -1]

  # Check our implementation and numpy gives close answers.
  assert np.abs(eigen_value - eigen_value_max					) <= 1e-6
  # Take absolute values element wise of each eigenvector.
  # as they are only unique to a minus sign.
  assert np.linalg.norm(np.abs(A__					) - np.abs(A__					)					) <= 1e-6


if __name__ == "__main__":
    import doctest

    doctest.testmod()
    test_power_iteration()







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       )       )
