from __future__ import annotations

import os
import tempfile
import unittest

from transformers import ConvBertConfig, 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, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
					import tensorflow as tf

					from transformers import (
					    TFConvBertForMaskedLM,
					    TFConvBertForMultipleChoice,
					    TFConvBertForQuestionAnswering,
					    TFConvBertForSequenceClassification,
					    TFConvBertForTokenClassification,
					    TFConvBertModel,
					)





class       lowercase__		:


						def __init__( self			:       List[str]					,	UpperCamelCase__			:       str					,	UpperCamelCase__			:       int=13					,	UpperCamelCase__			:       List[Any]=7					,	UpperCamelCase__			:       Union[str, Any]=True					,	UpperCamelCase__			:       List[str]=True					,	UpperCamelCase__			:       int=True					,	UpperCamelCase__			:       int=True					,	UpperCamelCase__			:       Dict=99					,	UpperCamelCase__			:       Dict=32					,	UpperCamelCase__			:       Union[str, Any]=2					,	UpperCamelCase__			:       Union[str, Any]=4					,	UpperCamelCase__			:       Union[str, Any]=37					,	UpperCamelCase__			:       str="gelu"					,	UpperCamelCase__			:       Union[str, Any]=0.1					,	UpperCamelCase__			:       Tuple=0.1					,	UpperCamelCase__			:       List[Any]=512					,	UpperCamelCase__			:       List[str]=16					,	UpperCamelCase__			:       int=2					,	UpperCamelCase__			:       Optional[int]=0.02					,	UpperCamelCase__			:       Tuple=3					,	UpperCamelCase__			:       List[Any]=4					,	UpperCamelCase__			:       List[Any]=None					,	):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:					Any           =							parent
												SCREAMING_SNAKE_CASE							:					List[str]           =							13
												SCREAMING_SNAKE_CASE							:					str           =							7
												SCREAMING_SNAKE_CASE							:					List[str]           =							True
												SCREAMING_SNAKE_CASE							:					int           =							True
												SCREAMING_SNAKE_CASE							:					List[Any]           =							True
												SCREAMING_SNAKE_CASE							:					int           =							True
												SCREAMING_SNAKE_CASE							:					str           =							99
												SCREAMING_SNAKE_CASE							:					int           =							384
												SCREAMING_SNAKE_CASE							:					Optional[int]           =							2
												SCREAMING_SNAKE_CASE							:					str           =							4
												SCREAMING_SNAKE_CASE							:					List[Any]           =							37
												SCREAMING_SNAKE_CASE							:					List[Any]           =							'''gelu'''
												SCREAMING_SNAKE_CASE							:					Any           =							0.1
												SCREAMING_SNAKE_CASE							:					Union[str, Any]           =							0.1
												SCREAMING_SNAKE_CASE							:					List[str]           =							512
												SCREAMING_SNAKE_CASE							:					str           =							16
												SCREAMING_SNAKE_CASE							:					str           =							2
												SCREAMING_SNAKE_CASE							:					Union[str, Any]           =							0.02
												SCREAMING_SNAKE_CASE							:					List[Any]           =							3
												SCREAMING_SNAKE_CASE							:					Dict           =							4
												SCREAMING_SNAKE_CASE							:					Optional[Any]           =							128
												SCREAMING_SNAKE_CASE							:					Optional[Any]           =							2
												SCREAMING_SNAKE_CASE							:					Optional[int]           =							9
												SCREAMING_SNAKE_CASE							:					int           =							1
												SCREAMING_SNAKE_CASE							:					str           =							None


						def  __A ( self			:       int		):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:					List[Any]           =							ids_tensor([self.batch_size, self.seq_length]					,	self.vocab_size		)

												SCREAMING_SNAKE_CASE							:					Union[str, Any]           =							None
												if self.use_input_mask:
																		SCREAMING_SNAKE_CASE							:					Tuple           =							random_attention_mask([self.batch_size, self.seq_length]		)

												SCREAMING_SNAKE_CASE							:					Any           =							None
												if self.use_token_type_ids:
																		SCREAMING_SNAKE_CASE							:					Optional[Any]           =							ids_tensor([self.batch_size, self.seq_length]					,	self.type_vocab_size		)

												SCREAMING_SNAKE_CASE							:					Optional[int]           =							None
												SCREAMING_SNAKE_CASE							:					Union[str, Any]           =							None
												SCREAMING_SNAKE_CASE							:					Any           =							None
												if self.use_labels:
																		SCREAMING_SNAKE_CASE							:					Any           =							ids_tensor([self.batch_size]					,	self.type_sequence_label_size		)
																		SCREAMING_SNAKE_CASE							:					Optional[Any]           =							ids_tensor([self.batch_size, self.seq_length]					,	self.num_labels		)
																		SCREAMING_SNAKE_CASE							:					Dict           =							ids_tensor([self.batch_size]					,	self.num_choices		)

												SCREAMING_SNAKE_CASE							:					int           =							ConvBertConfig(
												    vocab_size=self.vocab_size					,	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					,	max_position_embeddings=self.max_position_embeddings					,	type_vocab_size=self.type_vocab_size					,	initializer_range=self.initializer_range					,	return_dict=UpperCamelCase__					,	)

												return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels


						def  __A ( self			:       List[str]					,	UpperCamelCase__			:       int					,	UpperCamelCase__			:       Any					,	UpperCamelCase__			:       int					,	UpperCamelCase__			:       List[str]					,	UpperCamelCase__			:       Tuple					,	UpperCamelCase__			:       List[Any]					,	UpperCamelCase__			:       str		):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:					Dict           =							TFConvBertModel(config=UpperCamelCase__		)
												SCREAMING_SNAKE_CASE							:					Union[str, Any]           =							{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}

												SCREAMING_SNAKE_CASE							:					Optional[int]           =							[input_ids, input_mask]
												SCREAMING_SNAKE_CASE							:					Optional[int]           =							model(UpperCamelCase__		)

												SCREAMING_SNAKE_CASE							:					List[str]           =							model(UpperCamelCase__		)

												self.parent.assertEqual(result.last_hidden_state.shape					,	(self.batch_size, self.seq_length, self.hidden_size)		)


						def  __A ( self			:       Tuple					,	UpperCamelCase__			:       str					,	UpperCamelCase__			:       Dict					,	UpperCamelCase__			:       int					,	UpperCamelCase__			:       Optional[Any]					,	UpperCamelCase__			:       int					,	UpperCamelCase__			:       Optional[Any]					,	UpperCamelCase__			:       List[str]		):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:					str           =							TFConvBertForMaskedLM(config=UpperCamelCase__		)
												SCREAMING_SNAKE_CASE							:					Optional[Any]           =							{
												    '''input_ids''': input_ids,
												    '''attention_mask''': input_mask,
												    '''token_type_ids''': token_type_ids,
												}
												SCREAMING_SNAKE_CASE							:					List[str]           =							model(UpperCamelCase__		)
												self.parent.assertEqual(result.logits.shape					,	(self.batch_size, self.seq_length, self.vocab_size)		)


						def  __A ( self			:       List[Any]					,	UpperCamelCase__			:       Union[str, Any]					,	UpperCamelCase__			:       List[str]					,	UpperCamelCase__			:       Optional[int]					,	UpperCamelCase__			:       int					,	UpperCamelCase__			:       Tuple					,	UpperCamelCase__			:       int					,	UpperCamelCase__			:       int		):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:					Dict           =							self.num_labels
												SCREAMING_SNAKE_CASE							:					Any           =							TFConvBertForSequenceClassification(config=UpperCamelCase__		)
												SCREAMING_SNAKE_CASE							:					Dict           =							{
												    '''input_ids''': input_ids,
												    '''attention_mask''': input_mask,
												    '''token_type_ids''': token_type_ids,
												}

												SCREAMING_SNAKE_CASE							:					int           =							model(UpperCamelCase__		)
												self.parent.assertEqual(result.logits.shape					,	(self.batch_size, self.num_labels)		)


						def  __A ( self			:       List[Any]					,	UpperCamelCase__			:       Any					,	UpperCamelCase__			:       Optional[Any]					,	UpperCamelCase__			:       List[Any]					,	UpperCamelCase__			:       Tuple					,	UpperCamelCase__			:       Dict					,	UpperCamelCase__			:       int					,	UpperCamelCase__			:       Optional[Any]		):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:					Union[str, Any]           =							self.num_choices
												SCREAMING_SNAKE_CASE							:					Optional[int]           =							TFConvBertForMultipleChoice(config=UpperCamelCase__		)
												SCREAMING_SNAKE_CASE							:					Union[str, Any]           =							tf.tile(tf.expand_dims(UpperCamelCase__					,	1		)					,	(1, self.num_choices, 1)		)
												SCREAMING_SNAKE_CASE							:					Optional[int]           =							tf.tile(tf.expand_dims(UpperCamelCase__					,	1		)					,	(1, self.num_choices, 1)		)
												SCREAMING_SNAKE_CASE							:					Union[str, Any]           =							tf.tile(tf.expand_dims(UpperCamelCase__					,	1		)					,	(1, self.num_choices, 1)		)
												SCREAMING_SNAKE_CASE							:					Dict           =							{
												    '''input_ids''': multiple_choice_inputs_ids,
												    '''attention_mask''': multiple_choice_input_mask,
												    '''token_type_ids''': multiple_choice_token_type_ids,
												}
												SCREAMING_SNAKE_CASE							:					Any           =							model(UpperCamelCase__		)
												self.parent.assertEqual(result.logits.shape					,	(self.batch_size, self.num_choices)		)


						def  __A ( self			:       List[Any]					,	UpperCamelCase__			:       Union[str, Any]					,	UpperCamelCase__			:       str					,	UpperCamelCase__			:       Dict					,	UpperCamelCase__			:       Union[str, Any]					,	UpperCamelCase__			:       Tuple					,	UpperCamelCase__			:       Dict					,	UpperCamelCase__			:       Optional[Any]		):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:					Any           =							self.num_labels
												SCREAMING_SNAKE_CASE							:					int           =							TFConvBertForTokenClassification(config=UpperCamelCase__		)
												SCREAMING_SNAKE_CASE							:					str           =							{
												    '''input_ids''': input_ids,
												    '''attention_mask''': input_mask,
												    '''token_type_ids''': token_type_ids,
												}
												SCREAMING_SNAKE_CASE							:					str           =							model(UpperCamelCase__		)
												self.parent.assertEqual(result.logits.shape					,	(self.batch_size, self.seq_length, self.num_labels)		)


						def  __A ( self			:       str					,	UpperCamelCase__			:       Optional[Any]					,	UpperCamelCase__			:       Any					,	UpperCamelCase__			:       Dict					,	UpperCamelCase__			:       Optional[int]					,	UpperCamelCase__			:       Any					,	UpperCamelCase__			:       List[Any]					,	UpperCamelCase__			:       str		):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:					int           =							TFConvBertForQuestionAnswering(config=UpperCamelCase__		)
												SCREAMING_SNAKE_CASE							:					Optional[Any]           =							{
												    '''input_ids''': input_ids,
												    '''attention_mask''': input_mask,
												    '''token_type_ids''': token_type_ids,
												}

												SCREAMING_SNAKE_CASE							:					Any           =							model(UpperCamelCase__		)
												self.parent.assertEqual(result.start_logits.shape					,	(self.batch_size, self.seq_length)		)
												self.parent.assertEqual(result.end_logits.shape					,	(self.batch_size, self.seq_length)		)



						def  __A ( self			:       Dict		):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:					Any           =							self.prepare_config_and_inputs()
												(
												    (
												    SCREAMING_SNAKE_CASE
												)       ,						(
												    SCREAMING_SNAKE_CASE
												)       ,						(
												    SCREAMING_SNAKE_CASE
												)       ,						(
												    SCREAMING_SNAKE_CASE
												)       ,						(
												    SCREAMING_SNAKE_CASE
												)       ,						(
												    SCREAMING_SNAKE_CASE
												)       ,						(
												    SCREAMING_SNAKE_CASE
												)       ,						
												)							:					Any           =							config_and_inputs
												SCREAMING_SNAKE_CASE							:					Union[str, Any]           =							{'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
												return config, inputs_dict






@require_tf
class       lowercase__		(     UpperCamelCase_			,							UpperCamelCase_			,							unittest.TestCase):
						UpperCamelCase_  =	(
						    (
						        TFConvBertModel,
						        TFConvBertForMaskedLM,
						        TFConvBertForQuestionAnswering,
						        TFConvBertForSequenceClassification,
						        TFConvBertForTokenClassification,
						        TFConvBertForMultipleChoice,
						    )
						    if is_tf_available()
						    else ()
						)
						UpperCamelCase_  =	(
						    {
						        """feature-extraction""": TFConvBertModel,
						        """fill-mask""": TFConvBertForMaskedLM,
						        """question-answering""": TFConvBertForQuestionAnswering,
						        """text-classification""": TFConvBertForSequenceClassification,
						        """token-classification""": TFConvBertForTokenClassification,
						        """zero-shot""": TFConvBertForSequenceClassification,
						    }
						    if is_tf_available()
						    else {}
						)
						UpperCamelCase_  =	False
						UpperCamelCase_  =	False
						UpperCamelCase_  =	False


						def  __A ( self			:       Dict		):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:					List[Any]           =							TFConvBertModelTester(self		)
												SCREAMING_SNAKE_CASE							:					List[str]           =							ConfigTester(self					,	config_class=UpperCamelCase__					,	hidden_size=37		)


						def  __A ( self			:       Tuple		):





												'''simple docstring'''





												self.config_tester.run_common_tests()


						def  __A ( self			:       int		):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:					Union[str, Any]           =							self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_model(*UpperCamelCase__		)


						def  __A ( self			:       Optional[int]		):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:					Union[str, Any]           =							self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_for_masked_lm(*UpperCamelCase__		)


						def  __A ( self			:       Optional[Any]		):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:					List[str]           =							self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_for_multiple_choice(*UpperCamelCase__		)


						def  __A ( self			:       Tuple		):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:					Union[str, Any]           =							self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_for_question_answering(*UpperCamelCase__		)


						def  __A ( self			:       Union[str, Any]		):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:					Any           =							self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_for_sequence_classification(*UpperCamelCase__		)


						def  __A ( self			:       str		):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:					Union[str, Any]           =							self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_for_token_classification(*UpperCamelCase__		)


						@slow
						def  __A ( self			:       Any		):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE       ,						SCREAMING_SNAKE_CASE							:					Dict           =							self.model_tester.prepare_config_and_inputs_for_common()
												SCREAMING_SNAKE_CASE							:					Union[str, Any]           =							True
												SCREAMING_SNAKE_CASE							:					Any           =							True

												if hasattr(UpperCamelCase__					,	'''use_cache'''		):
																		SCREAMING_SNAKE_CASE							:					Any           =							True

												SCREAMING_SNAKE_CASE							:					int           =							getattr(self.model_tester					,	'''encoder_seq_length'''					,	self.model_tester.seq_length		)
												SCREAMING_SNAKE_CASE							:					Optional[int]           =							getattr(self.model_tester					,	'''key_length'''					,	UpperCamelCase__		)

												for model_class in self.all_model_classes:
																		SCREAMING_SNAKE_CASE							:					Any           =							self._prepare_for_class(UpperCamelCase__					,	UpperCamelCase__		)
																		SCREAMING_SNAKE_CASE							:					Tuple           =							model_class(UpperCamelCase__		)
																		SCREAMING_SNAKE_CASE							:					List[str]           =							len(model(UpperCamelCase__		)		)

																		with tempfile.TemporaryDirectory() as tmpdirname:
																								model.save_pretrained(UpperCamelCase__					,	saved_model=UpperCamelCase__		)
																								SCREAMING_SNAKE_CASE							:					Optional[int]           =							os.path.join(UpperCamelCase__					,	'''saved_model'''					,	'''1'''		)
																								SCREAMING_SNAKE_CASE							:					Union[str, Any]           =							tf.keras.models.load_model(UpperCamelCase__		)
																								SCREAMING_SNAKE_CASE							:					List[Any]           =							model(UpperCamelCase__		)

																								if self.is_encoder_decoder:
																														SCREAMING_SNAKE_CASE							:					Optional[Any]           =							outputs['''encoder_hidden_states''']
																														SCREAMING_SNAKE_CASE							:					str           =							outputs['''encoder_attentions''']
																								else:
																														SCREAMING_SNAKE_CASE							:					str           =							outputs['''hidden_states''']
																														SCREAMING_SNAKE_CASE							:					List[str]           =							outputs['''attentions''']

																								self.assertEqual(len(UpperCamelCase__		)					,	UpperCamelCase__		)

																								SCREAMING_SNAKE_CASE							:					List[str]           =							getattr(
																								    self.model_tester					,	'''expected_num_hidden_layers'''					,	self.model_tester.num_hidden_layers + 1		)

																								self.assertEqual(len(UpperCamelCase__		)					,	UpperCamelCase__		)
																								self.assertListEqual(
																								    list(output_hidden_states[0].shape[-2:]		)					,	[self.model_tester.seq_length, self.model_tester.hidden_size]					,	)

																								self.assertEqual(len(UpperCamelCase__		)					,	self.model_tester.num_hidden_layers		)
																								self.assertListEqual(
																								    list(output_attentions[0].shape[-3:]		)					,	[self.model_tester.num_attention_heads / 2, encoder_seq_length, encoder_key_length]					,	)


						@slow
						def  __A ( self			:       Optional[Any]		):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:					Union[str, Any]           =							TFConvBertModel.from_pretrained('''YituTech/conv-bert-base'''		)
												self.assertIsNotNone(UpperCamelCase__		)



						def  __A ( self			:       Optional[Any]		):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE       ,						SCREAMING_SNAKE_CASE							:					List[str]           =							self.model_tester.prepare_config_and_inputs_for_common()
												SCREAMING_SNAKE_CASE							:					str           =							True
												SCREAMING_SNAKE_CASE							:					str           =							getattr(self.model_tester					,	'''decoder_seq_length'''					,	self.model_tester.seq_length		)
												SCREAMING_SNAKE_CASE							:					Optional[int]           =							getattr(self.model_tester					,	'''encoder_seq_length'''					,	self.model_tester.seq_length		)
												SCREAMING_SNAKE_CASE							:					Union[str, Any]           =							getattr(self.model_tester					,	'''key_length'''					,	UpperCamelCase__		)
												SCREAMING_SNAKE_CASE							:					int           =							getattr(self.model_tester					,	'''key_length'''					,	UpperCamelCase__		)

												def check_decoder_attentions_output(UpperCamelCase__			:       str		):
																		SCREAMING_SNAKE_CASE							:					str           =							len(UpperCamelCase__		)
																		self.assertEqual(out_len % 2					,	0		)
																		SCREAMING_SNAKE_CASE							:					int           =							outputs.decoder_attentions
																		self.assertEqual(len(UpperCamelCase__		)					,	self.model_tester.num_hidden_layers		)
																		self.assertListEqual(
																		    list(decoder_attentions[0].shape[-3:]		)					,	[self.model_tester.num_attention_heads / 2, decoder_seq_length, decoder_key_length]					,	)

												def check_encoder_attentions_output(UpperCamelCase__			:       Optional[int]		):
																		SCREAMING_SNAKE_CASE							:					Any           =							[
																		    t.numpy() for t in (outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions)
																		]
																		self.assertEqual(len(UpperCamelCase__		)					,	self.model_tester.num_hidden_layers		)
																		self.assertListEqual(
																		    list(attentions[0].shape[-3:]		)					,	[self.model_tester.num_attention_heads / 2, encoder_seq_length, encoder_key_length]					,	)

												for model_class in self.all_model_classes:
																		SCREAMING_SNAKE_CASE							:					str           =							True
																		SCREAMING_SNAKE_CASE							:					Union[str, Any]           =							False
																		SCREAMING_SNAKE_CASE							:					Optional[int]           =							model_class(UpperCamelCase__		)
																		SCREAMING_SNAKE_CASE							:					List[str]           =							model(self._prepare_for_class(UpperCamelCase__					,	UpperCamelCase__		)		)
																		SCREAMING_SNAKE_CASE							:					Optional[int]           =							len(UpperCamelCase__		)
																		self.assertEqual(config.output_hidden_states					,	UpperCamelCase__		)
																		check_encoder_attentions_output(UpperCamelCase__		)

																		if self.is_encoder_decoder:
																								SCREAMING_SNAKE_CASE							:					int           =							model_class(UpperCamelCase__		)
																								SCREAMING_SNAKE_CASE							:					Tuple           =							model(self._prepare_for_class(UpperCamelCase__					,	UpperCamelCase__		)		)
																								self.assertEqual(config.output_hidden_states					,	UpperCamelCase__		)
																								check_decoder_attentions_output(UpperCamelCase__		)

																		# Check that output attentions can also be changed via the config
																		del inputs_dict["output_attentions"]
																		SCREAMING_SNAKE_CASE							:					str           =							True
																		SCREAMING_SNAKE_CASE							:					Union[str, Any]           =							model_class(UpperCamelCase__		)
																		SCREAMING_SNAKE_CASE							:					List[str]           =							model(self._prepare_for_class(UpperCamelCase__					,	UpperCamelCase__		)		)
																		self.assertEqual(config.output_hidden_states					,	UpperCamelCase__		)
																		check_encoder_attentions_output(UpperCamelCase__		)

																		# Check attention is always last and order is fine
																		SCREAMING_SNAKE_CASE							:					Any           =							True
																		SCREAMING_SNAKE_CASE							:					Tuple           =							True
																		SCREAMING_SNAKE_CASE							:					int           =							model_class(UpperCamelCase__		)
																		SCREAMING_SNAKE_CASE							:					Tuple           =							model(self._prepare_for_class(UpperCamelCase__					,	UpperCamelCase__		)		)

																		self.assertEqual(out_len + (2 if self.is_encoder_decoder else 1)					,	len(UpperCamelCase__		)		)
																		self.assertEqual(model.config.output_hidden_states					,	UpperCamelCase__		)
																		check_encoder_attentions_output(UpperCamelCase__		)






@require_tf
class       lowercase__		(     unittest.TestCase):


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





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:					Dict           =							TFConvBertModel.from_pretrained('''YituTech/conv-bert-base'''		)
												SCREAMING_SNAKE_CASE							:					Tuple           =							tf.constant([[0, 1, 2, 3, 4, 5]]		)
												SCREAMING_SNAKE_CASE							:					int           =							model(UpperCamelCase__		)[0]

												SCREAMING_SNAKE_CASE							:					Any           =							[1, 6, 768]
												self.assertEqual(output.shape					,	UpperCamelCase__		)

												SCREAMING_SNAKE_CASE							:					int           =							tf.constant(
												    [
												        [
												            [-0.0347_5493, -0.468_6034, -0.3063_8832],
												            [0.2263_7248, -0.2698_8646, -0.742_3424],
												            [0.1032_4868, -0.4501_3508, -0.5828_0784],
												        ]
												    ]		)
												tf.debugging.assert_near(output[:, :3, :3]					,	UpperCamelCase__					,	atol=1E-4		)






import os
import re
import warnings
from shutil import copyfile
from typing import List, Optional, Tuple

from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import is_sentencepiece_available, logging


if is_sentencepiece_available():
					from .tokenization_ta import TaTokenizer
else:
					__UpperCamelCase	: Dict    		=  None


__UpperCamelCase	: Tuple    		=  logging.get_logger(__name__)

__UpperCamelCase	: Optional[int]    		=  {'vocab_file': 'spiece.model', 'tokenizer_file': 'tokenizer.json'}

__UpperCamelCase	: Optional[int]    		=  {
    'vocab_file': {
        't5-small': 'https://huggingface.co/t5-small/resolve/main/spiece.model',
        't5-base': 'https://huggingface.co/t5-base/resolve/main/spiece.model',
        't5-large': 'https://huggingface.co/t5-large/resolve/main/spiece.model',
        't5-3b': 'https://huggingface.co/t5-3b/resolve/main/spiece.model',
        't5-11b': 'https://huggingface.co/t5-11b/resolve/main/spiece.model',
    },
    'tokenizer_file': {
        't5-small': 'https://huggingface.co/t5-small/resolve/main/tokenizer.json',
        't5-base': 'https://huggingface.co/t5-base/resolve/main/tokenizer.json',
        't5-large': 'https://huggingface.co/t5-large/resolve/main/tokenizer.json',
        't5-3b': 'https://huggingface.co/t5-3b/resolve/main/tokenizer.json',
        't5-11b': 'https://huggingface.co/t5-11b/resolve/main/tokenizer.json',
    },
}


# TODO(PVP) - this should be removed in Transformers v5
__UpperCamelCase	: Union[str, Any]    		=  {
    't5-small': 512,
    't5-base': 512,
    't5-large': 512,
    't5-3b': 512,
    't5-11b': 512,
}





class       lowercase__		(     UpperCamelCase_):
						UpperCamelCase_  =	VOCAB_FILES_NAMES
						UpperCamelCase_  =	PRETRAINED_VOCAB_FILES_MAP
						UpperCamelCase_  =	PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
						UpperCamelCase_  =	["""input_ids""", """attention_mask"""]
						UpperCamelCase_  =	TaTokenizer

						UpperCamelCase_  =	[]


						def __init__( self			:       str					,	UpperCamelCase__			:       Optional[int]=None					,	UpperCamelCase__			:       Dict=None					,	UpperCamelCase__			:       str="</s>"					,	UpperCamelCase__			:       str="<unk>"					,	UpperCamelCase__			:       Optional[int]="<pad>"					,	UpperCamelCase__			:       Optional[Any]=100					,	UpperCamelCase__			:       List[Any]=None					,	**UpperCamelCase__			:       str					,	):





												'''simple docstring'''





												if extra_ids > 0 and additional_special_tokens is None:
																		SCREAMING_SNAKE_CASE							:					List[str]           =							[f"""<extra_id_{i}>""" for i in range(UpperCamelCase__		)]
												elif extra_ids > 0 and additional_special_tokens is not None:
																		# Check that we have the right number of extra special tokens
																		SCREAMING_SNAKE_CASE							:					int           =							len(set(filter(lambda UpperCamelCase__		: bool('''extra_id_''' in str(UpperCamelCase__		)		)					,	UpperCamelCase__		)		)		)
																		if extra_tokens != extra_ids:
																								raise ValueError(
																								    f"""Both extra_ids ({extra_ids}) and additional_special_tokens ({additional_special_tokens}) are"""
																								    ''' provided to T5Tokenizer. In this case the additional_special_tokens must include the extra_ids'''
																								    ''' tokens'''		)

												super().__init__(
												    UpperCamelCase__					,	tokenizer_file=UpperCamelCase__					,	eos_token=UpperCamelCase__					,	unk_token=UpperCamelCase__					,	pad_token=UpperCamelCase__					,	extra_ids=UpperCamelCase__					,	additional_special_tokens=UpperCamelCase__					,	**UpperCamelCase__					,	)

												SCREAMING_SNAKE_CASE							:					str           =							vocab_file
												SCREAMING_SNAKE_CASE							:					int           =							False if not self.vocab_file else True
												SCREAMING_SNAKE_CASE							:					str           =							extra_ids


						@staticmethod
						def  __A ( UpperCamelCase__			:       Optional[int]					,	UpperCamelCase__			:       Optional[int]					,	UpperCamelCase__			:       Optional[Any]		):





												'''simple docstring'''





												if pretrained_model_name_or_path in TaTokenizerFast.max_model_input_sizes:
																		SCREAMING_SNAKE_CASE							:					List[str]           =							TaTokenizerFast.max_model_input_sizes[pretrained_model_name_or_path]
																		if init_max_model_length is not None and init_max_model_length != max_model_length:
																								return init_max_model_length
																		elif init_max_model_length is None:
																								warnings.warn(
																								    '''This tokenizer was incorrectly instantiated with a model max length of'''
																								    f""" {deprecated_max_model_length} which will be corrected in Transformers v5.\nFor now, this"""
																								    ''' behavior is kept to avoid breaking backwards compatibility when padding/encoding with'''
																								    ''' `truncation is True`.\n- Be aware that you SHOULD NOT rely on'''
																								    f""" {pretrained_model_name_or_path} automatically truncating your input to"""
																								    f""" {deprecated_max_model_length} when padding/encoding.\n- If you want to encode/pad to sequences"""
																								    f""" longer than {deprecated_max_model_length} you can either instantiate this tokenizer with"""
																								    ''' `model_max_length` or pass `max_length` when encoding/padding.\n- To avoid this warning, please'''
																								    ''' instantiate this tokenizer with `model_max_length` set to your preferred value.'''					,	UpperCamelCase__					,	)

												return max_model_length


						def  __A ( self			:       Tuple					,	UpperCamelCase__			:       str					,	UpperCamelCase__			:       Optional[str] = None		):





												'''simple docstring'''





												if not self.can_save_slow_tokenizer:
																		raise ValueError(
																		    '''Your fast tokenizer does not have the necessary information to save the vocabulary for a slow '''
																		    '''tokenizer.'''		)

												if not os.path.isdir(UpperCamelCase__		):
																		logger.error(f"""Vocabulary path ({save_directory}) should be a directory"""		)
																		return
												SCREAMING_SNAKE_CASE							:					Any           =							os.path.join(
												    UpperCamelCase__					,	(filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file''']		)

												if os.path.abspath(self.vocab_file		) != os.path.abspath(UpperCamelCase__		):
																		copyfile(self.vocab_file					,	UpperCamelCase__		)
																		logger.info(f"""Copy vocab file to {out_vocab_file}"""		)

												return (out_vocab_file,)


						def  __A ( self			:       Optional[Any]					,	UpperCamelCase__			:       List[int]					,	UpperCamelCase__			:       Optional[List[int]] = None		):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:					Optional[int]           =							token_ids_a + [self.eos_token_id]
												if token_ids_a is None:
																		return self.prefix_tokens + token_ids_a
												else:
																		SCREAMING_SNAKE_CASE							:					Tuple           =							token_ids_a + [self.eos_token_id]
																		return self.prefix_tokens + token_ids_a + token_ids_a


						def  __A ( self			:       Any					,	UpperCamelCase__			:       List[int]					,	UpperCamelCase__			:       Optional[List[int]] = None		):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:					Union[str, Any]           =							[self.eos_token_id]

												if token_ids_a is None:
																		return len(token_ids_a + eos		) * [0]
												return len(token_ids_a + eos + token_ids_a + eos		) * [0]


						def  __A ( self			:       Dict		):





												'''simple docstring'''





												return list(
												    set(filter(lambda UpperCamelCase__		: bool(re.search(r'''<extra_id_\d+>'''					,	UpperCamelCase__		)		) is not None					,	self.additional_special_tokens		)		)		)



						def  __A ( self			:       List[Any]		):





												'''simple docstring'''





												return [self.convert_tokens_to_ids(UpperCamelCase__		) for token in self.get_sentinel_tokens()]





