





import collections
import inspect
import unittest

from transformers import DinatConfig
from transformers.testing_utils import require_natten, 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, _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 DinatBackbone, DinatForImageClassification, DinatModel
       from transformers.models.dinat.modeling_dinat import DINAT_PRETRAINED_MODEL_ARCHIVE_LIST

if is_vision_available():
       from PIL import Image

       from transformers import AutoImageProcessor





class A_      :




   def __init__(self			:List[Any]					,     _UpperCamelCase			:List[Any]					,     _UpperCamelCase			:Tuple=13					,     _UpperCamelCase			:Optional[int]=64					,     _UpperCamelCase			:Optional[int]=4					,     _UpperCamelCase			:int=3					,     _UpperCamelCase			:List[Any]=16					,     _UpperCamelCase			:Union[str, Any]=[1, 2, 1]					,     _UpperCamelCase			:Optional[Any]=[2, 4, 8]					,     _UpperCamelCase			:Optional[int]=3					,     _UpperCamelCase			:Dict=[[3], [1, 2], [1]]					,     _UpperCamelCase			:Optional[Any]=2.0					,     _UpperCamelCase			:Optional[Any]=True					,     _UpperCamelCase			:Any=0.0					,     _UpperCamelCase			:Optional[Any]=0.0					,     _UpperCamelCase			:Tuple=0.1					,     _UpperCamelCase			:Dict="gelu"					,     _UpperCamelCase			:Any=True					,     _UpperCamelCase			:Any=0.0_2					,     _UpperCamelCase			:str=1e-5					,     _UpperCamelCase			:Dict=True					,     _UpperCamelCase			:Dict=None					,     _UpperCamelCase			:Optional[Any]=True					,     _UpperCamelCase			:int=10					,     _UpperCamelCase			:int=["stage1", "stage2"]					,     _UpperCamelCase			:Dict=[1, 2]					,     )->      Union[str, Any]:
         __A      =       parent
         __A      =       batch_size
         __A      =       image_size
         __A      =       patch_size
         __A      =       num_channels
         __A      =       embed_dim
         __A      =       depths
         __A      =       num_heads
         __A      =       kernel_size
         __A      =       dilations
         __A      =       mlp_ratio
         __A      =       qkv_bias
         __A      =       hidden_dropout_prob
         __A      =       attention_probs_dropout_prob
         __A      =       drop_path_rate
         __A      =       hidden_act
         __A      =       patch_norm
         __A      =       layer_norm_eps
         __A      =       initializer_range
         __A      =       is_training
         __A      =       scope
         __A      =       use_labels
         __A      =       num_labels
         __A      =       out_features
         __A      =       out_indices




   def _lowerCAmelCase   (self			:int				)->      Tuple:
         __A      =       floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]				)

         __A      =       None
         if self.use_labels:
               __A      =       ids_tensor([self.batch_size]					,     self.num_labels				)

         __A      =       self.get_config()

         return config, pixel_values, labels




   def _lowerCAmelCase   (self			:int				)->      Union[str, Any]:
         return DinatConfig(
             num_labels=self.num_labels					,     image_size=self.image_size					,     patch_size=self.patch_size					,     num_channels=self.num_channels					,     embed_dim=self.embed_dim					,     depths=self.depths					,     num_heads=self.num_heads					,     kernel_size=self.kernel_size					,     dilations=self.dilations					,     mlp_ratio=self.mlp_ratio					,     qkv_bias=self.qkv_bias					,     hidden_dropout_prob=self.hidden_dropout_prob					,     attention_probs_dropout_prob=self.attention_probs_dropout_prob					,     drop_path_rate=self.drop_path_rate					,     hidden_act=self.hidden_act					,     patch_norm=self.patch_norm					,     layer_norm_eps=self.layer_norm_eps					,     initializer_range=self.initializer_range					,     out_features=self.out_features					,     out_indices=self.out_indices					,     )




   def _lowerCAmelCase   (self			:List[str]					,     _UpperCamelCase			:List[str]					,     _UpperCamelCase			:Optional[Any]					,     _UpperCamelCase			:Union[str, Any]				)->      Tuple:
         __A      =       DinatModel(config=_UpperCamelCase				)
         model.to(_UpperCamelCase				)
         model.eval()
         __A      =       model(_UpperCamelCase				)

         __A      =       __A      =       (config.image_size // config.patch_size) // (2 ** (len(config.depths				) - 1))
         __A      =       int(config.embed_dim * 2 ** (len(config.depths				) - 1)				)

         self.parent.assertEqual(
             result.last_hidden_state.shape					,     (self.batch_size, expected_height, expected_width, expected_dim)				)




   def _lowerCAmelCase   (self			:Tuple					,     _UpperCamelCase			:Optional[Any]					,     _UpperCamelCase			:Tuple					,     _UpperCamelCase			:Tuple				)->      List[Any]:
         __A      =       DinatForImageClassification(_UpperCamelCase				)
         model.to(_UpperCamelCase				)
         model.eval()
         __A      =       model(_UpperCamelCase					,     labels=_UpperCamelCase				)
         self.parent.assertEqual(result.logits.shape					,     (self.batch_size, self.num_labels)				)

         # test greyscale images
         __A      =       1
         __A      =       DinatForImageClassification(_UpperCamelCase				)
         model.to(_UpperCamelCase				)
         model.eval()

         __A      =       floats_tensor([self.batch_size, 1, self.image_size, self.image_size]				)
         __A      =       model(_UpperCamelCase				)
         self.parent.assertEqual(result.logits.shape					,     (self.batch_size, self.num_labels)				)




   def _lowerCAmelCase   (self			:List[Any]					,     _UpperCamelCase			:Optional[Any]					,     _UpperCamelCase			:int					,     _UpperCamelCase			:Union[str, Any]				)->      List[str]:
         __A      =       DinatBackbone(config=_UpperCamelCase				)
         model.to(_UpperCamelCase				)
         model.eval()
         __A      =       model(_UpperCamelCase				)

         # verify hidden states
         self.parent.assertEqual(len(result.feature_maps				)					,     len(config.out_features				)				)
         self.parent.assertListEqual(list(result.feature_maps[0].shape				)					,     [self.batch_size, model.channels[0], 16, 16]				)

         # verify channels
         self.parent.assertEqual(len(model.channels				)					,     len(config.out_features				)				)

         # verify backbone works with out_features=None
         __A      =       None
         __A      =       DinatBackbone(config=_UpperCamelCase				)
         model.to(_UpperCamelCase				)
         model.eval()
         __A      =       model(_UpperCamelCase				)

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

         # verify channels
         self.parent.assertEqual(len(model.channels				)					,     1				)






   def _lowerCAmelCase   (self			:Optional[Any]				)->      str:
         __A      =       self.prepare_config_and_inputs()
         __A     ,		__A     ,		__A      =       config_and_inputs
         __A      =       {'''pixel_values''': pixel_values}
         return config, inputs_dict





@require_natten
@require_torch
class A_      (    _lowerCamelCase				,   _lowerCamelCase				,   unittest.TestCase     ):
   lowerCAmelCase__       =				(
       (
           DinatModel,
           DinatForImageClassification,
           DinatBackbone,
       )
       if is_torch_available()
       else ()
   )
   lowerCAmelCase__       =				(
       {"""feature-extraction""": DinatModel, """image-classification""": DinatForImageClassification}
       if is_torch_available()
       else {}
   )
   lowerCAmelCase__       =				False

   lowerCAmelCase__       =				False
   lowerCAmelCase__       =				False
   lowerCAmelCase__       =				False
   lowerCAmelCase__       =				False




   def _lowerCAmelCase   (self			:Tuple				)->      str:
         __A      =       DinatModelTester(self				)
         __A      =       ConfigTester(self					,     config_class=_UpperCamelCase					,     embed_dim=37				)




   def _lowerCAmelCase   (self			:str				)->      List[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			:Optional[int]				)->      str:
         return




   def _lowerCAmelCase   (self			:Any				)->      List[Any]:
         __A      =       self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_model(*_UpperCamelCase				)




   def _lowerCAmelCase   (self			:int				)->      Optional[int]:
         __A      =       self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_image_classification(*_UpperCamelCase				)




   def _lowerCAmelCase   (self			:str				)->      Dict:
         __A      =       self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_backbone(*_UpperCamelCase				)




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




   @unittest.skip(reason='''Dinat does not use feedforward chunking'''				)
   def _lowerCAmelCase   (self			:Tuple				)->      int:
         pass




   def _lowerCAmelCase   (self			:Dict				)->      Union[str, Any]:
         __A     ,		__A      =       self.model_tester.prepare_config_and_inputs_for_common()

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




   def _lowerCAmelCase   (self			:Tuple				)->      List[Any]:
         __A     ,		__A      =       self.model_tester.prepare_config_and_inputs_for_common()

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

               __A      =       ['''pixel_values''']
               self.assertListEqual(arg_names[:1]					,     _UpperCamelCase				)




   def _lowerCAmelCase   (self			:Optional[Any]				)->      Tuple:
         self.skipTest('''Dinat\'s attention operation is handled entirely by NATTEN.'''				)




   def _lowerCAmelCase   (self			:Tuple					,     _UpperCamelCase			:List[Any]					,     _UpperCamelCase			:int					,     _UpperCamelCase			:Any					,     _UpperCamelCase			:Optional[Any]				)->      Dict:
         __A      =       model_class(_UpperCamelCase				)
         model.to(_UpperCamelCase				)
         model.eval()

         with torch.no_grad():
               __A      =       model(**self._prepare_for_class(_UpperCamelCase					,     _UpperCamelCase				)				)

         __A      =       outputs.hidden_states

         __A      =       getattr(
             self.model_tester					,     '''expected_num_hidden_layers'''					,     len(self.model_tester.depths				) + 1				)
         self.assertEqual(len(_UpperCamelCase				)					,     _UpperCamelCase				)

         # Dinat has a different seq_length
         __A      =       (
             config.patch_size
             if isinstance(config.patch_size					,     collections.abc.Iterable				)
             else (config.patch_size, config.patch_size)
         )

         __A      =       image_size[0] // patch_size[0]
         __A      =       image_size[1] // patch_size[1]

         self.assertListEqual(
             list(hidden_states[0].shape[-3:]				)					,     [height, width, self.model_tester.embed_dim]					,     )

         if model_class.__name__ != "DinatBackbone":
               __A      =       outputs.reshaped_hidden_states
               self.assertEqual(len(_UpperCamelCase				)					,     _UpperCamelCase				)

               __A     ,		__A     ,		__A     ,		__A      =       reshaped_hidden_states[0].shape
               __A      =       (
                   reshaped_hidden_states[0].view(_UpperCamelCase					,     _UpperCamelCase					,     _UpperCamelCase					,     _UpperCamelCase				).permute(0					,     2					,     3					,     1				)
               )
               self.assertListEqual(
                   list(reshaped_hidden_states.shape[-3:]				)					,     [height, width, self.model_tester.embed_dim]					,     )




   def _lowerCAmelCase   (self			:Tuple				)->      Union[str, Any]:
         __A     ,		__A      =       self.model_tester.prepare_config_and_inputs_for_common()

         __A      =       (
             self.model_tester.image_size
             if isinstance(self.model_tester.image_size					,     collections.abc.Iterable				)
             else (self.model_tester.image_size, self.model_tester.image_size)
         )

         for model_class in self.all_model_classes:
               __A      =       True
               self.check_hidden_states_output(_UpperCamelCase					,     _UpperCamelCase					,     _UpperCamelCase					,     _UpperCamelCase				)

               # check that output_hidden_states also work using config
               del inputs_dict["output_hidden_states"]
               __A      =       True

               self.check_hidden_states_output(_UpperCamelCase					,     _UpperCamelCase					,     _UpperCamelCase					,     _UpperCamelCase				)




   @slow
   def _lowerCAmelCase   (self			:List[str]				)->      Dict:
         for model_name in DINAT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
               __A      =       DinatModel.from_pretrained(_UpperCamelCase				)
               self.assertIsNotNone(_UpperCamelCase				)






   def _lowerCAmelCase   (self			:str				)->      int:
         __A     ,		__A      =       self.model_tester.prepare_config_and_inputs_for_common()

         __A      =       _config_zero_init(_UpperCamelCase				)
         for model_class in self.all_model_classes:
               __A      =       model_class(config=_UpperCamelCase				)
               for name, param in model.named_parameters():
                     if "embeddings" not in name and 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"""					,     )





@require_natten
@require_vision
@require_torch
class A_      (    unittest.TestCase     ):




   @cached_property
   def _lowerCAmelCase   (self			:Optional[Any]				)->      List[str]:
         return AutoImageProcessor.from_pretrained('''shi-labs/dinat-mini-in1k-224'''				) if is_vision_available() else None






   @slow
   def _lowerCAmelCase   (self			:int				)->      str:
         __A      =       DinatForImageClassification.from_pretrained('''shi-labs/dinat-mini-in1k-224'''				).to(_UpperCamelCase				)
         __A      =       self.default_image_processor

         __A      =       Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''				)
         __A      =       image_processor(images=_UpperCamelCase					,     return_tensors='''pt'''				).to(_UpperCamelCase				)

         # forward pass
         with torch.no_grad():
               __A      =       model(**_UpperCamelCase				)

         # verify the logits
         __A      =       torch.Size((1, 1000)				)
         self.assertEqual(outputs.logits.shape					,     _UpperCamelCase				)
         __A      =       torch.tensor([-0.1_5_4_5, -0.7_6_6_7, 0.4_6_4_2]				).to(_UpperCamelCase				)
         self.assertTrue(torch.allclose(outputs.logits[0, :3]					,     _UpperCamelCase					,     atol=1e-4				)				)





@require_torch
@require_natten
class A_      (    unittest.TestCase				,   _lowerCamelCase     ):
   lowerCAmelCase__       =				(DinatBackbone,) if is_torch_available() else ()
   lowerCAmelCase__       =				DinatConfig




   def _lowerCAmelCase   (self			:str				)->      Dict:
         __A      =       DinatModelTester(self				)












import argparse
import json
import re
import string
import sys

import numpy as np


snake_case__      :							Dict									=  0.5



def _a       (			lowerCamelCase:     List[str]      ,       lowerCamelCase:     List[str]				)			->		List[Any]:







      '''simple docstring'''






      __A      =       ['''.''', ''',''', ''';''', ''':''']

      for token in remove_tokens:
            __A      =       ground_truth.replace(lowerCamelCase      ,       ''''''				)
            __A      =       prediction.replace(lowerCamelCase      ,       ''''''				)

      __A     ,		__A      =       ground_truth.lower(), prediction.lower()
      __A     ,		__A      =       ground_truth.replace('''/'''      ,       ''' '''				), prediction.replace('''/'''      ,       ''' '''				)
      __A     ,		__A      =       set(ground_truth.split(''' '''				)				), set(prediction.split(''' '''				)				)

      __A      =       ground_truth.intersection(lowerCamelCase				)
      __A      =       ground_truth.union(lowerCamelCase				)
      __A      =       len(lowerCamelCase				) / len(lowerCamelCase				)
      return jaccard



def _a       (			lowerCamelCase:     List[str]				)			->		Dict:







      '''simple docstring'''






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

      def white_space_fix(lowerCamelCase:     Tuple				):
            return " ".join(text.split()				)

      def remove_punc(lowerCamelCase:     List[Any]				):
            __A      =       set(string.punctuation				)
            return "".join(ch for ch in text if ch not in exclude				)

      def lower(lowerCamelCase:     int				):
            return text.lower()

      return white_space_fix(remove_articles(remove_punc(lower(lowerCamelCase				)				)				)				)



def _a       (			lowerCamelCase:     Dict      ,       lowerCamelCase:     int      ,       lowerCamelCase:     List[str]				)			->		str:







      '''simple docstring'''






      __A     ,		__A     ,		__A      =       0, 0, 0

      __A      =       '''Parties''' in qa_id

      # first check if ground truth is empty
      if len(lowerCamelCase				) == 0:
            if len(lowerCamelCase				) > 0:
                  fp += len(lowerCamelCase				)  # false positive for each one
      else:
            for ground_truth in ground_truths:
                  assert len(lowerCamelCase				) > 0
                  # check if there is a match
                  __A      =       False
                  for pred in predictions:
                        if substr_ok:
                              __A      =       get_jaccard(lowerCamelCase      ,       lowerCamelCase				) >= IOU_THRESH or ground_truth in pred
                        else:
                              __A      =       get_jaccard(lowerCamelCase      ,       lowerCamelCase				) >= IOU_THRESH
                        if is_match:
                              __A      =       True

                  if match_found:
                        tp += 1
                  else:
                        fn += 1

        # now also get any fps by looping through preds
            for pred in predictions:
                  # Check if there's a match. if so, don't count (don't want to double count based on the above)
                  # but if there's no match, then this is a false positive.
                  # (Note: we get the true positives in the above loop instead of this loop so that we don't double count
                  # multiple predictions that are matched with the same answer.)
                  __A      =       False
                  for ground_truth in ground_truths:
                        assert len(lowerCamelCase				) > 0
                        if substr_ok:
                              __A      =       get_jaccard(lowerCamelCase      ,       lowerCamelCase				) >= IOU_THRESH or ground_truth in pred
                        else:
                              __A      =       get_jaccard(lowerCamelCase      ,       lowerCamelCase				) >= IOU_THRESH
                        if is_match:
                              __A      =       True

                  if not match_found:
                        fp += 1

      __A      =       tp / (tp + fp) if tp + fp > 0 else np.nan
      __A      =       tp / (tp + fn) if tp + fn > 0 else np.nan

      return precision, recall



def _a       (			lowerCamelCase:     Dict				)			->		List[str]:







      '''simple docstring'''






      __A      =       precisions[::-1]
      for i in range(1      ,       len(lowerCamelCase				)				):
            __A      =       max(precision_best[i - 1]      ,       precision_best[i]				)
      __A      =       precision_best[::-1]
      return precisions



def _a       (			lowerCamelCase:     Optional[int]      ,       lowerCamelCase:     Dict				)			->		Tuple:







      '''simple docstring'''






      __A      =       process_precisions(lowerCamelCase				)
      __A      =       np.trapz(lowerCamelCase      ,       lowerCamelCase				)
      if np.isnan(lowerCamelCase				):
            return 0
      return aupr



def _a       (			lowerCamelCase:     Any      ,       lowerCamelCase:     List[str]      ,       lowerCamelCase:     Optional[int]				)			->		str:







      '''simple docstring'''






      __A      =       process_precisions(lowerCamelCase				)
      __A      =       0
      for prec, recall in zip(lowerCamelCase      ,       lowerCamelCase				):
            if recall >= recall_thresh:
                  __A      =       prec
                  break
      return prec_at_recall



def _a       (			lowerCamelCase:     Tuple      ,       lowerCamelCase:     Dict				)			->		Optional[Any]:







      '''simple docstring'''






      return normalize_answer(lowerCamelCase				) == normalize_answer(lowerCamelCase				)



def _a       (			lowerCamelCase:     Any      ,       lowerCamelCase:     List[Any]      ,       lowerCamelCase:     Optional[int]				)			->		List[str]:







      '''simple docstring'''






      __A      =       0
      for pred in predictions:
            for ground_truth in ground_truths:
                  __A      =       metric_fn(lowerCamelCase      ,       lowerCamelCase				)
                  if score == 1:  # break the loop when one prediction matches the ground truth
                        break
            if score == 1:
                  break
      return score



def _a       (			lowerCamelCase:     Union[str, Any]      ,       lowerCamelCase:     int				)			->		int:







      '''simple docstring'''






      __A      =       __A      =       __A      =       0
      __A      =       []
      __A      =       []
      for article in dataset:
            for paragraph in article["paragraphs"]:
                  for qa in paragraph["qas"]:
                        total += 1
                        if qa["id"] not in predictions:
                              __A      =       '''Unanswered question ''' + qa['''id'''] + ''' will receive score 0.'''
                              print(lowerCamelCase      ,       file=sys.stderr				)
                              continue
                        __A      =       [x['''text'''] for x in qa['''answers''']]
                        __A      =       predictions[qa['''id''']]
                        __A     ,		__A      =       compute_precision_recall(lowerCamelCase      ,       lowerCamelCase      ,       qa['''id''']				)

                        precisions.append(lowerCamelCase				)
                        recalls.append(lowerCamelCase				)

                        if precision == 0 and recall == 0:
                              fa += 0
                        else:
                              fa += 2 * (precision * recall) / (precision + recall)

                        exact_match += metric_max_over_ground_truths(lowerCamelCase      ,       lowerCamelCase      ,       lowerCamelCase				)

      __A      =       [x for _, x in sorted(zip(lowerCamelCase      ,       lowerCamelCase				)				)]
      recalls.sort()

      __A      =       100.0 * fa / total
      __A      =       100.0 * exact_match / total
      __A      =       get_aupr(lowerCamelCase      ,       lowerCamelCase				)

      __A      =       get_prec_at_recall(lowerCamelCase      ,       lowerCamelCase      ,       recall_thresh=0.9				)
      __A      =       get_prec_at_recall(lowerCamelCase      ,       lowerCamelCase      ,       recall_thresh=0.8				)

      return {
          "exact_match": exact_match,
          "f1": fa,
          "aupr": aupr,
          "prec_at_80_recall": prec_at_aa_recall,
          "prec_at_90_recall": prec_at_aa_recall,
      }


if __name__ == "__main__":
       snake_case__      :							Dict									=  argparse.ArgumentParser(description='Evaluation for CUAD')
       parser.add_argument('dataset_file', help='Dataset file')
       parser.add_argument('prediction_file', help='Prediction File')
       snake_case__      :							List[str]									=  parser.parse_args()
       with open(args.dataset_file) as dataset_file:
              snake_case__      :							List[Any]									=  json.load(dataset_file)
              snake_case__      :							int									=  dataset_json['data']
       with open(args.prediction_file) as prediction_file:
              snake_case__      :							Union[str, Any]									=  json.load(prediction_file)
       print(json.dumps(evaluate(dataset, predictions)))





