'''simple docstring'''







import inspect
import unittest

from transformers import BitConfig
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_backbone_common import BackboneTesterMixin
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 BitBackbone, BitForImageClassification, BitImageProcessor, BitModel
		from transformers.models.bit.modeling_bit import BIT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
		from PIL import Image




class _SCREAMING_SNAKE_CASE:




						def __init__(    self :       int							,							UpperCamelCase_ :       Optional[int]							,							UpperCamelCase_ :       int=3							,							UpperCamelCase_ :       List[Any]=32							,							UpperCamelCase_ :       Any=3							,							UpperCamelCase_ :       Union[str, Any]=10							,							UpperCamelCase_ :       Any=[8, 16, 32, 64]							,							UpperCamelCase_ :       Tuple=[1, 1, 2, 1]							,							UpperCamelCase_ :       Optional[Any]=True							,							UpperCamelCase_ :       Union[str, Any]=True							,							UpperCamelCase_ :       Union[str, Any]="relu"							,							UpperCamelCase_ :       List[Any]=3							,							UpperCamelCase_ :       int=None							,							UpperCamelCase_ :       str=["stage2", "stage3", "stage4"]							,							UpperCamelCase_ :       int=[2, 3, 4]							,							UpperCamelCase_ :       Any=1							,							)     ->				Union[str, Any]:
													SCREAMING_SNAKE_CASE__     :Optional[int]						    = parent
													SCREAMING_SNAKE_CASE__     :str						    = batch_size
													SCREAMING_SNAKE_CASE__     :List[Any]						    = image_size
													SCREAMING_SNAKE_CASE__     :Any						    = num_channels
													SCREAMING_SNAKE_CASE__     :Optional[Any]						    = embeddings_size
													SCREAMING_SNAKE_CASE__     :Any						    = hidden_sizes
													SCREAMING_SNAKE_CASE__     :Tuple						    = depths
													SCREAMING_SNAKE_CASE__     :Any						    = is_training
													SCREAMING_SNAKE_CASE__     :Union[str, Any]						    = use_labels
													SCREAMING_SNAKE_CASE__     :List[str]						    = hidden_act
													SCREAMING_SNAKE_CASE__     :Optional[int]						    = num_labels
													SCREAMING_SNAKE_CASE__     :Any						    = scope
													SCREAMING_SNAKE_CASE__     :Dict						    = len(__lowerCAmelCase      )
													SCREAMING_SNAKE_CASE__     :Any						    = out_features
													SCREAMING_SNAKE_CASE__     :str						    = out_indices
													SCREAMING_SNAKE_CASE__     :Tuple						    = num_groups




						def      __lowerCamelCase       (    self :       Optional[int]      )     ->				Dict:
													SCREAMING_SNAKE_CASE__     :Optional[int]						    = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]      )

													SCREAMING_SNAKE_CASE__     :Any						    = None
													if self.use_labels:
																				SCREAMING_SNAKE_CASE__     :str						    = ids_tensor([self.batch_size]							,							self.num_labels      )

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

													return config, pixel_values, labels




						def      __lowerCamelCase       (    self :       str      )     ->				Optional[int]:
													return BitConfig(
													    num_channels=self.num_channels							,							embeddings_size=self.embeddings_size							,							hidden_sizes=self.hidden_sizes							,							depths=self.depths							,							hidden_act=self.hidden_act							,							num_labels=self.num_labels							,							out_features=self.out_features							,							out_indices=self.out_indices							,							num_groups=self.num_groups							,							)




						def      __lowerCamelCase       (    self :       Dict							,							UpperCamelCase_ :       int							,							UpperCamelCase_ :       List[str]							,							UpperCamelCase_ :       Union[str, Any]      )     ->				Any:
													SCREAMING_SNAKE_CASE__     :Dict						    = BitModel(config=__lowerCAmelCase      )
													model.to(__lowerCAmelCase      )
													model.eval()
													SCREAMING_SNAKE_CASE__     :Optional[Any]						    = model(__lowerCAmelCase      )
													self.parent.assertEqual(
													    result.last_hidden_state.shape							,							(self.batch_size, self.hidden_sizes[-1], self.image_size // 32, self.image_size // 32)							,							)




						def      __lowerCamelCase       (    self :       Optional[Any]							,							UpperCamelCase_ :       List[Any]							,							UpperCamelCase_ :       str							,							UpperCamelCase_ :       Any      )     ->				str:
													SCREAMING_SNAKE_CASE__     :Optional[int]						    = self.num_labels
													SCREAMING_SNAKE_CASE__     :int						    = BitForImageClassification(__lowerCAmelCase      )
													model.to(__lowerCAmelCase      )
													model.eval()
													SCREAMING_SNAKE_CASE__     :str						    = model(__lowerCAmelCase							,							labels=__lowerCAmelCase      )
													self.parent.assertEqual(result.logits.shape							,							(self.batch_size, self.num_labels)      )




						def      __lowerCamelCase       (    self :       List[Any]							,							UpperCamelCase_ :       List[str]							,							UpperCamelCase_ :       List[str]							,							UpperCamelCase_ :       Any      )     ->				Any:
													SCREAMING_SNAKE_CASE__     :List[Any]						    = BitBackbone(config=__lowerCAmelCase      )
													model.to(__lowerCAmelCase      )
													model.eval()
													SCREAMING_SNAKE_CASE__     :Optional[int]						    = model(__lowerCAmelCase      )

													# verify feature maps
													self.parent.assertEqual(len(result.feature_maps      )							,							len(config.out_features      )      )
													self.parent.assertListEqual(list(result.feature_maps[0].shape      )							,							[self.batch_size, self.hidden_sizes[1], 4, 4]      )

													# verify channels
													self.parent.assertEqual(len(model.channels      )							,							len(config.out_features      )      )
													self.parent.assertListEqual(model.channels							,							config.hidden_sizes[1:]      )

													# verify backbone works with out_features=None
													SCREAMING_SNAKE_CASE__     :Any						    = None
													SCREAMING_SNAKE_CASE__     :Union[str, Any]						    = BitBackbone(config=__lowerCAmelCase      )
													model.to(__lowerCAmelCase      )
													model.eval()
													SCREAMING_SNAKE_CASE__     :Optional[Any]						    = model(__lowerCAmelCase      )

													# verify feature maps
													self.parent.assertEqual(len(result.feature_maps      )							,							1      )
													self.parent.assertListEqual(list(result.feature_maps[0].shape      )							,							[self.batch_size, self.hidden_sizes[-1], 1, 1]      )

													# verify channels
													self.parent.assertEqual(len(model.channels      )							,							1      )
													self.parent.assertListEqual(model.channels							,							[config.hidden_sizes[-1]]      )




						def      __lowerCamelCase       (    self :       str      )     ->				Optional[int]:
													SCREAMING_SNAKE_CASE__     :Tuple						    = self.prepare_config_and_inputs()
													SCREAMING_SNAKE_CASE__     :int						    = config_and_inputs
													SCREAMING_SNAKE_CASE__     :str						    = {'''pixel_values''': pixel_values}
													return config, inputs_dict




@require_torch
class _SCREAMING_SNAKE_CASE(							_SCREAMING_SNAKE_CASE						,						_SCREAMING_SNAKE_CASE						,						unittest.TestCase					):
						A_			:							Tuple     		= (BitModel, BitForImageClassification, BitBackbone) if is_torch_available() else ()
						A_			:							Tuple     		= (
						    {'feature-extraction': BitModel, 'image-classification': BitForImageClassification}
						    if is_torch_available()
						    else {}
						)

						A_			:							Dict     		= False
						A_			:							Dict     		= False
						A_			:							List[Any]     		= False
						A_			:							Dict     		= False
						A_			:							Optional[int]     		= False




						def      __lowerCamelCase       (    self :       int      )     ->				str:
													SCREAMING_SNAKE_CASE__     :Dict						    = BitModelTester(self      )
													SCREAMING_SNAKE_CASE__     :List[str]						    = ConfigTester(self							,							config_class=__lowerCAmelCase							,							has_text_modality=__lowerCAmelCase      )




						def      __lowerCamelCase       (    self :       str      )     ->				Any:
													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      __lowerCamelCase       (    self :       Any      )     ->				str:
													return




						@unittest.skip(reason='Bit does not output attentions'      )
						def      __lowerCamelCase       (    self :       int      )     ->				Optional[Any]:
													pass




						@unittest.skip(reason='Bit does not use inputs_embeds'      )
						def      __lowerCamelCase       (    self :       Any      )     ->				Optional[int]:
													pass




						@unittest.skip(reason='Bit does not support input and output embeddings'      )
						def      __lowerCamelCase       (    self :       List[str]      )     ->				int:
													pass




						def      __lowerCamelCase       (    self :       Any      )     ->				Optional[int]:
													SCREAMING_SNAKE_CASE__     :int						    = self.model_tester.prepare_config_and_inputs_for_common()

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

																				SCREAMING_SNAKE_CASE__     :Optional[int]						    = ['''pixel_values''']
																				self.assertListEqual(arg_names[:1]							,							__lowerCAmelCase      )




						def      __lowerCamelCase       (    self :       Tuple      )     ->				Any:
													SCREAMING_SNAKE_CASE__     :Tuple						    = self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_model(*__lowerCAmelCase      )




						def      __lowerCamelCase       (    self :       Tuple      )     ->				str:
													SCREAMING_SNAKE_CASE__     :str						    = self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_backbone(*__lowerCAmelCase      )




						def      __lowerCamelCase       (    self :       int      )     ->				int:
													SCREAMING_SNAKE_CASE__     :Optional[int]						    = self.model_tester.prepare_config_and_inputs_for_common()

													for model_class in self.all_model_classes:
																				SCREAMING_SNAKE_CASE__     :Tuple						    = model_class(config=__lowerCAmelCase      )
																				for name, module in model.named_modules():
																											if isinstance(__lowerCAmelCase							,							(nn.BatchNormad, nn.GroupNorm)      ):
																																		self.assertTrue(
																																		    torch.all(module.weight == 1      )							,							msg=f'''Parameter {name} of model {model_class} seems not properly initialized'''							,							)
																																		self.assertTrue(
																																		    torch.all(module.bias == 0      )							,							msg=f'''Parameter {name} of model {model_class} seems not properly initialized'''							,							)




						def      __lowerCamelCase       (    self :       Optional[Any]      )     ->				Dict:
													def check_hidden_states_output(UpperCamelCase_ :       List[str]							,							UpperCamelCase_ :       List[Any]							,							UpperCamelCase_ :       Union[str, Any]      ):
																				SCREAMING_SNAKE_CASE__     :Optional[int]						    = model_class(__lowerCAmelCase      )
																				model.to(__lowerCAmelCase      )
																				model.eval()

																				with torch.no_grad():
																											SCREAMING_SNAKE_CASE__     :Tuple						    = model(**self._prepare_for_class(__lowerCAmelCase							,							__lowerCAmelCase      )      )

																				SCREAMING_SNAKE_CASE__     :List[str]						    = outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

																				SCREAMING_SNAKE_CASE__     :Dict						    = self.model_tester.num_stages
																				self.assertEqual(len(__lowerCAmelCase      )							,							expected_num_stages + 1      )

																				# Bit's feature maps are of shape (batch_size, num_channels, height, width)
																				self.assertListEqual(
																				    list(hidden_states[0].shape[-2:]      )							,							[self.model_tester.image_size // 4, self.model_tester.image_size // 4]							,							)

													SCREAMING_SNAKE_CASE__     :Tuple						    = self.model_tester.prepare_config_and_inputs_for_common()
													SCREAMING_SNAKE_CASE__     :Tuple						    = ['''preactivation''', '''bottleneck''']
													for model_class in self.all_model_classes:
																				for layer_type in layers_type:
																											SCREAMING_SNAKE_CASE__     :int						    = layer_type
																											SCREAMING_SNAKE_CASE__     :Tuple						    = True
																											check_hidden_states_output(__lowerCAmelCase							,							__lowerCAmelCase							,							__lowerCAmelCase      )

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

																											check_hidden_states_output(__lowerCAmelCase							,							__lowerCAmelCase							,							__lowerCAmelCase      )




						@unittest.skip(reason='Bit does not use feedforward chunking'      )
						def      __lowerCamelCase       (    self :       Dict      )     ->				int:
													pass




						def      __lowerCamelCase       (    self :       Optional[Any]      )     ->				List[str]:
													SCREAMING_SNAKE_CASE__     :List[Any]						    = self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_for_image_classification(*__lowerCAmelCase      )




						@slow
						def      __lowerCamelCase       (    self :       List[str]      )     ->				Union[str, Any]:
													for model_name in BIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																				SCREAMING_SNAKE_CASE__     :List[str]						    = BitModel.from_pretrained(__lowerCAmelCase      )
																				self.assertIsNotNone(__lowerCAmelCase      )


def    lowerCamelCase       (			)			->	Dict:


							'''simple docstring'''






							SCREAMING_SNAKE_CASE__     :Union[str, Any]						    = Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'		)
							return image




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




						@cached_property
						def      __lowerCamelCase       (    self :       List[str]      )     ->				Tuple:
													return (
													    BitImageProcessor.from_pretrained(BIT_PRETRAINED_MODEL_ARCHIVE_LIST[0]      ) if is_vision_available() else None
													)




						@slow
						def      __lowerCamelCase       (    self :       List[Any]      )     ->				Optional[int]:
													SCREAMING_SNAKE_CASE__     :Any						    = BitForImageClassification.from_pretrained(BIT_PRETRAINED_MODEL_ARCHIVE_LIST[0]      ).to(__lowerCAmelCase      )

													SCREAMING_SNAKE_CASE__     :Union[str, Any]						    = self.default_image_processor
													SCREAMING_SNAKE_CASE__     :int						    = prepare_img()
													SCREAMING_SNAKE_CASE__     :Tuple						    = image_processor(images=__lowerCAmelCase							,							return_tensors='pt'      ).to(__lowerCAmelCase      )

													# forward pass
													with torch.no_grad():
																				SCREAMING_SNAKE_CASE__     :Tuple						    = model(**__lowerCAmelCase      )

													# verify the logits
													SCREAMING_SNAKE_CASE__     :Optional[Any]						    = torch.Size((1, 10_00)      )
													self.assertEqual(outputs.logits.shape							,							__lowerCAmelCase      )

													SCREAMING_SNAKE_CASE__     :int						    = torch.tensor([[-0.6526, -0.5263, -1.4398]]      ).to(__lowerCAmelCase      )

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




@require_torch
class _SCREAMING_SNAKE_CASE(							_SCREAMING_SNAKE_CASE						,						unittest.TestCase					):
						A_			:							Optional[Any]     		= (BitBackbone,) if is_torch_available() else ()
						A_			:							Tuple     		= BitConfig

						A_			:							Optional[Any]     		= False




						def      __lowerCamelCase       (    self :       Optional[Any]      )     ->				int:
													SCREAMING_SNAKE_CASE__     :Dict						    = BitModelTester(self      )














from sklearn.metrics import matthews_corrcoef

import datasets


SCREAMING_SNAKE_CASE__   :      Optional[Any]				=		"""
Compute the Matthews correlation coefficient (MCC)

The Matthews correlation coefficient is used in machine learning as a
measure of the quality of binary and multiclass classifications. It takes
into account true and false positives and negatives and is generally
regarded as a balanced measure which can be used even if the classes are of
very different sizes. The MCC is in essence a correlation coefficient value
between -1 and +1. A coefficient of +1 represents a perfect prediction, 0
an average random prediction and -1 an inverse prediction.  The statistic
is also known as the phi coefficient. [source: Wikipedia]
"""

SCREAMING_SNAKE_CASE__   :      Union[str, Any]				=		"""
Args:
    predictions (list of int): Predicted labels, as returned by a model.
    references (list of int): Ground truth labels.
    sample_weight (list of int, float, or bool): Sample weights. Defaults to `None`.
Returns:
    matthews_correlation (dict containing float): Matthews correlation.
Examples:
    Example 1, a basic example with only predictions and references as inputs:
        >>> matthews_metric = datasets.load_metric(\"matthews_correlation\")
        >>> results = matthews_metric.compute(references=[1, 3, 2, 0, 3, 2],
        ...                                     predictions=[1, 2, 2, 0, 3, 3])
        >>> print(round(results['matthews_correlation'], 2))
        0.54

    Example 2, the same example as above, but also including sample weights:
        >>> matthews_metric = datasets.load_metric(\"matthews_correlation\")
        >>> results = matthews_metric.compute(references=[1, 3, 2, 0, 3, 2],
        ...                                     predictions=[1, 2, 2, 0, 3, 3],
        ...                                     sample_weight=[0.5, 3, 1, 1, 1, 2])
        >>> print(round(results['matthews_correlation'], 2))
        0.1

    Example 3, the same example as above, but with sample weights that cause a negative correlation:
        >>> matthews_metric = datasets.load_metric(\"matthews_correlation\")
        >>> results = matthews_metric.compute(references=[1, 3, 2, 0, 3, 2],
        ...                                     predictions=[1, 2, 2, 0, 3, 3],
        ...                                     sample_weight=[0.5, 1, 0, 0, 0, 1])
        >>> print(round(results['matthews_correlation'], 2))
        -0.25
"""

SCREAMING_SNAKE_CASE__   :      int				=		"""\
@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}
}
"""
@datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION      ,					_KWARGS_DESCRIPTION			)
class     lowerCamelCase_					(   datasets.Metric			):







				def       A						(    self  ):
								"""simple docstring"""
								return datasets.MetricInfo(
								    description=_DESCRIPTION							,       citation=_CITATION							,       inputs_description=_KWARGS_DESCRIPTION							,       features=datasets.Features(
								        {
								            '''predictions''': datasets.Value('''int32'''  ),
								            '''references''': datasets.Value('''int32'''  ),
								        }  )							,       reference_urls=[
								        '''https://scikit-learn.org/stable/modules/generated/sklearn.metrics.matthews_corrcoef.html'''
								    ]							,       )







				def       A						(    self							,       __lowerCAmelCase							,       __lowerCAmelCase							,       __lowerCAmelCase=None  ):
								"""simple docstring"""
								return {
								    "matthews_correlation": float(matthews_corrcoef(__lowerCAmelCase							,       __lowerCAmelCase							,       sample_weight=__lowerCAmelCase  )  ),
								}






