






import inspect
import unittest

from transformers import RegNetConfig, is_flax_available
from transformers.testing_utils import require_flax, slow
from transformers.utils import cached_property, is_vision_available

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


if is_flax_available():
	import jax
	import jax.numpy as jnp

	from transformers.models.regnet.modeling_flax_regnet import FlaxRegNetForImageClassification, FlaxRegNetModel

if is_vision_available():
	from PIL import Image

	from transformers import AutoImageProcessor

class     _SCREAMING_SNAKE_CASE       (			unittest.TestCase      ):





	def __init__(		self				:					str ,  __lowerCamelCase				:					Optional[int] ,  __lowerCamelCase				:					List[str]=3 ,  __lowerCamelCase				:					List[Any]=32 ,  __lowerCamelCase				:					int=3 ,  __lowerCamelCase				:					Any=10 ,  __lowerCamelCase				:					Optional[int]=[10, 20, 30, 40] ,  __lowerCamelCase				:					Tuple=[1, 1, 2, 1] ,  __lowerCamelCase				:					Tuple=True ,  __lowerCamelCase				:					Tuple=True ,  __lowerCamelCase				:					Union[str, Any]="relu" ,  __lowerCamelCase				:					Optional[int]=3 ,  __lowerCamelCase				:					Any=None ,  ):
		UpperCamelCase     :Dict          =	parent
		UpperCamelCase     :Optional[Any]          =	batch_size
		UpperCamelCase     :Optional[int]          =	image_size
		UpperCamelCase     :Dict          =	num_channels
		UpperCamelCase     :Tuple          =	embeddings_size
		UpperCamelCase     :Optional[int]          =	hidden_sizes
		UpperCamelCase     :Any          =	depths
		UpperCamelCase     :List[str]          =	is_training
		UpperCamelCase     :int          =	use_labels
		UpperCamelCase     :Any          =	hidden_act
		UpperCamelCase     :Optional[int]          =	num_labels
		UpperCamelCase     :Optional[int]          =	scope
		UpperCamelCase     :List[Any]          =	len(__lowerCamelCase	)





	def 						_A (		self				:					int	):
		UpperCamelCase     :int          =	floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]	)

		UpperCamelCase     :int          =	self.get_config()

		return config, pixel_values





	def 						_A (		self				:					Optional[int]	):
		return RegNetConfig(
		    num_channels=self.num_channels ,  embeddings_size=self.embeddings_size ,  hidden_sizes=self.hidden_sizes ,  depths=self.depths ,  hidden_act=self.hidden_act ,  num_labels=self.num_labels ,  image_size=self.image_size ,  )





	def 						_A (		self				:					str ,  __lowerCamelCase				:					int ,  __lowerCamelCase				:					Tuple	):
		UpperCamelCase     :int          =	FlaxRegNetModel(config=__lowerCamelCase	)
		UpperCamelCase     :int          =	model(__lowerCamelCase	)

		# Output shape (b, c, h, w)
		self.parent.assertEqual(
		    result.last_hidden_state.shape ,  (self.batch_size, self.hidden_sizes[-1], self.image_size // 32, self.image_size // 32) ,  )





	def 						_A (		self				:					Union[str, Any] ,  __lowerCamelCase				:					Any ,  __lowerCamelCase				:					str	):
		UpperCamelCase     :str          =	self.num_labels
		UpperCamelCase     :Tuple          =	FlaxRegNetForImageClassification(config=__lowerCamelCase	)
		UpperCamelCase     :Union[str, Any]          =	model(__lowerCamelCase	)
		self.parent.assertEqual(result.logits.shape ,  (self.batch_size, self.num_labels)	)





	def 						_A (		self				:					Any	):
		UpperCamelCase     :str          =	self.prepare_config_and_inputs()
		UpperCamelCase     ,      UpperCamelCase     :Tuple          =	config_and_inputs
		UpperCamelCase     :int          =	{"""pixel_values""": pixel_values}
		return config, inputs_dict

@require_flax
class     _SCREAMING_SNAKE_CASE       (			_a		,   unittest.TestCase      ):
	snake_case__							:     int     	=  (FlaxRegNetModel, FlaxRegNetForImageClassification) if is_flax_available() else ()

	snake_case__							:     List[str]     	=  False
	snake_case__							:     Optional[Any]     	=  False
	snake_case__							:     Optional[int]     	=  False





	def 						_A (		self				:					Any	):
		UpperCamelCase     :Any          =	FlaxRegNetModelTester(self	)
		UpperCamelCase     :Tuple          =	ConfigTester(self ,  config_class=__lowerCamelCase ,  has_text_modality=__lowerCamelCase	)





	def 						_A (		self				:					str	):
		self.create_and_test_config_common_properties()
		self.config_tester.create_and_test_config_to_json_string()
		self.config_tester.create_and_test_config_to_json_file()
		self.config_tester.create_and_test_config_from_and_save_pretrained()
		self.config_tester.create_and_test_config_with_num_labels()
		self.config_tester.check_config_can_be_init_without_params()
		self.config_tester.check_config_arguments_init()





	def 						_A (		self				:					List[Any]	):
		return





	def 						_A (		self				:					int	):
		UpperCamelCase     :Any          =	self.model_tester.prepare_config_and_inputs()
		self.model_tester.create_and_check_model(*__lowerCamelCase	)





	def 						_A (		self				:					List[Any]	):
		UpperCamelCase     :Optional[Any]          =	self.model_tester.prepare_config_and_inputs()
		self.model_tester.create_and_check_for_image_classification(*__lowerCamelCase	)





	@unittest.skip(reason="""RegNet does not use inputs_embeds"""	)
	def 						_A (		self				:					Any	):
		pass





	@unittest.skip(reason="""RegNet does not support input and output embeddings"""	)
	def 						_A (		self				:					Union[str, Any]	):
		pass





	def 						_A (		self				:					str	):
		UpperCamelCase     ,      UpperCamelCase     :Optional[Any]          =	self.model_tester.prepare_config_and_inputs_for_common()

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

			UpperCamelCase     :Dict          =	["""pixel_values"""]
			self.assertListEqual(arg_names[:1] ,  __lowerCamelCase	)





	def 						_A (		self				:					List[str]	):
		def check_hidden_states_output(__lowerCamelCase				:					Any ,  __lowerCamelCase				:					Tuple ,  __lowerCamelCase				:					Tuple	):
			UpperCamelCase     :Union[str, Any]          =	model_class(__lowerCamelCase	)

			UpperCamelCase     :str          =	model(**self._prepare_for_class(__lowerCamelCase ,  __lowerCamelCase	)	)

			UpperCamelCase     :str          =	outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

			UpperCamelCase     :str          =	self.model_tester.num_stages
			self.assertEqual(len(__lowerCamelCase	) ,  expected_num_stages + 1	)

		UpperCamelCase     ,      UpperCamelCase     :Any          =	self.model_tester.prepare_config_and_inputs_for_common()

		for model_class in self.all_model_classes:
			UpperCamelCase     :Dict          =	True
			check_hidden_states_output(__lowerCamelCase ,  __lowerCamelCase ,  __lowerCamelCase	)

			# check that output_hidden_states also work using config
			del inputs_dict["output_hidden_states"]
			UpperCamelCase     :int          =	True

			check_hidden_states_output(__lowerCamelCase ,  __lowerCamelCase ,  __lowerCamelCase	)





	def 						_A (		self				:					Optional[Any]	):
		UpperCamelCase     ,      UpperCamelCase     :Tuple          =	self.model_tester.prepare_config_and_inputs_for_common()

		for model_class in self.all_model_classes:
			with self.subTest(model_class.__name__	):
				UpperCamelCase     :Tuple          =	self._prepare_for_class(__lowerCamelCase ,  __lowerCamelCase	)
				UpperCamelCase     :Optional[int]          =	model_class(__lowerCamelCase	)

				@jax.jit
				def model_jitted(__lowerCamelCase				:					Optional[Any] ,  **__lowerCamelCase				:					Optional[int]	):
					return model(pixel_values=__lowerCamelCase ,  **__lowerCamelCase	)

				with self.subTest("""JIT Enabled"""	):
					UpperCamelCase     :Dict          =	model_jitted(**__lowerCamelCase	).to_tuple()

				with self.subTest("""JIT Disabled"""	):
					with jax.disable_jit():
						UpperCamelCase     :Optional[Any]          =	model_jitted(**__lowerCamelCase	).to_tuple()

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

def    SCREAMING_SNAKE_CASE_							(							)		->			Any:






	"""simple docstring"""



	UpperCamelCase     :Any          =	Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""					)
	return image

@require_flax
class     _SCREAMING_SNAKE_CASE       (			unittest.TestCase      ):





	@cached_property
	def 						_A (		self				:					Optional[Any]	):
		return AutoImageProcessor.from_pretrained("""facebook/regnet-y-040"""	) if is_vision_available() else None





	@slow
	def 						_A (		self				:					Any	):
		UpperCamelCase     :Any          =	FlaxRegNetForImageClassification.from_pretrained("""facebook/regnet-y-040"""	)

		UpperCamelCase     :Optional[int]          =	self.default_image_processor
		UpperCamelCase     :Dict          =	prepare_img()
		UpperCamelCase     :Union[str, Any]          =	image_processor(images=__lowerCamelCase ,  return_tensors="""np"""	)

		UpperCamelCase     :List[Any]          =	model(**__lowerCamelCase	)

		# verify the logits
		UpperCamelCase     :int          =	(1, 1_000)
		self.assertEqual(outputs.logits.shape ,  __lowerCamelCase	)

		UpperCamelCase     :Union[str, Any]          =	jnp.array([-0.4180, -1.5051, -3.4836]	)

		self.assertTrue(jnp.allclose(outputs.logits[0, :3] ,  __lowerCamelCase ,  atol=1E-4	)	)








import os
import unittest

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

from ...test_tokenization_common import TokenizerTesterMixin

class     _SCREAMING_SNAKE_CASE       (			_a		,   unittest.TestCase      ):
	snake_case__							:     Optional[Any]     	=  TransfoXLTokenizer
	snake_case__							:     List[Any]     	=  False
	snake_case__							:     Tuple     	=  False





	def 						_A (		self				:					str	):
		super().setUp()

		UpperCamelCase     :Dict          =	[
		    """<unk>""",
		    """[CLS]""",
		    """[SEP]""",
		    """want""",
		    """unwanted""",
		    """wa""",
		    """un""",
		    """running""",
		    """,""",
		    """low""",
		    """l""",
		]
		UpperCamelCase     :str          =	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				:					List[str] ,  **__lowerCamelCase				:					Any	):
		UpperCamelCase     :Any          =	True
		return TransfoXLTokenizer.from_pretrained(self.tmpdirname ,  **__lowerCamelCase	)





	def 						_A (		self				:					Any ,  __lowerCamelCase				:					int	):
		UpperCamelCase     :List[Any]          =	"""<unk> UNwanted , running"""
		UpperCamelCase     :int          =	"""<unk> unwanted, running"""
		return input_text, output_text





	def 						_A (		self				:					Tuple	):
		UpperCamelCase     :List[str]          =	TransfoXLTokenizer(vocab_file=self.vocab_file ,  lower_case=__lowerCamelCase	)

		UpperCamelCase     :Any          =	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				:					Optional[Any]	):
		UpperCamelCase     :List[Any]          =	TransfoXLTokenizer(lower_case=__lowerCamelCase	)

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





	def 						_A (		self				:					Union[str, Any]	):
		UpperCamelCase     :int          =	TransfoXLTokenizer(lower_case=__lowerCamelCase	)

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





	def 						_A (		self				:					Tuple	):
		UpperCamelCase     :Any          =	TransfoXLTokenizer(lower_case=__lowerCamelCase	)
		UpperCamelCase     :Optional[int]          =	"""Hello (bracket) and side-scrolled [and] Henry's $5,000 with 3.34 m. What's up!?"""
		UpperCamelCase     :Optional[int]          =	[
		    """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				:					List[Any]	):
		UpperCamelCase     :Any          =	self.get_tokenizer()
		UpperCamelCase     :List[str]          =	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"""	)
