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





			def __init__(       self	:	Optional[int]  ,  lowercase__	:	Tuple  ,  lowercase__	:	List[str]=13  ,  lowercase__	:	Optional[Any]=7  ,  lowercase__	:	List[str]=True  ,  lowercase__	:	List[str]=True  ,  lowercase__	:	List[Any]=True  ,  lowercase__	:	List[str]=True  ,  lowercase__	:	List[str]=99  ,  lowercase__	:	List[Any]=32  ,  lowercase__	:	int=2  ,  lowercase__	:	Union[str, Any]=4  ,  lowercase__	:	Optional[Any]=37  ,  lowercase__	:	Tuple="gelu"  ,  lowercase__	:	Tuple=0.1  ,  lowercase__	:	List[str]=0.1  ,  lowercase__	:	str=5_12  ,  lowercase__	:	Optional[Any]=16  ,  lowercase__	:	Any=2  ,  lowercase__	:	List[Any]=0.0_2  ,  lowercase__	:	str=3  ,  lowercase__	:	Any=4  ,  lowercase__	:	Any=None  ,  ):
										_lowerCAmelCase							=							parent
										_lowerCAmelCase							=							13
										_lowerCAmelCase							=							7
										_lowerCAmelCase							=							True
										_lowerCAmelCase							=							True
										_lowerCAmelCase							=							True
										_lowerCAmelCase							=							True
										_lowerCAmelCase							=							99
										_lowerCAmelCase							=							3_84
										_lowerCAmelCase							=							2
										_lowerCAmelCase							=							4
										_lowerCAmelCase							=							37
										_lowerCAmelCase							=							'gelu'
										_lowerCAmelCase							=							0.1
										_lowerCAmelCase							=							0.1
										_lowerCAmelCase							=							5_12
										_lowerCAmelCase							=							16
										_lowerCAmelCase							=							2
										_lowerCAmelCase							=							0.0_2
										_lowerCAmelCase							=							3
										_lowerCAmelCase							=							4
										_lowerCAmelCase							=							1_28
										_lowerCAmelCase							=							2
										_lowerCAmelCase							=							9
										_lowerCAmelCase							=							1
										_lowerCAmelCase							=							None





			def   SCREAMING_SNAKE_CASE__     (       self	:	List[str]						):
										_lowerCAmelCase							=							ids_tensor([self.batch_size, self.seq_length]  ,  self.vocab_size						)

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

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

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

										_lowerCAmelCase							=							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   SCREAMING_SNAKE_CASE__     (       self	:	Tuple  ,  lowercase__	:	Any  ,  lowercase__	:	Optional[Any]  ,  lowercase__	:	List[str]  ,  lowercase__	:	Any  ,  lowercase__	:	Any  ,  lowercase__	:	Any  ,  lowercase__	:	Optional[Any]						):
										_lowerCAmelCase							=							TFConvBertModel(config=lowercase__						)
										_lowerCAmelCase							=							{'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}

										_lowerCAmelCase							=							[input_ids, input_mask]
										_lowerCAmelCase							=							model(lowercase__						)

										_lowerCAmelCase							=							model(lowercase__						)

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





			def   SCREAMING_SNAKE_CASE__     (       self	:	List[str]  ,  lowercase__	:	List[str]  ,  lowercase__	:	Dict  ,  lowercase__	:	str  ,  lowercase__	:	Union[str, Any]  ,  lowercase__	:	Any  ,  lowercase__	:	List[Any]  ,  lowercase__	:	List[Any]						):
										_lowerCAmelCase							=							TFConvBertForMaskedLM(config=lowercase__						)
										_lowerCAmelCase							=							{
										    'input_ids': input_ids,
										    'attention_mask': input_mask,
										    'token_type_ids': token_type_ids,
										}
										_lowerCAmelCase							=							model(lowercase__						)
										self.parent.assertEqual(result.logits.shape  ,  (self.batch_size, self.seq_length, self.vocab_size)						)





			def   SCREAMING_SNAKE_CASE__     (       self	:	Optional[Any]  ,  lowercase__	:	List[str]  ,  lowercase__	:	Union[str, Any]  ,  lowercase__	:	Tuple  ,  lowercase__	:	Any  ,  lowercase__	:	Union[str, Any]  ,  lowercase__	:	List[str]  ,  lowercase__	:	Optional[Any]						):
										_lowerCAmelCase							=							self.num_labels
										_lowerCAmelCase							=							TFConvBertForSequenceClassification(config=lowercase__						)
										_lowerCAmelCase							=							{
										    'input_ids': input_ids,
										    'attention_mask': input_mask,
										    'token_type_ids': token_type_ids,
										}

										_lowerCAmelCase							=							model(lowercase__						)
										self.parent.assertEqual(result.logits.shape  ,  (self.batch_size, self.num_labels)						)





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





			def   SCREAMING_SNAKE_CASE__     (       self	:	Union[str, Any]  ,  lowercase__	:	List[str]  ,  lowercase__	:	Tuple  ,  lowercase__	:	Dict  ,  lowercase__	:	int  ,  lowercase__	:	List[Any]  ,  lowercase__	:	Tuple  ,  lowercase__	:	Optional[Any]						):
										_lowerCAmelCase							=							self.num_labels
										_lowerCAmelCase							=							TFConvBertForTokenClassification(config=lowercase__						)
										_lowerCAmelCase							=							{
										    'input_ids': input_ids,
										    'attention_mask': input_mask,
										    'token_type_ids': token_type_ids,
										}
										_lowerCAmelCase							=							model(lowercase__						)
										self.parent.assertEqual(result.logits.shape  ,  (self.batch_size, self.seq_length, self.num_labels)						)





			def   SCREAMING_SNAKE_CASE__     (       self	:	Union[str, Any]  ,  lowercase__	:	Union[str, Any]  ,  lowercase__	:	Optional[Any]  ,  lowercase__	:	List[Any]  ,  lowercase__	:	Union[str, Any]  ,  lowercase__	:	Optional[Any]  ,  lowercase__	:	List[Any]  ,  lowercase__	:	Any						):
										_lowerCAmelCase							=							TFConvBertForQuestionAnswering(config=lowercase__						)
										_lowerCAmelCase							=							{
										    'input_ids': input_ids,
										    'attention_mask': input_mask,
										    'token_type_ids': token_type_ids,
										}

										_lowerCAmelCase							=							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   SCREAMING_SNAKE_CASE__     (       self	:	Optional[int]						):
										_lowerCAmelCase							=							self.prepare_config_and_inputs()
										(
										    (
										    _lowerCAmelCase
										)			,		(
										    _lowerCAmelCase
										)			,		(
										    _lowerCAmelCase
										)			,		(
										    _lowerCAmelCase
										)			,		(
										    _lowerCAmelCase
										)			,		(
										    _lowerCAmelCase
										)			,		(
										    _lowerCAmelCase
										)			,		
										)							=							config_and_inputs
										_lowerCAmelCase							=							{'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask}
										return config, inputs_dict







@require_tf
class        lowerCamelCase__				(   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   SCREAMING_SNAKE_CASE__     (       self	:	int						):
										_lowerCAmelCase							=							TFConvBertModelTester(self						)
										_lowerCAmelCase							=							ConfigTester(self  ,  config_class=lowercase__  ,  hidden_size=37						)





			def   SCREAMING_SNAKE_CASE__     (       self	:	int						):
										self.config_tester.run_common_tests()





			def   SCREAMING_SNAKE_CASE__     (       self	:	int						):
										_lowerCAmelCase							=							self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_model(*lowercase__						)





			def   SCREAMING_SNAKE_CASE__     (       self	:	Tuple						):
										_lowerCAmelCase							=							self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_masked_lm(*lowercase__						)





			def   SCREAMING_SNAKE_CASE__     (       self	:	Dict						):
										_lowerCAmelCase							=							self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_multiple_choice(*lowercase__						)





			def   SCREAMING_SNAKE_CASE__     (       self	:	Any						):
										_lowerCAmelCase							=							self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_question_answering(*lowercase__						)





			def   SCREAMING_SNAKE_CASE__     (       self	:	List[Any]						):
										_lowerCAmelCase							=							self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_sequence_classification(*lowercase__						)





			def   SCREAMING_SNAKE_CASE__     (       self	:	Tuple						):
										_lowerCAmelCase							=							self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_token_classification(*lowercase__						)





			@slow
			def   SCREAMING_SNAKE_CASE__     (       self	:	Optional[int]						):
										_lowerCAmelCase			,		_lowerCAmelCase							=							self.model_tester.prepare_config_and_inputs_for_common()
										_lowerCAmelCase							=							True
										_lowerCAmelCase							=							True

										if hasattr(lowercase__  ,  'use_cache'						):
																	_lowerCAmelCase							=							True

										_lowerCAmelCase							=							getattr(self.model_tester  ,  'encoder_seq_length'  ,  self.model_tester.seq_length						)
										_lowerCAmelCase							=							getattr(self.model_tester  ,  'key_length'  ,  lowercase__						)

										for model_class in self.all_model_classes:
																	_lowerCAmelCase							=							self._prepare_for_class(lowercase__  ,  lowercase__						)
																	_lowerCAmelCase							=							model_class(lowercase__						)
																	_lowerCAmelCase							=							len(model(lowercase__						)						)

																	with tempfile.TemporaryDirectory() as tmpdirname:
																								model.save_pretrained(lowercase__  ,  saved_model=lowercase__						)
																								_lowerCAmelCase							=							os.path.join(lowercase__  ,  'saved_model'  ,  '1'						)
																								_lowerCAmelCase							=							tf.keras.models.load_model(lowercase__						)
																								_lowerCAmelCase							=							model(lowercase__						)

																								if self.is_encoder_decoder:
																															_lowerCAmelCase							=							outputs['encoder_hidden_states']
																															_lowerCAmelCase							=							outputs['encoder_attentions']
																								else:
																															_lowerCAmelCase							=							outputs['hidden_states']
																															_lowerCAmelCase							=							outputs['attentions']

																								self.assertEqual(len(lowercase__						)  ,  lowercase__						)

																								_lowerCAmelCase							=							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   SCREAMING_SNAKE_CASE__     (       self	:	List[str]						):
										_lowerCAmelCase							=							TFConvBertModel.from_pretrained('YituTech/conv-bert-base'						)
										self.assertIsNotNone(lowercase__						)





			def   SCREAMING_SNAKE_CASE__     (       self	:	Any						):
										_lowerCAmelCase			,		_lowerCAmelCase							=							self.model_tester.prepare_config_and_inputs_for_common()
										_lowerCAmelCase							=							True
										_lowerCAmelCase							=							getattr(self.model_tester  ,  'decoder_seq_length'  ,  self.model_tester.seq_length						)
										_lowerCAmelCase							=							getattr(self.model_tester  ,  'encoder_seq_length'  ,  self.model_tester.seq_length						)
										_lowerCAmelCase							=							getattr(self.model_tester  ,  'key_length'  ,  lowercase__						)
										_lowerCAmelCase							=							getattr(self.model_tester  ,  'key_length'  ,  lowercase__						)

										def check_decoder_attentions_output(lowercase__	:	Optional[Any]						):
																	_lowerCAmelCase							=							len(lowercase__						)
																	self.assertEqual(out_len % 2  ,  0						)
																	_lowerCAmelCase							=							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(lowercase__	:	Tuple						):
																	_lowerCAmelCase							=							[
																	    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:
																	_lowerCAmelCase							=							True
																	_lowerCAmelCase							=							False
																	_lowerCAmelCase							=							model_class(lowercase__						)
																	_lowerCAmelCase							=							model(self._prepare_for_class(lowercase__  ,  lowercase__						)						)
																	_lowerCAmelCase							=							len(lowercase__						)
																	self.assertEqual(config.output_hidden_states  ,  lowercase__						)
																	check_encoder_attentions_output(lowercase__						)

																	if self.is_encoder_decoder:
																								_lowerCAmelCase							=							model_class(lowercase__						)
																								_lowerCAmelCase							=							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"]
																	_lowerCAmelCase							=							True
																	_lowerCAmelCase							=							model_class(lowercase__						)
																	_lowerCAmelCase							=							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
																	_lowerCAmelCase							=							True
																	_lowerCAmelCase							=							True
																	_lowerCAmelCase							=							model_class(lowercase__						)
																	_lowerCAmelCase							=							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        lowerCamelCase__				(   unittest.TestCase				):





			@slow
			def   SCREAMING_SNAKE_CASE__     (       self	:	Union[str, Any]						):
										_lowerCAmelCase							=							TFConvBertModel.from_pretrained('YituTech/conv-bert-base'						)
										_lowerCAmelCase							=							tf.constant([[0, 1, 2, 3, 4, 5]]						)
										_lowerCAmelCase							=							model(lowercase__						)[0]

										_lowerCAmelCase							=							[1, 6, 7_68]
										self.assertEqual(output.shape  ,  lowercase__						)

										_lowerCAmelCase							=							tf.constant(
										    [
										        [
										            [-0.0_3_4_7_5_4_9_3, -0.4_6_8_6_0_3_4, -0.3_0_6_3_8_8_3_2],
										            [0.2_2_6_3_7_2_4_8, -0.2_6_9_8_8_6_4_6, -0.7_4_2_3_4_2_4],
										            [0.1_0_3_2_4_8_6_8, -0.4_5_0_1_3_5_0_8, -0.5_8_2_8_0_7_8_4],
										        ]
										    ]						)
										tf.debugging.assert_near(output[:, :3, :3]  ,  lowercase__  ,  atol=1e-4						)







def _lowerCamelCase  (		snake_case  ):
							assert (
							    isinstance(snake_case     , snake_case  ) and number_of_steps > 0
							), F'number_of_steps needs to be positive integer, your input {number_of_steps}'
							if number_of_steps == 1:
														return 1
							_lowerCAmelCase			,		_lowerCAmelCase							=							1, 1
							for _ in range(number_of_steps - 1  ):
														_lowerCAmelCase			,		_lowerCAmelCase							=							current + previous, current
							return current


if __name__ == "__main__":
				import doctest

				doctest.testmod()






