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






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

         _lowerCAmelCase						:       Optional[int]  			=     [t[-1] for t in os.walk(os.path.join(a__				,     os.listdir(a__  )[0]				,     """snapshots"""  )  )]
         _lowerCAmelCase						:       Tuple  			=     [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		):






     def     __A  (		self  ):
       _lowerCAmelCase			,	_lowerCAmelCase						:       Union[str, Any]  			=     FlaxStableDiffusionPipeline.from_pretrained(
           """hf-internal-testing/tiny-stable-diffusion-pipe"""				,     safety_checker=a__  )

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

       _lowerCAmelCase						:       str  			=     jax.random.PRNGKey(0  )
       _lowerCAmelCase						:       Optional[int]  			=     4

       _lowerCAmelCase						:       Any  			=     jax.device_count()
       _lowerCAmelCase						:       Any  			=     num_samples * [prompt]
       _lowerCAmelCase						:       List[str]  			=     pipeline.prepare_inputs(a__  )

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

       _lowerCAmelCase						:       Any  			=     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.1_5_1_4_7_4_5  ) < 1e-3
         assert np.abs(np.abs(a__				,     dtype=np.floataa  ).sum() - 4_9_9_4_7.8_7_5  ) < 5e-1

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






     def     __A  (		self  ):
       _lowerCAmelCase			,	_lowerCAmelCase						:       Dict  			=     FlaxStableDiffusionPipeline.from_pretrained(
           """CompVis/stable-diffusion-v1-4"""				,     revision="""flax"""				,     safety_checker=a__  )

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

       _lowerCAmelCase						:       Tuple  			=     jax.random.PRNGKey(0  )
       _lowerCAmelCase						:       Optional[int]  			=     50

       _lowerCAmelCase						:       List[Any]  			=     jax.device_count()
       _lowerCAmelCase						:       List[Any]  			=     num_samples * [prompt]
       _lowerCAmelCase						:       Union[str, Any]  			=     pipeline.prepare_inputs(a__  )

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

       _lowerCAmelCase						:       Dict  			=     pipeline(a__				,     a__				,     a__				,     a__				,     jit=a__  ).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_5_6_5_2_4_0_1)  ) < 1e-3
         assert np.abs((np.abs(a__				,     dtype=np.floataa  ).sum() - 2_3_8_3_8_0_8.2)  ) < 5e-1






     def     __A  (		self  ):
       _lowerCAmelCase			,	_lowerCAmelCase						:       Dict  			=     FlaxStableDiffusionPipeline.from_pretrained(
           """CompVis/stable-diffusion-v1-4"""				,     revision="""bf16"""				,     dtype=jnp.bfloataa				,     safety_checker=a__  )

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

       _lowerCAmelCase						:       str  			=     jax.random.PRNGKey(0  )
       _lowerCAmelCase						:       List[Any]  			=     50

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

       # shard inputs and rng
       _lowerCAmelCase						:       Tuple  			=     replicate(a__  )
       _lowerCAmelCase						:       Any  			=     jax.random.split(a__				,     a__  )
       _lowerCAmelCase						:       str  			=     shard(a__  )

       _lowerCAmelCase						:       str  			=     pipeline(a__				,     a__				,     a__				,     a__				,     jit=a__  ).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_4_0_0_3_9_0_6)  ) < 1e-3
         assert np.abs((np.abs(a__				,     dtype=np.floataa  ).sum() - 2_3_7_3_5_1_6.7_5)  ) < 5e-1






     def     __A  (		self  ):
       _lowerCAmelCase			,	_lowerCAmelCase						:       List[str]  			=     FlaxStableDiffusionPipeline.from_pretrained(
           """CompVis/stable-diffusion-v1-4"""				,     revision="""bf16"""				,     dtype=jnp.bfloataa  )

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

       _lowerCAmelCase						:       str  			=     jax.random.PRNGKey(0  )
       _lowerCAmelCase						:       Optional[int]  			=     50

       _lowerCAmelCase						:       Dict  			=     jax.device_count()
       _lowerCAmelCase						:       int  			=     num_samples * [prompt]
       _lowerCAmelCase						:       List[str]  			=     pipeline.prepare_inputs(a__  )

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

       _lowerCAmelCase						:       Optional[int]  			=     pipeline(a__				,     a__				,     a__				,     a__				,     jit=a__  ).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_4_0_0_3_9_0_6)  ) < 1e-3
         assert np.abs((np.abs(a__				,     dtype=np.floataa  ).sum() - 2_3_7_3_5_1_6.7_5)  ) < 5e-1






     def     __A  (		self  ):
       _lowerCAmelCase						:       Any  			=     FlaxDDIMScheduler(
           beta_start=0.0_0_0_8_5				,     beta_end=0.0_1_2				,     beta_schedule="""scaled_linear"""				,     set_alpha_to_one=a__				,     steps_offset=1				,     )

       _lowerCAmelCase			,	_lowerCAmelCase						:       Optional[int]  			=     FlaxStableDiffusionPipeline.from_pretrained(
           """CompVis/stable-diffusion-v1-4"""				,     revision="""bf16"""				,     dtype=jnp.bfloataa				,     scheduler=a__				,     safety_checker=a__				,     )
       _lowerCAmelCase						:       List[str]  			=     scheduler.create_state()

       _lowerCAmelCase						:       List[Any]  			=     scheduler_state

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

       _lowerCAmelCase						:       str  			=     jax.random.PRNGKey(0  )
       _lowerCAmelCase						:       List[Any]  			=     50

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

       # shard inputs and rng
       _lowerCAmelCase						:       Tuple  			=     replicate(a__  )
       _lowerCAmelCase						:       List[Any]  			=     jax.random.split(a__				,     a__  )
       _lowerCAmelCase						:       Tuple  			=     shard(a__  )

       _lowerCAmelCase						:       Optional[Any]  			=     pipeline(a__				,     a__				,     a__				,     a__				,     jit=a__  ).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_4_5_0_4_3_9_4_5)  ) < 1e-3
         assert np.abs((np.abs(a__				,     dtype=np.floataa  ).sum() - 2_3_4_7_6_9_3.5)  ) < 5e-1






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

       _lowerCAmelCase						:       Union[str, Any]  			=     jax.device_count()
       _lowerCAmelCase						:       str  			=     num_samples * [prompt]
       _lowerCAmelCase						:       Dict  			=     jax.random.split(jax.random.PRNGKey(0  )				,     a__  )

       _lowerCAmelCase			,	_lowerCAmelCase						:       Any  			=     FlaxStableDiffusionPipeline.from_pretrained(
           """CompVis/stable-diffusion-v1-4"""				,     revision="""bf16"""				,     dtype=jnp.bfloataa				,     safety_checker=a__				,     )

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

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

       _lowerCAmelCase						:       Any  			=     replicate(a__  )
       _lowerCAmelCase						:       Tuple  			=     pipeline.prepare_inputs(a__  )
       _lowerCAmelCase						:       List[str]  			=     shard(a__  )
       _lowerCAmelCase						:       List[Any]  			=     pipeline(a__				,     a__				,     a__				,     jit=a__  ).images
       assert images_eff.shape == (num_samples, 1, 512, 512, 3)
       _lowerCAmelCase						:       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





