


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       ,  lowercase_       ,  lowercase_=13       ,  lowercase_=7       ,  lowercase_=True       ,  lowercase_=True       ,  lowercase_=True       ,  lowercase_=True       ,  lowercase_=99       ,  lowercase_=32       ,  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_=None       ,  )  -> Any:
											a__      =parent
											a__      =13
											a__      =7
											a__      =True
											a__      =True
											a__      =True
											a__      =True
											a__      =99
											a__      =384
											a__      =2
											a__      =4
											a__      =37
											a__      ='gelu'
											a__      =0.1
											a__      =0.1
											a__      =512
											a__      =16
											a__      =2
											a__      =0.02
											a__      =3
											a__      =4
											a__      =128
											a__      =2
											a__      =9
											a__      =1
											a__      =None





				def 						__UpperCamelCase    (				self)  -> Dict:
											a__      =ids_tensor([self.batch_size, self.seq_length]       ,  self.vocab_size)

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

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

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

											a__      =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 						__UpperCamelCase    (				self       ,  lowercase_       ,  lowercase_       ,  lowercase_       ,  lowercase_       ,  lowercase_       ,  lowercase_       ,  lowercase_)  -> List[Any]:
											a__      =TFConvBertModel(config=lowercase_)
											a__      ={'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}

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

											a__      =model(lowercase_)

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





				def 						__UpperCamelCase    (				self       ,  lowercase_       ,  lowercase_       ,  lowercase_       ,  lowercase_       ,  lowercase_       ,  lowercase_       ,  lowercase_)  -> List[Any]:
											a__      =TFConvBertForMaskedLM(config=lowercase_)
											a__      ={
											    'input_ids': input_ids,
											    'attention_mask': input_mask,
											    'token_type_ids': token_type_ids,
											}
											a__      =model(lowercase_)
											self.parent.assertEqual(result.logits.shape       ,  (self.batch_size, self.seq_length, self.vocab_size))





				def 						__UpperCamelCase    (				self       ,  lowercase_       ,  lowercase_       ,  lowercase_       ,  lowercase_       ,  lowercase_       ,  lowercase_       ,  lowercase_)  -> Any:
											a__      =self.num_labels
											a__      =TFConvBertForSequenceClassification(config=lowercase_)
											a__      ={
											    'input_ids': input_ids,
											    'attention_mask': input_mask,
											    'token_type_ids': token_type_ids,
											}

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





				def 						__UpperCamelCase    (				self       ,  lowercase_       ,  lowercase_       ,  lowercase_       ,  lowercase_       ,  lowercase_       ,  lowercase_       ,  lowercase_)  -> List[Any]:
											a__      =self.num_choices
											a__      =TFConvBertForMultipleChoice(config=lowercase_)
											a__      =tf.tile(tf.expand_dims(lowercase_       ,  1)       ,  (1, self.num_choices, 1))
											a__      =tf.tile(tf.expand_dims(lowercase_       ,  1)       ,  (1, self.num_choices, 1))
											a__      =tf.tile(tf.expand_dims(lowercase_       ,  1)       ,  (1, self.num_choices, 1))
											a__      ={
											    'input_ids': multiple_choice_inputs_ids,
											    'attention_mask': multiple_choice_input_mask,
											    'token_type_ids': multiple_choice_token_type_ids,
											}
											a__      =model(lowercase_)
											self.parent.assertEqual(result.logits.shape       ,  (self.batch_size, self.num_choices))





				def 						__UpperCamelCase    (				self       ,  lowercase_       ,  lowercase_       ,  lowercase_       ,  lowercase_       ,  lowercase_       ,  lowercase_       ,  lowercase_)  -> List[str]:
											a__      =self.num_labels
											a__      =TFConvBertForTokenClassification(config=lowercase_)
											a__      ={
											    'input_ids': input_ids,
											    'attention_mask': input_mask,
											    'token_type_ids': token_type_ids,
											}
											a__      =model(lowercase_)
											self.parent.assertEqual(result.logits.shape       ,  (self.batch_size, self.seq_length, self.num_labels))





				def 						__UpperCamelCase    (				self       ,  lowercase_       ,  lowercase_       ,  lowercase_       ,  lowercase_       ,  lowercase_       ,  lowercase_       ,  lowercase_)  -> List[str]:
											a__      =TFConvBertForQuestionAnswering(config=lowercase_)
											a__      ={
											    'input_ids': input_ids,
											    'attention_mask': input_mask,
											    'token_type_ids': token_type_ids,
											}

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







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





				def 						__UpperCamelCase    (				self)  -> str:
											a__      =TFConvBertModelTester(self)
											a__      =ConfigTester(self       ,  config_class=lowercase_       ,  hidden_size=37)





				def 						__UpperCamelCase    (				self)  -> int:
											self.config_tester.run_common_tests()





				def 						__UpperCamelCase    (				self)  -> List[Any]:
											a__      =self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_model(*lowercase_)





				def 						__UpperCamelCase    (				self)  -> Optional[int]:
											a__      =self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_masked_lm(*lowercase_)





				def 						__UpperCamelCase    (				self)  -> int:
											a__      =self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_multiple_choice(*lowercase_)





				def 						__UpperCamelCase    (				self)  -> Tuple:
											a__      =self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_question_answering(*lowercase_)





				def 						__UpperCamelCase    (				self)  -> Union[str, Any]:
											a__      =self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_sequence_classification(*lowercase_)





				def 						__UpperCamelCase    (				self)  -> List[str]:
											a__      =self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_token_classification(*lowercase_)





				@slow
				def 						__UpperCamelCase    (				self)  -> Tuple:
											a__    ,     a__      =self.model_tester.prepare_config_and_inputs_for_common()
											a__      =True
											a__      =True

											if hasattr(lowercase_       ,  'use_cache'):
																		a__      =True

											a__      =getattr(self.model_tester       ,  'encoder_seq_length'       ,  self.model_tester.seq_length)
											a__      =getattr(self.model_tester       ,  'key_length'       ,  lowercase_)

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

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

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

																									self.assertEqual(len(lowercase_)       ,  lowercase_)

																									a__      =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 						__UpperCamelCase    (				self)  -> List[Any]:
											a__      =TFConvBertModel.from_pretrained('YituTech/conv-bert-base')
											self.assertIsNotNone(lowercase_)





				def 						__UpperCamelCase    (				self)  -> Optional[int]:
											a__    ,     a__      =self.model_tester.prepare_config_and_inputs_for_common()
											a__      =True
											a__      =getattr(self.model_tester       ,  'decoder_seq_length'       ,  self.model_tester.seq_length)
											a__      =getattr(self.model_tester       ,  'encoder_seq_length'       ,  self.model_tester.seq_length)
											a__      =getattr(self.model_tester       ,  'key_length'       ,  lowercase_)
											a__      =getattr(self.model_tester       ,  'key_length'       ,  lowercase_)

											def check_decoder_attentions_output(lowercase_):
																		a__      =len(lowercase_)
																		self.assertEqual(out_len % 2       ,  0)
																		a__      =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_):
																		a__      =[
																		    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:
																		a__      =True
																		a__      =False
																		a__      =model_class(lowercase_)
																		a__      =model(self._prepare_for_class(lowercase_       ,  lowercase_))
																		a__      =len(lowercase_)
																		self.assertEqual(config.output_hidden_states       ,  lowercase_)
																		check_encoder_attentions_output(lowercase_)

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





				@slow
				def 						__UpperCamelCase    (				self)  -> Optional[int]:
											a__      =TFConvBertModel.from_pretrained('YituTech/conv-bert-base')
											a__      =tf.constant([[0, 1, 2, 3, 4, 5]])
											a__      =model(lowercase_)[0]

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

											a__      =tf.constant(
											    [
											        [
											            [-0.03_47_54_93, -0.4_68_60_34, -0.30_63_88_32],
											            [0.22_63_72_48, -0.26_98_86_46, -0.7_42_34_24],
											            [0.10_32_48_68, -0.45_01_35_08, -0.58_28_07_84],
											        ]
											    ])
											tf.debugging.assert_near(output[:, :3, :3]       ,  lowercase_       ,  atol=1e-4)








"""simple docstring"""





import tempfile
import unittest

from make_student import create_student_by_copying_alternating_layers

from transformers import AutoConfig
from transformers.file_utils import cached_property
from transformers.testing_utils import require_torch


__A											=						"""sshleifer/bart-tiny-random"""
__A											=						"""patrickvonplaten/t5-tiny-random"""






@require_torch
class    _lowerCAmelCase  (	unittest.TestCase			):
       """simple docstring"""




       @cached_property
       def 	snake_case		(       self ):



         '''simple docstring'''

         return AutoConfig.from_pretrained(__UpperCAmelCase )






       def 	snake_case		(       self ):



         '''simple docstring'''

         lowerCAmelCase__				,							*lowerCAmelCase__     :List[str]											=				create_student_by_copying_alternating_layers(__UpperCAmelCase       ,					tempfile.mkdtemp()       ,					e=1       ,					d=1 )
         self.assertEqual(student.config.num_hidden_layers       ,					1 )






       def 	snake_case		(       self ):



         '''simple docstring'''

         lowerCAmelCase__				,							*lowerCAmelCase__     :Optional[int]											=				create_student_by_copying_alternating_layers(__UpperCAmelCase       ,					tempfile.mkdtemp()       ,					e=1       ,					d=__UpperCAmelCase )






       def 	snake_case		(       self ):



         '''simple docstring'''

         lowerCAmelCase__				,							*lowerCAmelCase__     :List[Any]											=				create_student_by_copying_alternating_layers(__UpperCAmelCase       ,					tempfile.mkdtemp()       ,					e=1       ,					d=__UpperCAmelCase )
         self.assertEqual(student.config.encoder_layers       ,					1 )
         self.assertEqual(student.config.decoder_layers       ,					self.teacher_config.encoder_layers )






       def 	snake_case		(       self ):



         '''simple docstring'''

         lowerCAmelCase__				,							*lowerCAmelCase__     :Optional[int]											=				create_student_by_copying_alternating_layers(__UpperCAmelCase       ,					tempfile.mkdtemp()       ,					e=1       ,					d=1 )
         self.assertEqual(student.config.encoder_layers       ,					1 )
         self.assertEqual(student.config.decoder_layers       ,					1 )






       def 	snake_case		(       self ):



         '''simple docstring'''

         with self.assertRaises(__UpperCAmelCase ):
           create_student_by_copying_alternating_layers(__UpperCAmelCase       ,					tempfile.mkdtemp()       ,					e=__UpperCAmelCase       ,					d=__UpperCAmelCase )
