






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






      '''simple docstring'''

      def _SCREAMING_SNAKE_CASE (						self     :  List[str]):




          '''simple docstring'''


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

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






      '''simple docstring'''

      def _SCREAMING_SNAKE_CASE (						self     :  Optional[Any]):




          '''simple docstring'''


          SCREAMING_SNAKE_CASE_      :  Union[str, Any]    						=		FlaxStableDiffusionPipeline.from_pretrained(
              '''hf-internal-testing/tiny-stable-diffusion-pipe'''						,	safety_checker=a__)

          SCREAMING_SNAKE_CASE_      :  str    						=		(
              """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_      :  str    						=		jax.random.PRNGKey(0)
          SCREAMING_SNAKE_CASE_      :  Optional[int]    						=		4

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

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

          SCREAMING_SNAKE_CASE_      :  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_51_47_45) < 1e-3
              assert np.abs(np.abs(a__						,	dtype=np.floataa).sum() - 49947.875) < 5e-1

          SCREAMING_SNAKE_CASE_      :  Dict    						=		pipeline.numpy_to_pil(np.asarray(images.reshape((num_samples,) + images.shape[-3:])))
          assert len(a__) == num_samples
      def _SCREAMING_SNAKE_CASE (						self     :  Union[str, Any]):




          '''simple docstring'''


          SCREAMING_SNAKE_CASE_      :  Dict    						=		FlaxStableDiffusionPipeline.from_pretrained(
              '''CompVis/stable-diffusion-v1-4'''						,	revision='''flax'''						,	safety_checker=a__)

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

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

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

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

          SCREAMING_SNAKE_CASE_      :  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.05_65_24_01)) < 1e-3
              assert np.abs((np.abs(a__						,	dtype=np.floataa).sum() - 2383808.2)) < 5e-1
      def _SCREAMING_SNAKE_CASE (						self     :  Tuple):




          '''simple docstring'''


          SCREAMING_SNAKE_CASE_      :  Dict    						=		FlaxStableDiffusionPipeline.from_pretrained(
              '''CompVis/stable-diffusion-v1-4'''						,	revision='''bf16'''						,	dtype=jnp.bfloataa						,	safety_checker=a__)

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

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

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

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

          SCREAMING_SNAKE_CASE_      :  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.04_00_39_06)) < 1e-3
              assert np.abs((np.abs(a__						,	dtype=np.floataa).sum() - 2373516.75)) < 5e-1
      def _SCREAMING_SNAKE_CASE (						self     :  Any):




          '''simple docstring'''


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

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

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

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

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

          SCREAMING_SNAKE_CASE_      :  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.04_00_39_06)) < 1e-3
              assert np.abs((np.abs(a__						,	dtype=np.floataa).sum() - 2373516.75)) < 5e-1
      def _SCREAMING_SNAKE_CASE (						self     :  Tuple):




          '''simple docstring'''


          SCREAMING_SNAKE_CASE_      :  Any    						=		FlaxDDIMScheduler(
              beta_start=0.0_00_85						,	beta_end=0.0_12						,	beta_schedule='''scaled_linear'''						,	set_alpha_to_one=a__						,	steps_offset=1						,	)

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

          SCREAMING_SNAKE_CASE_      :  List[Any]    						=		scheduler_state

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

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

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

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

          SCREAMING_SNAKE_CASE_      :  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_45_04_39_45)) < 1e-3
              assert np.abs((np.abs(a__						,	dtype=np.floataa).sum() - 2347693.5)) < 5e-1



      def _SCREAMING_SNAKE_CASE (						self     :  str):




          '''simple docstring'''


          SCREAMING_SNAKE_CASE_      :  Tuple    						=		(
              """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_      :  Union[str, Any]    						=		jax.device_count()
          SCREAMING_SNAKE_CASE_      :  str    						=		num_samples * [prompt]
          SCREAMING_SNAKE_CASE_      :  Dict    						=		jax.random.split(jax.random.PRNGKey(0)						,	a__)

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

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

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

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




