
from __future__ import annotations

import random
import unittest

from transformers import TransfoXLConfig, is_tf_available
from transformers.testing_utils import require_tf, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
				import tensorflow as tf

				from transformers import (
				    TF_TRANSFO_XL_PRETRAINED_MODEL_ARCHIVE_LIST,
				    TFTransfoXLForSequenceClassification,
				    TFTransfoXLLMHeadModel,
				    TFTransfoXLModel,
				)





class       _snake_case					:







			def __init__(				self						,   _a						,   ):
				__magic_name__		:		Optional[int]	   =     parent
				__magic_name__		:		Tuple	   =     13
				__magic_name__		:		Optional[int]	   =     7
				__magic_name__		:		Any	   =     30
				__magic_name__		:		List[Any]	   =     self.seq_length + self.mem_len
				__magic_name__		:		int	   =     15
				__magic_name__		:		int	   =     True
				__magic_name__		:		Optional[Any]	   =     True
				__magic_name__		:		str	   =     99
				__magic_name__		:		Any	   =     [10, 50, 80]
				__magic_name__		:		Optional[Any]	   =     32
				__magic_name__		:		str	   =     32
				__magic_name__		:		int	   =     4
				__magic_name__		:		Optional[Any]	   =     8
				__magic_name__		:		str	   =     128
				__magic_name__		:		Optional[int]	   =     2
				__magic_name__		:		Union[str, Any]	   =     2
				__magic_name__		:		int	   =     None
				__magic_name__		:		int	   =     1
				__magic_name__		:		Optional[int]	   =     0
				__magic_name__		:		int	   =     3
				__magic_name__		:		Union[str, Any]	   =     self.vocab_size - 1
				__magic_name__		:		Any	   =     0.01







			def 			SCREAMING_SNAKE_CASE    (				self		):
				__magic_name__		:		Optional[int]	   =     ids_tensor([self.batch_size, self.seq_length]						,   self.vocab_size		)
				__magic_name__		:		Any	   =     ids_tensor([self.batch_size, self.seq_length]						,   self.vocab_size		)

				__magic_name__		:		str	   =     None
				if self.use_labels:
					__magic_name__		:		Dict	   =     ids_tensor([self.batch_size, self.seq_length]						,   self.vocab_size		)

				__magic_name__		:		str	   =     TransfoXLConfig(
				    vocab_size=self.vocab_size						,   mem_len=self.mem_len						,   clamp_len=self.clamp_len						,   cutoffs=self.cutoffs						,   d_model=self.hidden_size						,   d_embed=self.d_embed						,   n_head=self.num_attention_heads						,   d_head=self.d_head						,   d_inner=self.d_inner						,   div_val=self.div_val						,   n_layer=self.num_hidden_layers						,   eos_token_id=self.eos_token_id						,   pad_token_id=self.vocab_size - 1						,   init_range=self.init_range						,   num_labels=self.num_labels						,   )

				return (config, input_ids_a, input_ids_a, lm_labels)







			def 			SCREAMING_SNAKE_CASE    (				self		):
				random.seed(self.seed		)
				tf.random.set_seed(self.seed		)







			def 			SCREAMING_SNAKE_CASE    (				self						,   _a						,   _a						,   _a						,   _a		):
				__magic_name__		:		List[Any]	   =     TFTransfoXLModel(_a		)

				__magic_name__   ,    __magic_name__		:		Optional[Any]	   =     model(_a		).to_tuple()

				__magic_name__		:		Dict	   =     {"input_ids": input_ids_a, "mems": mems_a}

				__magic_name__   ,    __magic_name__		:		Any	   =     model(_a		).to_tuple()

				self.parent.assertEqual(hidden_states_a.shape						,   (self.batch_size, self.seq_length, self.hidden_size)		)
				self.parent.assertEqual(hidden_states_a.shape						,   (self.batch_size, self.seq_length, self.hidden_size)		)
				self.parent.assertListEqual(
				    [mem.shape for mem in mems_a]						,   [(self.mem_len, self.batch_size, self.hidden_size)] * self.num_hidden_layers						,   )
				self.parent.assertListEqual(
				    [mem.shape for mem in mems_a]						,   [(self.mem_len, self.batch_size, self.hidden_size)] * self.num_hidden_layers						,   )







			def 			SCREAMING_SNAKE_CASE    (				self						,   _a						,   _a						,   _a						,   _a		):
				__magic_name__		:		Optional[int]	   =     TFTransfoXLLMHeadModel(_a		)

				__magic_name__   ,    __magic_name__		:		List[Any]	   =     model(_a		).to_tuple()

				__magic_name__		:		int	   =     {"input_ids": input_ids_a, "labels": lm_labels}
				__magic_name__   ,    __magic_name__		:		Dict	   =     model(_a		).to_tuple()

				__magic_name__   ,    __magic_name__		:		Optional[Any]	   =     model([input_ids_a, mems_a]		).to_tuple()

				__magic_name__		:		Any	   =     {"input_ids": input_ids_a, "mems": mems_a, "labels": lm_labels}

				__magic_name__   ,    __magic_name__		:		Any	   =     model(_a		).to_tuple()

				self.parent.assertEqual(lm_logits_a.shape						,   (self.batch_size, self.seq_length, self.vocab_size)		)
				self.parent.assertListEqual(
				    [mem.shape for mem in mems_a]						,   [(self.mem_len, self.batch_size, self.hidden_size)] * self.num_hidden_layers						,   )

				self.parent.assertEqual(lm_logits_a.shape						,   (self.batch_size, self.seq_length, self.vocab_size)		)
				self.parent.assertListEqual(
				    [mem.shape for mem in mems_a]						,   [(self.mem_len, self.batch_size, self.hidden_size)] * self.num_hidden_layers						,   )







			def 			SCREAMING_SNAKE_CASE    (				self						,   _a						,   _a						,   _a						,   _a		):
				__magic_name__		:		List[str]	   =     TFTransfoXLForSequenceClassification(_a		)
				__magic_name__		:		int	   =     model(_a		)
				self.parent.assertEqual(result.logits.shape						,   (self.batch_size, self.num_labels)		)







			def 			SCREAMING_SNAKE_CASE    (				self		):
				__magic_name__		:		Tuple	   =     self.prepare_config_and_inputs()
				((__magic_name__)   ,    (__magic_name__)   ,    (__magic_name__)   ,    (__magic_name__))		:		Optional[Any]	   =     config_and_inputs
				__magic_name__		:		Optional[Any]	   =     {"input_ids": input_ids_a}
				return config, inputs_dict







