




'''simple docstring'''





import torch

from accelerate import Accelerator, DistributedType

class      _a       :






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



          if self.enabled:
               _snake_case							=   self.model.no_sync()
               self.model_sync_obj.__enter__()

          return self






     def __exit__(				self      ,_SCREAMING_SNAKE_CASE      ,_SCREAMING_SNAKE_CASE      ,_SCREAMING_SNAKE_CASE					)							->    int:



          if self.enabled:
               # Average all models on exit
               self._sync_and_avg_model_params()
               self.model_sync_obj.__exit__(lowerCamelCase__      ,lowerCamelCase__      ,lowerCamelCase__					)






     def __init__(				self      ,_SCREAMING_SNAKE_CASE      ,_SCREAMING_SNAKE_CASE      ,_SCREAMING_SNAKE_CASE      ,_SCREAMING_SNAKE_CASE = True					)							->    int:



          if accelerator.distributed_type not in [
              DistributedType.NO,
              DistributedType.MULTI_CPU,
              DistributedType.MULTI_GPU,
          ]:
               raise NotImplementedError("LocalSGD is supported only for CPUs and GPUs (no DeepSpeed or MegatronLM)"					)
          _snake_case							=   enabled and accelerator.distributed_type != DistributedType.NO
          _snake_case							=   0
          if self.enabled:
               _snake_case							=   accelerator
               _snake_case							=   model
               _snake_case							=   local_sgd_steps






     def        _lowercase			(				self					)							->    Any:
          self.num_steps += 1
          if not self.enabled:
               return

          if self.num_steps % self.local_sgd_steps == 0:
               self._sync_and_avg_model_params()






     def        _lowercase			(				self					)							->    Optional[int]:
          self.accelerator.wait_for_everyone()
          with self.accelerator.autocast():
               for param in self.model.parameters():
                    _snake_case							=   self.accelerator.reduce(param.data      ,reduction="mean"					)








'''simple docstring'''





import json
import os
import tempfile

import datasets
from utils import generate_example_dataset, get_duration


UpperCamelCase_		:    List[str]									=		500000

UpperCamelCase_			,			UpperCamelCase_		:    Dict									=		os.path.split(__file__)
UpperCamelCase_		:    int									=		os.path.join(RESULTS_BASEPATH, '''results''', RESULTS_FILENAME.replace('''.py''', '''.json'''))





@get_duration
def 			__a			(     _UpperCamelCase:					datasets.Dataset					)		->				List[Any]:

     """simple docstring"""




     _snake_case							=   dataset.select(range(0      ,		len(_UpperCamelCase					)      ,		2					)					)





@get_duration
def 			__a			(     _UpperCamelCase:					datasets.Dataset					)		->				List[Any]:

     """simple docstring"""




     _snake_case							=   dataset.sort("numbers"					)





@get_duration
def 			__a			(     _UpperCamelCase:					datasets.Dataset					)		->				List[Any]:

     """simple docstring"""




     _snake_case							=   dataset.shuffle()





@get_duration
def 			__a			(     _UpperCamelCase:					datasets.Dataset					)		->				Any:

     """simple docstring"""




     _snake_case							=   dataset.train_test_split(0.1					)





@get_duration
def 			__a			(     _UpperCamelCase:					datasets.Dataset      ,		_UpperCamelCase:					int=10					)		->				List[str]:

     """simple docstring"""




     for shard_id in range(_UpperCamelCase					):
          _snake_case							=   dataset.shard(_UpperCamelCase      ,		_UpperCamelCase					)





def 			__a			(     )		->				int:

     """simple docstring"""




     _snake_case							=   {"num examples": SPEED_TEST_N_EXAMPLES}
     _snake_case							=   (select, sort, shuffle, train_test_split, shard)
     with tempfile.TemporaryDirectory() as tmp_dir:
          print("generating dataset"					)
          _snake_case							=   datasets.Features({"text": datasets.Value("string"					), "numbers": datasets.Value("float32"					)}					)
          _snake_case							=   generate_example_dataset(
              os.path.join(_UpperCamelCase      ,		"dataset.arrow"					)      ,		_UpperCamelCase      ,		num_examples=_UpperCamelCase					)
          print("Functions"					)
          for func in functions:
               print(func.__name__					)
               _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_indices_mapping()


