



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







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

														'''simple docstring'''

														_lowerCAmelCase  :							Any				     =					parent
														_lowerCAmelCase  :							str				     =					batch_size
														_lowerCAmelCase  :							int				     =					image_size
														_lowerCAmelCase  :							Tuple				     =					patch_size
														_lowerCAmelCase  :							Optional[Any]				     =					num_channels
														_lowerCAmelCase  :							List[Any]				     =					is_training
														_lowerCAmelCase  :							List[str]				     =					use_labels
														_lowerCAmelCase  :							List[Any]				     =					hidden_size
														_lowerCAmelCase  :							Tuple				     =					num_hidden_layers
														_lowerCAmelCase  :							Optional[Any]				     =					num_attention_heads
														_lowerCAmelCase  :							Tuple				     =					intermediate_size
														_lowerCAmelCase  :							int				     =					hidden_act
														_lowerCAmelCase  :							List[str]				     =					hidden_dropout_prob
														_lowerCAmelCase  :							Any				     =					attention_probs_dropout_prob
														_lowerCAmelCase  :							Optional[int]				     =					type_sequence_label_size
														_lowerCAmelCase  :							List[Any]				     =					initializer_range
														_lowerCAmelCase  :							Optional[int]				     =					scope
														_lowerCAmelCase  :							Union[str, Any]				     =					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  :							List[str]				     =					(self.image_size // 32) ** 2
														_lowerCAmelCase  :							Union[str, Any]				     =					num_patches + 1







							def      snake_case__   (     self):

														'''simple docstring'''

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

														_lowerCAmelCase  :							List[Any]				     =					None
														if self.use_labels:
																					_lowerCAmelCase  :							List[Any]				     =					ids_tensor([self.batch_size],    self.type_sequence_label_size)

														_lowerCAmelCase  :							Optional[int]				     =					self.get_config()

														return config, pixel_values, labels







							def      snake_case__   (     self):

														'''simple docstring'''

														_lowerCAmelCase  :							Any				     =					{
														    "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=__a,    initializer_range=self.initializer_range,    backbone_featmap_shape=self.backbone_featmap_shape,    backbone_config=__a,    )







							def      snake_case__   (     self,    __a,    __a,    __a):

														'''simple docstring'''

														_lowerCAmelCase  :							Any				     =					ViTHybridModel(config=__a)
														model.to(__a)
														model.eval()
														_lowerCAmelCase  :							Optional[Any]				     =					model(__a)
														self.parent.assertEqual(result.last_hidden_state.shape,    (self.batch_size, self.seq_length, self.hidden_size))







							def      snake_case__   (     self,    __a,    __a,    __a):

														'''simple docstring'''

														_lowerCAmelCase  :							Optional[int]				     =					self.type_sequence_label_size
														_lowerCAmelCase  :							Tuple				     =					ViTHybridForImageClassification(__a)
														model.to(__a)
														model.eval()
														_lowerCAmelCase  :							List[str]				     =					model(__a,    labels=__a)
														self.parent.assertEqual(result.logits.shape,    (self.batch_size, self.type_sequence_label_size))







							def      snake_case__   (     self):

														'''simple docstring'''

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



@require_torch
class 						UpperCAmelCase_					( a     ,      a     ,      unittest.TestCase):
							lowerCamelCase__         =	(ViTHybridModel, ViTHybridForImageClassification) if is_torch_available() else ()
							lowerCamelCase__         =	(
							    {'feature-extraction': ViTHybridModel, 'image-classification': ViTHybridForImageClassification}
							    if is_torch_available()
							    else {}
							)
							lowerCamelCase__         =	False
							lowerCamelCase__         =	False
							lowerCamelCase__         =	False







							def      snake_case__   (     self):

														'''simple docstring'''

														_lowerCAmelCase  :							Optional[Any]				     =					ViTHybridModelTester(self)
														_lowerCAmelCase  :							int				     =					ConfigTester(self,    config_class=__a,    has_text_modality=__a,    hidden_size=37)







							def      snake_case__   (     self):

														'''simple docstring'''

														self.config_tester.run_common_tests()







							@unittest.skip(reason="ViT does not use inputs_embeds")
							def      snake_case__   (     self):

														'''simple docstring'''

														pass







							def      snake_case__   (     self):

														'''simple docstring'''

														_lowerCAmelCase     ,					_lowerCAmelCase  :							int				     =					self.model_tester.prepare_config_and_inputs_for_common()

														for model_class in self.all_model_classes:
																					_lowerCAmelCase  :							List[Any]				     =					model_class(__a)
																					self.assertIsInstance(model.get_input_embeddings(),    (nn.Module))
																					_lowerCAmelCase  :							Tuple				     =					model.get_output_embeddings()
																					self.assertTrue(x is None or isinstance(__a,    nn.Linear))







							def      snake_case__   (     self):

														'''simple docstring'''

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

														for model_class in self.all_model_classes:
																					_lowerCAmelCase  :							Tuple				     =					model_class(__a)
																					_lowerCAmelCase  :							Dict				     =					inspect.signature(model.forward)
																					# signature.parameters is an OrderedDict => so arg_names order is deterministic
																					_lowerCAmelCase  :							Optional[Any]				     =					[*signature.parameters.keys()]

																					_lowerCAmelCase  :							List[Any]				     =					["pixel_values"]
																					self.assertListEqual(arg_names[:1],    __a)







							def      snake_case__   (     self):

														'''simple docstring'''

														_lowerCAmelCase  :							Optional[Any]				     =					self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_model(*__a)







							def      snake_case__   (     self):

														'''simple docstring'''

														_lowerCAmelCase  :							Dict				     =					self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_for_image_classification(*__a)







							def      snake_case__   (     self):

														'''simple docstring'''

														_lowerCAmelCase     ,					_lowerCAmelCase  :							Union[str, Any]				     =					self.model_tester.prepare_config_and_inputs_for_common()

														_lowerCAmelCase  :							Union[str, Any]				     =					_config_zero_init(__a)
														for model_class in self.all_model_classes:
																					_lowerCAmelCase  :							List[Any]				     =					model_class(config=__a)
																					# Skip the check for the backbone
																					for name, module in model.named_modules():
																												if module.__class__.__name__ == "ViTHybridPatchEmbeddings":
																																			_lowerCAmelCase  :							List[Any]				     =					[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      snake_case__   (     self):

														'''simple docstring'''

														for model_name in VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																					_lowerCAmelCase  :							Tuple				     =					ViTHybridModel.from_pretrained(__a)
																					self.assertIsNotNone(__a)






def        A       (      ):





							'''simple docstring'''




							_lowerCAmelCase  :							Optional[int]				     =					Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"						)
							return image



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







							@cached_property
							def      snake_case__   (     self):

														'''simple docstring'''

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







							@slow
							def      snake_case__   (     self):

														'''simple docstring'''

														_lowerCAmelCase  :							List[str]				     =					ViTHybridForImageClassification.from_pretrained(VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[0]).to(
														    __a)

														_lowerCAmelCase  :							str				     =					self.default_image_processor
														_lowerCAmelCase  :							str				     =					prepare_img()
														_lowerCAmelCase  :							Dict				     =					image_processor(images=__a,    return_tensors="pt").to(__a)

														# forward pass
														with torch.no_grad():
																					_lowerCAmelCase  :							Union[str, Any]				     =					model(**__a)

														# verify the logits
														_lowerCAmelCase  :							int				     =					torch.Size((1, 1000))
														self.assertEqual(outputs.logits.shape,    __a)

														_lowerCAmelCase  :							Any				     =					torch.tensor([-1.9_090, -0.4_993, -0.2_389]).to(__a)

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







							@slow
							@require_accelerate
							def      snake_case__   (     self):

														'''simple docstring'''

														_lowerCAmelCase  :							str				     =					ViTHybridImageProcessor.from_pretrained("google/vit-hybrid-base-bit-384")
														_lowerCAmelCase  :							Optional[Any]				     =					ViTHybridForImageClassification.from_pretrained("google/vit-hybrid-base-bit-384",    device_map="auto")

														_lowerCAmelCase  :							List[str]				     =					prepare_img()

														_lowerCAmelCase  :							int				     =					image_processor(images=__a,    return_tensors="pt")
														_lowerCAmelCase  :							str				     =					model(**__a)
														_lowerCAmelCase  :							Dict				     =					outputs.logits
														# model predicts one of the 1000 ImageNet classes
														_lowerCAmelCase  :							str				     =					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        A       (      _lowerCamelCase						):





							'''simple docstring'''




							return input_array.reshape((input_array.size, 1)						)






def        A       (      _lowerCamelCase       ,				_lowerCamelCase       ,				_lowerCamelCase						):





							'''simple docstring'''




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

							return covariance_sum / features.shape[1]






def        A       (      _lowerCamelCase       ,				_lowerCamelCase       ,				_lowerCamelCase						):





							'''simple docstring'''




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

							return covariance_sum / features.shape[1]






def        A       (      _lowerCamelCase       ,				_lowerCamelCase						):





							'''simple docstring'''




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

														return projected_data
							else:
														logging.basicConfig(level=logging.ERROR       ,				format="%(message)s"       ,				force=_lowerCamelCase						)
														logging.error("Dataset empty"						)
														raise AssertionError






def        A       (      _lowerCamelCase       ,				_lowerCamelCase       ,				_lowerCamelCase       ,				_lowerCamelCase						):





							'''simple docstring'''




							assert classes > dimensions

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

														return projected_data
							else:
														logging.basicConfig(level=logging.ERROR       ,				format="%(message)s"       ,				force=_lowerCamelCase						)
														logging.error("Dataset empty"						)
														raise AssertionError






def        A       (      ):





							'''simple docstring'''




							_lowerCAmelCase  :							Optional[int]				     =					np.array([[1, 2, 3, 4, 5], [2, 3, 4, 5, 6], [3, 4, 5, 6, 7]]						)
							_lowerCAmelCase  :							List[Any]				     =					np.array([0, 0, 0, 1, 1]						)
							_lowerCAmelCase  :							List[Any]				     =					2
							_lowerCAmelCase  :							Union[str, Any]				     =					2

							# Assert that the function raises an AssertionError if dimensions > classes
							with pytest.raises(_lowerCamelCase						) as error_info:
														_lowerCAmelCase  :							Union[str, Any]				     =					linear_discriminant_analysis(
														    _lowerCamelCase       ,				_lowerCamelCase       ,				_lowerCamelCase       ,				_lowerCamelCase						)
														if isinstance(_lowerCamelCase       ,				np.ndarray						):
																					raise AssertionError(
																					    "Did not raise AssertionError for dimensions > classes"						)
														assert error_info.type is AssertionError






def        A       (      ):





							'''simple docstring'''




							_lowerCAmelCase  :							Optional[Any]				     =					np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]						)
							_lowerCAmelCase  :							List[str]				     =					2
							_lowerCAmelCase  :							List[Any]				     =					np.array([[6.92_82_03_23, 8.66_02_54_04, 10.39_23_04_85], [3.0, 3.0, 3.0]]						)

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


if __name__ == "__main__":
						import doctest

						doctest.testmod()
