'''simple docstring'''





import inspect
import unittest

from transformers import ViTHybridConfig
from transformers.testing_utils import require_accelerate, require_torch, require_vision, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_vision_available

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


if is_torch_available():
     import torch
     from torch import nn

     from transformers import ViTHybridForImageClassification, ViTHybridImageProcessor, ViTHybridModel
     from transformers.models.vit_hybrid.modeling_vit_hybrid import VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
     from PIL import Image



class  UpperCamelCase				:

     '''simple docstring'''

     def __init__(					self				,				A_				,				A_=13				,				A_=64				,				A_=2				,				A_=3				,				A_=True				,				A_=True				,				A_=32				,				A_=5				,				A_=4				,				A_=37				,				A_="gelu"				,				A_=0.1				,				A_=0.1				,				A_=10				,				A_=0.02				,				A_=[1, 16, 4, 4]				,				A_=None				,				) ->				Any:






           """simple docstring"""
           _lowerCamelCase			    =				parent
           _lowerCamelCase			    =				batch_size
           _lowerCamelCase			    =				image_size
           _lowerCamelCase			    =				patch_size
           _lowerCamelCase			    =				num_channels
           _lowerCamelCase			    =				is_training
           _lowerCamelCase			    =				use_labels
           _lowerCamelCase			    =				hidden_size
           _lowerCamelCase			    =				num_hidden_layers
           _lowerCamelCase			    =				num_attention_heads
           _lowerCamelCase			    =				intermediate_size
           _lowerCamelCase			    =				hidden_act
           _lowerCamelCase			    =				hidden_dropout_prob
           _lowerCamelCase			    =				attention_probs_dropout_prob
           _lowerCamelCase			    =				type_sequence_label_size
           _lowerCamelCase			    =				initializer_range
           _lowerCamelCase			    =				scope
           _lowerCamelCase			    =				backbone_featmap_shape

           # in ViT hybrid, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token)
           # the number of patches is based on the feature map of the backbone, which by default uses an output stride
           # of 32, which means that the feature map has a spatial resolution of 1/32 of the input image size
           _lowerCamelCase			    =				(self.image_size // 32) ** 2
           _lowerCamelCase			    =				num_patches + 1



     def 		UpperCamelCase_						(					self						) ->				int:






           """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.type_sequence_label_size						)

           _lowerCamelCase			    =				self.get_config()

           return config, pixel_values, labels



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






           """simple docstring"""
           _lowerCamelCase			    =				{
               '''global_padding''': '''same''',
               '''layer_type''': '''bottleneck''',
               '''depths''': [3, 4, 9],
               '''out_features''': ['''stage1''', '''stage2''', '''stage3'''],
               '''embedding_dynamic_padding''': True,
               '''hidden_sizes''': [4, 8, 16, 32],
               '''num_groups''': 2,
           }

           return ViTHybridConfig(
               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=__UpperCamelCase				,				initializer_range=self.initializer_range				,				backbone_featmap_shape=self.backbone_featmap_shape				,				backbone_config=__UpperCamelCase				,				)



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






           """simple docstring"""
           _lowerCamelCase			    =				ViTHybridModel(config=__UpperCamelCase						)
           model.to(__UpperCamelCase						)
           model.eval()
           _lowerCamelCase			    =				model(__UpperCamelCase						)
           self.parent.assertEqual(result.last_hidden_state.shape				,				(self.batch_size, self.seq_length, self.hidden_size)						)



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






           """simple docstring"""
           _lowerCamelCase			    =				self.type_sequence_label_size
           _lowerCamelCase			    =				ViTHybridForImageClassification(__UpperCamelCase						)
           model.to(__UpperCamelCase						)
           model.eval()
           _lowerCamelCase			    =				model(__UpperCamelCase				,				labels=__UpperCamelCase						)
           self.parent.assertEqual(result.logits.shape				,				(self.batch_size, self.type_sequence_label_size)						)





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






           """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_										=  (ViTHybridModel, ViTHybridForImageClassification) if is_torch_available() else ()
     A_										=  (
         {'feature-extraction': ViTHybridModel, 'image-classification': ViTHybridForImageClassification}
         if is_torch_available()
         else {}
     )
     A_										=  False
     A_										=  False
     A_										=  False



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






           """simple docstring"""
           _lowerCamelCase			    =				ViTHybridModelTester(self						)
           _lowerCamelCase			    =				ConfigTester(self				,				config_class=__UpperCamelCase				,				has_text_modality=__UpperCamelCase				,				hidden_size=37						)



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






           """simple docstring"""
           self.config_tester.run_common_tests()



     @unittest.skip(reason='''ViT does not use inputs_embeds'''						)
     def 		UpperCamelCase_						(					self						) ->				Union[str, Any]:






           """simple docstring"""
           pass



     def 		UpperCamelCase_						(					self						) ->				Optional[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(__UpperCamelCase						)
                 self.assertIsInstance(model.get_input_embeddings()				,				(nn.Module)						)
                 _lowerCamelCase			    =				model.get_output_embeddings()
                 self.assertTrue(x is None or isinstance(__UpperCamelCase				,				nn.Linear						)						)



     def 		UpperCamelCase_						(					self						) ->				List[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(__UpperCamelCase						)
                 _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]				,				__UpperCamelCase						)



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






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



     def 		UpperCamelCase_						(					self						) ->				Tuple:






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



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






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

           _lowerCamelCase			    =				_config_zero_init(__UpperCamelCase						)
           for model_class in self.all_model_classes:
                 _lowerCamelCase			    =				model_class(config=__UpperCamelCase						)
                 # Skip the check for the backbone
                 for name, module in model.named_modules():
                       if module.__class__.__name__ == "ViTHybridPatchEmbeddings":
                             _lowerCamelCase			    =				[F'{name}.{key}' for key in module.state_dict().keys()]
                             break

                 for name, param in model.named_parameters():
                       if param.requires_grad:
                             if name in backbone_params:
                                   continue
                             self.assertIn(
                                 ((param.data.mean() * 1E9).round() / 1E9).item()				,				[0.0, 1.0]				,				msg=F'Parameter {name} of model {model_class} seems not properly initialized'				,				)





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






           """simple docstring"""
           for model_name in VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                 _lowerCamelCase			    =				ViTHybridModel.from_pretrained(__UpperCamelCase						)
                 self.assertIsNotNone(__UpperCamelCase						)





def       __magic_name__(  )						->    Tuple:



      '''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						) ->				int:






           """simple docstring"""
           return (
               ViTHybridImageProcessor.from_pretrained(VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[0]						)
               if is_vision_available()
               else None
           )



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






           """simple docstring"""
           _lowerCamelCase			    =				ViTHybridForImageClassification.from_pretrained(VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[0]						).to(
               __UpperCamelCase						)

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

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

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

           _lowerCamelCase			    =				torch.tensor([-1.9090, -0.4993, -0.2389]						).to(__UpperCamelCase						)

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





     @slow
     @require_accelerate
     def 		UpperCamelCase_						(					self						) ->				str:






           """simple docstring"""
           _lowerCamelCase			    =				ViTHybridImageProcessor.from_pretrained('''google/vit-hybrid-base-bit-384'''						)
           _lowerCamelCase			    =				ViTHybridForImageClassification.from_pretrained('''google/vit-hybrid-base-bit-384'''				,				device_map='''auto'''						)

           _lowerCamelCase			    =				prepare_img()

           _lowerCamelCase			    =				image_processor(images=__UpperCamelCase				,				return_tensors='''pt'''						)
           _lowerCamelCase			    =				model(**__UpperCamelCase						)
           _lowerCamelCase			    =				outputs.logits
           # model predicts one of the 1000 ImageNet classes
           _lowerCamelCase			    =				logits.argmax(-1						).item()

           self.assertTrue(model.config.idalabel[predicted_class_idx]				,				'''tabby, tabby cat'''						)
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()