
from manim import *





class 						_a   (						_UpperCamelCase       ):

       """simple docstring"""





       def     SCREAMING_SNAKE_CASE  (					self	):
        _UpperCAmelCase 				=Rectangle(height=0.5       ,					width=0.5	)
        _UpperCAmelCase 				=Rectangle(height=0.25       ,					width=0.25	)
        _UpperCAmelCase 				=Rectangle(height=0.46       ,					width=0.46	).set_stroke(width=0	)

        _UpperCAmelCase 				=[mem.copy() for i in range(6	)]
        _UpperCAmelCase 				=[mem.copy() for i in range(6	)]
        _UpperCAmelCase 				=VGroup(*_snake_case	).arrange(_snake_case       ,					buff=0	)
        _UpperCAmelCase 				=VGroup(*_snake_case	).arrange(_snake_case       ,					buff=0	)
        _UpperCAmelCase 				=VGroup(_snake_case       ,					_snake_case	).arrange(_snake_case       ,					buff=0	)
        _UpperCAmelCase 				=Text("CPU"       ,					font_size=24	)
        _UpperCAmelCase 				=Group(_snake_case       ,					_snake_case	).arrange(_snake_case       ,					buff=0.5       ,					aligned_edge=_snake_case	)
        cpu.move_to([-2.5, -0.5, 0]	)
        self.add(_snake_case	)

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

        _UpperCAmelCase 				=[mem.copy() for i in range(6	)]
        _UpperCAmelCase 				=VGroup(*_snake_case	).arrange(_snake_case       ,					buff=0	)

        _UpperCAmelCase 				=Text("Model"       ,					font_size=24	)
        _UpperCAmelCase 				=Group(_snake_case       ,					_snake_case	).arrange(_snake_case       ,					buff=0.5       ,					aligned_edge=_snake_case	)
        model.move_to([3, -1.0, 0]	)
        self.add(_snake_case	)

        _UpperCAmelCase 				=[]
        _UpperCAmelCase 				=[]
        _UpperCAmelCase 				=[]

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

         _UpperCAmelCase 				=Rectangle(height=0.46 / 4       ,					width=0.46 / 3	).set_stroke(width=0.0	).set_fill(_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=_snake_case	)
          cpu_target.set_x(cpu_target.get_x() + 0.1	)
         elif i == 3:
          cpu_target.next_to(model_cpu_arr[0]       ,					direction=_snake_case       ,					buff=0.0	)
         else:
          cpu_target.next_to(model_cpu_arr[i - 1]       ,					direction=_snake_case       ,					buff=0.0	)
         self.add(_snake_case	)
         model_cpu_arr.append(_snake_case	)

        self.add(*_snake_case       ,					*_snake_case       ,					*_snake_case	)

        _UpperCAmelCase 				=[mem.copy() for i in range(6	)]
        _UpperCAmelCase 				=VGroup(*_snake_case	).arrange(_snake_case       ,					buff=0	)

        _UpperCAmelCase 				=Text("Loaded Checkpoint"       ,					font_size=24	)
        _UpperCAmelCase 				=Group(_snake_case       ,					_snake_case	).arrange(_snake_case       ,					buff=0.5       ,					aligned_edge=_snake_case	)
        checkpoint.move_to([3, 0.5, 0]	)

        self.add(_snake_case	)

        _UpperCAmelCase 				=[]
        _UpperCAmelCase 				=[]

        for i, rect in enumerate(_snake_case	):
         _UpperCAmelCase 				=fill.copy().set_fill(_snake_case       ,					opacity=0.7	)
         target.move_to(_snake_case	)
         ckpt_arr.append(_snake_case	)

         _UpperCAmelCase 				=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(_snake_case	)
        self.add(*_snake_case       ,					*_snake_case	)

        _UpperCAmelCase 				=Square(side_length=2.2	)
        key.move_to([-5, 2, 0]	)

        _UpperCAmelCase 				=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(_snake_case       ,					_snake_case	)

        _UpperCAmelCase 				=MarkupText(
            F"<span fgcolor=\'{BLUE}\'>●</span> Checkpoint"       ,					font_size=18       ,					)

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

        _UpperCAmelCase 				=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]	)

        _UpperCAmelCase 				=[meta_mem.copy() for i in range(6	)]
        _UpperCAmelCase 				=[meta_mem.copy() for i in range(6	)]
        _UpperCAmelCase 				=VGroup(*_snake_case	).arrange(_snake_case       ,					buff=0	)
        _UpperCAmelCase 				=VGroup(*_snake_case	).arrange(_snake_case       ,					buff=0	)
        _UpperCAmelCase 				=VGroup(_snake_case       ,					_snake_case	).arrange(_snake_case       ,					buff=0	)
        _UpperCAmelCase 				=Text("Disk"       ,					font_size=24	)
        _UpperCAmelCase 				=Group(_snake_case       ,					_snake_case	).arrange(_snake_case       ,					buff=0.5       ,					aligned_edge=_snake_case	)
        disk.move_to([-4.0, -1.25, 0]	)
        self.play(Write(_snake_case       ,					run_time=3	)       ,					Write(_snake_case       ,					run_time=1	)       ,					Create(_snake_case       ,					run_time=1	)	)

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

        self.play(FadeOut(_snake_case	)	)

        _UpperCAmelCase 				=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(_snake_case       ,					run_time=3	)	)

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

        self.wait()










