

'''simple docstring'''







from __future__ import annotations

import unittest

from transformers import FunnelConfig, is_tf_available
from transformers.testing_utils import require_tf

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 (
          TFFunnelBaseModel,
          TFFunnelForMaskedLM,
          TFFunnelForMultipleChoice,
          TFFunnelForPreTraining,
          TFFunnelForQuestionAnswering,
          TFFunnelForSequenceClassification,
          TFFunnelForTokenClassification,
          TFFunnelModel,
      )


class        A__   :



    def __init__(       self		,					UpperCamelCase__		,					UpperCamelCase__=13		,					UpperCamelCase__=7		,					UpperCamelCase__=True		,					UpperCamelCase__=True		,					UpperCamelCase__=True		,					UpperCamelCase__=True		,					UpperCamelCase__=99		,					UpperCamelCase__=[1, 1, 2]		,					UpperCamelCase__=1		,					UpperCamelCase__=32		,					UpperCamelCase__=4		,					UpperCamelCase__=8		,					UpperCamelCase__=37		,					UpperCamelCase__="gelu_new"		,					UpperCamelCase__=0.1		,					UpperCamelCase__=0.1		,					UpperCamelCase__=0.0		,					UpperCamelCase__=512		,					UpperCamelCase__=3		,					UpperCamelCase__=0.02		,					UpperCamelCase__=3		,					UpperCamelCase__=4		,					UpperCamelCase__=None		,					UpperCamelCase__=False		,					)    ->   str:





           '''simple docstring'''
           A_						       =   parent
           A_						       =   batch_size
           A_						       =   seq_length
           A_						       =   is_training
           A_						       =   use_input_mask
           A_						       =   use_token_type_ids
           A_						       =   use_labels
           A_						       =   vocab_size
           A_						       =   block_sizes
           A_						       =   num_decoder_layers
           A_						       =   d_model
           A_						       =   n_head
           A_						       =   d_head
           A_						       =   d_inner
           A_						       =   hidden_act
           A_						       =   hidden_dropout
           A_						       =   attention_dropout
           A_						       =   activation_dropout
           A_						       =   max_position_embeddings
           A_						       =   type_vocab_size
           A_						       =   2
           A_						       =   num_labels
           A_						       =   num_choices
           A_						       =   scope
           A_						       =   initializer_std

           # Used in the tests to check the size of the first attention layer
           A_						       =   n_head
           # Used in the tests to check the size of the first hidden state
           A_						       =   self.d_model
           # Used in the tests to check the number of output hidden states/attentions
           A_						       =   sum(self.block_sizes							) + (0 if base else self.num_decoder_layers)
           # FunnelModel adds two hidden layers: input embeddings and the sum of the upsampled encoder hidden state with
           # the last hidden state of the first block (which is the first hidden state of the decoder).
           if not base:
                  A_						       =   self.num_hidden_layers + 2



    def        snake_case_      (       self							)    ->   str:





           '''simple docstring'''
           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_						       =   FunnelConfig(
               vocab_size=self.vocab_size		,					block_sizes=self.block_sizes		,					num_decoder_layers=self.num_decoder_layers		,					d_model=self.d_model		,					n_head=self.n_head		,					d_head=self.d_head		,					d_inner=self.d_inner		,					hidden_act=self.hidden_act		,					hidden_dropout=self.hidden_dropout		,					attention_dropout=self.attention_dropout		,					activation_dropout=self.activation_dropout		,					max_position_embeddings=self.max_position_embeddings		,					type_vocab_size=self.type_vocab_size		,					initializer_std=self.initializer_std		,					)

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



    def        snake_case_      (       self		,					UpperCamelCase__		,					UpperCamelCase__		,					UpperCamelCase__		,					UpperCamelCase__		,					UpperCamelCase__		,					UpperCamelCase__		,					UpperCamelCase__		,					)    ->   Optional[Any]:





           '''simple docstring'''
           A_						       =   TFFunnelModel(config=UpperCamelCase__							)
           A_						       =   {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
           A_						       =   model(UpperCamelCase__							)

           A_						       =   [input_ids, input_mask]
           A_						       =   model(UpperCamelCase__							)

           A_						       =   model(UpperCamelCase__							)
           self.parent.assertEqual(result.last_hidden_state.shape		,					(self.batch_size, self.seq_length, self.d_model)							)

           A_						       =   False
           A_						       =   TFFunnelModel(config=UpperCamelCase__							)
           A_						       =   model(UpperCamelCase__							)
           self.parent.assertEqual(result.last_hidden_state.shape		,					(self.batch_size, self.seq_length, self.d_model)							)

           A_						       =   False
           A_						       =   TFFunnelModel(config=UpperCamelCase__							)
           A_						       =   model(UpperCamelCase__							)
           self.parent.assertEqual(result.last_hidden_state.shape		,					(self.batch_size, self.seq_length, self.d_model)							)



    def        snake_case_      (       self		,					UpperCamelCase__		,					UpperCamelCase__		,					UpperCamelCase__		,					UpperCamelCase__		,					UpperCamelCase__		,					UpperCamelCase__		,					UpperCamelCase__		,					)    ->   Optional[Any]:





           '''simple docstring'''
           A_						       =   TFFunnelBaseModel(config=UpperCamelCase__							)
           A_						       =   {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
           A_						       =   model(UpperCamelCase__							)

           A_						       =   [input_ids, input_mask]
           A_						       =   model(UpperCamelCase__							)

           A_						       =   model(UpperCamelCase__							)
           self.parent.assertEqual(result.last_hidden_state.shape		,					(self.batch_size, 2, self.d_model)							)

           A_						       =   False
           A_						       =   TFFunnelBaseModel(config=UpperCamelCase__							)
           A_						       =   model(UpperCamelCase__							)
           self.parent.assertEqual(result.last_hidden_state.shape		,					(self.batch_size, 3, self.d_model)							)

           A_						       =   False
           A_						       =   TFFunnelBaseModel(config=UpperCamelCase__							)
           A_						       =   model(UpperCamelCase__							)
           self.parent.assertEqual(result.last_hidden_state.shape		,					(self.batch_size, 2, self.d_model)							)



    def        snake_case_      (       self		,					UpperCamelCase__		,					UpperCamelCase__		,					UpperCamelCase__		,					UpperCamelCase__		,					UpperCamelCase__		,					UpperCamelCase__		,					UpperCamelCase__		,					)    ->   Optional[Any]:





           '''simple docstring'''
           A_						       =   TFFunnelForPreTraining(config=UpperCamelCase__							)
           A_						       =   {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
           A_						       =   model(UpperCamelCase__							)
           self.parent.assertEqual(result.logits.shape		,					(self.batch_size, self.seq_length)							)



    def        snake_case_      (       self		,					UpperCamelCase__		,					UpperCamelCase__		,					UpperCamelCase__		,					UpperCamelCase__		,					UpperCamelCase__		,					UpperCamelCase__		,					UpperCamelCase__		,					)    ->   Any:





           '''simple docstring'''
           A_						       =   TFFunnelForMaskedLM(config=UpperCamelCase__							)
           A_						       =   {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
           A_						       =   model(UpperCamelCase__							)
           self.parent.assertEqual(result.logits.shape		,					(self.batch_size, self.seq_length, self.vocab_size)							)



    def        snake_case_      (       self		,					UpperCamelCase__		,					UpperCamelCase__		,					UpperCamelCase__		,					UpperCamelCase__		,					UpperCamelCase__		,					UpperCamelCase__		,					UpperCamelCase__		,					)    ->   Dict:





           '''simple docstring'''
           A_						       =   self.num_labels
           A_						       =   TFFunnelForSequenceClassification(config=UpperCamelCase__							)
           A_						       =   {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
           A_						       =   model(UpperCamelCase__							)
           self.parent.assertEqual(result.logits.shape		,					(self.batch_size, self.num_labels)							)



    def        snake_case_      (       self		,					UpperCamelCase__		,					UpperCamelCase__		,					UpperCamelCase__		,					UpperCamelCase__		,					UpperCamelCase__		,					UpperCamelCase__		,					UpperCamelCase__		,					)    ->   Any:





           '''simple docstring'''
           A_						       =   self.num_choices
           A_						       =   TFFunnelForMultipleChoice(config=UpperCamelCase__							)
           A_						       =   tf.tile(tf.expand_dims(UpperCamelCase__		,					1							)		,					(1, self.num_choices, 1)							)
           A_						       =   tf.tile(tf.expand_dims(UpperCamelCase__		,					1							)		,					(1, self.num_choices, 1)							)
           A_						       =   tf.tile(tf.expand_dims(UpperCamelCase__		,					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(UpperCamelCase__							)
           self.parent.assertEqual(result.logits.shape		,					(self.batch_size, self.num_choices)							)



    def        snake_case_      (       self		,					UpperCamelCase__		,					UpperCamelCase__		,					UpperCamelCase__		,					UpperCamelCase__		,					UpperCamelCase__		,					UpperCamelCase__		,					UpperCamelCase__		,					)    ->   Any:





           '''simple docstring'''
           A_						       =   self.num_labels
           A_						       =   TFFunnelForTokenClassification(config=UpperCamelCase__							)
           A_						       =   {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
           A_						       =   model(UpperCamelCase__							)
           self.parent.assertEqual(result.logits.shape		,					(self.batch_size, self.seq_length, self.num_labels)							)



    def        snake_case_      (       self		,					UpperCamelCase__		,					UpperCamelCase__		,					UpperCamelCase__		,					UpperCamelCase__		,					UpperCamelCase__		,					UpperCamelCase__		,					UpperCamelCase__		,					)    ->   Dict:





           '''simple docstring'''
           A_						       =   TFFunnelForQuestionAnswering(config=UpperCamelCase__							)
           A_						       =   {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
           A_						       =   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        snake_case_      (       self							)    ->   str:





           '''simple docstring'''
           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        A__   (   _snake_case    ,    _snake_case    ,    unittest.TestCase      ):
    lowercase						       =		(
        (
            TFFunnelModel,
            TFFunnelForMaskedLM,
            TFFunnelForPreTraining,
            TFFunnelForQuestionAnswering,
            TFFunnelForTokenClassification,
        )
        if is_tf_available()
        else ()
    )
    lowercase						       =		(
        {
            "feature-extraction": (TFFunnelBaseModel, TFFunnelModel),
            "fill-mask": TFFunnelForMaskedLM,
            "question-answering": TFFunnelForQuestionAnswering,
            "text-classification": TFFunnelForSequenceClassification,
            "token-classification": TFFunnelForTokenClassification,
            "zero-shot": TFFunnelForSequenceClassification,
        }
        if is_tf_available()
        else {}
    )
    lowercase						       =		False
    lowercase						       =		False



    def        snake_case_      (       self							)    ->   Union[str, Any]:





           '''simple docstring'''
           A_						       =   TFFunnelModelTester(self							)
           A_						       =   ConfigTester(self		,					config_class=UpperCamelCase__							)



    def        snake_case_      (       self							)    ->   str:





           '''simple docstring'''
           self.config_tester.run_common_tests()



    def        snake_case_      (       self							)    ->   Tuple:





           '''simple docstring'''
           A_						       =   self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_model(*UpperCamelCase__							)



    def        snake_case_      (       self							)    ->   Any:





           '''simple docstring'''
           A_						       =   self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_pretraining(*UpperCamelCase__							)



    def        snake_case_      (       self							)    ->   Dict:





           '''simple docstring'''
           A_						       =   self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_masked_lm(*UpperCamelCase__							)



    def        snake_case_      (       self							)    ->   Union[str, Any]:





           '''simple docstring'''
           A_						       =   self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_token_classification(*UpperCamelCase__							)







    def        snake_case_      (       self							)    ->   int:





           '''simple docstring'''
           A_						       =   self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_question_answering(*UpperCamelCase__							)


@require_tf
class        A__   (   _snake_case    ,    unittest.TestCase      ):
    lowercase						       =		(
        (TFFunnelBaseModel, TFFunnelForMultipleChoice, TFFunnelForSequenceClassification) if is_tf_available() else ()
    )
    lowercase						       =		False
    lowercase						       =		False



    def        snake_case_      (       self							)    ->   List[Any]:





           '''simple docstring'''
           A_						       =   TFFunnelModelTester(self		,					base=UpperCamelCase__							)
           A_						       =   ConfigTester(self		,					config_class=UpperCamelCase__							)



    def        snake_case_      (       self							)    ->   int:





           '''simple docstring'''
           self.config_tester.run_common_tests()



    def        snake_case_      (       self							)    ->   str:





           '''simple docstring'''
           A_						       =   self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_base_model(*UpperCamelCase__							)



    def        snake_case_      (       self							)    ->   Optional[int]:





           '''simple docstring'''
           A_						       =   self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_sequence_classification(*UpperCamelCase__							)







    def        snake_case_      (       self							)    ->   str:





           '''simple docstring'''
           A_						       =   self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_multiple_choice(*UpperCamelCase__							)



'''simple docstring'''







import numpy as np
def    UpperCAmelCase__     (   UpperCAmelCase__,							UpperCAmelCase__,							UpperCAmelCase__ = 1e-12,							UpperCAmelCase__ = 1_00,							)      ->      tuple[float, np.ndarray]:
       assert np.shape(UpperCAmelCase__					)[0] == np.shape(UpperCAmelCase__					)[1]
       # Ensure proper dimensionality.
       assert np.shape(UpperCAmelCase__					)[0] == np.shape(UpperCAmelCase__					)[0]
       # Ensure inputs are either both complex or both real
       assert np.iscomplexobj(UpperCAmelCase__					) == np.iscomplexobj(UpperCAmelCase__					)
       A_						       =   np.iscomplexobj(UpperCAmelCase__					)
       if is_complex:
              # Ensure complex input_matrix is Hermitian
              assert np.array_equal(UpperCAmelCase__,							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.

       A_						       =   False
       A_						       =   0
       A_						       =   0
       A_						       =   1e12

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

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

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

              A_						       =   lambda_

       if is_complex:
              A_						       =   np.real(lambda_					)

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

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

              # Our implementation.
              A_       ,      A_						       =   power_iteration(UpperCAmelCase__,							UpperCAmelCase__					)

              # Numpy implementation.

              # Get eigenvalues and eigenvectors using built-in numpy
              # eigh (eigh used for symmetric or hermetian matrices).
              A_       ,      A_						       =   np.linalg.eigh(UpperCAmelCase__					)
              # Last eigenvalue is the maximum one.
              A_						       =   eigen_values[-1]
              # Last column in this matrix is eigenvector corresponding to largest eigenvalue.
              A_						       =   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(UpperCAmelCase__					) - np.abs(UpperCAmelCase__					)					) <= 1e-6


if __name__ == "__main__":
      import doctest

      doctest.testmod()
      test_power_iteration()
