





'''simple docstring'''


from .hash_table import HashTable
from .number_theory.prime_numbers import is_prime, next_prime



class 							__UpperCAmelCase  (       A__						):







		'''simple docstring'''







		def __init__(self						:      List[Any]      ,      *_lowerCAmelCase						:      Dict      ,      **_lowerCAmelCase						:      Optional[int]      ):
			super().__init__(*UpperCamelCase_      ,      **UpperCamelCase_      )







		def        A     (self						:      Any      ,      _lowerCAmelCase						:      Optional[int]      ,      _lowerCAmelCase						:      Optional[int]      ):
			A							  =      (
			    next_prime(value % self.size_table      )
			    if not is_prime(value % self.size_table      )
			    else value % self.size_table
			)  # gt = bigger than
			return next_prime_gt - (data % next_prime_gt)







		def        A     (self						:      str      ,      _lowerCAmelCase						:      List[str]      ,      _lowerCAmelCase						:      str      ,      _lowerCAmelCase						:      Tuple      ):
			return (increment * self.__hash_function_a(UpperCamelCase_      ,      UpperCamelCase_      )) % self.size_table







		def        A     (self						:      str      ,      _lowerCAmelCase						:      int      ,      _lowerCAmelCase						:      str=None      ):
			A							  =      1
			A							  =      self.hash_function(UpperCamelCase_      )

			while self.values[new_key] is not None and self.values[new_key] != key:
				A							  =      (
				    self.__hash_double_function(UpperCamelCase_      ,      UpperCamelCase_      ,      UpperCamelCase_      )
				    if self.balanced_factor() >= self.lim_charge
				    else None
				)
				if new_key is None:
					break
				else:
					i += 1

			return new_key








'''simple docstring'''


import tempfile
import unittest

import numpy as np
import torch

from diffusers import DiffusionPipeline
from diffusers.models.attention_processor import Attention, AttnAddedKVProcessor



class 							__UpperCAmelCase  (       unittest.TestCase						):







		'''simple docstring'''







		def        A     (self						:      Optional[int]      ,      _lowerCAmelCase						:      bool = False      ):
			A							  =      10

			if only_cross_attention:
				A							  =      12
			else:
				# when only cross attention is not set, the cross attention dim must be the same as the query dim
				A							  =      query_dim

			return {
			    "query_dim": query_dim,
			    "cross_attention_dim": cross_attention_dim,
			    "heads": 2,
			    "dim_head": 4,
			    "added_kv_proj_dim": 6,
			    "norm_num_groups": 1,
			    "only_cross_attention": only_cross_attention,
			    "processor": AttnAddedKVProcessor(),
			}







		def        A     (self						:      Optional[int]      ,      _lowerCAmelCase						:      Dict      ,      _lowerCAmelCase						:      Any      ):
			A							  =      2

			A							  =      torch.rand(_lowerCAmelCase      ,      _lowerCAmelCase      ,      3      ,      2      )
			A							  =      torch.rand(_lowerCAmelCase      ,      4      ,      _lowerCAmelCase      )
			A							  =      None

			return {
			    "hidden_states": hidden_states,
			    "encoder_hidden_states": encoder_hidden_states,
			    "attention_mask": attention_mask,
			}







		def        A     (self						:      Optional[Any]      ):
			# self and cross attention

			torch.manual_seed(0      )

			A							  =      self.get_constructor_arguments(only_cross_attention=_lowerCAmelCase      )
			A							  =      Attention(**_lowerCAmelCase      )

			self.assertTrue(attn.to_k is not None      )
			self.assertTrue(attn.to_v is not None      )

			A							  =      self.get_forward_arguments(
			    query_dim=constructor_args["""query_dim"""]      ,      added_kv_proj_dim=constructor_args["""added_kv_proj_dim"""]      )

			A							  =      attn(**_lowerCAmelCase      )

			# only self attention

			torch.manual_seed(0      )

			A							  =      self.get_constructor_arguments(only_cross_attention=_lowerCAmelCase      )
			A							  =      Attention(**_lowerCAmelCase      )

			self.assertTrue(attn.to_k is None      )
			self.assertTrue(attn.to_v is None      )

			A							  =      self.get_forward_arguments(
			    query_dim=constructor_args["""query_dim"""]      ,      added_kv_proj_dim=constructor_args["""added_kv_proj_dim"""]      )

			A							  =      attn(**_lowerCAmelCase      )

			self.assertTrue((only_cross_attn_out != self_and_cross_attn_out).all()      )






class 							__UpperCAmelCase  (       unittest.TestCase						):







		'''simple docstring'''







		def        A     (self						:      Any      ):
			A							  =      DiffusionPipeline.from_pretrained("""hf-internal-testing/tiny-stable-diffusion-pipe"""      ,      safety_checker=_lowerCAmelCase      )

			A							  =      pipe(
			    """foo"""      ,      num_inference_steps=2      ,      generator=torch.Generator("""cpu"""      ).manual_seed(0      )      ,      output_type="""np"""      ,      ).images

			# the initial conversion succeeds
			A							  =      DiffusionPipeline.from_pretrained(
			    """hf-internal-testing/tiny-stable-diffusion-pipe"""      ,      device_map="""sequential"""      ,      safety_checker=_lowerCAmelCase      )

			A							  =      pipe(
			    """foo"""      ,      num_inference_steps=2      ,      generator=torch.Generator("""cpu"""      ).manual_seed(0      )      ,      output_type="""np"""      ,      ).images

			with tempfile.TemporaryDirectory() as tmpdir:
				# save the converted model
				pipe.save_pretrained(_lowerCAmelCase      )

				# can also load the converted weights
				A							  =      DiffusionPipeline.from_pretrained(_lowerCAmelCase      ,      device_map="""sequential"""      ,      safety_checker=_lowerCAmelCase      )

			A							  =      pipe(
			    """foo"""      ,      num_inference_steps=2      ,      generator=torch.Generator("""cpu"""      ).manual_seed(0      )      ,      output_type="""np"""      ,      ).images

			self.assertTrue(np.allclose(_lowerCAmelCase      ,      _lowerCAmelCase      ,      atol=1e-5      )      )
			self.assertTrue(np.allclose(_lowerCAmelCase      ,      _lowerCAmelCase      ,      atol=1e-5      )      )

