





import copy
import os
import tempfile
from unittest import TestCase
from unittest.mock import patch

import numpy as np
import pyarrow as pa
import pyarrow.parquet as pq
import pytest

from datasets.arrow_writer import ArrowWriter, OptimizedTypedSequence, ParquetWriter, TypedSequence
from datasets.features import ArrayaD, ClassLabel, Features, Image, Value
from datasets.features.features import ArrayaDExtensionType, cast_to_python_objects
from datasets.keyhash import DuplicatedKeysError, InvalidKeyError

from .utils import require_pil



class 					__lowerCAmelCase       (			snake_case__     ):






			def 			lowerCamelCase  (self    )     ->   int:


							'''simple docstring'''



							snake_case_						:    Optional[int]				=  pa.array(TypedSequence([1, 2, 3]    )    )
							self.assertEqual(arr.type					,							pa.intaa()    )






			def 			lowerCamelCase  (self    )     ->   str:


							'''simple docstring'''



							with self.assertRaises(__magic_name__    ):
											snake_case_						:    Optional[int]				=  pa.array(TypedSequence([1, 2, 3]    )					,							type=pa.intaa()    )






			def 			lowerCamelCase  (self    )     ->   List[str]:


							'''simple docstring'''



							with self.assertRaises(__magic_name__    ):
											snake_case_						:    List[Any]				=  pa.array(TypedSequence([1, 2, 3]					,							try_type=Value('''bool'''    )					,							type=Value('''int64'''    )    )    )






			def 			lowerCamelCase  (self    )     ->   Union[str, Any]:


							'''simple docstring'''



							snake_case_						:    Optional[int]				=  pa.array(TypedSequence([1, 2, 3]					,							type=Value('''int32'''    )    )    )
							self.assertEqual(arr.type					,							pa.intaa()    )






			def 			lowerCamelCase  (self    )     ->   Union[str, Any]:


							'''simple docstring'''



							with self.assertRaises((TypeError, pa.lib.ArrowInvalid)    ):
											snake_case_						:    str				=  pa.array(TypedSequence(['''foo''', '''bar''']					,							type=Value('''int64'''    )    )    )






			def 			lowerCamelCase  (self    )     ->   int:


							'''simple docstring'''



							snake_case_						:    List[str]				=  pa.array(TypedSequence([1, 2, 3]					,							try_type=Value('''int32'''    )    )    )
							self.assertEqual(arr.type					,							pa.intaa()    )






			def 			lowerCamelCase  (self    )     ->   List[str]:


							'''simple docstring'''



							snake_case_						:    Optional[Any]				=  pa.array(TypedSequence(['''foo''', '''bar''']					,							try_type=Value('''int64'''    )    )    )
							self.assertEqual(arr.type					,							pa.string()    )






			def 			lowerCamelCase  (self    )     ->   Optional[int]:


							'''simple docstring'''



							snake_case_						:    Optional[int]				=  pa.array(TypedSequence([[[1, 2, 3]]]					,							type=ArrayaD((1, 3)					,							'''int64'''    )    )    )
							self.assertEqual(arr.type					,							ArrayaDExtensionType((1, 3)					,							'''int64'''    )    )






			def 			lowerCamelCase  (self    )     ->   Tuple:


							'''simple docstring'''



							with self.assertRaises((TypeError, pa.lib.ArrowInvalid)    ):
											snake_case_						:    Optional[int]				=  pa.array(TypedSequence(['''foo''', '''bar''']					,							type=ArrayaD((1, 3)					,							'''int64'''    )    )    )






			def 			lowerCamelCase  (self    )     ->   Any:


							'''simple docstring'''



							snake_case_						:    Optional[Any]				=  pa.array(TypedSequence([[[1, 2, 3]]]					,							try_type=ArrayaD((1, 3)					,							'''int64'''    )    )    )
							self.assertEqual(arr.type					,							ArrayaDExtensionType((1, 3)					,							'''int64'''    )    )






			def 			lowerCamelCase  (self    )     ->   Dict:


							'''simple docstring'''



							snake_case_						:    Tuple				=  pa.array(TypedSequence(['''foo''', '''bar''']					,							try_type=ArrayaD((1, 3)					,							'''int64'''    )    )    )
							self.assertEqual(arr.type					,							pa.string()    )






			@require_pil
			def 			lowerCamelCase  (self    )     ->   Union[str, Any]:


							'''simple docstring'''



							import PIL.Image

							snake_case_						:    Dict				=  PIL.Image.fromarray(np.arange(10					,							dtype=np.uinta    ).reshape(2					,							5    )    )
							with patch(
							    '''datasets.arrow_writer.cast_to_python_objects'''					,							side_effect=__magic_name__    ) as mock_cast_to_python_objects:
											snake_case_						:    str				=  pa.array(TypedSequence([{'''path''': None, '''bytes''': B'''image_bytes'''}, pil_image]					,							type=Image()    )    )
											snake_case_			,							snake_case_						:    List[str]				=  mock_cast_to_python_objects.call_args_list[-1]
											self.assertIn('''optimize_list_casting'''					,							__magic_name__    )
											self.assertFalse(kwargs['''optimize_list_casting''']    )






