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






    '''simple docstring'''






    def       UpperCamelCase__       (     self)	->     Union[str, Any]:
        with tempfile.TemporaryDirectory() as tmpdirname:
            # pipeline has Flax weights
            __UpperCamelCase					:List[str]        =		FlaxDiffusionPipeline.from_pretrained(
                '''hf-internal-testing/tiny-stable-diffusion-pipe'''			,			safety_checker=__lowercase			,			cache_dir=__lowercase)

            __UpperCamelCase					:int        =		[t[-1] for t in os.walk(os.path.join(__lowercase			,			os.listdir(__lowercase)[0]			,			'''snapshots'''))]
            __UpperCamelCase					: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      lowerCamelCase_					(       unittest.TestCase			):






    '''simple docstring'''






    def       UpperCamelCase__       (     self)	->     Union[str, Any]:
        __UpperCamelCase					:int        =		FlaxStableDiffusionPipeline.from_pretrained(
            '''hf-internal-testing/tiny-stable-diffusion-pipe'''			,			safety_checker=__lowercase)

        __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					:Any        =		jax.random.PRNGKey(0)
        __UpperCamelCase					:Dict        =		4

        __UpperCamelCase					:int        =		jax.device_count()
        __UpperCamelCase					:Any        =		num_samples * [prompt]
        __UpperCamelCase					:List[str]        =		pipeline.prepare_inputs(__lowercase)

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

        __UpperCamelCase					:str        =		pipeline(__lowercase			,			__lowercase			,			__lowercase			,			__lowercase			,			jit=__lowercase).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.1_51_47_45) < 1E-3
            assert np.abs(np.abs(__lowercase			,			dtype=np.floataa).sum() - 49_947.875) < 5E-1

        __UpperCamelCase					:Union[str, Any]        =		pipeline.numpy_to_pil(np.asarray(images.reshape((num_samples,) + images.shape[-3:])))
        assert len(__lowercase) == num_samples







    def       UpperCamelCase__       (     self)	->     Optional[Any]:
        __UpperCamelCase					:Optional[int]        =		FlaxStableDiffusionPipeline.from_pretrained(
            '''CompVis/stable-diffusion-v1-4'''			,			revision='''flax'''			,			safety_checker=__lowercase)

        __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					:Any        =		jax.random.PRNGKey(0)
        __UpperCamelCase					:Union[str, Any]        =		50

        __UpperCamelCase					:Union[str, Any]        =		jax.device_count()
        __UpperCamelCase					:int        =		num_samples * [prompt]
        __UpperCamelCase					:Dict        =		pipeline.prepare_inputs(__lowercase)

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

        __UpperCamelCase					:Optional[int]        =		pipeline(__lowercase			,			__lowercase			,			__lowercase			,			__lowercase			,			jit=__lowercase).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.05_65_24_01)) < 1E-3
            assert np.abs((np.abs(__lowercase			,			dtype=np.floataa).sum() - 2_383_808.2)) < 5E-1







    def       UpperCamelCase__       (     self)	->     Dict:
        __UpperCamelCase					:Any        =		FlaxStableDiffusionPipeline.from_pretrained(
            '''CompVis/stable-diffusion-v1-4'''			,			revision='''bf16'''			,			dtype=jnp.bfloataa			,			safety_checker=__lowercase)

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

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

        __UpperCamelCase					:Optional[int]        =		jax.device_count()
        __UpperCamelCase					:int        =		num_samples * [prompt]
        __UpperCamelCase					:str        =		pipeline.prepare_inputs(__lowercase)

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

        __UpperCamelCase					:int        =		pipeline(__lowercase			,			__lowercase			,			__lowercase			,			__lowercase			,			jit=__lowercase).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.04_00_39_06)) < 1E-3
            assert np.abs((np.abs(__lowercase			,			dtype=np.floataa).sum() - 2_373_516.75)) < 5E-1







    def       UpperCamelCase__       (     self)	->     str:
        __UpperCamelCase					:List[Any]        =		FlaxStableDiffusionPipeline.from_pretrained(
            '''CompVis/stable-diffusion-v1-4'''			,			revision='''bf16'''			,			dtype=jnp.bfloataa)

        __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					:Any        =		jax.random.PRNGKey(0)
        __UpperCamelCase					:Dict        =		50

        __UpperCamelCase					:Union[str, Any]        =		jax.device_count()
        __UpperCamelCase					:str        =		num_samples * [prompt]
        __UpperCamelCase					:Tuple        =		pipeline.prepare_inputs(__lowercase)

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

        __UpperCamelCase					:int        =		pipeline(__lowercase			,			__lowercase			,			__lowercase			,			__lowercase			,			jit=__lowercase).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.04_00_39_06)) < 1E-3
            assert np.abs((np.abs(__lowercase			,			dtype=np.floataa).sum() - 2_373_516.75)) < 5E-1







    def       UpperCamelCase__       (     self)	->     Any:
        __UpperCamelCase					:List[str]        =		FlaxDDIMScheduler(
            beta_start=0.0_00_85			,			beta_end=0.0_12			,			beta_schedule='''scaled_linear'''			,			set_alpha_to_one=__lowercase			,			steps_offset=1			,			)

        __UpperCamelCase					:List[str]        =		FlaxStableDiffusionPipeline.from_pretrained(
            '''CompVis/stable-diffusion-v1-4'''			,			revision='''bf16'''			,			dtype=jnp.bfloataa			,			scheduler=__lowercase			,			safety_checker=__lowercase			,			)
        __UpperCamelCase					:Dict        =		scheduler.create_state()

        __UpperCamelCase					:Optional[Any]        =		scheduler_state

        __UpperCamelCase					:List[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[Any]        =		50

        __UpperCamelCase					:Optional[int]        =		jax.device_count()
        __UpperCamelCase					:int        =		num_samples * [prompt]
        __UpperCamelCase					:Dict        =		pipeline.prepare_inputs(__lowercase)

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

        __UpperCamelCase					:Optional[Any]        =		pipeline(__lowercase			,			__lowercase			,			__lowercase			,			__lowercase			,			jit=__lowercase).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_45_04_39_45)) < 1E-3
            assert np.abs((np.abs(__lowercase			,			dtype=np.floataa).sum() - 2_347_693.5)) < 5E-1







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

        __UpperCamelCase					:Any        =		jax.device_count()
        __UpperCamelCase					:Dict        =		num_samples * [prompt]
        __UpperCamelCase					:List[Any]        =		jax.random.split(jax.random.PRNGKey(0)			,			__lowercase)

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

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

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

        __UpperCamelCase					:Optional[int]        =		replicate(__lowercase)
        __UpperCamelCase					:List[str]        =		pipeline.prepare_inputs(__lowercase)
        __UpperCamelCase					:Any        =		shard(__lowercase)
        __UpperCamelCase					:Any        =		pipeline(__lowercase			,			__lowercase			,			__lowercase			,			jit=__lowercase).images
        assert images_eff.shape == (num_samples, 1, 512, 512, 3)
        __UpperCamelCase					:List[str]        =		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 os
