






import json
import os
from typing import Dict, List, Optional, Tuple

from ...tokenization_utils import PreTrainedTokenizer
from ...utils import logging


lowerCamelCase : List[Any]						   =       logging.get_logger(__name__)


lowerCamelCase : Tuple						   =       {
    '''vocab_file''': '''vocab.json''',
    '''tokenizer_config_file''': '''tokenizer_config.json''',
    '''merges_file''': '''merges.txt''',
}

lowerCamelCase : Any						   =       {
    '''vocab_file''': {
        '''facebook/s2t-wav2vec2-large-en-de''': (
            '''https://huggingface.co/facebook/s2t-wav2vec2-large-en-de/resolve/main/vocab.json'''
        ),
    },
    '''tokenizer_config_file''': {
        '''facebook/s2t-wav2vec2-large-en-de''': (
            '''https://huggingface.co/facebook/s2t-wav2vec2-large-en-de/resolve/main/tokenizer_config.json'''
        ),
    },
    '''merges_file''': {
        '''facebook/s2t-wav2vec2-large-en-de''': (
            '''https://huggingface.co/facebook/s2t-wav2vec2-large-en-de/resolve/main/merges.txt'''
        ),
    },
}

lowerCamelCase : Any						   =       '''</w>'''
lowerCamelCase : Optional[int]						   =       '''@@ '''
def       snake_case_		(     lowerCAmelCase_       :						Dict      ):
							__lowercase      :							Optional[Any]				=     set()
							__lowercase      :							Optional[Any]				=     word[0]
							for char in word[1:]:
														pairs.add((prev_char, char)      )
														__lowercase      :							int				=     char
							return pairs


# Speech2Text2 has no max input length
lowerCamelCase : Optional[int]						   =       {'''facebook/s2t-wav2vec2-large-en-de''': 10_24}



