






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






		"""simple docstring"""





		def __init__(     self		,		_UpperCAmelCase		,		_UpperCAmelCase=13		,		_UpperCAmelCase=7		,		_UpperCAmelCase=True		,		_UpperCAmelCase=True		,		_UpperCAmelCase=True		,		_UpperCAmelCase=True		,		_UpperCAmelCase=99		,		_UpperCAmelCase=32		,		_UpperCAmelCase=2		,		_UpperCAmelCase=4		,		_UpperCAmelCase=37		,		_UpperCAmelCase="gelu"		,		_UpperCAmelCase=0.1		,		_UpperCAmelCase=0.1		,		_UpperCAmelCase=512		,		_UpperCAmelCase=16		,		_UpperCAmelCase=2		,		_UpperCAmelCase=0.0_2		,		_UpperCAmelCase=3		,		_UpperCAmelCase=4		,		_UpperCAmelCase=None		,		)			-> str:
					UpperCamelCase_									=     parent
					UpperCamelCase_									=     13
					UpperCamelCase_									=     7
					UpperCamelCase_									=     True
					UpperCamelCase_									=     True
					UpperCamelCase_									=     True
					UpperCamelCase_									=     True
					UpperCamelCase_									=     99
					UpperCamelCase_									=     384
					UpperCamelCase_									=     2
					UpperCamelCase_									=     4
					UpperCamelCase_									=     37
					UpperCamelCase_									=     'gelu'
					UpperCamelCase_									=     0.1
					UpperCamelCase_									=     0.1
					UpperCamelCase_									=     512
					UpperCamelCase_									=     16
					UpperCamelCase_									=     2
					UpperCamelCase_									=     0.0_2
					UpperCamelCase_									=     3
					UpperCamelCase_									=     4
					UpperCamelCase_									=     128
					UpperCamelCase_									=     2
					UpperCamelCase_									=     9
					UpperCamelCase_									=     1
					UpperCamelCase_									=     None




		def    _UpperCAmelCase     (     self )			-> Union[str, Any]:
					UpperCamelCase_									=     ids_tensor([self.batch_size, self.seq_length]		,		self.vocab_size )

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

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

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

					UpperCamelCase_									=     ConvBertConfig(
					    vocab_size=self.vocab_size		,		hidden_size=self.hidden_size		,		num_hidden_layers=self.num_hidden_layers		,		num_attention_heads=self.num_attention_heads		,		intermediate_size=self.intermediate_size		,		hidden_act=self.hidden_act		,		hidden_dropout_prob=self.hidden_dropout_prob		,		attention_probs_dropout_prob=self.attention_probs_dropout_prob		,		max_position_embeddings=self.max_position_embeddings		,		type_vocab_size=self.type_vocab_size		,		initializer_range=self.initializer_range		,		return_dict=_UpperCAmelCase		,		)

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




		def    _UpperCAmelCase     (     self		,		_UpperCAmelCase		,		_UpperCAmelCase		,		_UpperCAmelCase		,		_UpperCAmelCase		,		_UpperCAmelCase		,		_UpperCAmelCase		,		_UpperCAmelCase )			-> List[Any]:
					UpperCamelCase_									=     TFConvBertModel(config=_UpperCAmelCase )
					UpperCamelCase_									=     {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}

					UpperCamelCase_									=     [input_ids, input_mask]
					UpperCamelCase_									=     model(_UpperCAmelCase )

					UpperCamelCase_									=     model(_UpperCAmelCase )

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




		def    _UpperCAmelCase     (     self		,		_UpperCAmelCase		,		_UpperCAmelCase		,		_UpperCAmelCase		,		_UpperCAmelCase		,		_UpperCAmelCase		,		_UpperCAmelCase		,		_UpperCAmelCase )			-> str:
					UpperCamelCase_									=     TFConvBertForMaskedLM(config=_UpperCAmelCase )
					UpperCamelCase_									=     {
					    'input_ids': input_ids,
					    'attention_mask': input_mask,
					    'token_type_ids': token_type_ids,
					}
					UpperCamelCase_									=     model(_UpperCAmelCase )
					self.parent.assertEqual(result.logits.shape		,		(self.batch_size, self.seq_length, self.vocab_size) )




		def    _UpperCAmelCase     (     self		,		_UpperCAmelCase		,		_UpperCAmelCase		,		_UpperCAmelCase		,		_UpperCAmelCase		,		_UpperCAmelCase		,		_UpperCAmelCase		,		_UpperCAmelCase )			-> List[str]:
					UpperCamelCase_									=     self.num_labels
					UpperCamelCase_									=     TFConvBertForSequenceClassification(config=_UpperCAmelCase )
					UpperCamelCase_									=     {
					    'input_ids': input_ids,
					    'attention_mask': input_mask,
					    'token_type_ids': token_type_ids,
					}

					UpperCamelCase_									=     model(_UpperCAmelCase )
					self.parent.assertEqual(result.logits.shape		,		(self.batch_size, self.num_labels) )




		def    _UpperCAmelCase     (     self		,		_UpperCAmelCase		,		_UpperCAmelCase		,		_UpperCAmelCase		,		_UpperCAmelCase		,		_UpperCAmelCase		,		_UpperCAmelCase		,		_UpperCAmelCase )			-> Dict:
					UpperCamelCase_									=     self.num_choices
					UpperCamelCase_									=     TFConvBertForMultipleChoice(config=_UpperCAmelCase )
					UpperCamelCase_									=     tf.tile(tf.expand_dims(_UpperCAmelCase		,		1 )		,		(1, self.num_choices, 1) )
					UpperCamelCase_									=     tf.tile(tf.expand_dims(_UpperCAmelCase		,		1 )		,		(1, self.num_choices, 1) )
					UpperCamelCase_									=     tf.tile(tf.expand_dims(_UpperCAmelCase		,		1 )		,		(1, self.num_choices, 1) )
					UpperCamelCase_									=     {
					    'input_ids': multiple_choice_inputs_ids,
					    'attention_mask': multiple_choice_input_mask,
					    'token_type_ids': multiple_choice_token_type_ids,
					}
					UpperCamelCase_									=     model(_UpperCAmelCase )
					self.parent.assertEqual(result.logits.shape		,		(self.batch_size, self.num_choices) )




		def    _UpperCAmelCase     (     self		,		_UpperCAmelCase		,		_UpperCAmelCase		,		_UpperCAmelCase		,		_UpperCAmelCase		,		_UpperCAmelCase		,		_UpperCAmelCase		,		_UpperCAmelCase )			-> Union[str, Any]:
					UpperCamelCase_									=     self.num_labels
					UpperCamelCase_									=     TFConvBertForTokenClassification(config=_UpperCAmelCase )
					UpperCamelCase_									=     {
					    'input_ids': input_ids,
					    'attention_mask': input_mask,
					    'token_type_ids': token_type_ids,
					}
					UpperCamelCase_									=     model(_UpperCAmelCase )
					self.parent.assertEqual(result.logits.shape		,		(self.batch_size, self.seq_length, self.num_labels) )




		def    _UpperCAmelCase     (     self		,		_UpperCAmelCase		,		_UpperCAmelCase		,		_UpperCAmelCase		,		_UpperCAmelCase		,		_UpperCAmelCase		,		_UpperCAmelCase		,		_UpperCAmelCase )			-> Union[str, Any]:
					UpperCamelCase_									=     TFConvBertForQuestionAnswering(config=_UpperCAmelCase )
					UpperCamelCase_									=     {
					    'input_ids': input_ids,
					    'attention_mask': input_mask,
					    'token_type_ids': token_type_ids,
					}

					UpperCamelCase_									=     model(_UpperCAmelCase )
					self.parent.assertEqual(result.start_logits.shape		,		(self.batch_size, self.seq_length) )
					self.parent.assertEqual(result.end_logits.shape		,		(self.batch_size, self.seq_length) )




		def    _UpperCAmelCase     (     self )			-> Optional[Any]:
					UpperCamelCase_									=     self.prepare_config_and_inputs()
					(
					    (
					    UpperCamelCase_      
					)		,      (
					    UpperCamelCase_      
					)		,      (
					    UpperCamelCase_      
					)		,      (
					    UpperCamelCase_      
					)		,      (
					    UpperCamelCase_      
					)		,      (
					    UpperCamelCase_      
					)		,      (
					    UpperCamelCase_      
					)		,            
					)									=     config_and_inputs
					UpperCamelCase_									=     {'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask}
					return config, inputs_dict




@require_tf
class 						_a       (  UpperCAmelCase__						,       UpperCAmelCase__						,       unittest.TestCase    ):






		"""simple docstring"""





		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    _UpperCAmelCase     (     self )			-> Union[str, Any]:
					UpperCamelCase_									=     TFConvBertModelTester(self )
					UpperCamelCase_									=     ConfigTester(self		,		config_class=_UpperCAmelCase		,		hidden_size=37 )




		def    _UpperCAmelCase     (     self )			-> str:
					self.config_tester.run_common_tests()




		def    _UpperCAmelCase     (     self )			-> Tuple:
					UpperCamelCase_									=     self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_model(*_UpperCAmelCase )




		def    _UpperCAmelCase     (     self )			-> Union[str, Any]:
					UpperCamelCase_									=     self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_masked_lm(*_UpperCAmelCase )




		def    _UpperCAmelCase     (     self )			-> List[Any]:
					UpperCamelCase_									=     self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_multiple_choice(*_UpperCAmelCase )




		def    _UpperCAmelCase     (     self )			-> Optional[int]:
					UpperCamelCase_									=     self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_question_answering(*_UpperCAmelCase )




		def    _UpperCAmelCase     (     self )			-> Optional[int]:
					UpperCamelCase_									=     self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_sequence_classification(*_UpperCAmelCase )




		def    _UpperCAmelCase     (     self )			-> int:
					UpperCamelCase_									=     self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_token_classification(*_UpperCAmelCase )




		@slow
		def    _UpperCAmelCase     (     self )			-> Any:
					UpperCamelCase_		,      UpperCamelCase_									=     self.model_tester.prepare_config_and_inputs_for_common()
					UpperCamelCase_									=     True
					UpperCamelCase_									=     True

					if hasattr(_UpperCAmelCase		,		'use_cache' ):
								UpperCamelCase_									=     True

					UpperCamelCase_									=     getattr(self.model_tester		,		'encoder_seq_length'		,		self.model_tester.seq_length )
					UpperCamelCase_									=     getattr(self.model_tester		,		'key_length'		,		_UpperCAmelCase )

					for model_class in self.all_model_classes:
								UpperCamelCase_									=     self._prepare_for_class(_UpperCAmelCase		,		_UpperCAmelCase )
								UpperCamelCase_									=     model_class(_UpperCAmelCase )
								UpperCamelCase_									=     len(model(_UpperCAmelCase ) )

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

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

											self.assertEqual(len(_UpperCAmelCase )		,		_UpperCAmelCase )

											UpperCamelCase_									=     getattr(
											    self.model_tester		,		'expected_num_hidden_layers'		,		self.model_tester.num_hidden_layers + 1 )

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

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




		@slow
		def    _UpperCAmelCase     (     self )			-> Tuple:
					UpperCamelCase_									=     TFConvBertModel.from_pretrained('YituTech/conv-bert-base' )
					self.assertIsNotNone(_UpperCAmelCase )




		def    _UpperCAmelCase     (     self )			-> Any:
					UpperCamelCase_		,      UpperCamelCase_									=     self.model_tester.prepare_config_and_inputs_for_common()
					UpperCamelCase_									=     True
					UpperCamelCase_									=     getattr(self.model_tester		,		'decoder_seq_length'		,		self.model_tester.seq_length )
					UpperCamelCase_									=     getattr(self.model_tester		,		'encoder_seq_length'		,		self.model_tester.seq_length )
					UpperCamelCase_									=     getattr(self.model_tester		,		'key_length'		,		_UpperCAmelCase )
					UpperCamelCase_									=     getattr(self.model_tester		,		'key_length'		,		_UpperCAmelCase )

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

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

					for model_class in self.all_model_classes:
								UpperCamelCase_									=     True
								UpperCamelCase_									=     False
								UpperCamelCase_									=     model_class(_UpperCAmelCase )
								UpperCamelCase_									=     model(self._prepare_for_class(_UpperCAmelCase		,		_UpperCAmelCase ) )
								UpperCamelCase_									=     len(_UpperCAmelCase )
								self.assertEqual(config.output_hidden_states		,		_UpperCAmelCase )
								check_encoder_attentions_output(_UpperCAmelCase )

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

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

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

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




@require_tf
class 						_a       (  unittest.TestCase    ):






		"""simple docstring"""





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

					UpperCamelCase_									=     [1, 6, 768]
					self.assertEqual(output.shape		,		_UpperCAmelCase )

					UpperCamelCase_									=     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]		,		_UpperCAmelCase		,		atol=1e-4 )













