






import tempfile
import unittest

import numpy as np
import torch

from diffusers import DiffusionPipeline
from diffusers.models.attention_processor import Attention, AttnAddedKVProcessor
class        __snake_case(					unittest.TestCase      ):



 def 						UpperCAmelCase    (	self   :   Union[str, Any] ,		UpperCAmelCase__   :   bool = False	):

    """simple docstring"""






    lowercase__						:	List[str]         = 10

    if only_cross_attention:
       lowercase__						:	Any         = 12
    else:
       # when only cross attention is not set, the cross attention dim must be the same as the query dim
       lowercase__						:	Dict         = 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 						UpperCAmelCase    (	self   :   Union[str, Any] ,		UpperCAmelCase__   :   Optional[int] ,		UpperCAmelCase__   :   Optional[int]	):

    """simple docstring"""






    lowercase__						:	List[Any]         = 2

    lowercase__						:	int         = torch.rand(UpperCAmelCase__ ,		UpperCAmelCase__ ,		3 ,		2	)
    lowercase__						:	Optional[int]         = torch.rand(UpperCAmelCase__ ,		4 ,		UpperCAmelCase__	)
    lowercase__						:	List[str]         = None

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







 def 						UpperCAmelCase    (	self   :   Optional[Any]	):

    """simple docstring"""






    torch.manual_seed(0	)

    lowercase__						:	Union[str, Any]         = self.get_constructor_arguments(only_cross_attention=UpperCAmelCase__	)
    lowercase__						:	Union[str, Any]         = Attention(**UpperCAmelCase__	)

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

    lowercase__						:	List[Any]         = self.get_forward_arguments(
        query_dim=constructor_args["""query_dim"""] ,		added_kv_proj_dim=constructor_args["""added_kv_proj_dim"""]	)

    lowercase__						:	Any         = attn(**UpperCAmelCase__	)

    # only self attention

    torch.manual_seed(0	)

    lowercase__						:	List[str]         = self.get_constructor_arguments(only_cross_attention=UpperCAmelCase__	)
    lowercase__						:	int         = Attention(**UpperCAmelCase__	)

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

    lowercase__						:	Optional[Any]         = self.get_forward_arguments(
        query_dim=constructor_args["""query_dim"""] ,		added_kv_proj_dim=constructor_args["""added_kv_proj_dim"""]	)

    lowercase__						:	int         = attn(**UpperCAmelCase__	)

    self.assertTrue((only_cross_attn_out != self_and_cross_attn_out).all()	)
class        __snake_case(					unittest.TestCase      ):



 def 						UpperCAmelCase    (	self   :   List[str]	):

    """simple docstring"""






    lowercase__						:	Optional[Any]         = DiffusionPipeline.from_pretrained("""hf-internal-testing/tiny-stable-diffusion-pipe""" ,		safety_checker=UpperCAmelCase__	)

    lowercase__						:	Union[str, Any]         = pipe(
        """foo""" ,		num_inference_steps=2 ,		generator=torch.Generator("""cpu"""	).manual_seed(0	) ,		output_type="""np""" ,		).images

    # the initial conversion succeeds
    lowercase__						:	Union[str, Any]         = DiffusionPipeline.from_pretrained(
        """hf-internal-testing/tiny-stable-diffusion-pipe""" ,		device_map="""sequential""" ,		safety_checker=UpperCAmelCase__	)

    lowercase__						:	Dict         = 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(UpperCAmelCase__	)

       # can also load the converted weights
       lowercase__						:	str         = DiffusionPipeline.from_pretrained(UpperCAmelCase__ ,		device_map="""sequential""" ,		safety_checker=UpperCAmelCase__	)

    lowercase__						:	Optional[Any]         = pipe(
        """foo""" ,		num_inference_steps=2 ,		generator=torch.Generator("""cpu"""	).manual_seed(0	) ,		output_type="""np""" ,		).images

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










import os
import sys
import time

from . import transposition_cipher as trans_cipher
def       a__						(  ):
   lowercase__						:	Union[str, Any]         = """Prehistoric Men.txt"""
   lowercase__						:	Optional[Any]         = """Output.txt"""
   lowercase__						:	List[str]         = int(input("""Enter key: """					)					)
   lowercase__						:	Tuple         = input("""Encrypt/Decrypt [e/d]: """					)

   if not os.path.exists(A					):
      print(F'''File {input_file} does not exist. Quitting...'''					)
      sys.exit()
   if os.path.exists(A					):
      print(F'''Overwrite {output_file}? [y/n]'''					)
      lowercase__						:	List[Any]         = input("""> """					)
      if not response.lower().startswith("""y"""					):
         sys.exit()

   lowercase__						:	str         = time.time()
   if mode.lower().startswith("""e"""					):
      with open(A					) as f:
         lowercase__						:	Optional[int]         = f.read()
      lowercase__						:	List[str]         = trans_cipher.encrypt_message(A		,				A					)
   elif mode.lower().startswith("""d"""					):
      with open(A					) as f:
         lowercase__						:	int         = f.read()
      lowercase__						:	int         = trans_cipher.decrypt_message(A		,				A					)

   with open(A		,				"""w"""					) as output_obj:
      output_obj.write(A					)

   lowercase__						:	Optional[Any]         = round(time.time() - start_time		,				2					)
   print(("""Done (""", total_time, """seconds )""")					)


if __name__ == "__main__":
 main()