class 		lowerCAmelCase     ( __a			):

					'''simple docstring'''

					_A						:			Dict									=      VOCAB_FILES_NAMES
					_A						:			str									=      PRETRAINED_VOCAB_FILES_MAP
					_A						:			Optional[int]									=      PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
					_A						:			Optional[int]									=      ['''input_ids''', '''attention_mask''']



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


												"""simple docstring"""

												super().__init__(
												    unk_token=__a		,     bos_token=__a		,     eos_token=__a		,     pad_token=__a		,     do_lower_case=__a		,     **__a		,     )

												__lowercase      :							int				=     do_lower_case

												with open(__a		,     encoding="""utf-8"""							) as vocab_handle:
																			__lowercase      :							List[Any]				=     json.load(__a							)
												__lowercase      :							Union[str, Any]				=     {v: k for k, v in self.encoder.items()}

												if merges_file is None:
																			logger.info(F"No merges files provided. {self.__class__.__name__} can only be used for decoding."							)

																			__lowercase      :							int				=     None
																			__lowercase      :							Union[str, Any]				=     None
												else:
																			with open(__a		,     encoding="""utf-8"""							) as merges_handle:
																										__lowercase      :							List[str]				=     merges_handle.read().split("""\n"""							)[:-1]

																			__lowercase      :							Union[str, Any]				=     [tuple(merge.split()[:2]							) for merge in merges]
																			__lowercase      :							List[str]				=     dict(zip(__a		,     range(len(__a							)							)							)							)
																			__lowercase      :							Union[str, Any]				=     {}



					@property
					def       lowerCAmelCase				(  self				:	Tuple							)			->  int:


												"""simple docstring"""

												return len(self.decoder							)



					def       lowerCAmelCase				(  self				:	str							)			->  Dict:


												"""simple docstring"""

												return dict(self.encoder		,     **self.added_tokens_encoder							)



					def       lowerCAmelCase				(  self				:	Tuple		,     __a				:	Any							)			->  Dict:


												"""simple docstring"""

												__lowercase      :							List[Any]				=     tuple(token[:-1]							) + (token[-1] + BPE_TOKEN_MERGES,)
												if token in self.cache:
																			return self.cache[token]
												__lowercase      :							Dict				=     get_pairs(__a							)

												if not pairs:
																			return token

												while True:
																			__lowercase      :							Any				=     min(__a		,     key=lambda __a							: self.bpe_ranks.get(__a		,     float("""inf"""							)							)							)
																			if bigram not in self.bpe_ranks:
																										break
																			__lowercase      :							Any				=     bigram
																			__lowercase      :							Dict				=     []
																			__lowercase      :							List[str]				=     0
																			while i < len(__a							):
																										try:
																																	__lowercase      :							Tuple				=     word.index(__a		,     __a							)
																										except ValueError:
																																	new_word.extend(word[i:]							)
																																	break
																										else:
																																	new_word.extend(word[i:j]							)
																																	__lowercase      :							str				=     j

																										if word[i] == first and i < len(__a							) - 1 and word[i + 1] == second:
																																	new_word.append(first + second							)
																																	i += 2
																										else:
																																	new_word.append(word[i]							)
																																	i += 1
																			__lowercase      :							Dict				=     tuple(__a							)
																			__lowercase      :							List[str]				=     new_word
																			if len(__a							) == 1:
																										break
																			else:
																										__lowercase      :							str				=     get_pairs(__a							)
												__lowercase      :							List[str]				=     """ """.join(__a							)
												if word == "\n  " + BPE_TOKEN_MERGES:
																			__lowercase      :							Tuple				=     """\n""" + BPE_TOKEN_MERGES

												if word.endswith(__a							):
																			__lowercase      :							List[Any]				=     word.replace(__a		,     """"""							)

												__lowercase      :							Union[str, Any]				=     word.replace(""" """		,     __a							)
												__lowercase      :							Any				=     word
												return word



					def       lowerCAmelCase				(  self				:	Optional[Any]		,     __a				:	Optional[Any]							)			->  Union[str, Any]:


												"""simple docstring"""

												if self.bpe_ranks is None:
																			raise ValueError(
																			    """This tokenizer was instantiated without a `merges.txt` file, so"""
																			    """ that it can only be used for decoding, not for encoding."""
																			    """Make sure to provide `merges.txt` file at instantiation to enable """
																			    """encoding."""							)

												if self.do_lower_case:
																			__lowercase      :							Union[str, Any]				=     text.lower()

												__lowercase      :							Union[str, Any]				=     text.split()

												__lowercase      :							Optional[Any]				=     []
												for token in text:
																			if token:
																										split_tokens.extend(list(self.bpe(__a							).split(""" """							)							)							)

												return split_tokens



					def       lowerCAmelCase				(  self				:	Dict		,     __a				:	str							)			->  int:


												"""simple docstring"""

												return self.encoder.get(__a		,     self.encoder.get(self.unk_token							)							)



					def       lowerCAmelCase				(  self				:	Tuple		,     __a				:	int							)			->  str:


												"""simple docstring"""

												__lowercase      :							str				=     self.decoder.get(__a		,     self.unk_token							)
												return result



					def       lowerCAmelCase				(  self				:	Union[str, Any]		,     __a				:	List[str]							)			->  str:


												"""simple docstring"""

												__lowercase      :							Optional[Any]				=     """ """.join(__a							)

												# make sure @@ tokens are concatenated
												__lowercase      :							Union[str, Any]				=     """""".join(string.split(__a							)							)

												return string



					def       lowerCAmelCase				(  self				:	int		,     __a				:	str		,     __a				:	Optional[str] = None							)			->  Tuple[str]:


												"""simple docstring"""

												if not os.path.isdir(__a							):
																			logger.error(F"Vocabulary path ({save_directory}) should be a directory"							)
																			return
												__lowercase      :							Tuple				=     os.path.join(
												    __a		,     (filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""vocab_file"""]							)
												__lowercase      :							Optional[Any]				=     os.path.join(
												    __a		,     (filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""merges_file"""]							)

												with open(__a		,     """w"""		,     encoding="""utf-8"""							) as f:
																			f.write(json.dumps(self.encoder		,     indent=2		,     sort_keys=__a		,     ensure_ascii=__a							) + """\n"""							)

												__lowercase      :							Dict				=     0
												if self.bpe_ranks is None:
																			return (vocab_file,)

												with open(__a		,     """w"""		,     encoding="""utf-8"""							) as writer:
																			for bpe_tokens, token_index in sorted(self.bpe_ranks.items()		,     key=lambda __a							: kv[1]							):
																										if index != token_index:
																																	logger.warning(
																																	    F"Saving vocabulary to {merges_file}: BPE merge indices are not consecutive."
																																	    """ Please check that the tokenizer is not corrupted!"""							)
																																	__lowercase      :							str				=     token_index
																										writer.write(""" """.join(__a							) + """\n"""							)
																										index += 1

												return (vocab_file, merges_file)







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

					'''simple docstring'''

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


												"""simple docstring"""

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



					def       lowerCAmelCase				(  self				:	str							)			->  List[Any]:


												"""simple docstring"""

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

												__lowercase      :							Optional[Any]				=     None
												if self.use_input_mask:
																			__lowercase      :							Dict				=     random_attention_mask([self.batch_size, self.seq_length]							)

												__lowercase      :							Dict				=     None
												if self.use_token_type_ids:
																			__lowercase      :							Dict				=     ids_tensor([self.batch_size, self.seq_length]		,     self.type_vocab_size							)

												__lowercase      :							Optional[Any]				=     None
												__lowercase      :							str				=     None
												__lowercase      :							Tuple				=     None
												if self.use_labels:
																			__lowercase      :							List[str]				=     ids_tensor([self.batch_size]		,     self.type_sequence_label_size							)
																			__lowercase      :							List[Any]				=     ids_tensor([self.batch_size, self.seq_length]		,     self.num_labels							)
																			__lowercase      :							str				=     ids_tensor([self.batch_size]		,     self.num_choices							)

												__lowercase      :							Optional[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=__a		,     )

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



					def       lowerCAmelCase				(  self				:	Dict		,     __a				:	List[Any]		,     __a				:	List[str]		,     __a				:	Union[str, Any]		,     __a				:	str		,     __a				:	Union[str, Any]		,     __a				:	Tuple		,     __a				:	Tuple							)			->  Dict:


												"""simple docstring"""

												__lowercase      :							Dict				=     TFConvBertModel(config=__a							)
												__lowercase      :							Tuple				=     {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}

												__lowercase      :							Any				=     [input_ids, input_mask]
												__lowercase      :							Dict				=     model(__a							)

												__lowercase      :							str				=     model(__a							)

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



					def       lowerCAmelCase				(  self				:	Tuple		,     __a				:	Union[str, Any]		,     __a				:	Any		,     __a				:	Tuple		,     __a				:	Union[str, Any]		,     __a				:	str		,     __a				:	Dict		,     __a				:	str							)			->  Dict:


												"""simple docstring"""

												__lowercase      :							Optional[int]				=     TFConvBertForMaskedLM(config=__a							)
												__lowercase      :							List[Any]				=     {
												    """input_ids""": input_ids,
												    """attention_mask""": input_mask,
												    """token_type_ids""": token_type_ids,
												}
												__lowercase      :							Any				=     model(__a							)
												self.parent.assertEqual(result.logits.shape		,     (self.batch_size, self.seq_length, self.vocab_size)							)



					def       lowerCAmelCase				(  self				:	Optional[int]		,     __a				:	int		,     __a				:	Any		,     __a				:	Optional[int]		,     __a				:	int		,     __a				:	int		,     __a				:	List[Any]		,     __a				:	Optional[int]							)			->  List[Any]:


												"""simple docstring"""

												__lowercase      :							str				=     self.num_labels
												__lowercase      :							List[Any]				=     TFConvBertForSequenceClassification(config=__a							)
												__lowercase      :							int				=     {
												    """input_ids""": input_ids,
												    """attention_mask""": input_mask,
												    """token_type_ids""": token_type_ids,
												}

												__lowercase      :							List[str]				=     model(__a							)
												self.parent.assertEqual(result.logits.shape		,     (self.batch_size, self.num_labels)							)



					def       lowerCAmelCase				(  self				:	Optional[int]		,     __a				:	Any		,     __a				:	Optional[Any]		,     __a				:	int		,     __a				:	Optional[int]		,     __a				:	Tuple		,     __a				:	int		,     __a				:	int							)			->  Dict:


												"""simple docstring"""

												__lowercase      :							Tuple				=     self.num_choices
												__lowercase      :							Dict				=     TFConvBertForMultipleChoice(config=__a							)
												__lowercase      :							List[str]				=     tf.tile(tf.expand_dims(__a		,     1							)		,     (1, self.num_choices, 1)							)
												__lowercase      :							int				=     tf.tile(tf.expand_dims(__a		,     1							)		,     (1, self.num_choices, 1)							)
												__lowercase      :							str				=     tf.tile(tf.expand_dims(__a		,     1							)		,     (1, self.num_choices, 1)							)
												__lowercase      :							str				=     {
												    """input_ids""": multiple_choice_inputs_ids,
												    """attention_mask""": multiple_choice_input_mask,
												    """token_type_ids""": multiple_choice_token_type_ids,
												}
												__lowercase      :							Dict				=     model(__a							)
												self.parent.assertEqual(result.logits.shape		,     (self.batch_size, self.num_choices)							)



					def       lowerCAmelCase				(  self				:	List[str]		,     __a				:	str		,     __a				:	List[str]		,     __a				:	List[str]		,     __a				:	List[str]		,     __a				:	Any		,     __a				:	Tuple		,     __a				:	Union[str, Any]							)			->  Optional[int]:


												"""simple docstring"""

												__lowercase      :							Tuple				=     self.num_labels
												__lowercase      :							Tuple				=     TFConvBertForTokenClassification(config=__a							)
												__lowercase      :							Dict				=     {
												    """input_ids""": input_ids,
												    """attention_mask""": input_mask,
												    """token_type_ids""": token_type_ids,
												}
												__lowercase      :							str				=     model(__a							)
												self.parent.assertEqual(result.logits.shape		,     (self.batch_size, self.seq_length, self.num_labels)							)



					def       lowerCAmelCase				(  self				:	List[Any]		,     __a				:	Optional[int]		,     __a				:	List[str]		,     __a				:	Optional[Any]		,     __a				:	int		,     __a				:	Tuple		,     __a				:	Any		,     __a				:	Union[str, Any]							)			->  List[Any]:


												"""simple docstring"""

												__lowercase      :							Any				=     TFConvBertForQuestionAnswering(config=__a							)
												__lowercase      :							str				=     {
												    """input_ids""": input_ids,
												    """attention_mask""": input_mask,
												    """token_type_ids""": token_type_ids,
												}

												__lowercase      :							List[Any]				=     model(__a							)
												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       lowerCAmelCase				(  self				:	Tuple							)			->  Tuple:


												"""simple docstring"""

												__lowercase      :							Tuple				=     self.prepare_config_and_inputs()
												(
												    (
												    __lowercase
												)			,			(
												    __lowercase
												)			,			(
												    __lowercase
												)			,			(
												    __lowercase
												)			,			(
												    __lowercase
												)			,			(
												    __lowercase
												)			,			(
												    __lowercase
												)			,			
												)      :							int				=     config_and_inputs
												__lowercase      :							Union[str, Any]				=     {"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": input_mask}
												return config, inputs_dict







