

from typing import Any

import numpy as np

def 				A_					(							a					):

						"""simple docstring"""



						assert np.shape(a					)[0] == np.shape(a					)[1]

						# If matrix not symmetric, exit right away.
						if np.allclose(a   ,				matrix.T					) is False:
												return False

						# Get eigenvalues and eignevectors for a symmetric matrix.
						SCREAMING_SNAKE_CASE_			:					Optional[int]          =       np.linalg.eigh(a					)

						# Check sign of all eigenvalues.
						# np.all returns a value of type np.bool_
						return bool(np.all(eigen_values > 0					)					)

def 				A_					(							a					):

						"""simple docstring"""



						SCREAMING_SNAKE_CASE_			:					List[Any]          =       np.random.randn(a   ,				a					)
						SCREAMING_SNAKE_CASE_			:					str          =       np.dot(a   ,				random_matrix.T					)
						assert _is_matrix_spd(a					)
						return spd_matrix

def 				A_					(							a   ,				a   ,				a = 1_0_0_0   ,				a = 1e-8   ,				):

						"""simple docstring"""



						assert np.shape(a					)[0] == np.shape(a					)[1]
						assert np.shape(a					)[0] == np.shape(a					)[0]
						assert _is_matrix_spd(a					)

						# Initialize solution guess, residual, search direction.
						SCREAMING_SNAKE_CASE_			:					int          =       np.zeros((np.shape(a					)[0], 1)					)
						SCREAMING_SNAKE_CASE_			:					Union[str, Any]          =       np.copy(a					)
						SCREAMING_SNAKE_CASE_			:					List[Any]          =       np.copy(a					)

						# Set initial errors in solution guess and residual.
						SCREAMING_SNAKE_CASE_			:					Dict          =       1e9
						SCREAMING_SNAKE_CASE_			:					List[str]          =       1e9
						SCREAMING_SNAKE_CASE_			:					List[Any]          =       1e9

						# Set iteration counter to threshold number of iterations.
						SCREAMING_SNAKE_CASE_			:					str          =       0

						while error > tol:
												# Save this value so we only calculate the matrix-vector product once.
												SCREAMING_SNAKE_CASE_			:					List[Any]          =       np.dot(a   ,				a					)

												# The main algorithm.

												# Update search direction magnitude.
												SCREAMING_SNAKE_CASE_			:					Tuple          =       np.dot(ra.T   ,				a					) / np.dot(pa.T   ,				a					)
												# Update solution guess.
												SCREAMING_SNAKE_CASE_			:					List[str]          =       xa + alpha * pa
												# Calculate new residual.
												SCREAMING_SNAKE_CASE_			:					Dict          =       ra - alpha * w
												# Calculate new Krylov subspace scale.
												SCREAMING_SNAKE_CASE_			:					Union[str, Any]          =       np.dot(r.T   ,				a					) / np.dot(ra.T   ,				a					)
												# Calculate new A conjuage search direction.
												SCREAMING_SNAKE_CASE_			:					Optional[Any]          =       r + beta * pa

												# Calculate errors.
												SCREAMING_SNAKE_CASE_			:					Dict          =       np.linalg.norm(r - ra					)
												SCREAMING_SNAKE_CASE_			:					Tuple          =       np.linalg.norm(x - xa					)
												SCREAMING_SNAKE_CASE_			:					List[str]          =       np.maximum(a   ,				a					)

												# Update variables.
												SCREAMING_SNAKE_CASE_			:					Union[str, Any]          =       np.copy(a					)
												SCREAMING_SNAKE_CASE_			:					List[Any]          =       np.copy(a					)
												SCREAMING_SNAKE_CASE_			:					Optional[int]          =       np.copy(a					)

												# Update number of iterations.
												iterations += 1
												if iterations > max_iterations:
																		break

						return x

def 				A_					(							):

						"""simple docstring"""



						SCREAMING_SNAKE_CASE_			:					Union[str, Any]          =       3
						SCREAMING_SNAKE_CASE_			:					Dict          =       _create_spd_matrix(a					)
						SCREAMING_SNAKE_CASE_			:					Optional[Any]          =       np.random.randn(a   ,				1					)
						SCREAMING_SNAKE_CASE_			:					int          =       np.dot(a   ,				a					)

						# Numpy solution.
						SCREAMING_SNAKE_CASE_			:					int          =       np.linalg.solve(a   ,				a					)

						# Our implementation.
						SCREAMING_SNAKE_CASE_			:					Tuple          =       conjugate_gradient(a   ,				a					)

						# Ensure both solutions are close to x_true (and therefore one another).
						assert np.linalg.norm(x_numpy - x_true					) <= 1e-6
						assert np.linalg.norm(x_conjugate_gradient - x_true					) <= 1e-6


