



'''simple docstring'''




import unittest
from pathlib import Path
from tempfile import TemporaryDirectory

from transformers import AutoConfig, TFGPTaLMHeadModel, is_keras_nlp_available, is_tf_available
from transformers.models.gpta.tokenization_gpta import GPTaTokenizer
from transformers.testing_utils import require_keras_nlp, require_tf, slow


if is_tf_available():
						import tensorflow as tf

if is_keras_nlp_available():
						from transformers.models.gpta import TFGPTaTokenizer


UpperCamelCase__ :			Tuple												=				['''gpt2''']
UpperCamelCase__ :			Optional[Any]												=				'''gpt2'''

if is_tf_available():






						class 		_UpperCamelCase						(							tf.Module					):


									'''simple docstring'''

									def __init__(							self  :				int							,  lowerCAmelCase__  :				Any		):





											"""simple docstring"""






											super().__init__()
											__SCREAMING_SNAKE_CASE :	int					 =	tokenizer
											__SCREAMING_SNAKE_CASE :	List[str]					 =	AutoConfig.from_pretrained(lowerCAmelCase__		)
											__SCREAMING_SNAKE_CASE :	str					 =	TFGPTaLMHeadModel.from_config(lowerCAmelCase__		)



									@tf.function(input_signature=(tf.TensorSpec((None,)							,  tf.string							,  name="""text"""		),)		)
									def UpperCamelCase__      (							self  :				str							,  lowerCAmelCase__  :				int		):





											"""simple docstring"""






											__SCREAMING_SNAKE_CASE :	Tuple					 =	self.tokenizer(lowerCAmelCase__		)
											__SCREAMING_SNAKE_CASE :	int					 =	tokenized["""input_ids"""].to_tensor()

											__SCREAMING_SNAKE_CASE :	Optional[Any]					 =	tf.cast(input_ids_dense > 0							,  tf.intaa		)
											# input_mask = tf.reshape(input_mask, [-1, MAX_SEQ_LEN])

											__SCREAMING_SNAKE_CASE :	Optional[int]					 =	self.model(input_ids=lowerCAmelCase__							,  attention_mask=lowerCAmelCase__		)["""logits"""]

											return outputs






