


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     ):


   '''simple docstring'''







   def 		UpperCamelCase  (     self		):
        with tempfile.TemporaryDirectory() as tmpdirname:
             # pipeline has Flax weights
             lowercase_       :Tuple     =       FlaxDiffusionPipeline.from_pretrained(
                 '''hf-internal-testing/tiny-stable-diffusion-pipe'''   ,							safety_checker=UpperCamelCase_   ,							cache_dir=UpperCamelCase_		)

             lowercase_       :Tuple     =       [t[-1] for t in os.walk(os.path.join(UpperCamelCase_   ,							os.listdir(UpperCamelCase_		)[0]   ,							'''snapshots'''		)		)]
             lowercase_       :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     ):


   '''simple docstring'''







   def 		UpperCamelCase  (     self		):
        lowercase_    ,      lowercase_       :Dict     =       FlaxStableDiffusionPipeline.from_pretrained(
            '''hf-internal-testing/tiny-stable-diffusion-pipe'''   ,							safety_checker=UpperCamelCase_		)

        lowercase_       :Optional[int]     =       (
            '''A cinematic film still of Morgan Freeman starring as Jimi Hendrix, portrait, 40mm lens, shallow depth of'''
            ''' field, close up, split lighting, cinematic'''
        )

        lowercase_       :List[Any]     =       jax.random.PRNGKey(0		)
        lowercase_       :Optional[Any]     =       4

        lowercase_       :Optional[Any]     =       jax.device_count()
        lowercase_       :List[Any]     =       num_samples * [prompt]
        lowercase_       :int     =       pipeline.prepare_inputs(UpperCamelCase_		)

        # shard inputs and rng
        lowercase_       :Union[str, Any]     =       replicate(UpperCamelCase_		)
        lowercase_       :List[Any]     =       jax.random.split(UpperCamelCase_   ,							UpperCamelCase_		)
        lowercase_       :Optional[int]     =       shard(UpperCamelCase_		)

        lowercase_       :List[str]     =       pipeline(UpperCamelCase_   ,							UpperCamelCase_   ,							UpperCamelCase_   ,							UpperCamelCase_   ,							jit=UpperCamelCase_		).images

        assert images.shape == (num_samples, 1, 64, 64, 3)
        if jax.device_count() == 8:
             assert np.abs(np.abs(images[0, 0, :2, :2, -2:]   ,							dtype=np.floataa		).sum() - 4.151_4745		) < 1E-3
             assert np.abs(np.abs(UpperCamelCase_   ,							dtype=np.floataa		).sum() - 4_9947.875		) < 5E-1

        lowercase_       :Any     =       pipeline.numpy_to_pil(np.asarray(images.reshape((num_samples,) + images.shape[-3:]		)		)		)
        assert len(UpperCamelCase_		) == num_samples
   def 		UpperCamelCase  (     self		):
        lowercase_    ,      lowercase_       :Union[str, Any]     =       FlaxStableDiffusionPipeline.from_pretrained(
            '''CompVis/stable-diffusion-v1-4'''   ,							revision='''flax'''   ,							safety_checker=UpperCamelCase_		)

        lowercase_       :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'''
        )

        lowercase_       :str     =       jax.random.PRNGKey(0		)
        lowercase_       :Tuple     =       50

        lowercase_       :List[str]     =       jax.device_count()
        lowercase_       :Optional[Any]     =       num_samples * [prompt]
        lowercase_       :str     =       pipeline.prepare_inputs(UpperCamelCase_		)

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

        lowercase_       :Optional[Any]     =       pipeline(UpperCamelCase_   ,							UpperCamelCase_   ,							UpperCamelCase_   ,							UpperCamelCase_   ,							jit=UpperCamelCase_		).images

        assert images.shape == (num_samples, 1, 512, 512, 3)
        if jax.device_count() == 8:
             assert np.abs((np.abs(images[0, 0, :2, :2, -2:]   ,							dtype=np.floataa		).sum() - 0.0565_2401)		) < 1E-3
             assert np.abs((np.abs(UpperCamelCase_   ,							dtype=np.floataa		).sum() - 238_3808.2)		) < 5E-1
   def 		UpperCamelCase  (     self		):
        lowercase_    ,      lowercase_       :Tuple     =       FlaxStableDiffusionPipeline.from_pretrained(
            '''CompVis/stable-diffusion-v1-4'''   ,							revision='''bf16'''   ,							dtype=jnp.bfloataa   ,							safety_checker=UpperCamelCase_		)

        lowercase_       :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'''
        )

        lowercase_       :Union[str, Any]     =       jax.random.PRNGKey(0		)
        lowercase_       :Tuple     =       50

        lowercase_       :Optional[int]     =       jax.device_count()
        lowercase_       :Dict     =       num_samples * [prompt]
        lowercase_       :List[Any]     =       pipeline.prepare_inputs(UpperCamelCase_		)

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

        lowercase_       :List[Any]     =       pipeline(UpperCamelCase_   ,							UpperCamelCase_   ,							UpperCamelCase_   ,							UpperCamelCase_   ,							jit=UpperCamelCase_		).images

        assert images.shape == (num_samples, 1, 512, 512, 3)
        if jax.device_count() == 8:
             assert np.abs((np.abs(images[0, 0, :2, :2, -2:]   ,							dtype=np.floataa		).sum() - 0.0400_3906)		) < 1E-3
             assert np.abs((np.abs(UpperCamelCase_   ,							dtype=np.floataa		).sum() - 237_3516.75)		) < 5E-1
   def 		UpperCamelCase  (     self		):
        lowercase_    ,      lowercase_       :Tuple     =       FlaxStableDiffusionPipeline.from_pretrained(
            '''CompVis/stable-diffusion-v1-4'''   ,							revision='''bf16'''   ,							dtype=jnp.bfloataa		)

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

        lowercase_       :Union[str, Any]     =       jax.random.PRNGKey(0		)
        lowercase_       :Optional[Any]     =       50

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

        # shard inputs and rng
        lowercase_       :Optional[int]     =       replicate(UpperCamelCase_		)
        lowercase_       :int     =       jax.random.split(UpperCamelCase_   ,							UpperCamelCase_		)
        lowercase_       :Optional[int]     =       shard(UpperCamelCase_		)

        lowercase_       :List[Any]     =       pipeline(UpperCamelCase_   ,							UpperCamelCase_   ,							UpperCamelCase_   ,							UpperCamelCase_   ,							jit=UpperCamelCase_		).images

        assert images.shape == (num_samples, 1, 512, 512, 3)
        if jax.device_count() == 8:
             assert np.abs((np.abs(images[0, 0, :2, :2, -2:]   ,							dtype=np.floataa		).sum() - 0.0400_3906)		) < 1E-3
             assert np.abs((np.abs(UpperCamelCase_   ,							dtype=np.floataa		).sum() - 237_3516.75)		) < 5E-1
   def 		UpperCamelCase  (     self		):
        lowercase_       :Optional[Any]     =       FlaxDDIMScheduler(
            beta_start=0.0_0085   ,							beta_end=0.012   ,							beta_schedule='''scaled_linear'''   ,							set_alpha_to_one=UpperCamelCase_   ,							steps_offset=1   ,							)

        lowercase_    ,      lowercase_       :Optional[Any]     =       FlaxStableDiffusionPipeline.from_pretrained(
            '''CompVis/stable-diffusion-v1-4'''   ,							revision='''bf16'''   ,							dtype=jnp.bfloataa   ,							scheduler=UpperCamelCase_   ,							safety_checker=UpperCamelCase_   ,							)
        lowercase_       :Optional[Any]     =       scheduler.create_state()

        lowercase_       :Optional[Any]     =       scheduler_state

        lowercase_       :Optional[int]     =       (
            '''A cinematic film still of Morgan Freeman starring as Jimi Hendrix, portrait, 40mm lens, shallow depth of'''
            ''' field, close up, split lighting, cinematic'''
        )

        lowercase_       :Dict     =       jax.random.PRNGKey(0		)
        lowercase_       :Optional[int]     =       50

        lowercase_       :Dict     =       jax.device_count()
        lowercase_       :List[str]     =       num_samples * [prompt]
        lowercase_       :Optional[int]     =       pipeline.prepare_inputs(UpperCamelCase_		)

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

        lowercase_       :str     =       pipeline(UpperCamelCase_   ,							UpperCamelCase_   ,							UpperCamelCase_   ,							UpperCamelCase_   ,							jit=UpperCamelCase_		).images

        assert images.shape == (num_samples, 1, 512, 512, 3)
        if jax.device_count() == 8:
             assert np.abs((np.abs(images[0, 0, :2, :2, -2:]   ,							dtype=np.floataa		).sum() - 0.0_4504_3945)		) < 1E-3
             assert np.abs((np.abs(UpperCamelCase_   ,							dtype=np.floataa		).sum() - 234_7693.5)		) < 5E-1




   def 		UpperCamelCase  (     self		):
        lowercase_       :int     =       (
            '''A cinematic film still of Morgan Freeman starring as Jimi Hendrix, portrait, 40mm lens, shallow depth of'''
            ''' field, close up, split lighting, cinematic'''
        )

        lowercase_       :Optional[int]     =       jax.device_count()
        lowercase_       :int     =       num_samples * [prompt]
        lowercase_       :Optional[Any]     =       jax.random.split(jax.random.PRNGKey(0		)   ,							UpperCamelCase_		)

        lowercase_    ,      lowercase_       :Optional[Any]     =       FlaxStableDiffusionPipeline.from_pretrained(
            '''CompVis/stable-diffusion-v1-4'''   ,							revision='''bf16'''   ,							dtype=jnp.bfloataa   ,							safety_checker=UpperCamelCase_   ,							)

        lowercase_       :str     =       replicate(UpperCamelCase_		)
        lowercase_       :int     =       pipeline.prepare_inputs(UpperCamelCase_		)
        lowercase_       :Any     =       shard(UpperCamelCase_		)
        lowercase_       :Union[str, Any]     =       pipeline(UpperCamelCase_   ,							UpperCamelCase_   ,							UpperCamelCase_   ,							jit=UpperCamelCase_		).images
        assert images.shape == (num_samples, 1, 512, 512, 3)
        lowercase_       :str     =       images[2, 0, 256, 10:17, 1]

        # With memory efficient attention
        lowercase_    ,      lowercase_       :List[Any]     =       FlaxStableDiffusionPipeline.from_pretrained(
            '''CompVis/stable-diffusion-v1-4'''   ,							revision='''bf16'''   ,							dtype=jnp.bfloataa   ,							safety_checker=UpperCamelCase_   ,							use_memory_efficient_attention=UpperCamelCase_   ,							)

        lowercase_       :List[Any]     =       replicate(UpperCamelCase_		)
        lowercase_       :Dict     =       pipeline.prepare_inputs(UpperCamelCase_		)
        lowercase_       :Optional[int]     =       shard(UpperCamelCase_		)
        lowercase_       :Any     =       pipeline(UpperCamelCase_   ,							UpperCamelCase_   ,							UpperCamelCase_   ,							jit=UpperCamelCase_		).images
        assert images_eff.shape == (num_samples, 1, 512, 512, 3)
        lowercase_       :Optional[Any]     =       images[2, 0, 256, 10:17, 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





import numpy as np
from numpy import ndarray
from scipy.optimize import Bounds, LinearConstraint, minimize




def     UpperCamelCase    (      _a		)					->							float:
     '''simple docstring'''





     return np.dot(_a       ,		_a		)






class      UpperCamelCase					:


   '''simple docstring'''







   def __init__(     self   ,							*,
       UpperCamelCase_ = np.inf   ,							UpperCamelCase_ = "linear"   ,							UpperCamelCase_ = 0.0   ,							):
        lowercase_       :Dict     =       regularization
        lowercase_       :Optional[Any]     =       gamma
        if kernel == "linear":
             lowercase_       :Optional[Any]     =       self.__linear
        elif kernel == "rbf":
             if self.gamma == 0:
                  raise ValueError('''rbf kernel requires gamma'''		)
             if not isinstance(self.gamma   ,							(float, int)		):
                  raise ValueError('''gamma must be float or int'''		)
             if not self.gamma > 0:
                  raise ValueError('''gamma must be > 0'''		)
             lowercase_       :Union[str, Any]     =       self.__rbf
             # in the future, there could be a default value like in sklearn
             # sklear: def_gamma = 1/(n_features * X.var()) (wiki)
             # previously it was 1/(n_features)
        else:
             lowercase_       :str     =       f"Unknown kernel: {kernel}"
             raise ValueError(UpperCamelCase_		)
   def 		UpperCamelCase  (     self   ,							UpperCamelCase_   ,							UpperCamelCase_		):
        return np.dot(UpperCamelCase_   ,							UpperCamelCase_		)
   def 		UpperCamelCase  (     self   ,							UpperCamelCase_   ,							UpperCamelCase_		):
        return np.exp(-(self.gamma * norm_squared(vectora - vectora		))		)
   def 		UpperCamelCase  (     self   ,							UpperCamelCase_   ,							UpperCamelCase_		):
        lowercase_       :Tuple     =       observations
        lowercase_       :Union[str, Any]     =       classes

        # using Wolfe's Dual to calculate w.
        # Primal problem: minimize 1/2*norm_squared(w)
        #   constraint: yn(w . xn + b) >= 1
        #
        # With l a vector
        # Dual problem: maximize sum_n(ln) -
        #       1/2 * sum_n(sum_m(ln*lm*yn*ym*xn . xm))
        #   constraint: self.C >= ln >= 0
        #           and sum_n(ln*yn) = 0
        # Then we get w using w = sum_n(ln*yn*xn)
        # At the end we can get b ~= mean(yn - w . xn)
        #
        # Since we use kernels, we only need l_star to calculate b
        # and to classify observations

        ((lowercase_)    ,      )       :Optional[Any]     =       np.shape(UpperCamelCase_		)

        def to_minimize(UpperCamelCase_		) -> float:
             lowercase_       :Dict     =       0
             ((lowercase_)    ,      )       :Tuple     =       np.shape(UpperCamelCase_		)
             for i in range(UpperCamelCase_		):
                  for j in range(UpperCamelCase_		):
                       s += (
                           candidate[i]
                           * candidate[j]
                           * classes[i]
                           * classes[j]
                           * self.kernel(observations[i]   ,							observations[j]		)
                       )
             return 1 / 2 * s - sum(UpperCamelCase_		)

        lowercase_       :Dict     =       LinearConstraint(UpperCamelCase_   ,							0   ,							0		)
        lowercase_       :Optional[Any]     =       Bounds(0   ,							self.regularization		)

        lowercase_       :Any     =       minimize(
            UpperCamelCase_   ,							np.ones(UpperCamelCase_		)   ,							bounds=UpperCamelCase_   ,							constraints=[ly_contraint]		).x
        lowercase_       :Tuple     =       l_star

        # calculating mean offset of separation plane to points
        lowercase_       :str     =       0
        for i in range(UpperCamelCase_		):
             for j in range(UpperCamelCase_		):
                  s += classes[i] - classes[i] * self.optimum[i] * self.kernel(
                      observations[i]   ,							observations[j]		)
        lowercase_       :List[str]     =       s / n




   def 		UpperCamelCase  (     self   ,							UpperCamelCase_		):
        lowercase_       :Union[str, Any]     =       sum(
            self.optimum[n]
            * self.classes[n]
            * self.kernel(self.observations[n]   ,							UpperCamelCase_		)
            for n in range(len(self.classes		)		)		)
        return 1 if s + self.offset >= 0 else -1


if __name__ == "__main__":
   import doctest

   doctest.testmod()