if __name__ == "__main__":
				import doctest

				doctest.testmod()
				test_conjugate_gradient()









import unittest

from transformers import ErnieMConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
    import torch

    from transformers import (
        ErnieMForInformationExtraction,
        ErnieMForMultipleChoice,
        ErnieMForQuestionAnswering,
        ErnieMForSequenceClassification,
        ErnieMForTokenClassification,
        ErnieMModel,
    )
    from transformers.models.ernie_m.modeling_ernie_m import ERNIE_M_PRETRAINED_MODEL_ARCHIVE_LIST
class 	_A						:

  def __init__(			self	,   _SCREAMING_SNAKE_CASE	,   _SCREAMING_SNAKE_CASE=13	,   _SCREAMING_SNAKE_CASE=7	,   _SCREAMING_SNAKE_CASE=True	,   _SCREAMING_SNAKE_CASE=True	,   _SCREAMING_SNAKE_CASE=True	,   _SCREAMING_SNAKE_CASE=99	,   _SCREAMING_SNAKE_CASE=32	,   _SCREAMING_SNAKE_CASE=5	,   _SCREAMING_SNAKE_CASE=4	,   _SCREAMING_SNAKE_CASE=37	,   _SCREAMING_SNAKE_CASE="gelu"	,   _SCREAMING_SNAKE_CASE=0.1	,   _SCREAMING_SNAKE_CASE=0.1	,   _SCREAMING_SNAKE_CASE=512	,   _SCREAMING_SNAKE_CASE=16	,   _SCREAMING_SNAKE_CASE=2	,   _SCREAMING_SNAKE_CASE=0.02	,   _SCREAMING_SNAKE_CASE=3	,   _SCREAMING_SNAKE_CASE=4	,   _SCREAMING_SNAKE_CASE=None	,   ):





        """simple docstring"""







        SCREAMING_SNAKE_CASE_			:					Tuple          =       parent
        SCREAMING_SNAKE_CASE_			:					Tuple          =       batch_size
        SCREAMING_SNAKE_CASE_			:					Dict          =       seq_length
        SCREAMING_SNAKE_CASE_			:					Optional[int]          =       is_training
        SCREAMING_SNAKE_CASE_			:					str          =       use_input_mask
        SCREAMING_SNAKE_CASE_			:					str          =       use_labels
        SCREAMING_SNAKE_CASE_			:					int          =       vocab_size
        SCREAMING_SNAKE_CASE_			:					Any          =       hidden_size
        SCREAMING_SNAKE_CASE_			:					int          =       num_hidden_layers
        SCREAMING_SNAKE_CASE_			:					Optional[Any]          =       num_attention_heads
        SCREAMING_SNAKE_CASE_			:					List[Any]          =       intermediate_size
        SCREAMING_SNAKE_CASE_			:					Dict          =       hidden_act
        SCREAMING_SNAKE_CASE_			:					Tuple          =       hidden_dropout_prob
        SCREAMING_SNAKE_CASE_			:					Optional[int]          =       attention_probs_dropout_prob
        SCREAMING_SNAKE_CASE_			:					Union[str, Any]          =       max_position_embeddings
        SCREAMING_SNAKE_CASE_			:					Optional[Any]          =       type_vocab_size
        SCREAMING_SNAKE_CASE_			:					str          =       type_sequence_label_size
        SCREAMING_SNAKE_CASE_			:					Dict          =       initializer_range
        SCREAMING_SNAKE_CASE_			:					Optional[Any]          =       num_labels
        SCREAMING_SNAKE_CASE_			:					Optional[int]          =       num_choices
        SCREAMING_SNAKE_CASE_			:					Union[str, Any]          =       scope

  def 	UpperCAmelCase      (			self   ):





        """simple docstring"""







        SCREAMING_SNAKE_CASE_			:					List[Any]          =       ids_tensor([self.batch_size, self.seq_length]	,   self.vocab_size   )

        SCREAMING_SNAKE_CASE_			:					Optional[Any]          =       None
        if self.use_input_mask:
              SCREAMING_SNAKE_CASE_			:					Tuple          =       random_attention_mask([self.batch_size, self.seq_length]   )

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

        SCREAMING_SNAKE_CASE_			:					int          =       self.get_config()

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

  def 	UpperCAmelCase      (			self   ):





        """simple docstring"""







        SCREAMING_SNAKE_CASE_			:					int          =       ids_tensor([self.batch_size, self.seq_length]	,   self.vocab_size   )

        SCREAMING_SNAKE_CASE_			:					int          =       None
        if self.use_input_mask:
              SCREAMING_SNAKE_CASE_			:					str          =       random_attention_mask([self.batch_size, self.seq_length]   )
        SCREAMING_SNAKE_CASE_			:					Optional[int]          =       self.get_config()

        return config, input_ids, input_mask

  def 	UpperCAmelCase      (			self   ):





        """simple docstring"""







        return ErnieMConfig(
            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	,   )

  def 	UpperCAmelCase      (			self	,   _SCREAMING_SNAKE_CASE	,   _SCREAMING_SNAKE_CASE	,   _SCREAMING_SNAKE_CASE	,   _SCREAMING_SNAKE_CASE	,   _SCREAMING_SNAKE_CASE	,   _SCREAMING_SNAKE_CASE   ):





        """simple docstring"""







        SCREAMING_SNAKE_CASE_			:					Optional[Any]          =       ErnieMModel(config=_SCREAMING_SNAKE_CASE   )
        model.to(_SCREAMING_SNAKE_CASE   )
        model.eval()
        SCREAMING_SNAKE_CASE_			:					Tuple          =       model(_SCREAMING_SNAKE_CASE	,   return_dict=_SCREAMING_SNAKE_CASE   )
        self.parent.assertEqual(result.last_hidden_state.shape	,   (self.batch_size, self.seq_length, self.hidden_size)   )

  def 	UpperCAmelCase      (			self	,   _SCREAMING_SNAKE_CASE	,   _SCREAMING_SNAKE_CASE	,   _SCREAMING_SNAKE_CASE	,   _SCREAMING_SNAKE_CASE	,   _SCREAMING_SNAKE_CASE	,   _SCREAMING_SNAKE_CASE   ):





        """simple docstring"""







        SCREAMING_SNAKE_CASE_			:					Union[str, Any]          =       ErnieMForQuestionAnswering(config=_SCREAMING_SNAKE_CASE   )
        model.to(_SCREAMING_SNAKE_CASE   )
        model.eval()
        SCREAMING_SNAKE_CASE_			:					Dict          =       model(
            _SCREAMING_SNAKE_CASE	,   attention_mask=_SCREAMING_SNAKE_CASE	,   start_positions=_SCREAMING_SNAKE_CASE	,   end_positions=_SCREAMING_SNAKE_CASE	,   )
        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	,   _SCREAMING_SNAKE_CASE	,   _SCREAMING_SNAKE_CASE	,   _SCREAMING_SNAKE_CASE	,   _SCREAMING_SNAKE_CASE	,   _SCREAMING_SNAKE_CASE	,   _SCREAMING_SNAKE_CASE   ):





        """simple docstring"""







        SCREAMING_SNAKE_CASE_			:					List[Any]          =       ErnieMForInformationExtraction(config=_SCREAMING_SNAKE_CASE   )
        model.to(_SCREAMING_SNAKE_CASE   )
        model.eval()
        SCREAMING_SNAKE_CASE_			:					List[str]          =       torch.ones_like(_SCREAMING_SNAKE_CASE	,   dtype=torch.floataa   )
        SCREAMING_SNAKE_CASE_			:					Optional[int]          =       model(
            _SCREAMING_SNAKE_CASE	,   attention_mask=_SCREAMING_SNAKE_CASE	,   start_positions=_SCREAMING_SNAKE_CASE	,   end_positions=_SCREAMING_SNAKE_CASE	,   )
        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	,   _SCREAMING_SNAKE_CASE	,   _SCREAMING_SNAKE_CASE	,   _SCREAMING_SNAKE_CASE	,   _SCREAMING_SNAKE_CASE	,   _SCREAMING_SNAKE_CASE	,   _SCREAMING_SNAKE_CASE   ):





        """simple docstring"""







        SCREAMING_SNAKE_CASE_			:					str          =       self.num_labels
        SCREAMING_SNAKE_CASE_			:					Tuple          =       ErnieMForSequenceClassification(_SCREAMING_SNAKE_CASE   )
        model.to(_SCREAMING_SNAKE_CASE   )
        model.eval()
        SCREAMING_SNAKE_CASE_			:					List[Any]          =       model(_SCREAMING_SNAKE_CASE	,   attention_mask=_SCREAMING_SNAKE_CASE	,   labels=_SCREAMING_SNAKE_CASE   )
        self.parent.assertEqual(result.logits.shape	,   (self.batch_size, self.num_labels)   )

  def 	UpperCAmelCase      (			self	,   _SCREAMING_SNAKE_CASE	,   _SCREAMING_SNAKE_CASE	,   _SCREAMING_SNAKE_CASE	,   _SCREAMING_SNAKE_CASE	,   _SCREAMING_SNAKE_CASE	,   _SCREAMING_SNAKE_CASE   ):





        """simple docstring"""







        SCREAMING_SNAKE_CASE_			:					Union[str, Any]          =       self.num_labels
        SCREAMING_SNAKE_CASE_			:					List[Any]          =       ErnieMForTokenClassification(config=_SCREAMING_SNAKE_CASE   )
        model.to(_SCREAMING_SNAKE_CASE   )
        model.eval()
        input_ids.to(_SCREAMING_SNAKE_CASE   )
        input_mask.to(_SCREAMING_SNAKE_CASE   )
        token_labels.to(_SCREAMING_SNAKE_CASE   )

        SCREAMING_SNAKE_CASE_			:					Optional[Any]          =       model(_SCREAMING_SNAKE_CASE	,   attention_mask=_SCREAMING_SNAKE_CASE	,   labels=_SCREAMING_SNAKE_CASE   )

        self.parent.assertEqual(result.logits.shape	,   (self.batch_size, self.seq_length, self.num_labels)   )

  def 	UpperCAmelCase      (			self	,   _SCREAMING_SNAKE_CASE	,   _SCREAMING_SNAKE_CASE	,   _SCREAMING_SNAKE_CASE	,   _SCREAMING_SNAKE_CASE	,   _SCREAMING_SNAKE_CASE	,   _SCREAMING_SNAKE_CASE   ):





        """simple docstring"""







        SCREAMING_SNAKE_CASE_			:					str          =       self.num_choices
        SCREAMING_SNAKE_CASE_			:					List[str]          =       ErnieMForMultipleChoice(config=_SCREAMING_SNAKE_CASE   )
        model.to(_SCREAMING_SNAKE_CASE   )
        model.eval()
        SCREAMING_SNAKE_CASE_			:					List[Any]          =       input_ids.unsqueeze(1   ).expand(-1	,   self.num_choices	,   -1   ).contiguous()
        SCREAMING_SNAKE_CASE_			:					Optional[Any]          =       input_mask.unsqueeze(1   ).expand(-1	,   self.num_choices	,   -1   ).contiguous()
        SCREAMING_SNAKE_CASE_			:					List[str]          =       model(
            _SCREAMING_SNAKE_CASE	,   attention_mask=_SCREAMING_SNAKE_CASE	,   labels=_SCREAMING_SNAKE_CASE	,   )
        self.parent.assertEqual(result.logits.shape	,   (self.batch_size, self.num_choices)   )



  def 	UpperCAmelCase      (			self   ):





        """simple docstring"""







        SCREAMING_SNAKE_CASE_			:					Any          =       self.prepare_config_and_inputs()
        (
            (
            SCREAMING_SNAKE_CASE_
        )    ,  (
            SCREAMING_SNAKE_CASE_
        )    ,  (
            SCREAMING_SNAKE_CASE_
        )    ,  (
            SCREAMING_SNAKE_CASE_
        )    ,  (
            SCREAMING_SNAKE_CASE_
        )    ,  (
            SCREAMING_SNAKE_CASE_
        )    ,  
        )			:					str          =       config_and_inputs
        SCREAMING_SNAKE_CASE_			:					Optional[int]          =       {'input_ids': input_ids, 'attention_mask': input_mask}
        return config, inputs_dict

