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

if is_tf_available():

 class 					_lowercase    (     tf.keras.Model			):






     '''simple docstring'''




     def __init__(    self       ,						lowerCamelCase__ ):
        super().__init__()
        lowerCAmelCase_:							List[Any]       =	tokenizer
        lowerCAmelCase_:							Tuple       =	AutoConfig.from_pretrained(lowerCamelCase__ )
        lowerCAmelCase_:							Tuple       =	TFAutoModel.from_config(lowerCamelCase__ )




     def      _a    (    self       ,						lowerCamelCase__ ):
        lowerCAmelCase_:							Optional[int]       =	self.tokenizer(lowerCamelCase__ )
        lowerCAmelCase_:							List[str]       =	self.bert(**lowerCamelCase__ )
        return out["pooler_output"]

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






    '''simple docstring'''




    def      _a    (    self ):
       super().setUp()

       lowerCAmelCase_:							Optional[int]       =	[
           BertTokenizer.from_pretrained(lowerCamelCase__ ) for checkpoint in (TOKENIZER_CHECKPOINTS * 2)
       ]  # repeat for when fast_bert_tokenizer=false
       lowerCAmelCase_:							Tuple       =	[TFBertTokenizer.from_pretrained(lowerCamelCase__ ) for checkpoint in TOKENIZER_CHECKPOINTS] + [
           TFBertTokenizer.from_pretrained(lowerCamelCase__       ,						use_fast_bert_tokenizer=lowerCamelCase__ )
           for checkpoint in TOKENIZER_CHECKPOINTS
       ]
       assert len(self.tokenizers ) == len(self.tf_tokenizers )

       lowerCAmelCase_:							List[Any]       =	[
           "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ċ, ꝼ",
       ]
       lowerCAmelCase_:							Tuple       =	list(zip(self.test_sentences       ,						self.test_sentences[::-1] ) )




    def      _a    (    self ):
       for tokenizer, tf_tokenizer in zip(self.tokenizers       ,						self.tf_tokenizers ):
          for test_inputs in (self.test_sentences, self.paired_sentences):
             lowerCAmelCase_:							Tuple       =	tokenizer(lowerCamelCase__       ,						return_tensors="tf"       ,						padding="longest" )
             lowerCAmelCase_:							List[str]       =	tf_tokenizer(lowerCamelCase__ )

             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 ):
       for tf_tokenizer in self.tf_tokenizers:
          lowerCAmelCase_:							List[str]       =	tf_tokenizer(self.paired_sentences )
          lowerCAmelCase_:							Optional[int]       =	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 ):
       for tf_tokenizer in self.tf_tokenizers:
          lowerCAmelCase_:							List[str]       =	tf.function(lowerCamelCase__ )
          for test_inputs in (self.test_sentences, self.paired_sentences):
             lowerCAmelCase_:							Optional[Any]       =	tf.constant(lowerCamelCase__ )
             lowerCAmelCase_:							Union[str, Any]       =	compiled_tokenizer(lowerCamelCase__ )
             lowerCAmelCase_:							Optional[Any]       =	tf_tokenizer(lowerCamelCase__ )

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




    @slow
    def      _a    (    self ):
       for tf_tokenizer in self.tf_tokenizers:
          lowerCAmelCase_:							int       =	ModelToSave(tokenizer=lowerCamelCase__ )
          lowerCAmelCase_:							Optional[int]       =	tf.convert_to_tensor(self.test_sentences )
          lowerCAmelCase_:							List[str]       =	model(lowerCamelCase__ )  # Build model with some sample inputs
          with TemporaryDirectory() as tempdir:
             lowerCAmelCase_:							List[str]       =	Path(lowerCamelCase__ ) / "saved.model"
             model.save(lowerCamelCase__ )
             lowerCAmelCase_:							Union[str, Any]       =	tf.keras.models.load_model(lowerCamelCase__ )
          lowerCAmelCase_:							List[Any]       =	loaded_model(lowerCamelCase__ )
          # 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 )
from ...configuration_utils import PretrainedConfig
from ...utils import logging


a  :      List[Any]            =    logging.get_logger(__name__)

a  :      Any            =    {"""openai-gpt""": """https://huggingface.co/openai-gpt/resolve/main/config.json"""}

class 					_lowercase    (     UpperCAmelCase__			):






    '''simple docstring'''
    SCREAMING_SNAKE_CASE:				int          =    'openai-gpt'
    SCREAMING_SNAKE_CASE:				List[str]          =    {
        'max_position_embeddings': 'n_positions',
        'hidden_size': 'n_embd',
        'num_attention_heads': 'n_head',
        'num_hidden_layers': 'n_layer',
    }




    def __init__(    self       ,						lowerCamelCase__=40_478       ,						lowerCamelCase__=512       ,						lowerCamelCase__=768       ,						lowerCamelCase__=12       ,						lowerCamelCase__=12       ,						lowerCamelCase__="gelu"       ,						lowerCamelCase__=0.1       ,						lowerCamelCase__=0.1       ,						lowerCamelCase__=0.1       ,						lowerCamelCase__=1E-5       ,						lowerCamelCase__=0.0_2       ,						lowerCamelCase__="cls_index"       ,						lowerCamelCase__=True       ,						lowerCamelCase__=None       ,						lowerCamelCase__=True       ,						lowerCamelCase__=0.1       ,						**lowerCamelCase__       ,						):
       lowerCAmelCase_:							Union[str, Any]       =	vocab_size
       lowerCAmelCase_:							List[Any]       =	n_positions
       lowerCAmelCase_:							Tuple       =	n_embd
       lowerCAmelCase_:							Optional[int]       =	n_layer
       lowerCAmelCase_:							Optional[int]       =	n_head
       lowerCAmelCase_:							int       =	afn
       lowerCAmelCase_:							str       =	resid_pdrop
       lowerCAmelCase_:							Optional[int]       =	embd_pdrop
       lowerCAmelCase_:							Optional[int]       =	attn_pdrop
       lowerCAmelCase_:							Dict       =	layer_norm_epsilon
       lowerCAmelCase_:							List[Any]       =	initializer_range
       lowerCAmelCase_:							Union[str, Any]       =	summary_type
       lowerCAmelCase_:							Any       =	summary_use_proj
       lowerCAmelCase_:							Dict       =	summary_activation
       lowerCAmelCase_:							Dict       =	summary_first_dropout
       lowerCAmelCase_:							List[Any]       =	summary_proj_to_labels
       super().__init__(**lowerCamelCase__ )