


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        a					(			unittest.TestCase   ):





     def  SCREAMING_SNAKE_CASE__       (    self					:				Any							):
        # clean up the VRAM after each test
        super().tearDown()
        gc.collect()





     def  SCREAMING_SNAKE_CASE__       (    self					:				Union[str, Any]							):
        __lowerCamelCase   ,     __lowerCamelCase:				int     =	FlaxControlNetModel.from_pretrained(
            """lllyasviel/sd-controlnet-canny"""    ,	from_pt=SCREAMING_SNAKE_CASE_    ,	dtype=jnp.bfloataa							)
        __lowerCamelCase   ,     __lowerCamelCase:				Dict     =	FlaxStableDiffusionControlNetPipeline.from_pretrained(
            """runwayml/stable-diffusion-v1-5"""    ,	controlnet=SCREAMING_SNAKE_CASE_    ,	from_pt=SCREAMING_SNAKE_CASE_    ,	dtype=jnp.bfloataa							)
        __lowerCamelCase:				Union[str, Any]     =	controlnet_params

        __lowerCamelCase:				Optional[int]     =	"""bird"""
        __lowerCamelCase:				str     =	jax.device_count()
        __lowerCamelCase:				Union[str, Any]     =	pipe.prepare_text_inputs([prompts] * num_samples							)

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

        __lowerCamelCase:				str     =	jax.random.PRNGKey(0							)
        __lowerCamelCase:				List[str]     =	jax.random.split(SCREAMING_SNAKE_CASE_    ,	jax.device_count()							)

        __lowerCamelCase:				str     =	replicate(SCREAMING_SNAKE_CASE_							)
        __lowerCamelCase:				List[str]     =	shard(SCREAMING_SNAKE_CASE_							)
        __lowerCamelCase:				Union[str, Any]     =	shard(SCREAMING_SNAKE_CASE_							)

        __lowerCamelCase:				str     =	pipe(
            prompt_ids=SCREAMING_SNAKE_CASE_    ,	image=SCREAMING_SNAKE_CASE_    ,	params=SCREAMING_SNAKE_CASE_    ,	prng_seed=SCREAMING_SNAKE_CASE_    ,	num_inference_steps=50    ,	jit=SCREAMING_SNAKE_CASE_    ,	).images
        assert images.shape == (jax.device_count(), 1, 768, 512, 3)

        __lowerCamelCase:				Dict     =	images.reshape((images.shape[0] * images.shape[1],) + images.shape[-3:]							)
        __lowerCamelCase:				List[Any]     =	images[0, 253:256, 253:256, -1]

        __lowerCamelCase:				Tuple     =	jnp.asarray(jax.device_get(image_slice.flatten()							)							)
        __lowerCamelCase:				int     =	jnp.array(
            [0.16_7969, 0.11_6699, 0.08_1543, 0.15_4297, 0.13_2812, 0.10_8887, 0.16_9922, 0.16_9922, 0.20_5078]							)
        print(F'output_slice: {output_slice}'							)
        assert jnp.abs(output_slice - expected_slice							).max() < 1E-2







     def  SCREAMING_SNAKE_CASE__       (    self					:				Tuple							):
        __lowerCamelCase   ,     __lowerCamelCase:				Any     =	FlaxControlNetModel.from_pretrained(
            """lllyasviel/sd-controlnet-openpose"""    ,	from_pt=SCREAMING_SNAKE_CASE_    ,	dtype=jnp.bfloataa							)
        __lowerCamelCase   ,     __lowerCamelCase:				Tuple     =	FlaxStableDiffusionControlNetPipeline.from_pretrained(
            """runwayml/stable-diffusion-v1-5"""    ,	controlnet=SCREAMING_SNAKE_CASE_    ,	from_pt=SCREAMING_SNAKE_CASE_    ,	dtype=jnp.bfloataa							)
        __lowerCamelCase:				Optional[int]     =	controlnet_params

        __lowerCamelCase:				List[str]     =	"""Chef in the kitchen"""
        __lowerCamelCase:				Optional[int]     =	jax.device_count()
        __lowerCamelCase:				Optional[int]     =	pipe.prepare_text_inputs([prompts] * num_samples							)

        __lowerCamelCase:				Optional[int]     =	load_image(
            """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd_controlnet/pose.png"""							)
        __lowerCamelCase:				Union[str, Any]     =	pipe.prepare_image_inputs([pose_image] * num_samples							)

        __lowerCamelCase:				Optional[Any]     =	jax.random.PRNGKey(0							)
        __lowerCamelCase:				Dict     =	jax.random.split(SCREAMING_SNAKE_CASE_    ,	jax.device_count()							)

        __lowerCamelCase:				Union[str, Any]     =	replicate(SCREAMING_SNAKE_CASE_							)
        __lowerCamelCase:				Dict     =	shard(SCREAMING_SNAKE_CASE_							)
        __lowerCamelCase:				Union[str, Any]     =	shard(SCREAMING_SNAKE_CASE_							)

        __lowerCamelCase:				Tuple     =	pipe(
            prompt_ids=SCREAMING_SNAKE_CASE_    ,	image=SCREAMING_SNAKE_CASE_    ,	params=SCREAMING_SNAKE_CASE_    ,	prng_seed=SCREAMING_SNAKE_CASE_    ,	num_inference_steps=50    ,	jit=SCREAMING_SNAKE_CASE_    ,	).images
        assert images.shape == (jax.device_count(), 1, 768, 512, 3)

        __lowerCamelCase:				Union[str, Any]     =	images.reshape((images.shape[0] * images.shape[1],) + images.shape[-3:]							)
        __lowerCamelCase:				Optional[Any]     =	images[0, 253:256, 253:256, -1]

        __lowerCamelCase:				Any     =	jnp.asarray(jax.device_get(image_slice.flatten()							)							)
        __lowerCamelCase:				Optional[int]     =	jnp.array(
            [[0.27_1484, 0.26_1719, 0.27_5391, 0.27_7344, 0.27_9297, 0.29_1016, 0.29_4922, 0.30_2734, 0.30_2734]]							)
        print(F'output_slice: {output_slice}'							)
        assert jnp.abs(output_slice - expected_slice							).max() < 1E-2