@require_torch
class 	_A						(					__magic_name__				,       __magic_name__				,       unittest.TestCase):
  SCREAMING_SNAKE_CASE      : str    =					(
      (
          ErnieMModel,
          ErnieMForMultipleChoice,
          ErnieMForQuestionAnswering,
          ErnieMForSequenceClassification,
          ErnieMForTokenClassification,
      )
      if is_torch_available()
      else ()
  )
  SCREAMING_SNAKE_CASE      : Optional[int]    =					()
  SCREAMING_SNAKE_CASE      : Union[str, Any]    =					(
      {
          '''feature-extraction''': ErnieMModel,
          '''question-answering''': ErnieMForQuestionAnswering,
          '''text-classification''': ErnieMForSequenceClassification,
          '''token-classification''': ErnieMForTokenClassification,
          '''zero-shot''': ErnieMForSequenceClassification,
      }
      if is_torch_available()
      else {}
  )
  SCREAMING_SNAKE_CASE      : Union[str, Any]    =					False

  def 	UpperCAmelCase      (			self	,   _SCREAMING_SNAKE_CASE	,   _SCREAMING_SNAKE_CASE	,   _SCREAMING_SNAKE_CASE	,   _SCREAMING_SNAKE_CASE	,   _SCREAMING_SNAKE_CASE   ):





        """simple docstring"""







        if pipeline_test_casse_name == "QAPipelineTests":
              return True

        return False

  def 	UpperCAmelCase      (			self   ):





        """simple docstring"""







        SCREAMING_SNAKE_CASE_			:					Optional[int]          =       ErnieMModelTester(self   )
        SCREAMING_SNAKE_CASE_			:					Dict          =       ConfigTester(self	,   config_class=_SCREAMING_SNAKE_CASE	,   hidden_size=37   )

  def 	UpperCAmelCase      (			self   ):





        """simple docstring"""







        self.config_tester.run_common_tests()

  def 	UpperCAmelCase      (			self   ):





        """simple docstring"""







        SCREAMING_SNAKE_CASE_			:					Optional[Any]          =       self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*_SCREAMING_SNAKE_CASE   )

  def 	UpperCAmelCase      (			self   ):





        """simple docstring"""







        SCREAMING_SNAKE_CASE_			:					Dict          =       self.model_tester.prepare_config_and_inputs()
        for type in ["absolute", "relative_key", "relative_key_query"]:
              SCREAMING_SNAKE_CASE_			:					Optional[int]          =       type
              self.model_tester.create_and_check_model(*_SCREAMING_SNAKE_CASE   )

  def 	UpperCAmelCase      (			self   ):





        """simple docstring"""







        SCREAMING_SNAKE_CASE_			:					List[Any]          =       self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_multiple_choice(*_SCREAMING_SNAKE_CASE   )

  def 	UpperCAmelCase      (			self   ):





        """simple docstring"""







        SCREAMING_SNAKE_CASE_			:					int          =       self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_question_answering(*_SCREAMING_SNAKE_CASE   )

  def 	UpperCAmelCase      (			self   ):





        """simple docstring"""







        SCREAMING_SNAKE_CASE_			:					Union[str, Any]          =       self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_sequence_classification(*_SCREAMING_SNAKE_CASE   )

  def 	UpperCAmelCase      (			self   ):





        """simple docstring"""







        SCREAMING_SNAKE_CASE_			:					Tuple          =       self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_information_extraction(*_SCREAMING_SNAKE_CASE   )

  def 	UpperCAmelCase      (			self   ):





        """simple docstring"""







        SCREAMING_SNAKE_CASE_			:					int          =       self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_token_classification(*_SCREAMING_SNAKE_CASE   )



  @slow
  def 	UpperCAmelCase      (			self   ):





        """simple docstring"""







        for model_name in ERNIE_M_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
              SCREAMING_SNAKE_CASE_			:					int          =       ErnieMModel.from_pretrained(_SCREAMING_SNAKE_CASE   )
              self.assertIsNotNone(_SCREAMING_SNAKE_CASE   )

