
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  UpperCamelCase__		:







	'''simple docstring'''





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





	def   __snake_case				(			self		):
					A__  :	Dict         =							ids_tensor([self.batch_size, self.seq_length]				,       self.vocab_size		)
					A__  :	List[str]         =							ids_tensor([self.batch_size, self.seq_length]				,       self.vocab_size		)

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

					A__  :	Dict         =							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   __snake_case				(			self		):
					random.seed(self.seed		)
					tf.random.set_seed(self.seed		)





	def   __snake_case				(			self				,       UpperCamelCase__				,       UpperCamelCase__				,       UpperCamelCase__				,       UpperCamelCase__		):
					A__  :	Union[str, Any]         =							TFTransfoXLModel(UpperCamelCase__		)

					A__       ,							A__  :	Tuple         =							model(UpperCamelCase__		).to_tuple()

					A__  :	Optional[Any]         =							{'''input_ids''': input_ids_a, '''mems''': mems_a}

					A__       ,							A__  :	int         =							model(UpperCamelCase__		).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   __snake_case				(			self				,       UpperCamelCase__				,       UpperCamelCase__				,       UpperCamelCase__				,       UpperCamelCase__		):
					A__  :	Optional[Any]         =							TFTransfoXLLMHeadModel(UpperCamelCase__		)

					A__       ,							A__  :	Optional[int]         =							model(UpperCamelCase__		).to_tuple()

					A__  :	List[Any]         =							{'''input_ids''': input_ids_a, '''labels''': lm_labels}
					A__       ,							A__  :	Tuple         =							model(UpperCamelCase__		).to_tuple()

					A__       ,							A__  :	Optional[int]         =							model([input_ids_a, mems_a]		).to_tuple()

					A__  :	Any         =							{'''input_ids''': input_ids_a, '''mems''': mems_a, '''labels''': lm_labels}

					A__       ,							A__  :	List[str]         =							model(UpperCamelCase__		).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   __snake_case				(			self				,       UpperCamelCase__				,       UpperCamelCase__				,       UpperCamelCase__				,       UpperCamelCase__		):
					A__  :	Tuple         =							TFTransfoXLForSequenceClassification(UpperCamelCase__		)
					A__  :	Tuple         =							model(UpperCamelCase__		)
					self.parent.assertEqual(result.logits.shape				,       (self.batch_size, self.num_labels)		)





	def   __snake_case				(			self		):
					A__  :	int         =							self.prepare_config_and_inputs()
					((A__)       ,							(A__)       ,							(A__)       ,							(A__))  :	Dict         =							config_and_inputs
					A__  :	List[str]         =							{'''input_ids''': input_ids_a}
					return config, inputs_dict







