


'''simple docstring'''




import json
import os
import tempfile

import datasets
from datasets.arrow_writer import ArrowWriter
from datasets.features import ArrayaD
from utils import generate_examples, get_duration


_lowerCAmelCase	:		int		  =						(30, 487)
_lowerCAmelCase	:		Dict		  =						(36, 1024)
_lowerCAmelCase	:		int		  =						(100, 100)
_lowerCAmelCase	:		Any		  =						100

_lowerCAmelCase	:		str		  =						datasets.Features(
    {'text': ArrayaD(SHAPE_TEST_1, dtype='float32'), 'image': ArrayaD(SHAPE_TEST_2, dtype='float32')}
)

_lowerCAmelCase  ,   _lowerCAmelCase	:		str		  =						os.path.split(__file__)
_lowerCAmelCase	:		Optional[int]		  =						os.path.join(RESULTS_BASEPATH, 'results', RESULTS_FILENAME.replace('.py', '.json'))

@get_duration
def       __UpperCamelCase     (     _A     :					Union[str, Any]       , _A     :					Any       , _A     :					Optional[int]					)     ->   List[str]:




	"""simple docstring"""





	with ArrowWriter(features=_A       , path=os.path.join(_A       , 'beta.arrow'					)					) as writer:
		for key, record in dummy_data:
			lowerCAmelCase       :     Any   				=			my_features.encode_example(_A					)
			writer.write(_A					)
		lowerCAmelCase				, lowerCAmelCase       :     Any   				=			writer.finalize()

@get_duration
def       __UpperCamelCase     (     _A     :					Union[str, Any]       , _A     :					List[Any]					)     ->   int:




	"""simple docstring"""





	lowerCAmelCase       :     int   				=			datasets.Dataset.from_file(
	    filename=os.path.join(_A       , 'beta.arrow'					)       , info=datasets.DatasetInfo(features=_A					)					)
	for _ in dataset:
		pass

@get_duration
def       __UpperCamelCase     (     _A     :					str       , _A     :					Tuple					)     ->   Union[str, Any]:




	"""simple docstring"""





	lowerCAmelCase       :     List[str]   				=			datasets.Dataset.from_file(
	    filename=os.path.join(_A       , 'beta.arrow'					)       , info=datasets.DatasetInfo(features=_A					)					)
	dataset.set_format('numpy'					)
	for _ in dataset:
		pass

@get_duration
def       __UpperCamelCase     (     _A     :					Optional[Any]       , _A     :					Dict					)     ->   str:




	"""simple docstring"""





	lowerCAmelCase       :     str   				=			10
	lowerCAmelCase       :     Optional[int]   				=			datasets.Dataset.from_file(
	    filename=os.path.join(_A       , 'beta.arrow'					)       , info=datasets.DatasetInfo(features=_A					)					)
	for i in range(0       , len(_A					)       , _A					):
		lowerCAmelCase       :     Dict   				=			dataset[i : i + batch_size]

@get_duration
def       __UpperCamelCase     (     _A     :					Tuple       , _A     :					Optional[Any]					)     ->   Optional[Any]:




	"""simple docstring"""





	lowerCAmelCase       :     Dict   				=			10
	lowerCAmelCase       :     str   				=			datasets.Dataset.from_file(
	    filename=os.path.join(_A       , 'beta.arrow'					)       , info=datasets.DatasetInfo(features=_A					)					)
	dataset.set_format('numpy'					)
	for i in range(0       , len(_A					)       , _A					):
		lowerCAmelCase       :     Dict   				=			dataset[i : i + batch_size]

@get_duration
def       __UpperCamelCase     (     _A     :					Optional[Any]       , _A     :					Dict					)     ->   Optional[int]:




	"""simple docstring"""





	lowerCAmelCase       :     Union[str, Any]   				=			datasets.Dataset.from_file(
	    filename=os.path.join(_A       , 'beta.arrow'					)       , info=datasets.DatasetInfo(features=_A					)					)
	for col in feats:
		lowerCAmelCase       :     Optional[int]   				=			dataset[col]

