'''simple docstring'''





import inspect
import unittest
import warnings
from math import ceil, floor

from transformers import LevitConfig
from transformers.file_utils import cached_property, is_torch_available, is_vision_available
from transformers.models.auto import get_values
from transformers.testing_utils import require_torch, require_vision, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
     import torch

     from transformers import (
         MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING,
         MODEL_MAPPING,
         LevitForImageClassification,
         LevitForImageClassificationWithTeacher,
         LevitModel,
     )
     from transformers.models.levit.modeling_levit import LEVIT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
     from PIL import Image

     from transformers import LevitImageProcessor



class  UpperCamelCase				( lowercase__ ):

     '''simple docstring'''

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






           """simple docstring"""
           _lowerCamelCase			    =				self.config_class(**self.inputs_dict						)
           self.parent.assertTrue(hasattr(__lowercase				,				'''hidden_sizes'''						)						)
           self.parent.assertTrue(hasattr(__lowercase				,				'''num_attention_heads'''						)						)





class  UpperCamelCase				:

     '''simple docstring'''

     def __init__(					self				,				A_				,				A_=13				,				A_=64				,				A_=3				,				A_=3				,				A_=2				,				A_=1				,				A_=16				,				A_=[1_28, 2_56, 3_84]				,				A_=[4, 6, 8]				,				A_=[2, 3, 4]				,				A_=[16, 16, 16]				,				A_=0				,				A_=[2, 2, 2]				,				A_=[2, 2, 2]				,				A_=0.02				,				A_=True				,				A_=True				,				A_=2				,				) ->				Optional[int]:






           """simple docstring"""
           _lowerCamelCase			    =				parent
           _lowerCamelCase			    =				batch_size
           _lowerCamelCase			    =				image_size
           _lowerCamelCase			    =				num_channels
           _lowerCamelCase			    =				kernel_size
           _lowerCamelCase			    =				stride
           _lowerCamelCase			    =				padding
           _lowerCamelCase			    =				hidden_sizes
           _lowerCamelCase			    =				num_attention_heads
           _lowerCamelCase			    =				depths
           _lowerCamelCase			    =				key_dim
           _lowerCamelCase			    =				drop_path_rate
           _lowerCamelCase			    =				patch_size
           _lowerCamelCase			    =				attention_ratio
           _lowerCamelCase			    =				mlp_ratio
           _lowerCamelCase			    =				initializer_range
           _lowerCamelCase			    =				[
               ['''Subsample''', key_dim[0], hidden_sizes[0] // key_dim[0], 4, 2, 2],
               ['''Subsample''', key_dim[0], hidden_sizes[1] // key_dim[0], 4, 2, 2],
           ]
           _lowerCamelCase			    =				is_training
           _lowerCamelCase			    =				use_labels
           _lowerCamelCase			    =				num_labels
           _lowerCamelCase			    =				initializer_range



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






           """simple docstring"""
           _lowerCamelCase			    =				floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]						)

           _lowerCamelCase			    =				None
           if self.use_labels:
                 _lowerCamelCase			    =				ids_tensor([self.batch_size]				,				self.num_labels						)

           _lowerCamelCase			    =				self.get_config()
           return config, pixel_values, labels



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






           """simple docstring"""
           return LevitConfig(
               image_size=self.image_size				,				num_channels=self.num_channels				,				kernel_size=self.kernel_size				,				stride=self.stride				,				padding=self.padding				,				patch_size=self.patch_size				,				hidden_sizes=self.hidden_sizes				,				num_attention_heads=self.num_attention_heads				,				depths=self.depths				,				key_dim=self.key_dim				,				drop_path_rate=self.drop_path_rate				,				mlp_ratio=self.mlp_ratio				,				attention_ratio=self.attention_ratio				,				initializer_range=self.initializer_range				,				down_ops=self.down_ops				,				)



     def 		UpperCamelCase_						(					self				,				A_				,				A_				,				A_						) ->				Dict:






           """simple docstring"""
           _lowerCamelCase			    =				LevitModel(config=__lowercase						)
           model.to(__lowercase						)
           model.eval()
           _lowerCamelCase			    =				model(__lowercase						)
           _lowerCamelCase			    =				(self.image_size, self.image_size)
           _lowerCamelCase    ,					_lowerCamelCase			    =				image_size[0], image_size[1]
           for _ in range(4						):
                 _lowerCamelCase			    =				floor(((height + 2 * self.padding - self.kernel_size) / self.stride) + 1						)
                 _lowerCamelCase			    =				floor(((width + 2 * self.padding - self.kernel_size) / self.stride) + 1						)
           self.parent.assertEqual(
               result.last_hidden_state.shape				,				(self.batch_size, ceil(height / 4						) * ceil(width / 4						), self.hidden_sizes[-1])				,				)



     def 		UpperCamelCase_						(					self				,				A_				,				A_				,				A_						) ->				Optional[int]:






           """simple docstring"""
           _lowerCamelCase			    =				self.num_labels
           _lowerCamelCase			    =				LevitForImageClassification(__lowercase						)
           model.to(__lowercase						)
           model.eval()
           _lowerCamelCase			    =				model(__lowercase				,				labels=__lowercase						)
           self.parent.assertEqual(result.logits.shape				,				(self.batch_size, self.num_labels)						)





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






           """simple docstring"""
           _lowerCamelCase			    =				self.prepare_config_and_inputs()
           _lowerCamelCase    ,					_lowerCamelCase    ,					_lowerCamelCase			    =				config_and_inputs
           _lowerCamelCase			    =				{'''pixel_values''': pixel_values}
           return config, inputs_dict





