



import numpy as np



def    snake_case_				(    _SCREAMING_SNAKE_CASE  ,	_SCREAMING_SNAKE_CASE  ,	_SCREAMING_SNAKE_CASE = 1E-1_2  ,	_SCREAMING_SNAKE_CASE = 1_0_0  ,	):
	assert np.shape(_SCREAMING_SNAKE_CASE   )[0] == np.shape(_SCREAMING_SNAKE_CASE   )[1]
	# Ensure proper dimensionality.
	assert np.shape(_SCREAMING_SNAKE_CASE   )[0] == np.shape(_SCREAMING_SNAKE_CASE   )[0]
	# Ensure inputs are either both complex or both real
	assert np.iscomplexobj(_SCREAMING_SNAKE_CASE   ) == np.iscomplexobj(_SCREAMING_SNAKE_CASE   )
	__lowercase		       =			np.iscomplexobj(_SCREAMING_SNAKE_CASE   )
	if is_complex:
		# Ensure complex input_matrix is Hermitian
		assert np.array_equal(_SCREAMING_SNAKE_CASE  ,	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.

	__lowercase		       =			False
	__lowercase		       =			0
	__lowercase		       =			0
	__lowercase		       =			1E1_2

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

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

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

		__lowercase		       =			lambda_

	if is_complex:
		__lowercase		       =			np.real(lambda_   )

	return lambda_, vector



def    snake_case_				(    ):
	__lowercase		       =			np.array([[4_1, 4, 2_0], [4, 2_6, 3_0], [2_0, 3_0, 5_0]]   )
	__lowercase		       =			np.array([4_1, 4, 2_0]   )
	__lowercase		       =			real_input_matrix.astype(np.complexaaa   )
	__lowercase		       =			np.triu(1j * complex_input_matrix  ,	1   )
	complex_input_matrix += imag_matrix
	complex_input_matrix += -1 * imag_matrix.T
	__lowercase		       =			np.array([4_1, 4, 2_0]   ).astype(np.complexaaa   )

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

		# Our implementation.
		__lowercase       ,    __lowercase		       =			power_iteration(_SCREAMING_SNAKE_CASE  ,	_SCREAMING_SNAKE_CASE   )

		# Numpy implementation.

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


if __name__ == "__main__":
	import doctest

	doctest.testmod()
	test_power_iteration()








import torch
from torch import nn

from ...configuration_utils import ConfigMixin, register_to_config
from ...models import ModelMixin

class   _A   ( _lowercase  ,						_lowercase	):


						'''simple docstring'''





						@register_to_config
						def __init__(	self	:							Optional[Any]							,      *,
						    lowerCamelCase	:							int = 4							,      lowerCamelCase	:							int = 768							,      lowerCamelCase	:							int							,      lowerCamelCase	:							Optional[int]							,      ):






							'''simple docstring'''



							super().__init__()

							__lowercase		       =			nn.Parameter(torch.zeros(lowerCamelCase	)	)

							# parameters for additional clip time embeddings
							__lowercase		       =			nn.Linear(lowerCamelCase							,      lowerCamelCase	)
							__lowercase		       =			nn.Linear(lowerCamelCase							,      lowerCamelCase	)

							# parameters for encoder hidden states
							__lowercase		       =			clip_extra_context_tokens
							__lowercase		       =			nn.Linear(
							    lowerCamelCase							,      self.clip_extra_context_tokens * cross_attention_dim	)
							__lowercase		       =			nn.Linear(lowerCamelCase							,      lowerCamelCase	)
							__lowercase		       =			nn.LayerNorm(lowerCamelCase	)



						def     _snake_case	(	self	:							Union[str, Any]							,      *, lowerCamelCase	:							Any							,      lowerCamelCase	:							Tuple							,      lowerCamelCase	:							Optional[int]							,      lowerCamelCase	:							Tuple	):






							'''simple docstring'''



							if do_classifier_free_guidance:
								# Add the classifier free guidance embeddings to the image embeddings
								__lowercase		       =			image_embeddings.shape[0]
								__lowercase		       =			self.learned_classifier_free_guidance_embeddings.unsqueeze(0	)
								__lowercase		       =			classifier_free_guidance_embeddings.expand(
								    lowerCamelCase							,      -1	)
								__lowercase		       =			torch.cat([classifier_free_guidance_embeddings, image_embeddings]							,      dim=0	)

							# The image embeddings batch size and the text embeddings batch size are equal
							assert image_embeddings.shape[0] == prompt_embeds.shape[0]

							__lowercase		       =			prompt_embeds.shape[0]

							# "Specifically, we modify the architecture described in Nichol et al. (2021) by projecting and
							# adding CLIP embeddings to the existing timestep embedding, ...
							__lowercase		       =			self.embedding_proj(lowerCamelCase	)
							__lowercase		       =			self.clip_image_embeddings_project_to_time_embeddings(lowerCamelCase	)
							__lowercase		       =			time_projected_image_embeddings + time_projected_prompt_embeds

							# ... and by projecting CLIP embeddings into four
							# extra tokens of context that are concatenated to the sequence of outputs from the GLIDE text encoder"
							__lowercase		       =			self.clip_extra_context_tokens_proj(lowerCamelCase	)
							__lowercase		       =			clip_extra_context_tokens.reshape(lowerCamelCase							,      -1							,      self.clip_extra_context_tokens	)
							__lowercase		       =			clip_extra_context_tokens.permute(0							,      2							,      1	)

							__lowercase		       =			self.encoder_hidden_states_proj(lowerCamelCase	)
							__lowercase		       =			self.text_encoder_hidden_states_norm(lowerCamelCase	)
							__lowercase		       =			torch.cat([clip_extra_context_tokens, text_encoder_hidden_states]							,      dim=1	)

							return text_encoder_hidden_states, additive_clip_time_embeddings



