






from collections import deque
from math import floor
from random import random
from time import time





class __lowerCamelCase :





	"""simple docstring"""






	def __init__(							self    ):
							"""simple docstring"""







							_UpperCAmelCase									=			{}

	def        UpperCamelCase    (							self       ,    UpperCAmelCase       ,    UpperCAmelCase       ,    UpperCAmelCase=1    ):
							"""simple docstring"""







							if self.graph.get(UpperCAmelCase    ):
													if self.graph[u].count([w, v]    ) == 0:
																			self.graph[u].append([w, v]    )
							else:
													_UpperCAmelCase									=			[[w, v]]
							if not self.graph.get(UpperCAmelCase    ):
													_UpperCAmelCase									=			[]

	def        UpperCamelCase    (							self    ):
							"""simple docstring"""







							return list(self.graph    )

	def        UpperCamelCase    (							self       ,    UpperCAmelCase       ,    UpperCAmelCase    ):
							"""simple docstring"""







							if self.graph.get(UpperCAmelCase    ):
													for _ in self.graph[u]:
																			if _[1] == v:
																									self.graph[u].remove(UpperCAmelCase    )

	def        UpperCamelCase    (							self       ,    UpperCAmelCase=-2       ,    UpperCAmelCase=-1    ):
							"""simple docstring"""







							if s == d:
													return []
							_UpperCAmelCase									=			[]
							_UpperCAmelCase									=			[]
							if s == -2:
													_UpperCAmelCase									=			list(self.graph    )[0]
							stack.append(UpperCAmelCase    )
							visited.append(UpperCAmelCase    )
							_UpperCAmelCase									=			s

							while True:
													# check if there is any non isolated nodes
													if len(self.graph[s]    ) != 0:
																			_UpperCAmelCase									=			s
																			for node in self.graph[s]:
																									if visited.count(node[1]    ) < 1:
																															if node[1] == d:
																																					visited.append(UpperCAmelCase    )
																																					return visited
																															else:
																																					stack.append(node[1]    )
																																					visited.append(node[1]    )
																																					_UpperCAmelCase									=			node[1]
																																					break

            # check if all the children are visited
													if s == ss:
																			stack.pop()
																			if len(UpperCAmelCase    ) != 0:
																									_UpperCAmelCase									=			stack[len(UpperCAmelCase    ) - 1]
													else:
																			_UpperCAmelCase									=			ss

													# check if se have reached the starting point
													if len(UpperCAmelCase    ) == 0:
																			return visited

	def        UpperCamelCase    (							self       ,    UpperCAmelCase=-1    ):
							"""simple docstring"""







							if c == -1:
													_UpperCAmelCase									=			floor(random() * 1_0000    ) + 10
							for i in range(UpperCAmelCase    ):
													# every vertex has max 100 edges
													for _ in range(floor(random() * 102    ) + 1    ):
																			_UpperCAmelCase									=			floor(random() * c    ) + 1
																			if n != i:
																									self.add_pair(UpperCAmelCase       ,    UpperCAmelCase       ,    1    )

	def        UpperCamelCase    (							self       ,    UpperCAmelCase=-2    ):
							"""simple docstring"""







							_UpperCAmelCase									=			deque()
							_UpperCAmelCase									=			[]
							if s == -2:
													_UpperCAmelCase									=			list(self.graph    )[0]
							d.append(UpperCAmelCase    )
							visited.append(UpperCAmelCase    )
							while d:
													_UpperCAmelCase									=			d.popleft()
													if len(self.graph[s]    ) != 0:
																			for node in self.graph[s]:
																									if visited.count(node[1]    ) < 1:
																															d.append(node[1]    )
																															visited.append(node[1]    )
							return visited

	def        UpperCamelCase    (							self       ,    UpperCAmelCase    ):
							"""simple docstring"""







							_UpperCAmelCase									=			0
							for x in self.graph:
													for y in self.graph[x]:
																			if y[1] == u:
																									count += 1
							return count

	def        UpperCamelCase    (							self       ,    UpperCAmelCase    ):
							"""simple docstring"""







							return len(self.graph[u]    )

	def        UpperCamelCase    (							self       ,    UpperCAmelCase=-2    ):
							"""simple docstring"""







							_UpperCAmelCase									=			[]
							_UpperCAmelCase									=			[]
							if s == -2:
													_UpperCAmelCase									=			list(self.graph    )[0]
							stack.append(UpperCAmelCase    )
							visited.append(UpperCAmelCase    )
							_UpperCAmelCase									=			s
							_UpperCAmelCase									=			[]

							while True:
													# check if there is any non isolated nodes
													if len(self.graph[s]    ) != 0:
																			_UpperCAmelCase									=			s
																			for node in self.graph[s]:
																									if visited.count(node[1]    ) < 1:
																															stack.append(node[1]    )
																															visited.append(node[1]    )
																															_UpperCAmelCase									=			node[1]
																															break

            # check if all the children are visited
													if s == ss:
																			sorted_nodes.append(stack.pop()    )
																			if len(UpperCAmelCase    ) != 0:
																									_UpperCAmelCase									=			stack[len(UpperCAmelCase    ) - 1]
													else:
																			_UpperCAmelCase									=			ss

													# check if se have reached the starting point
													if len(UpperCAmelCase    ) == 0:
																			return sorted_nodes

	def        UpperCamelCase    (							self    ):
							"""simple docstring"""







							_UpperCAmelCase									=			[]
							_UpperCAmelCase									=			[]
							_UpperCAmelCase									=			list(self.graph    )[0]
							stack.append(UpperCAmelCase    )
							visited.append(UpperCAmelCase    )
							_UpperCAmelCase									=			-2
							_UpperCAmelCase									=			[]
							_UpperCAmelCase									=			s
							_UpperCAmelCase									=			False
							_UpperCAmelCase									=			set()

							while True:
													# check if there is any non isolated nodes
													if len(self.graph[s]    ) != 0:
																			_UpperCAmelCase									=			s
																			for node in self.graph[s]:
																									if (
																									    visited.count(node[1]    ) > 0
																									    and node[1] != parent
																									    and indirect_parents.count(node[1]    ) > 0
																									    and not on_the_way_back
																									):
																															_UpperCAmelCase									=			len(UpperCAmelCase    ) - 1
																															while len_stack >= 0:
																																					if stack[len_stack] == node[1]:
																																											anticipating_nodes.add(node[1]    )
																																											break
																																					else:
																																											anticipating_nodes.add(stack[len_stack]    )
																																											len_stack -= 1
																									if visited.count(node[1]    ) < 1:
																															stack.append(node[1]    )
																															visited.append(node[1]    )
																															_UpperCAmelCase									=			node[1]
																															break

            # check if all the children are visited
													if s == ss:
																			stack.pop()
																			_UpperCAmelCase									=			True
																			if len(UpperCAmelCase    ) != 0:
																									_UpperCAmelCase									=			stack[len(UpperCAmelCase    ) - 1]
													else:
																			_UpperCAmelCase									=			False
																			indirect_parents.append(UpperCAmelCase    )
																			_UpperCAmelCase									=			s
																			_UpperCAmelCase									=			ss

													# check if se have reached the starting point
													if len(UpperCAmelCase    ) == 0:
																			return list(UpperCAmelCase    )

	def        UpperCamelCase    (							self    ):
							"""simple docstring"""







							_UpperCAmelCase									=			[]
							_UpperCAmelCase									=			[]
							_UpperCAmelCase									=			list(self.graph    )[0]
							stack.append(UpperCAmelCase    )
							visited.append(UpperCAmelCase    )
							_UpperCAmelCase									=			-2
							_UpperCAmelCase									=			[]
							_UpperCAmelCase									=			s
							_UpperCAmelCase									=			False
							_UpperCAmelCase									=			set()

							while True:
													# check if there is any non isolated nodes
													if len(self.graph[s]    ) != 0:
																			_UpperCAmelCase									=			s
																			for node in self.graph[s]:
																									if (
																									    visited.count(node[1]    ) > 0
																									    and node[1] != parent
																									    and indirect_parents.count(node[1]    ) > 0
																									    and not on_the_way_back
																									):
																															_UpperCAmelCase									=			len(UpperCAmelCase    ) - 1
																															while len_stack_minus_one >= 0:
																																					if stack[len_stack_minus_one] == node[1]:
																																											anticipating_nodes.add(node[1]    )
																																											break
																																					else:
																																											return True
																									if visited.count(node[1]    ) < 1:
																															stack.append(node[1]    )
																															visited.append(node[1]    )
																															_UpperCAmelCase									=			node[1]
																															break

            # check if all the children are visited
													if s == ss:
																			stack.pop()
																			_UpperCAmelCase									=			True
																			if len(UpperCAmelCase    ) != 0:
																									_UpperCAmelCase									=			stack[len(UpperCAmelCase    ) - 1]
													else:
																			_UpperCAmelCase									=			False
																			indirect_parents.append(UpperCAmelCase    )
																			_UpperCAmelCase									=			s
																			_UpperCAmelCase									=			ss

													# check if se have reached the starting point
													if len(UpperCAmelCase    ) == 0:
																			return False

	def        UpperCamelCase    (							self       ,    UpperCAmelCase=-2       ,    UpperCAmelCase=-1    ):
							"""simple docstring"""







							_UpperCAmelCase									=			time()
							self.dfs(UpperCAmelCase       ,    UpperCAmelCase    )
							_UpperCAmelCase									=			time()
							return end - begin

	def        UpperCamelCase    (							self       ,    UpperCAmelCase=-2    ):
							"""simple docstring"""







							_UpperCAmelCase									=			time()
							self.bfs(UpperCAmelCase    )
							_UpperCAmelCase									=			time()
							return end - begin





