





'''simple docstring'''

import random
import unittest

import numpy as np

from diffusers import (
    DPMSolverMultistepScheduler,
    EulerAncestralDiscreteScheduler,
    EulerDiscreteScheduler,
    LMSDiscreteScheduler,
    OnnxStableDiffusionImgaImgPipeline,
    PNDMScheduler,
)
from diffusers.utils import floats_tensor
from diffusers.utils.testing_utils import (
    is_onnx_available,
    load_image,
    nightly,
    require_onnxruntime,
    require_torch_gpu,
)

from ..test_pipelines_onnx_common import OnnxPipelineTesterMixin


if is_onnx_available():
   import onnxruntime as ort
class 							__lowerCAmelCase	(       __magic_name__		,		unittest.TestCase					):






       """simple docstring"""
       _snake_case      :	Optional[Any]									=       'hf-internal-testing/tiny-random-OnnxStableDiffusionPipeline'





       def snake_case__		(				self  :						Dict			,					lowerCAmelCase__  :						str=0	)       ->	Union[str, Any]:

            '''simple docstring'''




            _UpperCamelCase							       =			floats_tensor((1, 3, 128, 128)			,					rng=random.Random(lowerCAmelCase__	)	)
            _UpperCamelCase							       =			np.random.RandomState(lowerCAmelCase__	)
            _UpperCamelCase							       =			{
                '''prompt''': '''A painting of a squirrel eating a burger''',
                '''image''': image,
                '''generator''': generator,
                '''num_inference_steps''': 3,
                '''strength''': 0.75,
                '''guidance_scale''': 7.5,
                '''output_type''': '''numpy''',
            }
            return inputs





       def snake_case__		(				self  :						str	)       ->	Tuple:

            '''simple docstring'''




            _UpperCamelCase							       =			OnnxStableDiffusionImgaImgPipeline.from_pretrained(self.hub_checkpoint			,					provider='''CPUExecutionProvider'''	)
            pipe.set_progress_bar_config(disable=lowerCAmelCase__	)

            _UpperCamelCase							       =			self.get_dummy_inputs()
            _UpperCamelCase							       =			pipe(**lowerCAmelCase__	).images
            _UpperCamelCase							       =			image[0, -3:, -3:, -1].flatten()

            assert image.shape == (1, 128, 128, 3)
            _UpperCamelCase							       =			np.array([0.69643, 0.58484, 0.50314, 0.58760, 0.55368, 0.59643, 0.51529, 0.41217, 0.49087]	)
            assert np.abs(image_slice - expected_slice	).max() < 1e-1





       def snake_case__		(				self  :						List[Any]	)       ->	Optional[int]:

            '''simple docstring'''




            _UpperCamelCase							       =			OnnxStableDiffusionImgaImgPipeline.from_pretrained(self.hub_checkpoint			,					provider='''CPUExecutionProvider'''	)
            _UpperCamelCase							       =			PNDMScheduler.from_config(pipe.scheduler.config			,					skip_prk_steps=lowerCAmelCase__	)
            pipe.set_progress_bar_config(disable=lowerCAmelCase__	)

            _UpperCamelCase							       =			self.get_dummy_inputs()
            _UpperCamelCase							       =			pipe(**lowerCAmelCase__	).images
            _UpperCamelCase							       =			image[0, -3:, -3:, -1]

            assert image.shape == (1, 128, 128, 3)
            _UpperCamelCase							       =			np.array([0.61737, 0.54642, 0.53183, 0.54465, 0.52742, 0.60525, 0.49969, 0.40655, 0.48154]	)

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





       def snake_case__		(				self  :						Any	)       ->	Optional[int]:

            '''simple docstring'''




            _UpperCamelCase							       =			OnnxStableDiffusionImgaImgPipeline.from_pretrained(self.hub_checkpoint			,					provider='''CPUExecutionProvider'''	)
            _UpperCamelCase							       =			LMSDiscreteScheduler.from_config(pipe.scheduler.config	)
            pipe.set_progress_bar_config(disable=lowerCAmelCase__	)

            # warmup pass to apply optimizations
            _UpperCamelCase							       =			pipe(**self.get_dummy_inputs()	)

            _UpperCamelCase							       =			self.get_dummy_inputs()
            _UpperCamelCase							       =			pipe(**lowerCAmelCase__	).images
            _UpperCamelCase							       =			image[0, -3:, -3:, -1]

            assert image.shape == (1, 128, 128, 3)
            _UpperCamelCase							       =			np.array([0.52761, 0.59977, 0.49033, 0.49619, 0.54282, 0.50311, 0.47600, 0.40918, 0.45203]	)

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





       def snake_case__		(				self  :						str	)       ->	Optional[int]:

            '''simple docstring'''




            _UpperCamelCase							       =			OnnxStableDiffusionImgaImgPipeline.from_pretrained(self.hub_checkpoint			,					provider='''CPUExecutionProvider'''	)
            _UpperCamelCase							       =			EulerDiscreteScheduler.from_config(pipe.scheduler.config	)
            pipe.set_progress_bar_config(disable=lowerCAmelCase__	)

            _UpperCamelCase							       =			self.get_dummy_inputs()
            _UpperCamelCase							       =			pipe(**lowerCAmelCase__	).images
            _UpperCamelCase							       =			image[0, -3:, -3:, -1]

            assert image.shape == (1, 128, 128, 3)
            _UpperCamelCase							       =			np.array([0.52911, 0.60004, 0.49229, 0.49805, 0.54502, 0.50680, 0.47777, 0.41028, 0.45304]	)

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





       def snake_case__		(				self  :						Tuple	)       ->	Any:

            '''simple docstring'''




            _UpperCamelCase							       =			OnnxStableDiffusionImgaImgPipeline.from_pretrained(self.hub_checkpoint			,					provider='''CPUExecutionProvider'''	)
            _UpperCamelCase							       =			EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config	)
            pipe.set_progress_bar_config(disable=lowerCAmelCase__	)

            _UpperCamelCase							       =			self.get_dummy_inputs()
            _UpperCamelCase							       =			pipe(**lowerCAmelCase__	).images
            _UpperCamelCase							       =			image[0, -3:, -3:, -1]

            assert image.shape == (1, 128, 128, 3)
            _UpperCamelCase							       =			np.array([0.52911, 0.60004, 0.49229, 0.49805, 0.54502, 0.50680, 0.47777, 0.41028, 0.45304]	)

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







       def snake_case__		(				self  :						List[Any]	)       ->	Dict:

            '''simple docstring'''




            _UpperCamelCase							       =			OnnxStableDiffusionImgaImgPipeline.from_pretrained(self.hub_checkpoint			,					provider='''CPUExecutionProvider'''	)
            _UpperCamelCase							       =			DPMSolverMultistepScheduler.from_config(pipe.scheduler.config	)
            pipe.set_progress_bar_config(disable=lowerCAmelCase__	)

            _UpperCamelCase							       =			self.get_dummy_inputs()
            _UpperCamelCase							       =			pipe(**lowerCAmelCase__	).images
            _UpperCamelCase							       =			image[0, -3:, -3:, -1]

            assert image.shape == (1, 128, 128, 3)
            _UpperCamelCase							       =			np.array([0.65331, 0.58277, 0.48204, 0.56059, 0.53665, 0.56235, 0.50969, 0.40009, 0.46552]	)

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