@require_torch
class 	_A						(					unittest.TestCase):

  @slow
  def 	UpperCAmelCase      (			self   ):





        """simple docstring"""







        SCREAMING_SNAKE_CASE_			:					Tuple          =       ErnieMModel.from_pretrained('susnato/ernie-m-base_pytorch'   )
        model.eval()
        SCREAMING_SNAKE_CASE_			:					List[str]          =       torch.tensor([[0, 1, 2, 3, 4, 5]]   )
        SCREAMING_SNAKE_CASE_			:					Tuple          =       model(_SCREAMING_SNAKE_CASE   )[0]

        # TODO Replace vocab size
        SCREAMING_SNAKE_CASE_			:					List[Any]          =       768

        SCREAMING_SNAKE_CASE_			:					Optional[int]          =       torch.Size((1, 6, hidden_size)   )
        self.assertEqual(output.shape	,   _SCREAMING_SNAKE_CASE   )

        SCREAMING_SNAKE_CASE_			:					Optional[Any]          =       torch.tensor(
            [[[-0.0012, 0.1245, -0.0214], [-0.0742, 0.0244, -0.0771], [-0.0333, 0.1164, -0.1554]]]   )

        self.assertTrue(torch.allclose(output[:, :3, :3]	,   _SCREAMING_SNAKE_CASE	,   atol=1e-3   )   )