from manim import *



class        a					(			_UpperCAmelCase   ):





     def  SCREAMING_SNAKE_CASE__       (    self					:				int							):
        __lowerCamelCase:				int     =	Rectangle(height=0.5    ,	width=0.5							)
        __lowerCamelCase:				List[str]     =	Rectangle(height=0.25    ,	width=0.25							)
        __lowerCamelCase:				Optional[int]     =	Rectangle(height=0.46    ,	width=0.46							).set_stroke(width=0							)

        __lowerCamelCase:				str     =	[mem.copy() for i in range(6							)]
        __lowerCamelCase:				Dict     =	[mem.copy() for i in range(6							)]
        __lowerCamelCase:				Union[str, Any]     =	VGroup(*SCREAMING_SNAKE_CASE_							).arrange(SCREAMING_SNAKE_CASE_    ,	buff=0							)
        __lowerCamelCase:				Tuple     =	VGroup(*SCREAMING_SNAKE_CASE_							).arrange(SCREAMING_SNAKE_CASE_    ,	buff=0							)
        __lowerCamelCase:				Optional[int]     =	VGroup(SCREAMING_SNAKE_CASE_    ,	SCREAMING_SNAKE_CASE_							).arrange(SCREAMING_SNAKE_CASE_    ,	buff=0							)
        __lowerCamelCase:				Dict     =	Text("""CPU"""    ,	font_size=24							)
        __lowerCamelCase:				Any     =	Group(SCREAMING_SNAKE_CASE_    ,	SCREAMING_SNAKE_CASE_							).arrange(SCREAMING_SNAKE_CASE_    ,	buff=0.5    ,	aligned_edge=SCREAMING_SNAKE_CASE_							)
        cpu.move_to([-2.5, -0.5, 0]							)
        self.add(SCREAMING_SNAKE_CASE_							)

        __lowerCamelCase:				Optional[int]     =	[mem.copy() for i in range(4							)]
        __lowerCamelCase:				Optional[int]     =	VGroup(*SCREAMING_SNAKE_CASE_							).arrange(SCREAMING_SNAKE_CASE_    ,	buff=0							)
        __lowerCamelCase:				Dict     =	Text("""GPU"""    ,	font_size=24							)
        __lowerCamelCase:				Dict     =	Group(SCREAMING_SNAKE_CASE_    ,	SCREAMING_SNAKE_CASE_							).arrange(SCREAMING_SNAKE_CASE_    ,	buff=0.5    ,	aligned_edge=SCREAMING_SNAKE_CASE_							)
        gpu.move_to([-1, -1, 0]							)
        self.add(SCREAMING_SNAKE_CASE_							)

        __lowerCamelCase:				Any     =	[mem.copy() for i in range(6							)]
        __lowerCamelCase:				Dict     =	VGroup(*SCREAMING_SNAKE_CASE_							).arrange(SCREAMING_SNAKE_CASE_    ,	buff=0							)

        __lowerCamelCase:				List[Any]     =	Text("""Model"""    ,	font_size=24							)
        __lowerCamelCase:				Any     =	Group(SCREAMING_SNAKE_CASE_    ,	SCREAMING_SNAKE_CASE_							).arrange(SCREAMING_SNAKE_CASE_    ,	buff=0.5    ,	aligned_edge=SCREAMING_SNAKE_CASE_							)
        model.move_to([3, -1.0, 0]							)
        self.add(SCREAMING_SNAKE_CASE_							)

        __lowerCamelCase:				Tuple     =	[]
        __lowerCamelCase:				Any     =	[]
        __lowerCamelCase:				int     =	[]

        for i, rect in enumerate(SCREAMING_SNAKE_CASE_							):
           rect.set_stroke(SCREAMING_SNAKE_CASE_							)

           __lowerCamelCase:				int     =	Rectangle(height=0.46 / 4    ,	width=0.46 / 3							).set_stroke(width=0.0							).set_fill(SCREAMING_SNAKE_CASE_    ,	opacity=0.7							)

           if i == 0:
              cpu_target.next_to(cpu_left_col_base[0].get_corner(DOWN + LEFT							)    ,	buff=0.02    ,	direction=SCREAMING_SNAKE_CASE_							)
              cpu_target.set_x(cpu_target.get_x() + 0.1							)
           elif i == 3:
              cpu_target.next_to(model_cpu_arr[0]    ,	direction=SCREAMING_SNAKE_CASE_    ,	buff=0.0							)
           else:
              cpu_target.next_to(model_cpu_arr[i - 1]    ,	direction=SCREAMING_SNAKE_CASE_    ,	buff=0.0							)
           self.add(SCREAMING_SNAKE_CASE_							)
           model_cpu_arr.append(SCREAMING_SNAKE_CASE_							)

        self.add(*SCREAMING_SNAKE_CASE_    ,	*SCREAMING_SNAKE_CASE_    ,	*SCREAMING_SNAKE_CASE_							)

        __lowerCamelCase:				List[Any]     =	[mem.copy() for i in range(6							)]
        __lowerCamelCase:				Any     =	VGroup(*SCREAMING_SNAKE_CASE_							).arrange(SCREAMING_SNAKE_CASE_    ,	buff=0							)

        __lowerCamelCase:				Tuple     =	Text("""Loaded Checkpoint"""    ,	font_size=24							)
        __lowerCamelCase:				Tuple     =	Group(SCREAMING_SNAKE_CASE_    ,	SCREAMING_SNAKE_CASE_							).arrange(SCREAMING_SNAKE_CASE_    ,	buff=0.5    ,	aligned_edge=SCREAMING_SNAKE_CASE_							)
        checkpoint.move_to([3, 0.5, 0]							)

        self.add(SCREAMING_SNAKE_CASE_							)

        __lowerCamelCase:				List[Any]     =	[]
        __lowerCamelCase:				Optional[int]     =	[]

        for i, rect in enumerate(SCREAMING_SNAKE_CASE_							):
           __lowerCamelCase:				Optional[int]     =	fill.copy().set_fill(SCREAMING_SNAKE_CASE_    ,	opacity=0.7							)
           target.move_to(SCREAMING_SNAKE_CASE_							)
           ckpt_arr.append(SCREAMING_SNAKE_CASE_							)

           __lowerCamelCase:				List[Any]     =	target.copy()
           if i < 5:
              cpu_target.move_to(cpu_left_col_base[i + 1]							)
           else:
              cpu_target.move_to(cpu_right_col_base[i - 5]							)
           ckpt_cpu_arr.append(SCREAMING_SNAKE_CASE_							)
        self.add(*SCREAMING_SNAKE_CASE_    ,	*SCREAMING_SNAKE_CASE_							)

        __lowerCamelCase:				Optional[Any]     =	Square(side_length=2.2							)
        key.move_to([-5, 2, 0]							)

        __lowerCamelCase:				Dict     =	MarkupText(
            F'<b>Key:</b>\n\n<span fgcolor=\'{YELLOW}\'>●</span> Empty Model'    ,	font_size=18    ,	)

        key_text.move_to([-5, 2.4, 0]							)

        self.add(SCREAMING_SNAKE_CASE_    ,	SCREAMING_SNAKE_CASE_							)

        __lowerCamelCase:				Optional[int]     =	MarkupText(
            F'<span fgcolor=\'{BLUE}\'>●</span> Checkpoint'    ,	font_size=18    ,	)

        blue_text.next_to(SCREAMING_SNAKE_CASE_    ,	DOWN * 2.4    ,	aligned_edge=key_text.get_left()							)
        self.add(SCREAMING_SNAKE_CASE_							)

        __lowerCamelCase:				Tuple     =	MarkupText(
            F'Based on the passed in configuration, weights are stored in\na variety of np.memmaps on disk or to a particular device.'    ,	font_size=24    ,	)
        step_a.move_to([2, 2, 0]							)

        __lowerCamelCase:				List[Any]     =	[meta_mem.copy() for i in range(6							)]
        __lowerCamelCase:				Optional[int]     =	[meta_mem.copy() for i in range(6							)]
        __lowerCamelCase:				str     =	VGroup(*SCREAMING_SNAKE_CASE_							).arrange(SCREAMING_SNAKE_CASE_    ,	buff=0							)
        __lowerCamelCase:				Optional[int]     =	VGroup(*SCREAMING_SNAKE_CASE_							).arrange(SCREAMING_SNAKE_CASE_    ,	buff=0							)
        __lowerCamelCase:				List[str]     =	VGroup(SCREAMING_SNAKE_CASE_    ,	SCREAMING_SNAKE_CASE_							).arrange(SCREAMING_SNAKE_CASE_    ,	buff=0							)
        __lowerCamelCase:				Dict     =	Text("""Disk"""    ,	font_size=24							)
        __lowerCamelCase:				Any     =	Group(SCREAMING_SNAKE_CASE_    ,	SCREAMING_SNAKE_CASE_							).arrange(SCREAMING_SNAKE_CASE_    ,	buff=0.5    ,	aligned_edge=SCREAMING_SNAKE_CASE_							)
        disk.move_to([-4.0, -1.25, 0]							)
        self.play(Write(SCREAMING_SNAKE_CASE_    ,	run_time=3							)    ,	Write(SCREAMING_SNAKE_CASE_    ,	run_time=1							)    ,	Create(SCREAMING_SNAKE_CASE_    ,	run_time=1							)							)

        __lowerCamelCase:				int     =	[]
        for i, rect in enumerate(SCREAMING_SNAKE_CASE_							):
           __lowerCamelCase:				Optional[int]     =	rect.copy()
           target.generate_target()
           target.target.move_to(disk_left_col_base[i]							).scale(0.5							)
           animations.append(MoveToTarget(SCREAMING_SNAKE_CASE_    ,	run_time=1.5							)							)
        self.play(*SCREAMING_SNAKE_CASE_							)

        self.play(FadeOut(SCREAMING_SNAKE_CASE_							)							)

        __lowerCamelCase:				List[Any]     =	MarkupText(F'Then, the checkpoint is removed from memory\nthrough garbage collection.'    ,	font_size=24							)
        step_a.move_to([2, 2, 0]							)

        self.play(Write(SCREAMING_SNAKE_CASE_    ,	run_time=3							)							)

        self.play(
            FadeOut(SCREAMING_SNAKE_CASE_    ,	SCREAMING_SNAKE_CASE_    ,	*SCREAMING_SNAKE_CASE_    ,	*SCREAMING_SNAKE_CASE_							)    ,	)

        self.wait()


