





import inspect
import unittest

import numpy as np

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

from ...test_configuration_common import ConfigTester
from ...test_modeling_flax_common import FlaxModelTesterMixin, floats_tensor


if is_flax_available():
  import jax

  from transformers.models.vit.modeling_flax_vit import FlaxViTForImageClassification, FlaxViTModel







class 	__lowerCAmelCase      (       unittest.TestCase	):





   def __init__(			self       ,      snake_case       ,      snake_case=13       ,      snake_case=30       ,      snake_case=2       ,      snake_case=3       ,      snake_case=True       ,      snake_case=True       ,      snake_case=32       ,      snake_case=5       ,      snake_case=4       ,      snake_case=37       ,      snake_case="gelu"       ,      snake_case=0.1       ,      snake_case=0.1       ,      snake_case=10       ,      snake_case=0.02       ,      )					->	Optional[Any]:
      """simple docstring"""





      a__						:    str   		=	parent
      a__						:    Optional[int]   		=	batch_size
      a__						:    Optional[Any]   		=	image_size
      a__						:    List[str]   		=	patch_size
      a__						:    Union[str, Any]   		=	num_channels
      a__						:    List[Any]   		=	is_training
      a__						:    Any   		=	use_labels
      a__						:    Optional[int]   		=	hidden_size
      a__						:    Tuple   		=	num_hidden_layers
      a__						:    Optional[Any]   		=	num_attention_heads
      a__						:    int   		=	intermediate_size
      a__						:    Optional[Any]   		=	hidden_act
      a__						:    Optional[int]   		=	hidden_dropout_prob
      a__						:    Dict   		=	attention_probs_dropout_prob
      a__						:    str   		=	type_sequence_label_size
      a__						:    Optional[int]   		=	initializer_range

      # in ViT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token)
      a__						:    str   		=	(image_size // patch_size) ** 2
      a__						:    List[Any]   		=	num_patches + 1





   def 						_snake_case			(			self   )					->	List[str]:
      """simple docstring"""





      a__						:    Optional[int]   		=	floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]   )

      a__						:    Tuple   		=	ViTConfig(
          image_size=self.image_size       ,      patch_size=self.patch_size       ,      num_channels=self.num_channels       ,      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       ,      is_decoder=snake_case       ,      initializer_range=self.initializer_range       ,      )

      return config, pixel_values





   def 						_snake_case			(			self       ,      snake_case       ,      snake_case   )					->	Tuple:
      """simple docstring"""





      a__						:    int   		=	FlaxViTModel(config=snake_case   )
      a__						:    Optional[Any]   		=	model(snake_case   )
      # expected sequence length = num_patches + 1 (we add 1 for the [CLS] token)
      a__						:    Union[str, Any]   		=	(self.image_size, self.image_size)
      a__						:    Tuple   		=	(self.patch_size, self.patch_size)
      a__						:    Optional[Any]   		=	(image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0])
      self.parent.assertEqual(result.last_hidden_state.shape       ,      (self.batch_size, num_patches + 1, self.hidden_size)   )





   def 						_snake_case			(			self       ,      snake_case       ,      snake_case   )					->	List[str]:
      """simple docstring"""





      a__						:    List[Any]   		=	self.type_sequence_label_size
      a__						:    Dict   		=	FlaxViTForImageClassification(config=snake_case   )
      a__						:    Optional[Any]   		=	model(snake_case   )
      self.parent.assertEqual(result.logits.shape       ,      (self.batch_size, self.type_sequence_label_size)   )

      # test greyscale images
      a__						:    List[Any]   		=	1
      a__						:    int   		=	FlaxViTForImageClassification(snake_case   )

      a__						:    List[Any]   		=	floats_tensor([self.batch_size, 1, self.image_size, self.image_size]   )
      a__						:    Any   		=	model(snake_case   )





   def 						_snake_case			(			self   )					->	Dict:
      """simple docstring"""





      a__						:    str   		=	self.prepare_config_and_inputs()
      (
          (
          a__					
      )		,  (
          a__					
      )		,  					
      )						:    Dict   		=	config_and_inputs
      a__						:    str   		=	{"pixel_values": pixel_values}
      return config, inputs_dict







