



'''simple docstring'''




from manim import *






class 		_UpperCamelCase						(							lowerCamelCase__					):


   '''simple docstring'''

   def UpperCamelCase__      (							self  :				Tuple		):





     """simple docstring"""






     __SCREAMING_SNAKE_CASE :	Optional[Any]					 =	Rectangle(height=0.5							,  width=0.5		)
     __SCREAMING_SNAKE_CASE :	str					 =	Rectangle(height=0.46							,  width=0.46		).set_stroke(width=0		)

     __SCREAMING_SNAKE_CASE :	Union[str, Any]					 =	[mem.copy() for i in range(6		)]
     __SCREAMING_SNAKE_CASE :	Dict					 =	[mem.copy() for i in range(6		)]
     __SCREAMING_SNAKE_CASE :	Tuple					 =	VGroup(*lowerCAmelCase__		).arrange(lowerCAmelCase__							,  buff=0		)
     __SCREAMING_SNAKE_CASE :	str					 =	VGroup(*lowerCAmelCase__		).arrange(lowerCAmelCase__							,  buff=0		)
     __SCREAMING_SNAKE_CASE :	Dict					 =	VGroup(lowerCAmelCase__							,  lowerCAmelCase__		).arrange(lowerCAmelCase__							,  buff=0		)
     __SCREAMING_SNAKE_CASE :	List[str]					 =	Text("""CPU"""							,  font_size=2_4		)
     __SCREAMING_SNAKE_CASE :	Any					 =	Group(lowerCAmelCase__							,  lowerCAmelCase__		).arrange(lowerCAmelCase__							,  buff=0.5							,  aligned_edge=lowerCAmelCase__		)
     cpu.move_to([-2.5, -0.5, 0]		)
     self.add(lowerCAmelCase__		)

     __SCREAMING_SNAKE_CASE :	Optional[Any]					 =	[mem.copy() for i in range(4		)]
     __SCREAMING_SNAKE_CASE :	Union[str, Any]					 =	VGroup(*lowerCAmelCase__		).arrange(lowerCAmelCase__							,  buff=0		)
     __SCREAMING_SNAKE_CASE :	Any					 =	Text("""GPU"""							,  font_size=2_4		)
     __SCREAMING_SNAKE_CASE :	str					 =	Group(lowerCAmelCase__							,  lowerCAmelCase__		).arrange(lowerCAmelCase__							,  buff=0.5							,  aligned_edge=lowerCAmelCase__		)
     gpu.move_to([-1, -1, 0]		)
     self.add(lowerCAmelCase__		)

     __SCREAMING_SNAKE_CASE :	str					 =	[mem.copy() for i in range(6		)]
     __SCREAMING_SNAKE_CASE :	Any					 =	VGroup(*lowerCAmelCase__		).arrange(lowerCAmelCase__							,  buff=0		)

     __SCREAMING_SNAKE_CASE :	str					 =	Text("""Model"""							,  font_size=2_4		)
     __SCREAMING_SNAKE_CASE :	int					 =	Group(lowerCAmelCase__							,  lowerCAmelCase__		).arrange(lowerCAmelCase__							,  buff=0.5							,  aligned_edge=lowerCAmelCase__		)
     model.move_to([3, -1.0, 0]		)
     self.add(lowerCAmelCase__		)

     __SCREAMING_SNAKE_CASE :	Optional[Any]					 =	[]
     for i, rect in enumerate(lowerCAmelCase__		):
       rect.set_stroke(lowerCAmelCase__		)
       # target = fill.copy().set_fill(YELLOW, opacity=0.7)
       # target.move_to(rect)
       # self.add(target)

       __SCREAMING_SNAKE_CASE :	List[str]					 =	Rectangle(height=0.46 / 4							,  width=0.46 / 3		).set_stroke(width=0.0		).set_fill(lowerCAmelCase__							,  opacity=0.7		)

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

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

     __SCREAMING_SNAKE_CASE :	Any					 =	Text("""Loaded Checkpoint"""							,  font_size=2_4		)
     __SCREAMING_SNAKE_CASE :	Any					 =	Group(lowerCAmelCase__							,  lowerCAmelCase__		).arrange(lowerCAmelCase__							,  aligned_edge=lowerCAmelCase__							,  buff=0.4		)
     checkpoint.move_to([3, 0.5, 0]		)

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

     __SCREAMING_SNAKE_CASE :	int					 =	MarkupText(
         F"<b>Key:</b>\n\n<span fgcolor='{YELLOW}'>●</span> Empty Model"							,  font_size=1_8							,  )

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

     self.add(lowerCAmelCase__							,  lowerCAmelCase__		)

     __SCREAMING_SNAKE_CASE :	int					 =	MarkupText(
         F"<span fgcolor='{BLUE}'>●</span> Checkpoint"							,  font_size=1_8							,  )

     blue_text.next_to(lowerCAmelCase__							,  DOWN * 2.4							,  aligned_edge=key_text.get_left()		)

     __SCREAMING_SNAKE_CASE :	Dict					 =	MarkupText(
         F"Next, a <i><span fgcolor=\"{BLUE}\">second</span></i> model is loaded into memory,\nwith the weights of a <span fgcolor=\"{BLUE}\">single shard</span>."							,  font_size=2_4							,  )
     step_a.move_to([2, 2, 0]		)
     self.play(Write(lowerCAmelCase__		)							,  Write(lowerCAmelCase__		)		)

     self.play(Write(lowerCAmelCase__							,  run_time=1		)							,  Create(lowerCAmelCase__							,  run_time=1		)		)

     __SCREAMING_SNAKE_CASE :	Any					 =	[]
     __SCREAMING_SNAKE_CASE :	str					 =	[]
     for i, rect in enumerate(lowerCAmelCase__		):
       __SCREAMING_SNAKE_CASE :	List[Any]					 =	fill.copy().set_fill(lowerCAmelCase__							,  opacity=0.7		)
       target.move_to(lowerCAmelCase__		)
       first_animations.append(GrowFromCenter(lowerCAmelCase__							,  run_time=1		)		)

       __SCREAMING_SNAKE_CASE :	Union[str, Any]					 =	target.copy()
       cpu_target.generate_target()
       if i < 5:
         cpu_target.target.move_to(cpu_left_col_base[i + 1]		)
       else:
         cpu_target.target.move_to(cpu_right_col_base[i - 5]		)
       second_animations.append(MoveToTarget(lowerCAmelCase__							,  run_time=1.5		)		)

     self.play(*lowerCAmelCase__		)
     self.play(*lowerCAmelCase__		)
     self.wait()




