






import unittest

import numpy as np

from transformers import BertConfig, is_flax_available
from transformers.testing_utils import require_flax, slow

from ...test_modeling_flax_common import FlaxModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask


if is_flax_available():
   from transformers.models.bert.modeling_flax_bert import (
       FlaxBertForMaskedLM,
       FlaxBertForMultipleChoice,
       FlaxBertForNextSentencePrediction,
       FlaxBertForPreTraining,
       FlaxBertForQuestionAnswering,
       FlaxBertForSequenceClassification,
       FlaxBertForTokenClassification,
       FlaxBertModel,
   )




class 							_lowerCAmelCase(			unittest.TestCase   ):





       """simple docstring"""







       def __init__(   self    ,     _lowerCamelCase    ,     _lowerCamelCase=1_3    ,     _lowerCamelCase=7    ,     _lowerCamelCase=True    ,     _lowerCamelCase=True    ,     _lowerCamelCase=True    ,     _lowerCamelCase=True    ,     _lowerCamelCase=9_9    ,     _lowerCamelCase=3_2    ,     _lowerCamelCase=5    ,     _lowerCamelCase=4    ,     _lowerCamelCase=3_7    ,     _lowerCamelCase="gelu"    ,     _lowerCamelCase=0.1    ,     _lowerCamelCase=0.1    ,     _lowerCamelCase=5_1_2    ,     _lowerCamelCase=1_6    ,     _lowerCamelCase=2    ,     _lowerCamelCase=0.0_2    ,     _lowerCamelCase=4    ,     ):
         UpperCamelCase_:   int        =							parent
         UpperCamelCase_:   Any        =							batch_size
         UpperCamelCase_:   Tuple        =							seq_length
         UpperCamelCase_:   int        =							is_training
         UpperCamelCase_:   Dict        =							use_attention_mask
         UpperCamelCase_:   Optional[int]        =							use_token_type_ids
         UpperCamelCase_:   Optional[Any]        =							use_labels
         UpperCamelCase_:   Union[str, Any]        =							vocab_size
         UpperCamelCase_:   Any        =							hidden_size
         UpperCamelCase_:   Optional[int]        =							num_hidden_layers
         UpperCamelCase_:   List[Any]        =							num_attention_heads
         UpperCamelCase_:   Tuple        =							intermediate_size
         UpperCamelCase_:   str        =							hidden_act
         UpperCamelCase_:   Optional[Any]        =							hidden_dropout_prob
         UpperCamelCase_:   Optional[Any]        =							attention_probs_dropout_prob
         UpperCamelCase_:   Any        =							max_position_embeddings
         UpperCamelCase_:   Optional[Any]        =							type_vocab_size
         UpperCamelCase_:   Dict        =							type_sequence_label_size
         UpperCamelCase_:   List[Any]        =							initializer_range
         UpperCamelCase_:   List[str]        =							num_choices





       def      _a       (   self				):
         UpperCamelCase_:   str        =							ids_tensor([self.batch_size, self.seq_length]    ,     self.vocab_size				)

         UpperCamelCase_:   str        =							None
         if self.use_attention_mask:
           UpperCamelCase_:   str        =							random_attention_mask([self.batch_size, self.seq_length]				)

         UpperCamelCase_:   str        =							None
         if self.use_token_type_ids:
           UpperCamelCase_:   Dict        =							ids_tensor([self.batch_size, self.seq_length]    ,     self.type_vocab_size				)

         UpperCamelCase_:   str        =							BertConfig(
             vocab_size=self.vocab_size    ,     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    ,     max_position_embeddings=self.max_position_embeddings    ,     type_vocab_size=self.type_vocab_size    ,     is_decoder=_lowerCamelCase    ,     initializer_range=self.initializer_range    ,     )

         return config, input_ids, token_type_ids, attention_mask





       def      _a       (   self				):
         UpperCamelCase_:   str        =							self.prepare_config_and_inputs()
         UpperCamelCase_ ,UpperCamelCase_ ,UpperCamelCase_ ,UpperCamelCase_:   Union[str, Any]        =							config_and_inputs
         UpperCamelCase_:   Union[str, Any]        =							{'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': attention_mask}
         return config, inputs_dict





       def      _a       (   self				):
         UpperCamelCase_:   Optional[int]        =							self.prepare_config_and_inputs()
         UpperCamelCase_ ,UpperCamelCase_ ,UpperCamelCase_ ,UpperCamelCase_:   List[Any]        =							config_and_inputs

         UpperCamelCase_:   Dict        =							True
         UpperCamelCase_:   Union[str, Any]        =							floats_tensor([self.batch_size, self.seq_length, self.hidden_size]				)
         UpperCamelCase_:   Union[str, Any]        =							ids_tensor([self.batch_size, self.seq_length]    ,     vocab_size=2				)

         return (
             config,
             input_ids,
             attention_mask,
             encoder_hidden_states,
             encoder_attention_mask,
         )







@require_flax
class 							_lowerCAmelCase(			UpperCAmelCase_				,      unittest.TestCase   ):





       """simple docstring"""







       a							:						Any										=True

       a							:						Tuple										=(
           (
               FlaxBertModel,
               FlaxBertForPreTraining,
               FlaxBertForMaskedLM,
               FlaxBertForMultipleChoice,
               FlaxBertForQuestionAnswering,
               FlaxBertForNextSentencePrediction,
               FlaxBertForSequenceClassification,
               FlaxBertForTokenClassification,
               FlaxBertForQuestionAnswering,
           )
           if is_flax_available()
           else ()
       )





       def      _a       (   self				):
         UpperCamelCase_:   Union[str, Any]        =							FlaxBertModelTester(self				)





       @slow
       def      _a       (   self				):
         UpperCamelCase_:   str        =							FlaxBertModel.from_pretrained('bert-base-cased'				)
         UpperCamelCase_:   Union[str, Any]        =							model(np.ones((1, 1)				)				)
         self.assertIsNotNone(_lowerCamelCase				)