@require_flax
class 	__lowerCAmelCase      (       _UpperCamelCase		,unittest.TestCase	):
   _UpperCamelCase			:    Dict  		=					(FlaxViTModel, FlaxViTForImageClassification) if is_flax_available() else ()





   def 						_snake_case			(			self   )					->	Dict:
      """simple docstring"""





      a__						:    List[Any]   		=	FlaxViTModelTester(self   )
      a__						:    Dict   		=	ConfigTester(self       ,      config_class=snake_case       ,      has_text_modality=snake_case       ,      hidden_size=37   )





   def 						_snake_case			(			self   )					->	Any:
      """simple docstring"""





      self.config_tester.run_common_tests()





   def 						_snake_case			(			self   )					->	List[Any]:
      """simple docstring"""





      a__						:    Tuple   		=	self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_model(*snake_case   )





   def 						_snake_case			(			self   )					->	Optional[int]:
      """simple docstring"""





      a__						:    Tuple   		=	self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_image_classification(*snake_case   )





   def 						_snake_case			(			self   )					->	List[Any]:
      """simple docstring"""





      a__		,  a__						:    Union[str, Any]   		=	self.model_tester.prepare_config_and_inputs_for_common()

      for model_class in self.all_model_classes:
         a__						:    Any   		=	model_class(snake_case   )
         a__						:    List[Any]   		=	inspect.signature(model.__call__   )
         # signature.parameters is an OrderedDict => so arg_names order is deterministic
         a__						:    Dict   		=	[*signature.parameters.keys()]

         a__						:    List[Any]   		=	["pixel_values"]
         self.assertListEqual(arg_names[:1]       ,      snake_case   )





   def 						_snake_case			(			self   )					->	Optional[Any]:
      """simple docstring"""





      a__		,  a__						:    Tuple   		=	self.model_tester.prepare_config_and_inputs_for_common()

      for model_class in self.all_model_classes:
         with self.subTest(model_class.__name__   ):
            a__						:    Optional[int]   		=	self._prepare_for_class(snake_case       ,      snake_case   )
            a__						:    List[str]   		=	model_class(snake_case   )

            @jax.jit
            def model_jitted(snake_case       ,      **snake_case   ):
               return model(pixel_values=snake_case       ,      **snake_case   )

            with self.subTest("JIT Enabled"   ):
               a__						:    Optional[Any]   		=	model_jitted(**snake_case   ).to_tuple()

            with self.subTest("JIT Disabled"   ):
               with jax.disable_jit():
                  a__						:    Dict   		=	model_jitted(**snake_case   ).to_tuple()

            self.assertEqual(len(snake_case   )       ,      len(snake_case   )   )
            for jitted_output, output in zip(snake_case       ,      snake_case   ):
               self.assertEqual(jitted_output.shape       ,      output.shape   )





   @slow
   def 						_snake_case			(			self   )					->	Dict:
      """simple docstring"""





      for model_class_name in self.all_model_classes:
         a__						:    List[str]   		=	model_class_name.from_pretrained("google/vit-base-patch16-224"   )
         a__						:    str   		=	model(np.ones((1, 3, 224, 224)   )   )
         self.assertIsNotNone(snake_case   )







import os
import unittest

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

from ...test_tokenization_common import TokenizerTesterMixin

class 						__lowerCAmelCase      (      __magic_name__ ,			unittest.TestCase					):
							UpperCamelCase__   	=    TransfoXLTokenizer
							UpperCamelCase__   	=    False
							UpperCamelCase__   	=    False
							def  lowerCamelCase__				(		self		:Optional[Any]							):







								'''simple docstring'''







								super().setUp()

								a									=				[
								    """<unk>""",
								    """[CLS]""",
								    """[SEP]""",
								    """want""",
								    """unwanted""",
								    """wa""",
								    """un""",
								    """running""",
								    """,""",
								    """low""",
								    """l""",
								]
								a									=				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  lowerCamelCase__				(		self		:List[Any]						, **__magic_name__		:Optional[int]							):







								'''simple docstring'''







								a									=				True
								return TransfoXLTokenizer.from_pretrained(self.tmpdirname						, **__magic_name__							)
							def  lowerCamelCase__				(		self		:Union[str, Any]						, __magic_name__		:int							):







								'''simple docstring'''







								a									=				"""<unk> UNwanted , running"""
								a									=				"""<unk> unwanted, running"""
								return input_text, output_text
							def  lowerCamelCase__				(		self		:str							):







								'''simple docstring'''







								a									=				TransfoXLTokenizer(vocab_file=self.vocab_file						, lower_case=__magic_name__							)

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

								self.assertListEqual(tokenizer.convert_tokens_to_ids(__magic_name__							)						, [0, 4, 8, 7]							)
							def  lowerCamelCase__				(		self		:int							):







								'''simple docstring'''







								a									=				TransfoXLTokenizer(lower_case=__magic_name__							)

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







								'''simple docstring'''







								a									=				TransfoXLTokenizer(lower_case=__magic_name__							)

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







								'''simple docstring'''







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

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

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





							def  lowerCamelCase__				(		self		:List[Any]							):







								'''simple docstring'''







								a									=				self.get_tokenizer()
								a									=				len(__magic_name__							)

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

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

