




'''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     _lowercase     (			self   )							->      Dict:
          with tempfile.TemporaryDirectory() as tmpdirname:
               # pipeline has Flax weights
               _snake_case							=				FlaxDiffusionPipeline.from_pretrained(
                   "hf-internal-testing/tiny-stable-diffusion-pipe" ,safety_checker=_SCREAMING_SNAKE_CASE ,cache_dir=_SCREAMING_SNAKE_CASE   )

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






     def     _lowercase     (			self   )							->      List[str]:
          _snake_case							, _snake_case							=				FlaxStableDiffusionPipeline.from_pretrained(
              "hf-internal-testing/tiny-stable-diffusion-pipe" ,safety_checker=_SCREAMING_SNAKE_CASE   )

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

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

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

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

          _snake_case							=				pipeline(_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,jit=_SCREAMING_SNAKE_CASE   ).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(_SCREAMING_SNAKE_CASE ,dtype=np.floataa   ).sum() - 4_9_9_4_7.8_7_5   ) < 5e-1

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






     def     _lowercase     (			self   )							->      Any:
          _snake_case							, _snake_case							=				FlaxStableDiffusionPipeline.from_pretrained(
              "CompVis/stable-diffusion-v1-4" ,revision="flax" ,safety_checker=_SCREAMING_SNAKE_CASE   )

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

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

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

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

          _snake_case							=				pipeline(_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,jit=_SCREAMING_SNAKE_CASE   ).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(_SCREAMING_SNAKE_CASE ,dtype=np.floataa   ).sum() - 2_3_8_3_8_0_8.2)   ) < 5e-1






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

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

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

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

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

          _snake_case							=				pipeline(_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,jit=_SCREAMING_SNAKE_CASE   ).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(_SCREAMING_SNAKE_CASE ,dtype=np.floataa   ).sum() - 2_3_7_3_5_1_6.7_5)   ) < 5e-1






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

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

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

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

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

          _snake_case							=				pipeline(_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,jit=_SCREAMING_SNAKE_CASE   ).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(_SCREAMING_SNAKE_CASE ,dtype=np.floataa   ).sum() - 2_3_7_3_5_1_6.7_5)   ) < 5e-1






     def     _lowercase     (			self   )							->      Optional[Any]:
          _snake_case							=				FlaxDDIMScheduler(
              beta_start=0.0_0_0_8_5 ,beta_end=0.0_1_2 ,beta_schedule="scaled_linear" ,set_alpha_to_one=_SCREAMING_SNAKE_CASE ,steps_offset=1 ,)

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

          _snake_case							=				scheduler_state

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

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

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

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

          _snake_case							=				pipeline(_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,jit=_SCREAMING_SNAKE_CASE   ).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(_SCREAMING_SNAKE_CASE ,dtype=np.floataa   ).sum() - 2_3_4_7_6_9_3.5)   ) < 5e-1






     def     _lowercase     (			self   )							->      int:
          _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"
          )

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

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

          _snake_case							=				replicate(_SCREAMING_SNAKE_CASE   )
          _snake_case							=				pipeline.prepare_inputs(_SCREAMING_SNAKE_CASE   )
          _snake_case							=				shard(_SCREAMING_SNAKE_CASE   )
          _snake_case							=				pipeline(_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,jit=_SCREAMING_SNAKE_CASE   ).images
          assert images.shape == (num_samples, 1, 512, 512, 3)
          _snake_case							=				images[2, 0, 256, 10:17, 1]

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

          _snake_case							=				replicate(_SCREAMING_SNAKE_CASE   )
          _snake_case							=				pipeline.prepare_inputs(_SCREAMING_SNAKE_CASE   )
          _snake_case							=				shard(_SCREAMING_SNAKE_CASE   )
          _snake_case							=				pipeline(_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,jit=_SCREAMING_SNAKE_CASE   ).images
          assert images_eff.shape == (num_samples, 1, 512, 512, 3)
          _snake_case							=				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'''