@require_tf
@require_keras_nlp
class 		_UpperCamelCase						(							unittest.TestCase					):


			'''simple docstring'''

			def UpperCamelCase__      (							self  :				List[Any]		):





					"""simple docstring"""






					super().setUp()

					__SCREAMING_SNAKE_CASE :	Dict					 =	[GPTaTokenizer.from_pretrained(lowerCAmelCase__		) for checkpoint in (TOKENIZER_CHECKPOINTS)]
					__SCREAMING_SNAKE_CASE :	Any					 =	[TFGPTaTokenizer.from_pretrained(lowerCAmelCase__		) for checkpoint in TOKENIZER_CHECKPOINTS]
					assert len(self.tokenizers		) == len(self.tf_tokenizers		)

					__SCREAMING_SNAKE_CASE :	Optional[int]					 =	[
					    """This is a straightforward English test sentence.""",
					    """This one has some weird characters\rto\nsee\r\nif  those\u00E9break things.""",
					    """Now we're going to add some Chinese: 一 二 三 一二三""",
					    """And some much more rare Chinese: 齉 堃 齉堃""",
					    """Je vais aussi écrire en français pour tester les accents""",
					    """Classical Irish also has some unusual characters, so in they go: Gaelaċ, ꝼ""",
					]
					__SCREAMING_SNAKE_CASE :	List[Any]					 =	list(zip(self.test_sentences							,  self.test_sentences[::-1]		)		)

			def UpperCamelCase__      (							self  :				List[Any]		):





					"""simple docstring"""






					for tokenizer, tf_tokenizer in zip(self.tokenizers							,  self.tf_tokenizers		):
							for test_inputs in self.test_sentences:
									__SCREAMING_SNAKE_CASE :	int					 =	tokenizer([test_inputs]							,  return_tensors="""tf"""		)
									__SCREAMING_SNAKE_CASE :	Union[str, Any]					 =	tf_tokenizer([test_inputs]		)

									for key in python_outputs.keys():
											# convert them to numpy to avoid messing with ragged tensors
											__SCREAMING_SNAKE_CASE :	str					 =	python_outputs[key].numpy()
											__SCREAMING_SNAKE_CASE :	Optional[int]					 =	tf_outputs[key].numpy()

											self.assertTrue(tf.reduce_all(python_outputs_values.shape == tf_outputs_values.shape		)		)
											self.assertTrue(tf.reduce_all(tf.cast(lowerCAmelCase__							,  tf.intaa		) == tf_outputs_values		)		)

			@slow
			def UpperCamelCase__      (							self  :				Dict		):





					"""simple docstring"""






					for tf_tokenizer in self.tf_tokenizers:
							__SCREAMING_SNAKE_CASE :	List[Any]					 =	tf.function(lowerCAmelCase__		)
							for test_inputs in self.test_sentences:
									__SCREAMING_SNAKE_CASE :	Optional[Any]					 =	tf.constant(lowerCAmelCase__		)
									__SCREAMING_SNAKE_CASE :	List[str]					 =	compiled_tokenizer(lowerCAmelCase__		)
									__SCREAMING_SNAKE_CASE :	Tuple					 =	tf_tokenizer(lowerCAmelCase__		)

									for key in eager_outputs.keys():
											self.assertTrue(tf.reduce_all(eager_outputs[key] == compiled_outputs[key]		)		)

			@slow
			def UpperCamelCase__      (							self  :				Optional[int]		):





					"""simple docstring"""






					for tf_tokenizer in self.tf_tokenizers:
							__SCREAMING_SNAKE_CASE :	Union[str, Any]					 =	ModelToSave(tokenizer=lowerCAmelCase__		)
							__SCREAMING_SNAKE_CASE :	Tuple					 =	tf.convert_to_tensor([self.test_sentences[0]]		)
							__SCREAMING_SNAKE_CASE :	Any					 =	model.serving(lowerCAmelCase__		)  # Build model with some sample inputs
							with TemporaryDirectory() as tempdir:
									__SCREAMING_SNAKE_CASE :	Union[str, Any]					 =	Path(lowerCAmelCase__		) / """saved.model"""
									tf.saved_model.save(lowerCAmelCase__							,  lowerCAmelCase__							,  signatures={"""serving_default""": model.serving}		)
									__SCREAMING_SNAKE_CASE :	str					 =	tf.saved_model.load(lowerCAmelCase__		)
							__SCREAMING_SNAKE_CASE :	Tuple					 =	loaded_model.signatures["""serving_default"""](lowerCAmelCase__		)["""output_0"""]
							# We may see small differences because the loaded model is compiled, so we need an epsilon for the test
							self.assertTrue(tf.reduce_all(out == loaded_output		)		)

			@slow
			def UpperCamelCase__      (							self  :				Optional[int]		):





					"""simple docstring"""






					for tf_tokenizer in self.tf_tokenizers:
							__SCREAMING_SNAKE_CASE :	Tuple					 =	tf.convert_to_tensor([self.test_sentences[0]]		)
							__SCREAMING_SNAKE_CASE :	Dict					 =	tf_tokenizer(lowerCAmelCase__		)  # Build model with some sample inputs

							__SCREAMING_SNAKE_CASE :	Optional[Any]					 =	tf_tokenizer.get_config()
							__SCREAMING_SNAKE_CASE :	Any					 =	TFGPTaTokenizer.from_config(lowerCAmelCase__		)
							__SCREAMING_SNAKE_CASE :	List[Any]					 =	model_from_config(lowerCAmelCase__		)

							for key in from_config_output.keys():
									self.assertTrue(tf.reduce_all(from_config_output[key] == out[key]		)		)



			@slow
			def UpperCamelCase__      (							self  :				List[str]		):





					"""simple docstring"""






					for tf_tokenizer in self.tf_tokenizers:
							# for the test to run
							__SCREAMING_SNAKE_CASE :	str					 =	1_2_3_1_2_3

							for max_length in [3, 5, 1_0_2_4]:
									__SCREAMING_SNAKE_CASE :	Union[str, Any]					 =	tf.convert_to_tensor([self.test_sentences[0]]		)
									__SCREAMING_SNAKE_CASE :	str					 =	tf_tokenizer(lowerCAmelCase__							,  max_length=lowerCAmelCase__		)

									__SCREAMING_SNAKE_CASE :	str					 =	out["""input_ids"""].numpy().shape[1]

									assert out_length == max_length




'''simple docstring'''




from typing import Mapping

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxSeqaSeqConfigWithPast
from ...utils import logging


UpperCamelCase__ :			Tuple												=				logging.get_logger(__name__)

UpperCamelCase__ :			List[str]												=				{
    '''t5-small''': '''https://huggingface.co/t5-small/resolve/main/config.json''',
    '''t5-base''': '''https://huggingface.co/t5-base/resolve/main/config.json''',
    '''t5-large''': '''https://huggingface.co/t5-large/resolve/main/config.json''',
    '''t5-3b''': '''https://huggingface.co/t5-3b/resolve/main/config.json''',
    '''t5-11b''': '''https://huggingface.co/t5-11b/resolve/main/config.json''',
}