@nightly
@require_onnxruntime
@require_torch_gpu
class 							__lowerCAmelCase	(       unittest.TestCase					):






       """simple docstring"""





       @property
       def snake_case__		(				self  :						List[Any]	)       ->	Dict:

            '''simple docstring'''




            return (
                "CUDAExecutionProvider",
                {
                    "gpu_mem_limit": "15000000000",  # 15GB
                    "arena_extend_strategy": "kSameAsRequested",
                },
            )





       @property
       def snake_case__		(				self  :						List[Any]	)       ->	Optional[Any]:

            '''simple docstring'''




            _UpperCamelCase							       =			ort.SessionOptions()
            _UpperCamelCase							       =			False
            return options





       def snake_case__		(				self  :						str	)       ->	Any:

            '''simple docstring'''




            _UpperCamelCase							       =			load_image(
                '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'''
                '''/img2img/sketch-mountains-input.jpg'''	)
            _UpperCamelCase							       =			init_image.resize((768, 512)	)
            # using the PNDM scheduler by default
            _UpperCamelCase							       =			OnnxStableDiffusionImgaImgPipeline.from_pretrained(
                '''CompVis/stable-diffusion-v1-4'''			,					revision='''onnx'''			,					safety_checker=lowerCAmelCase__			,					feature_extractor=lowerCAmelCase__			,					provider=self.gpu_provider			,					sess_options=self.gpu_options			,					)
            pipe.set_progress_bar_config(disable=lowerCAmelCase__	)

            _UpperCamelCase							       =			'''A fantasy landscape, trending on artstation'''

            _UpperCamelCase							       =			np.random.RandomState(0	)
            _UpperCamelCase							       =			pipe(
                prompt=lowerCAmelCase__			,					image=lowerCAmelCase__			,					strength=0.75			,					guidance_scale=7.5			,					num_inference_steps=10			,					generator=lowerCAmelCase__			,					output_type='''np'''			,					)
            _UpperCamelCase							       =			output.images
            _UpperCamelCase							       =			images[0, 255:258, 383:386, -1]

            assert images.shape == (1, 512, 768, 3)
            _UpperCamelCase							       =			np.array([0.4909, 0.5059, 0.5372, 0.4623, 0.4876, 0.5049, 0.4820, 0.4956, 0.5019]	)
            # TODO: lower the tolerance after finding the cause of onnxruntime reproducibility issues

            assert np.abs(image_slice.flatten() - expected_slice	).max() < 2e-2







       def snake_case__		(				self  :						Any	)       ->	Tuple:

            '''simple docstring'''




            _UpperCamelCase							       =			load_image(
                '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'''
                '''/img2img/sketch-mountains-input.jpg'''	)
            _UpperCamelCase							       =			init_image.resize((768, 512)	)
            _UpperCamelCase							       =			LMSDiscreteScheduler.from_pretrained(
                '''runwayml/stable-diffusion-v1-5'''			,					subfolder='''scheduler'''			,					revision='''onnx'''	)
            _UpperCamelCase							       =			OnnxStableDiffusionImgaImgPipeline.from_pretrained(
                '''runwayml/stable-diffusion-v1-5'''			,					revision='''onnx'''			,					scheduler=lowerCAmelCase__			,					safety_checker=lowerCAmelCase__			,					feature_extractor=lowerCAmelCase__			,					provider=self.gpu_provider			,					sess_options=self.gpu_options			,					)
            pipe.set_progress_bar_config(disable=lowerCAmelCase__	)

            _UpperCamelCase							       =			'''A fantasy landscape, trending on artstation'''

            _UpperCamelCase							       =			np.random.RandomState(0	)
            _UpperCamelCase							       =			pipe(
                prompt=lowerCAmelCase__			,					image=lowerCAmelCase__			,					strength=0.75			,					guidance_scale=7.5			,					num_inference_steps=20			,					generator=lowerCAmelCase__			,					output_type='''np'''			,					)
            _UpperCamelCase							       =			output.images
            _UpperCamelCase							       =			images[0, 255:258, 383:386, -1]

            assert images.shape == (1, 512, 768, 3)
            _UpperCamelCase							       =			np.array([0.8043, 0.926, 0.9581, 0.8119, 0.8954, 0.913, 0.7209, 0.7463, 0.7431]	)
            # TODO: lower the tolerance after finding the cause of onnxruntime reproducibility issues

            assert np.abs(image_slice.flatten() - expected_slice	).max() < 2e-2