@require_tf
class 		lowerCAmelCase     ( __a      ,       __a      ,       unittest.TestCase			):

					'''simple docstring'''

					_A						:			Dict									=      (
					    (
					        TFConvBertModel,
					        TFConvBertForMaskedLM,
					        TFConvBertForQuestionAnswering,
					        TFConvBertForSequenceClassification,
					        TFConvBertForTokenClassification,
					        TFConvBertForMultipleChoice,
					    )
					    if is_tf_available()
					    else ()
					)
					_A						:			str									=      (
					    {
					        '''feature-extraction''': TFConvBertModel,
					        '''fill-mask''': TFConvBertForMaskedLM,
					        '''question-answering''': TFConvBertForQuestionAnswering,
					        '''text-classification''': TFConvBertForSequenceClassification,
					        '''token-classification''': TFConvBertForTokenClassification,
					        '''zero-shot''': TFConvBertForSequenceClassification,
					    }
					    if is_tf_available()
					    else {}
					)
					_A						:			Union[str, Any]									=      False
					_A						:			List[str]									=      False
					_A						:			Dict									=      False



					def       lowerCAmelCase				(  self				:	List[str]							)			->  int:


												"""simple docstring"""

												__lowercase      :							int				=     TFConvBertModelTester(self							)
												__lowercase      :							Tuple				=     ConfigTester(self		,     config_class=__a		,     hidden_size=37							)



					def       lowerCAmelCase				(  self				:	Optional[int]							)			->  Optional[Any]:


												"""simple docstring"""

												self.config_tester.run_common_tests()



					def       lowerCAmelCase				(  self				:	Optional[Any]							)			->  Union[str, Any]:


												"""simple docstring"""

												__lowercase      :							Optional[Any]				=     self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_model(*__a							)



					def       lowerCAmelCase				(  self				:	Dict							)			->  Union[str, Any]:


												"""simple docstring"""

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



					def       lowerCAmelCase				(  self				:	Union[str, Any]							)			->  Union[str, Any]:


												"""simple docstring"""

												__lowercase      :							int				=     self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_for_multiple_choice(*__a							)



					def       lowerCAmelCase				(  self				:	str							)			->  Any:


												"""simple docstring"""

												__lowercase      :							List[str]				=     self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_for_question_answering(*__a							)



					def       lowerCAmelCase				(  self				:	str							)			->  str:


												"""simple docstring"""

												__lowercase      :							List[Any]				=     self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_for_sequence_classification(*__a							)



					def       lowerCAmelCase				(  self				:	str							)			->  Any:


												"""simple docstring"""

												__lowercase      :							Optional[int]				=     self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_for_token_classification(*__a							)



					@slow
					def       lowerCAmelCase				(  self				:	str							)			->  Any:


												"""simple docstring"""

												__lowercase			,			__lowercase      :							Optional[int]				=     self.model_tester.prepare_config_and_inputs_for_common()
												__lowercase      :							Union[str, Any]				=     True
												__lowercase      :							List[Any]				=     True

												if hasattr(__a		,     """use_cache"""							):
																			__lowercase      :							Optional[Any]				=     True

												__lowercase      :							List[str]				=     getattr(self.model_tester		,     """encoder_seq_length"""		,     self.model_tester.seq_length							)
												__lowercase      :							int				=     getattr(self.model_tester		,     """key_length"""		,     __a							)

												for model_class in self.all_model_classes:
																			__lowercase      :							Optional[Any]				=     self._prepare_for_class(__a		,     __a							)
																			__lowercase      :							Tuple				=     model_class(__a							)
																			__lowercase      :							Tuple				=     len(model(__a							)							)

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

																										if self.is_encoder_decoder:
																																	__lowercase      :							Union[str, Any]				=     outputs["""encoder_hidden_states"""]
																																	__lowercase      :							Union[str, Any]				=     outputs["""encoder_attentions"""]
																										else:
																																	__lowercase      :							Union[str, Any]				=     outputs["""hidden_states"""]
																																	__lowercase      :							List[str]				=     outputs["""attentions"""]

																										self.assertEqual(len(__a							)		,     __a							)

																										__lowercase      :							List[Any]				=     getattr(
																										    self.model_tester		,     """expected_num_hidden_layers"""		,     self.model_tester.num_hidden_layers + 1							)

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

																										self.assertEqual(len(__a							)		,     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       lowerCAmelCase				(  self				:	Union[str, Any]							)			->  List[Any]:


												"""simple docstring"""

												__lowercase      :							str				=     TFConvBertModel.from_pretrained("""YituTech/conv-bert-base"""							)
												self.assertIsNotNone(__a							)



					def       lowerCAmelCase				(  self				:	Any							)			->  List[Any]:


												"""simple docstring"""

												__lowercase			,			__lowercase      :							int				=     self.model_tester.prepare_config_and_inputs_for_common()
												__lowercase      :							List[str]				=     True
												__lowercase      :							List[Any]				=     getattr(self.model_tester		,     """decoder_seq_length"""		,     self.model_tester.seq_length							)
												__lowercase      :							Optional[int]				=     getattr(self.model_tester		,     """encoder_seq_length"""		,     self.model_tester.seq_length							)
												__lowercase      :							List[str]				=     getattr(self.model_tester		,     """key_length"""		,     __a							)
												__lowercase      :							List[Any]				=     getattr(self.model_tester		,     """key_length"""		,     __a							)

												def check_decoder_attentions_output(__a				:	List[str]							):
																			__lowercase      :							Union[str, Any]				=     len(__a							)
																			self.assertEqual(out_len % 2		,     0							)
																			__lowercase      :							Any				=     outputs.decoder_attentions
																			self.assertEqual(len(__a							)		,     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(__a				:	str							):
																			__lowercase      :							str				=     [
																			    t.numpy() for t in (outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions)
																			]
																			self.assertEqual(len(__a							)		,     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:
																			__lowercase      :							int				=     True
																			__lowercase      :							Any				=     False
																			__lowercase      :							List[Any]				=     model_class(__a							)
																			__lowercase      :							Tuple				=     model(self._prepare_for_class(__a		,     __a							)							)
																			__lowercase      :							Dict				=     len(__a							)
																			self.assertEqual(config.output_hidden_states		,     __a							)
																			check_encoder_attentions_output(__a							)

																			if self.is_encoder_decoder:
																										__lowercase      :							Any				=     model_class(__a							)
																										__lowercase      :							List[str]				=     model(self._prepare_for_class(__a		,     __a							)							)
																										self.assertEqual(config.output_hidden_states		,     __a							)
																										check_decoder_attentions_output(__a							)

																			# Check that output attentions can also be changed via the config
																			del inputs_dict["output_attentions"]
																			__lowercase      :							Dict				=     True
																			__lowercase      :							Optional[Any]				=     model_class(__a							)
																			__lowercase      :							Optional[int]				=     model(self._prepare_for_class(__a		,     __a							)							)
																			self.assertEqual(config.output_hidden_states		,     __a							)
																			check_encoder_attentions_output(__a							)

																			# Check attention is always last and order is fine
																			__lowercase      :							List[str]				=     True
																			__lowercase      :							List[Any]				=     True
																			__lowercase      :							Any				=     model_class(__a							)
																			__lowercase      :							Optional[int]				=     model(self._prepare_for_class(__a		,     __a							)							)

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







@require_tf
class 		lowerCAmelCase     ( unittest.TestCase			):

					'''simple docstring'''

					@slow
					def       lowerCAmelCase				(  self				:	Dict							)			->  Union[str, Any]:


												"""simple docstring"""

												__lowercase      :							List[str]				=     TFConvBertModel.from_pretrained("""YituTech/conv-bert-base"""							)
												__lowercase      :							str				=     tf.constant([[0, 1, 2, 3, 4, 5]]							)
												__lowercase      :							Tuple				=     model(__a							)[0]

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

												__lowercase      :							Optional[Any]				=     tf.constant(
												    [
												        [
												            [-0.03475493, -0.4686034, -0.30638832],
												            [0.22637248, -0.26988646, -0.7423424],
												            [0.10324868, -0.45013508, -0.58280784],
												        ]
												    ]							)
												tf.debugging.assert_near(output[:, :3, :3]		,     __a		,     atol=1E-4							)