@require_tf
class       _snake_case					(     snake_case				,						snake_case				,						unittest.TestCase				):
			UpperCamelCase__          =    (
			    (TFTransfoXLModel, TFTransfoXLLMHeadModel, TFTransfoXLForSequenceClassification) if is_tf_available() else ()
			)
			UpperCamelCase__          =    () if is_tf_available() else ()
			UpperCamelCase__          =    (
			    {
			        'feature-extraction': TFTransfoXLModel,
			        'text-classification': TFTransfoXLForSequenceClassification,
			        'text-generation': TFTransfoXLLMHeadModel,
			        'zero-shot': TFTransfoXLForSequenceClassification,
			    }
			    if is_tf_available()
			    else {}
			)
			# TODO: add this test when TFTransfoXLLMHead has a linear output layer implemented
			UpperCamelCase__          =    False
			UpperCamelCase__          =    False
			UpperCamelCase__          =    False
			UpperCamelCase__          =    False







			def 			SCREAMING_SNAKE_CASE    (				self						,   _a						,   _a						,   _a						,   _a						,   _a		):
				if pipeline_test_casse_name == "TextGenerationPipelineTests":
					# Get `ValueError: AttributeError: 'NoneType' object has no attribute 'new_ones'` or `AssertionError`.
					# `TransfoXLConfig` was never used in pipeline tests: cannot create a simple
					# tokenizer.
					return True

				return False







			def 			SCREAMING_SNAKE_CASE    (				self		):
				__magic_name__		:		Union[str, Any]	   =     TFTransfoXLModelTester(self		)
				__magic_name__		:		str	   =     ConfigTester(self						,   config_class=_a						,   d_embed=37		)







			def 			SCREAMING_SNAKE_CASE    (				self		):
				self.config_tester.run_common_tests()







			def 			SCREAMING_SNAKE_CASE    (				self		):
				self.model_tester.set_seed()
				__magic_name__		:		Optional[int]	   =     self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_transfo_xl_model(*_a		)







			def 			SCREAMING_SNAKE_CASE    (				self		):
				self.model_tester.set_seed()
				__magic_name__		:		int	   =     self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_transfo_xl_lm_head(*_a		)







			def 			SCREAMING_SNAKE_CASE    (				self		):
				__magic_name__		:		Union[str, Any]	   =     self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_transfo_xl_for_sequence_classification(*_a		)







			def 			SCREAMING_SNAKE_CASE    (				self		):
				__magic_name__   ,    __magic_name__		:		Optional[int]	   =     self.model_tester.prepare_config_and_inputs_for_common()
				__magic_name__		:		List[str]	   =     [TFTransfoXLForSequenceClassification]

				for model_class in self.all_model_classes:
					__magic_name__		:		List[str]	   =     model_class(_a		)
					assert isinstance(model.get_input_embeddings()						,   tf.keras.layers.Layer		)
					if model_class in list_other_models_with_output_ebd:
						__magic_name__		:		List[Any]	   =     model.get_output_embeddings()
						assert isinstance(_a						,   tf.keras.layers.Layer		)
						__magic_name__		:		str	   =     model.get_bias()
						assert name is None
					else:
						__magic_name__		:		str	   =     model.get_output_embeddings()
						assert x is None
						__magic_name__		:		List[str]	   =     model.get_bias()
						assert name is None







			def 			SCREAMING_SNAKE_CASE    (				self		):
				# TODO JP: Make TransfoXL XLA compliant
				pass







			@slow
			def 			SCREAMING_SNAKE_CASE    (				self		):
				for model_name in TF_TRANSFO_XL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
					__magic_name__		:		List[Any]	   =     TFTransfoXLModel.from_pretrained(_a		)
					self.assertIsNotNone(_a		)







			@unittest.skip(reason="This model doesn't play well with fit() due to not returning a single loss."		)
			def 			SCREAMING_SNAKE_CASE    (				self		):
				pass