def      lowerCamelCase_						(				_UpperCamelCase ,				_UpperCamelCase )		->						List[str]:






				"""simple docstring"""


				snake_case_						:    Union[str, Any]				=  pa.BufferReader(__lowerCAmelCase ) if isinstance(__lowerCAmelCase ,				pa.Buffer ) else pa.memory_map(__lowerCAmelCase )
				snake_case_						:    Optional[int]				=  pa.ipc.open_stream(__lowerCAmelCase )
				snake_case_						:    Optional[int]				=  f.read_all()
				assert len(pa_table.to_batches() ) == expected_num_chunks
				assert pa_table.to_pydict() == {"col_1": ["foo", "bar"], "col_2": [1, 2]}
				del pa_table






@pytest.mark.parametrize('''writer_batch_size''' ,				[None, 1, 10] )
@pytest.mark.parametrize(
    '''fields''' ,				[None, {'''col_1''': pa.string(), '''col_2''': pa.intaa()}, {'''col_1''': pa.string(), '''col_2''': pa.intaa()}] )
def      lowerCamelCase_						(				_UpperCamelCase ,				_UpperCamelCase )		->						List[str]:






				"""simple docstring"""


				snake_case_						:    int				=  pa.BufferOutputStream()
				snake_case_						:    Union[str, Any]				=  pa.schema(__lowerCAmelCase ) if fields else None
				with ArrowWriter(stream=__lowerCAmelCase ,				schema=__lowerCAmelCase ,				writer_batch_size=__lowerCAmelCase ) as writer:
								writer.write({'''col_1''': '''foo''', '''col_2''': 1} )
								writer.write({'''col_1''': '''bar''', '''col_2''': 2} )
								snake_case_			,							snake_case_						:    Optional[int]				=  writer.finalize()
				assert num_examples == 2
				assert num_bytes > 0
				if not fields:
								snake_case_						:    int				=  {'''col_1''': pa.string(), '''col_2''': pa.intaa()}
				assert writer._schema == pa.schema(__lowerCAmelCase ,				metadata=writer._schema.metadata )
				_check_output(output.getvalue() ,				expected_num_chunks=num_examples if writer_batch_size == 1 else 1 )






