
'''simple docstring'''



from sklearn.metrics import mean_squared_error

import datasets


_lowercase          =						"""\
@article{scikit-learn,
 title={Scikit-learn: Machine Learning in {P}ython},
 author={Pedregosa, F. and Varoquaux, G. and Gramfort, A. and Michel, V.
         and Thirion, B. and Grisel, O. and Blondel, M. and Prettenhofer, P.
         and Weiss, R. and Dubourg, V. and Vanderplas, J. and Passos, A. and
         Cournapeau, D. and Brucher, M. and Perrot, M. and Duchesnay, E.},
 journal={Journal of Machine Learning Research},
 volume={12},
 pages={2825--2830},
 year={2011}
}
"""

_lowercase          =						"""\
Mean Squared Error(MSE) is the average of the square of difference between the predicted
and actual values.
"""


_lowercase          =						"""
Args:
    predictions: array-like of shape (n_samples,) or (n_samples, n_outputs)
        Estimated target values.
    references: array-like of shape (n_samples,) or (n_samples, n_outputs)
        Ground truth (correct) target values.
    sample_weight: array-like of shape (n_samples,), default=None
        Sample weights.
    multioutput: {\"raw_values\", \"uniform_average\"} or array-like of shape (n_outputs,), default=\"uniform_average\"
        Defines aggregating of multiple output values. Array-like value defines weights used to average errors.

        \"raw_values\" : Returns a full set of errors in case of multioutput input.

        \"uniform_average\" : Errors of all outputs are averaged with uniform weight.

    squared : bool, default=True
        If True returns MSE value, if False returns RMSE (Root Mean Squared Error) value.

Returns:
    mse : mean squared error.
Examples:

    >>> mse_metric = datasets.load_metric(\"mse\")
    >>> predictions = [2.5, 0.0, 2, 8]
    >>> references = [3, -0.5, 2, 7]
    >>> results = mse_metric.compute(predictions=predictions, references=references)
    >>> print(results)
    {'mse': 0.375}
    >>> rmse_result = mse_metric.compute(predictions=predictions, references=references, squared=False)
    >>> print(rmse_result)
    {'mse': 0.6123724356957945}

    If you're using multi-dimensional lists, then set the config as follows :

    >>> mse_metric = datasets.load_metric(\"mse\", \"multilist\")
    >>> predictions = [[0.5, 1], [-1, 1], [7, -6]]
    >>> references = [[0, 2], [-1, 2], [8, -5]]
    >>> results = mse_metric.compute(predictions=predictions, references=references)
    >>> print(results)
    {'mse': 0.7083333333333334}
    >>> results = mse_metric.compute(predictions=predictions, references=references, multioutput='raw_values')
    >>> print(results) # doctest: +NORMALIZE_WHITESPACE
    {'mse': array([0.41666667, 1. ])}
"""




@datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION			,				_KWARGS_DESCRIPTION					)
class     UpperCAmelCase_	(						datasets.Metric					):






						'''simple docstring'''






						def    _lowercase      (   self ):

												"""simple docstring"""



												return datasets.MetricInfo(
												    description=_DESCRIPTION       ,  citation=_CITATION       ,  inputs_description=_KWARGS_DESCRIPTION       ,  features=datasets.Features(self._get_feature_types() )       ,  reference_urls=[
												        """https://scikit-learn.org/stable/modules/generated/sklearn.metrics.mean_squared_error.html"""
												    ]       ,  )




						def    _lowercase      (   self ):

												"""simple docstring"""



												if self.config_name == "multilist":
																		return {
																		    "predictions": datasets.Sequence(datasets.Value("""float""" ) ),
																		    "references": datasets.Sequence(datasets.Value("""float""" ) ),
																		}
												else:
																		return {
																		    "predictions": datasets.Value("""float""" ),
																		    "references": datasets.Value("""float""" ),
																		}




						def    _lowercase      (   self       ,  _lowercase       ,  _lowercase       ,  _lowercase=None       ,  _lowercase="uniform_average"       ,  _lowercase=True ):

												"""simple docstring"""



												_lowerCAmelCase    		=					mean_squared_error(
												    _lowercase       ,  _lowercase       ,  sample_weight=_lowercase       ,  multioutput=_lowercase       ,  squared=_lowercase )

												return {"mse": mse}




'''simple docstring'''
import copy
import inspect
import unittest

