



'''simple docstring'''




import numpy as np


def        lowerCAmelCase_     (			_lowerCamelCase:     np.ndarray ,   _lowerCamelCase:     np.ndarray ,   _lowerCamelCase:     float = 1E-12 ,   _lowerCamelCase:     int = 1_00 ,   ):
		assert np.shape(_lowerCamelCase      )[0] == np.shape(_lowerCamelCase      )[1]
		# Ensure proper dimensionality.
		assert np.shape(_lowerCamelCase      )[0] == np.shape(_lowerCamelCase      )[0]
		# Ensure inputs are either both complex or both real
		assert np.iscomplexobj(_lowerCamelCase      ) == np.iscomplexobj(_lowerCamelCase      )
		__SCREAMING_SNAKE_CASE :	List[Any]					 =	np.iscomplexobj(_lowerCamelCase      )
		if is_complex:
				# Ensure complex input_matrix is Hermitian
				assert np.array_equal(_lowerCamelCase ,   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.

		__SCREAMING_SNAKE_CASE :	Tuple					 =	False
		__SCREAMING_SNAKE_CASE :	int					 =	0
		__SCREAMING_SNAKE_CASE :	List[Any]					 =	0
		__SCREAMING_SNAKE_CASE :	List[Any]					 =	1E12

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

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

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

				__SCREAMING_SNAKE_CASE :	Tuple					 =	lambda_

		if is_complex:
				__SCREAMING_SNAKE_CASE :	Any					 =	np.real(lambda_      )

		return lambda_, vector


def        lowerCAmelCase_     (			):
		__SCREAMING_SNAKE_CASE :	Optional[Any]					 =	np.array([[41, 4, 20], [4, 26, 30], [20, 30, 50]]      )
		__SCREAMING_SNAKE_CASE :	Tuple					 =	np.array([41, 4, 20]      )
		__SCREAMING_SNAKE_CASE :	str					 =	real_input_matrix.astype(np.complexaaa      )
		__SCREAMING_SNAKE_CASE :	Any					 =	np.triu(1J * complex_input_matrix ,   1      )
		complex_input_matrix += imag_matrix
		complex_input_matrix += -1 * imag_matrix.T
		__SCREAMING_SNAKE_CASE :	List[str]					 =	np.array([41, 4, 20]      ).astype(np.complexaaa      )

		for problem_type in ["real", "complex"]:
				if problem_type == "real":
						__SCREAMING_SNAKE_CASE :	Any					 =	real_input_matrix
						__SCREAMING_SNAKE_CASE :	Dict					 =	real_vector
				elif problem_type == "complex":
						__SCREAMING_SNAKE_CASE :	Any					 =	complex_input_matrix
						__SCREAMING_SNAKE_CASE :	Optional[Any]					 =	complex_vector

				# Our implementation.
				__SCREAMING_SNAKE_CASE :	str					 =	power_iteration(_lowerCamelCase ,   _lowerCamelCase      )

				# Numpy implementation.

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


if __name__ == "__main__":
						import doctest

						doctest.testmod()
						test_power_iteration()




'''simple docstring'''




UpperCamelCase__ :			List[str]												=				'''0.21.0'''

from .accelerator import Accelerator
from .big_modeling import (
    cpu_offload,
    cpu_offload_with_hook,
    disk_offload,
    dispatch_model,
    init_empty_weights,
    init_on_device,
    load_checkpoint_and_dispatch,
)
from .data_loader import skip_first_batches
from .launchers import debug_launcher, notebook_launcher
from .state import PartialState
from .utils import (
    DeepSpeedPlugin,
    DistributedDataParallelKwargs,
    DistributedType,
    FullyShardedDataParallelPlugin,
    GradScalerKwargs,
    InitProcessGroupKwargs,
    find_executable_batch_size,
    infer_auto_device_map,
    is_rich_available,
    load_checkpoint_in_model,
    synchronize_rng_states,
)


if is_rich_available():
						from .utils import rich