def     _snake_case    (__lowercase    ,      __lowercase):
   _enforce_args(__lowercase    ,      __lowercase)
   if n == 0:
      return 0
   UpperCamelCase_									=     float('-inf')
   for i in range(1    ,      n + 1):
      UpperCamelCase_									=     max(
          __lowercase    ,      prices[i - 1] + naive_cut_rod_recursive(n - i    ,      __lowercase))

   return max_revue






def     _snake_case    (__lowercase    ,      __lowercase):
   _enforce_args(__lowercase    ,      __lowercase)
   UpperCamelCase_									=     [float('-inf') for _ in range(n + 1)]
   return _top_down_cut_rod_recursive(__lowercase    ,      __lowercase    ,      __lowercase)






def     _snake_case    (__lowercase    ,      __lowercase    ,      __lowercase):
   if max_rev[n] >= 0:
      return max_rev[n]
   elif n == 0:
      return 0
   else:
      UpperCamelCase_									=     float('-inf')
      for i in range(1    ,      n + 1):
         UpperCamelCase_									=     max(
             __lowercase    ,      prices[i - 1] + _top_down_cut_rod_recursive(n - i    ,      __lowercase    ,      __lowercase)    ,      )

      UpperCamelCase_									=     max_revenue

   return max_rev[n]






