






"""simple docstring"""





from dataclasses import dataclass, field
from typing import Tuple

from ..utils import cached_property, is_torch_available, is_torch_tpu_available, logging, requires_backends
from .benchmark_args_utils import BenchmarkArguments


if is_torch_available():
  import torch

if is_torch_tpu_available(check_device=False):
  import torch_xla.core.xla_model as xm


A__					: Tuple=	logging.get_logger(__name__)
@dataclass
class    __lowerCamelCase    (  _a  ):
   a      :				Optional[Any] 						=[
       """no_inference""",
       """no_cuda""",
       """no_tpu""",
       """no_speed""",
       """no_memory""",
       """no_env_print""",
       """no_multi_process""",
   ]

   def __init__(					self ,				**snake_case_      )		->       str:
       for deprecated_arg in self.deprecated_args:
           if deprecated_arg in kwargs:
               UpperCamelCase__         =    deprecated_arg[3:]
               setattr(self ,				snake_case_ ,				not kwargs.pop(snake_case_      )      )
               logger.warning(
                   F'{deprecated_arg} is depreciated. Please use --no_{positive_arg} or'
                   F' {positive_arg}={kwargs[positive_arg]}'      )

       UpperCamelCase__         =    kwargs.pop('torchscript' ,				self.torchscript      )
       UpperCamelCase__         =    kwargs.pop('torch_xla_tpu_print_metrics' ,				self.torch_xla_tpu_print_metrics      )
       UpperCamelCase__         =    kwargs.pop('fp16_opt_level' ,				self.fpaa_opt_level      )
       super().__init__(**snake_case_      )

   a      :				bool 						=field(default=_a							,	metadata={"""help""": """Trace the models using torchscript"""}  )
   a      :				bool 						=field(default=_a							,	metadata={"""help""": """Print Xla/PyTorch tpu metrics"""}  )
   a      :				str 						=field(
       default="""O1"""							,	metadata={
           """help""": (
               """For fp16: Apex AMP optimization level selected in ['O0', 'O1', 'O2', and 'O3']. """
               """See details at https://nvidia.github.io/apex/amp.html"""
           )
       }							,	)

   @cached_property
   def 						SCREAMING_SNAKE_CASE__ (					self      )		->       Tuple["torch.device", int]:
       requires_backends(self ,				['torch']      )
       logger.info('PyTorch: setting up devices'      )
       if not self.cuda:
           UpperCamelCase__         =    torch.device('cpu'      )
           UpperCamelCase__         =    0
       elif is_torch_tpu_available():
           UpperCamelCase__         =    xm.xla_device()
           UpperCamelCase__         =    0
       else:
           UpperCamelCase__         =    torch.device('cuda' if torch.cuda.is_available() else 'cpu'      )
           UpperCamelCase__         =    torch.cuda.device_count()
       return device, n_gpu

   @property
   def 						SCREAMING_SNAKE_CASE__ (					self      )		->       Tuple:
       return is_torch_tpu_available() and self.tpu

   @property
   def 						SCREAMING_SNAKE_CASE__ (					self      )		->       int:
       requires_backends(self ,				['torch']      )
       # TODO(PVP): currently only single GPU is supported
       return torch.cuda.current_device()

   @property
   def 						SCREAMING_SNAKE_CASE__ (					self      )		->       "torch.device":
       requires_backends(self ,				['torch']      )
       return self._setup_devices[0]

   @property
   def 						SCREAMING_SNAKE_CASE__ (					self      )		->       int:
       requires_backends(self ,				['torch']      )
       return self._setup_devices[1]







   @property
   def 						SCREAMING_SNAKE_CASE__ (					self      )		->       Dict:
       return self.n_gpu > 0












"""simple docstring"""





import unittest
from pathlib import Path
from tempfile import TemporaryDirectory

from transformers import AutoConfig, TFAutoModel, is_tensorflow_text_available, is_tf_available
from transformers.models.bert.tokenization_bert import BertTokenizer
from transformers.testing_utils import require_tensorflow_text, require_tf, slow


if is_tf_available():
  import tensorflow as tf

if is_tensorflow_text_available():
  from transformers.models.bert import TFBertTokenizer


A__					: List[Any]=	["""bert-base-uncased""", """bert-base-cased"""]
A__					: Optional[int]=	"""hf-internal-testing/tiny-bert-tf-only"""

if is_tf_available():
  class    __lowerCamelCase    (  tf.keras.Model  ):

     def __init__(					self ,				snake_case_      )		->       Optional[int]:
         super().__init__()
         UpperCamelCase__         =    tokenizer
         UpperCamelCase__         =    AutoConfig.from_pretrained(snake_case_      )
         UpperCamelCase__         =    TFAutoModel.from_config(snake_case_      )







     def 						SCREAMING_SNAKE_CASE__ (					self ,				snake_case_      )		->       int:
         UpperCamelCase__         =    self.tokenizer(snake_case_      )
         UpperCamelCase__         =    self.bert(**snake_case_      )
         return out["pooler_output"]