@get_duration
def       __UpperCamelCase     (     _A     :					Tuple       , _A     :					Union[str, Any]					)     ->   Dict:




	"""simple docstring"""





	lowerCAmelCase       :     Optional[Any]   				=			datasets.Dataset.from_file(
	    filename=os.path.join(_A       , 'beta.arrow'					)       , info=datasets.DatasetInfo(features=_A					)					)
	dataset.set_format('numpy'					)
	for col in feats:
		lowerCAmelCase       :     Union[str, Any]   				=			dataset[col]

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




	"""simple docstring"""





	lowerCAmelCase       :     Any   				=			{}
	lowerCAmelCase       :     int   				=			(
	    read_unformated,
	    read_formatted_as_numpy,
	    read_batch_unformated,
	    read_batch_formatted_as_numpy,
	    read_col_unformated,
	    read_col_formatted_as_numpy,
	)
	with tempfile.TemporaryDirectory() as tmp_dir:
		lowerCAmelCase       :     Any   				=			datasets.Features({'image': ArrayaD(_A       , dtype='float32'					)}					)
		lowerCAmelCase       :     Dict   				=			generate_examples(features=_A       , num_examples=_A					)
		lowerCAmelCase       :     Any   				=			write(_A       , _A       , _A					)
		for read_func in read_functions:
			lowerCAmelCase       :     Tuple   				=			read_func(_A       , _A					)

	with tempfile.TemporaryDirectory() as tmp_dir:
		# don't use fixed length for fair comparison
		# feats = datasets.Features(
		#     {"image": datasets.Sequence(datasets.Sequence(datasets.Value("float32"), SPEED_TEST_SHAPE[1]), SPEED_TEST_SHAPE[0])}
		# )
		lowerCAmelCase       :     Tuple   				=			datasets.Features({'image': datasets.Sequence(datasets.Sequence(datasets.Value('float32'					)					)					)}					)
		lowerCAmelCase       :     Optional[Any]   				=			generate_examples(
		    features=_A       , num_examples=_A       , seq_shapes={'image': SPEED_TEST_SHAPE}					)
		lowerCAmelCase       :     Tuple   				=			write(_A       , _A       , _A					)
		for read_func in read_functions:
			lowerCAmelCase       :     List[Any]   				=			read_func(_A       , _A					)

	with tempfile.TemporaryDirectory() as tmp_dir:
		# don't use fixed length for fair comparison
		# feats = datasets.Features(
		#     {"image": datasets.Sequence(datasets.Value("float32"), SPEED_TEST_SHAPE[0] * SPEED_TEST_SHAPE[1])}
		# )
		lowerCAmelCase       :     Union[str, Any]   				=			datasets.Features({'image': datasets.Sequence(datasets.Value('float32'					)					)}					)
		lowerCAmelCase       :     str   				=			generate_examples(
		    features=_A       , num_examples=_A       , seq_shapes={'image': [SPEED_TEST_SHAPE[0] * SPEED_TEST_SHAPE[1]]}       , )
		lowerCAmelCase       :     Optional[int]   				=			write(_A       , _A       , _A					)
		for read_func in read_functions:
			lowerCAmelCase       :     Optional[int]   				=			read_func(_A       , _A					)

	with open(_A       , 'wb'					) as f:
		f.write(json.dumps(_A					).encode('utf-8'					)					)


if __name__ == "__main__":  # useful to run the profiler
				benchmark_array_xd()




'''simple docstring'''




import inspect

from diffusers.utils import is_flax_available
from diffusers.utils.testing_utils import require_flax


if is_flax_available():
				import jax



@require_flax
class 			lowerCAmelCase					:



	def 					lowercase  (       self     ):
		lowerCAmelCase				, lowerCAmelCase       :     Union[str, Any]   				=			self.prepare_init_args_and_inputs_for_common()

		lowerCAmelCase       :     Any   				=			self.model_class(**snake_case__     )
		lowerCAmelCase       :     int   				=			model.init(inputs_dict['prng_key']			,       inputs_dict['sample']     )
		jax.lax.stop_gradient(snake_case__     )

		lowerCAmelCase       :     Tuple   				=			model.apply(snake_case__			,       inputs_dict['sample']     )

		if isinstance(snake_case__			,       snake_case__     ):
			lowerCAmelCase       :     int   				=			output.sample

		self.assertIsNotNone(snake_case__     )
		lowerCAmelCase       :     List[str]   				=			inputs_dict['sample'].shape
		self.assertEqual(output.shape			,       snake_case__			,       'Input and output shapes do not match'     )



	def 					lowercase  (       self     ):
		lowerCAmelCase				, lowerCAmelCase       :     Optional[int]   				=			self.prepare_init_args_and_inputs_for_common()

		lowerCAmelCase       :     Tuple   				=			16
		lowerCAmelCase       :     Dict   				=			(16, 32)

		lowerCAmelCase       :     Dict   				=			self.model_class(**snake_case__     )
		lowerCAmelCase       :     int   				=			model.init(inputs_dict['prng_key']			,       inputs_dict['sample']     )
		jax.lax.stop_gradient(snake_case__     )

		lowerCAmelCase       :     int   				=			model.apply(snake_case__			,       inputs_dict['sample']     )

		if isinstance(snake_case__			,       snake_case__     ):
			lowerCAmelCase       :     Dict   				=			output.sample

		self.assertIsNotNone(snake_case__     )
		lowerCAmelCase       :     Union[str, Any]   				=			inputs_dict['sample'].shape
		self.assertEqual(output.shape			,       snake_case__			,       'Input and output shapes do not match'     )



	def 					lowercase  (       self     ):
		lowerCAmelCase       :     Any   				=			'kwargs' in inspect.signature(self.model_class.__init__     ).parameters
		lowerCAmelCase       :     Optional[Any]   				=			len(self.model_class._deprecated_kwargs     ) > 0

		if has_kwarg_in_model_class and not has_deprecated_kwarg:
			raise ValueError(
			    f"{self.model_class} has `**kwargs` in its __init__ method but has not defined any deprecated kwargs"
			    ' under the `_deprecated_kwargs` class attribute. Make sure to either remove `**kwargs` if there are'
			    ' no deprecated arguments or add the deprecated argument with `_deprecated_kwargs ='
			    ' [<deprecated_argument>]`'     )

		if not has_kwarg_in_model_class and has_deprecated_kwarg:
			raise ValueError(
			    f"{self.model_class} doesn't have `**kwargs` in its __init__ method but has defined deprecated kwargs"
			    ' under the `_deprecated_kwargs` class attribute. Make sure to either add the `**kwargs` argument to'
			    f" {self.model_class}.__init__ if there are deprecated arguments or remove the deprecated argument"
			    ' from `_deprecated_kwargs = [<deprecated_argument>]`'     )