@require_torch
class  UpperCamelCase				( lowercase__   ,  lowercase__   ,  unittest.TestCase ):

     '''simple docstring'''

     A_										=  (
         (LevitModel, LevitForImageClassification, LevitForImageClassificationWithTeacher)
         if is_torch_available()
         else ()
     )
     A_										=  (
         {
             """feature-extraction""": LevitModel,
             """image-classification""": (LevitForImageClassification, LevitForImageClassificationWithTeacher),
         }
         if is_torch_available()
         else {}
     )

     A_										=  False
     A_										=  False
     A_										=  False
     A_										=  False
     A_										=  False



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






           """simple docstring"""
           _lowerCamelCase			    =				LevitModelTester(self						)
           _lowerCamelCase			    =				ConfigTester(self				,				config_class=__lowercase				,				has_text_modality=__lowercase				,				hidden_size=37						)



     def 		UpperCamelCase_						(					self						) ->				Tuple:






           """simple docstring"""
           self.create_and_test_config_common_properties()
           self.config_tester.create_and_test_config_to_json_string()
           self.config_tester.create_and_test_config_to_json_file()
           self.config_tester.create_and_test_config_from_and_save_pretrained()
           self.config_tester.create_and_test_config_with_num_labels()
           self.config_tester.check_config_can_be_init_without_params()
           self.config_tester.check_config_arguments_init()



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






           """simple docstring"""
           return



     @unittest.skip(reason='''Levit does not use inputs_embeds'''						)
     def 		UpperCamelCase_						(					self						) ->				List[Any]:






           """simple docstring"""
           pass



     @unittest.skip(reason='''Levit does not support input and output embeddings'''						)
     def 		UpperCamelCase_						(					self						) ->				int:






           """simple docstring"""
           pass



     @unittest.skip(reason='''Levit does not output attentions'''						)
     def 		UpperCamelCase_						(					self						) ->				Union[str, Any]:






           """simple docstring"""
           pass



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






           """simple docstring"""
           _lowerCamelCase    ,					_lowerCamelCase			    =				self.model_tester.prepare_config_and_inputs_for_common()

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

                 _lowerCamelCase			    =				['''pixel_values''']
                 self.assertListEqual(arg_names[:1]				,				__lowercase						)



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






           """simple docstring"""
           def check_hidden_states_output(A_				,				A_				,				A_						):
                 _lowerCamelCase			    =				model_class(__lowercase						)
                 model.to(__lowercase						)
                 model.eval()

                 with torch.no_grad():
                       _lowerCamelCase			    =				model(**self._prepare_for_class(__lowercase				,				__lowercase						)						)

                 _lowerCamelCase			    =				outputs.hidden_states

                 _lowerCamelCase			    =				len(self.model_tester.depths						) + 1
                 self.assertEqual(len(__lowercase						)				,				__lowercase						)

                 _lowerCamelCase			    =				(self.model_tester.image_size, self.model_tester.image_size)
                 _lowerCamelCase    ,					_lowerCamelCase			    =				image_size[0], image_size[1]
                 for _ in range(4						):
                       _lowerCamelCase			    =				floor(
                           (
                               (height + 2 * self.model_tester.padding - self.model_tester.kernel_size)
                               / self.model_tester.stride
                           )
                           + 1						)
                       _lowerCamelCase			    =				floor(
                           (
                               (width + 2 * self.model_tester.padding - self.model_tester.kernel_size)
                               / self.model_tester.stride
                           )
                           + 1						)
                 # verify the first hidden states (first block)
                 self.assertListEqual(
                     list(hidden_states[0].shape[-2:]						)				,				[
                         height * width,
                         self.model_tester.hidden_sizes[0],
                     ]				,				)

           _lowerCamelCase    ,					_lowerCamelCase			    =				self.model_tester.prepare_config_and_inputs_for_common()

           for model_class in self.all_model_classes:
                 _lowerCamelCase			    =				True
                 check_hidden_states_output(__lowercase				,				__lowercase				,				__lowercase						)

                 # check that output_hidden_states also work using config
                 del inputs_dict["output_hidden_states"]
                 _lowerCamelCase			    =				True

                 check_hidden_states_output(__lowercase				,				__lowercase				,				__lowercase						)



     @unittest.skip('''Will be fixed soon by reducing the size of the model used for common tests.'''						)
     def 		UpperCamelCase_						(					self						) ->				Tuple:






           """simple docstring"""
           pass



     def 		UpperCamelCase_						(					self				,				A_				,				A_				,				A_=False						) ->				Any:






           """simple docstring"""
           _lowerCamelCase			    =				super()._prepare_for_class(__lowercase				,				__lowercase				,				return_labels=__lowercase						)

           if return_labels:
                 if model_class.__name__ == "LevitForImageClassificationWithTeacher":
                       del inputs_dict["labels"]

           return inputs_dict



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






           """simple docstring"""
           _lowerCamelCase			    =				self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_model(*__lowercase						)



     def 		UpperCamelCase_						(					self						) ->				Any:






           """simple docstring"""
           _lowerCamelCase			    =				self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_image_classification(*__lowercase						)



     def 		UpperCamelCase_						(					self						) ->				Tuple:






           """simple docstring"""
           if not self.model_tester.is_training:
                 return

           _lowerCamelCase    ,					_lowerCamelCase			    =				self.model_tester.prepare_config_and_inputs_for_common()
           _lowerCamelCase			    =				True

           for model_class in self.all_model_classes:
                 # LevitForImageClassificationWithTeacher supports inference-only
                 if (
                     model_class in get_values(__lowercase						)
                     or model_class.__name__ == "LevitForImageClassificationWithTeacher"
                 ):
                       continue
                 _lowerCamelCase			    =				model_class(__lowercase						)
                 model.to(__lowercase						)
                 model.train()
                 _lowerCamelCase			    =				self._prepare_for_class(__lowercase				,				__lowercase				,				return_labels=__lowercase						)
                 _lowerCamelCase			    =				model(**__lowercase						).loss
                 loss.backward()



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






           """simple docstring"""
           _lowerCamelCase    ,					_lowerCamelCase			    =				self.model_tester.prepare_config_and_inputs_for_common()
           if not self.model_tester.is_training:
                 return

           _lowerCamelCase			    =				False
           _lowerCamelCase			    =				True

           for model_class in self.all_model_classes:
                 if model_class in get_values(__lowercase						) or not model_class.supports_gradient_checkpointing:
                       continue
                 # LevitForImageClassificationWithTeacher supports inference-only
                 if model_class.__name__ == "LevitForImageClassificationWithTeacher":
                       continue
                 _lowerCamelCase			    =				model_class(__lowercase						)
                 model.gradient_checkpointing_enable()
                 model.to(__lowercase						)
                 model.train()
                 _lowerCamelCase			    =				self._prepare_for_class(__lowercase				,				__lowercase				,				return_labels=__lowercase						)
                 _lowerCamelCase			    =				model(**__lowercase						).loss
                 loss.backward()



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






           """simple docstring"""
           _lowerCamelCase    ,					_lowerCamelCase			    =				self.model_tester.prepare_config_and_inputs_for_common()

           _lowerCamelCase			    =				[
               {'''title''': '''multi_label_classification''', '''num_labels''': 2, '''dtype''': torch.float},
               {'''title''': '''single_label_classification''', '''num_labels''': 1, '''dtype''': torch.long},
               {'''title''': '''regression''', '''num_labels''': 1, '''dtype''': torch.float},
           ]

           for model_class in self.all_model_classes:
                 if (
                     model_class
                     not in [
                         *get_values(__lowercase						),
                     ]
                     or model_class.__name__ == "LevitForImageClassificationWithTeacher"
                 ):
                       continue

                 for problem_type in problem_types:
                       with self.subTest(msg=F'Testing {model_class} with {problem_type["title"]}'						):
                             _lowerCamelCase			    =				problem_type['''title''']
                             _lowerCamelCase			    =				problem_type['''num_labels''']

                             _lowerCamelCase			    =				model_class(__lowercase						)
                             model.to(__lowercase						)
                             model.train()

                             _lowerCamelCase			    =				self._prepare_for_class(__lowercase				,				__lowercase				,				return_labels=__lowercase						)

                             if problem_type["num_labels"] > 1:
                                   _lowerCamelCase			    =				inputs['''labels'''].unsqueeze(1						).repeat(1				,				problem_type['''num_labels''']						)

                             _lowerCamelCase			    =				inputs['''labels'''].to(problem_type['''dtype''']						)

                             # This tests that we do not trigger the warning form PyTorch "Using a target size that is different
                             # to the input size. This will likely lead to incorrect results due to broadcasting. Please ensure
                             # they have the same size." which is a symptom something in wrong for the regression problem.
                             # See https://github.com/huggingface/transformers/issues/11780
                             with warnings.catch_warnings(record=__lowercase						) as warning_list:
                                   _lowerCamelCase			    =				model(**__lowercase						).loss
                             for w in warning_list:
                                   if "Using a target size that is different to the input size" in str(w.message						):
                                         raise ValueError(
                                             F'Something is going wrong in the regression problem: intercepted {w.message}'						)

                             loss.backward()





     @slow
     def 		UpperCamelCase_						(					self						) ->				List[Any]:






           """simple docstring"""
           for model_name in LEVIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                 _lowerCamelCase			    =				LevitModel.from_pretrained(__lowercase						)
                 self.assertIsNotNone(__lowercase						)