import argparse
import struct
import unittest


class        __magic_name__	:





     """simple docstring"""



     def __init__(				self							:str      ,   snake_case							:bytes	):





        '''simple docstring'''




        A_       :					Dict        =      data

        # Initialize hash values
        A_       :					Union[str, Any]        =      [
            0x6a_09_e6_67,
            0xbb_67_ae_85,
            0x3c_6e_f3_72,
            0xa5_4f_f5_3a,
            0x51_0e_52_7f,
            0x9b_05_68_8c,
            0x1f_83_d9_ab,
            0x5b_e0_cd_19,
        ]

        # Initialize round constants
        A_       :					List[Any]        =      [
            0x42_8a_2f_98,
            0x71_37_44_91,
            0xb5_c0_fb_cf,
            0xe9_b5_db_a5,
            0x39_56_c2_5b,
            0x59_f1_11_f1,
            0x92_3f_82_a4,
            0xab_1c_5e_d5,
            0xd8_07_aa_98,
            0x12_83_5b_01,
            0x24_31_85_be,
            0x55_0c_7d_c3,
            0x72_be_5d_74,
            0x80_de_b1_fe,
            0x9b_dc_06_a7,
            0xc1_9b_f1_74,
            0xe4_9b_69_c1,
            0xef_be_47_86,
            0x0f_c1_9d_c6,
            0x24_0c_a1_cc,
            0x2d_e9_2c_6f,
            0x4a_74_84_aa,
            0x5c_b0_a9_dc,
            0x76_f9_88_da,
            0x98_3e_51_52,
            0xa8_31_c6_6d,
            0xb0_03_27_c8,
            0xbf_59_7f_c7,
            0xc6_e0_0b_f3,
            0xd5_a7_91_47,
            0x06_ca_63_51,
            0x14_29_29_67,
            0x27_b7_0a_85,
            0x2e_1b_21_38,
            0x4d_2c_6d_fc,
            0x53_38_0d_13,
            0x65_0a_73_54,
            0x76_6a_0a_bb,
            0x81_c2_c9_2e,
            0x92_72_2c_85,
            0xa2_bf_e8_a1,
            0xa8_1a_66_4b,
            0xc2_4b_8b_70,
            0xc7_6c_51_a3,
            0xd1_92_e8_19,
            0xd6_99_06_24,
            0xf4_0e_35_85,
            0x10_6a_a0_70,
            0x19_a4_c1_16,
            0x1e_37_6c_08,
            0x27_48_77_4c,
            0x34_b0_bc_b5,
            0x39_1c_0c_b3,
            0x4e_d8_aa_4a,
            0x5b_9c_ca_4f,
            0x68_2e_6f_f3,
            0x74_8f_82_ee,
            0x78_a5_63_6f,
            0x84_c8_78_14,
            0x8c_c7_02_08,
            0x90_be_ff_fa,
            0xa4_50_6c_eb,
            0xbe_f9_a3_f7,
            0xc6_71_78_f2,
        ]

        A_       :					Any        =      self.preprocessing(self.data	)
        self.final_hash()



     @staticmethod
     def     SCREAMING_SNAKE_CASE		(				snake_case							:bytes	):





        '''simple docstring'''




        A_       :					List[Any]        =      B"\x80" + (B"\x00" * (63 - (len(snake_case	) + 8) % 64))
        A_       :					str        =      struct.pack(">Q"      ,   (len(snake_case	) * 8)	)
        return data + padding + big_endian_integer



     def     SCREAMING_SNAKE_CASE		(				self							:Any	):





        '''simple docstring'''




        A_       :					Tuple        =      [
            self.preprocessed_data[x : x + 64]
            for x in range(0      ,   len(self.preprocessed_data	)      ,   64	)
        ]

        for block in self.blocks:
           # Convert the given block into a list of 4 byte integers
           A_       :					Dict        =      list(struct.unpack(">16L"      ,   snake_case	)	)
           # add 48 0-ed integers
           words += [0] * 48

           A_   ,    A_   ,    A_   ,    A_   ,    A_   ,    A_   ,    A_   ,    A_       :					int        =      self.hashes

           for index in range(0      ,   64	):
              if index > 15:
                 # modify the zero-ed indexes at the end of the array
                 A_       :					int        =      (
                     self.ror(words[index - 15]      ,   7	)
                     ^ self.ror(words[index - 15]      ,   18	)
                     ^ (words[index - 15] >> 3)
                 )
                 A_       :					Any        =      (
                     self.ror(words[index - 2]      ,   17	)
                     ^ self.ror(words[index - 2]      ,   19	)
                     ^ (words[index - 2] >> 10)
                 )

                 A_       :					Optional[int]        =      (
                     words[index - 16] + sa + words[index - 7] + sa
                 ) % 0x1_00_00_00_00

              # Compression
              A_       :					Dict        =      self.ror(snake_case      ,   6	) ^ self.ror(snake_case      ,   11	) ^ self.ror(snake_case      ,   25	)
              A_       :					int        =      (e & f) ^ ((~e & 0xff_ff_ff_ff) & g)
              A_       :					Optional[Any]        =      (
                  h + sa + ch + self.round_constants[index] + words[index]
              ) % 0x1_00_00_00_00
              A_       :					List[str]        =      self.ror(snake_case      ,   2	) ^ self.ror(snake_case      ,   13	) ^ self.ror(snake_case      ,   22	)
              A_       :					List[Any]        =      (a & b) ^ (a & c) ^ (b & c)
              A_       :					Dict        =      (sa + maj) % 0x1_00_00_00_00

              A_   ,    A_   ,    A_   ,    A_   ,    A_   ,    A_   ,    A_   ,    A_       :					Optional[int]        =      (
                  g,
                  f,
                  e,
                  ((d + tempa) % 0x1_00_00_00_00),
                  c,
                  b,
                  a,
                  ((tempa + tempa) % 0x1_00_00_00_00),
              )

           A_       :					List[str]        =      [a, b, c, d, e, f, g, h]

           # Modify final values
           A_       :					List[str]        =      [
               ((element + mutated_hash_values[index]) % 0x1_00_00_00_00)
               for index, element in enumerate(self.hashes	)
           ]

        A_       :					Optional[int]        =      "".join([hex(snake_case	)[2:].zfill(8	) for value in self.hashes]	)





     def     SCREAMING_SNAKE_CASE		(				self							:Optional[Any]      ,   snake_case							:int      ,   snake_case							:int	):





        '''simple docstring'''




        return 0xff_ff_ff_ff & (value << (32 - rotations)) | (value >> rotations)




