





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







  '''simple docstring'''


  def __init__(		self :							List[str]       , __a :							str       , __a :							Optional[int]=13       , __a :							Dict=7       , __a :							List[Any]=True       , __a :							List[str]=True       , __a :							Dict=True       , __a :							str=True       , __a :							Any=99       , __a :							str=[1, 1, 2]       , __a :							Optional[Any]=1       , __a :							Any=32       , __a :							Union[str, Any]=4       , __a :							Union[str, Any]=8       , __a :							Any=37       , __a :							str="gelu_new"       , __a :							List[str]=0.1       , __a :							int=0.1       , __a :							Optional[Any]=0.0       , __a :							int=512       , __a :							int=3       , __a :							List[str]=0.02       , __a :							int=3       , __a :							Union[str, Any]=4       , __a :							int=None       , __a :							List[str]=False       , )		->					Optional[Any]:
        _UpperCamelCase   :						Union[str, Any]    =							parent
        _UpperCamelCase   :						Dict    =							batch_size
        _UpperCamelCase   :						Tuple    =							seq_length
        _UpperCamelCase   :						Tuple    =							is_training
        _UpperCamelCase   :						Any    =							use_input_mask
        _UpperCamelCase   :						Union[str, Any]    =							use_token_type_ids
        _UpperCamelCase   :						List[str]    =							use_labels
        _UpperCamelCase   :						int    =							vocab_size
        _UpperCamelCase   :						str    =							block_sizes
        _UpperCamelCase   :						str    =							num_decoder_layers
        _UpperCamelCase   :						List[str]    =							d_model
        _UpperCamelCase   :						int    =							n_head
        _UpperCamelCase   :						Any    =							d_head
        _UpperCamelCase   :						int    =							d_inner
        _UpperCamelCase   :						str    =							hidden_act
        _UpperCamelCase   :						Union[str, Any]    =							hidden_dropout
        _UpperCamelCase   :						Any    =							attention_dropout
        _UpperCamelCase   :						Union[str, Any]    =							activation_dropout
        _UpperCamelCase   :						Optional[Any]    =							max_position_embeddings
        _UpperCamelCase   :						List[Any]    =							type_vocab_size
        _UpperCamelCase   :						Union[str, Any]    =							2
        _UpperCamelCase   :						Tuple    =							num_labels
        _UpperCamelCase   :						List[Any]    =							num_choices
        _UpperCamelCase   :						List[str]    =							scope
        _UpperCamelCase   :						str    =							initializer_std

        # Used in the tests to check the size of the first attention layer
        _UpperCamelCase   :						str    =							n_head
        # Used in the tests to check the size of the first hidden state
        _UpperCamelCase   :						Optional[Any]    =							self.d_model
        # Used in the tests to check the number of output hidden states/attentions
        _UpperCamelCase   :						Any    =							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:
              _UpperCamelCase   :						int    =							self.num_hidden_layers + 2

  def  __SCREAMING_SNAKE_CASE					(		self :							List[Any]						)		->					Any:
        _UpperCamelCase   :						Dict    =							ids_tensor([self.batch_size, self.seq_length]       , self.vocab_size						)

        _UpperCamelCase   :						List[str]    =							None
        if self.use_input_mask:
              _UpperCamelCase   :						int    =							random_attention_mask([self.batch_size, self.seq_length]						)

        _UpperCamelCase   :						Tuple    =							None
        if self.use_token_type_ids:
              _UpperCamelCase   :						Optional[Any]    =							ids_tensor([self.batch_size, self.seq_length]       , self.type_vocab_size						)

        _UpperCamelCase   :						List[Any]    =							None
        _UpperCamelCase   :						Union[str, Any]    =							None
        _UpperCamelCase   :						Union[str, Any]    =							None
        if self.use_labels:
              _UpperCamelCase   :						str    =							ids_tensor([self.batch_size]       , self.type_sequence_label_size						)
              _UpperCamelCase   :						Union[str, Any]    =							ids_tensor([self.batch_size, self.seq_length]       , self.num_labels						)
              _UpperCamelCase   :						int    =							ids_tensor([self.batch_size]       , self.num_choices						)

        _UpperCamelCase   :						Optional[Any]    =							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  __SCREAMING_SNAKE_CASE					(		self :							Optional[Any]       , __a :							Dict       , __a :							int       , __a :							List[str]       , __a :							Optional[int]       , __a :							str       , __a :							str       , __a :							Optional[Any]       , )		->					Optional[Any]:
        _UpperCamelCase   :						List[str]    =							TFFunnelModel(config=__a						)
        _UpperCamelCase   :						Optional[int]    =							{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
        _UpperCamelCase   :						Dict    =							model(__a						)

        _UpperCamelCase   :						List[Any]    =							[input_ids, input_mask]
        _UpperCamelCase   :						List[str]    =							model(__a						)

        _UpperCamelCase   :						Union[str, Any]    =							model(__a						)
        self.parent.assertEqual(result.last_hidden_state.shape       , (self.batch_size, self.seq_length, self.d_model)						)

        _UpperCamelCase   :						int    =							False
        _UpperCamelCase   :						int    =							TFFunnelModel(config=__a						)
        _UpperCamelCase   :						Union[str, Any]    =							model(__a						)
        self.parent.assertEqual(result.last_hidden_state.shape       , (self.batch_size, self.seq_length, self.d_model)						)

        _UpperCamelCase   :						str    =							False
        _UpperCamelCase   :						Optional[Any]    =							TFFunnelModel(config=__a						)
        _UpperCamelCase   :						List[str]    =							model(__a						)
        self.parent.assertEqual(result.last_hidden_state.shape       , (self.batch_size, self.seq_length, self.d_model)						)

  def  __SCREAMING_SNAKE_CASE					(		self :							List[str]       , __a :							Tuple       , __a :							Tuple       , __a :							List[str]       , __a :							Dict       , __a :							Optional[Any]       , __a :							Optional[Any]       , __a :							Tuple       , )		->					List[str]:
        _UpperCamelCase   :						List[Any]    =							TFFunnelBaseModel(config=__a						)
        _UpperCamelCase   :						Union[str, Any]    =							{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
        _UpperCamelCase   :						Optional[int]    =							model(__a						)

        _UpperCamelCase   :						List[str]    =							[input_ids, input_mask]
        _UpperCamelCase   :						int    =							model(__a						)

        _UpperCamelCase   :						Union[str, Any]    =							model(__a						)
        self.parent.assertEqual(result.last_hidden_state.shape       , (self.batch_size, 2, self.d_model)						)

        _UpperCamelCase   :						Optional[int]    =							False
        _UpperCamelCase   :						Any    =							TFFunnelBaseModel(config=__a						)
        _UpperCamelCase   :						Union[str, Any]    =							model(__a						)
        self.parent.assertEqual(result.last_hidden_state.shape       , (self.batch_size, 3, self.d_model)						)

        _UpperCamelCase   :						Any    =							False
        _UpperCamelCase   :						Union[str, Any]    =							TFFunnelBaseModel(config=__a						)
        _UpperCamelCase   :						List[Any]    =							model(__a						)
        self.parent.assertEqual(result.last_hidden_state.shape       , (self.batch_size, 2, self.d_model)						)

  def  __SCREAMING_SNAKE_CASE					(		self :							Optional[Any]       , __a :							Union[str, Any]       , __a :							List[Any]       , __a :							Optional[Any]       , __a :							Dict       , __a :							str       , __a :							int       , __a :							List[str]       , )		->					Tuple:
        _UpperCamelCase   :						List[str]    =							TFFunnelForPreTraining(config=__a						)
        _UpperCamelCase   :						Optional[int]    =							{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
        _UpperCamelCase   :						Union[str, Any]    =							model(__a						)
        self.parent.assertEqual(result.logits.shape       , (self.batch_size, self.seq_length)						)

  def  __SCREAMING_SNAKE_CASE					(		self :							Union[str, Any]       , __a :							Any       , __a :							Any       , __a :							List[Any]       , __a :							int       , __a :							List[Any]       , __a :							List[str]       , __a :							Union[str, Any]       , )		->					Any:
        _UpperCamelCase   :						List[Any]    =							TFFunnelForMaskedLM(config=__a						)
        _UpperCamelCase   :						Dict    =							{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
        _UpperCamelCase   :						str    =							model(__a						)
        self.parent.assertEqual(result.logits.shape       , (self.batch_size, self.seq_length, self.vocab_size)						)

  def  __SCREAMING_SNAKE_CASE					(		self :							Optional[Any]       , __a :							List[str]       , __a :							Union[str, Any]       , __a :							List[Any]       , __a :							Tuple       , __a :							Optional[Any]       , __a :							List[Any]       , __a :							str       , )		->					List[str]:
        _UpperCamelCase   :						str    =							self.num_labels
        _UpperCamelCase   :						Union[str, Any]    =							TFFunnelForSequenceClassification(config=__a						)
        _UpperCamelCase   :						Optional[Any]    =							{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
        _UpperCamelCase   :						List[Any]    =							model(__a						)
        self.parent.assertEqual(result.logits.shape       , (self.batch_size, self.num_labels)						)

  def  __SCREAMING_SNAKE_CASE					(		self :							Any       , __a :							Optional[Any]       , __a :							int       , __a :							int       , __a :							Union[str, Any]       , __a :							Optional[int]       , __a :							Any       , __a :							Union[str, Any]       , )		->					Optional[Any]:
        _UpperCamelCase   :						str    =							self.num_choices
        _UpperCamelCase   :						Optional[int]    =							TFFunnelForMultipleChoice(config=__a						)
        _UpperCamelCase   :						List[Any]    =							tf.tile(tf.expand_dims(__a       , 1						)       , (1, self.num_choices, 1)						)
        _UpperCamelCase   :						Optional[Any]    =							tf.tile(tf.expand_dims(__a       , 1						)       , (1, self.num_choices, 1)						)
        _UpperCamelCase   :						Union[str, Any]    =							tf.tile(tf.expand_dims(__a       , 1						)       , (1, self.num_choices, 1)						)
        _UpperCamelCase   :						Any    =							{
            "input_ids": multiple_choice_inputs_ids,
            "attention_mask": multiple_choice_input_mask,
            "token_type_ids": multiple_choice_token_type_ids,
        }
        _UpperCamelCase   :						str    =							model(__a						)
        self.parent.assertEqual(result.logits.shape       , (self.batch_size, self.num_choices)						)

  def  __SCREAMING_SNAKE_CASE					(		self :							List[Any]       , __a :							List[str]       , __a :							Tuple       , __a :							Union[str, Any]       , __a :							int       , __a :							Union[str, Any]       , __a :							Union[str, Any]       , __a :							Optional[Any]       , )		->					List[Any]:
        _UpperCamelCase   :						Tuple    =							self.num_labels
        _UpperCamelCase   :						Union[str, Any]    =							TFFunnelForTokenClassification(config=__a						)
        _UpperCamelCase   :						int    =							{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
        _UpperCamelCase   :						Optional[Any]    =							model(__a						)
        self.parent.assertEqual(result.logits.shape       , (self.batch_size, self.seq_length, self.num_labels)						)

  def  __SCREAMING_SNAKE_CASE					(		self :							Any       , __a :							List[Any]       , __a :							List[str]       , __a :							Union[str, Any]       , __a :							Union[str, Any]       , __a :							Tuple       , __a :							Dict       , __a :							Union[str, Any]       , )		->					Optional[Any]:
        _UpperCamelCase   :						Dict    =							TFFunnelForQuestionAnswering(config=__a						)
        _UpperCamelCase   :						List[Any]    =							{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
        _UpperCamelCase   :						List[Any]    =							model(__a						)
        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 :							List[Any]						)		->					Optional[Any]:
        _UpperCamelCase   :						List[str]    =							self.prepare_config_and_inputs()
        (
            (
            _UpperCamelCase
        ),	(
            _UpperCamelCase
        ),	(
            _UpperCamelCase
        ),	(
            _UpperCamelCase
        ),	(
            _UpperCamelCase
        ),	(
            _UpperCamelCase
        ),	(
            _UpperCamelCase
        ),	
        )   :						int    =							config_and_inputs
        _UpperCamelCase   :						Optional[Any]    =							{"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
        return config, inputs_dict





@require_tf
class   __SCREAMING_SNAKE_CASE     (    _UpperCamelCase		,	_UpperCamelCase		,	unittest.TestCase ):







  '''simple docstring'''


  SCREAMING_SNAKE_CASE__				:Optional[int]						  =       (
      (
          TFFunnelModel,
          TFFunnelForMaskedLM,
          TFFunnelForPreTraining,
          TFFunnelForQuestionAnswering,
          TFFunnelForTokenClassification,
      )
      if is_tf_available()
      else ()
  )
  SCREAMING_SNAKE_CASE__				:str						  =       (
      {
          "feature-extraction": (TFFunnelBaseModel, TFFunnelModel),
          "fill-mask": TFFunnelForMaskedLM,
          "question-answering": TFFunnelForQuestionAnswering,
          "text-classification": TFFunnelForSequenceClassification,
          "token-classification": TFFunnelForTokenClassification,
          "zero-shot": TFFunnelForSequenceClassification,
      }
      if is_tf_available()
      else {}
  )
  SCREAMING_SNAKE_CASE__				:int						  =       False
  SCREAMING_SNAKE_CASE__				:str						  =       False

  def  __SCREAMING_SNAKE_CASE					(		self :							int						)		->					str:
        _UpperCamelCase   :						List[str]    =							TFFunnelModelTester(self						)
        _UpperCamelCase   :						Any    =							ConfigTester(self       , config_class=__a						)

  def  __SCREAMING_SNAKE_CASE					(		self :							Optional[Any]						)		->					int:
        self.config_tester.run_common_tests()

  def  __SCREAMING_SNAKE_CASE					(		self :							Optional[Any]						)		->					Optional[int]:
        _UpperCamelCase   :						Dict    =							self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*__a						)

  def  __SCREAMING_SNAKE_CASE					(		self :							Dict						)		->					str:
        _UpperCamelCase   :						List[Any]    =							self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_pretraining(*__a						)

  def  __SCREAMING_SNAKE_CASE					(		self :							Dict						)		->					Optional[Any]:
        _UpperCamelCase   :						Any    =							self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_masked_lm(*__a						)

  def  __SCREAMING_SNAKE_CASE					(		self :							Union[str, Any]						)		->					List[str]:
        _UpperCamelCase   :						Dict    =							self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_token_classification(*__a						)


  def  __SCREAMING_SNAKE_CASE					(		self :							int						)		->					Union[str, Any]:
        _UpperCamelCase   :						Any    =							self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_question_answering(*__a						)





@require_tf
class   __SCREAMING_SNAKE_CASE     (    _UpperCamelCase		,	unittest.TestCase ):







  '''simple docstring'''


  SCREAMING_SNAKE_CASE__				:List[Any]						  =       (
      (TFFunnelBaseModel, TFFunnelForMultipleChoice, TFFunnelForSequenceClassification) if is_tf_available() else ()
  )
  SCREAMING_SNAKE_CASE__				:Optional[Any]						  =       False
  SCREAMING_SNAKE_CASE__				:List[Any]						  =       False

  def  __SCREAMING_SNAKE_CASE					(		self :							Union[str, Any]						)		->					List[Any]:
        _UpperCamelCase   :						int    =							TFFunnelModelTester(self       , base=__a						)
        _UpperCamelCase   :						str    =							ConfigTester(self       , config_class=__a						)

  def  __SCREAMING_SNAKE_CASE					(		self :							Optional[int]						)		->					List[str]:
        self.config_tester.run_common_tests()

  def  __SCREAMING_SNAKE_CASE					(		self :							Optional[Any]						)		->					Any:
        _UpperCamelCase   :						Tuple    =							self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_base_model(*__a						)

  def  __SCREAMING_SNAKE_CASE					(		self :							str						)		->					Optional[int]:
        _UpperCamelCase   :						Union[str, Any]    =							self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_sequence_classification(*__a						)


  def  __SCREAMING_SNAKE_CASE					(		self :							Tuple						)		->					Optional[int]:
        _UpperCamelCase   :						Optional[int]    =							self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_multiple_choice(*__a						)







"""simple docstring"""


import numpy as np




def        lowercase__     (       lowercase_		,lowercase_		,lowercase_ = 1e-12		,lowercase_ = 100		,)       ->		tuple[float, np.ndarray]:




      """simple docstring"""

      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_      )
      _UpperCamelCase   :						Dict    =							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.

      _UpperCamelCase   :						List[Any]    =							False
      _UpperCamelCase   :						Optional[int]    =							0
      _UpperCamelCase   :						int    =							0
      _UpperCamelCase   :						Optional[int]    =							1e12

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

            # Check convergence.
            _UpperCamelCase   :						Optional[int]    =							np.abs(lambda_ - lambda_previous      ) / lambda_
            iterations += 1

            if error <= error_tol or iterations >= max_iterations:
                  _UpperCamelCase   :						List[Any]    =							True

            _UpperCamelCase   :						Tuple    =							lambda_

      if is_complex:
            _UpperCamelCase   :						Tuple    =							np.real(lambda_      )

      return lambda_, vector




def        lowercase__     (       )       ->		None:




      """simple docstring"""

      _UpperCamelCase   :						Dict    =							np.array([[41, 4, 20], [4, 26, 30], [20, 30, 50]]      )
      _UpperCamelCase   :						Optional[int]    =							np.array([41, 4, 20]      )
      _UpperCamelCase   :						Union[str, Any]    =							real_input_matrix.astype(np.complexaaa      )
      _UpperCamelCase   :						int    =							np.triu(1J * complex_input_matrix		,1      )
      complex_input_matrix += imag_matrix
      complex_input_matrix += -1 * imag_matrix.T
      _UpperCamelCase   :						Union[str, Any]    =							np.array([41, 4, 20]      ).astype(np.complexaaa      )

      for problem_type in ["real", "complex"]:
            if problem_type == "real":
                  _UpperCamelCase   :						Dict    =							real_input_matrix
                  _UpperCamelCase   :						Any    =							real_vector
            elif problem_type == "complex":
                  _UpperCamelCase   :						int    =							complex_input_matrix
                  _UpperCamelCase   :						Union[str, Any]    =							complex_vector

            # Our implementation.
            _UpperCamelCase,	_UpperCamelCase   :						int    =							power_iteration(lowercase_		,lowercase_      )

            # Numpy implementation.

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