

"""simple docstring"""





import collections
import json
import os
import re
from typing import TYPE_CHECKING, List, Optional, Tuple

import numpy as np

from ...tokenization_utils_fast import PreTrainedTokenizer
from ...utils import logging


if TYPE_CHECKING:
							from transformers.pipelines.conversational import Conversation


__UpperCamelCase  :      Any							      =		logging.get_logger(__name__)

__UpperCamelCase  :      Union[str, Any]							      =		{'''vocab_file''': '''vocab.txt''', '''emoji_file''': '''emoji.json'''}

__UpperCamelCase  :      List[Any]							      =		{
    '''vocab_file''': {
        '''abeja/gpt-neox-japanese-2.7b''': '''https://huggingface.co/abeja/gpt-neox-japanese-2.7b/resolve/main/vocab.txt''',
    },
    '''emoji_file''': {
        '''abeja/gpt-neox-japanese-2.7b''': '''https://huggingface.co/abeja/gpt-neox-japanese-2.7b/resolve/main/emoji.json''',
    },
}

__UpperCamelCase  :      Optional[Any]							      =		{
    '''abeja/gpt-neox-japanese-2.7b''': 2_0_4_8,
}

def   __SCREAMING_SNAKE_CASE  (      A_						,     A_ ):
	with open(A_						,     '''r'''						,     encoding='''utf-8''' ) as f:
		lowerCAmelCase__	:						Dict  				=	json.loads(f.read() )

	lowerCAmelCase__	:						Union[str, Any]  				=	collections.OrderedDict()
	lowerCAmelCase__	:						Optional[int]  				=	collections.OrderedDict()
	lowerCAmelCase__	:						Optional[Any]  				=	collections.OrderedDict()
	with open(A_						,     '''r'''						,     encoding='''utf-8''' ) as f:
		lowerCAmelCase__	:						str  				=	f.readlines()
	lowerCAmelCase__	:						Optional[int]  				=	[[t.rstrip('''\n''' )] if (t == ''',''' or ''',''' not in t) else t.rstrip('''\n''' ).split(''',''' ) for t in token]
	for idx, b in enumerate(A_ ):
		lowerCAmelCase__	:						int  				=	b
		lowerCAmelCase__	:						Dict  				=	idx
		for wd in b:
			lowerCAmelCase__	:						List[str]  				=	idx

	return vocab, raw_vocab, ids_to_tokens, emoji