"""simple docstring"""




import json
from typing import List, Optional, Tuple

from tokenizers import normalizers

from ...tokenization_utils_fast import PreTrainedTokenizerFast
from .tokenization_electra import ElectraTokenizer


_a      :		List[Any]           =   {'vocab_file': 'vocab.txt', 'tokenizer_file': 'tokenizer.json'}

_a      :		Union[str, Any]           =   {
    'vocab_file': {
        'google/electra-small-generator': (
            'https://huggingface.co/google/electra-small-generator/resolve/main/vocab.txt'
        ),
        'google/electra-base-generator': 'https://huggingface.co/google/electra-base-generator/resolve/main/vocab.txt',
        'google/electra-large-generator': (
            'https://huggingface.co/google/electra-large-generator/resolve/main/vocab.txt'
        ),
        'google/electra-small-discriminator': (
            'https://huggingface.co/google/electra-small-discriminator/resolve/main/vocab.txt'
        ),
        'google/electra-base-discriminator': (
            'https://huggingface.co/google/electra-base-discriminator/resolve/main/vocab.txt'
        ),
        'google/electra-large-discriminator': (
            'https://huggingface.co/google/electra-large-discriminator/resolve/main/vocab.txt'
        ),
    },
    'tokenizer_file': {
        'google/electra-small-generator': (
            'https://huggingface.co/google/electra-small-generator/resolve/main/tokenizer.json'
        ),
        'google/electra-base-generator': (
            'https://huggingface.co/google/electra-base-generator/resolve/main/tokenizer.json'
        ),
        'google/electra-large-generator': (
            'https://huggingface.co/google/electra-large-generator/resolve/main/tokenizer.json'
        ),
        'google/electra-small-discriminator': (
            'https://huggingface.co/google/electra-small-discriminator/resolve/main/tokenizer.json'
        ),
        'google/electra-base-discriminator': (
            'https://huggingface.co/google/electra-base-discriminator/resolve/main/tokenizer.json'
        ),
        'google/electra-large-discriminator': (
            'https://huggingface.co/google/electra-large-discriminator/resolve/main/tokenizer.json'
        ),
    },
}