class __lowerCamelCase :





	"""simple docstring"""






	def __init__(							self    ):
							"""simple docstring"""







							_UpperCAmelCase									=			{}

	def        UpperCamelCase    (							self       ,    UpperCAmelCase       ,    UpperCAmelCase       ,    UpperCAmelCase=1    ):
							"""simple docstring"""







							if self.graph.get(UpperCAmelCase    ):
													# if there already is a edge
													if self.graph[u].count([w, v]    ) == 0:
																			self.graph[u].append([w, v]    )
							else:
													# if u does not exist
													_UpperCAmelCase									=			[[w, v]]
							# add the other way
							if self.graph.get(UpperCAmelCase    ):
													# if there already is a edge
													if self.graph[v].count([w, u]    ) == 0:
																			self.graph[v].append([w, u]    )
							else:
													# if u does not exist
													_UpperCAmelCase									=			[[w, u]]

	def        UpperCamelCase    (							self       ,    UpperCAmelCase       ,    UpperCAmelCase    ):
							"""simple docstring"""







							if self.graph.get(UpperCAmelCase    ):
													for _ in self.graph[u]:
																			if _[1] == v:
																									self.graph[u].remove(UpperCAmelCase    )
        # the other way round
							if self.graph.get(UpperCAmelCase    ):
													for _ in self.graph[v]:
																			if _[1] == u:
																									self.graph[v].remove(UpperCAmelCase    )

	def        UpperCamelCase    (							self       ,    UpperCAmelCase=-2       ,    UpperCAmelCase=-1    ):
							"""simple docstring"""







							if s == d:
													return []
							_UpperCAmelCase									=			[]
							_UpperCAmelCase									=			[]
							if s == -2:
													_UpperCAmelCase									=			list(self.graph    )[0]
							stack.append(UpperCAmelCase    )
							visited.append(UpperCAmelCase    )
							_UpperCAmelCase									=			s

							while True:
													# check if there is any non isolated nodes
													if len(self.graph[s]    ) != 0:
																			_UpperCAmelCase									=			s
																			for node in self.graph[s]:
																									if visited.count(node[1]    ) < 1:
																															if node[1] == d:
																																					visited.append(UpperCAmelCase    )
																																					return visited
																															else:
																																					stack.append(node[1]    )
																																					visited.append(node[1]    )
																																					_UpperCAmelCase									=			node[1]
																																					break

            # check if all the children are visited
													if s == ss:
																			stack.pop()
																			if len(UpperCAmelCase    ) != 0:
																									_UpperCAmelCase									=			stack[len(UpperCAmelCase    ) - 1]
													else:
																			_UpperCAmelCase									=			ss

													# check if se have reached the starting point
													if len(UpperCAmelCase    ) == 0:
																			return visited

	def        UpperCamelCase    (							self       ,    UpperCAmelCase=-1    ):
							"""simple docstring"""







							if c == -1:
													_UpperCAmelCase									=			floor(random() * 1_0000    ) + 10
							for i in range(UpperCAmelCase    ):
													# every vertex has max 100 edges
													for _ in range(floor(random() * 102    ) + 1    ):
																			_UpperCAmelCase									=			floor(random() * c    ) + 1
																			if n != i:
																									self.add_pair(UpperCAmelCase       ,    UpperCAmelCase       ,    1    )

	def        UpperCamelCase    (							self       ,    UpperCAmelCase=-2    ):
							"""simple docstring"""







							_UpperCAmelCase									=			deque()
							_UpperCAmelCase									=			[]
							if s == -2:
													_UpperCAmelCase									=			list(self.graph    )[0]
							d.append(UpperCAmelCase    )
							visited.append(UpperCAmelCase    )
							while d:
													_UpperCAmelCase									=			d.popleft()
													if len(self.graph[s]    ) != 0:
																			for node in self.graph[s]:
																									if visited.count(node[1]    ) < 1:
																															d.append(node[1]    )
																															visited.append(node[1]    )
							return visited

	def        UpperCamelCase    (							self       ,    UpperCAmelCase    ):
							"""simple docstring"""







							return len(self.graph[u]    )

	def        UpperCamelCase    (							self    ):
							"""simple docstring"""







							_UpperCAmelCase									=			[]
							_UpperCAmelCase									=			[]
							_UpperCAmelCase									=			list(self.graph    )[0]
							stack.append(UpperCAmelCase    )
							visited.append(UpperCAmelCase    )
							_UpperCAmelCase									=			-2
							_UpperCAmelCase									=			[]
							_UpperCAmelCase									=			s
							_UpperCAmelCase									=			False
							_UpperCAmelCase									=			set()

							while True:
													# check if there is any non isolated nodes
													if len(self.graph[s]    ) != 0:
																			_UpperCAmelCase									=			s
																			for node in self.graph[s]:
																									if (
																									    visited.count(node[1]    ) > 0
																									    and node[1] != parent
																									    and indirect_parents.count(node[1]    ) > 0
																									    and not on_the_way_back
																									):
																															_UpperCAmelCase									=			len(UpperCAmelCase    ) - 1
																															while len_stack >= 0:
																																					if stack[len_stack] == node[1]:
																																											anticipating_nodes.add(node[1]    )
																																											break
																																					else:
																																											anticipating_nodes.add(stack[len_stack]    )
																																											len_stack -= 1
																									if visited.count(node[1]    ) < 1:
																															stack.append(node[1]    )
																															visited.append(node[1]    )
																															_UpperCAmelCase									=			node[1]
																															break

            # check if all the children are visited
													if s == ss:
																			stack.pop()
																			_UpperCAmelCase									=			True
																			if len(UpperCAmelCase    ) != 0:
																									_UpperCAmelCase									=			stack[len(UpperCAmelCase    ) - 1]
													else:
																			_UpperCAmelCase									=			False
																			indirect_parents.append(UpperCAmelCase    )
																			_UpperCAmelCase									=			s
																			_UpperCAmelCase									=			ss

													# check if se have reached the starting point
													if len(UpperCAmelCase    ) == 0:
																			return list(UpperCAmelCase    )

	def        UpperCamelCase    (							self    ):
							"""simple docstring"""







							_UpperCAmelCase									=			[]
							_UpperCAmelCase									=			[]
							_UpperCAmelCase									=			list(self.graph    )[0]
							stack.append(UpperCAmelCase    )
							visited.append(UpperCAmelCase    )
							_UpperCAmelCase									=			-2
							_UpperCAmelCase									=			[]
							_UpperCAmelCase									=			s
							_UpperCAmelCase									=			False
							_UpperCAmelCase									=			set()

							while True:
													# check if there is any non isolated nodes
													if len(self.graph[s]    ) != 0:
																			_UpperCAmelCase									=			s
																			for node in self.graph[s]:
																									if (
																									    visited.count(node[1]    ) > 0
																									    and node[1] != parent
																									    and indirect_parents.count(node[1]    ) > 0
																									    and not on_the_way_back
																									):
																															_UpperCAmelCase									=			len(UpperCAmelCase    ) - 1
																															while len_stack_minus_one >= 0:
																																					if stack[len_stack_minus_one] == node[1]:
																																											anticipating_nodes.add(node[1]    )
																																											break
																																					else:
																																											return True
																									if visited.count(node[1]    ) < 1:
																															stack.append(node[1]    )
																															visited.append(node[1]    )
																															_UpperCAmelCase									=			node[1]
																															break

            # check if all the children are visited
													if s == ss:
																			stack.pop()
																			_UpperCAmelCase									=			True
																			if len(UpperCAmelCase    ) != 0:
																									_UpperCAmelCase									=			stack[len(UpperCAmelCase    ) - 1]
													else:
																			_UpperCAmelCase									=			False
																			indirect_parents.append(UpperCAmelCase    )
																			_UpperCAmelCase									=			s
																			_UpperCAmelCase									=			ss

													# check if se have reached the starting point
													if len(UpperCAmelCase    ) == 0:
																			return False

	def        UpperCamelCase    (							self    ):
							"""simple docstring"""







							return list(self.graph    )

	def        UpperCamelCase    (							self       ,    UpperCAmelCase=-2       ,    UpperCAmelCase=-1    ):
							"""simple docstring"""







							_UpperCAmelCase									=			time()
							self.dfs(UpperCAmelCase       ,    UpperCAmelCase    )
							_UpperCAmelCase									=			time()
							return end - begin

	def        UpperCamelCase    (							self       ,    UpperCAmelCase=-2    ):
							"""simple docstring"""







							_UpperCAmelCase									=			time()
							self.bfs(UpperCAmelCase    )
							_UpperCAmelCase									=			time()
							return end - begin








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        UpperCamelCase    (							self    ):
							"""simple docstring"""







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

													_UpperCAmelCase									=			[t[-1] for t in os.walk(os.path.join(UpperCAmelCase       ,    os.listdir(UpperCAmelCase    )[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        UpperCamelCase    (							self    ):
							"""simple docstring"""







							_UpperCAmelCase					, _UpperCAmelCase									=			FlaxStableDiffusionPipeline.from_pretrained(
							    'hf-internal-testing/tiny-stable-diffusion-pipe'       ,    safety_checker=UpperCAmelCase    )

							_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(UpperCAmelCase    )

							# shard inputs and rng
							_UpperCAmelCase									=			replicate(UpperCAmelCase    )
							_UpperCAmelCase									=			jax.random.split(UpperCAmelCase       ,    UpperCAmelCase    )
							_UpperCAmelCase									=			shard(UpperCAmelCase    )

							_UpperCAmelCase									=			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.1_51_47_45    ) < 1e-3
													assert np.abs(np.abs(UpperCAmelCase       ,    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(UpperCAmelCase    ) == num_samples

	def        UpperCamelCase    (							self    ):
							"""simple docstring"""







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

							_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(UpperCAmelCase    )

							# shard inputs and rng
							_UpperCAmelCase									=			replicate(UpperCAmelCase    )
							_UpperCAmelCase									=			jax.random.split(UpperCAmelCase       ,    UpperCAmelCase    )
							_UpperCAmelCase									=			shard(UpperCAmelCase    )

							_UpperCAmelCase									=			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.05_65_24_01)    ) < 1e-3
													assert np.abs((np.abs(UpperCAmelCase       ,    dtype=np.floataa    ).sum() - 2_38_38_08.2)    ) < 5e-1

	def        UpperCamelCase    (							self    ):
							"""simple docstring"""







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

							_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(UpperCAmelCase    )

							# shard inputs and rng
							_UpperCAmelCase									=			replicate(UpperCAmelCase    )
							_UpperCAmelCase									=			jax.random.split(UpperCAmelCase       ,    UpperCAmelCase    )
							_UpperCAmelCase									=			shard(UpperCAmelCase    )

							_UpperCAmelCase									=			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.04_00_39_06)    ) < 1e-3
													assert np.abs((np.abs(UpperCAmelCase       ,    dtype=np.floataa    ).sum() - 2_37_35_16.75)    ) < 5e-1

	def        UpperCamelCase    (							self    ):
							"""simple docstring"""







							_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(UpperCAmelCase    )

							# shard inputs and rng
							_UpperCAmelCase									=			replicate(UpperCAmelCase    )
							_UpperCAmelCase									=			jax.random.split(UpperCAmelCase       ,    UpperCAmelCase    )
							_UpperCAmelCase									=			shard(UpperCAmelCase    )

							_UpperCAmelCase									=			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.04_00_39_06)    ) < 1e-3
													assert np.abs((np.abs(UpperCAmelCase       ,    dtype=np.floataa    ).sum() - 2_37_35_16.75)    ) < 5e-1

	def        UpperCamelCase    (							self    ):
							"""simple docstring"""







							_UpperCAmelCase									=			FlaxDDIMScheduler(
							    beta_start=0.0_00_85       ,    beta_end=0.0_12       ,    beta_schedule='scaled_linear'       ,    set_alpha_to_one=UpperCAmelCase       ,    steps_offset=1       ,    )

							_UpperCAmelCase					, _UpperCAmelCase									=			FlaxStableDiffusionPipeline.from_pretrained(
							    'CompVis/stable-diffusion-v1-4'       ,    revision='bf16'       ,    dtype=jnp.bfloataa       ,    scheduler=UpperCAmelCase       ,    safety_checker=UpperCAmelCase       ,    )
							_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(UpperCAmelCase    )

							# shard inputs and rng
							_UpperCAmelCase									=			replicate(UpperCAmelCase    )
							_UpperCAmelCase									=			jax.random.split(UpperCAmelCase       ,    UpperCAmelCase    )
							_UpperCAmelCase									=			shard(UpperCAmelCase    )

							_UpperCAmelCase									=			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.0_45_04_39_45)    ) < 1e-3
													assert np.abs((np.abs(UpperCAmelCase       ,    dtype=np.floataa    ).sum() - 2_34_76_93.5)    ) < 5e-1

	def        UpperCamelCase    (							self    ):
							"""simple docstring"""







							_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    )       ,    UpperCAmelCase    )

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

							_UpperCAmelCase									=			replicate(UpperCAmelCase    )
							_UpperCAmelCase									=			pipeline.prepare_inputs(UpperCAmelCase    )
							_UpperCAmelCase									=			shard(UpperCAmelCase    )
							_UpperCAmelCase									=			pipeline(UpperCAmelCase       ,    UpperCAmelCase       ,    UpperCAmelCase       ,    jit=UpperCAmelCase    ).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=UpperCAmelCase       ,    use_memory_efficient_attention=UpperCAmelCase       ,    )

							_UpperCAmelCase									=			replicate(UpperCAmelCase    )
							_UpperCAmelCase									=			pipeline.prepare_inputs(UpperCAmelCase    )
							_UpperCAmelCase									=			shard(UpperCAmelCase    )
							_UpperCAmelCase									=			pipeline(UpperCAmelCase       ,    UpperCAmelCase       ,    UpperCAmelCase       ,    jit=UpperCAmelCase    ).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