class 				SCREAMING_SNAKE_CASE					(		a_       ):






					"""simple docstring"""



					lowercase__				 =       VOCAB_FILES_NAMES
					lowercase__				 =       PRETRAINED_VOCAB_FILES_MAP
					lowercase__				 =       PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
					lowercase__				 =       ["input_ids", "attention_mask"]







					def __init__(       self			:				List[Any]   ,lowercase_			:				Dict   ,lowercase_			:				Dict   ,lowercase_			:				int="<|endoftext|>"   ,lowercase_			:				Dict="<|endoftext|>"   ,lowercase_			:				str="<|startoftext|>"   ,lowercase_			:				str="<|endoftext|>"   ,lowercase_			:				List[Any]=False   ,**lowercase_			:				Optional[Any]   ,):
						super().__init__(
						    unk_token=lowercase_   ,pad_token=lowercase_   ,bos_token=lowercase_   ,eos_token=lowercase_   ,do_clean_text=lowercase_   ,**lowercase_   ,)
						if not os.path.isfile(lowercase_		):
							raise ValueError(
							    F'Can\'t find a vocabulary file at path \'{vocab_file}\'. To load the vocabulary from a Google pretrained'
							    ''' model use `tokenizer = GPTNeoXJapaneseokenizer.from_pretrained(PRETRAINED_MODEL_NAME)`'''		)
						if not os.path.isfile(lowercase_		):
							raise ValueError(
							    F'Can\'t find a emoji file at path \'{emoji_file}\'. To load the emoji information from a Google'
							    ''' pretrained model use `tokenizer = GPTNeoXJapaneseokenizer.from_pretrained(PRETRAINED_MODEL_NAME)`'''		)
						lowerCAmelCase__	:						Tuple  				=	do_clean_text
						lowerCAmelCase__				,lowerCAmelCase__				,lowerCAmelCase__				,lowerCAmelCase__	:						Any  				=	load_vocab_and_emoji(lowercase_   ,lowercase_		)
						lowerCAmelCase__	:						Tuple  				=	SubWordJapaneseTokenizer(
						    vocab=self.vocab   ,ids_to_tokens=self.ids_to_tokens   ,emoji=self.emoji		)







					@property
					def        __lowerCAmelCase (       self			:				int		):
						# self.vocab contains support for character fluctuation unique to Japanese, and has a large number of vocab
						return len(self.raw_vocab		)







					def        __lowerCAmelCase (       self			:				Union[str, Any]		):
						return dict(self.raw_vocab   ,**self.added_tokens_encoder		)







					def        __lowerCAmelCase (       self			:				Any   ,lowercase_			:				List[Any]		):
						return self.subword_tokenizer.tokenize(lowercase_   ,clean=self.do_clean_text		)







					def        __lowerCAmelCase (       self			:				List[str]   ,lowercase_			:				List[str]		):
						return self.vocab.get(lowercase_   ,self.vocab.get(self.unk_token		)		)







					def        __lowerCAmelCase (       self			:				int   ,lowercase_			:				str		):
						return self.subword_tokenizer.convert_id_to_token(lowercase_		)







					def        __lowerCAmelCase (       self			:				int   ,lowercase_			:				List[Any]		):
						lowerCAmelCase__	:						Union[str, Any]  				=	''''''.join(lowercase_		).strip()
						return out_string







					def        __lowerCAmelCase (       self			:				Union[str, Any]   ,lowercase_			:				"Conversation"		):
						lowerCAmelCase__	:						int  				=	[]
						for is_user, text in conversation.iter_texts():
							input_ids.extend(self.encode(lowercase_   ,add_special_tokens=lowercase_		) + [self.eos_token_id]		)

						if len(lowercase_		) > self.model_max_length:
							lowerCAmelCase__	:						Dict  				=	input_ids[-self.model_max_length :]
						return input_ids







					def        __lowerCAmelCase (       self			:				List[str]   ,lowercase_			:				str   ,lowercase_			:				Optional[str] = None		):
						lowerCAmelCase__	:						Tuple  				=	0
						if os.path.isdir(lowercase_		):
							lowerCAmelCase__	:						Union[str, Any]  				=	os.path.join(
							    lowercase_   ,(filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file''']		)
							lowerCAmelCase__	:						List[Any]  				=	os.path.join(
							    lowercase_   ,(filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''emoji_file''']		)
						else:
							lowerCAmelCase__	:						str  				=	(
							    (filename_prefix + '''-''' if filename_prefix else '''''') + save_directory + VOCAB_FILES_NAMES['''vocab_file''']
							)
							lowerCAmelCase__	:						Any  				=	(
							    (filename_prefix + '''-''' if filename_prefix else '''''') + save_directory + VOCAB_FILES_NAMES['''emoji_file''']
							)
						with open(lowercase_   ,'''w'''   ,encoding='''utf-8'''		) as writer:
							for token_index, token in self.ids_to_tokens.items():
								if index != token_index:
									logger.warning(
									    F'Saving vocabulary to {vocab_file}: vocabulary indices are not consecutive.'
									    ''' Please check that the vocabulary is not corrupted!'''		)
									lowerCAmelCase__	:						Dict  				=	token_index
								writer.write(''','''.join(lowercase_		) + '''\n'''		)
								index += 1
						with open(lowercase_   ,'''w'''   ,encoding='''utf-8'''		) as writer:
							json.dump(self.emoji   ,lowercase_		)
						return vocab_file, emoji_file