def       __magic_name__(  )						->    str:



      '''simple docstring'''

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




@require_torch
@require_vision
class  UpperCamelCase				( unittest.TestCase ):

     '''simple docstring'''

     @cached_property
     def 		UpperCamelCase_						(					self						) ->				Any:






           """simple docstring"""
           return LevitImageProcessor.from_pretrained(LEVIT_PRETRAINED_MODEL_ARCHIVE_LIST[0]						)





     @slow
     def 		UpperCamelCase_						(					self						) ->				Union[str, Any]:






           """simple docstring"""
           _lowerCamelCase			    =				LevitForImageClassificationWithTeacher.from_pretrained(LEVIT_PRETRAINED_MODEL_ARCHIVE_LIST[0]						).to(
               __lowercase						)

           _lowerCamelCase			    =				self.default_image_processor
           _lowerCamelCase			    =				prepare_img()
           _lowerCamelCase			    =				image_processor(images=__lowercase				,				return_tensors='''pt'''						).to(__lowercase						)

           # forward pass
           with torch.no_grad():
                 _lowerCamelCase			    =				model(**__lowercase						)

           # verify the logits
           _lowerCamelCase			    =				torch.Size((1, 10_00)						)
           self.assertEqual(outputs.logits.shape				,				__lowercase						)

           _lowerCamelCase			    =				torch.tensor([1.0448, -0.3745, -1.8317]						).to(__lowercase						)

           self.assertTrue(torch.allclose(outputs.logits[0, :3]				,				__lowercase				,				atol=1E-4						)						)
