




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     __a(	unittest.TestCase  ):






    """simple docstring"""





    def __init__( self  ,_SCREAMING_SNAKE_CASE  ,_SCREAMING_SNAKE_CASE=13  ,_SCREAMING_SNAKE_CASE=7  ,_SCREAMING_SNAKE_CASE=True  ,_SCREAMING_SNAKE_CASE=True  ,_SCREAMING_SNAKE_CASE=True  ,_SCREAMING_SNAKE_CASE=True  ,_SCREAMING_SNAKE_CASE=99  ,_SCREAMING_SNAKE_CASE=32  ,_SCREAMING_SNAKE_CASE=5  ,_SCREAMING_SNAKE_CASE=4  ,_SCREAMING_SNAKE_CASE=37  ,_SCREAMING_SNAKE_CASE="gelu"  ,_SCREAMING_SNAKE_CASE=0.1  ,_SCREAMING_SNAKE_CASE=0.1  ,_SCREAMING_SNAKE_CASE=512  ,_SCREAMING_SNAKE_CASE=16  ,_SCREAMING_SNAKE_CASE=2  ,_SCREAMING_SNAKE_CASE=0.02  ,_SCREAMING_SNAKE_CASE=4  ,)					->						Tuple:
         UpperCAmelCase_     :				List[Any]  =     parent
         UpperCAmelCase_     :				Any  =     batch_size
         UpperCAmelCase_     :				List[Any]  =     seq_length
         UpperCAmelCase_     :				Union[str, Any]  =     is_training
         UpperCAmelCase_     :				Union[str, Any]  =     use_attention_mask
         UpperCAmelCase_     :				Any  =     use_token_type_ids
         UpperCAmelCase_     :				List[str]  =     use_labels
         UpperCAmelCase_     :				Union[str, Any]  =     vocab_size
         UpperCAmelCase_     :				int  =     hidden_size
         UpperCAmelCase_     :				List[str]  =     num_hidden_layers
         UpperCAmelCase_     :				Optional[Any]  =     num_attention_heads
         UpperCAmelCase_     :				Dict  =     intermediate_size
         UpperCAmelCase_     :				List[Any]  =     hidden_act
         UpperCAmelCase_     :				str  =     hidden_dropout_prob
         UpperCAmelCase_     :				Optional[int]  =     attention_probs_dropout_prob
         UpperCAmelCase_     :				Tuple  =     max_position_embeddings
         UpperCAmelCase_     :				Any  =     type_vocab_size
         UpperCAmelCase_     :				Tuple  =     type_sequence_label_size
         UpperCAmelCase_     :				str  =     initializer_range
         UpperCAmelCase_     :				Optional[int]  =     num_choices





    def 				a__ ( self    )					->						int:
         UpperCAmelCase_     :				str  =     ids_tensor([self.batch_size, self.seq_length]  ,self.vocab_size    )

         UpperCAmelCase_     :				Tuple  =     None
         if self.use_attention_mask:
              UpperCAmelCase_     :				Dict  =     random_attention_mask([self.batch_size, self.seq_length]    )

         UpperCAmelCase_     :				Dict  =     None
         if self.use_token_type_ids:
              UpperCAmelCase_     :				Optional[int]  =     ids_tensor([self.batch_size, self.seq_length]  ,self.type_vocab_size    )

         UpperCAmelCase_     :				Optional[Any]  =     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=__lowerCAmelCase  ,initializer_range=self.initializer_range  ,)

         return config, input_ids, token_type_ids, attention_mask





    def 				a__ ( self    )					->						str:
         UpperCAmelCase_     :				Optional[int]  =     self.prepare_config_and_inputs()
         UpperCAmelCase_,       UpperCAmelCase_,       UpperCAmelCase_,       UpperCAmelCase_     :				Tuple  =     config_and_inputs
         UpperCAmelCase_     :				Optional[int]  =     {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': attention_mask}
         return config, inputs_dict


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






    """simple docstring"""





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





    def 				a__ ( self    )					->						Optional[int]:
         UpperCAmelCase_     :				int  =     FlaxAlbertModelTester(self    )





    @slow
    def 				a__ ( self    )					->						Tuple:



         for model_class_name in self.all_model_classes:
              UpperCAmelCase_     :				int  =     model_class_name.from_pretrained('''albert-base-v2'''    )
              UpperCAmelCase_     :				Any  =     model(np.ones((1, 1)    )    )
              self.assertIsNotNone(__lowerCAmelCase    )


@require_flax
class     __a(	unittest.TestCase  ):






    """simple docstring"""





    @slow
    def 				a__ ( self    )					->						Optional[Any]:
         UpperCAmelCase_     :				str  =     FlaxAlbertModel.from_pretrained('''albert-base-v2'''    )
         UpperCAmelCase_     :				Tuple  =     np.array([[0, 345, 232, 328, 740, 140, 1_695, 69, 6_078, 1_588, 2]]    )
         UpperCAmelCase_     :				Union[str, Any]  =     np.array([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]    )
         UpperCAmelCase_     :				Tuple  =     model(__lowerCAmelCase  ,attention_mask=__lowerCAmelCase    )[0]
         UpperCAmelCase_     :				int  =     (1, 11, 768)
         self.assertEqual(output.shape  ,__lowerCAmelCase    )
         UpperCAmelCase_     :				int  =     np.array(
             [[[-0.65_13, 1.50_35, -0.27_66], [-0.65_15, 1.50_46, -0.27_80], [-0.65_12, 1.50_49, -0.27_84]]]    )

         self.assertTrue(jnp.allclose(output[:, 1:4, 1:4]  ,__lowerCAmelCase  ,atol=1e-4    )    )
"""simple docstring"""






import os
import unittest

from transformers.models.transfo_xl.tokenization_transfo_xl import VOCAB_FILES_NAMES, TransfoXLTokenizer

from ...test_tokenization_common import TokenizerTesterMixin





class 					_A		(			lowerCAmelCase    ,       unittest.TestCase      ):
      snake_case__     :  str				    =      TransfoXLTokenizer
      snake_case__     :  Union[str, Any]				    =      False
      snake_case__     :  Union[str, Any]				    =      False

      def 							A__			(		self		):







            """simple docstring"""



            super().setUp()

            lowercase								=      [
                """<unk>""",
                """[CLS]""",
                """[SEP]""",
                """want""",
                """unwanted""",
                """wa""",
                """un""",
                """running""",
                """,""",
                """low""",
                """l""",
            ]
            lowercase								=      os.path.join(self.tmpdirname						,			VOCAB_FILES_NAMES["""vocab_file"""]		)
            with open(self.vocab_file						,			"""w"""						,			encoding="""utf-8"""		) as vocab_writer:
                  vocab_writer.write("""""".join([x + """\n""" for x in vocab_tokens]		)		)

      def 							A__			(		self						,			**__lowerCAmelCase		):







            """simple docstring"""



            lowercase								=      True
            return TransfoXLTokenizer.from_pretrained(self.tmpdirname						,			**__lowerCAmelCase		)

      def 							A__			(		self						,			__lowerCAmelCase		):







            """simple docstring"""



            lowercase								=      """<unk> UNwanted , running"""
            lowercase								=      """<unk> unwanted, running"""
            return input_text, output_text

      def 							A__			(		self		):







            """simple docstring"""



            lowercase								=      TransfoXLTokenizer(vocab_file=self.vocab_file						,			lower_case=__lowerCAmelCase		)

            lowercase								=      tokenizer.tokenize("""<unk> UNwanted , running"""		)
            self.assertListEqual(__lowerCAmelCase						,			["""<unk>""", """unwanted""", """,""", """running"""]		)

            self.assertListEqual(tokenizer.convert_tokens_to_ids(__lowerCAmelCase		)						,			[0, 4, 8, 7]		)

      def 							A__			(		self		):







            """simple docstring"""



            lowercase								=      TransfoXLTokenizer(lower_case=__lowerCAmelCase		)

            self.assertListEqual(
                tokenizer.tokenize(""" \tHeLLo ! how  \n Are yoU ?  """		)						,			["""hello""", """!""", """how""", """are""", """you""", """?"""]		)

      def 							A__			(		self		):







            """simple docstring"""



            lowercase								=      TransfoXLTokenizer(lower_case=__lowerCAmelCase		)

            self.assertListEqual(
                tokenizer.tokenize(""" \tHeLLo ! how  \n Are yoU ?  """		)						,			["""HeLLo""", """!""", """how""", """Are""", """yoU""", """?"""]		)

      def 							A__			(		self		):







            """simple docstring"""



            lowercase								=      TransfoXLTokenizer(lower_case=__lowerCAmelCase		)
            lowercase								=      """Hello (bracket) and side-scrolled [and] Henry's $5,000 with 3.34 m. What's up!?"""
            lowercase								=      [
                """Hello""",
                """(""",
                """bracket""",
                """)""",
                """and""",
                """side""",
                """@-@""",
                """scrolled""",
                """[""",
                """and""",
                """]""",
                """Henry""",
                """'s""",
                """$""",
                """5""",
                """@,@""",
                """000""",
                """with""",
                """3""",
                """@.@""",
                """34""",
                """m""",
                """.""",
                """What""",
                """'s""",
                """up""",
                """!""",
                """?""",
            ]

            self.assertListEqual(tokenizer.tokenize(__lowerCAmelCase		)						,			__lowerCAmelCase		)

            self.assertEqual(tokenizer.convert_tokens_to_string(__lowerCAmelCase		)						,			__lowerCAmelCase		)







      def 							A__			(		self		):







            """simple docstring"""



            lowercase								=      self.get_tokenizer()
            lowercase								=      len(__lowerCAmelCase		)

            tokenizer.add_tokens(["""new1""", """new2"""]		)
            tokenizer.move_added_token("""new1"""						,			1		)

            # Check that moved token is not copied (duplicate)
            self.assertEqual(len(__lowerCAmelCase		)						,			original_len + 2		)
            # Check that token is moved to specified id
            self.assertEqual(tokenizer.encode("""new1"""		)						,			[1]		)
            self.assertEqual(tokenizer.decode([1]		)						,			"""new1"""		)