@require_tf
@require_tensorflow_text
class    __lowerCamelCase    (  unittest.TestCase  ):

   def 						SCREAMING_SNAKE_CASE__ (					self      )		->       str:
       super().setUp()

       UpperCamelCase__         =    [
           BertTokenizer.from_pretrained(snake_case_      ) for checkpoint in (TOKENIZER_CHECKPOINTS * 2)
       ]  # repeat for when fast_bert_tokenizer=false
       UpperCamelCase__         =    [TFBertTokenizer.from_pretrained(snake_case_      ) for checkpoint in TOKENIZER_CHECKPOINTS] + [
           TFBertTokenizer.from_pretrained(snake_case_ ,				use_fast_bert_tokenizer=snake_case_      )
           for checkpoint in TOKENIZER_CHECKPOINTS
       ]
       assert len(self.tokenizers      ) == len(self.tf_tokenizers      )

       UpperCamelCase__         =    [
           'This is a straightforward English test sentence.',
           'This one has some weird characters\rto\nsee\r\nif  those\u00E9break things.',
           'Now we\'re going to add some Chinese: 一 二 三 一二三',
           'And some much more rare Chinese: 齉 堃 齉堃',
           'Je vais aussi écrire en français pour tester les accents',
           'Classical Irish also has some unusual characters, so in they go: Gaelaċ, ꝼ',
       ]
       UpperCamelCase__         =    list(zip(self.test_sentences ,				self.test_sentences[::-1]      )      )

   def 						SCREAMING_SNAKE_CASE__ (					self      )		->       Any:
       for tokenizer, tf_tokenizer in zip(self.tokenizers ,				self.tf_tokenizers      ):
           for test_inputs in (self.test_sentences, self.paired_sentences):
               UpperCamelCase__         =    tokenizer(snake_case_ ,				return_tensors='tf' ,				padding='longest'      )
               UpperCamelCase__         =    tf_tokenizer(snake_case_      )

               for key in python_outputs.keys():
                   self.assertTrue(tf.reduce_all(python_outputs[key].shape == tf_outputs[key].shape      )      )
                   self.assertTrue(tf.reduce_all(tf.cast(python_outputs[key] ,				tf.intaa      ) == tf_outputs[key]      )      )

   @slow
   def 						SCREAMING_SNAKE_CASE__ (					self      )		->       str:
       for tf_tokenizer in self.tf_tokenizers:
           UpperCamelCase__         =    tf_tokenizer(self.paired_sentences      )
           UpperCamelCase__         =    tf_tokenizer(
               text=[sentence[0] for sentence in self.paired_sentences] ,				text_pair=[sentence[1] for sentence in self.paired_sentences] ,				)
           for key in merged_outputs.keys():
               self.assertTrue(tf.reduce_all(tf.cast(merged_outputs[key] ,				tf.intaa      ) == separated_outputs[key]      )      )

   @slow
   def 						SCREAMING_SNAKE_CASE__ (					self      )		->       Dict:
       for tf_tokenizer in self.tf_tokenizers:
           UpperCamelCase__         =    tf.function(snake_case_      )
           for test_inputs in (self.test_sentences, self.paired_sentences):
               UpperCamelCase__         =    tf.constant(snake_case_      )
               UpperCamelCase__         =    compiled_tokenizer(snake_case_      )
               UpperCamelCase__         =    tf_tokenizer(snake_case_      )

               for key in eager_outputs.keys():
                   self.assertTrue(tf.reduce_all(eager_outputs[key] == compiled_outputs[key]      )      )







   @slow
   def 						SCREAMING_SNAKE_CASE__ (					self      )		->       List[str]:
       for tf_tokenizer in self.tf_tokenizers:
           UpperCamelCase__         =    ModelToSave(tokenizer=snake_case_      )
           UpperCamelCase__         =    tf.convert_to_tensor(self.test_sentences      )
           UpperCamelCase__         =    model(snake_case_      )  # Build model with some sample inputs
           with TemporaryDirectory() as tempdir:
               UpperCamelCase__         =    Path(snake_case_      ) / 'saved.model'
               model.save(snake_case_      )
               UpperCamelCase__         =    tf.keras.models.load_model(snake_case_      )
           UpperCamelCase__         =    loaded_model(snake_case_      )
           # We may see small differences because the loaded model is compiled, so we need an epsilon for the test
           self.assertLessEqual(tf.reduce_max(tf.abs(out - loaded_output      )      ) ,				1E-5      )




