





import inspect
import unittest

from transformers import DataaVecVisionConfig
from transformers.models.auto import get_values
from transformers.testing_utils import require_torch, require_torch_multi_gpu, 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 (
		    MODEL_MAPPING,
		    DataaVecVisionForImageClassification,
		    DataaVecVisionForSemanticSegmentation,
		    DataaVecVisionModel,
		)
		from transformers.models.dataavec.modeling_dataavec_vision import DATA2VEC_VISION_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
		from PIL import Image

		from transformers import BeitImageProcessor

class 		snake_case_      :







	'''simple docstring'''




	def __init__(	self       :    List[str]				,			__lowerCamelCase       :    Tuple				,			__lowerCamelCase       :    Optional[Any]=100				,			__lowerCamelCase       :    List[Any]=13				,			__lowerCamelCase       :    Dict=30				,			__lowerCamelCase       :    List[str]=2				,			__lowerCamelCase       :    int=3				,			__lowerCamelCase       :    List[Any]=True				,			__lowerCamelCase       :    List[Any]=True				,			__lowerCamelCase       :    str=32				,			__lowerCamelCase       :    List[Any]=4				,			__lowerCamelCase       :    int=4				,			__lowerCamelCase       :    Tuple=37				,			__lowerCamelCase       :    List[Any]="gelu"				,			__lowerCamelCase       :    List[str]=0.1				,			__lowerCamelCase       :    List[str]=0.1				,			__lowerCamelCase       :    List[Any]=10				,			__lowerCamelCase       :    Tuple=0.02				,			__lowerCamelCase       :    Optional[int]=3				,			__lowerCamelCase       :    Tuple=None				,			__lowerCamelCase       :    List[str]=[0, 1, 2, 3]				,			)					->   Optional[int]:

			'''simple docstring'''




			__lowercase					=    parent
			__lowercase					=    100
			__lowercase					=    batch_size
			__lowercase					=    image_size
			__lowercase					=    patch_size
			__lowercase					=    num_channels
			__lowercase					=    is_training
			__lowercase					=    use_labels
			__lowercase					=    hidden_size
			__lowercase					=    num_hidden_layers
			__lowercase					=    num_attention_heads
			__lowercase					=    intermediate_size
			__lowercase					=    hidden_act
			__lowercase					=    hidden_dropout_prob
			__lowercase					=    attention_probs_dropout_prob
			__lowercase					=    type_sequence_label_size
			__lowercase					=    initializer_range
			__lowercase					=    scope
			__lowercase					=    out_indices
			__lowercase					=    num_labels

			# in BeiT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token)
			__lowercase					=    (image_size // patch_size) ** 2
			__lowercase					=    num_patches + 1




	def       UpperCAmelCase (	self       :    Any      )					->   Tuple:

			'''simple docstring'''




			__lowercase					=    floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]      )

			__lowercase					=    None
			__lowercase					=    None
			if self.use_labels:
					__lowercase					=    ids_tensor([self.batch_size]				,			self.type_sequence_label_size      )
					__lowercase					=    ids_tensor([self.batch_size, self.image_size, self.image_size]				,			self.num_labels      )

			__lowercase					=    self.get_config()

			return config, pixel_values, labels, pixel_labels




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

			'''simple docstring'''




			return DataaVecVisionConfig(
			    vocab_size=self.vocab_size				,			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=__lowerCamelCase				,			initializer_range=self.initializer_range				,			out_indices=self.out_indices				,			)




	def       UpperCAmelCase (	self       :    Union[str, Any]				,			__lowerCamelCase       :    Optional[Any]				,			__lowerCamelCase       :    List[str]				,			__lowerCamelCase       :    Any				,			__lowerCamelCase       :    List[Any]      )					->   List[str]:

			'''simple docstring'''




			__lowercase					=    DataaVecVisionModel(config=__lowerCamelCase      )
			model.to(__lowerCamelCase      )
			model.eval()
			__lowercase					=    model(__lowerCamelCase      )
			# expected sequence length = num_patches + 1 (we add 1 for the [CLS] token)
			__lowercase					=    (self.image_size // self.patch_size) ** 2
			self.parent.assertEqual(result.last_hidden_state.shape				,			(self.batch_size, num_patches + 1, self.hidden_size)      )




	def       UpperCAmelCase (	self       :    Union[str, Any]				,			__lowerCamelCase       :    Dict				,			__lowerCamelCase       :    Optional[Any]				,			__lowerCamelCase       :    int				,			__lowerCamelCase       :    Optional[Any]      )					->   List[Any]:

			'''simple docstring'''




			__lowercase					=    self.type_sequence_label_size
			__lowercase					=    DataaVecVisionForImageClassification(__lowerCamelCase      )
			model.to(__lowerCamelCase      )
			model.eval()
			__lowercase					=    model(__lowerCamelCase				,			labels=__lowerCamelCase      )
			self.parent.assertEqual(result.logits.shape				,			(self.batch_size, self.type_sequence_label_size)      )




	def       UpperCAmelCase (	self       :    Optional[Any]				,			__lowerCamelCase       :    Optional[Any]				,			__lowerCamelCase       :    str				,			__lowerCamelCase       :    List[str]				,			__lowerCamelCase       :    str      )					->   List[Any]:

			'''simple docstring'''




			__lowercase					=    self.num_labels
			__lowercase					=    DataaVecVisionForSemanticSegmentation(__lowerCamelCase      )
			model.to(__lowerCamelCase      )
			model.eval()
			__lowercase					=    model(__lowerCamelCase      )
			self.parent.assertEqual(
			    result.logits.shape				,			(self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2)      )
			__lowercase					=    model(__lowerCamelCase				,			labels=__lowerCamelCase      )
			self.parent.assertEqual(
			    result.logits.shape				,			(self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2)      )






	def       UpperCAmelCase (	self       :    List[str]      )					->   Any:

			'''simple docstring'''




			__lowercase					=    self.prepare_config_and_inputs()
			__lowercase	,			__lowercase	,			__lowercase	,			__lowercase					=    config_and_inputs
			__lowercase					=    {'pixel_values': pixel_values}
			return config, inputs_dict





@require_torch
class 		snake_case_      (       UpperCAmelCase_    ,    UpperCAmelCase_    ,    unittest.TestCase	):







	'''simple docstring'''
	__UpperCamelCase        =		(
	    (DataaVecVisionModel, DataaVecVisionForImageClassification, DataaVecVisionForSemanticSegmentation)
	    if is_torch_available()
	    else ()
	)
	__UpperCamelCase        =		(
	    {
	        '''feature-extraction''': DataaVecVisionModel,
	        '''image-classification''': DataaVecVisionForImageClassification,
	        '''image-segmentation''': DataaVecVisionForSemanticSegmentation,
	    }
	    if is_torch_available()
	    else {}
	)

	__UpperCamelCase        =		False
	__UpperCamelCase        =		False
	__UpperCamelCase        =		False




	def       UpperCAmelCase (	self       :    Optional[int]      )					->   Union[str, Any]:

			'''simple docstring'''




			__lowercase					=    DataaVecVisionModelTester(self      )
			__lowercase					=    ConfigTester(
			    self				,			config_class=__lowerCamelCase				,			has_text_modality=__lowerCamelCase				,			hidden_size=37      )




	def       UpperCAmelCase (	self       :    int      )					->   Optional[Any]:

			'''simple docstring'''




			self.config_tester.run_common_tests()




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

			'''simple docstring'''




			pass




	@require_torch_multi_gpu
	@unittest.skip(
	    reason='Data2VecVision has some layers using `add_module` which doesn\'t work well with `nn.DataParallel`'      )
	def       UpperCAmelCase (	self       :    Optional[Any]      )					->   Optional[Any]:

			'''simple docstring'''




			pass




	def       UpperCAmelCase (	self       :    Dict      )					->   Tuple:

			'''simple docstring'''




			__lowercase	,			__lowercase					=    self.model_tester.prepare_config_and_inputs_for_common()

			for model_class in self.all_model_classes:
					__lowercase					=    model_class(__lowerCamelCase      )
					self.assertIsInstance(model.get_input_embeddings()				,			(nn.Module)      )
					__lowercase					=    model.get_output_embeddings()
					self.assertTrue(x is None or isinstance(__lowerCamelCase				,			nn.Linear      )      )




	def       UpperCAmelCase (	self       :    Union[str, Any]      )					->   Optional[int]:

			'''simple docstring'''




			__lowercase	,			__lowercase					=    self.model_tester.prepare_config_and_inputs_for_common()

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

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




	def       UpperCAmelCase (	self       :    str      )					->   Any:

			'''simple docstring'''




			__lowercase					=    self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_model(*__lowerCamelCase      )




	def       UpperCAmelCase (	self       :    Tuple      )					->   Optional[Any]:

			'''simple docstring'''




			__lowercase					=    self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_for_image_segmentation(*__lowerCamelCase      )




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

			'''simple docstring'''




			if not self.model_tester.is_training:
					return

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

			for model_class in self.all_model_classes:
					if model_class in [*get_values(__lowerCamelCase      )]:
							continue

					__lowercase					=    model_class(__lowerCamelCase      )
					model.to(__lowerCamelCase      )
					model.train()
					__lowercase					=    self._prepare_for_class(__lowerCamelCase				,			__lowerCamelCase				,			return_labels=__lowerCamelCase      )
					__lowercase					=    model(**__lowerCamelCase      ).loss
					loss.backward()




	def       UpperCAmelCase (	self       :    Tuple      )					->   Dict:

			'''simple docstring'''




			__lowercase	,			__lowercase					=    self.model_tester.prepare_config_and_inputs_for_common()
			if not self.model_tester.is_training:
					return

			__lowercase					=    False
			__lowercase					=    True

			for model_class in self.all_model_classes:
					if model_class in [*get_values(__lowerCamelCase      )] or not model_class.supports_gradient_checkpointing:
							continue
					# TODO: remove the following 3 lines once we have a MODEL_FOR_SEMANTIC_SEGMENTATION_MAPPING
					# this can then be incorporated into _prepare_for_class in test_modeling_common.py
					elif model_class.__name__ == "Data2VecVisionForSemanticSegmentation":
							__lowercase	,			__lowercase	,			__lowercase	,			__lowercase					=    inputs_dict['pixel_values'].shape
							__lowercase					=    torch.zeros(
							    [self.model_tester.batch_size, height, width]				,			device=__lowerCamelCase      ).long()
					__lowercase					=    model_class(__lowerCamelCase      )
					model.gradient_checkpointing_enable()
					model.to(__lowerCamelCase      )
					model.train()
					__lowercase					=    self._prepare_for_class(__lowerCamelCase				,			__lowerCamelCase				,			return_labels=__lowerCamelCase      )
					__lowercase					=    model(**__lowerCamelCase      ).loss
					loss.backward()




	def       UpperCAmelCase (	self       :    Any      )					->   int:

			'''simple docstring'''




			__lowercase	,			__lowercase					=    self.model_tester.prepare_config_and_inputs_for_common()

			__lowercase					=    _config_zero_init(__lowerCamelCase      )
			for model_class in self.all_model_classes:
					__lowercase					=    model_class(config=__lowerCamelCase      )
					for name, param in model.named_parameters():
							# we skip lambda parameters as these require special initial values
							# determined by config.layer_scale_init_value
							if "lambda" in name:
									continue
							if param.requires_grad:
									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"				,			)




	def       UpperCAmelCase (	self       :    List[str]				,			__lowerCamelCase       :    Optional[Any]				,			__lowerCamelCase       :    List[str]				,			__lowerCamelCase       :    str				,			__lowerCamelCase       :    Tuple=2E-4				,			__lowerCamelCase       :    Optional[int]="outputs"				,			__lowerCamelCase       :    Optional[Any]=None      )					->   str:

			'''simple docstring'''




			super().check_pt_tf_outputs(__lowerCamelCase				,			__lowerCamelCase				,			__lowerCamelCase				,			__lowerCamelCase				,			__lowerCamelCase				,			__lowerCamelCase      )




	def       UpperCAmelCase (	self       :    Optional[Any]      )					->   List[Any]:

			'''simple docstring'''




			__lowercase					=    self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_for_image_classification(*__lowerCamelCase      )






	@slow
	def       UpperCAmelCase (	self       :    int      )					->   int:

			'''simple docstring'''




			for model_name in DATA2VEC_VISION_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
					__lowercase					=    DataaVecVisionModel.from_pretrained(__lowerCamelCase      )
					self.assertIsNotNone(__lowerCamelCase      )





def    SCREAMING_SNAKE_CASE							(			)		->						List[str]:
		__lowercase					=    Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'       )
		return image


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







	'''simple docstring'''




	@cached_property
	def       UpperCAmelCase (	self       :    Tuple      )					->   List[str]:

			'''simple docstring'''




			return (
			    BeitImageProcessor.from_pretrained('facebook/data2vec-vision-base-ft1k'      ) if is_vision_available() else None
			)






	@slow
	def       UpperCAmelCase (	self       :    Dict      )					->   Dict:

			'''simple docstring'''




			__lowercase					=    DataaVecVisionForImageClassification.from_pretrained('facebook/data2vec-vision-base-ft1k'      ).to(
			    __lowerCamelCase      )

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

			# forward pass
			with torch.no_grad():
					__lowercase					=    model(**__lowerCamelCase      )
			__lowercase					=    outputs.logits

			# verify the logits
			__lowercase					=    torch.Size((1, 1_000)      )
			self.assertEqual(logits.shape				,			__lowerCamelCase      )

			__lowercase					=    torch.tensor([0.3277, -0.1395, 0.0911]      ).to(__lowerCamelCase      )

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

			__lowercase					=    [model.config.labelaid[i] for i in ['remote control, remote', 'tabby, tabby cat']]
			self.assertEqual(logits[0].topk(2      ).indices.cpu().tolist()				,			__lowerCamelCase      )











import importlib
from typing import List, Optional, Union

from seqeval.metrics import accuracy_score, classification_report

import datasets


SCREAMING_SNAKE_CASE_      :			Any		  =      '''\
@inproceedings{ramshaw-marcus-1995-text,
    title = "Text Chunking using Transformation-Based Learning",
    author = "Ramshaw, Lance  and
      Marcus, Mitch",
    booktitle = "Third Workshop on Very Large Corpora",
    year = "1995",
    url = "https://www.aclweb.org/anthology/W95-0107",
}
@misc{seqeval,
  title={{seqeval}: A Python framework for sequence labeling evaluation},
  url={https://github.com/chakki-works/seqeval},
  note={Software available from https://github.com/chakki-works/seqeval},
  author={Hiroki Nakayama},
  year={2018},
}
'''

SCREAMING_SNAKE_CASE_      :			Tuple		  =      '''\
seqeval is a Python framework for sequence labeling evaluation.
seqeval can evaluate the performance of chunking tasks such as named-entity recognition, part-of-speech tagging, semantic role labeling and so on.

This is well-tested by using the Perl script conlleval, which can be used for
measuring the performance of a system that has processed the CoNLL-2000 shared task data.

seqeval supports following formats:
IOB1
IOB2
IOE1
IOE2
IOBES

See the [README.md] file at https://github.com/chakki-works/seqeval for more information.
'''

SCREAMING_SNAKE_CASE_      :			Dict		  =      '''
Produces labelling scores along with its sufficient statistics
from a source against one or more references.

Args:
    predictions: List of List of predicted labels (Estimated targets as returned by a tagger)
    references: List of List of reference labels (Ground truth (correct) target values)
    suffix: True if the IOB prefix is after type, False otherwise. default: False
    scheme: Specify target tagging scheme. Should be one of ["IOB1", "IOB2", "IOE1", "IOE2", "IOBES", "BILOU"].
        default: None
    mode: Whether to count correct entity labels with incorrect I/B tags as true positives or not.
        If you want to only count exact matches, pass mode="strict". default: None.
    sample_weight: Array-like of shape (n_samples,), weights for individual samples. default: None
    zero_division: Which value to substitute as a metric value when encountering zero division. Should be on of 0, 1,
        "warn". "warn" acts as 0, but the warning is raised.

Returns:
    \'scores\': dict. Summary of the scores for overall and per type
        Overall:
            \'accuracy\': accuracy,
            \'precision\': precision,
            \'recall\': recall,
            \'f1\': F1 score, also known as balanced F-score or F-measure,
        Per type:
            \'precision\': precision,
            \'recall\': recall,
            \'f1\': F1 score, also known as balanced F-score or F-measure
Examples:

    >>> predictions = [[\'O\', \'O\', \'B-MISC\', \'I-MISC\', \'I-MISC\', \'I-MISC\', \'O\'], [\'B-PER\', \'I-PER\', \'O\']]
    >>> references = [[\'O\', \'O\', \'O\', \'B-MISC\', \'I-MISC\', \'I-MISC\', \'O\'], [\'B-PER\', \'I-PER\', \'O\']]
    >>> seqeval = datasets.load_metric("seqeval")
    >>> results = seqeval.compute(predictions=predictions, references=references)
    >>> print(list(results.keys()))
    [\'MISC\', \'PER\', \'overall_precision\', \'overall_recall\', \'overall_f1\', \'overall_accuracy\']
    >>> print(results["overall_f1"])
    0.5
    >>> print(results["PER"]["f1"])
    1.0
'''

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







	'''simple docstring'''




	def       UpperCAmelCase (	self       :    Any      )					->   str:

			'''simple docstring'''




			return datasets.MetricInfo(
			    description=_DESCRIPTION				,			citation=_CITATION				,			homepage='https://github.com/chakki-works/seqeval'				,			inputs_description=_KWARGS_DESCRIPTION				,			features=datasets.Features(
			        {
			            'predictions': datasets.Sequence(datasets.Value('string'				,			id='label'      )				,			id='sequence'      ),
			            'references': datasets.Sequence(datasets.Value('string'				,			id='label'      )				,			id='sequence'      ),
			        }      )				,			codebase_urls=['https://github.com/chakki-works/seqeval']				,			reference_urls=['https://github.com/chakki-works/seqeval']				,			)






	def       UpperCAmelCase (	self       :    Optional[Any]				,			__lowerCamelCase       :    int				,			__lowerCamelCase       :    str				,			__lowerCamelCase       :    bool = False				,			__lowerCamelCase       :    Optional[str] = None				,			__lowerCamelCase       :    Optional[str] = None				,			__lowerCamelCase       :    Optional[List[int]] = None				,			__lowerCamelCase       :    Union[str, int] = "warn"				,			)					->   Tuple:

			'''simple docstring'''




			if scheme is not None:
					try:
							__lowercase					=    importlib.import_module('seqeval.scheme'      )
							__lowercase					=    getattr(__lowerCamelCase				,			__lowerCamelCase      )
					except AttributeError:
							raise ValueError(F"Scheme should be one of [IOB1, IOB2, IOE1, IOE2, IOBES, BILOU], got {scheme}"      )
			__lowercase					=    classification_report(
			    y_true=__lowerCamelCase				,			y_pred=__lowerCamelCase				,			suffix=__lowerCamelCase				,			output_dict=__lowerCamelCase				,			scheme=__lowerCamelCase				,			mode=__lowerCamelCase				,			sample_weight=__lowerCamelCase				,			zero_division=__lowerCamelCase				,			)
			report.pop('macro avg'      )
			report.pop('weighted avg'      )
			__lowercase					=    report.pop('micro avg'      )

			__lowercase					=    {
			    type_name: {
			        'precision': score['precision'],
			        'recall': score['recall'],
			        'f1': score['f1-score'],
			        'number': score['support'],
			    }
			    for type_name, score in report.items()
			}
			__lowercase					=    overall_score['precision']
			__lowercase					=    overall_score['recall']
			__lowercase					=    overall_score['f1-score']
			__lowercase					=    accuracy_score(y_true=__lowerCamelCase				,			y_pred=__lowerCamelCase      )

			return scores




