





'''simple docstring'''

# Copyright 2021 The HuggingFace Team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import argparse

from ...utils.dataclasses import (
    ComputeEnvironment,
    DistributedType,
    DynamoBackend,
    PrecisionType,
    SageMakerDistributedType,
)
from ..menu import BulletMenu


__lowerCAmelCase				:      Optional[Any]						      =[
    "EAGER",
    "AOT_EAGER",
    "INDUCTOR",
    "NVFUSER",
    "AOT_NVFUSER",
    "AOT_CUDAGRAPHS",
    "OFI",
    "FX2TRT",
    "ONNXRT",
    "IPEX",
]





def 		UpperCamelCase							(				_lowerCamelCase   :   Tuple		,			_lowerCamelCase   :   Union[str, Any]=None		,			_lowerCamelCase   :   Any=None		,			_lowerCamelCase   :   List[str]=None			):
    A__							=     True
    while ask_again:
        A__							=     input(_lowerCamelCase			)
        try:
            if default is not None and len(_lowerCamelCase			) == 0:
                return default
            return convert_value(_lowerCamelCase			) if convert_value is not None else result
        except Exception:
            if error_message is not None:
                print(_lowerCamelCase			)





def 		UpperCamelCase							(				_lowerCamelCase   :   List[str]		,			_lowerCamelCase   :   Any=[]		,			_lowerCamelCase   :   Any=None		,			_lowerCamelCase   :   str=0			):
    A__							=     BulletMenu(_lowerCamelCase		,			_lowerCamelCase			)
    A__							=     menu.run(default_choice=_lowerCamelCase			)
    return convert_value(_lowerCamelCase			) if convert_value is not None else result





def 		UpperCamelCase							(				_lowerCamelCase   :   List[Any]			):
    A__							=     int(_lowerCamelCase			)
    return ComputeEnvironment(["LOCAL_MACHINE", "AMAZON_SAGEMAKER"][value]			)





def 		UpperCamelCase							(				_lowerCamelCase   :   Optional[int]			):
    A__							=     int(_lowerCamelCase			)
    return DistributedType(["NO", "MULTI_CPU", "MULTI_XPU", "MULTI_GPU", "MULTI_NPU", "TPU"][value]			)





def 		UpperCamelCase							(				_lowerCamelCase   :   Any			):
    A__							=     int(_lowerCamelCase			)
    return DynamoBackend(DYNAMO_BACKENDS[value]			).value





def 		UpperCamelCase							(				_lowerCamelCase   :   Tuple			):
    A__							=     int(_lowerCamelCase			)
    return PrecisionType(["no", "fp16", "bf16", "fp8"][value]			)





def 		UpperCamelCase							(				_lowerCamelCase   :   Optional[Any]			):
    A__							=     int(_lowerCamelCase			)
    return SageMakerDistributedType(["NO", "DATA_PARALLEL", "MODEL_PARALLEL"][value]			)





def 		UpperCamelCase							(				_lowerCamelCase   :   Union[str, Any]			):
    return {"yes": True, "no": False}[value.lower()]






class   UpperCAmelCase      (			argparse.RawDescriptionHelpFormatter							):
 def 	UpperCAmelCase_				(							self :Optional[Any]		,			lowercase_ :Dict		,			lowercase_ :Dict		,			lowercase_ :Optional[int]		,			lowercase_ :Tuple				)->				Any:
     A__							=     super()._format_usage(lowercase_		,			lowercase_		,			lowercase_		,			lowercase_				)
     A__							=     usage.replace("<command> [<args>] "		,			""				)
     return usage










'''simple docstring'''

import json
import os
import tempfile

import datasets
from utils import generate_example_dataset, get_duration


__lowerCAmelCase				:      int						      =5_0000
__lowerCAmelCase				:      Union[str, Any]						      =5000

__lowerCAmelCase      ,	__lowerCAmelCase				:      Tuple						      =os.path.split(__file__)
__lowerCAmelCase				:      int						      =os.path.join(RESULTS_BASEPATH, "results", RESULTS_FILENAME.replace(".py", ".json"))





@get_duration
def 		UpperCamelCase							(				_lowerCamelCase   :   datasets.Dataset		,			_lowerCamelCase   :   int			):
    for i in range(_lowerCamelCase			):
        A__							=     dataset[i]





