



import sys
from collections.abc import Mapping
from typing import TYPE_CHECKING

import numpy as np
import pyarrow as pa

from .. import config
from ..utils.py_utils import map_nested
from .formatting import TensorFormatter


if TYPE_CHECKING:
		import tensorflow as tf


class 	lowerCamelCase__ (		TensorFormatter[Mapping, '''tf.Tensor''', Mapping]   ):


						def __init__( self						:  Optional[int]      ,       _a						:  Union[str, Any]=None      ,       **_a						:  int			):
							super().__init__(features=__SCREAMING_SNAKE_CASE			)
							a__:       Optional[Any]											=tf_tensor_kwargs
							import tensorflow as tf  # noqa: import tf at initialization


						def   _lowerCamelCase			( self						:  Optional[Any]      ,       _a						:  Dict			):
							import tensorflow as tf

							if isinstance(__SCREAMING_SNAKE_CASE      ,       __SCREAMING_SNAKE_CASE			) and column:
								if all(
								    isinstance(__SCREAMING_SNAKE_CASE      ,       tf.Tensor			) and x.shape == column[0].shape and x.dtype == column[0].dtype for x in column			):
									return tf.stack(__SCREAMING_SNAKE_CASE			)
								elif all(
								    isinstance(__SCREAMING_SNAKE_CASE      ,       (tf.Tensor, tf.RaggedTensor)			) and x.ndim == 1 and x.dtype == column[0].dtype
								    for x in column			):
									# only rag 1-D tensors, otherwise some dimensions become ragged even though they were consolidated
									return tf.ragged.stack(__SCREAMING_SNAKE_CASE			)

							return column


						def   _lowerCamelCase			( self						:  int      ,       _a						:  Optional[Any]			):
							import tensorflow as tf

							if value is None:
								return value

							a__:       int											={}

							if isinstance(__SCREAMING_SNAKE_CASE      ,       (np.number, np.ndarray)			) and np.issubdtype(value.dtype      ,       np.integer			):
								a__:       Union[str, Any]											={"dtype": tf.intaa}
							elif isinstance(__SCREAMING_SNAKE_CASE      ,       (np.number, np.ndarray)			) and np.issubdtype(value.dtype      ,       np.floating			):
								a__:       Union[str, Any]											={"dtype": tf.floataa}
							elif config.PIL_AVAILABLE and "PIL" in sys.modules:
								import PIL.Image

								if isinstance(__SCREAMING_SNAKE_CASE      ,       PIL.Image.Image			):
									a__:       List[Any]											=np.asarray(__SCREAMING_SNAKE_CASE			)

							return tf.convert_to_tensor(__SCREAMING_SNAKE_CASE      ,       **{**default_dtype, **self.tf_tensor_kwargs}			)


						def   _lowerCamelCase			( self						:  Union[str, Any]      ,       _a						:  List[Any]			):
							import tensorflow as tf

							# support for torch, tf, jax etc.
							if config.TORCH_AVAILABLE and "torch" in sys.modules:
								import torch

								if isinstance(__SCREAMING_SNAKE_CASE      ,       torch.Tensor			):
									return self._tensorize(data_struct.detach().cpu().numpy()[()]			)
							if hasattr(__SCREAMING_SNAKE_CASE      ,       "__array__"			) and not isinstance(__SCREAMING_SNAKE_CASE      ,       tf.Tensor			):
								a__:       List[str]											=data_struct.__array__()
							# support for nested types like struct of list of struct
							if isinstance(__SCREAMING_SNAKE_CASE      ,       np.ndarray			):
								if data_struct.dtype == object:  # tf tensors cannot be instantied from an array of objects
									return self._consolidate([self.recursive_tensorize(__SCREAMING_SNAKE_CASE			) for substruct in data_struct]			)
							elif isinstance(__SCREAMING_SNAKE_CASE      ,       (list, tuple)			):
								return self._consolidate([self.recursive_tensorize(__SCREAMING_SNAKE_CASE			) for substruct in data_struct]			)
							return self._tensorize(__SCREAMING_SNAKE_CASE			)


						def   _lowerCamelCase			( self						:  Any      ,       _a						:  str			):
							return map_nested(self._recursive_tensorize      ,       __SCREAMING_SNAKE_CASE      ,       map_list=__SCREAMING_SNAKE_CASE			)


						def   _lowerCamelCase			( self						:  Dict      ,       _a						:  Optional[int]			):
							a__:       List[str]											=self.numpy_arrow_extractor().extract_row(__SCREAMING_SNAKE_CASE			)
							a__:       Union[str, Any]											=self.python_features_decoder.decode_row(__SCREAMING_SNAKE_CASE			)
							return self.recursive_tensorize(__SCREAMING_SNAKE_CASE			)


						def   _lowerCamelCase			( self						:  Any      ,       _a						:  Union[str, Any]			):
							a__:       Optional[int]											=self.numpy_arrow_extractor().extract_column(__SCREAMING_SNAKE_CASE			)
							a__:       List[str]											=self.python_features_decoder.decode_column(__SCREAMING_SNAKE_CASE      ,       pa_table.column_names[0]			)
							a__:       int											=self.recursive_tensorize(__SCREAMING_SNAKE_CASE			)
							a__:       List[Any]											=self._consolidate(__SCREAMING_SNAKE_CASE			)
							return column







						def   _lowerCamelCase			( self						:  str      ,       _a						:  Tuple			):
							a__:       List[str]											=self.numpy_arrow_extractor().extract_batch(__SCREAMING_SNAKE_CASE			)
							a__:       Any											=self.python_features_decoder.decode_batch(__SCREAMING_SNAKE_CASE			)
							a__:       Union[str, Any]											=self.recursive_tensorize(__SCREAMING_SNAKE_CASE			)
							for column_name in batch:
								a__:       List[Any]											=self._consolidate(batch[column_name]			)
							return batch










