



'''simple docstring'''


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







	def __init__(     self			,					_lowerCAmelCase			,					_lowerCAmelCase=13			,					_lowerCAmelCase=7			,					_lowerCAmelCase=True			,					_lowerCAmelCase=True			,					_lowerCAmelCase=True			,					_lowerCAmelCase=True			,					_lowerCAmelCase=99			,					_lowerCAmelCase=32			,					_lowerCAmelCase=2			,					_lowerCAmelCase=4			,					_lowerCAmelCase=37			,					_lowerCAmelCase="gelu"			,					_lowerCAmelCase=0.1			,					_lowerCAmelCase=0.1			,					_lowerCAmelCase=512			,					_lowerCAmelCase=16			,					_lowerCAmelCase=2			,					_lowerCAmelCase=0.02			,					_lowerCAmelCase=3			,					_lowerCAmelCase=4			,					_lowerCAmelCase=None			,					)     ->     Any:
							'''simple docstring'''






							lowercase   =				parent
							lowercase   =				13
							lowercase   =				7
							lowercase   =				True
							lowercase   =				True
							lowercase   =				True
							lowercase   =				True
							lowercase   =				99
							lowercase   =				384
							lowercase   =				2
							lowercase   =				4
							lowercase   =				37
							lowercase   =				"""gelu"""
							lowercase   =				0.1
							lowercase   =				0.1
							lowercase   =				512
							lowercase   =				16
							lowercase   =				2
							lowercase   =				0.02
							lowercase   =				3
							lowercase   =				4
							lowercase   =				128
							lowercase   =				2
							lowercase   =				9
							lowercase   =				1
							lowercase   =				None







	def 	_a (     self    )     ->     Dict:
							'''simple docstring'''






							lowercase   =				ids_tensor([self.batch_size, self.seq_length]			,					self.vocab_size    )

							lowercase   =				None
							if self.use_input_mask:
													lowercase   =				random_attention_mask([self.batch_size, self.seq_length]    )

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

							lowercase   =				None
							lowercase   =				None
							lowercase   =				None
							if self.use_labels:
													lowercase   =				ids_tensor([self.batch_size]			,					self.type_sequence_label_size    )
													lowercase   =				ids_tensor([self.batch_size, self.seq_length]			,					self.num_labels    )
													lowercase   =				ids_tensor([self.batch_size]			,					self.num_choices    )

							lowercase   =				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=lowercase_			,					)

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







	def 	_a (     self			,					_lowerCAmelCase			,					_lowerCAmelCase			,					_lowerCAmelCase			,					_lowerCAmelCase			,					_lowerCAmelCase			,					_lowerCAmelCase			,					_lowerCAmelCase    )     ->     List[Any]:
							'''simple docstring'''






							lowercase   =				TFConvBertModel(config=lowercase_    )
							lowercase   =				{"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}

							lowercase   =				[input_ids, input_mask]
							lowercase   =				model(lowercase_    )

							lowercase   =				model(lowercase_    )

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







	def 	_a (     self			,					_lowerCAmelCase			,					_lowerCAmelCase			,					_lowerCAmelCase			,					_lowerCAmelCase			,					_lowerCAmelCase			,					_lowerCAmelCase			,					_lowerCAmelCase    )     ->     List[Any]:
							'''simple docstring'''






							lowercase   =				TFConvBertForMaskedLM(config=lowercase_    )
							lowercase   =				{
							    """input_ids""": input_ids,
							    """attention_mask""": input_mask,
							    """token_type_ids""": token_type_ids,
							}
							lowercase   =				model(lowercase_    )
							self.parent.assertEqual(result.logits.shape			,					(self.batch_size, self.seq_length, self.vocab_size)    )







	def 	_a (     self			,					_lowerCAmelCase			,					_lowerCAmelCase			,					_lowerCAmelCase			,					_lowerCAmelCase			,					_lowerCAmelCase			,					_lowerCAmelCase			,					_lowerCAmelCase    )     ->     Any:
							'''simple docstring'''






							lowercase   =				self.num_labels
							lowercase   =				TFConvBertForSequenceClassification(config=lowercase_    )
							lowercase   =				{
							    """input_ids""": input_ids,
							    """attention_mask""": input_mask,
							    """token_type_ids""": token_type_ids,
							}

							lowercase   =				model(lowercase_    )
							self.parent.assertEqual(result.logits.shape			,					(self.batch_size, self.num_labels)    )







	def 	_a (     self			,					_lowerCAmelCase			,					_lowerCAmelCase			,					_lowerCAmelCase			,					_lowerCAmelCase			,					_lowerCAmelCase			,					_lowerCAmelCase			,					_lowerCAmelCase    )     ->     List[Any]:
							'''simple docstring'''






							lowercase   =				self.num_choices
							lowercase   =				TFConvBertForMultipleChoice(config=lowercase_    )
							lowercase   =				tf.tile(tf.expand_dims(lowercase_			,					1    )			,					(1, self.num_choices, 1)    )
							lowercase   =				tf.tile(tf.expand_dims(lowercase_			,					1    )			,					(1, self.num_choices, 1)    )
							lowercase   =				tf.tile(tf.expand_dims(lowercase_			,					1    )			,					(1, self.num_choices, 1)    )
							lowercase   =				{
							    """input_ids""": multiple_choice_inputs_ids,
							    """attention_mask""": multiple_choice_input_mask,
							    """token_type_ids""": multiple_choice_token_type_ids,
							}
							lowercase   =				model(lowercase_    )
							self.parent.assertEqual(result.logits.shape			,					(self.batch_size, self.num_choices)    )







	def 	_a (     self			,					_lowerCAmelCase			,					_lowerCAmelCase			,					_lowerCAmelCase			,					_lowerCAmelCase			,					_lowerCAmelCase			,					_lowerCAmelCase			,					_lowerCAmelCase    )     ->     List[str]:
							'''simple docstring'''






							lowercase   =				self.num_labels
							lowercase   =				TFConvBertForTokenClassification(config=lowercase_    )
							lowercase   =				{
							    """input_ids""": input_ids,
							    """attention_mask""": input_mask,
							    """token_type_ids""": token_type_ids,
							}
							lowercase   =				model(lowercase_    )
							self.parent.assertEqual(result.logits.shape			,					(self.batch_size, self.seq_length, self.num_labels)    )







	def 	_a (     self			,					_lowerCAmelCase			,					_lowerCAmelCase			,					_lowerCAmelCase			,					_lowerCAmelCase			,					_lowerCAmelCase			,					_lowerCAmelCase			,					_lowerCAmelCase    )     ->     List[str]:
							'''simple docstring'''






							lowercase   =				TFConvBertForQuestionAnswering(config=lowercase_    )
							lowercase   =				{
							    """input_ids""": input_ids,
							    """attention_mask""": input_mask,
							    """token_type_ids""": token_type_ids,
							}

							lowercase   =				model(lowercase_    )
							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    )     ->     Optional[Any]:
							'''simple docstring'''






							lowercase   =				self.prepare_config_and_inputs()
							(
							    (
							    lowercase     
							)  , (
							    lowercase     
							)  , (
							    lowercase     
							)  , (
							    lowercase     
							)  , (
							    lowercase     
							)  , (
							    lowercase     
							)  , (
							    lowercase     
							)  ,      
							)   =				config_and_inputs
							lowercase   =				{"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": input_mask}
							return config, inputs_dict




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







	def 	_a (     self    )     ->     str:
							'''simple docstring'''






							lowercase   =				TFConvBertModelTester(self    )
							lowercase   =				ConfigTester(self			,					config_class=lowercase_			,					hidden_size=37    )







	def 	_a (     self    )     ->     int:
							'''simple docstring'''






							self.config_tester.run_common_tests()







	def 	_a (     self    )     ->     List[Any]:
							'''simple docstring'''






							lowercase   =				self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*lowercase_    )







	def 	_a (     self    )     ->     Optional[int]:
							'''simple docstring'''






							lowercase   =				self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_masked_lm(*lowercase_    )







	def 	_a (     self    )     ->     int:
							'''simple docstring'''






							lowercase   =				self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_multiple_choice(*lowercase_    )







	def 	_a (     self    )     ->     Tuple:
							'''simple docstring'''






							lowercase   =				self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_question_answering(*lowercase_    )







	def 	_a (     self    )     ->     Union[str, Any]:
							'''simple docstring'''






							lowercase   =				self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_sequence_classification(*lowercase_    )







	def 	_a (     self    )     ->     List[str]:
							'''simple docstring'''






							lowercase   =				self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_token_classification(*lowercase_    )







	@slow
	def 	_a (     self    )     ->     Tuple:
							'''simple docstring'''






							lowercase  , lowercase   =				self.model_tester.prepare_config_and_inputs_for_common()
							lowercase   =				True
							lowercase   =				True

							if hasattr(lowercase_			,					"""use_cache"""    ):
													lowercase   =				True

							lowercase   =				getattr(self.model_tester			,					"""encoder_seq_length"""			,					self.model_tester.seq_length    )
							lowercase   =				getattr(self.model_tester			,					"""key_length"""			,					lowercase_    )

							for model_class in self.all_model_classes:
													lowercase   =				self._prepare_for_class(lowercase_			,					lowercase_    )
													lowercase   =				model_class(lowercase_    )
													lowercase   =				len(model(lowercase_    )    )

													with tempfile.TemporaryDirectory() as tmpdirname:
																			model.save_pretrained(lowercase_			,					saved_model=lowercase_    )
																			lowercase   =				os.path.join(lowercase_			,					"""saved_model"""			,					"""1"""    )
																			lowercase   =				tf.keras.models.load_model(lowercase_    )
																			lowercase   =				model(lowercase_    )

																			if self.is_encoder_decoder:
																									lowercase   =				outputs["""encoder_hidden_states"""]
																									lowercase   =				outputs["""encoder_attentions"""]
																			else:
																									lowercase   =				outputs["""hidden_states"""]
																									lowercase   =				outputs["""attentions"""]

																			self.assertEqual(len(lowercase_    )			,					lowercase_    )

																			lowercase   =				getattr(
																			    self.model_tester			,					"""expected_num_hidden_layers"""			,					self.model_tester.num_hidden_layers + 1    )

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

																			self.assertEqual(len(lowercase_    )			,					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    )     ->     List[Any]:
							'''simple docstring'''






							lowercase   =				TFConvBertModel.from_pretrained("""YituTech/conv-bert-base"""    )
							self.assertIsNotNone(lowercase_    )







	def 	_a (     self    )     ->     Optional[int]:
							'''simple docstring'''






							lowercase  , lowercase   =				self.model_tester.prepare_config_and_inputs_for_common()
							lowercase   =				True
							lowercase   =				getattr(self.model_tester			,					"""decoder_seq_length"""			,					self.model_tester.seq_length    )
							lowercase   =				getattr(self.model_tester			,					"""encoder_seq_length"""			,					self.model_tester.seq_length    )
							lowercase   =				getattr(self.model_tester			,					"""key_length"""			,					lowercase_    )
							lowercase   =				getattr(self.model_tester			,					"""key_length"""			,					lowercase_    )

							def check_decoder_attentions_output(_lowerCAmelCase    ):
													lowercase   =				len(lowercase_    )
													self.assertEqual(out_len % 2			,					0    )
													lowercase   =				outputs.decoder_attentions
													self.assertEqual(len(lowercase_    )			,					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(_lowerCAmelCase    ):
													lowercase   =				[
													    t.numpy() for t in (outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions)
													]
													self.assertEqual(len(lowercase_    )			,					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   =				True
													lowercase   =				False
													lowercase   =				model_class(lowercase_    )
													lowercase   =				model(self._prepare_for_class(lowercase_			,					lowercase_    )    )
													lowercase   =				len(lowercase_    )
													self.assertEqual(config.output_hidden_states			,					lowercase_    )
													check_encoder_attentions_output(lowercase_    )

													if self.is_encoder_decoder:
																			lowercase   =				model_class(lowercase_    )
																			lowercase   =				model(self._prepare_for_class(lowercase_			,					lowercase_    )    )
																			self.assertEqual(config.output_hidden_states			,					lowercase_    )
																			check_decoder_attentions_output(lowercase_    )

													# Check that output attentions can also be changed via the config
													del inputs_dict["output_attentions"]
													lowercase   =				True
													lowercase   =				model_class(lowercase_    )
													lowercase   =				model(self._prepare_for_class(lowercase_			,					lowercase_    )    )
													self.assertEqual(config.output_hidden_states			,					lowercase_    )
													check_encoder_attentions_output(lowercase_    )

													# Check attention is always last and order is fine
													lowercase   =				True
													lowercase   =				True
													lowercase   =				model_class(lowercase_    )
													lowercase   =				model(self._prepare_for_class(lowercase_			,					lowercase_    )    )

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




@require_tf
class   __UpperCamelCase							(unittest.TestCase      ):







	@slow
	def 	_a (     self    )     ->     Optional[int]:
							'''simple docstring'''






							lowercase   =				TFConvBertModel.from_pretrained("""YituTech/conv-bert-base"""    )
							lowercase   =				tf.constant([[0, 1, 2, 3, 4, 5]]    )
							lowercase   =				model(lowercase_    )[0]

							lowercase   =				[1, 6, 768]
							self.assertEqual(output.shape			,					lowercase_    )

							lowercase   =				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]			,					lowercase_			,					atol=1E-4    )