@get_duration
def 		UpperCamelCase							(				_lowerCamelCase   :   datasets.Dataset		,			_lowerCamelCase   :   Optional[int]		,			_lowerCamelCase   :   Any			):
    for i in range(0		,			len(_lowerCamelCase			)		,			_lowerCamelCase			):
        A__							=     dataset[i : i + batch_size]





@get_duration
def 		UpperCamelCase							(				_lowerCamelCase   :   datasets.Dataset		,			_lowerCamelCase   :   Optional[Any]		,			_lowerCamelCase   :   List[str]			):
    with dataset.formatted_as(type=_lowerCamelCase			):
        for i in range(_lowerCamelCase			):
            A__							=     dataset[i]





@get_duration
def 		UpperCamelCase							(				_lowerCamelCase   :   datasets.Dataset		,			_lowerCamelCase   :   Optional[int]		,			_lowerCamelCase   :   int		,			_lowerCamelCase   :   Dict			):
    with dataset.formatted_as(type=_lowerCamelCase			):
        for i in range(0		,			_lowerCamelCase		,			_lowerCamelCase			):
            A__							=     dataset[i : i + batch_size]





def 		UpperCamelCase							(				):
    A__							=     {"num examples": SPEED_TEST_N_EXAMPLES}
    A__							=     [
        (read, {"length": SMALL_TEST}),
        (read, {"length": SPEED_TEST_N_EXAMPLES}),
        (read_batch, {"length": SPEED_TEST_N_EXAMPLES, "batch_size": 10}),
        (read_batch, {"length": SPEED_TEST_N_EXAMPLES, "batch_size": 1_00}),
        (read_batch, {"length": SPEED_TEST_N_EXAMPLES, "batch_size": 10_00}),
        (read_formatted, {"type": "numpy", "length": SMALL_TEST}),
        (read_formatted, {"type": "pandas", "length": SMALL_TEST}),
        (read_formatted, {"type": "torch", "length": SMALL_TEST}),
        (read_formatted, {"type": "tensorflow", "length": SMALL_TEST}),
        (read_formatted_batch, {"type": "numpy", "length": SMALL_TEST, "batch_size": 10}),
        (read_formatted_batch, {"type": "numpy", "length": SMALL_TEST, "batch_size": 10_00}),
    ]

    A__							=     [
        (read, {"length": SMALL_TEST}),
        (read, {"length": SPEED_TEST_N_EXAMPLES}),
        (read_batch, {"length": SPEED_TEST_N_EXAMPLES, "batch_size": 10}),
        (read_batch, {"length": SPEED_TEST_N_EXAMPLES, "batch_size": 1_00}),
        (read_batch, {"length": SPEED_TEST_N_EXAMPLES, "batch_size": 10_00}),
        (read_formatted, {"type": "numpy", "length": SMALL_TEST}),
        (read_formatted_batch, {"type": "numpy", "length": SMALL_TEST, "batch_size": 10}),
        (read_formatted_batch, {"type": "numpy", "length": SMALL_TEST, "batch_size": 10_00}),
    ]
    with tempfile.TemporaryDirectory() as tmp_dir:
        print("generating dataset"			)
        A__							=     datasets.Features(
            {"list": datasets.Sequence(datasets.Value("float32"			)			), "numbers": datasets.Value("float32"			)}			)
        A__							=     generate_example_dataset(
            os.path.join(_lowerCamelCase		,			"dataset.arrow"			)		,			_lowerCamelCase		,			num_examples=_lowerCamelCase		,			seq_shapes={"list": (1_00,)}		,			)
        print("first set of iterations"			)
        for func, kwargs in functions:
            print(func.__name__		,			str(_lowerCamelCase			)			)
            A__							=     func(_lowerCamelCase		,			**_lowerCamelCase			)

        print("shuffling dataset"			)
        A__							=     dataset.shuffle()
        print("Second set of iterations (after shuffling"			)
        for func, kwargs in functions_shuffled:
            print("shuffled "		,			func.__name__		,			str(_lowerCamelCase			)			)
            A__							=     func(
                _lowerCamelCase		,			**_lowerCamelCase			)

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


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



