


"""simple docstring"""






import unittest
from pathlib import Path
from tempfile import TemporaryDirectory

from transformers import AutoConfig, TFGPTaLMHeadModel, is_keras_nlp_available, is_tf_available
from transformers.models.gpta.tokenization_gpta import GPTaTokenizer
from transformers.testing_utils import require_keras_nlp, require_tf, slow


if is_tf_available():
  import tensorflow as tf

if is_keras_nlp_available():
  from transformers.models.gpta import TFGPTaTokenizer


_lowerCAmelCase							:    Any					   =	['''gpt2''']
_lowerCAmelCase							:    int					   =	'''gpt2'''

if is_tf_available():




  class 							A_      (       tf.Module ):


    def __init__(			self:						List[str]							,__lowerCAmelCase:						Dict    ):







       '''simple docstring'''







       super().__init__()
       _lowerCamelCase   :				str							=     tokenizer
       _lowerCamelCase   :				Dict							=     AutoConfig.from_pretrained(__lowerCAmelCase    )
       _lowerCamelCase   :				List[str]							=     TFGPTaLMHeadModel.from_config(__lowerCAmelCase    )






    @tf.function(input_signature=(tf.TensorSpec((None,)							,tf.string							,name="text"    ),)    )
    def 							_lowercase					(			self:						List[str]							,__lowerCAmelCase:						List[str]    ):







       '''simple docstring'''







       _lowerCamelCase   :				Dict							=     self.tokenizer(__lowerCAmelCase    )
       _lowerCamelCase   :				Union[str, Any]							=     tokenized["input_ids"].to_tensor()

       _lowerCamelCase   :				List[str]							=     tf.cast(input_ids_dense > 0							,tf.intaa    )
       # input_mask = tf.reshape(input_mask, [-1, MAX_SEQ_LEN])

       _lowerCamelCase   :				str							=     self.model(input_ids=__lowerCAmelCase							,attention_mask=__lowerCAmelCase    )["logits"]

       return outputs