import logging

import numpy as np
import pytest
from scipy.linalg import eigh

logging.basicConfig(level=logging.INFO, format='%(message)s')
def       __magic_name__(  __UpperCAmelCase		)						->    np.ndarray:



      '''simple docstring'''

      return input_array.reshape((input_array.size, 1)		)
def       __magic_name__(  __UpperCAmelCase ,						__UpperCAmelCase ,						__UpperCAmelCase		)						->    np.ndarray:



      '''simple docstring'''

      _lowerCamelCase			    =				np.nan
      for i in range(__UpperCAmelCase		):
            _lowerCamelCase			    =				features[:, labels == i]
            _lowerCamelCase			    =				data.mean(1		)
            # Centralize the data of class i
            _lowerCamelCase			    =				data - column_reshape(__UpperCAmelCase		)
            if i > 0:
                  # If covariance_sum is not None
                  covariance_sum += np.dot(__UpperCAmelCase ,						centered_data.T		)
            else:
                  # If covariance_sum is np.nan (i.e. first loop)
                  _lowerCamelCase			    =				np.dot(__UpperCAmelCase ,						centered_data.T		)

      return covariance_sum / features.shape[1]
def       __magic_name__(  __UpperCAmelCase ,						__UpperCAmelCase ,						__UpperCAmelCase		)						->    np.ndarray:



      '''simple docstring'''

      _lowerCamelCase			    =				features.mean(1		)
      _lowerCamelCase			    =				np.nan
      for i in range(__UpperCAmelCase		):
            _lowerCamelCase			    =				features[:, labels == i]
            _lowerCamelCase			    =				data.shape[1]
            _lowerCamelCase			    =				data.mean(1		)
            if i > 0:
                  # If covariance_sum is not None
                  covariance_sum += device_data * np.dot(
                      column_reshape(__UpperCAmelCase		) - column_reshape(__UpperCAmelCase		) ,						(column_reshape(__UpperCAmelCase		) - column_reshape(__UpperCAmelCase		)).T ,						)
            else:
                  # If covariance_sum is np.nan (i.e. first loop)
                  _lowerCamelCase			    =				device_data * np.dot(
                      column_reshape(__UpperCAmelCase		) - column_reshape(__UpperCAmelCase		) ,						(column_reshape(__UpperCAmelCase		) - column_reshape(__UpperCAmelCase		)).T ,						)

      return covariance_sum / features.shape[1]