def      lowerCamelCase_						(				)		->						Union[str, Any]:






				"""simple docstring"""


				snake_case_						:    Optional[Any]				=  pa.BufferOutputStream()
				snake_case_						:    Optional[int]				=  Features({'''labels''': ClassLabel(names=['''neg''', '''pos'''] )} )
				with ArrowWriter(stream=__lowerCAmelCase ,				features=__lowerCAmelCase ) as writer:
								writer.write({'''labels''': 0} )
								writer.write({'''labels''': 1} )
								snake_case_			,							snake_case_						:    List[Any]				=  writer.finalize()
				assert num_examples == 2
				assert num_bytes > 0
				assert writer._schema == features.arrow_schema
				assert writer._schema.metadata == features.arrow_schema.metadata
				snake_case_						:    Optional[Any]				=  pa.BufferReader(output.getvalue() )
				snake_case_						:    Tuple				=  pa.ipc.open_stream(__lowerCAmelCase )
				snake_case_						:    List[Any]				=  f.read_all()
				snake_case_						:    Tuple				=  pa_table.schema
				assert pa_table.num_rows == 2
				assert schema == features.arrow_schema
				assert schema.metadata == features.arrow_schema.metadata
				assert features == Features.from_arrow_schema(__lowerCAmelCase )






@pytest.mark.parametrize('''writer_batch_size''' ,				[None, 1, 10] )
def      lowerCamelCase_						(				_UpperCamelCase )		->						Dict:






				"""simple docstring"""


				snake_case_						:    Optional[int]				=  pa.BufferOutputStream()
				with ArrowWriter(
				    stream=__lowerCAmelCase ,				writer_batch_size=__lowerCAmelCase ,				hash_salt='''split_name''' ,				check_duplicates=__lowerCAmelCase ,				) as writer:
								with pytest.raises(__lowerCAmelCase ):
												writer.write({'''col_1''': '''foo''', '''col_2''': 1} ,				key=[1, 2] )
												snake_case_			,							snake_case_						:    Any				=  writer.finalize()






@pytest.mark.parametrize('''writer_batch_size''' ,				[None, 2, 10] )
def      lowerCamelCase_						(				_UpperCamelCase )		->						Any:






				"""simple docstring"""


				snake_case_						:    Tuple				=  pa.BufferOutputStream()
				with ArrowWriter(
				    stream=__lowerCAmelCase ,				writer_batch_size=__lowerCAmelCase ,				hash_salt='''split_name''' ,				check_duplicates=__lowerCAmelCase ,				) as writer:
								with pytest.raises(__lowerCAmelCase ):
												writer.write({'''col_1''': '''foo''', '''col_2''': 1} ,				key=10 )
												writer.write({'''col_1''': '''bar''', '''col_2''': 2} ,				key=10 )
												snake_case_			,							snake_case_						:    Union[str, Any]				=  writer.finalize()






@pytest.mark.parametrize('''writer_batch_size''' ,				[None, 2, 10] )
def      lowerCamelCase_						(				_UpperCamelCase )		->						Any:






				"""simple docstring"""


				snake_case_						:    Optional[int]				=  pa.BufferOutputStream()
				with ArrowWriter(
				    stream=__lowerCAmelCase ,				writer_batch_size=__lowerCAmelCase ,				hash_salt='''split_name''' ,				check_duplicates=__lowerCAmelCase ,				) as writer:
								writer.write({'''col_1''': '''foo''', '''col_2''': 1} ,				key=1 )
								writer.write({'''col_1''': '''bar''', '''col_2''': 2} ,				key=2 )
								snake_case_			,							snake_case_						:    Union[str, Any]				=  writer.finalize()
				assert num_examples == 2
				assert num_bytes > 0
				_check_output(output.getvalue() ,				expected_num_chunks=num_examples if writer_batch_size == 1 else 1 )






@pytest.mark.parametrize('''writer_batch_size''' ,				[None, 1, 10] )
@pytest.mark.parametrize(
    '''fields''' ,				[None, {'''col_1''': pa.string(), '''col_2''': pa.intaa()}, {'''col_1''': pa.string(), '''col_2''': pa.intaa()}] )
