




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

						"""simple docstring"""

						def __init__(       self   :	Optional[int]	,    snake_case   :	str	,    snake_case   :	str=100	,    snake_case   :	Union[str, Any]=13	,    snake_case   :	int=30	,    snake_case   :	Dict=2	,    snake_case   :	Union[str, Any]=3	,    snake_case   :	Tuple=True	,    snake_case   :	List[Any]=True	,    snake_case   :	int=32	,    snake_case   :	Tuple=4	,    snake_case   :	List[Any]=4	,    snake_case   :	int=37	,    snake_case   :	List[str]="gelu"	,    snake_case   :	List[Any]=0.1	,    snake_case   :	Any=0.1	,    snake_case   :	Dict=10	,    snake_case   :	Dict=0.02	,    snake_case   :	Any=3	,    snake_case   :	Optional[int]=None	,    snake_case   :	int=[0, 1, 2, 3]	,    ):
										__UpperCamelCase											=  parent
										__UpperCamelCase											=  100
										__UpperCamelCase											=  batch_size
										__UpperCamelCase											=  image_size
										__UpperCamelCase											=  patch_size
										__UpperCamelCase											=  num_channels
										__UpperCamelCase											=  is_training
										__UpperCamelCase											=  use_labels
										__UpperCamelCase											=  hidden_size
										__UpperCamelCase											=  num_hidden_layers
										__UpperCamelCase											=  num_attention_heads
										__UpperCamelCase											=  intermediate_size
										__UpperCamelCase											=  hidden_act
										__UpperCamelCase											=  hidden_dropout_prob
										__UpperCamelCase											=  attention_probs_dropout_prob
										__UpperCamelCase											=  type_sequence_label_size
										__UpperCamelCase											=  initializer_range
										__UpperCamelCase											=  scope
										__UpperCamelCase											=  out_indices
										__UpperCamelCase											=  num_labels

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







						def  snake_case		(       self   :	int			):
										__UpperCamelCase											=  floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]			)

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

										__UpperCamelCase											=  self.get_config()

										return config, pixel_values, labels, pixel_labels







						def  snake_case		(       self   :	str			):
										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=snake_case	,    initializer_range=self.initializer_range	,    out_indices=self.out_indices	,    )







						def  snake_case		(       self   :	Dict	,    snake_case   :	int	,    snake_case   :	List[Any]	,    snake_case   :	Any	,    snake_case   :	Any			):
										__UpperCamelCase											=  DataaVecVisionModel(config=snake_case			)
										model.to(snake_case			)
										model.eval()
										__UpperCamelCase											=  model(snake_case			)
										# expected sequence length = num_patches + 1 (we add 1 for the [CLS] token)
										__UpperCamelCase											=  (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  snake_case		(       self   :	Union[str, Any]	,    snake_case   :	str	,    snake_case   :	Tuple	,    snake_case   :	List[Any]	,    snake_case   :	Optional[int]			):
										__UpperCamelCase											=  self.type_sequence_label_size
										__UpperCamelCase											=  DataaVecVisionForImageClassification(snake_case			)
										model.to(snake_case			)
										model.eval()
										__UpperCamelCase											=  model(snake_case	,    labels=snake_case			)
										self.parent.assertEqual(result.logits.shape	,    (self.batch_size, self.type_sequence_label_size)			)







						def  snake_case		(       self   :	Optional[Any]	,    snake_case   :	int	,    snake_case   :	Any	,    snake_case   :	List[Any]	,    snake_case   :	List[str]			):
										__UpperCamelCase											=  self.num_labels
										__UpperCamelCase											=  DataaVecVisionForSemanticSegmentation(snake_case			)
										model.to(snake_case			)
										model.eval()
										__UpperCamelCase											=  model(snake_case			)
										self.parent.assertEqual(
										    result.logits.shape	,    (self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2)			)
										__UpperCamelCase											=  model(snake_case	,    labels=snake_case			)
										self.parent.assertEqual(
										    result.logits.shape	,    (self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2)			)







						def  snake_case		(       self   :	Union[str, Any]			):
										__UpperCamelCase											=  self.prepare_config_and_inputs()
										__UpperCamelCase						, __UpperCamelCase						, __UpperCamelCase						, __UpperCamelCase											=  config_and_inputs
										__UpperCamelCase											=  {'''pixel_values''': pixel_values}
										return config, inputs_dict







@require_torch
class 	_lowerCamelCase  (     _SCREAMING_SNAKE_CASE							,     _SCREAMING_SNAKE_CASE							,     unittest.TestCase				):

						"""simple docstring"""

						lowerCAmelCase__     :	str      =   (
						    (DataaVecVisionModel, DataaVecVisionForImageClassification, DataaVecVisionForSemanticSegmentation)
						    if is_torch_available()
						    else ()
						)
						lowerCAmelCase__     :	Tuple      =   (
						    {
						        "feature-extraction": DataaVecVisionModel,
						        "image-classification": DataaVecVisionForImageClassification,
						        "image-segmentation": DataaVecVisionForSemanticSegmentation,
						    }
						    if is_torch_available()
						    else {}
						)

						lowerCAmelCase__     :	List[Any]      =   False
						lowerCAmelCase__     :	Optional[Any]      =   False
						lowerCAmelCase__     :	Union[str, Any]      =   False







						def  snake_case		(       self   :	List[str]			):
										__UpperCamelCase											=  DataaVecVisionModelTester(self			)
										__UpperCamelCase											=  ConfigTester(
										    self	,    config_class=snake_case	,    has_text_modality=snake_case	,    hidden_size=37			)







						def  snake_case		(       self   :	str			):
										self.config_tester.run_common_tests()







						def  snake_case		(       self   :	List[Any]			):
										# Data2VecVision does not use inputs_embeds
										pass







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







						def  snake_case		(       self   :	Tuple			):
										__UpperCamelCase						, __UpperCamelCase											=  self.model_tester.prepare_config_and_inputs_for_common()

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







						def  snake_case		(       self   :	Any			):
										__UpperCamelCase						, __UpperCamelCase											=  self.model_tester.prepare_config_and_inputs_for_common()

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

														__UpperCamelCase											=  ['''pixel_values''']
														self.assertListEqual(arg_names[:1]	,    snake_case			)







						def  snake_case		(       self   :	Any			):
										__UpperCamelCase											=  self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_model(*snake_case			)







						def  snake_case		(       self   :	Optional[Any]			):
										__UpperCamelCase											=  self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_image_segmentation(*snake_case			)







						def  snake_case		(       self   :	Any			):
										if not self.model_tester.is_training:
														return

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

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

														__UpperCamelCase											=  model_class(snake_case			)
														model.to(snake_case			)
														model.train()
														__UpperCamelCase											=  self._prepare_for_class(snake_case	,    snake_case	,    return_labels=snake_case			)
														__UpperCamelCase											=  model(**snake_case			).loss
														loss.backward()







						def  snake_case		(       self   :	Tuple			):
										__UpperCamelCase						, __UpperCamelCase											=  self.model_tester.prepare_config_and_inputs_for_common()
										if not self.model_tester.is_training:
														return

										__UpperCamelCase											=  False
										__UpperCamelCase											=  True

										for model_class in self.all_model_classes:
														if model_class in [*get_values(snake_case			)] 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":
																		__UpperCamelCase						, __UpperCamelCase						, __UpperCamelCase						, __UpperCamelCase											=  inputs_dict['''pixel_values'''].shape
																		__UpperCamelCase											=  torch.zeros(
																		    [self.model_tester.batch_size, height, width]	,    device=snake_case			).long()
														__UpperCamelCase											=  model_class(snake_case			)
														model.gradient_checkpointing_enable()
														model.to(snake_case			)
														model.train()
														__UpperCamelCase											=  self._prepare_for_class(snake_case	,    snake_case	,    return_labels=snake_case			)
														__UpperCamelCase											=  model(**snake_case			).loss
														loss.backward()







						def  snake_case		(       self   :	List[Any]			):
										__UpperCamelCase						, __UpperCamelCase											=  self.model_tester.prepare_config_and_inputs_for_common()

										__UpperCamelCase											=  _config_zero_init(snake_case			)
										for model_class in self.all_model_classes:
														__UpperCamelCase											=  model_class(config=snake_case			)
														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  snake_case		(       self   :	Any	,    snake_case   :	int	,    snake_case   :	Optional[Any]	,    snake_case   :	List[str]	,    snake_case   :	Dict=2E-4	,    snake_case   :	Tuple="outputs"	,    snake_case   :	List[Any]=None			):
										# We override with a slightly higher tol value, as semseg models tend to diverge a bit more
										super().check_pt_tf_outputs(snake_case	,    snake_case	,    snake_case	,    snake_case	,    snake_case	,    snake_case			)







						def  snake_case		(       self   :	Tuple			):
										__UpperCamelCase											=  self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_image_classification(*snake_case			)







						@slow
						def  snake_case		(       self   :	Dict			):
										for model_name in DATA2VEC_VISION_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
														__UpperCamelCase											=  DataaVecVisionModel.from_pretrained(snake_case			)
														self.assertIsNotNone(snake_case			)







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

				"""simple docstring"""


				__UpperCamelCase											=  Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' )
				return image


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

						"""simple docstring"""

						@cached_property
						def  snake_case		(       self   :	Any			):
										return (
										    BeitImageProcessor.from_pretrained('''facebook/data2vec-vision-base-ft1k'''			) if is_vision_available() else None
										)







						@slow
						def  snake_case		(       self   :	int			):
										__UpperCamelCase											=  DataaVecVisionForImageClassification.from_pretrained('''facebook/data2vec-vision-base-ft1k'''			).to(
										    snake_case			)

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

										# forward pass
										with torch.no_grad():
														__UpperCamelCase											=  model(**snake_case			)
										__UpperCamelCase											=  outputs.logits

										# verify the logits
										__UpperCamelCase											=  torch.Size((1, 1000)			)
										self.assertEqual(logits.shape	,    snake_case			)

										__UpperCamelCase											=  torch.tensor([0.32_77, -0.13_95, 0.09_11]			).to(snake_case			)

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

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












import argparse
import json
import re
import string
import sys
from collections import Counter




def    __SCREAMING_SNAKE_CASE				(					lowercase_ )    ->		Dict:

    """simple docstring"""


    def remove_articles(lowercase_ ):
        return re.sub(r'''\b(a|an|the)\b'''   ,       ''' '''   ,       lowercase_ )

    def white_space_fix(lowercase_ ):
        return " ".join(text.split() )

    def remove_punc(lowercase_ ):
        __UpperCamelCase											=  set(string.punctuation )
        return "".join(ch for ch in text if ch not in exclude )

    def lower(lowercase_ ):
        return text.lower()

    return white_space_fix(remove_articles(remove_punc(lower(lowercase_ ) ) ) )




def    __SCREAMING_SNAKE_CASE				(					lowercase_   ,       lowercase_ )    ->		List[str]:

    """simple docstring"""


    __UpperCamelCase											=  normalize_answer(lowercase_ ).split()
    __UpperCamelCase											=  normalize_answer(lowercase_ ).split()
    __UpperCamelCase											=  Counter(lowercase_ ) & Counter(lowercase_ )
    __UpperCamelCase											=  sum(common.values() )
    if num_same == 0:
        return 0
    __UpperCamelCase											=  1.0 * num_same / len(lowercase_ )
    __UpperCamelCase											=  1.0 * num_same / len(lowercase_ )
    __UpperCamelCase											=  (2 * precision * recall) / (precision + recall)
    return fa




def    __SCREAMING_SNAKE_CASE				(					lowercase_   ,       lowercase_ )    ->		Any:

    """simple docstring"""


    return normalize_answer(lowercase_ ) == normalize_answer(lowercase_ )




def    __SCREAMING_SNAKE_CASE				(					lowercase_   ,       lowercase_   ,       lowercase_ )    ->		int:

    """simple docstring"""


    __UpperCamelCase											=  []
    for ground_truth in ground_truths:
        __UpperCamelCase											=  metric_fn(lowercase_   ,       lowercase_ )
        scores_for_ground_truths.append(lowercase_ )
    return max(lowercase_ )




def    __SCREAMING_SNAKE_CASE				(					lowercase_   ,       lowercase_ )    ->		Optional[int]:

    """simple docstring"""


    __UpperCamelCase											=  __UpperCamelCase											=  __UpperCamelCase											=  0
    __UpperCamelCase											=  []
    for passage in dataset:
        for qa in passage["qas"]:
            total += 1
            if qa["id"] not in predictions:
                __UpperCamelCase											=  F"Unanswered question {qa['id']} will receive score 0."
                print(lowercase_   ,       file=sys.stderr )
                continue

            __UpperCamelCase											=  [x['''text'''] for x in qa['''answers''']]
            __UpperCamelCase											=  predictions[qa['''id''']]

            __UpperCamelCase											=  metric_max_over_ground_truths(lowercase_   ,       lowercase_   ,       lowercase_ )
            if int(_exact_match ) == 1:
                correct_ids.append(qa['''id'''] )
            exact_match += _exact_match

            fa += metric_max_over_ground_truths(lowercase_   ,       lowercase_   ,       lowercase_ )

    __UpperCamelCase											=  exact_match / total
    __UpperCamelCase											=  fa / total

    return {"exact_match": exact_match, "f1": fa}, correct_ids


if __name__ == "__main__":
 a_										=       "1.0"
 a_										=       argparse.ArgumentParser("Official evaluation script for ReCoRD v1.0.")
 parser.add_argument("data_file", help="The dataset file in JSON format.")
 parser.add_argument("pred_file", help="The model prediction file in JSON format.")
 parser.add_argument("--output_correct_ids", action="store_true", help="Output the correctly answered query IDs.")
 a_										=       parser.parse_args()

 with open(args.data_file) as data_file:
  a_										=       json.load(data_file)
  if dataset_json["version"] != expected_version:
   print(
       f"""Evaluation expects v-{expected_version}, but got dataset with v-{dataset_json['version']}""",
       file=sys.stderr,
   )
  a_										=       dataset_json["data"]

 with open(args.pred_file) as pred_file:
  a_										=       json.load(pred_file)

 a_						,      a_										=       evaluate(dataset, predictions)

 if args.output_correct_ids:
  print(f"""Output {len(correct_ids)} correctly answered question IDs.""")
  with open("correct_ids.json", "w") as f:
   json.dump(correct_ids, f)