@require_tf
class       _snake_case					(     unittest.TestCase				):







			@unittest.skip("Skip test until #12651 is resolved."		)
			@slow
			def 			SCREAMING_SNAKE_CASE    (				self		):
				__magic_name__		:		Any	   =     TFTransfoXLLMHeadModel.from_pretrained("transfo-xl-wt103"		)
				# fmt: off
				__magic_name__		:		Tuple	   =     tf.convert_to_tensor([[33,1_297,2,1,1_009,4,1_109,11_739,4_762,358,5,25,245,22,1_706,17,20_098,5,3_215,21,37,1_110,3,13,1_041,4,24,603,490,2,71_477,20_098,104_447,2,20_961,1,2_604,4,1,329,3,6_224,831,16_002,2,8,603,78_967,29_546,23,803,20,25,416,5,8,232,4,277,6,1_855,4_601,3,29_546,54,8,3_609,5,57_211,49,4,1,277,18,8,1_755,15_691,3,341,25,416,693,42_573,71,17,401,94,31,17_919,2,29_546,7_873,18,1,435,23,11_011,755,5,5_167,3,7_983,98,84,2,29_546,3_267,8,3_609,4,1,4_865,1_075,2,6_087,71,6,346,8,5_854,3,29_546,824,1_400,1_868,2,19,160,2,311,8,5_496,2,20_920,17,25,15_097,3,24,24,0]]						,   dtype=tf.intaa		)  # noqa: E231
				# fmt: on
				#  In 1991 , the remains of Russian Tsar Nicholas II and his family
				#  ( except for Alexei and Maria ) are discovered .
				#  The voice of Nicholas's young son , Tsarevich Alexei Nikolaevich , narrates the
				#  remainder of the story . 1883 Western Siberia ,
				#  a young Grigori Rasputin is asked by his father and a group of men to perform magic .
				#  Rasputin has a vision and denounces one of the men as a horse thief . Although his
				#  father initially slaps him for making such an accusation , Rasputin watches as the
				#  man is chased outside and beaten . Twenty years later , Rasputin sees a vision of
				#  the Virgin Mary , prompting him to become a priest . Rasputin quickly becomes famous ,
				#  with people , even a bishop , begging for his blessing . <eod> </s> <eos>

				# fmt: off
				__magic_name__		:		List[str]	   =     [33,1_297,2,1,1_009,4,1_109,11_739,4_762,358,5,25,245,22,1_706,17,20_098,5,3_215,21,37,1_110,3,13,1_041,4,24,603,490,2,71_477,20_098,104_447,2,20_961,1,2_604,4,1,329,3,6_224,831,16_002,2,8,603,78_967,29_546,23,803,20,25,416,5,8,232,4,277,6,1_855,4_601,3,29_546,54,8,3_609,5,57_211,49,4,1,277,18,8,1_755,15_691,3,341,25,416,693,42_573,71,17,401,94,31,17_919,2,29_546,7_873,18,1,435,23,11_011,755,5,5_167,3,7_983,98,84,2,29_546,3_267,8,3_609,4,1,4_865,1_075,2,6_087,71,6,346,8,5_854,3,29_546,824,1_400,1_868,2,19,160,2,311,8,5_496,2,20_920,17,25,15_097,3,24,24,0,33,1,1_857,2,1,1_009,4,1_109,11_739,4_762,358,5,25,245,28,1_110,3,13,1_041,4,24,603,490,2,71_477,20_098,104_447,2,20_961,1,2_604,4,1,329,3,0]  # noqa: E231
				# fmt: on
				#  In 1991, the remains of Russian Tsar Nicholas II and his family (
				#  except for Alexei and Maria ) are discovered. The voice of young son,
				#  Tsarevich Alexei Nikolaevich, narrates the remainder of the story.
				#  1883 Western Siberia, a young Grigori Rasputin is asked by his father
				#  and a group of men to perform magic. Rasputin has a vision and
				#  denounces one of the men as a horse thief. Although his father initially
				#  slaps him for making such an accusation, Rasputin watches as the man
				#  is chased outside and beaten. Twenty years later, Rasputin sees a vision
				#  of the Virgin Mary, prompting him to become a priest.
				#  Rasputin quickly becomes famous, with people, even a bishop, begging for
				#  his blessing. <unk> <unk> <eos> In the 1990s, the remains of Russian Tsar
				# Nicholas II and his family were discovered. The voice of <unk> young son,
				# Tsarevich Alexei Nikolaevich, narrates the remainder of the story.<eos>

				__magic_name__		:		str	   =     model.generate(_a						,   max_length=200						,   do_sample=_a		)
				self.assertListEqual(output_ids[0].numpy().tolist()						,   _a		)


