
"""simple docstring"""



import gc
import unittest

import numpy as np
import torch
from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer

from diffusers import AutoencoderKL, DDIMScheduler, LDMTextToImagePipeline, UNetaDConditionModel
from diffusers.utils.testing_utils import (
    enable_full_determinism,
    load_numpy,
    nightly,
    require_torch_gpu,
    slow,
    torch_device,
)

from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_PARAMS
from ..test_pipelines_common import PipelineTesterMixin


enable_full_determinism()





class 				a			(					lowercase						,		unittest.TestCase      ):
      UpperCamelCase							:   Optional[int]           =     LDMTextToImagePipeline
      UpperCamelCase							:   List[str]           =     TEXT_TO_IMAGE_PARAMS - {
          """negative_prompt""",
          """negative_prompt_embeds""",
          """cross_attention_kwargs""",
          """prompt_embeds""",
      }
      UpperCamelCase							:   List[Any]           =     PipelineTesterMixin.required_optional_params - {
          """num_images_per_prompt""",
          """callback""",
          """callback_steps""",
      }
      UpperCamelCase							:   Any           =     TEXT_TO_IMAGE_BATCH_PARAMS
      UpperCamelCase							:   Any           =     False



      def  __snake_case							(   self      ):
          torch.manual_seed(0      )
          UpperCAmelCase__	:      List[str]     							=					UNetaDConditionModel(
              block_out_channels=(32, 64)     ,	layers_per_block=2     ,	sample_size=32     ,	in_channels=4     ,	out_channels=4     ,	down_block_types=('DownBlock2D', 'CrossAttnDownBlock2D')     ,	up_block_types=('CrossAttnUpBlock2D', 'UpBlock2D')     ,	cross_attention_dim=32     ,	)
          UpperCAmelCase__	:      List[Any]     							=					DDIMScheduler(
              beta_start=0.00085     ,	beta_end=0.012     ,	beta_schedule='scaled_linear'     ,	clip_sample=UpperCamelCase_     ,	set_alpha_to_one=UpperCamelCase_     ,	)
          torch.manual_seed(0      )
          UpperCAmelCase__	:      Dict     							=					AutoencoderKL(
              block_out_channels=(32, 64)     ,	in_channels=3     ,	out_channels=3     ,	down_block_types=('DownEncoderBlock2D', 'DownEncoderBlock2D')     ,	up_block_types=('UpDecoderBlock2D', 'UpDecoderBlock2D')     ,	latent_channels=4     ,	)
          torch.manual_seed(0      )
          UpperCAmelCase__	:      str     							=					CLIPTextConfig(
              bos_token_id=0     ,	eos_token_id=2     ,	hidden_size=32     ,	intermediate_size=37     ,	layer_norm_eps=1E-05     ,	num_attention_heads=4     ,	num_hidden_layers=5     ,	pad_token_id=1     ,	vocab_size=1_000     ,	)
          UpperCAmelCase__	:      Optional[int]     							=					CLIPTextModel(UpperCamelCase_      )
          UpperCAmelCase__	:      Optional[Any]     							=					CLIPTokenizer.from_pretrained('hf-internal-testing/tiny-random-clip'      )

          UpperCAmelCase__	:      Dict     							=					{
              'unet': unet,
              'scheduler': scheduler,
              'vqvae': vae,
              'bert': text_encoder,
              'tokenizer': tokenizer,
          }
          return components



      def  __snake_case							(   self     ,	UpperCamelCase_     ,	UpperCamelCase_=0      ):
          if str(UpperCamelCase_      ).startswith('mps'      ):
              UpperCAmelCase__	:      Optional[Any]     							=					torch.manual_seed(UpperCamelCase_      )
          else:
              UpperCAmelCase__	:      str     							=					torch.Generator(device=UpperCamelCase_      ).manual_seed(UpperCamelCase_      )
          UpperCAmelCase__	:      Dict     							=					{
              'prompt': 'A painting of a squirrel eating a burger',
              'generator': generator,
              'num_inference_steps': 2,
              'guidance_scale': 6.0,
              'output_type': 'numpy',
          }
          return inputs



      def  __snake_case							(   self      ):
          UpperCAmelCase__	:      List[str]     							=					'cpu'  # ensure determinism for the device-dependent torch.Generator

          UpperCAmelCase__	:      Optional[Any]     							=					self.get_dummy_components()
          UpperCAmelCase__	:      List[Any]     							=					LDMTextToImagePipeline(**UpperCamelCase_      )
          pipe.to(UpperCamelCase_      )
          pipe.set_progress_bar_config(disable=UpperCamelCase_      )

          UpperCAmelCase__	:      List[str]     							=					self.get_dummy_inputs(UpperCamelCase_      )
          UpperCAmelCase__	:      str     							=					pipe(**UpperCamelCase_      ).images
          UpperCAmelCase__	:      Dict     							=					image[0, -3:, -3:, -1]

          assert image.shape == (1, 16, 16, 3)
          UpperCAmelCase__	:      Tuple     							=					np.array([0.6101, 0.6156, 0.5622, 0.4895, 0.6661, 0.3804, 0.5748, 0.6136, 0.5014]      )

          assert np.abs(image_slice.flatten() - expected_slice      ).max() < 1E-3