import json
import os
from dataclasses import dataclass

import numpy as np
import pyarrow as pa

import datasets
from utils import get_duration


__UpperCAmelCase						  =     10_00_00_00_00_00
__UpperCAmelCase						  =     1_00_00

__UpperCAmelCase  ,							__UpperCAmelCase						  =     os.path.split(__file__)
__UpperCAmelCase						  =     os.path.join(RESULTS_BASEPATH, '''results''', RESULTS_FILENAME.replace('''.py''', '''.json'''))





def 						__lowerCamelCase       (      __magic_name__	:  int     , __magic_name__	:  int						):
							a__:       Any											=pa.Table.from_pydict({"col": [0] * chunk_size}						)
							a__:       Optional[Any]											=pa.concat_tables([table] * (num_examples // chunk_size)						)
							return datasets.Dataset(__magic_name__     , fingerprint="table_100B"						)


@dataclass
class 	lowerCamelCase__ :
						_lowerCAmelCase								= 42
						_lowerCAmelCase								= 42
						_lowerCAmelCase								= 42
						_lowerCAmelCase								= 42


						def   _lowerCamelCase			( self						:  Union[str, Any]			):
													a__:       Tuple											=np.random.default_rng(self.seed			)
													a__:       Tuple											=rng.integers(low=self.low      ,       high=self.high      ,       size=self.size			).tolist()


						def __iter__( self						:  Any			):
													return iter(self._sampled_values			)







						def __len__( self						:  Any			):
													return self.size





@get_duration
def 						__lowerCamelCase       (      __magic_name__	:  datasets.Dataset						):
							a__:       Union[str, Any]											=dataset[0]





@get_duration
def 						__lowerCamelCase       (      __magic_name__	:  datasets.Dataset						):
							a__:       Optional[Any]											=dataset[-1]





@get_duration
def 						__lowerCamelCase       (      __magic_name__	:  datasets.Dataset						):
							a__:       List[str]											=dataset[range(len(__magic_name__						) // 2     , len(__magic_name__						) // 2 + 1_024						)]





@get_duration
def 						__lowerCamelCase       (      __magic_name__	:  datasets.Dataset						):
							a__:       Optional[int]											=dataset[RandIter(0     , len(__magic_name__						)     , 1_024     , seed=42						)]





def 						__lowerCamelCase       (      ):
							a__:       Dict											={"num examples": SPEED_TEST_N_EXAMPLES}
							a__:       Union[str, Any]											=(get_first_row, get_last_row, get_batch_of_aaaa_rows, get_batch_of_aaaa_random_rows)
							print("generating dataset"						)
							a__:       Dict											=generate_100B_dataset(num_examples=__magic_name__     , chunk_size=__magic_name__						)
							print("Functions"						)
							for func in functions:
														print(func.__name__						)
														a__:       Dict											=func(__magic_name__						)

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


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