"""simple docstring"""




import os
import tempfile
import unittest

import numpy as np

from diffusers.utils import is_flax_available
from diffusers.utils.testing_utils import require_flax, slow


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

 from diffusers import FlaxDDIMScheduler, FlaxDiffusionPipeline, FlaxStableDiffusionPipeline






@require_flax
class       UpperCAmelCase_    (							unittest.TestCase		):



      def 		snake_case_   (	self	:      Optional[int]		):
           with tempfile.TemporaryDirectory() as tmpdirname:
                # pipeline has Flax weights
                _UpperCAmelCase     :      Union[str, Any]														=       FlaxDiffusionPipeline.from_pretrained(
                    "hf-internal-testing/tiny-stable-diffusion-pipe"	,      safety_checker=A	,      cache_dir=A		)

                _UpperCAmelCase     :      List[str]														=       [t[-1] for t in os.walk(os.path.join(A	,      os.listdir(A		)[0]	,      "snapshots"		)		)]
                _UpperCAmelCase     :      Union[str, Any]														=       [item for sublist in all_root_files for item in sublist]

                # None of the downloaded files should be a PyTorch file even if we have some here:
                # https://huggingface.co/hf-internal-testing/tiny-stable-diffusion-pipe/blob/main/unet/diffusion_pytorch_model.bin
                assert not any(f.endswith(".bin"		) for f in files		)







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



      def 		snake_case_   (	self	:      List[str]		):
           _UpperCAmelCase							,			_UpperCAmelCase     :      int														=       FlaxStableDiffusionPipeline.from_pretrained(
               "hf-internal-testing/tiny-stable-diffusion-pipe"	,      safety_checker=A		)

           _UpperCAmelCase     :      str														=       (
               "A cinematic film still of Morgan Freeman starring as Jimi Hendrix, portrait, 40mm lens, shallow depth of"
               " field, close up, split lighting, cinematic"
           )

           _UpperCAmelCase     :      int														=       jax.random.PRNGKey(0		)
           _UpperCAmelCase     :      List[str]														=       4

           _UpperCAmelCase     :      Any														=       jax.device_count()
           _UpperCAmelCase     :      Tuple														=       num_samples * [prompt]
           _UpperCAmelCase     :      Optional[int]														=       pipeline.prepare_inputs(A		)

           # shard inputs and rng
           _UpperCAmelCase     :      Union[str, Any]														=       replicate(A		)
           _UpperCAmelCase     :      List[Any]														=       jax.random.split(A	,      A		)
           _UpperCAmelCase     :      List[Any]														=       shard(A		)

           _UpperCAmelCase     :      Tuple														=       pipeline(A	,      A	,      A	,      A	,      jit=A		).images

           assert images.shape == (num_samples, 1, 6_4, 6_4, 3)
           if jax.device_count() == 8:
                assert np.abs(np.abs(images[0, 0, :2, :2, -2:]	,      dtype=np.floataa		).sum() - 4.1_514_745		) < 1e-3
                assert np.abs(np.abs(A	,      dtype=np.floataa		).sum() - 49_947.875		) < 5e-1

           _UpperCAmelCase     :      Optional[int]														=       pipeline.numpy_to_pil(np.asarray(images.reshape((num_samples,) + images.shape[-3:]		)		)		)
           assert len(A		) == num_samples



      def 		snake_case_   (	self	:      Union[str, Any]		):
           _UpperCAmelCase							,			_UpperCAmelCase     :      Optional[Any]														=       FlaxStableDiffusionPipeline.from_pretrained(
               "CompVis/stable-diffusion-v1-4"	,      revision="flax"	,      safety_checker=A		)

           _UpperCAmelCase     :      Dict														=       (
               "A cinematic film still of Morgan Freeman starring as Jimi Hendrix, portrait, 40mm lens, shallow depth of"
               " field, close up, split lighting, cinematic"
           )

           _UpperCAmelCase     :      Dict														=       jax.random.PRNGKey(0		)
           _UpperCAmelCase     :      str														=       5_0

           _UpperCAmelCase     :      List[str]														=       jax.device_count()
           _UpperCAmelCase     :      Any														=       num_samples * [prompt]
           _UpperCAmelCase     :      Dict														=       pipeline.prepare_inputs(A		)

           # shard inputs and rng
           _UpperCAmelCase     :      Dict														=       replicate(A		)
           _UpperCAmelCase     :      str														=       jax.random.split(A	,      A		)
           _UpperCAmelCase     :      Optional[Any]														=       shard(A		)

           _UpperCAmelCase     :      List[Any]														=       pipeline(A	,      A	,      A	,      A	,      jit=A		).images

           assert images.shape == (num_samples, 1, 5_1_2, 5_1_2, 3)
           if jax.device_count() == 8:
                assert np.abs((np.abs(images[0, 0, :2, :2, -2:]	,      dtype=np.floataa		).sum() - 0.05_652_401)		) < 1e-3
                assert np.abs((np.abs(A	,      dtype=np.floataa		).sum() - 2_383_808.2)		) < 5e-1



      def 		snake_case_   (	self	:      List[Any]		):
           _UpperCAmelCase							,			_UpperCAmelCase     :      List[Any]														=       FlaxStableDiffusionPipeline.from_pretrained(
               "CompVis/stable-diffusion-v1-4"	,      revision="bf16"	,      dtype=jnp.bfloataa	,      safety_checker=A		)

           _UpperCAmelCase     :      Any														=       (
               "A cinematic film still of Morgan Freeman starring as Jimi Hendrix, portrait, 40mm lens, shallow depth of"
               " field, close up, split lighting, cinematic"
           )

           _UpperCAmelCase     :      List[Any]														=       jax.random.PRNGKey(0		)
           _UpperCAmelCase     :      List[Any]														=       5_0

           _UpperCAmelCase     :      Union[str, Any]														=       jax.device_count()
           _UpperCAmelCase     :      Optional[int]														=       num_samples * [prompt]
           _UpperCAmelCase     :      Union[str, Any]														=       pipeline.prepare_inputs(A		)

           # shard inputs and rng
           _UpperCAmelCase     :      int														=       replicate(A		)
           _UpperCAmelCase     :      Optional[int]														=       jax.random.split(A	,      A		)
           _UpperCAmelCase     :      str														=       shard(A		)

           _UpperCAmelCase     :      Union[str, Any]														=       pipeline(A	,      A	,      A	,      A	,      jit=A		).images

           assert images.shape == (num_samples, 1, 5_1_2, 5_1_2, 3)
           if jax.device_count() == 8:
                assert np.abs((np.abs(images[0, 0, :2, :2, -2:]	,      dtype=np.floataa		).sum() - 0.04_003_906)		) < 1e-3
                assert np.abs((np.abs(A	,      dtype=np.floataa		).sum() - 2_373_516.75)		) < 5e-1



      def 		snake_case_   (	self	:      int		):
           _UpperCAmelCase							,			_UpperCAmelCase     :      Dict														=       FlaxStableDiffusionPipeline.from_pretrained(
               "CompVis/stable-diffusion-v1-4"	,      revision="bf16"	,      dtype=jnp.bfloataa		)

           _UpperCAmelCase     :      str														=       (
               "A cinematic film still of Morgan Freeman starring as Jimi Hendrix, portrait, 40mm lens, shallow depth of"
               " field, close up, split lighting, cinematic"
           )

           _UpperCAmelCase     :      Optional[int]														=       jax.random.PRNGKey(0		)
           _UpperCAmelCase     :      Dict														=       5_0

           _UpperCAmelCase     :      Union[str, Any]														=       jax.device_count()
           _UpperCAmelCase     :      str														=       num_samples * [prompt]
           _UpperCAmelCase     :      int														=       pipeline.prepare_inputs(A		)

           # shard inputs and rng
           _UpperCAmelCase     :      Optional[int]														=       replicate(A		)
           _UpperCAmelCase     :      Tuple														=       jax.random.split(A	,      A		)
           _UpperCAmelCase     :      Dict														=       shard(A		)

           _UpperCAmelCase     :      Optional[Any]														=       pipeline(A	,      A	,      A	,      A	,      jit=A		).images

           assert images.shape == (num_samples, 1, 5_1_2, 5_1_2, 3)
           if jax.device_count() == 8:
                assert np.abs((np.abs(images[0, 0, :2, :2, -2:]	,      dtype=np.floataa		).sum() - 0.04_003_906)		) < 1e-3
                assert np.abs((np.abs(A	,      dtype=np.floataa		).sum() - 2_373_516.75)		) < 5e-1



      def 		snake_case_   (	self	:      Optional[Any]		):
           _UpperCAmelCase     :      Dict														=       FlaxDDIMScheduler(
               beta_start=0.00_085	,      beta_end=0.012	,      beta_schedule="scaled_linear"	,      set_alpha_to_one=A	,      steps_offset=1	,      )

           _UpperCAmelCase							,			_UpperCAmelCase     :      Dict														=       FlaxStableDiffusionPipeline.from_pretrained(
               "CompVis/stable-diffusion-v1-4"	,      revision="bf16"	,      dtype=jnp.bfloataa	,      scheduler=A	,      safety_checker=A	,      )
           _UpperCAmelCase     :      Union[str, Any]														=       scheduler.create_state()

           _UpperCAmelCase     :      str														=       scheduler_state

           _UpperCAmelCase     :      Union[str, Any]														=       (
               "A cinematic film still of Morgan Freeman starring as Jimi Hendrix, portrait, 40mm lens, shallow depth of"
               " field, close up, split lighting, cinematic"
           )

           _UpperCAmelCase     :      str														=       jax.random.PRNGKey(0		)
           _UpperCAmelCase     :      List[str]														=       5_0

           _UpperCAmelCase     :      Optional[int]														=       jax.device_count()
           _UpperCAmelCase     :      int														=       num_samples * [prompt]
           _UpperCAmelCase     :      Optional[int]														=       pipeline.prepare_inputs(A		)

           # shard inputs and rng
           _UpperCAmelCase     :      List[Any]														=       replicate(A		)
           _UpperCAmelCase     :      Tuple														=       jax.random.split(A	,      A		)
           _UpperCAmelCase     :      Union[str, Any]														=       shard(A		)

           _UpperCAmelCase     :      Dict														=       pipeline(A	,      A	,      A	,      A	,      jit=A		).images

           assert images.shape == (num_samples, 1, 5_1_2, 5_1_2, 3)
           if jax.device_count() == 8:
                assert np.abs((np.abs(images[0, 0, :2, :2, -2:]	,      dtype=np.floataa		).sum() - 0.045_043_945)		) < 1e-3
                assert np.abs((np.abs(A	,      dtype=np.floataa		).sum() - 2_347_693.5)		) < 5e-1



      def 		snake_case_   (	self	:      int		):
           _UpperCAmelCase     :      Optional[Any]														=       (
               "A cinematic film still of Morgan Freeman starring as Jimi Hendrix, portrait, 40mm lens, shallow depth of"
               " field, close up, split lighting, cinematic"
           )

           _UpperCAmelCase     :      Optional[Any]														=       jax.device_count()
           _UpperCAmelCase     :      Union[str, Any]														=       num_samples * [prompt]
           _UpperCAmelCase     :      Optional[Any]														=       jax.random.split(jax.random.PRNGKey(0		)	,      A		)

           _UpperCAmelCase							,			_UpperCAmelCase     :      str														=       FlaxStableDiffusionPipeline.from_pretrained(
               "CompVis/stable-diffusion-v1-4"	,      revision="bf16"	,      dtype=jnp.bfloataa	,      safety_checker=A	,      )

           _UpperCAmelCase     :      int														=       replicate(A		)
           _UpperCAmelCase     :      Union[str, Any]														=       pipeline.prepare_inputs(A		)
           _UpperCAmelCase     :      int														=       shard(A		)
           _UpperCAmelCase     :      Dict														=       pipeline(A	,      A	,      A	,      jit=A		).images
           assert images.shape == (num_samples, 1, 5_1_2, 5_1_2, 3)
           _UpperCAmelCase     :      List[str]														=       images[2, 0, 2_5_6, 1_0:1_7, 1]

           # With memory efficient attention
           _UpperCAmelCase							,			_UpperCAmelCase     :      str														=       FlaxStableDiffusionPipeline.from_pretrained(
               "CompVis/stable-diffusion-v1-4"	,      revision="bf16"	,      dtype=jnp.bfloataa	,      safety_checker=A	,      use_memory_efficient_attention=A	,      )

           _UpperCAmelCase     :      Any														=       replicate(A		)
           _UpperCAmelCase     :      str														=       pipeline.prepare_inputs(A		)
           _UpperCAmelCase     :      Tuple														=       shard(A		)
           _UpperCAmelCase     :      Tuple														=       pipeline(A	,      A	,      A	,      jit=A		).images
           assert images_eff.shape == (num_samples, 1, 5_1_2, 5_1_2, 3)
           _UpperCAmelCase     :      str														=       images[2, 0, 2_5_6, 1_0:1_7, 1]

           # I checked the results visually and they are very similar. However, I saw that the max diff is `1` and the `sum`
           # over the 8 images is exactly `256`, which is very suspicious. Testing a random slice for now.
           assert abs(slice_eff - slice		).max() < 1e-2
