





"""simple docstring"""


from __future__ import annotations

import inspect
import unittest
from typing import List, Tuple

from transformers import RegNetConfig
from transformers.testing_utils import require_tf, require_vision, slow
from transformers.utils import cached_property, is_tf_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
		import tensorflow as tf

		from transformers import TF_REGNET_PRETRAINED_MODEL_ARCHIVE_LIST, TFRegNetForImageClassification, TFRegNetModel


if is_vision_available():
		from PIL import Image

		from transformers import AutoImageProcessor






class 							lowercase__			:

			"""simple docstring"""







			def __init__(      self						,   _A						,   _A=3						,   _A=3_2						,   _A=3						,   _A=1_0						,   _A=[1_0, 2_0, 3_0, 4_0]						,   _A=[1, 1, 2, 1]						,   _A=True						,   _A=True						,   _A="relu"						,   _A=3						,   _A=None						,   ):






									'''simple docstring'''







									UpperCamelCase				:		List[str]				   =			parent
									UpperCamelCase				:		Tuple				   =			batch_size
									UpperCamelCase				:		Any				   =			image_size
									UpperCamelCase				:		List[str]				   =			num_channels
									UpperCamelCase				:		str				   =			embeddings_size
									UpperCamelCase				:		Union[str, Any]				   =			hidden_sizes
									UpperCamelCase				:		str				   =			depths
									UpperCamelCase				:		Dict				   =			is_training
									UpperCamelCase				:		Any				   =			use_labels
									UpperCamelCase				:		Any				   =			hidden_act
									UpperCamelCase				:		Tuple				   =			num_labels
									UpperCamelCase				:		Any				   =			scope
									UpperCamelCase				:		Union[str, Any]				   =			len(_A    )







			def   _a      (      self    ):






									'''simple docstring'''







									UpperCamelCase				:		List[Any]				   =			floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]    )

									UpperCamelCase				:		Tuple				   =			None
									if self.use_labels:
															UpperCamelCase				:		int				   =			ids_tensor([self.batch_size]						,   self.num_labels    )

									UpperCamelCase				:		Tuple				   =			self.get_config()
									return config, pixel_values, labels







			def   _a      (      self    ):






									'''simple docstring'''







									return RegNetConfig(
									    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						,   )







			def   _a      (      self						,   _A						,   _A						,   _A    ):






									'''simple docstring'''







									UpperCamelCase				:		Tuple				   =			TFRegNetModel(config=_A    )
									UpperCamelCase				:		Optional[Any]				   =			model(_A						,   training=_A    )
									# expected last hidden states: B, C, H // 32, W // 32
									self.parent.assertEqual(
									    result.last_hidden_state.shape						,   (self.batch_size, self.hidden_sizes[-1], self.image_size // 3_2, self.image_size // 3_2)						,   )







			def   _a      (      self						,   _A						,   _A						,   _A    ):






									'''simple docstring'''







									UpperCamelCase				:		Tuple				   =			self.num_labels
									UpperCamelCase				:		Any				   =			TFRegNetForImageClassification(_A    )
									UpperCamelCase				:		Any				   =			model(_A						,   labels=_A						,   training=_A    )
									self.parent.assertEqual(result.logits.shape						,   (self.batch_size, self.num_labels)    )







			def   _a      (      self    ):






									'''simple docstring'''







									UpperCamelCase				:		List[str]				   =			self.prepare_config_and_inputs()
									UpperCamelCase   ,						UpperCamelCase   ,						UpperCamelCase				:		List[str]				   =			config_and_inputs
									UpperCamelCase				:		int				   =			{"""pixel_values""": pixel_values}
									return config, inputs_dict






@require_tf
class 							lowercase__			(   __SCREAMING_SNAKE_CASE					,     __SCREAMING_SNAKE_CASE					,     unittest.TestCase	):

			"""simple docstring"""
			__lowerCAmelCase     :				int										=  (TFRegNetModel, TFRegNetForImageClassification) if is_tf_available() else ()
			__lowerCAmelCase     :				Tuple										=  (
			    {"""feature-extraction""": TFRegNetModel, """image-classification""": TFRegNetForImageClassification}
			    if is_tf_available()
			    else {}
			)

			__lowerCAmelCase     :				int										=  False
			__lowerCAmelCase     :				Optional[int]										=  False
			__lowerCAmelCase     :				List[Any]										=  False
			__lowerCAmelCase     :				Union[str, Any]										=  False
			__lowerCAmelCase     :				str										=  False







			def   _a      (      self    ):






									'''simple docstring'''







									UpperCamelCase				:		Union[str, Any]				   =			TFRegNetModelTester(self    )
									UpperCamelCase				:		Union[str, Any]				   =			ConfigTester(self						,   config_class=_A						,   has_text_modality=_A    )







			def   _a      (      self    ):






									'''simple docstring'''







									return







			@unittest.skip(reason="""RegNet does not use inputs_embeds"""    )
			def   _a      (      self    ):






									'''simple docstring'''







									pass







			@unittest.skipIf(
			    not is_tf_available() or len(tf.config.list_physical_devices("""GPU"""    )    ) == 0						,   reason="""TF does not support backprop for grouped convolutions on CPU."""						,   )
			@slow
			def   _a      (      self    ):






									'''simple docstring'''







									super().test_keras_fit()







			@unittest.skip(reason="""RegNet does not support input and output embeddings"""    )
			def   _a      (      self    ):






									'''simple docstring'''







									pass







			def   _a      (      self    ):






									'''simple docstring'''







									UpperCamelCase   ,						UpperCamelCase				:		List[str]				   =			self.model_tester.prepare_config_and_inputs_for_common()

									for model_class in self.all_model_classes:
															UpperCamelCase				:		List[str]				   =			model_class(_A    )
															UpperCamelCase				:		Any				   =			inspect.signature(model.call    )
															# signature.parameters is an OrderedDict => so arg_names order is deterministic
															UpperCamelCase				:		Optional[int]				   =			[*signature.parameters.keys()]

															UpperCamelCase				:		Any				   =			["""pixel_values"""]
															self.assertListEqual(arg_names[:1]						,   _A    )







			def   _a      (      self    ):






									'''simple docstring'''







									UpperCamelCase				:		Union[str, Any]				   =			self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_model(*_A    )







			def   _a      (      self    ):






									'''simple docstring'''







									def check_hidden_states_output(_A						,   _A						,   _A    ):
															UpperCamelCase				:		int				   =			model_class(_A    )
															UpperCamelCase				:		Dict				   =			model(**self._prepare_for_class(_A						,   _A    )						,   training=_A    )

															UpperCamelCase				:		Optional[Any]				   =			outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

															UpperCamelCase				:		Dict				   =			self.model_tester.num_stages
															self.assertEqual(len(_A    )						,   expected_num_stages + 1    )

															# RegNet'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 // 2, self.model_tester.image_size // 2]						,   )

									UpperCamelCase   ,						UpperCamelCase				:		List[Any]				   =			self.model_tester.prepare_config_and_inputs_for_common()
									UpperCamelCase				:		Dict				   =			["""basic""", """bottleneck"""]
									for model_class in self.all_model_classes:
															for layer_type in layers_type:
																					UpperCamelCase				:		Tuple				   =			layer_type
																					UpperCamelCase				:		Dict				   =			True
																					check_hidden_states_output(_A						,   _A						,   _A    )

																					# check that output_hidden_states also work using config
																					del inputs_dict["output_hidden_states"]
																					UpperCamelCase				:		Optional[Any]				   =			True

																					check_hidden_states_output(_A						,   _A						,   _A    )







			def   _a      (      self    ):






									'''simple docstring'''







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

									def check_equivalence(_A						,   _A						,   _A						,   _A={}    ):
															UpperCamelCase				:		Tuple				   =			model(_A						,   return_dict=_A						,   **_A    )
															UpperCamelCase				:		int				   =			model(_A						,   return_dict=_A						,   **_A    ).to_tuple()

															def recursive_check(_A						,   _A    ):
																					if isinstance(_A						,   (List, Tuple)    ):
																											for tuple_iterable_value, dict_iterable_value in zip(_A						,   _A    ):
																																	recursive_check(_A						,   _A    )
																					elif tuple_object is None:
																											return
																					else:
																											self.assertTrue(
																											    all(tf.equal(_A						,   _A    )    )						,   msg=(
																											        """Tuple and dict output are not equal. Difference:"""
																											        f""" {tf.math.reduce_max(tf.abs(tuple_object - dict_object    )    )}"""
																											    )						,   )

																					recursive_check(_A						,   _A    )

									for model_class in self.all_model_classes:
															UpperCamelCase				:		Any				   =			model_class(_A    )

															UpperCamelCase				:		Union[str, Any]				   =			self._prepare_for_class(_A						,   _A    )
															UpperCamelCase				:		str				   =			self._prepare_for_class(_A						,   _A    )
															check_equivalence(_A						,   _A						,   _A    )

															UpperCamelCase				:		Union[str, Any]				   =			self._prepare_for_class(_A						,   _A						,   return_labels=_A    )
															UpperCamelCase				:		Any				   =			self._prepare_for_class(_A						,   _A						,   return_labels=_A    )
															check_equivalence(_A						,   _A						,   _A    )

															UpperCamelCase				:		Any				   =			self._prepare_for_class(_A						,   _A    )
															UpperCamelCase				:		str				   =			self._prepare_for_class(_A						,   _A    )
															check_equivalence(_A						,   _A						,   _A						,   {"""output_hidden_states""": True}    )

															UpperCamelCase				:		List[Any]				   =			self._prepare_for_class(_A						,   _A						,   return_labels=_A    )
															UpperCamelCase				:		Optional[Any]				   =			self._prepare_for_class(_A						,   _A						,   return_labels=_A    )
															check_equivalence(_A						,   _A						,   _A						,   {"""output_hidden_states""": True}    )







			def   _a      (      self    ):






									'''simple docstring'''







									UpperCamelCase				:		Dict				   =			self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_image_classification(*_A    )







			@slow
			def   _a      (      self    ):






									'''simple docstring'''







									for model_name in TF_REGNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
															UpperCamelCase				:		int				   =			TFRegNetModel.from_pretrained(_A    )
															self.assertIsNotNone(_A    )


def      UpperCamelCase							():
						UpperCamelCase				:		Any				   =			Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""     )
						return image






@require_tf
@require_vision
class 							lowercase__			(   unittest.TestCase	):

			"""simple docstring"""







			@cached_property
			def   _a      (      self    ):






									'''simple docstring'''







									return (
									    AutoImageProcessor.from_pretrained(TF_REGNET_PRETRAINED_MODEL_ARCHIVE_LIST[0]    )
									    if is_vision_available()
									    else None
									)







			@slow
			def   _a      (      self    ):






									'''simple docstring'''







									UpperCamelCase				:		Any				   =			TFRegNetForImageClassification.from_pretrained(TF_REGNET_PRETRAINED_MODEL_ARCHIVE_LIST[0]    )

									UpperCamelCase				:		List[Any]				   =			self.default_image_processor
									UpperCamelCase				:		List[Any]				   =			prepare_img()
									UpperCamelCase				:		Any				   =			image_processor(images=_A						,   return_tensors="""tf"""    )

									# forward pass
									UpperCamelCase				:		Optional[int]				   =			model(**_A						,   training=_A    )

									# verify the logits
									UpperCamelCase				:		List[Any]				   =			tf.TensorShape((1, 1_0_0_0)    )
									self.assertEqual(outputs.logits.shape						,   _A    )

									UpperCamelCase				:		int				   =			tf.constant([-0.41_80, -1.50_51, -3.48_36]    )

									tf.debugging.assert_near(outputs.logits[0, :3]						,   _A						,   atol=1e-4    )




"""simple docstring"""



import datasets

from .evaluate import evaluate


_UpperCamelCase								=		"\\n@article{hendrycks2021cuad,\n      title={CUAD: An Expert-Annotated NLP Dataset for Legal Contract Review},\n      author={Dan Hendrycks and Collin Burns and Anya Chen and Spencer Ball},\n      journal={arXiv preprint arXiv:2103.06268},\n      year={2021}\n}\n"

_UpperCamelCase								=		"\nThis metric wrap the official scoring script for version 1 of the Contract\nUnderstanding Atticus Dataset (CUAD).\nContract Understanding Atticus Dataset (CUAD) v1 is a corpus of more than 13,000 labels in 510\ncommercial legal contracts that have been manually labeled to identify 41 categories of important\nclauses that lawyers look for when reviewing contracts in connection with corporate transactions.\n"

_UpperCamelCase								=		"\nComputes CUAD scores (EM, F1, AUPR, Precision@80%Recall, and Precision@90%Recall).\nArgs:\n    predictions: List of question-answers dictionaries with the following key-values:\n        - 'id': id of the question-answer pair as given in the references (see below)\n        - 'prediction_text': list of possible texts for the answer, as a list of strings\n        depending on a threshold on the confidence probability of each prediction.\n    references: List of question-answers dictionaries with the following key-values:\n        - 'id': id of the question-answer pair (see above),\n        - 'answers': a Dict in the CUAD dataset format\n            {\n                'text': list of possible texts for the answer, as a list of strings\n                'answer_start': list of start positions for the answer, as a list of ints\n            }\n            Note that answer_start values are not taken into account to compute the metric.\nReturns:\n    'exact_match': Exact match (the normalized answer exactly match the gold answer)\n    'f1': The F-score of predicted tokens versus the gold answer\n    'aupr': Area Under the Precision-Recall curve\n    'prec_at_80_recall': Precision at 80% recall\n    'prec_at_90_recall': Precision at 90% recall\nExamples:\n    >>> predictions = [{'prediction_text': ['The seller:', 'The buyer/End-User: Shenzhen LOHAS Supply Chain Management Co., Ltd.'], 'id': 'LohaCompanyltd_20191209_F-1_EX-10.16_11917878_EX-10.16_Supply Agreement__Parties'}]\n    >>> references = [{'answers': {'answer_start': [143, 49], 'text': ['The seller:', 'The buyer/End-User: Shenzhen LOHAS Supply Chain Management Co., Ltd.']}, 'id': 'LohaCompanyltd_20191209_F-1_EX-10.16_11917878_EX-10.16_Supply Agreement__Parties'}]\n    >>> cuad_metric = datasets.load_metric(\"cuad\")\n    >>> results = cuad_metric.compute(predictions=predictions, references=references)\n    >>> print(results)\n    {'exact_match': 100.0, 'f1': 100.0, 'aupr': 0.0, 'prec_at_80_recall': 1.0, 'prec_at_90_recall': 1.0}\n"
@datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION							,   _KWARGS_DESCRIPTION     )
class 						__UpperCAmelCase    (datasets.Metric     ):







			'''simple docstring'''





			def 		lowerCamelCase					(			self							):







										'''simple docstring'''


										return datasets.MetricInfo(
										    description=_DESCRIPTION   ,      citation=_CITATION   ,      inputs_description=_KWARGS_DESCRIPTION   ,      features=datasets.Features(
										        {
										            """predictions""": {
										                """id""": datasets.Value("""string"""							),
										                """prediction_text""": datasets.features.Sequence(datasets.Value("""string"""							)							),
										            },
										            """references""": {
										                """id""": datasets.Value("""string"""							),
										                """answers""": datasets.features.Sequence(
										                    {
										                        """text""": datasets.Value("""string"""							),
										                        """answer_start""": datasets.Value("""int32"""							),
										                    }							),
										            },
										        }							)   ,      codebase_urls=["""https://www.atticusprojectai.org/cuad"""]   ,      reference_urls=["""https://www.atticusprojectai.org/cuad"""]   ,      )





			def 		lowerCamelCase					(			self   ,      snake_case_   ,      snake_case_							):







										'''simple docstring'''


										A__       :     str								=			{prediction["""id"""]: prediction["""prediction_text"""] for prediction in predictions}
										A__       :     Optional[Any]								=			[
										    {
										        """paragraphs""": [
										            {
										                """qas""": [
										                    {
										                        """answers""": [{"""text""": answer_text} for answer_text in ref["""answers"""]["""text"""]],
										                        """id""": ref["""id"""],
										                    }
										                    for ref in references
										                ]
										            }
										        ]
										    }
										]
										A__       :     Optional[Any]								=			evaluate(dataset=snake_case_   ,      predictions=snake_case_							)
										return score





