





'''simple docstring'''




import json
import pathlib
import unittest

import numpy as np

from transformers.testing_utils import require_torch, require_vision, slow
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs


if is_torch_available():
  import torch

if is_vision_available():
  from PIL import Image

  from transformers import DeformableDetrImageProcessor


class  SCREAMING_SNAKE_CASE    (unittest.TestCase							):


     def __init__(						self						,	_UpperCAmelCase						,	_UpperCAmelCase=7						,	_UpperCAmelCase=3						,	_UpperCAmelCase=30						,	_UpperCAmelCase=400						,	_UpperCAmelCase=True						,	_UpperCAmelCase=None						,	_UpperCAmelCase=True						,	_UpperCAmelCase=[0.5, 0.5, 0.5]						,	_UpperCAmelCase=[0.5, 0.5, 0.5]						,	_UpperCAmelCase=True						,	_UpperCAmelCase=1 / 255						,	_UpperCAmelCase=True						,	):

          '''simple docstring'''






          __A						: str      =       size if size is not None else {'shortest_edge': 18, 'longest_edge': 1333}
          __A						: Optional[Any]      =       parent
          __A						: str      =       batch_size
          __A						: Union[str, Any]      =       num_channels
          __A						: Tuple      =       min_resolution
          __A						: Tuple      =       max_resolution
          __A						: str      =       do_resize
          __A						: Tuple      =       size
          __A						: Any      =       do_normalize
          __A						: Dict      =       image_mean
          __A						: Dict      =       image_std
          __A						: int      =       do_rescale
          __A						: Optional[int]      =       rescale_factor
          __A						: List[Any]      =       do_pad


     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






          return {
              "do_resize": self.do_resize,
              "size": self.size,
              "do_normalize": self.do_normalize,
              "image_mean": self.image_mean,
              "image_std": self.image_std,
              "do_rescale": self.do_rescale,
              "rescale_factor": self.rescale_factor,
              "do_pad": self.do_pad,
          }


     def 							SCREAMING_SNAKE_CASE  (						self						,	_UpperCAmelCase						,	_UpperCAmelCase=False):

          '''simple docstring'''






          if not batched:
               __A						: Any      =       image_inputs[0]
               if isinstance(_UpperCAmelCase						,	Image.Image):
                    __A ,__A						: Any      =       image.size
               else:
                    __A ,__A						: Optional[int]      =       image.shape[1], image.shape[2]
               if w < h:
                    __A						: Tuple      =       int(self.size['shortest_edge'] * h / w)
                    __A						: Optional[Any]      =       self.size['shortest_edge']
               elif w > h:
                    __A						: Tuple      =       self.size['shortest_edge']
                    __A						: List[str]      =       int(self.size['shortest_edge'] * w / h)
               else:
                    __A						: Optional[int]      =       self.size['shortest_edge']
                    __A						: int      =       self.size['shortest_edge']

          else:
               __A						: List[Any]      =       []
               for image in image_inputs:
                    __A ,__A						: Union[str, Any]      =       self.get_expected_values([image])
                    expected_values.append((expected_height, expected_width))
               __A						: Dict      =       max(_UpperCAmelCase						,	key=lambda _UpperCAmelCase: item[0])[0]
               __A						: List[Any]      =       max(_UpperCAmelCase						,	key=lambda _UpperCAmelCase: item[1])[1]

          return expected_height, expected_width




