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





       """simple docstring"""




       def        UpperCamelCase				(							self      )    ->			str:
          with tempfile.TemporaryDirectory() as tmpdirname:
             # pipeline has Flax weights
             _SCREAMING_SNAKE_CASE      =				FlaxDiffusionPipeline.from_pretrained(
                 """hf-internal-testing/tiny-stable-diffusion-pipe"""       ,							safety_checker=A__       ,							cache_dir=A__      )

             _SCREAMING_SNAKE_CASE      =				[t[-1] for t in os.walk(os.path.join(A__       ,							os.listdir(A__      )[0]       ,							"""snapshots"""      )      )]
             _SCREAMING_SNAKE_CASE      =				[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    _a				(unittest.TestCase):





       """simple docstring"""




       def        UpperCamelCase				(							self      )    ->			Optional[Any]:
          _SCREAMING_SNAKE_CASE ,							_SCREAMING_SNAKE_CASE      =				FlaxStableDiffusionPipeline.from_pretrained(
              """hf-internal-testing/tiny-stable-diffusion-pipe"""       ,							safety_checker=A__      )

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

          _SCREAMING_SNAKE_CASE      =				jax.random.PRNGKey(0      )
          _SCREAMING_SNAKE_CASE      =				4

          _SCREAMING_SNAKE_CASE      =				jax.device_count()
          _SCREAMING_SNAKE_CASE      =				num_samples * [prompt]
          _SCREAMING_SNAKE_CASE      =				pipeline.prepare_inputs(A__      )

          # shard inputs and rng
          _SCREAMING_SNAKE_CASE      =				replicate(A__      )
          _SCREAMING_SNAKE_CASE      =				jax.random.split(A__       ,							A__      )
          _SCREAMING_SNAKE_CASE      =				shard(A__      )

          _SCREAMING_SNAKE_CASE      =				pipeline(A__       ,							A__       ,							A__       ,							A__       ,							jit=A__      ).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(A__       ,							dtype=np.floataa      ).sum() - 4_99_47.8_75      ) < 5E-1

          _SCREAMING_SNAKE_CASE      =				pipeline.numpy_to_pil(np.asarray(images.reshape((num_samples,) + images.shape[-3:]      )      )      )
          assert len(A__      ) == num_samples






       def        UpperCamelCase				(							self      )    ->			Optional[int]:
          _SCREAMING_SNAKE_CASE ,							_SCREAMING_SNAKE_CASE      =				FlaxStableDiffusionPipeline.from_pretrained(
              """CompVis/stable-diffusion-v1-4"""       ,							revision="""flax"""       ,							safety_checker=A__      )

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

          _SCREAMING_SNAKE_CASE      =				jax.random.PRNGKey(0      )
          _SCREAMING_SNAKE_CASE      =				50

          _SCREAMING_SNAKE_CASE      =				jax.device_count()
          _SCREAMING_SNAKE_CASE      =				num_samples * [prompt]
          _SCREAMING_SNAKE_CASE      =				pipeline.prepare_inputs(A__      )

          # shard inputs and rng
          _SCREAMING_SNAKE_CASE      =				replicate(A__      )
          _SCREAMING_SNAKE_CASE      =				jax.random.split(A__       ,							A__      )
          _SCREAMING_SNAKE_CASE      =				shard(A__      )

          _SCREAMING_SNAKE_CASE      =				pipeline(A__       ,							A__       ,							A__       ,							A__       ,							jit=A__      ).images

          assert images.shape == (num_samples, 1, 5_12, 5_12, 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(A__       ,							dtype=np.floataa      ).sum() - 2_38_38_08.2)      ) < 5E-1






       def        UpperCamelCase				(							self      )    ->			Any:
          _SCREAMING_SNAKE_CASE ,							_SCREAMING_SNAKE_CASE      =				FlaxStableDiffusionPipeline.from_pretrained(
              """CompVis/stable-diffusion-v1-4"""       ,							revision="""bf16"""       ,							dtype=jnp.bfloataa       ,							safety_checker=A__      )

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

          _SCREAMING_SNAKE_CASE      =				jax.random.PRNGKey(0      )
          _SCREAMING_SNAKE_CASE      =				50

          _SCREAMING_SNAKE_CASE      =				jax.device_count()
          _SCREAMING_SNAKE_CASE      =				num_samples * [prompt]
          _SCREAMING_SNAKE_CASE      =				pipeline.prepare_inputs(A__      )

          # shard inputs and rng
          _SCREAMING_SNAKE_CASE      =				replicate(A__      )
          _SCREAMING_SNAKE_CASE      =				jax.random.split(A__       ,							A__      )
          _SCREAMING_SNAKE_CASE      =				shard(A__      )

          _SCREAMING_SNAKE_CASE      =				pipeline(A__       ,							A__       ,							A__       ,							A__       ,							jit=A__      ).images

          assert images.shape == (num_samples, 1, 5_12, 5_12, 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(A__       ,							dtype=np.floataa      ).sum() - 2_37_35_16.75)      ) < 5E-1






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

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

          _SCREAMING_SNAKE_CASE      =				jax.random.PRNGKey(0      )
          _SCREAMING_SNAKE_CASE      =				50

          _SCREAMING_SNAKE_CASE      =				jax.device_count()
          _SCREAMING_SNAKE_CASE      =				num_samples * [prompt]
          _SCREAMING_SNAKE_CASE      =				pipeline.prepare_inputs(A__      )

          # shard inputs and rng
          _SCREAMING_SNAKE_CASE      =				replicate(A__      )
          _SCREAMING_SNAKE_CASE      =				jax.random.split(A__       ,							A__      )
          _SCREAMING_SNAKE_CASE      =				shard(A__      )

          _SCREAMING_SNAKE_CASE      =				pipeline(A__       ,							A__       ,							A__       ,							A__       ,							jit=A__      ).images

          assert images.shape == (num_samples, 1, 5_12, 5_12, 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(A__       ,							dtype=np.floataa      ).sum() - 2_37_35_16.75)      ) < 5E-1






       def        UpperCamelCase				(							self      )    ->			Tuple:
          _SCREAMING_SNAKE_CASE      =				FlaxDDIMScheduler(
              beta_start=0.0_0085       ,							beta_end=0.012       ,							beta_schedule="""scaled_linear"""       ,							set_alpha_to_one=A__       ,							steps_offset=1       ,							)

          _SCREAMING_SNAKE_CASE ,							_SCREAMING_SNAKE_CASE      =				FlaxStableDiffusionPipeline.from_pretrained(
              """CompVis/stable-diffusion-v1-4"""       ,							revision="""bf16"""       ,							dtype=jnp.bfloataa       ,							scheduler=A__       ,							safety_checker=A__       ,							)
          _SCREAMING_SNAKE_CASE      =				scheduler.create_state()

          _SCREAMING_SNAKE_CASE      =				scheduler_state

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

          _SCREAMING_SNAKE_CASE      =				jax.random.PRNGKey(0      )
          _SCREAMING_SNAKE_CASE      =				50

          _SCREAMING_SNAKE_CASE      =				jax.device_count()
          _SCREAMING_SNAKE_CASE      =				num_samples * [prompt]
          _SCREAMING_SNAKE_CASE      =				pipeline.prepare_inputs(A__      )

          # shard inputs and rng
          _SCREAMING_SNAKE_CASE      =				replicate(A__      )
          _SCREAMING_SNAKE_CASE      =				jax.random.split(A__       ,							A__      )
          _SCREAMING_SNAKE_CASE      =				shard(A__      )

          _SCREAMING_SNAKE_CASE      =				pipeline(A__       ,							A__       ,							A__       ,							A__       ,							jit=A__      ).images

          assert images.shape == (num_samples, 1, 5_12, 5_12, 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(A__       ,							dtype=np.floataa      ).sum() - 2_34_76_93.5)      ) < 5E-1






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

          _SCREAMING_SNAKE_CASE      =				jax.device_count()
          _SCREAMING_SNAKE_CASE      =				num_samples * [prompt]
          _SCREAMING_SNAKE_CASE      =				jax.random.split(jax.random.PRNGKey(0      )       ,							A__      )

          _SCREAMING_SNAKE_CASE ,							_SCREAMING_SNAKE_CASE      =				FlaxStableDiffusionPipeline.from_pretrained(
              """CompVis/stable-diffusion-v1-4"""       ,							revision="""bf16"""       ,							dtype=jnp.bfloataa       ,							safety_checker=A__       ,							)

          _SCREAMING_SNAKE_CASE      =				replicate(A__      )
          _SCREAMING_SNAKE_CASE      =				pipeline.prepare_inputs(A__      )
          _SCREAMING_SNAKE_CASE      =				shard(A__      )
          _SCREAMING_SNAKE_CASE      =				pipeline(A__       ,							A__       ,							A__       ,							jit=A__      ).images
          assert images.shape == (num_samples, 1, 5_12, 5_12, 3)
          _SCREAMING_SNAKE_CASE      =				images[2, 0, 2_56, 10:17, 1]

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

          _SCREAMING_SNAKE_CASE      =				replicate(A__      )
          _SCREAMING_SNAKE_CASE      =				pipeline.prepare_inputs(A__      )
          _SCREAMING_SNAKE_CASE      =				shard(A__      )
          _SCREAMING_SNAKE_CASE      =				pipeline(A__       ,							A__       ,							A__       ,							jit=A__      ).images
          assert images_eff.shape == (num_samples, 1, 5_12, 5_12, 3)
          _SCREAMING_SNAKE_CASE      =				images[2, 0, 2_56, 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








'''simple docstring'''



from pathlib import PurePosixPath
from typing import Optional

import fsspec
from fsspec import AbstractFileSystem
from huggingface_hub.hf_api import DatasetInfo

from ..utils.file_utils import get_authentication_headers_for_url
from ..utils.hub import hf_hub_url

class    _a				(_lowerCamelCase):





       """simple docstring"""




       SCREAMING_SNAKE_CASE      =  ''
       SCREAMING_SNAKE_CASE      =  'hf-legacy'  # "hf://"" is reserved for hffs






       def __init__(							self       ,							A__ = None       ,							A__ = None       ,							**A__       ,							)    ->			Optional[int]:
          super().__init__(self       ,							**A__      )
          _SCREAMING_SNAKE_CASE      =				repo_info
          _SCREAMING_SNAKE_CASE      =				token
          _SCREAMING_SNAKE_CASE      =				None






       def        UpperCamelCase				(							self      )    ->			Tuple:
          if self.dir_cache is None:
             _SCREAMING_SNAKE_CASE      =				{}
             for hf_file in self.repo_info.siblings:
                # TODO(QL): add sizes
                _SCREAMING_SNAKE_CASE      =				{
                    """name""": hf_file.rfilename,
                    """size""": None,
                    """type""": """file""",
                }
                self.dir_cache.update(
                    {
                        str(A__      ): {"""name""": str(A__      ), """size""": None, """type""": """directory"""}
                        for d in list(PurePosixPath(hf_file.rfilename      ).parents      )[:-1]
                    }      )






       def        UpperCamelCase				(							self       ,							A__       ,							A__ = "rb"       ,							**A__       ,							)    ->			Optional[int]:
          if not isinstance(self.repo_info       ,							A__      ):
             raise NotImplementedError(F"Open is only implemented for dataset repositories, but got {self.repo_info}"      )
          _SCREAMING_SNAKE_CASE      =				hf_hub_url(self.repo_info.id       ,							A__       ,							revision=self.repo_info.sha      )
          return fsspec.open(
              A__       ,							mode=A__       ,							headers=get_authentication_headers_for_url(A__       ,							use_auth_token=self.token      )       ,							client_kwargs={"""trust_env""": True}       ,							).open()






       def        UpperCamelCase				(							self       ,							A__       ,							**A__      )    ->			str:
          self._get_dirs()
          _SCREAMING_SNAKE_CASE      =				self._strip_protocol(A__      )
          if path in self.dir_cache:
             return self.dir_cache[path]
          else:
             raise FileNotFoundError(A__      )






       def        UpperCamelCase				(							self       ,							A__       ,							A__=False       ,							**A__      )    ->			List[Any]:
          self._get_dirs()
          _SCREAMING_SNAKE_CASE      =				PurePosixPath(path.strip("""/"""      )      )
          _SCREAMING_SNAKE_CASE      =				{}
          for p, f in self.dir_cache.items():
             _SCREAMING_SNAKE_CASE      =				PurePosixPath(p.strip("""/"""      )      )
             _SCREAMING_SNAKE_CASE      =				p.parent
             if root == path:
                _SCREAMING_SNAKE_CASE      =				f
          _SCREAMING_SNAKE_CASE      =				list(paths.values()      )
          if detail:
             return out
          else:
             return sorted(f["""name"""] for f in out      )