def      lowerCamelCase_						(				_UpperCamelCase ,				_UpperCamelCase )		->						Optional[int]:






				"""simple docstring"""


				snake_case_						:    Dict				=  pa.BufferOutputStream()
				snake_case_						:    Union[str, Any]				=  pa.schema(__lowerCAmelCase ) if fields else None
				with ArrowWriter(stream=__lowerCAmelCase ,				schema=__lowerCAmelCase ,				writer_batch_size=__lowerCAmelCase ) as writer:
								writer.write_batch({'''col_1''': ['''foo''', '''bar'''], '''col_2''': [1, 2]} )
								writer.write_batch({'''col_1''': [], '''col_2''': []} )
								snake_case_			,							snake_case_						:    Tuple				=  writer.finalize()
				assert num_examples == 2
				assert num_bytes > 0
				if not fields:
								snake_case_						:    List[str]				=  {'''col_1''': pa.string(), '''col_2''': pa.intaa()}
				assert writer._schema == pa.schema(__lowerCAmelCase ,				metadata=writer._schema.metadata )
				_check_output(output.getvalue() ,				expected_num_chunks=num_examples if writer_batch_size == 1 else 1 )






@pytest.mark.parametrize('''writer_batch_size''' ,				[None, 1, 10] )
@pytest.mark.parametrize(
    '''fields''' ,				[None, {'''col_1''': pa.string(), '''col_2''': pa.intaa()}, {'''col_1''': pa.string(), '''col_2''': pa.intaa()}] )
def      lowerCamelCase_						(				_UpperCamelCase ,				_UpperCamelCase )		->						Optional[Any]:






				"""simple docstring"""


				snake_case_						:    int				=  pa.BufferOutputStream()
				snake_case_						:    Optional[int]				=  pa.schema(__lowerCAmelCase ) if fields else None
				with ArrowWriter(stream=__lowerCAmelCase ,				schema=__lowerCAmelCase ,				writer_batch_size=__lowerCAmelCase ) as writer:
								writer.write_table(pa.Table.from_pydict({'''col_1''': ['''foo''', '''bar'''], '''col_2''': [1, 2]} ) )
								snake_case_			,							snake_case_						:    Tuple				=  writer.finalize()
				assert num_examples == 2
				assert num_bytes > 0
				if not fields:
								snake_case_						:    Optional[Any]				=  {'''col_1''': pa.string(), '''col_2''': pa.intaa()}
				assert writer._schema == pa.schema(__lowerCAmelCase ,				metadata=writer._schema.metadata )
				_check_output(output.getvalue() ,				expected_num_chunks=num_examples if writer_batch_size == 1 else 1 )






@pytest.mark.parametrize('''writer_batch_size''' ,				[None, 1, 10] )
@pytest.mark.parametrize(
    '''fields''' ,				[None, {'''col_1''': pa.string(), '''col_2''': pa.intaa()}, {'''col_1''': pa.string(), '''col_2''': pa.intaa()}] )
def      lowerCamelCase_						(				_UpperCamelCase ,				_UpperCamelCase )		->						List[str]:






				"""simple docstring"""


				snake_case_						:    int				=  pa.BufferOutputStream()
				snake_case_						:    List[str]				=  pa.schema(__lowerCAmelCase ) if fields else None
				with ArrowWriter(stream=__lowerCAmelCase ,				schema=__lowerCAmelCase ,				writer_batch_size=__lowerCAmelCase ) as writer:
								writer.write_row(pa.Table.from_pydict({'''col_1''': ['''foo'''], '''col_2''': [1]} ) )
								writer.write_row(pa.Table.from_pydict({'''col_1''': ['''bar'''], '''col_2''': [2]} ) )
								snake_case_			,							snake_case_						:    Tuple				=  writer.finalize()
				assert num_examples == 2
				assert num_bytes > 0
				if not fields:
								snake_case_						:    Union[str, Any]				=  {'''col_1''': pa.string(), '''col_2''': pa.intaa()}
				assert writer._schema == pa.schema(__lowerCAmelCase ,				metadata=writer._schema.metadata )
				_check_output(output.getvalue() ,				expected_num_chunks=num_examples if writer_batch_size == 1 else 1 )