@require_torch
@require_vision
class  SCREAMING_SNAKE_CASE    (a__					,		unittest.TestCase							):
     lowerCAmelCase					=       DeformableDetrImageProcessor if is_vision_available() else None


     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






          __A						: Optional[Any]      =       DeformableDetrImageProcessingTester(self)


     @property
     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






          return self.image_processor_tester.prepare_image_processor_dict()


     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






          __A						: List[str]      =       self.image_processing_class(**self.image_processor_dict)
          self.assertTrue(hasattr(_UpperCAmelCase						,	'image_mean'))
          self.assertTrue(hasattr(_UpperCAmelCase						,	'image_std'))
          self.assertTrue(hasattr(_UpperCAmelCase						,	'do_normalize'))
          self.assertTrue(hasattr(_UpperCAmelCase						,	'do_resize'))
          self.assertTrue(hasattr(_UpperCAmelCase						,	'do_rescale'))
          self.assertTrue(hasattr(_UpperCAmelCase						,	'do_pad'))
          self.assertTrue(hasattr(_UpperCAmelCase						,	'size'))


     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






          __A						: Dict      =       self.image_processing_class.from_dict(self.image_processor_dict)
          self.assertEqual(image_processor.size						,	{'shortest_edge': 18, 'longest_edge': 1333})
          self.assertEqual(image_processor.do_pad						,	_UpperCAmelCase)

          __A						: List[str]      =       self.image_processing_class.from_dict(
              self.image_processor_dict						,	size=42						,	max_size=84						,	pad_and_return_pixel_mask=_UpperCAmelCase)
          self.assertEqual(image_processor.size						,	{'shortest_edge': 42, 'longest_edge': 84})
          self.assertEqual(image_processor.do_pad						,	_UpperCAmelCase)


     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






          pass


     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






          __A						: Union[str, Any]      =       self.image_processing_class(**self.image_processor_dict)
          # create random PIL images
          __A						: str      =       prepare_image_inputs(self.image_processor_tester						,	equal_resolution=_UpperCAmelCase)
          for image in image_inputs:
               self.assertIsInstance(_UpperCAmelCase						,	Image.Image)

          # Test not batched input
          __A						: List[Any]      =       image_processing(image_inputs[0]						,	return_tensors='pt').pixel_values

          __A ,__A						: Union[str, Any]      =       self.image_processor_tester.get_expected_values(_UpperCAmelCase)

          self.assertEqual(
              encoded_images.shape						,	(1, self.image_processor_tester.num_channels, expected_height, expected_width)						,	)

          # Test batched
          __A ,__A						: Tuple      =       self.image_processor_tester.get_expected_values(_UpperCAmelCase						,	batched=_UpperCAmelCase)

          __A						: Union[str, Any]      =       image_processing(_UpperCAmelCase						,	return_tensors='pt').pixel_values
          self.assertEqual(
              encoded_images.shape						,	(
                  self.image_processor_tester.batch_size,
                  self.image_processor_tester.num_channels,
                  expected_height,
                  expected_width,
              )						,	)


     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






          __A						: int      =       self.image_processing_class(**self.image_processor_dict)
          # create random numpy tensors
          __A						: Dict      =       prepare_image_inputs(self.image_processor_tester						,	equal_resolution=_UpperCAmelCase						,	numpify=_UpperCAmelCase)
          for image in image_inputs:
               self.assertIsInstance(_UpperCAmelCase						,	np.ndarray)

          # Test not batched input
          __A						: Optional[int]      =       image_processing(image_inputs[0]						,	return_tensors='pt').pixel_values

          __A ,__A						: Optional[Any]      =       self.image_processor_tester.get_expected_values(_UpperCAmelCase)

          self.assertEqual(
              encoded_images.shape						,	(1, self.image_processor_tester.num_channels, expected_height, expected_width)						,	)

          # Test batched
          __A						: int      =       image_processing(_UpperCAmelCase						,	return_tensors='pt').pixel_values

          __A ,__A						: Union[str, Any]      =       self.image_processor_tester.get_expected_values(_UpperCAmelCase						,	batched=_UpperCAmelCase)

          self.assertEqual(
              encoded_images.shape						,	(
                  self.image_processor_tester.batch_size,
                  self.image_processor_tester.num_channels,
                  expected_height,
                  expected_width,
              )						,	)


     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






          __A						: List[Any]      =       self.image_processing_class(**self.image_processor_dict)
          # create random PyTorch tensors
          __A						: str      =       prepare_image_inputs(self.image_processor_tester						,	equal_resolution=_UpperCAmelCase						,	torchify=_UpperCAmelCase)
          for image in image_inputs:
               self.assertIsInstance(_UpperCAmelCase						,	torch.Tensor)

          # Test not batched input
          __A						: List[Any]      =       image_processing(image_inputs[0]						,	return_tensors='pt').pixel_values

          __A ,__A						: List[Any]      =       self.image_processor_tester.get_expected_values(_UpperCAmelCase)

          self.assertEqual(
              encoded_images.shape						,	(1, self.image_processor_tester.num_channels, expected_height, expected_width)						,	)

          # Test batched
          __A						: Dict      =       image_processing(_UpperCAmelCase						,	return_tensors='pt').pixel_values

          __A ,__A						: Optional[int]      =       self.image_processor_tester.get_expected_values(_UpperCAmelCase						,	batched=_UpperCAmelCase)

          self.assertEqual(
              encoded_images.shape						,	(
                  self.image_processor_tester.batch_size,
                  self.image_processor_tester.num_channels,
                  expected_height,
                  expected_width,
              )						,	)


     @slow
     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






          __A						: List[str]      =       Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png')
          with open('./tests/fixtures/tests_samples/COCO/coco_annotations.txt'						,	'r') as f:
               __A						: int      =       json.loads(f.read())

          __A						: Optional[int]      =       {'image_id': 3_9769, 'annotations': target}

          # encode them
          __A						: int      =       DeformableDetrImageProcessor()
          __A						: List[str]      =       image_processing(images=_UpperCAmelCase						,	annotations=_UpperCAmelCase						,	return_tensors='pt')

          # verify pixel values
          __A						: Optional[int]      =       torch.Size([1, 3, 800, 1066])
          self.assertEqual(encoding['pixel_values'].shape						,	_UpperCAmelCase)

          __A						: Dict      =       torch.tensor([0.2796, 0.3138, 0.3481])
          self.assertTrue(torch.allclose(encoding['pixel_values'][0, 0, 0, :3]						,	_UpperCAmelCase						,	atol=1e-4))

          # verify area
          __A						: int      =       torch.tensor([5887.9600, 11250.2061, 489353.8438, 837122.7500, 147967.5156, 165732.3438])
          self.assertTrue(torch.allclose(encoding['labels'][0]['area']						,	_UpperCAmelCase))
          # verify boxes
          __A						: Any      =       torch.Size([6, 4])
          self.assertEqual(encoding['labels'][0]['boxes'].shape						,	_UpperCAmelCase)
          __A						: Dict      =       torch.tensor([0.5503, 0.2765, 0.0604, 0.2215])
          self.assertTrue(torch.allclose(encoding['labels'][0]['boxes'][0]						,	_UpperCAmelCase						,	atol=1e-3))
          # verify image_id
          __A						: Optional[Any]      =       torch.tensor([3_9769])
          self.assertTrue(torch.allclose(encoding['labels'][0]['image_id']						,	_UpperCAmelCase))
          # verify is_crowd
          __A						: Optional[int]      =       torch.tensor([0, 0, 0, 0, 0, 0])
          self.assertTrue(torch.allclose(encoding['labels'][0]['iscrowd']						,	_UpperCAmelCase))
          # verify class_labels
          __A						: Tuple      =       torch.tensor([75, 75, 63, 65, 17, 17])
          self.assertTrue(torch.allclose(encoding['labels'][0]['class_labels']						,	_UpperCAmelCase))
          # verify orig_size
          __A						: int      =       torch.tensor([480, 640])
          self.assertTrue(torch.allclose(encoding['labels'][0]['orig_size']						,	_UpperCAmelCase))
          # verify size
          __A						: Optional[int]      =       torch.tensor([800, 1066])
          self.assertTrue(torch.allclose(encoding['labels'][0]['size']						,	_UpperCAmelCase))


     @slow
     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






          __A						: Optional[int]      =       Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png')
          with open('./tests/fixtures/tests_samples/COCO/coco_panoptic_annotations.txt'						,	'r') as f:
               __A						: Dict      =       json.loads(f.read())

          __A						: int      =       {'file_name': '000000039769.png', 'image_id': 3_9769, 'segments_info': target}

          __A						: Optional[Any]      =       pathlib.Path('./tests/fixtures/tests_samples/COCO/coco_panoptic')

          # encode them
          __A						: Union[str, Any]      =       DeformableDetrImageProcessor(format='coco_panoptic')
          __A						: str      =       image_processing(images=_UpperCAmelCase						,	annotations=_UpperCAmelCase						,	masks_path=_UpperCAmelCase						,	return_tensors='pt')

          # verify pixel values
          __A						: Union[str, Any]      =       torch.Size([1, 3, 800, 1066])
          self.assertEqual(encoding['pixel_values'].shape						,	_UpperCAmelCase)

          __A						: Dict      =       torch.tensor([0.2796, 0.3138, 0.3481])
          self.assertTrue(torch.allclose(encoding['pixel_values'][0, 0, 0, :3]						,	_UpperCAmelCase						,	atol=1e-4))

          # verify area
          __A						: Tuple      =       torch.tensor([147979.6875, 165527.0469, 484638.5938, 11292.9375, 5879.6562, 7634.1147])
          self.assertTrue(torch.allclose(encoding['labels'][0]['area']						,	_UpperCAmelCase))
          # verify boxes
          __A						: Tuple      =       torch.Size([6, 4])
          self.assertEqual(encoding['labels'][0]['boxes'].shape						,	_UpperCAmelCase)
          __A						: Dict      =       torch.tensor([0.2625, 0.5437, 0.4688, 0.8625])
          self.assertTrue(torch.allclose(encoding['labels'][0]['boxes'][0]						,	_UpperCAmelCase						,	atol=1e-3))
          # verify image_id
          __A						: Union[str, Any]      =       torch.tensor([3_9769])
          self.assertTrue(torch.allclose(encoding['labels'][0]['image_id']						,	_UpperCAmelCase))
          # verify is_crowd
          __A						: Dict      =       torch.tensor([0, 0, 0, 0, 0, 0])
          self.assertTrue(torch.allclose(encoding['labels'][0]['iscrowd']						,	_UpperCAmelCase))
          # verify class_labels
          __A						: List[str]      =       torch.tensor([17, 17, 63, 75, 75, 93])
          self.assertTrue(torch.allclose(encoding['labels'][0]['class_labels']						,	_UpperCAmelCase))
          # verify masks
          __A						: Optional[int]      =       82_2873
          self.assertEqual(encoding['labels'][0]['masks'].sum().item()						,	_UpperCAmelCase)
          # verify orig_size
          __A						: Any      =       torch.tensor([480, 640])
          self.assertTrue(torch.allclose(encoding['labels'][0]['orig_size']						,	_UpperCAmelCase))
          # verify size
          __A						: str      =       torch.tensor([800, 1066])
          self.assertTrue(torch.allclose(encoding['labels'][0]['size']						,	_UpperCAmelCase))