def     _snake_case    (__lowercase    ,      __lowercase):
   _enforce_args(__lowercase    ,      __lowercase)

   # length(max_rev) = n + 1, to accommodate for the revenue obtainable from a rod of
   # length 0.
   UpperCamelCase_									=     [float('-inf') for _ in range(n + 1)]
   UpperCamelCase_									=     0

   for i in range(1    ,      n + 1):
      UpperCamelCase_									=     max_rev[i]
      for j in range(1    ,      i + 1):
         UpperCamelCase_									=     max(__lowercase    ,      prices[j - 1] + max_rev[i - j])

      UpperCamelCase_									=     max_revenue_i

   return max_rev[n]






def     _snake_case    (__lowercase    ,      __lowercase):
   if n < 0:
      UpperCamelCase_									=     f"""n must be greater than or equal to 0. Got n = {n}"""
      raise ValueError(__lowercase)

   if n > len(__lowercase):
      UpperCamelCase_									=     (
          'Each integral piece of rod must have a corresponding price. '
          f"""Got n = {n} but length of prices = {len(__lowercase)}"""
      )
      raise ValueError(__lowercase)






def     _snake_case    ():
   UpperCamelCase_									=     [6, 10, 12, 15, 20, 23]
   UpperCamelCase_									=     len(__lowercase)

   # the best revenue comes from cutting the rod into 6 pieces, each
   # of length 1 resulting in a revenue of 6 * 6 = 36.
   UpperCamelCase_									=     36

   UpperCamelCase_									=     top_down_cut_rod(__lowercase    ,      __lowercase)
   UpperCamelCase_									=     bottom_up_cut_rod(__lowercase    ,      __lowercase)
   UpperCamelCase_									=     naive_cut_rod_recursive(__lowercase    ,      __lowercase)

   assert expected_max_revenue == max_rev_top_down
   assert max_rev_top_down == max_rev_bottom_up
   assert max_rev_bottom_up == max_rev_naive


if __name__ == "__main__":
 main()