'''simple docstring'''




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


			'''simple docstring'''

			def UpperCamelCase__      (							self  :				Optional[Any]		):





					"""simple docstring"""






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

			def UpperCamelCase__      (							self  :				List[Any]		):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE ,		__SCREAMING_SNAKE_CASE :	int					 =	FlaxControlNetModel.from_pretrained(
					    """lllyasviel/sd-controlnet-canny"""							,  from_pt=lowerCAmelCase__							,  dtype=jnp.bfloataa		)
					__SCREAMING_SNAKE_CASE ,		__SCREAMING_SNAKE_CASE :	Tuple					 =	FlaxStableDiffusionControlNetPipeline.from_pretrained(
					    """runwayml/stable-diffusion-v1-5"""							,  controlnet=lowerCAmelCase__							,  from_pt=lowerCAmelCase__							,  dtype=jnp.bfloataa		)
					__SCREAMING_SNAKE_CASE :	str					 =	controlnet_params

					__SCREAMING_SNAKE_CASE :	str					 =	"""bird"""
					__SCREAMING_SNAKE_CASE :	Optional[int]					 =	jax.device_count()
					__SCREAMING_SNAKE_CASE :	str					 =	pipe.prepare_text_inputs([prompts] * num_samples		)

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

					__SCREAMING_SNAKE_CASE :	Union[str, Any]					 =	jax.random.PRNGKey(0		)
					__SCREAMING_SNAKE_CASE :	Union[str, Any]					 =	jax.random.split(lowerCAmelCase__							,  jax.device_count()		)

					__SCREAMING_SNAKE_CASE :	Optional[int]					 =	replicate(lowerCAmelCase__		)
					__SCREAMING_SNAKE_CASE :	Union[str, Any]					 =	shard(lowerCAmelCase__		)
					__SCREAMING_SNAKE_CASE :	Optional[Any]					 =	shard(lowerCAmelCase__		)

					__SCREAMING_SNAKE_CASE :	List[Any]					 =	pipe(
					    prompt_ids=lowerCAmelCase__							,  image=lowerCAmelCase__							,  params=lowerCAmelCase__							,  prng_seed=lowerCAmelCase__							,  num_inference_steps=5_0							,  jit=lowerCAmelCase__							,  ).images
					assert images.shape == (jax.device_count(), 1, 7_6_8, 5_1_2, 3)

					__SCREAMING_SNAKE_CASE :	Union[str, Any]					 =	images.reshape((images.shape[0] * images.shape[1],) + images.shape[-3:]		)
					__SCREAMING_SNAKE_CASE :	Tuple					 =	images[0, 2_5_3:2_5_6, 2_5_3:2_5_6, -1]

					__SCREAMING_SNAKE_CASE :	Optional[Any]					 =	jnp.asarray(jax.device_get(image_slice.flatten()		)		)
					__SCREAMING_SNAKE_CASE :	Tuple					 =	jnp.array(
					    [0.16_79_69, 0.11_66_99, 0.08_15_43, 0.15_42_97, 0.13_28_12, 0.10_88_87, 0.16_99_22, 0.16_99_22, 0.20_50_78]		)
					print(F"output_slice: {output_slice}"		)
					assert jnp.abs(output_slice - expected_slice		).max() < 1E-2



			def UpperCamelCase__      (							self  :				str		):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE ,		__SCREAMING_SNAKE_CASE :	Union[str, Any]					 =	FlaxControlNetModel.from_pretrained(
					    """lllyasviel/sd-controlnet-openpose"""							,  from_pt=lowerCAmelCase__							,  dtype=jnp.bfloataa		)
					__SCREAMING_SNAKE_CASE ,		__SCREAMING_SNAKE_CASE :	Any					 =	FlaxStableDiffusionControlNetPipeline.from_pretrained(
					    """runwayml/stable-diffusion-v1-5"""							,  controlnet=lowerCAmelCase__							,  from_pt=lowerCAmelCase__							,  dtype=jnp.bfloataa		)
					__SCREAMING_SNAKE_CASE :	Union[str, Any]					 =	controlnet_params

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

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

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

					__SCREAMING_SNAKE_CASE :	List[Any]					 =	replicate(lowerCAmelCase__		)
					__SCREAMING_SNAKE_CASE :	Optional[Any]					 =	shard(lowerCAmelCase__		)
					__SCREAMING_SNAKE_CASE :	List[Any]					 =	shard(lowerCAmelCase__		)

					__SCREAMING_SNAKE_CASE :	Tuple					 =	pipe(
					    prompt_ids=lowerCAmelCase__							,  image=lowerCAmelCase__							,  params=lowerCAmelCase__							,  prng_seed=lowerCAmelCase__							,  num_inference_steps=5_0							,  jit=lowerCAmelCase__							,  ).images
					assert images.shape == (jax.device_count(), 1, 7_6_8, 5_1_2, 3)

					__SCREAMING_SNAKE_CASE :	Optional[Any]					 =	images.reshape((images.shape[0] * images.shape[1],) + images.shape[-3:]		)
					__SCREAMING_SNAKE_CASE :	List[Any]					 =	images[0, 2_5_3:2_5_6, 2_5_3:2_5_6, -1]

					__SCREAMING_SNAKE_CASE :	Tuple					 =	jnp.asarray(jax.device_get(image_slice.flatten()		)		)
					__SCREAMING_SNAKE_CASE :	Tuple					 =	jnp.array(
					    [[0.27_14_84, 0.26_17_19, 0.27_53_91, 0.27_73_44, 0.27_92_97, 0.29_10_16, 0.29_49_22, 0.30_27_34, 0.30_27_34]]		)
					print(F"output_slice: {output_slice}"		)
					assert jnp.abs(output_slice - expected_slice		).max() < 1E-2