

"""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    :Optional[Any]			=       ["bert-base-uncased", "bert-base-cased"]
a    :Optional[int]			=       "hf-internal-testing/tiny-bert-tf-only"

if is_tf_available():

  class    __a							(tf.keras.Model):






       '''simple docstring'''

       def __init__(  self		,			_a     )     ->					Tuple:


             """simple docstring"""


             super().__init__()
             SCREAMING_SNAKE_CASE__      :					Union[str, Any]											=		tokenizer
             SCREAMING_SNAKE_CASE__      :					List[Any]											=		AutoConfig.from_pretrained(lowercase_     )
             SCREAMING_SNAKE_CASE__      :					Tuple											=		TFAutoModel.from_config(lowercase_     )





       def     _a	(  self		,			_a     )     ->					str:


             """simple docstring"""


             SCREAMING_SNAKE_CASE__      :					Any											=		self.tokenizer(lowercase_     )
             SCREAMING_SNAKE_CASE__      :					List[str]											=		self.bert(**lowercase_     )
             return out["pooler_output"]

@require_tf
@require_tensorflow_text
class    __a							(unittest.TestCase):






     '''simple docstring'''

     def     _a	(  self     )     ->					Dict:


           """simple docstring"""


           super().setUp()

           SCREAMING_SNAKE_CASE__      :					List[str]											=		[
               BertTokenizer.from_pretrained(lowercase_     ) for checkpoint in (TOKENIZER_CHECKPOINTS * 2)
           ]  # repeat for when fast_bert_tokenizer=false
           SCREAMING_SNAKE_CASE__      :					Dict											=		[TFBertTokenizer.from_pretrained(lowercase_     ) for checkpoint in TOKENIZER_CHECKPOINTS] + [
               TFBertTokenizer.from_pretrained(lowercase_		,			use_fast_bert_tokenizer=lowercase_     )
               for checkpoint in TOKENIZER_CHECKPOINTS
           ]
           assert len(self.tokenizers     ) == len(self.tf_tokenizers     )

           SCREAMING_SNAKE_CASE__      :					Optional[int]											=		[
               '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ċ, ꝼ',
           ]
           SCREAMING_SNAKE_CASE__      :					List[str]											=		list(zip(self.test_sentences		,			self.test_sentences[::-1]     )     )

     def     _a	(  self     )     ->					List[Any]:


           """simple docstring"""


           for tokenizer, tf_tokenizer in zip(self.tokenizers		,			self.tf_tokenizers     ):
                 for test_inputs in (self.test_sentences, self.paired_sentences):
                       SCREAMING_SNAKE_CASE__      :					List[str]											=		tokenizer(lowercase_		,			return_tensors="""tf"""		,			padding="""longest"""     )
                       SCREAMING_SNAKE_CASE__      :					List[Any]											=		tf_tokenizer(lowercase_     )

                       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     _a	(  self     )     ->					Dict:


           """simple docstring"""


           for tf_tokenizer in self.tf_tokenizers:
                 SCREAMING_SNAKE_CASE__      :					Dict											=		tf_tokenizer(self.paired_sentences     )
                 SCREAMING_SNAKE_CASE__      :					List[str]											=		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     _a	(  self     )     ->					List[Any]:


           """simple docstring"""


           for tf_tokenizer in self.tf_tokenizers:
                 SCREAMING_SNAKE_CASE__      :					Any											=		tf.function(lowercase_     )
                 for test_inputs in (self.test_sentences, self.paired_sentences):
                       SCREAMING_SNAKE_CASE__      :					Optional[Any]											=		tf.constant(lowercase_     )
                       SCREAMING_SNAKE_CASE__      :					int											=		compiled_tokenizer(lowercase_     )
                       SCREAMING_SNAKE_CASE__      :					List[Any]											=		tf_tokenizer(lowercase_     )

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





     @slow
     def     _a	(  self     )     ->					Optional[int]:


           """simple docstring"""


           for tf_tokenizer in self.tf_tokenizers:
                 SCREAMING_SNAKE_CASE__      :					int											=		ModelToSave(tokenizer=lowercase_     )
                 SCREAMING_SNAKE_CASE__      :					Optional[Any]											=		tf.convert_to_tensor(self.test_sentences     )
                 SCREAMING_SNAKE_CASE__      :					Any											=		model(lowercase_     )  # Build model with some sample inputs
                 with TemporaryDirectory() as tempdir:
                       SCREAMING_SNAKE_CASE__      :					Optional[int]											=		Path(lowercase_     ) / 'saved.model'
                       model.save(lowercase_     )
                       SCREAMING_SNAKE_CASE__      :					Optional[int]											=		tf.keras.models.load_model(lowercase_     )
                 SCREAMING_SNAKE_CASE__      :					Dict											=		loaded_model(lowercase_     )
                 # 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     )








import os

import pytest
from attr import dataclass


lowerCamelCase											=       'us-east-1'  # defaults region




@dataclass
class A  :
      UpperCamelCase__							:  str
      UpperCamelCase__							:  Dict      ='arn:aws:iam::558105141721:role/sagemaker_execution_role'
      UpperCamelCase__							:  int      ={
          'task_name': 'mnli',
          'per_device_train_batch_size': 16,
          'per_device_eval_batch_size': 16,
          'do_train': True,
          'do_eval': True,
          'do_predict': True,
          'output_dir': '/opt/ml/model',
          'overwrite_output_dir': True,
          'max_steps': 500,
          'save_steps': 5500,
      }
      UpperCamelCase__							:  Optional[Any]      ={**hyperparameters, 'max_steps': 1000}






      @property
      def        lowerCamelCase       (					self						:     List[Any] )					->						str:
             """simple docstring"""




             if self.framework == "pytorch":
                    return [
                        {"Name": "train_runtime", "Regex": r"train_runtime.*=\D*(.*?)$"},
                        {"Name": "eval_accuracy", "Regex": r"eval_accuracy.*=\D*(.*?)$"},
                        {"Name": "eval_loss", "Regex": r"eval_loss.*=\D*(.*?)$"},
                    ]
             else:
                    return [
                        {"Name": "train_runtime", "Regex": r"train_runtime.*=\D*(.*?)$"},
                        {"Name": "eval_accuracy", "Regex": r"loss.*=\D*(.*?)]?$"},
                        {"Name": "eval_loss", "Regex": r"sparse_categorical_accuracy.*=\D*(.*?)]?$"},
                    ]






      @property
      def        lowerCamelCase       (					self						:     List[Any] )					->						str:
             """simple docstring"""




             return F'''{self.framework}-transfromers-test'''






      @property
      def        lowerCamelCase       (					self						:     Union[str, Any] )					->						str:
             """simple docstring"""




             return F'''./tests/sagemaker/scripts/{self.framework}'''






      @property
      def        lowerCamelCase       (					self						:     Union[str, Any] )					->						str:
             """simple docstring"""




             if self.framework == "pytorch":
                    return "763104351884.dkr.ecr.us-east-1.amazonaws.com/huggingface-pytorch-training:1.7.1-transformers4.6.1-gpu-py36-cu110-ubuntu18.04"
             else:
                    return "763104351884.dkr.ecr.us-east-1.amazonaws.com/huggingface-tensorflow-training:2.4.1-transformers4.6.1-gpu-py37-cu110-ubuntu18.04"




@pytest.fixture(scope='class'     )
def 	a_      (    SCREAMING_SNAKE_CASE__					:      Tuple     ):
       '''simple docstring'''





       _lowerCamelCase       :		List[Any]		=SageMakerTestEnvironment(framework=request.cls.framework     )