class 				SCREAMING_SNAKE_CASE					(		a_       ):






					"""simple docstring"""



					def __init__(       self			:				str   ,lowercase_			:				Optional[Any]   ,lowercase_			:				Dict   ,lowercase_			:				Any		):
						lowerCAmelCase__	:						Optional[int]  				=	vocab  # same as swe
						lowerCAmelCase__	:						Optional[int]  				=	ids_to_tokens  # same as bpe
						lowerCAmelCase__	:						Optional[Any]  				=	emoji
						lowerCAmelCase__	:						Any  				=	np.max([len(lowercase_		) for w in self.vocab.keys()]		)
						lowerCAmelCase__	:						int  				=	re.compile(R'''(https?|ftp)(:\/\/[-_\.!~*\'()a-zA-Z0-9;\/?:\@&=\+$,%#]+)'''		)
						lowerCAmelCase__	:						str  				=	re.compile(R'''[A-Za-z0-9\._+]*@[\-_0-9A-Za-z]+(\.[A-Za-z]+)*'''		)
						lowerCAmelCase__	:						List[str]  				=	re.compile(R'''[\(]{0,1}[0-9]{2,4}[\)\-\(]{0,1}[0-9]{2,4}[\)\-]{0,1}[0-9]{3,4}'''		)
						lowerCAmelCase__	:						Union[str, Any]  				=	re.compile(
						    R'''([12]\d{3}[/\-年])*(0?[1-9]|1[0-2])[/\-月]((0?[1-9]|[12][0-9]|3[01])日?)*(\d{1,2}|:|\d{1,2}時|\d{1,2}分|\(日\)|\(月\)|\(火\)|\(水\)|\(木\)|\(金\)|\(土\)|㈰|㈪|㈫|㈬|㈭|㈮|㈯)*'''		)
						lowerCAmelCase__	:						List[Any]  				=	re.compile(
						    R'''(明治|大正|昭和|平成|令和|㍾|㍽|㍼|㍻|\u32ff)\d{1,2}年(0?[1-9]|1[0-2])月(0?[1-9]|[12][0-9]|3[01])日(\d{1,2}|:|\d{1,2}時|\d{1,2}分|\(日\)|\(月\)|\(火\)|\(水\)|\(木\)|\(金\)|\(土\)|㈰|㈪|㈫|㈬|㈭|㈮|㈯)*'''		)
						lowerCAmelCase__	:						Optional[int]  				=	re.compile(
						    R'''((0|[1-9]\d*|[1-9]\d{0,2}(,\d{3})+)*億)*((0|[1-9]\d*|[1-9]\d{0,2}(,\d{3})+)*万)*((0|[1-9]\d*|[1-9]\d{0,2}(,\d{3})+)*千)*(0|[1-9]\d*|[1-9]\d{0,2}(,\d{3})+)*(千円|万円|千万円|円|千ドル|万ドル|千万ドル|ドル|千ユーロ|万ユーロ|千万ユーロ|ユーロ)+(\(税込\)|\(税抜\)|\+tax)*'''		)
						lowerCAmelCase__	:						List[Any]  				=	'''─━│┃┄┅┆┇┈┉┊┋┌┍┎┏┐┑┒┓└┕┖┗┘┙┚┛├┝┞┟┠┡┢┣┤┥┦┧┨┩┪┫┬┭┮┯┰┱┲┳┴┵┶┷┸┹┺┻┼┽┾┿╀╁╂╃╄╅╆╇╈╉╊╋╌╍╎╏═║╒╓╔╕╖╗╘╙╚╛╜╝╞╟╠╡╢╣╤╥╦╧╨╩╪╫╬╭╮╯╰╱╲╳╴╵╶╷╸╹╺╻╼╽╾╿'''
						lowerCAmelCase__	:						List[Any]  				=	'''▀▁▂▃▄▅▆▇█▉▊▋▌▍▎▏▐░▒▓▔▕▖▗▘▙▚▛▜▝▞▟'''
						lowerCAmelCase__	:						List[str]  				=	str.maketrans({k: '''<BLOCK>''' for k in keisen + blocks}		)







					def __len__(       self			:				int		):
						return len(self.ids_to_tokens		)







					def        __lowerCAmelCase (       self			:				Optional[int]   ,lowercase_			:				Dict		):
						lowerCAmelCase__	:						Tuple  				=	self.content_repattera.sub('''<URL>'''   ,lowercase_		)
						lowerCAmelCase__	:						List[str]  				=	self.content_repattera.sub('''<EMAIL>'''   ,lowercase_		)
						lowerCAmelCase__	:						str  				=	self.content_repattera.sub('''<TEL>'''   ,lowercase_		)
						lowerCAmelCase__	:						Dict  				=	self.content_repattera.sub('''<DATE>'''   ,lowercase_		)
						lowerCAmelCase__	:						Optional[Any]  				=	self.content_repattera.sub('''<DATE>'''   ,lowercase_		)
						lowerCAmelCase__	:						Union[str, Any]  				=	self.content_repattera.sub('''<PRICE>'''   ,lowercase_		)
						lowerCAmelCase__	:						Optional[Any]  				=	content.translate(self.content_transa		)
						while "<BLOCK><BLOCK>" in content:
							lowerCAmelCase__	:						str  				=	content.replace('''<BLOCK><BLOCK>'''   ,'''<BLOCK>'''		)
						return content







					def        __lowerCAmelCase (       self			:				List[str]   ,lowercase_			:				Union[str, Any]   ,lowercase_			:				List[str]=False		):
						lowerCAmelCase__	:						str  				=	text.replace(''' '''   ,'''<SP>'''		)
						lowerCAmelCase__	:						Tuple  				=	text.replace('''　'''   ,'''<SP>'''		)
						lowerCAmelCase__	:						Optional[int]  				=	text.replace('''\r\n'''   ,'''<BR>'''		)
						lowerCAmelCase__	:						str  				=	text.replace('''\n'''   ,'''<BR>'''		)
						lowerCAmelCase__	:						Tuple  				=	text.replace('''\r'''   ,'''<BR>'''		)
						lowerCAmelCase__	:						Union[str, Any]  				=	text.replace('''\t'''   ,'''<TAB>'''		)
						lowerCAmelCase__	:						str  				=	text.replace('''—'''   ,'''ー'''		)
						lowerCAmelCase__	:						Union[str, Any]  				=	text.replace('''−'''   ,'''ー'''		)
						for k, v in self.emoji["emoji"].items():
							if k in text:
								lowerCAmelCase__	:						Tuple  				=	text.replace(lowercase_   ,lowercase_		)
						if clean:
							lowerCAmelCase__	:						str  				=	self.clean_text(lowercase_		)

						def check_simbol(lowercase_			:				Optional[int]		):
							lowerCAmelCase__	:						List[Any]  				=	x.encode()
							if len(lowercase_		) == 1 and len(lowercase_		) == 2:
								lowerCAmelCase__	:						Union[str, Any]  				=	(int(e[0]		) << 8) + int(e[1]		)
								if (
								    (c >= 0xC2_A1 and c <= 0xC2_BF)
								    or (c >= 0xC7_80 and c <= 0xC7_83)
								    or (c >= 0xCA_B9 and c <= 0xCB_BF)
								    or (c >= 0xCC_80 and c <= 0xCD_A2)
								):
									return True
							return False

						def checkuae(lowercase_			:				Union[str, Any]		):
							lowerCAmelCase__	:						List[str]  				=	x.encode()
							if len(lowercase_		) == 1 and len(lowercase_		) == 3:
								lowerCAmelCase__	:						int  				=	(int(e[0]		) << 1_6) + (int(e[1]		) << 8) + int(e[2]		)
								if c >= 0xE2_80_80 and c <= 0xE2_B0_7F:
									return True
							return False

						lowerCAmelCase__	:						Tuple  				=	0
						lowerCAmelCase__	:						Optional[Any]  				=	[]
						while pos < len(lowercase_		):
							lowerCAmelCase__	:						Optional[int]  				=	min(len(lowercase_		)   ,pos + self.maxlen + 1		) if text[pos] == '''<''' else pos + 3
							lowerCAmelCase__	:						int  				=	[]  # (token_id, token, pos)
							for e in range(lowercase_   ,lowercase_   ,-1		):
								lowerCAmelCase__	:						int  				=	text[pos:e]
								if wd in self.vocab:
									if wd[0] == "<" and len(lowercase_		) > 2:
										lowerCAmelCase__	:						List[str]  				=	[(self.vocab[wd], wd, e)]
										break
									else:
										candidates.append((self.vocab[wd], wd, e)		)
							if len(lowercase_		) > 0:
								# the smallest token_id is adopted
								lowerCAmelCase__				,lowerCAmelCase__				,lowerCAmelCase__	:						Optional[Any]  				=	sorted(lowercase_   ,key=lambda lowercase_		: x[0]		)[0]
								result.append(lowercase_		)
								lowerCAmelCase__	:						Tuple  				=	e
							else:
								lowerCAmelCase__	:						Any  				=	pos + 1
								lowerCAmelCase__	:						Any  				=	text[pos:end]
								if check_simbol(lowercase_		):
									result.append('''<KIGOU>'''		)
								elif checkuae(lowercase_		):
									result.append('''<U2000U2BFF>'''		)
								else:
									for i in wd.encode('''utf-8'''		):
										result.append('''<|byte%d|>''' % i		)
								lowerCAmelCase__	:						Dict  				=	end
						return result







					def        __lowerCAmelCase (       self			:				Union[str, Any]   ,lowercase_			:				Union[str, Any]   ,lowercase_			:				Dict="\n"		):
						lowerCAmelCase__	:						Dict  				=	[]
						lowerCAmelCase__	:						str  				=	[]
						lowerCAmelCase__	:						Any  				=	self.ids_to_tokens[index][0]
						if word[:6] == "<|byte" and word[-2:] == "|>":
							byte_tokens.append(int(word[6:-2]		)		)
						else:
							if len(lowercase_		) > 0:
								words.append(bytearray(lowercase_		).decode('''utf-8'''   ,errors='''replace'''		)		)
								lowerCAmelCase__	:						Optional[int]  				=	[]
							if word[:7] == "<|emoji" and word[-2:] == "|>":
								words.append(self.emoji['''emoji_inv'''][word]		)
							elif word == "<SP>":
								words.append(''' '''		)
							elif word == "<BR>":
								words.append(lowercase_		)
							elif word == "<TAB>":
								words.append('''\t'''		)
							elif word == "<BLOCK>":
								words.append('''▀'''		)
							elif word == "<KIGOU>":
								words.append('''ǀ'''		)
							elif word == "<U2000U2BFF>":
								words.append('''‖'''		)
							else:
								words.append(lowercase_		)
						if len(lowercase_		) > 0:
							words.append(bytearray(lowercase_		).decode('''utf-8'''   ,errors='''replace'''		)		)
						lowerCAmelCase__	:						Any  				=	''''''.join(lowercase_		)
						return text









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







	"""simple docstring"""






	def      A	(						self			:     str		)			->		int:
								with tempfile.TemporaryDirectory() as tmpdirname:
															# pipeline has Flax weights
															UpperCAmelCase :       Optional[Any] 							=		FlaxDiffusionPipeline.from_pretrained(
															    '''hf-internal-testing/tiny-stable-diffusion-pipe'''		,			safety_checker=__snake_case		,			cache_dir=__snake_case		)

															UpperCAmelCase :       str 							=		[t[-1] for t in os.walk(os.path.join(__snake_case		,			os.listdir(__snake_case		)[0]		,			'''snapshots'''		)		)]
															UpperCAmelCase :       str 							=		[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			):







	"""simple docstring"""






	def      A	(						self			:     List[str]		)			->		Dict:
								UpperCAmelCase							,		UpperCAmelCase :       str 							=		FlaxStableDiffusionPipeline.from_pretrained(
								    '''hf-internal-testing/tiny-stable-diffusion-pipe'''		,			safety_checker=__snake_case		)

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

								UpperCAmelCase :       List[str] 							=		jax.random.PRNGKey(0		)
								UpperCAmelCase :       Optional[Any] 							=		4

								UpperCAmelCase :       Optional[Any] 							=		jax.device_count()
								UpperCAmelCase :       Tuple 							=		num_samples * [prompt]
								UpperCAmelCase :       int 							=		pipeline.prepare_inputs(__snake_case		)

								# shard inputs and rng
								UpperCAmelCase :       Tuple 							=		replicate(__snake_case		)
								UpperCAmelCase :       Any 							=		jax.random.split(__snake_case		,			__snake_case		)
								UpperCAmelCase :       Optional[Any] 							=		shard(__snake_case		)

								UpperCAmelCase :       Optional[int] 							=		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 :       Union[str, Any] 							=		pipeline.numpy_to_pil(np.asarray(images.reshape((num_samples,) + images.shape[-3:]		)		)		)
								assert len(__snake_case		) == num_samples







	def      A	(						self			:     List[Any]		)			->		List[str]:
								UpperCAmelCase							,		UpperCAmelCase :       Union[str, Any] 							=		FlaxStableDiffusionPipeline.from_pretrained(
								    '''CompVis/stable-diffusion-v1-4'''		,			revision='''flax'''		,			safety_checker=__snake_case		)

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

								UpperCAmelCase :       Optional[Any] 							=		jax.random.PRNGKey(0		)
								UpperCAmelCase :       Any 							=		50

								UpperCAmelCase :       Union[str, Any] 							=		jax.device_count()
								UpperCAmelCase :       int 							=		num_samples * [prompt]
								UpperCAmelCase :       Union[str, Any] 							=		pipeline.prepare_inputs(__snake_case		)

								# shard inputs and rng
								UpperCAmelCase :       Dict 							=		replicate(__snake_case		)
								UpperCAmelCase :       int 							=		jax.random.split(__snake_case		,			__snake_case		)
								UpperCAmelCase :       Tuple 							=		shard(__snake_case		)

								UpperCAmelCase :       Tuple 							=		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      A	(						self			:     int		)			->		Dict:
								UpperCAmelCase							,		UpperCAmelCase :       List[str] 							=		FlaxStableDiffusionPipeline.from_pretrained(
								    '''CompVis/stable-diffusion-v1-4'''		,			revision='''bf16'''		,			dtype=jnp.bfloataa		,			safety_checker=__snake_case		)

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

								UpperCAmelCase :       Union[str, Any] 							=		jax.random.PRNGKey(0		)
								UpperCAmelCase :       List[str] 							=		50

								UpperCAmelCase :       Union[str, Any] 							=		jax.device_count()
								UpperCAmelCase :       List[Any] 							=		num_samples * [prompt]
								UpperCAmelCase :       int 							=		pipeline.prepare_inputs(__snake_case		)

								# shard inputs and rng
								UpperCAmelCase :       Tuple 							=		replicate(__snake_case		)
								UpperCAmelCase :       List[Any] 							=		jax.random.split(__snake_case		,			__snake_case		)
								UpperCAmelCase :       Optional[int] 							=		shard(__snake_case		)

								UpperCAmelCase :       Optional[Any] 							=		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      A	(						self			:     int		)			->		Any:
								UpperCAmelCase							,		UpperCAmelCase :       Dict 							=		FlaxStableDiffusionPipeline.from_pretrained(
								    '''CompVis/stable-diffusion-v1-4'''		,			revision='''bf16'''		,			dtype=jnp.bfloataa		)

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

								UpperCAmelCase :       List[str] 							=		jax.random.PRNGKey(0		)
								UpperCAmelCase :       Union[str, Any] 							=		50

								UpperCAmelCase :       Optional[int] 							=		jax.device_count()
								UpperCAmelCase :       List[str] 							=		num_samples * [prompt]
								UpperCAmelCase :       Dict 							=		pipeline.prepare_inputs(__snake_case		)

								# shard inputs and rng
								UpperCAmelCase :       Tuple 							=		replicate(__snake_case		)
								UpperCAmelCase :       Any 							=		jax.random.split(__snake_case		,			__snake_case		)
								UpperCAmelCase :       str 							=		shard(__snake_case		)

								UpperCAmelCase :       Optional[int] 							=		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      A	(						self			:     Tuple		)			->		Optional[Any]:
								UpperCAmelCase :       int 							=		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 :       Tuple 							=		FlaxStableDiffusionPipeline.from_pretrained(
								    '''CompVis/stable-diffusion-v1-4'''		,			revision='''bf16'''		,			dtype=jnp.bfloataa		,			scheduler=__snake_case		,			safety_checker=__snake_case		,			)
								UpperCAmelCase :       Tuple 							=		scheduler.create_state()

								UpperCAmelCase :       Dict 							=		scheduler_state

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

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

								UpperCAmelCase :       Optional[Any] 							=		jax.device_count()
								UpperCAmelCase :       Any 							=		num_samples * [prompt]
								UpperCAmelCase :       Dict 							=		pipeline.prepare_inputs(__snake_case		)

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

								UpperCAmelCase :       Dict 							=		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      A	(						self			:     Any		)			->		Tuple:
								UpperCAmelCase :       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'''
								)

								UpperCAmelCase :       Union[str, Any] 							=		jax.device_count()
								UpperCAmelCase :       List[Any] 							=		num_samples * [prompt]
								UpperCAmelCase :       str 							=		jax.random.split(jax.random.PRNGKey(0		)		,			__snake_case		)

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

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

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

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