from . import __version__

# Backward compatibility imports, to make sure all those objects can be found in file_utils
from .utils import (
    CLOUDFRONT_DISTRIB_PREFIX,
    CONFIG_NAME,
    DISABLE_TELEMETRY,
    DUMMY_INPUTS,
    DUMMY_MASK,
    ENV_VARS_TRUE_AND_AUTO_VALUES,
    ENV_VARS_TRUE_VALUES,
    FEATURE_EXTRACTOR_NAME,
    FLAX_WEIGHTS_NAME,
    HF_MODULES_CACHE,
    HUGGINGFACE_CO_PREFIX,
    HUGGINGFACE_CO_RESOLVE_ENDPOINT,
    MODEL_CARD_NAME,
    MULTIPLE_CHOICE_DUMMY_INPUTS,
    PYTORCH_PRETRAINED_BERT_CACHE,
    PYTORCH_TRANSFORMERS_CACHE,
    S3_BUCKET_PREFIX,
    SENTENCEPIECE_UNDERLINE,
    SPIECE_UNDERLINE,
    TF2_WEIGHTS_NAME,
    TF_WEIGHTS_NAME,
    TORCH_FX_REQUIRED_VERSION,
    TRANSFORMERS_CACHE,
    TRANSFORMERS_DYNAMIC_MODULE_NAME,
    USE_JAX,
    USE_TF,
    USE_TORCH,
    WEIGHTS_INDEX_NAME,
    WEIGHTS_NAME,
    ContextManagers,
    DummyObject,
    EntryNotFoundError,
    ExplicitEnum,
    ModelOutput,
    PaddingStrategy,
    PushToHubMixin,
    RepositoryNotFoundError,
    RevisionNotFoundError,
    TensorType,
    _LazyModule,
    add_code_sample_docstrings,
    add_end_docstrings,
    add_start_docstrings,
    add_start_docstrings_to_model_forward,
    cached_property,
    copy_func,
    default_cache_path,
    define_sagemaker_information,
    get_cached_models,
    get_file_from_repo,
    get_full_repo_name,
    get_torch_version,
    has_file,
    http_user_agent,
    is_apex_available,
    is_bsa_available,
    is_coloredlogs_available,
    is_datasets_available,
    is_detectrona_available,
    is_faiss_available,
    is_flax_available,
    is_ftfy_available,
    is_in_notebook,
    is_ipex_available,
    is_librosa_available,
    is_offline_mode,
    is_onnx_available,
    is_pandas_available,
    is_phonemizer_available,
    is_protobuf_available,
    is_psutil_available,
    is_pyanvml_available,
    is_pyctcdecode_available,
    is_pytesseract_available,
    is_pytorch_quantization_available,
    is_rjieba_available,
    is_sagemaker_dp_enabled,
    is_sagemaker_mp_enabled,
    is_scipy_available,
    is_sentencepiece_available,
    is_seqio_available,
    is_sklearn_available,
    is_soundfile_availble,
    is_spacy_available,
    is_speech_available,
    is_tensor,
    is_tensorflow_probability_available,
    is_tfaonnx_available,
    is_tf_available,
    is_timm_available,
    is_tokenizers_available,
    is_torch_available,
    is_torch_bfaa_available,
    is_torch_cuda_available,
    is_torch_fx_available,
    is_torch_fx_proxy,
    is_torch_mps_available,
    is_torch_tfaa_available,
    is_torch_tpu_available,
    is_torchaudio_available,
    is_training_run_on_sagemaker,
    is_vision_available,
    replace_return_docstrings,
    requires_backends,
    to_numpy,
    to_py_obj,
    torch_only_method,
)