from collections import OrderedDict
from typing import Mapping

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


_lowerCAmelCase:						str   				=       logging.get_logger(__name__)

_lowerCAmelCase:						Any   				=       {
    'google/bigbird-roberta-base': 'https://huggingface.co/google/bigbird-roberta-base/resolve/main/config.json',
    'google/bigbird-roberta-large': 'https://huggingface.co/google/bigbird-roberta-large/resolve/main/config.json',
    'google/bigbird-base-trivia-itc': 'https://huggingface.co/google/bigbird-base-trivia-itc/resolve/main/config.json',
    # See all BigBird models at https://huggingface.co/models?filter=big_bird
}




class       lowercase_					(lowercase__   ):
				snake_case	    ='big_bird'





				def __init__(				self       ,  lowercase_=50358       ,  lowercase_=768       ,  lowercase_=12       ,  lowercase_=12       ,  lowercase_=3072       ,  lowercase_="gelu_new"       ,  lowercase_=0.1       ,  lowercase_=0.1       ,  lowercase_=4096       ,  lowercase_=2       ,  lowercase_=0.02       ,  lowercase_=1e-12       ,  lowercase_=True       ,  lowercase_=0       ,  lowercase_=1       ,  lowercase_=2       ,  lowercase_=66       ,  lowercase_="block_sparse"       ,  lowercase_=True       ,  lowercase_=False       ,  lowercase_=64       ,  lowercase_=3       ,  lowercase_=None       ,  **lowercase_       ,  )  -> Any:
											super().__init__(
											    pad_token_id=lowercase_       ,  bos_token_id=lowercase_       ,  eos_token_id=lowercase_       ,  sep_token_id=lowercase_       ,  **lowercase_       ,  )

											a__      =vocab_size
											a__      =max_position_embeddings
											a__      =hidden_size
											a__      =num_hidden_layers
											a__      =num_attention_heads
											a__      =intermediate_size
											a__      =hidden_act
											a__      =hidden_dropout_prob
											a__      =attention_probs_dropout_prob
											a__      =initializer_range
											a__      =type_vocab_size
											a__      =layer_norm_eps
											a__      =use_cache

											a__      =rescale_embeddings
											a__      =attention_type
											a__      =use_bias
											a__      =block_size
											a__      =num_random_blocks
											a__      =classifier_dropout







class       lowercase_					(lowercase__   ):





				@property
				def 						__UpperCamelCase    (				self)  -> Mapping[str, Mapping[int, str]]:
											if self.task == "multiple-choice":
																		a__      ={0: 'batch', 1: 'choice', 2: 'sequence'}
											else:
																		a__      ={0: 'batch', 1: 'sequence'}
											return OrderedDict(
											    [
											        ('input_ids', dynamic_axis),
											        ('attention_mask', dynamic_axis),
											    ])

