




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

              __UpperCamelCase  		=						[t[-1] for t in os.walk(os.path.join(snake_case ,       os.listdir(snake_case				)[0] ,       '''snapshots'''				)				)]
              __UpperCamelCase  		=						[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       snake_case	(     self					: Dict				):
          __UpperCamelCase		,      __UpperCamelCase  		=						FlaxStableDiffusionPipeline.from_pretrained(
              '''hf-internal-testing/tiny-stable-diffusion-pipe''' ,       safety_checker=snake_case				)

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

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

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

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

          __UpperCamelCase  		=						pipeline(snake_case ,       snake_case ,       snake_case ,       snake_case ,       jit=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_51_47_45				) < 1E-3
              assert np.abs(np.abs(snake_case ,       dtype=np.floataa				).sum() - 4_99_47.8_75				) < 5E-1

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







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

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

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

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

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

          __UpperCamelCase  		=						pipeline(snake_case ,       snake_case ,       snake_case ,       snake_case ,       jit=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.05_65_24_01)				) < 1E-3
              assert np.abs((np.abs(snake_case ,       dtype=np.floataa				).sum() - 2_38_38_08.2)				) < 5E-1







      def       snake_case	(     self					: str				):
          __UpperCamelCase		,      __UpperCamelCase  		=						FlaxStableDiffusionPipeline.from_pretrained(
              '''CompVis/stable-diffusion-v1-4''' ,       revision='''bf16''' ,       dtype=jnp.bfloataa ,       safety_checker=snake_case				)

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

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

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

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

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







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

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

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

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

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

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







      def       snake_case	(     self					: Union[str, Any]				):
          __UpperCamelCase  		=						FlaxDDIMScheduler(
              beta_start=0.0_00_85 ,       beta_end=0.0_12 ,       beta_schedule='''scaled_linear''' ,       set_alpha_to_one=snake_case ,       steps_offset=1 ,       )

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

          __UpperCamelCase  		=						scheduler_state

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

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

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

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

          __UpperCamelCase  		=						pipeline(snake_case ,       snake_case ,       snake_case ,       snake_case ,       jit=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_45_04_39_45)				) < 1E-3
              assert np.abs((np.abs(snake_case ,       dtype=np.floataa				).sum() - 2_34_76_93.5)				) < 5E-1







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

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

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

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

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

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

import numpy as np
import onnxruntime as ort


a_   		=     "1"
a_   		=     "0"
a_   		=     "1"

a_   		=     ort.SessionOptions()
a_   		=     ort.GraphOptimizationLevel.ORT_DISABLE_ALL
print("Create inference session...")
a_   		=     ["TensorrtExecutionProvider", "CUDAExecutionProvider"]
a_   		=     ort.InferenceSession("model.onnx", sess_options=sess_opt, providers=execution_provider)
a_   		=     ort.RunOptions()

a_   		=     128
a_   		=     1
a_   		=     np.ones((batch, sequence), dtype=np.intaa)
a_   		=     np.ones((batch, sequence), dtype=np.intaa)
a_   		=     np.ones((batch, sequence), dtype=np.intaa)

print("Warm up phase...")
sess.run(
    None,
    {
        sess.get_inputs()[0].name: input_ids,
        sess.get_inputs()[1].name: attention_mask,
        sess.get_inputs()[2].name: token_type_ids,
    },
    run_options=run_opt,
)

print("Start inference...")
a_   		=     time.time()
a_   		=     2000
a_   		=     {}
for iter in range(max_iters):
 a_   		=     sess.run(
     None,
     {
         sess.get_inputs()[0].name: input_ids,
         sess.get_inputs()[1].name: attention_mask,
         sess.get_inputs()[2].name: token_type_ids,
     },
     run_options=run_opt,
 )
print("Average Inference Time = {:.3f} ms".format((time.time() - start_time) * 1000 / max_iters))






