
lowercase__		:List[str]   =    "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"



def        UpperCamelCase    (							lowerCAmelCase__		):
					'''simple docstring'''
					# Make sure the supplied data is a bytes-like object
					if not isinstance(_A ,			_A		):
										lowercase							=						f'a bytes-like object is required, not \'{data.__class__.__name__}\''
										raise TypeError(_A		)

					lowercase							=						''''''.join(bin(_A		)[2:].zfill(8		) for byte in data		)

					lowercase							=						len(_A		) % 6 != 0

					if padding_needed:
										# The padding that will be added later
										lowercase							=						B'''=''' * ((6 - len(_A		) % 6) // 2)

										# Append binary_stream with arbitrary binary digits (0's by default) to make its
										# length a multiple of 6.
										binary_stream += "0" * (6 - len(_A		) % 6)
					else:
										lowercase							=						B''''''

					# Encode every 6 binary digits to their corresponding Base64 character
					return (
					    "".join(
					        B64_CHARSET[int(binary_stream[index : index + 6] ,			2		)]
					        for index in range(0 ,			len(_A		) ,			6		)		).encode()
					    + padding
					)



def        UpperCamelCase    (							lowerCAmelCase__		):
					'''simple docstring'''
					# Make sure encoded_data is either a string or a bytes-like object
					if not isinstance(_A ,			_A		) and not isinstance(_A ,			_A		):
										lowercase							=						(
										    '''argument should be a bytes-like object or ASCII string, '''
										    f'not \'{encoded_data.__class__.__name__}\''
										)
										raise TypeError(_A		)

					# In case encoded_data is a bytes-like object, make sure it contains only
					# ASCII characters so we convert it to a string object
					if isinstance(_A ,			_A		):
										try:
															lowercase							=						encoded_data.decode('''utf-8'''		)
										except UnicodeDecodeError:
															raise ValueError('''base64 encoded data should only contain ASCII characters'''		)

					lowercase							=						encoded_data.count('''='''		)

					# Check if the encoded string contains non base64 characters
					if padding:
										assert all(
										    char in B64_CHARSET for char in encoded_data[:-padding]		), "Invalid base64 character(s) found."
					else:
										assert all(
										    char in B64_CHARSET for char in encoded_data		), "Invalid base64 character(s) found."

					# Check the padding
					assert len(_A		) % 4 == 0 and padding < 3, "Incorrect padding"

					if padding:
										# Remove padding if there is one
										lowercase							=						encoded_data[:-padding]

										lowercase							=						''''''.join(
										    bin(B64_CHARSET.index(_A		)		)[2:].zfill(6		) for char in encoded_data		)[: -padding * 2]
					else:
										lowercase							=						''''''.join(
										    bin(B64_CHARSET.index(_A		)		)[2:].zfill(6		) for char in encoded_data		)

					lowercase							=						[
					    int(binary_stream[index : index + 8] ,			2		)
					    for index in range(0 ,			len(_A		) ,			8		)
					]

					return bytes(_A		)


if __name__ == "__main__":
							import doctest

							doctest.testmod()










import unittest

import numpy as np
import torch

from diffusers import KarrasVePipeline, KarrasVeScheduler, UNetaDModel
from diffusers.utils.testing_utils import enable_full_determinism, require_torch, slow, torch_device


enable_full_determinism()


class  A		(  unittest.TestCase     ):



		@property
		def        SCREAMING_SNAKE_CASE__			(     self   ):







								"""simple docstring"""



								torch.manual_seed(0   )
								lowerCAmelCase_      			=  UNetaDModel(
								    block_out_channels=(32, 64),	layers_per_block=2,	sample_size=32,	in_channels=3,	out_channels=3,	down_block_types=('''DownBlock2D''', '''AttnDownBlock2D'''),	up_block_types=('''AttnUpBlock2D''', '''UpBlock2D'''),	)
								return model







		def        SCREAMING_SNAKE_CASE__			(     self   ):







								"""simple docstring"""



								lowerCAmelCase_      			=  self.dummy_uncond_unet
								lowerCAmelCase_      			=  KarrasVeScheduler()

								lowerCAmelCase_      			=  KarrasVePipeline(unet=UpperCamelCase__,	scheduler=UpperCamelCase__   )
								pipe.to(UpperCamelCase__   )
								pipe.set_progress_bar_config(disable=UpperCamelCase__   )

								lowerCAmelCase_      			=  torch.manual_seed(0   )
								lowerCAmelCase_      			=  pipe(num_inference_steps=2,	generator=UpperCamelCase__,	output_type='''numpy'''   ).images

								lowerCAmelCase_      			=  torch.manual_seed(0   )
								lowerCAmelCase_      			=  pipe(num_inference_steps=2,	generator=UpperCamelCase__,	output_type='''numpy''',	return_dict=UpperCamelCase__   )[0]

								lowerCAmelCase_      			=  image[0, -3:, -3:, -1]
								lowerCAmelCase_      			=  image_from_tuple[0, -3:, -3:, -1]

								assert image.shape == (1, 32, 32, 3)
								lowerCAmelCase_      			=  np.array([0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0]   )

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







@slow
@require_torch
class  A		(  unittest.TestCase     ):



		def        SCREAMING_SNAKE_CASE__			(     self   ):







								"""simple docstring"""



								lowerCAmelCase_      			=  '''google/ncsnpp-celebahq-256'''
								lowerCAmelCase_      			=  UNetaDModel.from_pretrained(UpperCamelCase__   )
								lowerCAmelCase_      			=  KarrasVeScheduler()

								lowerCAmelCase_      			=  KarrasVePipeline(unet=UpperCamelCase__,	scheduler=UpperCamelCase__   )
								pipe.to(UpperCamelCase__   )
								pipe.set_progress_bar_config(disable=UpperCamelCase__   )

								lowerCAmelCase_      			=  torch.manual_seed(0   )
								lowerCAmelCase_      			=  pipe(num_inference_steps=20,	generator=UpperCamelCase__,	output_type='''numpy'''   ).images

								lowerCAmelCase_      			=  image[0, -3:, -3:, -1]
								assert image.shape == (1, 256, 256, 3)
								lowerCAmelCase_      			=  np.array([0.578, 0.5_811, 0.5_924, 0.5_809, 0.587, 0.5_886, 0.5_861, 0.5_802, 0.586]   )

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




