


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

              snake_case											=							[t[-1] for t in os.walk(os.path.join(__snake_case     ,   os.listdir(__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      ):







    """simple docstring"""

    def 						a_     (			self       ):
         snake_case					,    snake_case											=							FlaxStableDiffusionPipeline.from_pretrained(
             '''hf-internal-testing/tiny-stable-diffusion-pipe'''     ,   safety_checker=__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(__snake_case       )

         # shard inputs and rng
         snake_case											=							replicate(__snake_case       )
         snake_case											=							jax.random.split(__snake_case     ,   __snake_case       )
         snake_case											=							shard(__snake_case       )

         snake_case											=							pipeline(__snake_case     ,   __snake_case     ,   __snake_case     ,   __snake_case     ,   jit=__snake_case       ).images

         assert images.shape == (num_samples, 1, 6_4, 6_4, 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(__snake_case     ,   dtype=np.floataa       ).sum() - 4_9947.875       ) < 5E-1

         snake_case											=							pipeline.numpy_to_pil(np.asarray(images.reshape((num_samples,) + images.shape[-3:]       )       )       )
         assert len(__snake_case       ) == num_samples
    def 						a_     (			self       ):
         snake_case					,    snake_case											=							FlaxStableDiffusionPipeline.from_pretrained(
             '''CompVis/stable-diffusion-v1-4'''     ,   revision='''flax'''     ,   safety_checker=__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											=							5_0

         snake_case											=							jax.device_count()
         snake_case											=							num_samples * [prompt]
         snake_case											=							pipeline.prepare_inputs(__snake_case       )

         # shard inputs and rng
         snake_case											=							replicate(__snake_case       )
         snake_case											=							jax.random.split(__snake_case     ,   __snake_case       )
         snake_case											=							shard(__snake_case       )

         snake_case											=							pipeline(__snake_case     ,   __snake_case     ,   __snake_case     ,   __snake_case     ,   jit=__snake_case       ).images

         assert images.shape == (num_samples, 1, 5_1_2, 5_1_2, 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(__snake_case     ,   dtype=np.floataa       ).sum() - 238_3808.2)       ) < 5E-1
    def 						a_     (			self       ):
         snake_case					,    snake_case											=							FlaxStableDiffusionPipeline.from_pretrained(
             '''CompVis/stable-diffusion-v1-4'''     ,   revision='''bf16'''     ,   dtype=jnp.bfloataa     ,   safety_checker=__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											=							5_0

         snake_case											=							jax.device_count()
         snake_case											=							num_samples * [prompt]
         snake_case											=							pipeline.prepare_inputs(__snake_case       )

         # shard inputs and rng
         snake_case											=							replicate(__snake_case       )
         snake_case											=							jax.random.split(__snake_case     ,   __snake_case       )
         snake_case											=							shard(__snake_case       )

         snake_case											=							pipeline(__snake_case     ,   __snake_case     ,   __snake_case     ,   __snake_case     ,   jit=__snake_case       ).images

         assert images.shape == (num_samples, 1, 5_1_2, 5_1_2, 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(__snake_case     ,   dtype=np.floataa       ).sum() - 237_3516.75)       ) < 5E-1
    def 						a_     (			self       ):
         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											=							5_0

         snake_case											=							jax.device_count()
         snake_case											=							num_samples * [prompt]
         snake_case											=							pipeline.prepare_inputs(__snake_case       )

         # shard inputs and rng
         snake_case											=							replicate(__snake_case       )
         snake_case											=							jax.random.split(__snake_case     ,   __snake_case       )
         snake_case											=							shard(__snake_case       )

         snake_case											=							pipeline(__snake_case     ,   __snake_case     ,   __snake_case     ,   __snake_case     ,   jit=__snake_case       ).images

         assert images.shape == (num_samples, 1, 5_1_2, 5_1_2, 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(__snake_case     ,   dtype=np.floataa       ).sum() - 237_3516.75)       ) < 5E-1
    def 						a_     (			self       ):
         snake_case											=							FlaxDDIMScheduler(
             beta_start=0.0_0085     ,   beta_end=0.012     ,   beta_schedule='''scaled_linear'''     ,   set_alpha_to_one=__snake_case     ,   steps_offset=1     ,   )

         snake_case					,    snake_case											=							FlaxStableDiffusionPipeline.from_pretrained(
             '''CompVis/stable-diffusion-v1-4'''     ,   revision='''bf16'''     ,   dtype=jnp.bfloataa     ,   scheduler=__snake_case     ,   safety_checker=__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											=							5_0

         snake_case											=							jax.device_count()
         snake_case											=							num_samples * [prompt]
         snake_case											=							pipeline.prepare_inputs(__snake_case       )

         # shard inputs and rng
         snake_case											=							replicate(__snake_case       )
         snake_case											=							jax.random.split(__snake_case     ,   __snake_case       )
         snake_case											=							shard(__snake_case       )

         snake_case											=							pipeline(__snake_case     ,   __snake_case     ,   __snake_case     ,   __snake_case     ,   jit=__snake_case       ).images

         assert images.shape == (num_samples, 1, 5_1_2, 5_1_2, 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(__snake_case     ,   dtype=np.floataa       ).sum() - 234_7693.5)       ) < 5E-1




    def 						a_     (			self       ):
         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       )     ,   __snake_case       )

         snake_case					,    snake_case											=							FlaxStableDiffusionPipeline.from_pretrained(
             '''CompVis/stable-diffusion-v1-4'''     ,   revision='''bf16'''     ,   dtype=jnp.bfloataa     ,   safety_checker=__snake_case     ,   )

         snake_case											=							replicate(__snake_case       )
         snake_case											=							pipeline.prepare_inputs(__snake_case       )
         snake_case											=							shard(__snake_case       )
         snake_case											=							pipeline(__snake_case     ,   __snake_case     ,   __snake_case     ,   jit=__snake_case       ).images
         assert images.shape == (num_samples, 1, 5_1_2, 5_1_2, 3)
         snake_case											=							images[2, 0, 2_5_6, 1_0:1_7, 1]

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

         snake_case											=							replicate(__snake_case       )
         snake_case											=							pipeline.prepare_inputs(__snake_case       )
         snake_case											=							shard(__snake_case       )
         snake_case											=							pipeline(__snake_case     ,   __snake_case     ,   __snake_case     ,   jit=__snake_case       ).images
         assert images_eff.shape == (num_samples, 1, 5_1_2, 5_1_2, 3)
         snake_case											=							images[2, 0, 2_5_6, 1_0:1_7, 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 shutil
import tempfile
import unittest

import numpy as np
import pytest

from transformers import is_speech_available, is_vision_available
from transformers.testing_utils import require_torch


if is_vision_available():
 from transformers import TvltImageProcessor

if is_speech_available():
 from transformers import TvltFeatureExtractor

from transformers import TvltProcessor

@require_torch
class     A__   (       unittest.TestCase      ):







    """simple docstring"""

    def 						a_     (			self       ):
         snake_case											=							'''ZinengTang/tvlt-base'''
         snake_case											=							tempfile.mkdtemp()
    def 						a_     (			self     ,   **__snake_case       ):
         return TvltImageProcessor.from_pretrained(self.checkpoint     ,   **__snake_case       )
    def 						a_     (			self     ,   **__snake_case       ):
         return TvltFeatureExtractor.from_pretrained(self.checkpoint     ,   **__snake_case       )
    def 						a_     (			self       ):
         shutil.rmtree(self.tmpdirname       )
    def 						a_     (			self       ):
         snake_case											=							self.get_image_processor()
         snake_case											=							self.get_feature_extractor()

         snake_case											=							TvltProcessor(image_processor=__snake_case     ,   feature_extractor=__snake_case       )
         processor.save_pretrained(self.tmpdirname       )
         snake_case											=							TvltProcessor.from_pretrained(self.tmpdirname       )

         self.assertIsInstance(processor.feature_extractor     ,   __snake_case       )
         self.assertIsInstance(processor.image_processor     ,   __snake_case       )
    def 						a_     (			self       ):
         snake_case											=							self.get_image_processor()
         snake_case											=							self.get_feature_extractor()

         snake_case											=							TvltProcessor(image_processor=__snake_case     ,   feature_extractor=__snake_case       )

         snake_case											=							np.ones([1_2_0_0_0]       )

         snake_case											=							feature_extractor(__snake_case     ,   return_tensors='''np'''       )
         snake_case											=							processor(audio=__snake_case     ,   return_tensors='''np'''       )

         for key in audio_dict.keys():
              self.assertAlmostEqual(audio_dict[key].sum()     ,   input_processor[key].sum()     ,   delta=1E-2       )
    def 						a_     (			self       ):
         snake_case											=							self.get_image_processor()
         snake_case											=							self.get_feature_extractor()

         snake_case											=							TvltProcessor(image_processor=__snake_case     ,   feature_extractor=__snake_case       )

         snake_case											=							np.ones([3, 2_2_4, 2_2_4]       )

         snake_case											=							image_processor(__snake_case     ,   return_tensors='''np'''       )
         snake_case											=							processor(images=__snake_case     ,   return_tensors='''np'''       )

         for key in image_dict.keys():
              self.assertAlmostEqual(image_dict[key].sum()     ,   input_processor[key].sum()     ,   delta=1E-2       )
    def 						a_     (			self       ):
         snake_case											=							self.get_image_processor()
         snake_case											=							self.get_feature_extractor()

         snake_case											=							TvltProcessor(image_processor=__snake_case     ,   feature_extractor=__snake_case       )

         snake_case											=							np.ones([1_2_0_0_0]       )
         snake_case											=							np.ones([3, 2_2_4, 2_2_4]       )

         snake_case											=							processor(audio=__snake_case     ,   images=__snake_case       )

         self.assertListEqual(list(inputs.keys()       )     ,   ['''audio_values''', '''audio_mask''', '''pixel_values''', '''pixel_mask''']       )

         # test if it raises when no input is passed
         with pytest.raises(__snake_case       ):
              processor()




    def 						a_     (			self       ):
         snake_case											=							self.get_image_processor()
         snake_case											=							self.get_feature_extractor()

         snake_case											=							TvltProcessor(image_processor=__snake_case     ,   feature_extractor=__snake_case       )

         self.assertListEqual(
             processor.model_input_names     ,   image_processor.model_input_names + feature_extractor.model_input_names     ,   msg='''`processor` and `image_processor`+`feature_extractor` model input names do not match'''     ,   )