def       __magic_name__(  __UpperCAmelCase ,						__UpperCAmelCase		)						->    np.ndarray:



      '''simple docstring'''

      if features.any():
            _lowerCamelCase			    =				features.mean(1		)
            # Center the dataset
            _lowerCamelCase			    =				features - np.reshape(__UpperCAmelCase ,						(data_mean.size, 1)		)
            _lowerCamelCase			    =				np.dot(__UpperCAmelCase ,						centered_data.T		) / features.shape[1]
            _lowerCamelCase    ,					_lowerCamelCase			    =				np.linalg.eigh(__UpperCAmelCase		)
            # Take all the columns in the reverse order (-1), and then takes only the first
            _lowerCamelCase			    =				eigenvectors[:, ::-1][:, 0:dimensions]
            # Project the database on the new space
            _lowerCamelCase			    =				np.dot(filtered_eigenvectors.T ,						__UpperCAmelCase		)
            logging.info('''Principal Component Analysis computed'''		)

            return projected_data
      else:
            logging.basicConfig(level=logging.ERROR ,						format='''%(message)s''' ,						force=__UpperCAmelCase		)
            logging.error('''Dataset empty'''		)
            raise AssertionError
def       __magic_name__(  __UpperCAmelCase ,						__UpperCAmelCase ,						__UpperCAmelCase ,						__UpperCAmelCase		)						->    np.ndarray:



      '''simple docstring'''

      assert classes > dimensions

      # Check if features have been already loaded
      if features.any:
            _lowerCamelCase    ,					_lowerCamelCase			    =				eigh(
                covariance_between_classes(__UpperCAmelCase ,						__UpperCAmelCase ,						__UpperCAmelCase		) ,						covariance_within_classes(__UpperCAmelCase ,						__UpperCAmelCase ,						__UpperCAmelCase		) ,						)
            _lowerCamelCase			    =				eigenvectors[:, ::-1][:, :dimensions]
            _lowerCamelCase    ,					_lowerCamelCase    ,					_lowerCamelCase			    =				np.linalg.svd(__UpperCAmelCase		)
            _lowerCamelCase			    =				svd_matrix[:, 0:dimensions]
            _lowerCamelCase			    =				np.dot(filtered_svd_matrix.T ,						__UpperCAmelCase		)
            logging.info('''Linear Discriminant Analysis computed'''		)

            return projected_data
      else:
            logging.basicConfig(level=logging.ERROR ,						format='''%(message)s''' ,						force=__UpperCAmelCase		)
            logging.error('''Dataset empty'''		)
            raise AssertionError
