from typing import List, Optional, Tuple, Union

from . import logging
from .import_utils import is_torch_available, is_torch_version


if is_torch_available():
							import torch

_lowerCamelCase    :      List[str]	 =						logging.get_logger(__name__)  # pylint: disable=invalid-name

try:
							from torch._dynamo import allow_in_graph as maybe_allow_in_graph
except (ImportError, ModuleNotFoundError):





							def 		__lowerCamelCase							(cls   :							Dict     ):
												return cls





def 		__lowerCamelCase							(UpperCAmelCase__   :							Union[Tuple, List]					,       UpperCAmelCase__   :							Optional[Union[List["torch.Generator"], "torch.Generator"]] = None					,       UpperCAmelCase__   :							Optional["torch.device"] = None					,       UpperCAmelCase__   :							Optional["torch.dtype"] = None					,       UpperCAmelCase__   :							Optional["torch.layout"] = None					,       ):
					SCREAMING_SNAKE_CASE				=				device
					SCREAMING_SNAKE_CASE				=				shape[0]

					SCREAMING_SNAKE_CASE				=				layout or torch.strided
					SCREAMING_SNAKE_CASE				=				device or torch.device("cpu"     )

					if generator is not None:
										SCREAMING_SNAKE_CASE				=				generator.device.type if not isinstance(UpperCAmelCase__					,       UpperCAmelCase__     ) else generator[0].device.type
										if gen_device_type != device.type and gen_device_type == "cpu":
															SCREAMING_SNAKE_CASE				=				"cpu"
															if device != "mps":
																				logger.info(
																				    F"The passed generator was created on 'cpu' even though a tensor on {device} was expected."
																				    F" Tensors will be created on 'cpu' and then moved to {device}. Note that one can probably"
																				    F" slighly speed up this function by passing a generator that was created on the {device} device."     )
										elif gen_device_type != device.type and gen_device_type == "cuda":
															raise ValueError(F"Cannot generate a {device} tensor from a generator of type {gen_device_type}."     )

					if isinstance(UpperCAmelCase__					,       UpperCAmelCase__     ):
										SCREAMING_SNAKE_CASE				=				(1,) + shape[1:]
										SCREAMING_SNAKE_CASE				=				[
										    torch.randn(UpperCAmelCase__					,       generator=generator[i]					,       device=UpperCAmelCase__					,       dtype=UpperCAmelCase__					,       layout=UpperCAmelCase__     )
										    for i in range(UpperCAmelCase__     )
										]
										SCREAMING_SNAKE_CASE				=				torch.cat(UpperCAmelCase__					,       dim=0     ).to(UpperCAmelCase__     )
					else:
										SCREAMING_SNAKE_CASE				=				torch.randn(UpperCAmelCase__					,       generator=UpperCAmelCase__					,       device=UpperCAmelCase__					,       dtype=UpperCAmelCase__					,       layout=UpperCAmelCase__     ).to(UpperCAmelCase__     )

					return latents





def 		__lowerCamelCase							(UpperCAmelCase__   :							Optional[Any]     ):
					if is_torch_version("<"					,       "2.0.0"     ) or not hasattr(UpperCAmelCase__					,       "_dynamo"     ):
										return False
					return isinstance(UpperCAmelCase__					,       torch._dynamo.eval_frame.OptimizedModule     )





import json
import os
from dataclasses import dataclass

import numpy as np
import pyarrow as pa

import datasets
from utils import get_duration


_lowerCamelCase    :      int	 =						10_00_00_00_00_00
_lowerCamelCase    :      Union[str, Any]	 =						1_00_00

_lowerCamelCase ,  _lowerCamelCase    :      List[str]	 =						os.path.split(__file__)
_lowerCamelCase    :      Any	 =						os.path.join(RESULTS_BASEPATH, '''results''', RESULTS_FILENAME.replace('''.py''', '''.json'''))





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


@dataclass
class   lowercase			:
							lowercase__  :				int
							lowercase__  :				int
							lowercase__  :				int
							lowercase__  :				int




							def 		__snake_case(  self      :    Tuple					)    -> Dict:



												'''simple docstring'''

												SCREAMING_SNAKE_CASE				=				np.random.default_rng(self.seed					)
												SCREAMING_SNAKE_CASE				=				rng.integers(low=self.low		,							high=self.high		,							size=self.size					).tolist()




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



												'''simple docstring'''

												return iter(self._sampled_values					)





							def __len__(  self      :    Any					)    -> int:



												'''simple docstring'''

												return self.size





@get_duration
def 		__lowerCamelCase							(UpperCAmelCase__   :							datasets.Dataset     ):
					SCREAMING_SNAKE_CASE				=				dataset[0]





@get_duration
def 		__lowerCamelCase							(UpperCAmelCase__   :							datasets.Dataset     ):
					SCREAMING_SNAKE_CASE				=				dataset[-1]





@get_duration
def 		__lowerCamelCase							(UpperCAmelCase__   :							datasets.Dataset     ):
					SCREAMING_SNAKE_CASE				=				dataset[range(len(UpperCAmelCase__     ) // 2					,       len(UpperCAmelCase__     ) // 2 + 1_0_2_4     )]





@get_duration
def 		__lowerCamelCase							(UpperCAmelCase__   :							datasets.Dataset     ):
					SCREAMING_SNAKE_CASE				=				dataset[RandIter(0					,       len(UpperCAmelCase__     )					,       1_0_2_4					,       seed=4_2     )]





def 		__lowerCamelCase							():
					SCREAMING_SNAKE_CASE				=				{"num examples": SPEED_TEST_N_EXAMPLES}
					SCREAMING_SNAKE_CASE				=				(get_first_row, get_last_row, get_batch_of_aaaa_rows, get_batch_of_aaaa_random_rows)
					print("generating dataset"     )
					SCREAMING_SNAKE_CASE				=				generate_100B_dataset(num_examples=UpperCAmelCase__					,       chunk_size=UpperCAmelCase__     )
					print("Functions"     )
					for func in functions:
										print(func.__name__     )
										SCREAMING_SNAKE_CASE				=				func(UpperCAmelCase__     )

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


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