class        __magic_name__	(				unittest.TestCase				):





     """simple docstring"""



     def     SCREAMING_SNAKE_CASE		(				self							:List[Any]	):





        '''simple docstring'''




        import hashlib

        A_       :					int        =      bytes("Test String"      ,   "utf-8"	)
        self.assertEqual(SHAaaa(snake_case	).hash      ,   hashlib.shaaaa(snake_case	).hexdigest()	)




def   __snake_case    (       )			->    None:
   import doctest

   doctest.testmod()

   A_       :					Optional[Any]        =      argparse.ArgumentParser()
   parser.add_argument(
       "-s"					,					"--string"					,					dest="input_string"					,					default="Hello World!! Welcome to Cryptography"					,					help="Hash the string"					,					)
   parser.add_argument(
       "-f"					,					"--file"					,					dest="input_file"					,					help="Hash contents of a file"						)

   A_       :					List[Any]        =      parser.parse_args()

   A_       :					List[Any]        =      args.input_string

   # hash input should be a bytestring
   if args.input_file:
      with open(args.input_file					,					"rb"						) as f:
         A_       :					Optional[Any]        =      f.read()
   else:
      A_       :					Any        =      bytes(_lowerCAmelCase					,					"utf-8"						)

   print(SHAaaa(_lowerCAmelCase						).hash						)


if __name__ == "__main__":
 main()