def      lowerCamelCase_						(				)		->						int:






				"""simple docstring"""


				with tempfile.TemporaryDirectory() as tmp_dir:
								snake_case_						:    int				=  {'''col_1''': pa.string(), '''col_2''': pa.intaa()}
								snake_case_						:    List[str]				=  os.path.join(__lowerCAmelCase ,				'''test.arrow''' )
								with ArrowWriter(path=__lowerCAmelCase ,				schema=pa.schema(__lowerCAmelCase ) ) as writer:
												writer.write_batch({'''col_1''': ['''foo''', '''bar'''], '''col_2''': [1, 2]} )
												snake_case_			,							snake_case_						:    Optional[Any]				=  writer.finalize()
								assert num_examples == 2
								assert num_bytes > 0
								assert writer._schema == pa.schema(__lowerCAmelCase ,				metadata=writer._schema.metadata )
								_check_output(__lowerCAmelCase ,				1 )






def      lowerCamelCase_						(				_UpperCamelCase )		->						Tuple:






				"""simple docstring"""


				if pa.types.is_list(__lowerCAmelCase ):
								return get_base_dtype(arr_type.value_type )
				else:
								return arr_type






def      lowerCamelCase_						(				_UpperCamelCase ,				_UpperCamelCase )		->						Dict:






				"""simple docstring"""


				if isinstance(lst[0] ,				__lowerCAmelCase ):
								change_first_primitive_element_in_list(lst[0] ,				__lowerCAmelCase )
				else:
								snake_case_						:    List[Any]				=  value






@pytest.mark.parametrize('''optimized_int_type, expected_dtype''' ,				[(None, pa.intaa()), (Value('''int32''' ), pa.intaa())] )
@pytest.mark.parametrize('''sequence''' ,				[[1, 2, 3], [[1, 2, 3]], [[[1, 2, 3]]]] )
def      lowerCamelCase_						(				_UpperCamelCase ,				_UpperCamelCase ,				_UpperCamelCase )		->						int:






				"""simple docstring"""


				snake_case_						:    Tuple				=  pa.array(TypedSequence(__lowerCAmelCase ,				optimized_int_type=__lowerCAmelCase ) )
				assert get_base_dtype(arr.type ) == expected_dtype






@pytest.mark.parametrize(
    '''col, expected_dtype''' ,				[
        ('''attention_mask''', pa.inta()),
        ('''special_tokens_mask''', pa.inta()),
        ('''token_type_ids''', pa.inta()),
        ('''input_ids''', pa.intaa()),
        ('''other''', pa.intaa()),
    ] ,				)
@pytest.mark.parametrize('''sequence''' ,				[[1, 2, 3], [[1, 2, 3]], [[[1, 2, 3]]]] )
def      lowerCamelCase_						(				_UpperCamelCase ,				_UpperCamelCase ,				_UpperCamelCase )		->						Any:






				"""simple docstring"""


				snake_case_						:    Optional[int]				=  pa.array(OptimizedTypedSequence(__lowerCAmelCase ,				col=__lowerCAmelCase ) )
				assert get_base_dtype(arr.type ) == expected_dtype

				# not in range
				if col != "other":
								# avoids errors due to in-place modifications
								snake_case_						:    Optional[Any]				=  copy.deepcopy(__lowerCAmelCase )
								snake_case_						:    Any				=  np.iinfo(expected_dtype.to_pandas_dtype() ).max + 1
								change_first_primitive_element_in_list(__lowerCAmelCase ,				__lowerCAmelCase )
								snake_case_						:    Union[str, Any]				=  pa.array(OptimizedTypedSequence(__lowerCAmelCase ,				col=__lowerCAmelCase ) )
								assert get_base_dtype(arr.type ) == pa.intaa()