'''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  SCREAMING_SNAKE_CASE    (unittest.TestCase							):


     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






          with tempfile.TemporaryDirectory() as tmpdirname:
               # pipeline has Flax weights
               __A						: Union[str, Any]      =       FlaxDiffusionPipeline.from_pretrained(
                   'hf-internal-testing/tiny-stable-diffusion-pipe'						,	safety_checker=_UpperCAmelCase						,	cache_dir=_UpperCAmelCase)

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


     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






          __A ,__A						: Optional[int]      =       FlaxStableDiffusionPipeline.from_pretrained(
              'hf-internal-testing/tiny-stable-diffusion-pipe'						,	safety_checker=_UpperCAmelCase)

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

          __A						: Optional[Any]      =       jax.random.PRNGKey(0)
          __A						: int      =       4

          __A						: Tuple      =       jax.device_count()
          __A						: Union[str, Any]      =       num_samples * [prompt]
          __A						: Tuple      =       pipeline.prepare_inputs(_UpperCAmelCase)

          # shard inputs and rng
          __A						: str      =       replicate(_UpperCAmelCase)
          __A						: Tuple      =       jax.random.split(_UpperCAmelCase						,	_UpperCAmelCase)
          __A						: Union[str, Any]      =       shard(_UpperCAmelCase)

          __A						: Union[str, Any]      =       pipeline(_UpperCAmelCase						,	_UpperCAmelCase						,	_UpperCAmelCase						,	_UpperCAmelCase						,	jit=_UpperCAmelCase).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.1514745) < 1e-3
               assert np.abs(np.abs(_UpperCAmelCase						,	dtype=np.floataa).sum() - 49947.875) < 5e-1

          __A						: List[str]      =       pipeline.numpy_to_pil(np.asarray(images.reshape((num_samples,) + images.shape[-3:])))
          assert len(_UpperCAmelCase) == num_samples


     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






          __A ,__A						: str      =       FlaxStableDiffusionPipeline.from_pretrained(
              'CompVis/stable-diffusion-v1-4'						,	revision='flax'						,	safety_checker=_UpperCAmelCase)

          __A						: 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'
          )

          __A						: Tuple      =       jax.random.PRNGKey(0)
          __A						: Any      =       50

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

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

          __A						: Tuple      =       pipeline(_UpperCAmelCase						,	_UpperCAmelCase						,	_UpperCAmelCase						,	_UpperCAmelCase						,	jit=_UpperCAmelCase).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.05652401)) < 1e-3
               assert np.abs((np.abs(_UpperCAmelCase						,	dtype=np.floataa).sum() - 2383808.2)) < 5e-1


     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






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

          __A						: 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'
          )

          __A						: str      =       jax.random.PRNGKey(0)
          __A						: Any      =       50

          __A						: Optional[int]      =       jax.device_count()
          __A						: int      =       num_samples * [prompt]
          __A						: Optional[int]      =       pipeline.prepare_inputs(_UpperCAmelCase)

          # shard inputs and rng
          __A						: Optional[int]      =       replicate(_UpperCAmelCase)
          __A						: List[str]      =       jax.random.split(_UpperCAmelCase						,	_UpperCAmelCase)
          __A						: Dict      =       shard(_UpperCAmelCase)

          __A						: str      =       pipeline(_UpperCAmelCase						,	_UpperCAmelCase						,	_UpperCAmelCase						,	_UpperCAmelCase						,	jit=_UpperCAmelCase).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.04003906)) < 1e-3
               assert np.abs((np.abs(_UpperCAmelCase						,	dtype=np.floataa).sum() - 2373516.75)) < 5e-1


     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






          __A ,__A						: Optional[Any]      =       FlaxStableDiffusionPipeline.from_pretrained(
              'CompVis/stable-diffusion-v1-4'						,	revision='bf16'						,	dtype=jnp.bfloataa)

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

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

          __A						: Optional[int]      =       jax.device_count()
          __A						: List[Any]      =       num_samples * [prompt]
          __A						: List[Any]      =       pipeline.prepare_inputs(_UpperCAmelCase)

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

          __A						: int      =       pipeline(_UpperCAmelCase						,	_UpperCAmelCase						,	_UpperCAmelCase						,	_UpperCAmelCase						,	jit=_UpperCAmelCase).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.04003906)) < 1e-3
               assert np.abs((np.abs(_UpperCAmelCase						,	dtype=np.floataa).sum() - 2373516.75)) < 5e-1


     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






          __A						: List[Any]      =       FlaxDDIMScheduler(
              beta_start=0.00085						,	beta_end=0.012						,	beta_schedule='scaled_linear'						,	set_alpha_to_one=_UpperCAmelCase						,	steps_offset=1						,	)

          __A ,__A						: Any      =       FlaxStableDiffusionPipeline.from_pretrained(
              'CompVis/stable-diffusion-v1-4'						,	revision='bf16'						,	dtype=jnp.bfloataa						,	scheduler=_UpperCAmelCase						,	safety_checker=_UpperCAmelCase						,	)
          __A						: Optional[Any]      =       scheduler.create_state()

          __A						: Any      =       scheduler_state

          __A						: 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'
          )

          __A						: Union[str, Any]      =       jax.random.PRNGKey(0)
          __A						: Optional[int]      =       50

          __A						: Optional[Any]      =       jax.device_count()
          __A						: Any      =       num_samples * [prompt]
          __A						: Optional[Any]      =       pipeline.prepare_inputs(_UpperCAmelCase)

          # shard inputs and rng
          __A						: int      =       replicate(_UpperCAmelCase)
          __A						: Any      =       jax.random.split(_UpperCAmelCase						,	_UpperCAmelCase)
          __A						: Tuple      =       shard(_UpperCAmelCase)

          __A						: Union[str, Any]      =       pipeline(_UpperCAmelCase						,	_UpperCAmelCase						,	_UpperCAmelCase						,	_UpperCAmelCase						,	jit=_UpperCAmelCase).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.045043945)) < 1e-3
               assert np.abs((np.abs(_UpperCAmelCase						,	dtype=np.floataa).sum() - 2347693.5)) < 5e-1


     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






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

          __A						: int      =       jax.device_count()
          __A						: List[Any]      =       num_samples * [prompt]
          __A						: List[Any]      =       jax.random.split(jax.random.PRNGKey(0)						,	_UpperCAmelCase)

          __A ,__A						: Tuple      =       FlaxStableDiffusionPipeline.from_pretrained(
              'CompVis/stable-diffusion-v1-4'						,	revision='bf16'						,	dtype=jnp.bfloataa						,	safety_checker=_UpperCAmelCase						,	)

          __A						: str      =       replicate(_UpperCAmelCase)
          __A						: str      =       pipeline.prepare_inputs(_UpperCAmelCase)
          __A						: str      =       shard(_UpperCAmelCase)
          __A						: int      =       pipeline(_UpperCAmelCase						,	_UpperCAmelCase						,	_UpperCAmelCase						,	jit=_UpperCAmelCase).images
          assert images.shape == (num_samples, 1, 512, 512, 3)
          __A						: Any      =       images[2, 0, 256, 10:17, 1]

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

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