from typing import List, Optional, Union

from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding, PaddingStrategy, PreTokenizedInput, TextInput, TruncationStrategy
from ...utils import TensorType

class        _a    (						__lowerCAmelCase							):
     SCREAMING_SNAKE_CASE_							:   Any       =						["""image_processor""", """tokenizer"""]
     SCREAMING_SNAKE_CASE_							:   List[Any]       =						"""BridgeTowerImageProcessor"""
     SCREAMING_SNAKE_CASE_							:   List[Any]       =						("""RobertaTokenizer""", """RobertaTokenizerFast""")






     def __init__(			self ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE   )							->      Tuple:
          super().__init__(_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE   )






     def __call__(			self ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE = None ,_SCREAMING_SNAKE_CASE = True ,_SCREAMING_SNAKE_CASE = False ,_SCREAMING_SNAKE_CASE = None ,_SCREAMING_SNAKE_CASE = None ,_SCREAMING_SNAKE_CASE = 0 ,_SCREAMING_SNAKE_CASE = None ,_SCREAMING_SNAKE_CASE = None ,_SCREAMING_SNAKE_CASE = None ,_SCREAMING_SNAKE_CASE = False ,_SCREAMING_SNAKE_CASE = False ,_SCREAMING_SNAKE_CASE = False ,_SCREAMING_SNAKE_CASE = False ,_SCREAMING_SNAKE_CASE = True ,_SCREAMING_SNAKE_CASE = None ,**_SCREAMING_SNAKE_CASE ,)							->      BatchEncoding:
          _snake_case							=				self.tokenizer(
              text=_SCREAMING_SNAKE_CASE ,add_special_tokens=_SCREAMING_SNAKE_CASE ,padding=_SCREAMING_SNAKE_CASE ,truncation=_SCREAMING_SNAKE_CASE ,max_length=_SCREAMING_SNAKE_CASE ,stride=_SCREAMING_SNAKE_CASE ,pad_to_multiple_of=_SCREAMING_SNAKE_CASE ,return_token_type_ids=_SCREAMING_SNAKE_CASE ,return_attention_mask=_SCREAMING_SNAKE_CASE ,return_overflowing_tokens=_SCREAMING_SNAKE_CASE ,return_special_tokens_mask=_SCREAMING_SNAKE_CASE ,return_offsets_mapping=_SCREAMING_SNAKE_CASE ,return_length=_SCREAMING_SNAKE_CASE ,verbose=_SCREAMING_SNAKE_CASE ,return_tensors=_SCREAMING_SNAKE_CASE ,**_SCREAMING_SNAKE_CASE ,)
          # add pixel_values + pixel_mask
          _snake_case							=				self.image_processor(
              _SCREAMING_SNAKE_CASE ,return_tensors=_SCREAMING_SNAKE_CASE ,do_normalize=_SCREAMING_SNAKE_CASE ,do_center_crop=_SCREAMING_SNAKE_CASE ,**_SCREAMING_SNAKE_CASE   )
          encoding.update(_SCREAMING_SNAKE_CASE   )

          return encoding






     def     _lowercase     (			self ,*_SCREAMING_SNAKE_CASE ,**_SCREAMING_SNAKE_CASE   )							->      List[str]:
          return self.tokenizer.batch_decode(*_SCREAMING_SNAKE_CASE ,**_SCREAMING_SNAKE_CASE   )






     def     _lowercase     (			self ,*_SCREAMING_SNAKE_CASE ,**_SCREAMING_SNAKE_CASE   )							->      List[str]:
          return self.tokenizer.decode(*_SCREAMING_SNAKE_CASE ,**_SCREAMING_SNAKE_CASE   )






     @property
     def     _lowercase     (			self   )							->      Any:
          _snake_case							=				self.tokenizer.model_input_names
          _snake_case							=				self.image_processor.model_input_names
          return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names   )   )


