


import inspect
import warnings

import torch

from .state import AcceleratorState, GradientState
from .utils import DistributedType, honor_type, is_tpu_available


if is_tpu_available(check_device=False):
    import torch_xla.core.xla_model as xm

def  lowercase_						(						_A     :       Union[str, Any]       ,   _A     :       List[Any]		):

      """simple docstring"""




      if isinstance(_A       ,   (list, tuple)		):
            return honor_type(_A       ,   (move_to_device(_A       ,   _A		) for t in state)		)
      elif isinstance(_A       ,   _A		):
            return type(_A		)({k: move_to_device(_A       ,   _A		) for k, v in state.items()}		)
      elif isinstance(_A       ,   torch.Tensor		):
            return state.to(_A		)
      return state







class      _lowercase					(				torch.optim.Optimizer):


       """simple docstring"""




       def __init__(  self				:     str   , __lowerCamelCase				:     Optional[int]   , __lowerCamelCase				:     List[Any]=True   , __lowerCamelCase				:     Dict=None			):


             '''simple docstring'''



             lowerCamelCase__						:							int       =    optimizer
             lowerCamelCase__						:							Optional[Any]       =    scaler
             lowerCamelCase__						:							Any       =    AcceleratorState()
             lowerCamelCase__						:							str       =    GradientState()
             lowerCamelCase__						:							Tuple       =    device_placement
             lowerCamelCase__						:							List[str]       =    False
             lowerCamelCase__						:							Union[str, Any]       =    None

             # Handle device placement
             if device_placement:
                   lowerCamelCase__						:							Any       =    self.optimizer.state_dict()
                   if self.accelerator_state.distributed_type == DistributedType.TPU:
                         xm.send_cpu_data_to_device(__lowerCamelCase   , self.accelerator_state.device			)
                   else:
                         lowerCamelCase__						:							str       =    move_to_device(__lowerCamelCase   , self.accelerator_state.device			)
                   self.optimizer.load_state_dict(__lowerCamelCase			)





       @property
       def 		lowerCAmelCase	(  self				:     str			):


             '''simple docstring'''



             return self.optimizer.state





       @state.setter
       def 		lowerCAmelCase	(  self				:     str   , __lowerCamelCase				:     List[str]			):


             '''simple docstring'''



             lowerCamelCase__						:							List[str]       =    state





       @property
       def 		lowerCAmelCase	(  self				:     Union[str, Any]			):


             '''simple docstring'''



             return self.optimizer.param_groups





       @param_groups.setter
       def 		lowerCAmelCase	(  self				:     Tuple   , __lowerCamelCase				:     Dict			):


             '''simple docstring'''



             lowerCamelCase__						:							Any       =    param_groups





       @property
       def 		lowerCAmelCase	(  self				:     List[str]			):


             '''simple docstring'''



             return self.optimizer.defaults





       @defaults.setter
       def 		lowerCAmelCase	(  self				:     Tuple   , __lowerCamelCase				:     List[Any]			):


             '''simple docstring'''



             lowerCamelCase__						:							Dict       =    defaults





       def 		lowerCAmelCase	(  self				:     List[Any]   , __lowerCamelCase				:     Optional[int]			):


             '''simple docstring'''



             self.optimizer.add_param_group(__lowerCamelCase			)





       def 		lowerCAmelCase	(  self				:     Optional[int]   , __lowerCamelCase				:     List[str]			):


             '''simple docstring'''



             if self.accelerator_state.distributed_type == DistributedType.TPU and self.device_placement:
                   xm.send_cpu_data_to_device(__lowerCamelCase   , self.accelerator_state.device			)
             self.optimizer.load_state_dict(__lowerCamelCase			)





       def 		lowerCAmelCase	(  self				:     int			):


             '''simple docstring'''



             return self.optimizer.state_dict()





       def 		lowerCAmelCase	(  self				:     List[Any]   , __lowerCamelCase				:     Union[str, Any]=None			):


             '''simple docstring'''



             if self.gradient_state.sync_gradients:
                   lowerCamelCase__						:							Optional[Any]       =    "set_to_none" in inspect.signature(self.optimizer.zero_grad			).parameters
                   if accept_arg:
                         if set_to_none is None:
                               lowerCamelCase__						:							Union[str, Any]       =    False
                         self.optimizer.zero_grad(set_to_none=__lowerCamelCase			)
                   else:
                         if set_to_none is not None:
                               raise ValueError("`set_to_none` for Optimizer.zero_grad` is not supported by this optimizer."			)
                         self.optimizer.zero_grad()





       def 		lowerCAmelCase	(  self				:     List[Any]   , __lowerCamelCase				:     List[Any]=None			):


             '''simple docstring'''



             if self.gradient_state.sync_gradients:
                   if self.accelerator_state.distributed_type == DistributedType.TPU:
                         lowerCamelCase__						:							List[str]       =    {"closure": closure} if closure is not None else {}
                         xm.optimizer_step(self.optimizer   , optimizer_args=__lowerCamelCase			)
                   elif self.scaler is not None:
                         lowerCamelCase__						:							List[Any]       =    False
                         if self._last_scale is None:
                               # `get_scale` is an async operation requiring full synchronization
                               # on CPU and GPUs before finishing. As a result, we store away
                               # the prior one to reduce the call overhead
                               lowerCamelCase__						:							Union[str, Any]       =    self.scaler.get_scale()
                               lowerCamelCase__						:							Tuple       =    True
                         self.scaler.step(self.optimizer   , __lowerCamelCase			)
                         self.scaler.update()
                         lowerCamelCase__						:							List[Any]       =    self.scaler.get_scale()
                         if not new_scale:
                               # If we reduced the loss scale, it means the optimizer step was skipped because of gradient overflow.
                               lowerCamelCase__						:							Optional[int]       =    scale_after < self._last_scale
                         lowerCamelCase__						:							List[str]       =    scale_after

                   else:
                         self.optimizer.step(__lowerCamelCase			)





       def 		lowerCAmelCase	(  self				:     int   , __lowerCamelCase				:     Union[str, Any]			):


             '''simple docstring'''



             for param_group in self.optimizer.param_groups:
                   lowerCamelCase__						:							int       =    [parameters_map.get(__lowerCamelCase   , __lowerCamelCase			) for p in param_group["params"]]





       @property
       def 		lowerCAmelCase	(  self				:     List[str]			):


             '''simple docstring'''



             warnings.warn(
                 "The `is_overflow` property is deprecated and will be removed in version 1.0 of Accelerate use "
                 "`optimizer.step_was_skipped` instead."   , __lowerCamelCase   , )
             return self._is_overflow





       @property
       def 		lowerCAmelCase	(  self				:     int			):


             '''simple docstring'''



             return self._is_overflow





       def __getstate__(  self				:     str			):


             '''simple docstring'''



             return self.__dict__.copy()





       def __setstate__(  self				:     Any   , __lowerCamelCase				:     List[Any]			):


             '''simple docstring'''



             self.__dict__.update(__lowerCamelCase			)