@slow
@require_torch_gpu
class 				a			(					unittest.TestCase      ):



      def  __snake_case							(   self      ):
          super().tearDown()
          gc.collect()
          torch.cuda.empty_cache()



      def  __snake_case							(   self     ,	UpperCamelCase_     ,	UpperCamelCase_=torch.floataa     ,	UpperCamelCase_=0      ):
          UpperCAmelCase__	:      str     							=					torch.manual_seed(UpperCamelCase_      )
          UpperCAmelCase__	:      Optional[Any]     							=					np.random.RandomState(UpperCamelCase_      ).standard_normal((1, 4, 32, 32)      )
          UpperCAmelCase__	:      Optional[int]     							=					torch.from_numpy(UpperCamelCase_      ).to(device=UpperCamelCase_     ,	dtype=UpperCamelCase_      )
          UpperCAmelCase__	:      Any     							=					{
              'prompt': 'A painting of a squirrel eating a burger',
              'latents': latents,
              'generator': generator,
              'num_inference_steps': 3,
              'guidance_scale': 6.0,
              'output_type': 'numpy',
          }
          return inputs



      def  __snake_case							(   self      ):
          UpperCAmelCase__	:      List[str]     							=					LDMTextToImagePipeline.from_pretrained('CompVis/ldm-text2im-large-256'      ).to(UpperCamelCase_      )
          pipe.set_progress_bar_config(disable=UpperCamelCase_      )

          UpperCAmelCase__	:      Dict     							=					self.get_inputs(UpperCamelCase_      )
          UpperCAmelCase__	:      Tuple     							=					pipe(**UpperCamelCase_      ).images
          UpperCAmelCase__	:      Optional[Any]     							=					image[0, -3:, -3:, -1].flatten()

          assert image.shape == (1, 256, 256, 3)
          UpperCAmelCase__	:      Any     							=					np.array([0.51825, 0.52850, 0.52543, 0.54258, 0.52304, 0.52569, 0.54363, 0.55276, 0.56878]      )
          UpperCAmelCase__	:      Dict     							=					np.abs(expected_slice - image_slice      ).max()
          assert max_diff < 1E-3





@nightly
@require_torch_gpu
class 				a			(					unittest.TestCase      ):



      def  __snake_case							(   self      ):
          super().tearDown()
          gc.collect()
          torch.cuda.empty_cache()



      def  __snake_case							(   self     ,	UpperCamelCase_     ,	UpperCamelCase_=torch.floataa     ,	UpperCamelCase_=0      ):
          UpperCAmelCase__	:      Union[str, Any]     							=					torch.manual_seed(UpperCamelCase_      )
          UpperCAmelCase__	:      List[Any]     							=					np.random.RandomState(UpperCamelCase_      ).standard_normal((1, 4, 32, 32)      )
          UpperCAmelCase__	:      List[Any]     							=					torch.from_numpy(UpperCamelCase_      ).to(device=UpperCamelCase_     ,	dtype=UpperCamelCase_      )
          UpperCAmelCase__	:      Optional[Any]     							=					{
              'prompt': 'A painting of a squirrel eating a burger',
              'latents': latents,
              'generator': generator,
              'num_inference_steps': 50,
              'guidance_scale': 6.0,
              'output_type': 'numpy',
          }
          return inputs



      def  __snake_case							(   self      ):
          UpperCAmelCase__	:      str     							=					LDMTextToImagePipeline.from_pretrained('CompVis/ldm-text2im-large-256'      ).to(UpperCamelCase_      )
          pipe.set_progress_bar_config(disable=UpperCamelCase_      )

          UpperCAmelCase__	:      Dict     							=					self.get_inputs(UpperCamelCase_      )
          UpperCAmelCase__	:      Optional[Any]     							=					pipe(**UpperCamelCase_      ).images[0]

          UpperCAmelCase__	:      Any     							=					load_numpy(
              'https://huggingface.co/datasets/diffusers/test-arrays/resolve/main/ldm_text2img/ldm_large_256_ddim.npy'      )
          UpperCAmelCase__	:      int     							=					np.abs(expected_image - image      ).max()
          assert max_diff < 1E-3