def       __magic_name__(  )						->    None:



      '''simple docstring'''

      _lowerCamelCase			    =				np.array([[1, 2, 3, 4, 5], [2, 3, 4, 5, 6], [3, 4, 5, 6, 7]]		)
      _lowerCamelCase			    =				np.array([0, 0, 0, 1, 1]		)
      _lowerCamelCase			    =				2
      _lowerCamelCase			    =				2

      # Assert that the function raises an AssertionError if dimensions > classes
      with pytest.raises(__UpperCAmelCase		) as error_info:
            _lowerCamelCase			    =				linear_discriminant_analysis(
                __UpperCAmelCase ,						__UpperCAmelCase ,						__UpperCAmelCase ,						__UpperCAmelCase		)
            if isinstance(__UpperCAmelCase ,						np.ndarray		):
                  raise AssertionError(
                      '''Did not raise AssertionError for dimensions > classes'''		)
            assert error_info.type is AssertionError
def       __magic_name__(  )						->    None:



      '''simple docstring'''

      _lowerCamelCase			    =				np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]		)
      _lowerCamelCase			    =				2
      _lowerCamelCase			    =				np.array([[6.9_2_8_2_0_3_2_3, 8.6_6_0_2_5_4_0_4, 1_0.3_9_2_3_0_4_8_5], [3.0, 3.0, 3.0]]		)

      with pytest.raises(__UpperCAmelCase		) as error_info:
            _lowerCamelCase			    =				principal_component_analysis(__UpperCAmelCase ,						__UpperCAmelCase		)
            if not np.allclose(__UpperCAmelCase ,						__UpperCAmelCase		):
                  raise AssertionError
            assert error_info.type is AssertionError


if __name__ == "__main__":
     import doctest

     doctest.testmod()