import re
import tempfile
import unittest
from pathlib import Path

from datasets.utils.metadata import DatasetMetadata

def  lowercase_						(						_A     :       str		):

      """simple docstring"""




      lowerCamelCase__						:							Optional[Any]       =    min(re.search("^ +"       ,   _A		).end() if t.startswith(" "		) else 0 for t in string.splitlines()		)
      return "\n".join([line[indent_level:] for line in string.splitlines() if indent_level < len(_A		)]		)


A :       Optional[int]						=   "\\n---\nlanguage:\n- zh\n- en\ntask_ids:\n- sentiment-classification\n---\n# Begin of markdown\n\nSome cool dataset card\n"

A :       str						=   "\\n---\n---\n# Begin of markdown\n\nSome cool dataset card\n"


A :       Optional[Any]						=   "\\n# Begin of markdown\n\nSome cool dataset card\n"







class      _lowercase					(				unittest.TestCase):


       """simple docstring"""




       def 		lowerCAmelCase	(  self				:     List[Any]			):


             '''simple docstring'''



             with tempfile.TemporaryDirectory() as tmp_dir:
                   lowerCamelCase__						:							List[str]       =    Path(__lowerCamelCase			) / "README.md"
                   with open(__lowerCamelCase   , "w+"			) as readme_file:
                         readme_file.write(__lowerCamelCase			)
                   lowerCamelCase__						:							Dict       =    DatasetMetadata.from_readme(__lowerCamelCase			)
                   self.assertDictEqual(__lowerCamelCase   , {"language": ["zh", "en"], "task_ids": ["sentiment-classification"]}			)

                   with open(__lowerCamelCase   , "w+"			) as readme_file:
                         readme_file.write(__lowerCamelCase			)
                   lowerCamelCase__						:							Optional[Any]       =    DatasetMetadata.from_readme(__lowerCamelCase			)
                   self.assertDictEqual(__lowerCamelCase   , {}			)

                   with open(__lowerCamelCase   , "w+"			) as readme_file:
                         readme_file.write(__lowerCamelCase			)
                   lowerCamelCase__						:							int       =    DatasetMetadata.from_readme(__lowerCamelCase			)
                   self.assertEqual(__lowerCamelCase   , {}			)





       def 		lowerCAmelCase	(  self				:     Any			):


             '''simple docstring'''



             lowerCamelCase__						:							List[str]       =    _dedent(
                 "\\n            annotations_creators:\n            - found\n            language_creators:\n            - found\n            language:\n            - en\n            license:\n            - unknown\n            multilinguality:\n            - monolingual\n            pretty_name: Test Dataset\n            size_categories:\n            - 10K<n<100K\n            source_datasets:\n            - extended|other-yahoo-webscope-l6\n            task_categories:\n            - question-answering\n            task_ids:\n            - open-domain-qa\n            "			)
             assert DatasetMetadata.from_yaml_string(__lowerCamelCase			)

             lowerCamelCase__						:							Dict       =    _dedent(
                 "\\n            annotations_creators:\n            - found\n            language:\n            - en\n            license:\n            - unknown\n            multilinguality:\n            - monolingual\n            pretty_name: Test Dataset\n            size_categories:\n            - 10K<n<100K\n            source_datasets:\n            - extended|other-yahoo-webscope-l6\n            task_categories:\n            - question-answering\n            task_ids:\n            - open-domain-qa\n            task_ids:\n            - open-domain-qa\n            "			)
             with self.assertRaises(__lowerCamelCase			):
                   DatasetMetadata.from_yaml_string(__lowerCamelCase			)

             lowerCamelCase__						:							Optional[Any]       =    _dedent(
                 "\\n            annotations_creators:\n            - found\n            language_creators:\n            - found\n            language:\n            - en\n            license:\n            - unknown\n            multilinguality:\n            - monolingual\n            pretty_name: Test Dataset\n            size_categories:\n            - 10K<n<100K\n            source_datasets:\n            - extended|other-yahoo-webscope-l6\n            task_categories:\n            - text-classification\n            task_ids:\n            - multi-class-classification\n            paperswithcode_id:\n            - squad\n            configs:\n            - en\n            train-eval-index:\n            - config: en\n              task: text-classification\n              task_id: multi_class_classification\n              splits:\n                train_split: train\n                eval_split: test\n              col_mapping:\n                text: text\n                label: target\n              metrics:\n                - type: accuracy\n                  name: Accuracy\n            extra_gated_prompt: |\n              By clicking on “Access repository” below, you also agree to ImageNet Terms of Access:\n              [RESEARCHER_FULLNAME] (the \"Researcher\") has requested permission to use the ImageNet database (the \"Database\") at Princeton University and Stanford University. In exchange for such permission, Researcher hereby agrees to the following terms and conditions:\n              1. Researcher shall use the Database only for non-commercial research and educational purposes.\n            extra_gated_fields:\n              Company: text\n              Country: text\n              I agree to use this model for non-commerical use ONLY: checkbox\n            "			)
             assert DatasetMetadata.from_yaml_string(__lowerCamelCase			)