@require_tf
class  UpperCamelCase__		(    SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_,			unittest.TestCase   ):







	'''simple docstring'''
	_lowerCAmelCase							       =				(
	    (TFTransfoXLModel, TFTransfoXLLMHeadModel, TFTransfoXLForSequenceClassification) if is_tf_available() else ()
	)
	_lowerCAmelCase							       =				() if is_tf_available() else ()
	_lowerCAmelCase							       =				(
	    {
	        "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
	_lowerCAmelCase							       =				False
	_lowerCAmelCase							       =				False
	_lowerCAmelCase							       =				False
	_lowerCAmelCase							       =				False





	def   __snake_case				(			self				,       UpperCamelCase__				,       UpperCamelCase__				,       UpperCamelCase__				,       UpperCamelCase__				,       UpperCamelCase__		):
					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   __snake_case				(			self		):
					A__  :	str         =							TFTransfoXLModelTester(self		)
					A__  :	List[Any]         =							ConfigTester(self				,       config_class=UpperCamelCase__				,       d_embed=37		)





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





	def   __snake_case				(			self		):
					self.model_tester.set_seed()
					A__  :	str         =							self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_transfo_xl_model(*UpperCamelCase__		)





	def   __snake_case				(			self		):
					self.model_tester.set_seed()
					A__  :	str         =							self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_transfo_xl_lm_head(*UpperCamelCase__		)





	def   __snake_case				(			self		):
					A__  :	str         =							self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_transfo_xl_for_sequence_classification(*UpperCamelCase__		)





	def   __snake_case				(			self		):
					A__       ,							A__  :	List[Any]         =							self.model_tester.prepare_config_and_inputs_for_common()
					A__  :	Tuple         =							[TFTransfoXLForSequenceClassification]

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





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





	@slow
	def   __snake_case				(			self		):
					for model_name in TF_TRANSFO_XL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
									A__  :	Optional[Any]         =							TFTransfoXLModel.from_pretrained(UpperCamelCase__		)
									self.assertIsNotNone(UpperCamelCase__		)





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







@require_tf
class  UpperCamelCase__		(    unittest.TestCase   ):







	'''simple docstring'''





	@unittest.skip('''Skip test until #12651 is resolved.'''		)
	@slow
	def   __snake_case				(			self		):
					A__  :	Any         =							TFTransfoXLLMHeadModel.from_pretrained('''transfo-xl-wt103'''		)
					# fmt: off
					A__  :	List[str]         =							tf.convert_to_tensor([[33,1297,2,1,1009,4,1109,1_1739,4762,358,5,25,245,22,1706,17,2_0098,5,3215,21,37,1110,3,13,1041,4,24,603,490,2,7_1477,2_0098,10_4447,2,2_0961,1,2604,4,1,329,3,6224,831,1_6002,2,8,603,7_8967,2_9546,23,803,20,25,416,5,8,232,4,277,6,1855,4601,3,2_9546,54,8,3609,5,5_7211,49,4,1,277,18,8,1755,1_5691,3,341,25,416,693,4_2573,71,17,401,94,31,1_7919,2,2_9546,7873,18,1,435,23,1_1011,755,5,5167,3,7983,98,84,2,2_9546,3267,8,3609,4,1,4865,1075,2,6087,71,6,346,8,5854,3,2_9546,824,1400,1868,2,19,160,2,311,8,5496,2,2_0920,17,25,1_5097,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
					A__  :	Dict         =							[33,1297,2,1,1009,4,1109,1_1739,4762,358,5,25,245,22,1706,17,2_0098,5,3215,21,37,1110,3,13,1041,4,24,603,490,2,7_1477,2_0098,10_4447,2,2_0961,1,2604,4,1,329,3,6224,831,1_6002,2,8,603,7_8967,2_9546,23,803,20,25,416,5,8,232,4,277,6,1855,4601,3,2_9546,54,8,3609,5,5_7211,49,4,1,277,18,8,1755,1_5691,3,341,25,416,693,4_2573,71,17,401,94,31,1_7919,2,2_9546,7873,18,1,435,23,1_1011,755,5,5167,3,7983,98,84,2,2_9546,3267,8,3609,4,1,4865,1075,2,6087,71,6,346,8,5854,3,2_9546,824,1400,1868,2,19,160,2,311,8,5496,2,2_0920,17,25,1_5097,3,24,24,0,33,1,1857,2,1,1009,4,1109,1_1739,4762,358,5,25,245,28,1110,3,13,1041,4,24,603,490,2,7_1477,2_0098,10_4447,2,2_0961,1,2604,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>

					A__  :	Union[str, Any]         =							model.generate(UpperCamelCase__				,       max_length=200				,       do_sample=UpperCamelCase__		)
					self.assertListEqual(output_ids[0].numpy().tolist()				,       UpperCamelCase__		)

_SCREAMING_SNAKE_CASE    :      List[str]          =			range(2, 2_0 + 1)
_SCREAMING_SNAKE_CASE    :      Optional[Any]          =			[1_0**k for k in range(ks[-1] + 1)]
_SCREAMING_SNAKE_CASE    :      dict[int, dict[int, list[list[int]]]]          =			{}

def 				SCREAMING_SNAKE_CASE     (      __UpperCamelCase :						Any							,						__UpperCamelCase :						Dict							,						__UpperCamelCase :						Any							,						__UpperCamelCase :						Optional[Any]       ) ->       int:

				"""simple docstring"""







				A__  :	Tuple         =							sum(a_i[j] for j in range(__UpperCamelCase							,						len(__UpperCamelCase       )       )       )
				A__  :	Tuple         =							sum(a_i[j] * base[j] for j in range(min(len(__UpperCamelCase       )							,						__UpperCamelCase       )       )       )

				A__       ,							A__  :	Optional[int]         =							0, 0
				A__  :	List[Any]         =							n - i

				A__  :	Any         =							memo.get(__UpperCamelCase       )

				if sub_memo is not None:
								A__  :	Optional[int]         =							sub_memo.get(__UpperCamelCase       )

								if jumps is not None and len(__UpperCamelCase       ) > 0:
												# find and make the largest jump without going over
												A__  :	List[Any]         =							-1
												for _k in range(len(__UpperCamelCase       ) - 1							,						-1							,						-1       ):
																if jumps[_k][2] <= k and jumps[_k][1] <= max_dn:
																				A__  :	List[str]         =							_k
																				break

												if max_jump >= 0:
																A__       ,							A__       ,							A__  :	List[Any]         =							jumps[max_jump]
																# since the difference between jumps is cached, add c
																A__  :	int         =							diff + c
																for j in range(min(__UpperCamelCase							,						len(__UpperCamelCase       )       )       ):
																				A__       ,							A__  :	List[str]         =							divmod(__UpperCamelCase							,						10       )
																if new_c > 0:
																				add(__UpperCamelCase							,						__UpperCamelCase							,						__UpperCamelCase       )

								else:
												A__  :	List[Any]         =							[]
				else:
								A__  :	Optional[Any]         =							{c: []}
								A__  :	int         =							sub_memo

				if dn >= max_dn or c + diff >= base[k]:
								return diff, dn

				if k > ks[0]:
								while True:
												# keep doing smaller jumps
												A__       ,							A__  :	str         =							next_term(__UpperCamelCase							,						k - 1							,						i + dn							,						__UpperCamelCase       )
												diff += _diff
												dn += terms_jumped

												if dn >= max_dn or c + diff >= base[k]:
																break
				else:
								# would be too small a jump, just compute sequential terms instead
								A__       ,							A__  :	str         =							compute(__UpperCamelCase							,						__UpperCamelCase							,						i + dn							,						__UpperCamelCase       )
								diff += _diff
								dn += terms_jumped

				A__  :	str         =							sub_memo[c]

				# keep jumps sorted by # of terms skipped
				A__  :	List[Any]         =							0
				while j < len(__UpperCamelCase       ):
								if jumps[j][1] > dn:
												break
								j += 1

				# cache the jump for this value digitsum(b) and c
				sub_memo[c].insert(__UpperCamelCase							,						(diff, dn, k)       )
				return (diff, dn)

def 				SCREAMING_SNAKE_CASE     (      __UpperCamelCase :						int							,						__UpperCamelCase :						Optional[int]							,						__UpperCamelCase :						List[str]							,						__UpperCamelCase :						int       ) ->       Any:

				"""simple docstring"""







				if i >= n:
								return 0, i
				if k > len(__UpperCamelCase       ):
								a_i.extend([0 for _ in range(k - len(__UpperCamelCase       )       )]       )

				# note: a_i -> b * 10^k + c
				# ds_b -> digitsum(b)
				# ds_c -> digitsum(c)
				A__  :	Optional[Any]         =							i
				A__       ,							A__       ,							A__  :	Dict         =							0, 0, 0
				for j in range(len(__UpperCamelCase       )       ):
								if j >= k:
												ds_b += a_i[j]
								else:
												ds_c += a_i[j]

				while i < n:
								i += 1
								A__  :	int         =							ds_c + ds_b
								diff += addend
								A__  :	List[Any]         =							0
								for j in range(__UpperCamelCase       ):
												A__  :	Optional[Any]         =							a_i[j] + addend
												A__       ,							A__  :	List[str]         =							divmod(__UpperCamelCase							,						10       )

												ds_c += a_i[j]

								if addend > 0:
												break

				if addend > 0:
								add(__UpperCamelCase							,						__UpperCamelCase							,						__UpperCamelCase       )
				return diff, i - start_i

def 				SCREAMING_SNAKE_CASE     (      __UpperCamelCase :						Dict							,						__UpperCamelCase :						List[Any]							,						__UpperCamelCase :						int       ) ->       Tuple:

				"""simple docstring"""







				for j in range(__UpperCamelCase							,						len(__UpperCamelCase       )       ):
								A__  :	Any         =							digits[j] + addend
								if s >= 10:
												A__       ,							A__  :	Union[str, Any]         =							divmod(__UpperCamelCase							,						10       )
												A__  :	Optional[int]         =							addend // 10 + quotient
								else:
												A__  :	Any         =							s
												A__  :	Dict         =							addend // 10

								if addend == 0:
												break

				while addend > 0:
								A__       ,							A__  :	Dict         =							divmod(__UpperCamelCase							,						10       )
								digits.append(__UpperCamelCase       )

def 				SCREAMING_SNAKE_CASE     (      __UpperCamelCase :						int = 10**15       ) ->       int:

				"""simple docstring"""







				A__  :	List[Any]         =							[1]
				A__  :	Dict         =							1
				A__  :	Tuple         =							0
				while True:
								A__       ,							A__  :	List[str]         =							next_term(__UpperCamelCase							,						20							,						i + dn							,						__UpperCamelCase       )
								dn += terms_jumped
								if dn == n - i:
												break

				A__  :	List[str]         =							0
				for j in range(len(__UpperCamelCase       )       ):
								a_n += digits[j] * 10**j
				return a_n


if __name__ == "__main__":
					print(f"""{solution() = }""")