class 		_UpperCamelCase						(							lowerCamelCase__					):


			'''simple docstring'''

			_A   :	Union[str, Any]    					=	'''t5'''
			_A   :	Optional[Any]    					=	['''past_key_values''']
			_A   :	Any    					=	{'''hidden_size''': '''d_model''', '''num_attention_heads''': '''num_heads''', '''num_hidden_layers''': '''num_layers'''}

			def __init__(							self  :				Union[str, Any]							,  lowerCAmelCase__  :				List[str]=3_2_1_2_8							,  lowerCAmelCase__  :				Dict=5_1_2							,  lowerCAmelCase__  :				str=6_4							,  lowerCAmelCase__  :				Tuple=2_0_4_8							,  lowerCAmelCase__  :				Optional[Any]=6							,  lowerCAmelCase__  :				Any=None							,  lowerCAmelCase__  :				Any=8							,  lowerCAmelCase__  :				Dict=3_2							,  lowerCAmelCase__  :				Dict=1_2_8							,  lowerCAmelCase__  :				Any=0.1							,  lowerCAmelCase__  :				str=1E-6							,  lowerCAmelCase__  :				str=1.0							,  lowerCAmelCase__  :				Optional[Any]="relu"							,  lowerCAmelCase__  :				List[str]=True							,  lowerCAmelCase__  :				Union[str, Any]=True							,  lowerCAmelCase__  :				Optional[Any]=0							,  lowerCAmelCase__  :				List[Any]=1							,  **lowerCAmelCase__  :				Dict							,  ):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE :	List[str]					 =	vocab_size
					__SCREAMING_SNAKE_CASE :	str					 =	d_model
					__SCREAMING_SNAKE_CASE :	str					 =	d_kv
					__SCREAMING_SNAKE_CASE :	Optional[Any]					 =	d_ff
					__SCREAMING_SNAKE_CASE :	Optional[Any]					 =	num_layers
					__SCREAMING_SNAKE_CASE :	List[str]					 =	(
					    num_decoder_layers if num_decoder_layers is not None else self.num_layers
					)  # default = symmetry
					__SCREAMING_SNAKE_CASE :	Optional[int]					 =	num_heads
					__SCREAMING_SNAKE_CASE :	Any					 =	relative_attention_num_buckets
					__SCREAMING_SNAKE_CASE :	int					 =	relative_attention_max_distance
					__SCREAMING_SNAKE_CASE :	Optional[int]					 =	dropout_rate
					__SCREAMING_SNAKE_CASE :	Optional[int]					 =	layer_norm_epsilon
					__SCREAMING_SNAKE_CASE :	Optional[int]					 =	initializer_factor
					__SCREAMING_SNAKE_CASE :	Any					 =	feed_forward_proj
					__SCREAMING_SNAKE_CASE :	str					 =	use_cache

					__SCREAMING_SNAKE_CASE :	List[str]					 =	self.feed_forward_proj.split("""-"""		)
					__SCREAMING_SNAKE_CASE :	Tuple					 =	act_info[-1]
					__SCREAMING_SNAKE_CASE :	int					 =	act_info[0] == """gated"""

					if len(lowerCAmelCase__		) > 1 and act_info[0] != "gated" or len(lowerCAmelCase__		) > 2:
							raise ValueError(
							    F"`feed_forward_proj`: {feed_forward_proj} is not a valid activation function of the dense layer."
							    """Please make sure `feed_forward_proj` is of the format `gated-{ACT_FN}` or `{ACT_FN}`, e.g. """
							    """'gated-gelu' or 'relu'"""		)

					# for backwards compatibility
					if feed_forward_proj == "gated-gelu":
							__SCREAMING_SNAKE_CASE :	List[Any]					 =	"""gelu_new"""

					super().__init__(
					    pad_token_id=lowerCAmelCase__							,  eos_token_id=lowerCAmelCase__							,  is_encoder_decoder=lowerCAmelCase__							,  **lowerCAmelCase__							,  )






class 		_UpperCamelCase						(							lowerCamelCase__					):


			'''simple docstring'''

			@property
			def UpperCamelCase__      (							self  :				Tuple		):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE :	List[Any]					 =	{
					    """input_ids""": {0: """batch""", 1: """encoder_sequence"""},
					    """attention_mask""": {0: """batch""", 1: """encoder_sequence"""},
					}
					if self.use_past:
							__SCREAMING_SNAKE_CASE :	Optional[Any]					 =	"""past_encoder_sequence + sequence"""
							__SCREAMING_SNAKE_CASE :	Optional[Any]					 =	{0: """batch"""}
							__SCREAMING_SNAKE_CASE :	Optional[int]					 =	{0: """batch""", 1: """past_decoder_sequence + sequence"""}
					else:
							__SCREAMING_SNAKE_CASE :	List[Any]					 =	{0: """batch""", 1: """decoder_sequence"""}
							__SCREAMING_SNAKE_CASE :	Optional[Any]					 =	{0: """batch""", 1: """decoder_sequence"""}

					if self.use_past:
							self.fill_with_past_key_values_(lowerCAmelCase__							,  direction="""inputs"""		)

					return common_inputs



			@property
			def UpperCamelCase__      (							self  :				List[str]		):





					"""simple docstring"""






					return 1_3