_a      :		Optional[Any]           =   {
    'google/electra-small-generator': 512,
    'google/electra-base-generator': 512,
    'google/electra-large-generator': 512,
    'google/electra-small-discriminator': 512,
    'google/electra-base-discriminator': 512,
    'google/electra-large-discriminator': 512,
}

_a      :		Any           =   {
    'google/electra-small-generator': {'do_lower_case': True},
    'google/electra-base-generator': {'do_lower_case': True},
    'google/electra-large-generator': {'do_lower_case': True},
    'google/electra-small-discriminator': {'do_lower_case': True},
    'google/electra-base-discriminator': {'do_lower_case': True},
    'google/electra-large-discriminator': {'do_lower_case': True},
}
class 						__A							( SCREAMING_SNAKE_CASE_		):
     _UpperCamelCase							:			Tuple							=		VOCAB_FILES_NAMES
     _UpperCamelCase							:			List[Any]							=		PRETRAINED_VOCAB_FILES_MAP
     _UpperCamelCase							:			List[Any]							=		PRETRAINED_INIT_CONFIGURATION
     _UpperCamelCase							:			Union[str, Any]							=		PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
     _UpperCamelCase							:			Optional[Any]							=		ElectraTokenizer






     def __init__(		self				,     a__=None				,     a__=None				,     a__=True				,     a__="[UNK]"				,     a__="[SEP]"				,     a__="[PAD]"				,     a__="[CLS]"				,     a__="[MASK]"				,     a__=True				,     a__=None				,     **a__				,     ):
       super().__init__(
           a__				,     tokenizer_file=a__				,     do_lower_case=a__				,     unk_token=a__				,     sep_token=a__				,     pad_token=a__				,     cls_token=a__				,     mask_token=a__				,     tokenize_chinese_chars=a__				,     strip_accents=a__				,     **a__				,     )

       _lowerCAmelCase						:       int  			=     json.loads(self.backend_tokenizer.normalizer.__getstate__()  )
       if (
           normalizer_state.get("""lowercase"""				,     a__  ) != do_lower_case
           or normalizer_state.get("""strip_accents"""				,     a__  ) != strip_accents
           or normalizer_state.get("""handle_chinese_chars"""				,     a__  ) != tokenize_chinese_chars
       ):
         _lowerCAmelCase						:       Dict  			=     getattr(a__				,     normalizer_state.pop("""type"""  )  )
         _lowerCAmelCase						:       int  			=     do_lower_case
         _lowerCAmelCase						:       str  			=     strip_accents
         _lowerCAmelCase						:       Dict  			=     tokenize_chinese_chars
         _lowerCAmelCase						:       str  			=     normalizer_class(**a__  )

       _lowerCAmelCase						:       List[str]  			=     do_lower_case






     def     __A  (		self				,     a__				,     a__=None  ):
       _lowerCAmelCase						:       int  			=     [self.cls_token_id] + token_ids_a + [self.sep_token_id]

       if token_ids_a:
         output += token_ids_a + [self.sep_token_id]

       return output






     def     __A  (		self				,     a__				,     a__ = None  ):
       _lowerCAmelCase						:       List[str]  			=     [self.sep_token_id]
       _lowerCAmelCase						:       Any  			=     [self.cls_token_id]
       if token_ids_a is None:
         return len(cls + token_ids_a + sep  ) * [0]
       return len(cls + token_ids_a + sep  ) * [0] + len(token_ids_a + sep  ) * [1]






     def     __A  (		self				,     a__				,     a__ = None  ):
       _lowerCAmelCase						:       Optional[Any]  			=     self._tokenizer.model.save(a__				,     name=a__  )
       return tuple(a__  )