from transformers import PretrainedConfig, SwiftFormerConfig
from transformers.testing_utils import (
    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, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
					import torch
					from torch import nn

					from transformers import SwiftFormerForImageClassification, SwiftFormerModel
					from transformers.models.swiftformer.modeling_swiftformer import SWIFTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
					from PIL import Image

					from transformers import ViTImageProcessor




class        _UpperCamelCase	:


				'''simple docstring'''


				def __init__(     self							,      a_							,      a_=1_3							,      a_=3							,      a_=True							,      a_=True							,      a_=0.1							,      a_=0.1							,      a_=2_2_4							,      a_=1_0_0_0							,      a_=[3, 3, 6, 4]							,      a_=[4_8, 5_6, 1_1_2, 2_2_0]							,      )    ->  Union[str, Any]:
					lowercase						:		Optional[Any]   					=		parent
					lowercase						:		List[Any]   					=		batch_size
					lowercase						:		Union[str, Any]   					=		num_channels
					lowercase						:		str   					=		is_training
					lowercase						:		Any   					=		use_labels
					lowercase						:		Optional[Any]   					=		hidden_dropout_prob
					lowercase						:		List[str]   					=		attention_probs_dropout_prob
					lowercase						:		Optional[Any]   					=		num_labels
					lowercase						:		str   					=		image_size
					lowercase						:		Dict   					=		layer_depths
					lowercase						:		List[str]   					=		embed_dims

				def 	a__		(     self							)    ->  Optional[Any]:
					lowercase						:		int   					=		floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]							)

					lowercase						:		int   					=		None
					if self.use_labels:
						lowercase						:		Optional[Any]   					=		ids_tensor([self.batch_size]							,      self.num_labels							)

					lowercase						:		List[Any]   					=		self.get_config()

					return config, pixel_values, labels

				def 	a__		(     self							)    ->  List[Any]:
					return SwiftFormerConfig(
					    depths=self.layer_depths							,      embed_dims=self.embed_dims							,      mlp_ratio=4							,      downsamples=[True, True, True, True]							,      hidden_act="gelu"							,      num_labels=self.num_labels							,      down_patch_size=3							,      down_stride=2							,      down_pad=1							,      drop_rate=0.0							,      drop_path_rate=0.0							,      use_layer_scale=a_							,      layer_scale_init_value=1e-5							,      )

				def 	a__		(     self							,      a_							,      a_							,      a_							)    ->  Optional[Any]:
					lowercase						:		Tuple   					=		SwiftFormerModel(config=a_							)
					model.to(a_							)
					model.eval()
					lowercase						:		Union[str, Any]   					=		model(a_							)
					self.parent.assertEqual(result.last_hidden_state.shape							,      (self.batch_size, self.embed_dims[-1], 7, 7)							)

				def 	a__		(     self							,      a_							,      a_							,      a_							)    ->  Any:
					lowercase						:		Dict   					=		self.num_labels
					lowercase						:		Optional[int]   					=		SwiftFormerForImageClassification(a_							)
					model.to(a_							)
					model.eval()
					lowercase						:		Tuple   					=		model(a_							,      labels=a_							)
					self.parent.assertEqual(result.logits.shape							,      (self.batch_size, self.num_labels)							)

					lowercase						:		Tuple   					=		SwiftFormerForImageClassification(a_							)
					model.to(a_							)
					model.eval()

					lowercase						:		List[str]   					=		floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]							)
					lowercase						:		str   					=		model(a_							)
					self.parent.assertEqual(result.logits.shape							,      (self.batch_size, self.num_labels)							)


				def 	a__		(     self							)    ->  Dict:
					((lowercase)					,		(lowercase)					,		(lowercase))						:		Dict   					=		self.prepare_config_and_inputs()
					lowercase						:		Dict   					=		{"pixel_values": pixel_values}
					return config, inputs_dict