import re
import string

import numpy as np

import datasets


__UpperCamelCase     		=							'\nReturns the rate at which the input predicted strings exactly match their references, ignoring any strings input as part of the regexes_to_ignore list.\n'

__UpperCamelCase     		=							'\nArgs:\n    predictions: List of predicted texts.\n    references: List of reference texts.\n    regexes_to_ignore: List, defaults to None. Regex expressions of characters to\n        ignore when calculating the exact matches. Note: these regexes are removed\n        from the input data before the changes based on the options below (e.g. ignore_case,\n        ignore_punctuation, ignore_numbers) are applied.\n    ignore_case: Boolean, defaults to False. If true, turns everything\n        to lowercase so that capitalization differences are ignored.\n    ignore_punctuation: Boolean, defaults to False. If true, removes all punctuation before\n        comparing predictions and references.\n    ignore_numbers: Boolean, defaults to False. If true, removes all punctuation before\n        comparing predictions and references.\nReturns:\n    exact_match: Dictionary containing exact_match rate. Possible values are between 0.0 and 100.0, inclusive.\nExamples:\n    >>> exact_match = datasets.load_metric("exact_match")\n    >>> refs = ["the cat", "theater", "YELLING", "agent007"]\n    >>> preds = ["cat?", "theater", "yelling", "agent"]\n    >>> results = exact_match.compute(references=refs, predictions=preds)\n    >>> print(round(results["exact_match"], 1))\n    25.0\n\n    >>> exact_match = datasets.load_metric("exact_match")\n    >>> refs = ["the cat", "theater", "YELLING", "agent007"]\n    >>> preds = ["cat?", "theater", "yelling", "agent"]\n    >>> results = exact_match.compute(references=refs, predictions=preds, regexes_to_ignore=["the ", "yell"], ignore_case=True, ignore_punctuation=True)\n    >>> print(round(results["exact_match"], 1))\n    50.0\n\n\n    >>> exact_match = datasets.load_metric("exact_match")\n    >>> refs = ["the cat", "theater", "YELLING", "agent007"]\n    >>> preds = ["cat?", "theater", "yelling", "agent"]\n    >>> results = exact_match.compute(references=refs, predictions=preds, regexes_to_ignore=["the ", "yell", "YELL"], ignore_case=True, ignore_punctuation=True)\n    >>> print(round(results["exact_match"], 1))\n    75.0\n\n    >>> exact_match = datasets.load_metric("exact_match")\n    >>> refs = ["the cat", "theater", "YELLING", "agent007"]\n    >>> preds = ["cat?", "theater", "yelling", "agent"]\n    >>> results = exact_match.compute(references=refs, predictions=preds, regexes_to_ignore=["the ", "yell", "YELL"], ignore_case=True, ignore_punctuation=True, ignore_numbers=True)\n    >>> print(round(results["exact_match"], 1))\n    100.0\n\n    >>> exact_match = datasets.load_metric("exact_match")\n    >>> refs = ["The cat sat on the mat.", "Theaters are great.", "It\'s like comparing oranges and apples."]\n    >>> preds = ["The cat sat on the mat?", "Theaters are great.", "It\'s like comparing apples and oranges."]\n    >>> results = exact_match.compute(references=refs, predictions=preds)\n    >>> print(round(results["exact_match"], 1))\n    33.3\n\n'

__UpperCamelCase     		=							'\n'







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






			"""simple docstring"""






			def 					snake_case__					(				self  ):

						'''simple docstring'''


						return datasets.MetricInfo(
						    description=_DESCRIPTION   ,    citation=_CITATION   ,    inputs_description=_KWARGS_DESCRIPTION   ,    features=datasets.Features(
						        {
						            "predictions": datasets.Value("string"   ,    id="sequence"  ),
						            "references": datasets.Value("string"   ,    id="sequence"  ),
						        }  )   ,    reference_urls=[]   ,    )







			def 					snake_case__					(				self   ,    snake_case   ,    snake_case   ,    snake_case=None   ,    snake_case=False   ,    snake_case=False   ,    snake_case=False   ,    ):

						'''simple docstring'''


						if regexes_to_ignore is not None:
									for s in regexes_to_ignore:
												UpperCamelCase__       =							np.array([re.sub(snake_case   ,    ""   ,    snake_case  ) for x in predictions]  )
												UpperCamelCase__       =							np.array([re.sub(snake_case   ,    ""   ,    snake_case  ) for x in references]  )
						else:
									UpperCamelCase__       =							np.asarray(snake_case  )
									UpperCamelCase__       =							np.asarray(snake_case  )

						if ignore_case:
									UpperCamelCase__       =							np.char.lower(snake_case  )
									UpperCamelCase__       =							np.char.lower(snake_case  )

						if ignore_punctuation:
									UpperCamelCase__       =							string.punctuation.maketrans(""   ,    ""   ,    string.punctuation  )
									UpperCamelCase__       =							np.char.translate(snake_case   ,    table=snake_case  )
									UpperCamelCase__       =							np.char.translate(snake_case   ,    table=snake_case  )

						if ignore_numbers:
									UpperCamelCase__       =							string.digits.maketrans(""   ,    ""   ,    string.digits  )
									UpperCamelCase__       =							np.char.translate(snake_case   ,    table=snake_case  )
									UpperCamelCase__       =							np.char.translate(snake_case   ,    table=snake_case  )

						UpperCamelCase__       =							predictions == references

						return {"exact_match": np.mean(snake_case  ) * 100}