@require_tf
@require_keras_nlp
class 							A_      (       unittest.TestCase ):


  def 							_lowercase					(			self:						List[Any]    ):







     '''simple docstring'''







     super().setUp()

     _lowerCamelCase   :				Optional[Any]							=     [GPTaTokenizer.from_pretrained(__lowerCAmelCase    ) for checkpoint in (TOKENIZER_CHECKPOINTS)]
     _lowerCamelCase   :				str							=     [TFGPTaTokenizer.from_pretrained(__lowerCAmelCase    ) for checkpoint in TOKENIZER_CHECKPOINTS]
     assert len(self.tokenizers    ) == len(self.tf_tokenizers    )

     _lowerCamelCase   :				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   :				Union[str, Any]							=     list(zip(self.test_sentences							,self.test_sentences[::-1]    )    )


  def 							_lowercase					(			self:						Dict    ):







     '''simple docstring'''







     for tokenizer, tf_tokenizer in zip(self.tokenizers							,self.tf_tokenizers    ):
        for test_inputs in self.test_sentences:
           _lowerCamelCase   :				Any							=     tokenizer([test_inputs]							,return_tensors="tf"    )
           _lowerCamelCase   :				Optional[Any]							=     tf_tokenizer([test_inputs]    )

           for key in python_outputs.keys():
              # convert them to numpy to avoid messing with ragged tensors
              _lowerCamelCase   :				Optional[Any]							=     python_outputs[key].numpy()
              _lowerCamelCase   :				Tuple							=     tf_outputs[key].numpy()

              self.assertTrue(tf.reduce_all(python_outputs_values.shape == tf_outputs_values.shape    )    )
              self.assertTrue(tf.reduce_all(tf.cast(__lowerCAmelCase							,tf.intaa    ) == tf_outputs_values    )    )


  @slow
  def 							_lowercase					(			self:						str    ):







     '''simple docstring'''







     for tf_tokenizer in self.tf_tokenizers:
        _lowerCamelCase   :				Optional[int]							=     tf.function(__lowerCAmelCase    )
        for test_inputs in self.test_sentences:
           _lowerCamelCase   :				Union[str, Any]							=     tf.constant(__lowerCAmelCase    )
           _lowerCamelCase   :				str							=     compiled_tokenizer(__lowerCAmelCase    )
           _lowerCamelCase   :				Dict							=     tf_tokenizer(__lowerCAmelCase    )

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


  @slow
  def 							_lowercase					(			self:						Dict    ):







     '''simple docstring'''







     for tf_tokenizer in self.tf_tokenizers:
        _lowerCamelCase   :				str							=     ModelToSave(tokenizer=__lowerCAmelCase    )
        _lowerCamelCase   :				Optional[int]							=     tf.convert_to_tensor([self.test_sentences[0]]    )
        _lowerCamelCase   :				Optional[int]							=     model.serving(__lowerCAmelCase    )  # Build model with some sample inputs
        with TemporaryDirectory() as tempdir:
           _lowerCamelCase   :				Optional[Any]							=     Path(__lowerCAmelCase    ) / "saved.model"
           tf.saved_model.save(__lowerCAmelCase							,__lowerCAmelCase							,signatures={"serving_default": model.serving}    )
           _lowerCamelCase   :				Union[str, Any]							=     tf.saved_model.load(__lowerCAmelCase    )
        _lowerCamelCase   :				Union[str, Any]							=     loaded_model.signatures["serving_default"](__lowerCAmelCase    )["output_0"]
        # We may see small differences because the loaded model is compiled, so we need an epsilon for the test
        self.assertTrue(tf.reduce_all(out == loaded_output    )    )


  @slow
  def 							_lowercase					(			self:						Tuple    ):







     '''simple docstring'''







     for tf_tokenizer in self.tf_tokenizers:
        _lowerCamelCase   :				str							=     tf.convert_to_tensor([self.test_sentences[0]]    )
        _lowerCamelCase   :				Optional[Any]							=     tf_tokenizer(__lowerCAmelCase    )  # Build model with some sample inputs

        _lowerCamelCase   :				Dict							=     tf_tokenizer.get_config()
        _lowerCamelCase   :				int							=     TFGPTaTokenizer.from_config(__lowerCAmelCase    )
        _lowerCamelCase   :				Optional[Any]							=     model_from_config(__lowerCAmelCase    )

        for key in from_config_output.keys():
           self.assertTrue(tf.reduce_all(from_config_output[key] == out[key]    )    )






  @slow
  def 							_lowercase					(			self:						List[Any]    ):







     '''simple docstring'''







     for tf_tokenizer in self.tf_tokenizers:
        # for the test to run
        _lowerCamelCase   :				Tuple							=     123_123

        for max_length in [3, 5, 1_024]:
           _lowerCamelCase   :				Union[str, Any]							=     tf.convert_to_tensor([self.test_sentences[0]]    )
           _lowerCamelCase   :				Any							=     tf_tokenizer(__lowerCAmelCase							,max_length=__lowerCAmelCase    )

           _lowerCamelCase   :				Dict							=     out["input_ids"].numpy().shape[1]

           assert out_length == max_length
import unittest

import numpy as np

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

from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor, random_attention_mask


if is_flax_available():
  import jax.numpy as jnp

  from transformers.models.albert.modeling_flax_albert import (
      FlaxAlbertForMaskedLM,
      FlaxAlbertForMultipleChoice,
      FlaxAlbertForPreTraining,
      FlaxAlbertForQuestionAnswering,
      FlaxAlbertForSequenceClassification,
      FlaxAlbertForTokenClassification,
      FlaxAlbertModel,
  )