@require_torch
class        _UpperCamelCase	(    SCREAMING_SNAKE_CASE						,			SCREAMING_SNAKE_CASE						,			unittest.TestCase):


				'''simple docstring'''


				_snake_case						       =				(SwiftFormerModel, SwiftFormerForImageClassification) if is_torch_available() else ()
				_snake_case						       =				(
				    {'''feature-extraction''': SwiftFormerModel, '''image-classification''': SwiftFormerForImageClassification}
				    if is_torch_available()
				    else {}
				)

				_snake_case						       =				False
				_snake_case						       =				False
				_snake_case						       =				False
				_snake_case						       =				False
				_snake_case						       =				False

				def 	a__		(     self							)    ->  str:
					lowercase						:		Optional[int]   					=		SwiftFormerModelTester(self							)
					lowercase						:		int   					=		ConfigTester(
					    self							,      config_class=a_							,      has_text_modality=a_							,      hidden_size=3_7							,      num_attention_heads=1_2							,      num_hidden_layers=1_2							,      )

				def 	a__		(     self							)    ->  Any:
					self.config_tester.run_common_tests()

				@unittest.skip(reason="SwiftFormer does not use inputs_embeds"							)
				def 	a__		(     self							)    ->  int:
					pass

				def 	a__		(     self							)    ->  Optional[int]:
					lowercase					,		lowercase						:		Optional[int]   					=		self.model_tester.prepare_config_and_inputs_for_common()

					for model_class in self.all_model_classes:
						lowercase						:		Optional[int]   					=		model_class(a_							)
						lowercase						:		Tuple   					=		model.get_output_embeddings()
						self.assertTrue(x is None or isinstance(a_							,      nn.Linear							)							)

				def 	a__		(     self							)    ->  List[Any]:
					lowercase					,		lowercase						:		Optional[Any]   					=		self.model_tester.prepare_config_and_inputs_for_common()

					for model_class in self.all_model_classes:
						lowercase						:		str   					=		model_class(a_							)
						lowercase						:		int   					=		inspect.signature(model.forward							)
						# signature.parameters is an OrderedDict => so arg_names order is deterministic
						lowercase						:		Union[str, Any]   					=		[*signature.parameters.keys()]

						lowercase						:		int   					=		["pixel_values"]
						self.assertListEqual(arg_names[:1]							,      a_							)

				def 	a__		(     self							)    ->  int:
					lowercase						:		Optional[Any]   					=		self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_model(*a_							)

				def 	a__		(     self							)    ->  Optional[int]:
					lowercase						:		List[Any]   					=		self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_image_classification(*a_							)

				@slow
				def 	a__		(     self							)    ->  Dict:
					for model_name in SWIFTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
						lowercase						:		Optional[int]   					=		SwiftFormerModel.from_pretrained(a_							)
						self.assertIsNotNone(a_							)

				@unittest.skip(reason="SwiftFormer does not output attentions"							)
				def 	a__		(     self							)    ->  Optional[Any]:
					pass

				def 	a__		(     self							)    ->  str:
					def check_hidden_states_output(a_							,      a_							,      a_							):
						lowercase						:		Union[str, Any]   					=		model_class(a_							)
						model.to(a_							)
						model.eval()

						with torch.no_grad():
							lowercase						:		List[str]   					=		model(**self._prepare_for_class(a_							,      a_							)							)

						lowercase						:		str   					=		outputs.hidden_states

						lowercase						:		str   					=		8
						self.assertEqual(len(a_							)							,      a_							)  # TODO

						# SwiftFormer's feature maps are of shape (batch_size, embed_dims, height, width)
						# with the width and height being successively divided by 2, after every 2 blocks
						for i in range(len(a_							)							):
							self.assertEqual(
							    hidden_states[i].shape							,      torch.Size(
							        [
							            self.model_tester.batch_size,
							            self.model_tester.embed_dims[i // 2],
							            (self.model_tester.image_size // 4) // 2 ** (i // 2),
							            (self.model_tester.image_size // 4) // 2 ** (i // 2),
							        ]							)							,      )

					lowercase					,		lowercase						:		Optional[Any]   					=		self.model_tester.prepare_config_and_inputs_for_common()

					for model_class in self.all_model_classes:
						lowercase						:		Dict   					=		True
						check_hidden_states_output(a_							,      a_							,      a_							)

						# check that output_hidden_states also work using config
						del inputs_dict["output_hidden_states"]
						lowercase						:		str   					=		True

						check_hidden_states_output(a_							,      a_							,      a_							)

				def 	a__		(     self							)    ->  Optional[Any]:
					def _config_zero_init(a_							):
						lowercase						:		str   					=		copy.deepcopy(a_							)
						for key in configs_no_init.__dict__.keys():
							if "_range" in key or "_std" in key or "initializer_factor" in key or "layer_scale" in key:
								setattr(a_							,      a_							,      1e-1_0							)
							if isinstance(getattr(a_							,      a_							,      a_							)							,      a_							):
								lowercase						:		List[Any]   					=		_config_zero_init(getattr(a_							,      a_							)							)
								setattr(a_							,      a_							,      a_							)
						return configs_no_init

					lowercase					,		lowercase						:		Any   					=		self.model_tester.prepare_config_and_inputs_for_common()

					lowercase						:		List[str]   					=		_config_zero_init(a_							)
					for model_class in self.all_model_classes:
						lowercase						:		Tuple   					=		model_class(config=a_							)
						for name, param in model.named_parameters():
							if param.requires_grad:
								self.assertIn(
								    ((param.data.mean() * 1e9) / 1e9).round().item()							,      [0.0, 1.0]							,      msg=F'''Parameter {name} of model {model_class} seems not properly initialized'''							,      )


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




def 	_A  (       )  ->				List[str]:
	lowercase						:		Optional[int]   					=		Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"							)
	return image






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


				'''simple docstring'''


				@cached_property
				def 	a__		(     self							)    ->  List[Any]:
					return ViTImageProcessor.from_pretrained("MBZUAI/swiftformer-xs"							) if is_vision_available() else None


				@slow
				def 	a__		(     self							)    ->  List[str]:
					lowercase						:		Tuple   					=		SwiftFormerForImageClassification.from_pretrained("MBZUAI/swiftformer-xs"							).to(a_							)

					lowercase						:		Any   					=		self.default_image_processor
					lowercase						:		Optional[int]   					=		prepare_img()
					lowercase						:		Any   					=		image_processor(images=a_							,      return_tensors="pt"							).to(a_							)

					# forward pass
					with torch.no_grad():
						lowercase						:		List[str]   					=		model(**a_							)

					# verify the logits
					lowercase						:		str   					=		torch.Size((1, 1_0_0_0)							)
					self.assertEqual(outputs.logits.shape							,      a_							)

					lowercase						:		List[Any]   					=		torch.tensor([[-2.1_7_0_3e0_0, 2.1_1_0_7e0_0, -2.0_8_1_1e0_0]]							).to(a_							)
					self.assertTrue(torch.allclose(outputs.logits[0, :3]							,      a_							,      atol=1e-4							)							)


