



"""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 							lowerCamelCase_:





					'''simple docstring'''



					def __init__(    self				,       lowerCamelCase__				,       lowerCamelCase__=1_3				,       lowerCamelCase__=7				,       lowerCamelCase__=True				,       lowerCamelCase__=True				,       lowerCamelCase__=True				,       lowerCamelCase__=True				,       lowerCamelCase__=9_9				,       lowerCamelCase__=3_2				,       lowerCamelCase__=2				,       lowerCamelCase__=4				,       lowerCamelCase__=3_7				,       lowerCamelCase__="gelu"				,       lowerCamelCase__=0.1				,       lowerCamelCase__=0.1				,       lowerCamelCase__=5_1_2				,       lowerCamelCase__=1_6				,       lowerCamelCase__=2				,       lowerCamelCase__=0.0_2				,       lowerCamelCase__=3				,       lowerCamelCase__=4				,       lowerCamelCase__=None				,       ):
									_lowerCamelCase        =				parent
									_lowerCamelCase        =				1_3
									_lowerCamelCase        =				7
									_lowerCamelCase        =				True
									_lowerCamelCase        =				True
									_lowerCamelCase        =				True
									_lowerCamelCase        =				True
									_lowerCamelCase        =				9_9
									_lowerCamelCase        =				3_8_4
									_lowerCamelCase        =				2
									_lowerCamelCase        =				4
									_lowerCamelCase        =				3_7
									_lowerCamelCase        =				'''gelu'''
									_lowerCamelCase        =				0.1
									_lowerCamelCase        =				0.1
									_lowerCamelCase        =				5_1_2
									_lowerCamelCase        =				1_6
									_lowerCamelCase        =				2
									_lowerCamelCase        =				0.0_2
									_lowerCamelCase        =				3
									_lowerCamelCase        =				4
									_lowerCamelCase        =				1_2_8
									_lowerCamelCase        =				2
									_lowerCamelCase        =				9
									_lowerCamelCase        =				1
									_lowerCamelCase        =				None

					def 			snake_case__      (    self   ):
									_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=lowerCamelCase__				,       )

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

					def 			snake_case__      (    self				,       lowerCamelCase__				,       lowerCamelCase__				,       lowerCamelCase__				,       lowerCamelCase__				,       lowerCamelCase__				,       lowerCamelCase__				,       lowerCamelCase__   ):
									_lowerCamelCase        =				TFConvBertModel(config=lowerCamelCase__   )
									_lowerCamelCase        =				{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}

									_lowerCamelCase        =				[input_ids, input_mask]
									_lowerCamelCase        =				model(lowerCamelCase__   )

									_lowerCamelCase        =				model(lowerCamelCase__   )

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

					def 			snake_case__      (    self				,       lowerCamelCase__				,       lowerCamelCase__				,       lowerCamelCase__				,       lowerCamelCase__				,       lowerCamelCase__				,       lowerCamelCase__				,       lowerCamelCase__   ):
									_lowerCamelCase        =				TFConvBertForMaskedLM(config=lowerCamelCase__   )
									_lowerCamelCase        =				{
									    '''input_ids''': input_ids,
									    '''attention_mask''': input_mask,
									    '''token_type_ids''': token_type_ids,
									}
									_lowerCamelCase        =				model(lowerCamelCase__   )
									self.parent.assertEqual(result.logits.shape				,       (self.batch_size, self.seq_length, self.vocab_size)   )

					def 			snake_case__      (    self				,       lowerCamelCase__				,       lowerCamelCase__				,       lowerCamelCase__				,       lowerCamelCase__				,       lowerCamelCase__				,       lowerCamelCase__				,       lowerCamelCase__   ):
									_lowerCamelCase        =				self.num_labels
									_lowerCamelCase        =				TFConvBertForSequenceClassification(config=lowerCamelCase__   )
									_lowerCamelCase        =				{
									    '''input_ids''': input_ids,
									    '''attention_mask''': input_mask,
									    '''token_type_ids''': token_type_ids,
									}

									_lowerCamelCase        =				model(lowerCamelCase__   )
									self.parent.assertEqual(result.logits.shape				,       (self.batch_size, self.num_labels)   )

					def 			snake_case__      (    self				,       lowerCamelCase__				,       lowerCamelCase__				,       lowerCamelCase__				,       lowerCamelCase__				,       lowerCamelCase__				,       lowerCamelCase__				,       lowerCamelCase__   ):
									_lowerCamelCase        =				self.num_choices
									_lowerCamelCase        =				TFConvBertForMultipleChoice(config=lowerCamelCase__   )
									_lowerCamelCase        =				tf.tile(tf.expand_dims(lowerCamelCase__				,       1   )				,       (1, self.num_choices, 1)   )
									_lowerCamelCase        =				tf.tile(tf.expand_dims(lowerCamelCase__				,       1   )				,       (1, self.num_choices, 1)   )
									_lowerCamelCase        =				tf.tile(tf.expand_dims(lowerCamelCase__				,       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(lowerCamelCase__   )
									self.parent.assertEqual(result.logits.shape				,       (self.batch_size, self.num_choices)   )

					def 			snake_case__      (    self				,       lowerCamelCase__				,       lowerCamelCase__				,       lowerCamelCase__				,       lowerCamelCase__				,       lowerCamelCase__				,       lowerCamelCase__				,       lowerCamelCase__   ):
									_lowerCamelCase        =				self.num_labels
									_lowerCamelCase        =				TFConvBertForTokenClassification(config=lowerCamelCase__   )
									_lowerCamelCase        =				{
									    '''input_ids''': input_ids,
									    '''attention_mask''': input_mask,
									    '''token_type_ids''': token_type_ids,
									}
									_lowerCamelCase        =				model(lowerCamelCase__   )
									self.parent.assertEqual(result.logits.shape				,       (self.batch_size, self.seq_length, self.num_labels)   )

					def 			snake_case__      (    self				,       lowerCamelCase__				,       lowerCamelCase__				,       lowerCamelCase__				,       lowerCamelCase__				,       lowerCamelCase__				,       lowerCamelCase__				,       lowerCamelCase__   ):
									_lowerCamelCase        =				TFConvBertForQuestionAnswering(config=lowerCamelCase__   )
									_lowerCamelCase        =				{
									    '''input_ids''': input_ids,
									    '''attention_mask''': input_mask,
									    '''token_type_ids''': token_type_ids,
									}

									_lowerCamelCase        =				model(lowerCamelCase__   )
									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 			snake_case__      (    self   ):
									_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_(   A__,				A__,				unittest.TestCase    ):





					'''simple docstring'''



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

					def 			snake_case__      (    self   ):
									_lowerCamelCase        =				TFConvBertModelTester(self   )
									_lowerCamelCase        =				ConfigTester(self				,       config_class=lowerCamelCase__				,       hidden_size=3_7   )

					def 			snake_case__      (    self   ):
									self.config_tester.run_common_tests()

					def 			snake_case__      (    self   ):
									_lowerCamelCase        =				self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_model(*lowerCamelCase__   )

					def 			snake_case__      (    self   ):
									_lowerCamelCase        =				self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_masked_lm(*lowerCamelCase__   )

					def 			snake_case__      (    self   ):
									_lowerCamelCase        =				self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_multiple_choice(*lowerCamelCase__   )

					def 			snake_case__      (    self   ):
									_lowerCamelCase        =				self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_question_answering(*lowerCamelCase__   )

					def 			snake_case__      (    self   ):
									_lowerCamelCase        =				self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_sequence_classification(*lowerCamelCase__   )

					def 			snake_case__      (    self   ):
									_lowerCamelCase        =				self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_token_classification(*lowerCamelCase__   )

					@slow
					def 			snake_case__      (    self   ):
									_lowerCamelCase  ,     _lowerCamelCase        =				self.model_tester.prepare_config_and_inputs_for_common()
									_lowerCamelCase        =				True
									_lowerCamelCase        =				True

									if hasattr(lowerCamelCase__				,       '''use_cache'''   ):
													_lowerCamelCase        =				True

									_lowerCamelCase        =				getattr(self.model_tester				,       '''encoder_seq_length'''				,       self.model_tester.seq_length   )
									_lowerCamelCase        =				getattr(self.model_tester				,       '''key_length'''				,       lowerCamelCase__   )

									for model_class in self.all_model_classes:
													_lowerCamelCase        =				self._prepare_for_class(lowerCamelCase__				,       lowerCamelCase__   )
													_lowerCamelCase        =				model_class(lowerCamelCase__   )
													_lowerCamelCase        =				len(model(lowerCamelCase__   )   )

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

																	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(lowerCamelCase__   )				,       lowerCamelCase__   )

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

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

																	self.assertEqual(len(lowerCamelCase__   )				,       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 			snake_case__      (    self   ):
									_lowerCamelCase        =				TFConvBertModel.from_pretrained('''YituTech/conv-bert-base'''   )
									self.assertIsNotNone(lowerCamelCase__   )





					def 			snake_case__      (    self   ):
									_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'''				,       lowerCamelCase__   )
									_lowerCamelCase        =				getattr(self.model_tester				,       '''key_length'''				,       lowerCamelCase__   )

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

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

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

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

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


@require_tf
class 							lowerCamelCase_(   unittest.TestCase    ):





					'''simple docstring'''



					@slow
					def 			snake_case__      (    self   ):
									_lowerCamelCase        =				TFConvBertModel.from_pretrained('''YituTech/conv-bert-base'''   )
									_lowerCamelCase        =				tf.constant([[0, 1, 2, 3, 4, 5]]   )
									_lowerCamelCase        =				model(lowerCamelCase__   )[0]

									_lowerCamelCase        =				[1, 6, 7_6_8]
									self.assertEqual(output.shape				,       lowerCamelCase__   )

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





"""simple docstring"""
import numpy as np



def        lowerCAmelCase_(   lowercase_     :							np.ndarray						,   lowercase_     :							np.ndarray						,   lowercase_     :							float = 1e-12						,   lowercase_     :							int = 1_00						,   )	->       tuple[float, np.ndarray]:
				assert np.shape(lowercase_							)[0] == np.shape(lowercase_							)[1]
				# Ensure proper dimensionality.
				assert np.shape(lowercase_							)[0] == np.shape(lowercase_							)[0]
				# Ensure inputs are either both complex or both real
				assert np.iscomplexobj(lowercase_							) == np.iscomplexobj(lowercase_							)
				_lowerCamelCase        =				np.iscomplexobj(lowercase_							)
				if is_complex:
								# Ensure complex input_matrix is Hermitian
								assert np.array_equal(lowercase_						,   input_matrix.conj().T							)

				# Set convergence to False. Will define convergence when we exceed max_iterations
				# or when we have small changes from one iteration to next.

				_lowerCamelCase        =				False
				_lowerCamelCase        =				0
				_lowerCamelCase        =				0
				_lowerCamelCase        =				1e12

				while not convergence:
								# Multiple matrix by the vector.
								_lowerCamelCase        =				np.dot(lowercase_						,   lowercase_							)
								# Normalize the resulting output vector.
								_lowerCamelCase        =				w / np.linalg.norm(lowercase_							)
								# Find rayleigh quotient
								# (faster than usual b/c we know vector is normalized already)
								_lowerCamelCase        =				vector.conj().T if is_complex else vector.T
								_lowerCamelCase        =				np.dot(lowercase_						,   np.dot(lowercase_						,   lowercase_							)							)

								# Check convergence.
								_lowerCamelCase        =				np.abs(lambda_ - lambda_previous							) / lambda_
								iterations += 1

								if error <= error_tol or iterations >= max_iterations:
												_lowerCamelCase        =				True

								_lowerCamelCase        =				lambda_

				if is_complex:
								_lowerCamelCase        =				np.real(lambda_							)

				return lambda_, vector



def        lowerCAmelCase_(   )	->       None:
				_lowerCamelCase        =				np.array([[41, 4, 20], [4, 26, 30], [20, 30, 50]]							)
				_lowerCamelCase        =				np.array([41, 4, 20]							)
				_lowerCamelCase        =				real_input_matrix.astype(np.complexaaa							)
				_lowerCamelCase        =				np.triu(1j * complex_input_matrix						,   1							)
				complex_input_matrix += imag_matrix
				complex_input_matrix += -1 * imag_matrix.T
				_lowerCamelCase        =				np.array([41, 4, 20]							).astype(np.complexaaa							)

				for problem_type in ["real", "complex"]:
								if problem_type == "real":
												_lowerCamelCase        =				real_input_matrix
												_lowerCamelCase        =				real_vector
								elif problem_type == "complex":
												_lowerCamelCase        =				complex_input_matrix
												_lowerCamelCase        =				complex_vector

								# Our implementation.
								_lowerCamelCase  ,     _lowerCamelCase        =				power_iteration(lowercase_						,   lowercase_							)

								# Numpy implementation.

								# Get eigenvalues and eigenvectors using built-in numpy
								# eigh (eigh used for symmetric or hermetian matrices).
								_lowerCamelCase  ,     _lowerCamelCase        =				np.linalg.eigh(lowercase_							)
								# Last eigenvalue is the maximum one.
								_lowerCamelCase        =				eigen_values[-1]
								# Last column in this matrix is eigenvector corresponding to largest eigenvalue.
								_lowerCamelCase        =				eigen_vectors[:, -1]

								# Check our implementation and numpy gives close answers.
								assert np.abs(eigen_value - eigen_value_max							) <= 1e-6
								# Take absolute values element wise of each eigenvector.
								# as they are only unique to a minus sign.
								assert np.linalg.norm(np.abs(lowercase_							) - np.abs(lowercase_							)							) <= 1e-6


if __name__ == "__main__":
			import doctest

			doctest.testmod()
			test_power_iteration()