@pytest.mark.parametrize('''raise_exception''' ,				[False, True] )
def      lowerCamelCase_						(				_UpperCamelCase ,				_UpperCamelCase )		->						Optional[Any]:






				"""simple docstring"""


				snake_case_						:    str				=  str(tmp_path / '''dataset-train.arrow''' )
				try:
								with ArrowWriter(path=__lowerCAmelCase ) as writer:
												if raise_exception:
																raise pa.lib.ArrowInvalid()
												else:
																writer.stream.close()
				except pa.lib.ArrowInvalid:
								pass
				finally:
								assert writer.stream.closed






def      lowerCamelCase_						(				_UpperCamelCase )		->						str:






				"""simple docstring"""


				snake_case_						:    Dict				=  '''mock://dataset-train.arrow'''
				with ArrowWriter(path=__lowerCAmelCase ,				storage_options=mockfs.storage_options ) as writer:
								assert isinstance(writer._fs ,				type(__lowerCAmelCase ) )
								assert writer._fs.storage_options == mockfs.storage_options
								writer.write({'''col_1''': '''foo''', '''col_2''': 1} )
								writer.write({'''col_1''': '''bar''', '''col_2''': 2} )
								snake_case_			,							snake_case_						:    Dict				=  writer.finalize()
				assert num_examples == 2
				assert num_bytes > 0
				assert mockfs.exists(__lowerCAmelCase )






def      lowerCamelCase_						(				)		->						Any:






				"""simple docstring"""


				snake_case_						:    Optional[Any]				=  pa.BufferOutputStream()
				with ParquetWriter(stream=__lowerCAmelCase ) as writer:
								writer.write({'''col_1''': '''foo''', '''col_2''': 1} )
								writer.write({'''col_1''': '''bar''', '''col_2''': 2} )
								snake_case_			,							snake_case_						:    List[str]				=  writer.finalize()
				assert num_examples == 2
				assert num_bytes > 0
				snake_case_						:    Any				=  pa.BufferReader(output.getvalue() )
				snake_case_						:    int				=  pq.read_table(__lowerCAmelCase )
				assert pa_table.to_pydict() == {"col_1": ["foo", "bar"], "col_2": [1, 2]}






@require_pil
@pytest.mark.parametrize('''embed_local_files''' ,				[False, True] )
def      lowerCamelCase_						(				_UpperCamelCase ,				_UpperCamelCase )		->						str:






				"""simple docstring"""


				import PIL.Image

				snake_case_						:    Union[str, Any]				=  str(tmp_path / '''test_image_rgb.jpg''' )
				PIL.Image.fromarray(np.zeros((5, 5) ,				dtype=np.uinta ) ).save(__lowerCAmelCase ,				format='''png''' )
				snake_case_						:    Tuple				=  pa.BufferOutputStream()
				with ParquetWriter(
				    stream=__lowerCAmelCase ,				features=Features({'''image''': Image()} ) ,				embed_local_files=__lowerCAmelCase ) as writer:
								writer.write({'''image''': image_path} )
								writer.finalize()
				snake_case_						:    Any				=  pa.BufferReader(output.getvalue() )
				snake_case_						:    Union[str, Any]				=  pq.read_table(__lowerCAmelCase )
				snake_case_						:    Optional[int]				=  pa_table.to_pydict()
				if embed_local_files:
								assert isinstance(out['''image'''][0]['''path'''] ,				__lowerCAmelCase )
								with open(__lowerCAmelCase ,				'''rb''' ) as f:
												assert out["image"][0]["bytes"] == f.read()
				else:
								assert out["image"][0]["path"] == image_path
								assert out["image"][0]["bytes"] is None






def      lowerCamelCase_						(				)		->						Optional[Any]:






				"""simple docstring"""


				snake_case_						:    Dict				=  pa.schema([pa.field('''col_1''' ,				pa.string() ,				nullable=__lowerCAmelCase )] )
				snake_case_						:    Optional[Any]				=  pa.BufferOutputStream()
				with ArrowWriter(stream=__lowerCAmelCase ) as writer:
								writer._build_writer(inferred_schema=__lowerCAmelCase )
								assert writer._schema == pa.schema([pa.field('''col_1''' ,				pa.string() )] )








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