import unittest

from transformers.models.phobert.tokenization_phobert import VOCAB_FILES_NAMES, PhobertTokenizer

from ...test_tokenization_common import TokenizerTesterMixin


class      lowerCamelCase_					(       UpperCAmelCase_						,     unittest.TestCase			):






    '''simple docstring'''






    a__     :						List[Any]      =				PhobertTokenizer
    a__     :						Union[str, Any]      =				False







    def       UpperCamelCase__       (     self)	->     Optional[int]:
        super().setUp()

        # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt
        __UpperCamelCase					:Union[str, Any]        =		['''T@@''', '''i''', '''I''', '''R@@''', '''r''', '''e@@''']
        __UpperCamelCase					:int        =		dict(zip(__lowercase			,			range(len(__lowercase))))
        __UpperCamelCase					:Dict        =		['''#version: 0.2''', '''l à</w>''']
        __UpperCamelCase					:Any        =		{'''unk_token''': '''<unk>'''}

        __UpperCamelCase					:int        =		os.path.join(self.tmpdirname			,			VOCAB_FILES_NAMES['''vocab_file'''])
        __UpperCamelCase					:Optional[int]        =		os.path.join(self.tmpdirname			,			VOCAB_FILES_NAMES['''merges_file'''])

        with open(self.vocab_file			,			'''w'''			,			encoding='''utf-8''') as fp:
            for token in vocab_tokens:
                fp.write(f"""{token} {vocab_tokens[token]}\n""")
        with open(self.merges_file			,			'''w'''			,			encoding='''utf-8''') as fp:
            fp.write('''\n'''.join(__lowercase))







    def       UpperCamelCase__       (     self			,			**__lowercase)	->     Optional[Any]:
        kwargs.update(self.special_tokens_map)
        return PhobertTokenizer.from_pretrained(self.tmpdirname			,			**__lowercase)







    def       UpperCamelCase__       (     self			,			__lowercase)	->     int:
        __UpperCamelCase					:List[Any]        =		'''Tôi là VinAI Research'''
        __UpperCamelCase					:List[str]        =		'''T<unk> i <unk> <unk> <unk> <unk> <unk> <unk> I Re<unk> e<unk> <unk> <unk> <unk>'''
        return input_text, output_text







    def       UpperCamelCase__       (     self)	->     Optional[int]:
        __UpperCamelCase					:Dict        =		PhobertTokenizer(self.vocab_file			,			self.merges_file			,			**self.special_tokens_map)
        __UpperCamelCase					:List[Any]        =		'''Tôi là VinAI Research'''
        __UpperCamelCase					:List[str]        =		'''T@@ ô@@ i l@@ à V@@ i@@ n@@ A@@ I R@@ e@@ s@@ e@@ a@@ r@@ c@@ h'''.split()
        __UpperCamelCase					:int        =		tokenizer.tokenize(__lowercase)
        print(__lowercase)
        self.assertListEqual(__lowercase			,			__lowercase)

        __UpperCamelCase					:Dict        =		tokens + [tokenizer.unk_token]

        __UpperCamelCase					:Any        =		[4, 3, 5, 3, 3, 3, 3, 3, 3, 6, 7, 9, 3, 9, 3, 3, 3, 3, 3]
        self.assertListEqual(tokenizer.convert_tokens_to_ids(__lowercase)			,			__lowercase)