"""simple docstring"""



def 					lowerCamelCase   (     _snake_case    ,_snake_case ):
    UpperCAmelCase__	:      Optional[int]     							=					''
    for i in table:
        res += inp[i - 1]
    return res
def 					lowerCamelCase   (     _snake_case ):
    return data[1:] + data[0]
def 					lowerCamelCase   (     _snake_case    ,_snake_case ):
    UpperCAmelCase__	:      str     							=					''
    for i in range(len(_snake_case ) ):
        if a[i] == b[i]:
            res += "0"
        else:
            res += "1"
    return res
def 					lowerCamelCase   (     _snake_case    ,_snake_case ):
    UpperCAmelCase__	:      str     							=					int('0b' + data[0] + data[-1]    ,2 )
    UpperCAmelCase__	:      Optional[Any]     							=					int('0b' + data[1:3]    ,2 )
    return bin(s[row][col] )[2:]
def 					lowerCamelCase   (     _snake_case    ,_snake_case    ,_snake_case    ,_snake_case    ,_snake_case ):
    UpperCAmelCase__	:      Any     							=					message[:4]
    UpperCAmelCase__	:      Optional[Any]     							=					message[4:]
    UpperCAmelCase__	:      Dict     							=					apply_table(_snake_case    ,_snake_case )
    UpperCAmelCase__	:      Optional[Any]     							=					xor(_snake_case    ,_snake_case )
    UpperCAmelCase__	:      Dict     							=					apply_sbox(_snake_case    ,temp[:4] )  # noqa: E741
    UpperCAmelCase__	:      Tuple     							=					apply_sbox(_snake_case    ,temp[4:] )
    UpperCAmelCase__	:      List[str]     							=					'0' * (2 - len(_snake_case )) + l  # noqa: E741
    UpperCAmelCase__	:      Optional[Any]     							=					'0' * (2 - len(_snake_case )) + r
    UpperCAmelCase__	:      Optional[int]     							=					apply_table(l + r    ,_snake_case )
    UpperCAmelCase__	:      Any     							=					xor(_snake_case    ,_snake_case )
    return temp + right


if __name__ == "__main__":
    UpperCamelCase__          =	input('Enter 10 bit key: ')
    UpperCamelCase__          =	input('Enter 8 bit message: ')

    UpperCamelCase__          =	[6, 3, 7, 4, 8, 5, 10, 9]
    UpperCamelCase__          =	[3, 5, 2, 7, 4, 10, 1, 9, 8, 6]
    UpperCamelCase__          =	[2, 4, 3, 1]
    UpperCamelCase__          =	[2, 6, 3, 1, 4, 8, 5, 7]
    UpperCamelCase__          =	[4, 1, 3, 5, 7, 2, 8, 6]
    UpperCamelCase__          =	[4, 1, 2, 3, 2, 3, 4, 1]
    UpperCamelCase__          =	[[1, 0, 3, 2], [3, 2, 1, 0], [0, 2, 1, 3], [3, 1, 3, 2]]
    UpperCamelCase__          =	[[0, 1, 2, 3], [2, 0, 1, 3], [3, 0, 1, 0], [2, 1, 0, 3]]

    # key generation
    UpperCamelCase__          =	apply_table(key, paa_table)
    UpperCamelCase__          =	temp[:5]
    UpperCamelCase__          =	temp[5:]
    UpperCamelCase__          =	left_shift(left)
    UpperCamelCase__          =	left_shift(right)
    UpperCamelCase__          =	apply_table(left + right, pa_table)
    UpperCamelCase__          =	left_shift(left)
    UpperCamelCase__          =	left_shift(right)
    UpperCamelCase__          =	left_shift(left)
    UpperCamelCase__          =	left_shift(right)
    UpperCamelCase__          =	apply_table(left + right, pa_table)

    # encryption
    UpperCamelCase__          =	apply_table(message, IP)
    UpperCamelCase__          =	function(expansion, sa, sa, keya, temp)
    UpperCamelCase__          =	temp[4:] + temp[:4]
    UpperCamelCase__          =	function(expansion, sa, sa, keya, temp)
    UpperCamelCase__          =	apply_table(temp, IP_inv)
    print('Cipher text is:', CT)

    # decryption
    UpperCamelCase__          =	apply_table(CT, IP)
    UpperCamelCase__          =	function(expansion, sa, sa, keya, temp)
    UpperCamelCase__          =	temp[4:] + temp[:4]
    UpperCamelCase__          =	function(expansion, sa, sa, keya, temp)
    UpperCamelCase__          =	apply_table(temp, IP_inv)
    print('Plain text after decypting is:', PT)