class 			__magic_name__	(		unittest.TestCase     ):





   """simple docstring"""




   def __init__(						self       :	Any		,	_lowercase       :	Optional[int]		,	_lowercase       :	Tuple=13		,	_lowercase       :	str=7		,	_lowercase       :	List[Any]=True		,	_lowercase       :	Optional[int]=True		,	_lowercase       :	str=True		,	_lowercase       :	Optional[int]=True		,	_lowercase       :	Dict=99		,	_lowercase       :	List[Any]=32		,	_lowercase       :	List[str]=5		,	_lowercase       :	str=4		,	_lowercase       :	int=37		,	_lowercase       :	List[str]="gelu"		,	_lowercase       :	Any=0.1		,	_lowercase       :	Optional[int]=0.1		,	_lowercase       :	Dict=512		,	_lowercase       :	int=16		,	_lowercase       :	Optional[Any]=2		,	_lowercase       :	Dict=0.02		,	_lowercase       :	List[Any]=4		,	):



    """simple docstring"""



    _UpperCamelCase:  List[str]    						=			parent
    _UpperCamelCase:  int    						=			batch_size
    _UpperCamelCase:  List[str]    						=			seq_length
    _UpperCamelCase:  Optional[int]    						=			is_training
    _UpperCamelCase:  Optional[Any]    						=			use_attention_mask
    _UpperCamelCase:  Any    						=			use_token_type_ids
    _UpperCamelCase:  List[str]    						=			use_labels
    _UpperCamelCase:  Optional[int]    						=			vocab_size
    _UpperCamelCase:  List[str]    						=			hidden_size
    _UpperCamelCase:  Union[str, Any]    						=			num_hidden_layers
    _UpperCamelCase:  Any    						=			num_attention_heads
    _UpperCamelCase:  List[str]    						=			intermediate_size
    _UpperCamelCase:  Union[str, Any]    						=			hidden_act
    _UpperCamelCase:  Dict    						=			hidden_dropout_prob
    _UpperCamelCase:  List[str]    						=			attention_probs_dropout_prob
    _UpperCamelCase:  str    						=			max_position_embeddings
    _UpperCamelCase:  Dict    						=			type_vocab_size
    _UpperCamelCase:  Tuple    						=			type_sequence_label_size
    _UpperCamelCase:  List[Any]    						=			initializer_range
    _UpperCamelCase:  str    						=			num_choices



   def 	lowerCAmelCase    (						self       :	str				):



    """simple docstring"""



    _UpperCamelCase:  str    						=			ids_tensor([self.batch_size, self.seq_length]		,	self.vocab_size				)

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

    _UpperCamelCase:  Optional[Any]    						=			None
    if self.use_token_type_ids:
     _UpperCamelCase:  Optional[Any]    						=			ids_tensor([self.batch_size, self.seq_length]		,	self.type_vocab_size				)

    _UpperCamelCase:  Tuple    						=			AlbertConfig(
        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=_lowercase		,	initializer_range=self.initializer_range		,	)

    return config, input_ids, token_type_ids, attention_mask



   def 	lowerCAmelCase    (						self       :	int				):



    """simple docstring"""



    _UpperCamelCase:  List[Any]    						=			self.prepare_config_and_inputs()
    _UpperCamelCase						,				_UpperCamelCase						,				_UpperCamelCase						,				_UpperCamelCase:  int    						=			config_and_inputs
    _UpperCamelCase:  int    						=			{'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': attention_mask}
    return config, inputs_dict




@require_flax
class 			__magic_name__	(		__a   ,						unittest.TestCase     ):





   """simple docstring"""




   lowerCAmelCase   :     Any	      = (
       (
           FlaxAlbertModel,
           FlaxAlbertForPreTraining,
           FlaxAlbertForMaskedLM,
           FlaxAlbertForMultipleChoice,
           FlaxAlbertForQuestionAnswering,
           FlaxAlbertForSequenceClassification,
           FlaxAlbertForTokenClassification,
           FlaxAlbertForQuestionAnswering,
       )
       if is_flax_available()
       else ()
   )



   def 	lowerCAmelCase    (						self       :	Union[str, Any]				):



    """simple docstring"""



    _UpperCamelCase:  Optional[int]    						=			FlaxAlbertModelTester(self				)



   @slow
   def 	lowerCAmelCase    (						self       :	Union[str, Any]				):



    """simple docstring"""



    for model_class_name in self.all_model_classes:
     _UpperCamelCase:  int    						=			model_class_name.from_pretrained('''albert-base-v2'''				)
     _UpperCamelCase:  List[Any]    						=			model(np.ones((1, 1)				)				)
     self.assertIsNotNone(_lowercase				)




@require_flax
class 			__magic_name__	(		unittest.TestCase     ):





   """simple docstring"""




   @slow
   def 	lowerCAmelCase    (						self       :	int				):



    """simple docstring"""



    _UpperCamelCase:  Optional[int]    						=			FlaxAlbertModel.from_pretrained('''albert-base-v2'''				)
    _UpperCamelCase:  List[Any]    						=			np.array([[0, 345, 232, 328, 740, 140, 1_695, 69, 6_078, 1_588, 2]]				)
    _UpperCamelCase:  Optional[int]    						=			np.array([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]				)
    _UpperCamelCase:  Optional[int]    						=			model(_lowercase		,	attention_mask=_lowercase				)[0]
    _UpperCamelCase:  Tuple    						=			(1, 11, 768)
    self.assertEqual(output.shape		,	_lowercase				)
    _UpperCamelCase:  Tuple    						=			np.array(
        [[[-0.6513, 1.5035, -0.2766], [-0.6515, 1.5046, -0.2780], [-0.6512, 1.5049, -0.2784]]]				)

    self.assertTrue(jnp.allclose(output[:, 1:4, 1:4]		,	_lowercase		,	atol=1E-4				)				)