





snake_case_           =      'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'






def lowerCamelCase__   (							snake_case_     :  bytes	)				->    bytes:
   # Make sure the supplied data is a bytes-like object
   if not isinstance(snake_case_	,				snake_case_	):
      __snake_case								=       f"""a bytes-like object is required, not '{data.__class__.__name__}'"""
      raise TypeError(snake_case_	)

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

   __snake_case								=       len(snake_case_	) % 6 != 0

   if padding_needed:
      # The padding that will be added later
      __snake_case								=       B'''=''' * ((6 - len(snake_case_	) % 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(snake_case_	) % 6)
   else:
      __snake_case								=       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(snake_case_	)	,				6	)	).encode()
       + padding
   )






def lowerCamelCase__   (							snake_case_     :  str	)				->    bytes:
   # Make sure encoded_data is either a string or a bytes-like object
   if not isinstance(snake_case_	,				snake_case_	) and not isinstance(snake_case_	,				snake_case_	):
      __snake_case								=       (
          '''argument should be a bytes-like object or ASCII string, '''
          f"""not '{encoded_data.__class__.__name__}'"""
      )
      raise TypeError(snake_case_	)

   # 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(snake_case_	,				snake_case_	):
      try:
         __snake_case								=       encoded_data.decode('''utf-8'''	)
      except UnicodeDecodeError:
         raise ValueError('''base64 encoded data should only contain ASCII characters'''	)

   __snake_case								=       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(snake_case_	) % 4 == 0 and padding < 3, "Incorrect padding"

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

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

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

   return bytes(snake_case_	)


if __name__ == "__main__":
    import doctest

    doctest.testmod()








import gc
import unittest

from diffusers import FlaxControlNetModel, FlaxStableDiffusionControlNetPipeline
from diffusers.utils import is_flax_available, load_image, slow
from diffusers.utils.testing_utils import require_flax


if is_flax_available():
    import jax
    import jax.numpy as jnp
    from flax.jax_utils import replicate
    from flax.training.common_utils import shard

@slow
@require_flax
class 		SCREAMING_SNAKE_CASE__    (	unittest.TestCase  ):


      def        a			(self	:				int				):





         """simple docstring"""



         super().tearDown()
         gc.collect()


      def        a			(self	:				Dict				):





         """simple docstring"""



         __snake_case	,     __snake_case								=       FlaxControlNetModel.from_pretrained(
             '''lllyasviel/sd-controlnet-canny'''    ,      from_pt=a__    ,      dtype=jnp.bfloataa				)
         __snake_case	,     __snake_case								=       FlaxStableDiffusionControlNetPipeline.from_pretrained(
             '''runwayml/stable-diffusion-v1-5'''    ,      controlnet=a__    ,      from_pt=a__    ,      dtype=jnp.bfloataa				)
         __snake_case								=       controlnet_params

         __snake_case								=       '''bird'''
         __snake_case								=       jax.device_count()
         __snake_case								=       pipe.prepare_text_inputs([prompts] * num_samples				)

         __snake_case								=       load_image(
             '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd_controlnet/bird_canny.png'''				)
         __snake_case								=       pipe.prepare_image_inputs([canny_image] * num_samples				)

         __snake_case								=       jax.random.PRNGKey(0				)
         __snake_case								=       jax.random.split(a__    ,      jax.device_count()				)

         __snake_case								=       replicate(a__				)
         __snake_case								=       shard(a__				)
         __snake_case								=       shard(a__				)

         __snake_case								=       pipe(
             prompt_ids=a__    ,      image=a__    ,      params=a__    ,      prng_seed=a__    ,      num_inference_steps=50    ,      jit=a__    ,      ).images
         assert images.shape == (jax.device_count(), 1, 768, 512, 3)

         __snake_case								=       images.reshape((images.shape[0] * images.shape[1],) + images.shape[-3:]				)
         __snake_case								=       images[0, 253:256, 253:256, -1]

         __snake_case								=       jnp.asarray(jax.device_get(image_slice.flatten()				)				)
         __snake_case								=       jnp.array(
             [0.1_6_7_9_6_9, 0.1_1_6_6_9_9, 0.0_8_1_5_4_3, 0.1_5_4_2_9_7, 0.1_3_2_8_1_2, 0.1_0_8_8_8_7, 0.1_6_9_9_2_2, 0.1_6_9_9_2_2, 0.2_0_5_0_7_8]				)
         print(f"""output_slice: {output_slice}"""				)
         assert jnp.abs(output_slice - expected_slice				).max() < 1E-2


      def        a			(self	:				Dict				):





         """simple docstring"""



         __snake_case	,     __snake_case								=       FlaxControlNetModel.from_pretrained(
             '''lllyasviel/sd-controlnet-openpose'''    ,      from_pt=a__    ,      dtype=jnp.bfloataa				)
         __snake_case	,     __snake_case								=       FlaxStableDiffusionControlNetPipeline.from_pretrained(
             '''runwayml/stable-diffusion-v1-5'''    ,      controlnet=a__    ,      from_pt=a__    ,      dtype=jnp.bfloataa				)
         __snake_case								=       controlnet_params

         __snake_case								=       '''Chef in the kitchen'''
         __snake_case								=       jax.device_count()
         __snake_case								=       pipe.prepare_text_inputs([prompts] * num_samples				)

         __snake_case								=       load_image(
             '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd_controlnet/pose.png'''				)
         __snake_case								=       pipe.prepare_image_inputs([pose_image] * num_samples				)

         __snake_case								=       jax.random.PRNGKey(0				)
         __snake_case								=       jax.random.split(a__    ,      jax.device_count()				)

         __snake_case								=       replicate(a__				)
         __snake_case								=       shard(a__				)
         __snake_case								=       shard(a__				)

         __snake_case								=       pipe(
             prompt_ids=a__    ,      image=a__    ,      params=a__    ,      prng_seed=a__    ,      num_inference_steps=50    ,      jit=a__    ,      ).images
         assert images.shape == (jax.device_count(), 1, 768, 512, 3)

         __snake_case								=       images.reshape((images.shape[0] * images.shape[1],) + images.shape[-3:]				)
         __snake_case								=       images[0, 253:256, 253:256, -1]

         __snake_case								=       jnp.asarray(jax.device_get(image_slice.flatten()				)				)
         __snake_case								=       jnp.array(
             [[0.2_7_1_4_8_4, 0.2_6_1_7_1_9, 0.2_7_5_3_9_1, 0.2_7_7_3_4_4, 0.2_7_9_2_9_7, 0.2_9_1_0_1_6, 0.2_9_4_9_2_2, 0.3_0_2_7_3_4, 0.3_0_2_7_3_4]]				)
         print(f"""output_slice: {output_slice}"""				)
         assert jnp.abs(output_slice - expected_slice				).max() < 1E-2

