
"""simple docstring"""



import inspect
import os

import torch
from transformers import AutoModel
from transformers.testing_utils import mockenv_context
from transformers.trainer_utils import set_seed

import accelerate
from accelerate.accelerator import Accelerator
from accelerate.state import AcceleratorState
from accelerate.test_utils.testing import (
    AccelerateTestCase,
    TempDirTestCase,
    execute_subprocess_async,
    require_cuda,
    require_fsdp,
    require_multi_gpu,
    slow,
)
from accelerate.utils.constants import (
    FSDP_AUTO_WRAP_POLICY,
    FSDP_BACKWARD_PREFETCH,
    FSDP_SHARDING_STRATEGY,
    FSDP_STATE_DICT_TYPE,
)
from accelerate.utils.dataclasses import FullyShardedDataParallelPlugin
from accelerate.utils.other import patch_environment


set_seed(4_2)

_snake_case        =    '''bert-base-cased'''
_snake_case        =    '''fp16'''
_snake_case        =    '''bf16'''
_snake_case        =    [FPaa, BFaa]
@require_fsdp
@require_cuda
class 							_SCREAMING_SNAKE_CASE	(	UpperCAmelCase				):






				'''simple docstring'''



				def 					__lowerCamelCase						(       self						:      int	)		->	Optional[Any]:







											"""simple docstring"""




											super().setUp()

											_lowerCAmelCase							=      dict(
											    ACCELERATE_USE_FSDP='true'					,  MASTER_ADDR='localhost'					,  MASTER_PORT='10999'					,  RANK='0'					,  LOCAL_RANK='0'					,  WORLD_SIZE='1'					,  )


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







											"""simple docstring"""




											from torch.distributed.fsdp.fully_sharded_data_parallel import ShardingStrategy

											for i, strategy in enumerate(UpperCAmelCase_	):
																		_lowerCAmelCase							=      self.dist_env.copy()
																		_lowerCAmelCase							=      F"""{i + 1}"""
																		_lowerCAmelCase							=      strategy
																		with mockenv_context(**UpperCAmelCase_	):
																									_lowerCAmelCase							=      FullyShardedDataParallelPlugin()
																									self.assertEqual(fsdp_plugin.sharding_strategy					,  ShardingStrategy(i + 1	)	)


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







											"""simple docstring"""




											from torch.distributed.fsdp.fully_sharded_data_parallel import BackwardPrefetch

											for i, prefetch_policy in enumerate(UpperCAmelCase_	):
																		_lowerCAmelCase							=      self.dist_env.copy()
																		_lowerCAmelCase							=      prefetch_policy
																		with mockenv_context(**UpperCAmelCase_	):
																									_lowerCAmelCase							=      FullyShardedDataParallelPlugin()
																									if prefetch_policy == "NO_PREFETCH":
																																self.assertIsNone(fsdp_plugin.backward_prefetch	)
																									else:
																																self.assertEqual(fsdp_plugin.backward_prefetch					,  BackwardPrefetch(i + 1	)	)


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







											"""simple docstring"""




											from torch.distributed.fsdp.fully_sharded_data_parallel import StateDictType

											for i, state_dict_type in enumerate(UpperCAmelCase_	):
																		_lowerCAmelCase							=      self.dist_env.copy()
																		_lowerCAmelCase							=      state_dict_type
																		with mockenv_context(**UpperCAmelCase_	):
																									_lowerCAmelCase							=      FullyShardedDataParallelPlugin()
																									self.assertEqual(fsdp_plugin.state_dict_type					,  StateDictType(i + 1	)	)
																									if state_dict_type == "FULL_STATE_DICT":
																																self.assertTrue(fsdp_plugin.state_dict_config.offload_to_cpu	)
																																self.assertTrue(fsdp_plugin.state_dict_config.ranka_only	)


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







											"""simple docstring"""




											_lowerCAmelCase							=      AutoModel.from_pretrained(UpperCAmelCase_	)
											for policy in FSDP_AUTO_WRAP_POLICY:
																		_lowerCAmelCase							=      self.dist_env.copy()
																		_lowerCAmelCase							=      policy
																		if policy == "TRANSFORMER_BASED_WRAP":
																									_lowerCAmelCase							=      "BertLayer"
																		elif policy == "SIZE_BASED_WRAP":
																									_lowerCAmelCase							=      "2000"
																		with mockenv_context(**UpperCAmelCase_	):
																									_lowerCAmelCase							=      FullyShardedDataParallelPlugin()
																									fsdp_plugin.set_auto_wrap_policy(UpperCAmelCase_	)
																									if policy == "NO_WRAP":
																																self.assertIsNone(fsdp_plugin.auto_wrap_policy	)
																									else:
																																self.assertIsNotNone(fsdp_plugin.auto_wrap_policy	)

											_lowerCAmelCase							=      self.dist_env.copy()
											_lowerCAmelCase							=      "TRANSFORMER_BASED_WRAP"
											_lowerCAmelCase							=      "T5Layer"
											with mockenv_context(**UpperCAmelCase_	):
																		_lowerCAmelCase							=      FullyShardedDataParallelPlugin()
																		with self.assertRaises(UpperCAmelCase_	) as cm:
																									fsdp_plugin.set_auto_wrap_policy(UpperCAmelCase_	)
																		self.assertTrue('Could not find the transformer layer class to wrap in the model.' in str(cm.exception	)	)

											_lowerCAmelCase							=      self.dist_env.copy()
											_lowerCAmelCase							=      "SIZE_BASED_WRAP"
											_lowerCAmelCase							=      "0"
											with mockenv_context(**UpperCAmelCase_	):
																		_lowerCAmelCase							=      FullyShardedDataParallelPlugin()
																		fsdp_plugin.set_auto_wrap_policy(UpperCAmelCase_	)
																		self.assertIsNone(fsdp_plugin.auto_wrap_policy	)


				def 					__lowerCamelCase						(       self						:      List[str]	)		->	Any:







											"""simple docstring"""




											from torch.distributed.fsdp.fully_sharded_data_parallel import MixedPrecision
											from torch.distributed.fsdp.sharded_grad_scaler import ShardedGradScaler

											for mp_dtype in dtypes:
																		_lowerCAmelCase							=      self.dist_env.copy()
																		_lowerCAmelCase							=      mp_dtype
																		with mockenv_context(**UpperCAmelCase_	):
																									_lowerCAmelCase							=      Accelerator()
																									if mp_dtype == "fp16":
																																_lowerCAmelCase							=      torch.floataa
																									elif mp_dtype == "bf16":
																																_lowerCAmelCase							=      torch.bfloataa
																									_lowerCAmelCase							=      MixedPrecision(param_dtype=UpperCAmelCase_					,  reduce_dtype=UpperCAmelCase_					,  buffer_dtype=UpperCAmelCase_	)
																									self.assertEqual(accelerator.state.fsdp_plugin.mixed_precision_policy					,  UpperCAmelCase_	)
																									if mp_dtype == FPaa:
																																self.assertTrue(isinstance(accelerator.scaler					,  UpperCAmelCase_	)	)
																									elif mp_dtype == BFaa:
																																self.assertIsNone(accelerator.scaler	)
																									AcceleratorState._reset_state(UpperCAmelCase_	)







				def 					__lowerCamelCase						(       self						:      int	)		->	str:







											"""simple docstring"""




											from torch.distributed.fsdp.fully_sharded_data_parallel import CPUOffload

											for flag in [True, False]:
																		_lowerCAmelCase							=      self.dist_env.copy()
																		_lowerCAmelCase							=      str(UpperCAmelCase_	).lower()
																		with mockenv_context(**UpperCAmelCase_	):
																									_lowerCAmelCase							=      FullyShardedDataParallelPlugin()
																									self.assertEqual(fsdp_plugin.cpu_offload					,  CPUOffload(offload_params=UpperCAmelCase_	)	)






@require_fsdp
@require_multi_gpu
@slow
class 							_SCREAMING_SNAKE_CASE	(	UpperCAmelCase				):






				'''simple docstring'''



				def 					__lowerCamelCase						(       self						:      List[Any]	)		->	Any:







											"""simple docstring"""




											super().setUp()
											_lowerCAmelCase							=      0.82
											_lowerCAmelCase							=      [
											    "fsdp_shard_grad_op_transformer_based_wrap",
											    "fsdp_full_shard_transformer_based_wrap",
											]
											_lowerCAmelCase							=      {
											    "multi_gpu_fp16": 3_200,
											    "fsdp_shard_grad_op_transformer_based_wrap_fp16": 2_000,
											    "fsdp_full_shard_transformer_based_wrap_fp16": 1_900,
											    # Disabling below test as it overwhelms the RAM memory usage
											    # on CI self-hosted runner leading to tests getting killed.
											    # "fsdp_full_shard_cpu_offload_transformer_based_wrap_fp32": 1500,  # fp16 was leading to indefinite hang
											}
											_lowerCAmelCase							=      160
											_lowerCAmelCase							=      160

											_lowerCAmelCase							=      inspect.getfile(accelerate.test_utils	)
											_lowerCAmelCase							=      os.path.sep.join(mod_file.split(os.path.sep	)[:-1] + ['scripts', 'external_deps']	)


				def 					__lowerCamelCase						(       self						:      List[Any]	)		->	int:







											"""simple docstring"""




											_lowerCAmelCase							=      os.path.join(self.test_scripts_folder					,  'test_performance.py'	)
											_lowerCAmelCase							=      ["accelerate", "launch", "--num_processes=2", "--num_machines=1", "--machine_rank=0", "--use_fsdp"]
											for config in self.performance_configs:
																		_lowerCAmelCase							=      cmd.copy()
																		for i, strategy in enumerate(UpperCAmelCase_	):
																									if strategy.lower() in config:
																																cmd_config.append(F"""--fsdp_sharding_strategy={i+1}"""	)
																																break

																		if "fp32" in config:
																									cmd_config.append('--mixed_precision=no'	)
																		else:
																									cmd_config.append('--mixed_precision=fp16'	)

																		if "cpu_offload" in config:
																									cmd_config.append('--fsdp_offload_params=True'	)

																		for policy in FSDP_AUTO_WRAP_POLICY:
																									if policy.lower() in config:
																																cmd_config.append(F"""--fsdp_auto_wrap_policy={policy}"""	)
																																break

																		if policy == "TRANSFORMER_BASED_WRAP":
																									cmd_config.append('--fsdp_transformer_layer_cls_to_wrap=BertLayer'	)
																		elif policy == "SIZE_BASED_WRAP":
																									cmd_config.append('--fsdp_min_num_params=2000'	)

																		cmd_config.extend(
																		    [
																		        self.test_file_path,
																		        F"""--output_dir={self.tmpdir}""",
																		        F"""--performance_lower_bound={self.performance_lower_bound}""",
																		    ]	)
																		with patch_environment(omp_num_threads=1	):
																									execute_subprocess_async(UpperCAmelCase_					,  env=os.environ.copy()	)


				def 					__lowerCamelCase						(       self						:      List[str]	)		->	Tuple:







											"""simple docstring"""




											_lowerCAmelCase							=      os.path.join(self.test_scripts_folder					,  'test_checkpointing.py'	)
											_lowerCAmelCase							=      [
											    "accelerate",
											    "launch",
											    "--num_processes=2",
											    "--num_machines=1",
											    "--machine_rank=0",
											    "--use_fsdp",
											    "--mixed_precision=fp16",
											    "--fsdp_transformer_layer_cls_to_wrap=BertLayer",
											]

											for i, strategy in enumerate(UpperCAmelCase_	):
																		_lowerCAmelCase							=      cmd.copy()
																		cmd_config.append(F"""--fsdp_sharding_strategy={i+1}"""	)
																		if strategy != "FULL_SHARD":
																									continue
																		_lowerCAmelCase							=      len(UpperCAmelCase_	)
																		for state_dict_type in FSDP_STATE_DICT_TYPE:
																									_lowerCAmelCase							=      cmd_config[:state_dict_config_index]
																									cmd_config.append(F"""--fsdp_state_dict_type={state_dict_type}"""	)
																									cmd_config.extend(
																									    [
																									        self.test_file_path,
																									        F"""--output_dir={self.tmpdir}""",
																									        '--partial_train_epoch=1',
																									    ]	)
																									with patch_environment(omp_num_threads=1	):
																																execute_subprocess_async(UpperCAmelCase_					,  env=os.environ.copy()	)

																									_lowerCAmelCase							=      cmd_config[:-1]
																									_lowerCAmelCase							=      os.path.join(self.tmpdir					,  'epoch_0'	)
																									cmd_config.extend(
																									    [
																									        F"""--resume_from_checkpoint={resume_from_checkpoint}""",
																									    ]	)
																									with patch_environment(omp_num_threads=1	):
																																execute_subprocess_async(UpperCAmelCase_					,  env=os.environ.copy()	)







				def 					__lowerCamelCase						(       self						:      Dict	)		->	Tuple:







											"""simple docstring"""




											_lowerCAmelCase							=      os.path.join(self.test_scripts_folder					,  'test_peak_memory_usage.py'	)
											_lowerCAmelCase							=      [
											    "accelerate",
											    "launch",
											    "--num_processes=2",
											    "--num_machines=1",
											    "--machine_rank=0",
											]
											for spec, peak_mem_upper_bound in self.peak_memory_usage_upper_bound.items():
																		_lowerCAmelCase							=      cmd.copy()
																		if "fp16" in spec:
																									cmd_config.extend(['--mixed_precision=fp16']	)
																		else:
																									cmd_config.extend(['--mixed_precision=no']	)

																		if "multi_gpu" in spec:
																									continue
																		else:
																									cmd_config.extend(['--use_fsdp']	)
																									for i, strategy in enumerate(UpperCAmelCase_	):
																																if strategy.lower() in spec:
																																							cmd_config.append(F"""--fsdp_sharding_strategy={i+1}"""	)
																																							break

																									if "cpu_offload" in spec:
																																cmd_config.append('--fsdp_offload_params=True'	)

																									for policy in FSDP_AUTO_WRAP_POLICY:
																																if policy.lower() in spec:
																																							cmd_config.append(F"""--fsdp_auto_wrap_policy={policy}"""	)
																																							break

																									if policy == "TRANSFORMER_BASED_WRAP":
																																cmd_config.append('--fsdp_transformer_layer_cls_to_wrap=BertLayer'	)
																									elif policy == "SIZE_BASED_WRAP":
																																cmd_config.append('--fsdp_min_num_params=2000'	)

																		cmd_config.extend(
																		    [
																		        self.test_file_path,
																		        F"""--output_dir={self.tmpdir}""",
																		        F"""--peak_memory_upper_bound={peak_mem_upper_bound}""",
																		        F"""--n_train={self.n_train}""",
																		        F"""--n_val={self.n_val}""",
																		    ]	)
																		with patch_environment(omp_num_threads=1	):
																									execute_subprocess_async(UpperCAmelCase_					,  env=os.environ.copy()	)









"""simple docstring"""

import argparse
import json
import os
import sys
import tempfile
import unittest
from argparse import Namespace
from dataclasses import dataclass, field
from enum import Enum
from pathlib import Path
from typing import List, Literal, Optional

import yaml

from transformers import HfArgumentParser, TrainingArguments
from transformers.hf_argparser import make_choice_type_function, string_to_bool


# Since Python 3.10, we can use the builtin `|` operator for Union types
# See PEP 604: https://peps.python.org/pep-0604
_UpperCamelCase		     =    sys.version_info >= (3, 1_0)



def       SCREAMING_SNAKE_CASE							( lowercase__=None					,					lowercase__=None   )			->						List[Any]:
 return field(default_factory=lambda: default					,					metadata=lowercase__   )






@dataclass
class 			__a					:





       """simple docstring"""


       __UpperCamelCase					:      int
       __UpperCamelCase					:      float
       __UpperCamelCase					:      str
       __UpperCamelCase					:      bool






@dataclass
class 			__a					:





       """simple docstring"""


       __UpperCamelCase					:      int    =						42
       __UpperCamelCase					:      str    =						field(default='toto'       ,	metadata={'help': 'help message'}       )






@dataclass
class 			__a					:





       """simple docstring"""


       __UpperCamelCase					:      bool    =						False
       __UpperCamelCase					:      bool    =						True
       __UpperCamelCase					:      Optional[bool]    =						None






class 			__a					(						__magic_name__       ):





       """simple docstring"""


       __UpperCamelCase					:      List[str]    =						'titi'
       __UpperCamelCase					:      Tuple    =						'toto'






class 			__a					(						__magic_name__       ):





       """simple docstring"""


       __UpperCamelCase					:      Tuple    =						'titi'
       __UpperCamelCase					:      List[Any]    =						'toto'
       __UpperCamelCase					:      List[str]    =						42






@dataclass
class 			__a					:





       """simple docstring"""


       __UpperCamelCase					:      BasicEnum    =						"toto"






       def 			SCREAMING_SNAKE_CASE_	(   self   ):





        """simple docstring"""

        lowerCAmelCase__   :				Optional[int] 			=				BasicEnum(self.foo   )






@dataclass
class 			__a					:





       """simple docstring"""


       __UpperCamelCase					:      MixedTypeEnum    =						"toto"






       def 			SCREAMING_SNAKE_CASE_	(   self   ):





        """simple docstring"""

        lowerCAmelCase__   :				List[str] 			=				MixedTypeEnum(self.foo   )






@dataclass
class 			__a					:





       """simple docstring"""


       __UpperCamelCase					:      Optional[int]    =						None
       __UpperCamelCase					:      Optional[float]    =						field(default=__magic_name__       ,	metadata={'help': 'help message'}       )
       __UpperCamelCase					:      Optional[str]    =						None
       __UpperCamelCase					:      Optional[List[str]]    =						list_field(default=[]       )
       __UpperCamelCase					:      Optional[List[int]]    =						list_field(default=[]       )






@dataclass
class 			__a					:





       """simple docstring"""


       __UpperCamelCase					:      List[int]    =						list_field(default=[]       )
       __UpperCamelCase					:      List[int]    =						list_field(default=[1, 2, 3]       )
       __UpperCamelCase					:      List[str]    =						list_field(default=['Hallo', 'Bonjour', 'Hello']       )
       __UpperCamelCase					:      List[float]    =						list_field(default=[0.1, 0.2, 0.3]       )






@dataclass
class 			__a					:





       """simple docstring"""


       __UpperCamelCase					:      List[int]    =						field()
       __UpperCamelCase					:      str    =						field()
       __UpperCamelCase					:      BasicEnum    =						field()






       def 			SCREAMING_SNAKE_CASE_	(   self   ):





        """simple docstring"""

        lowerCAmelCase__   :				Optional[Any] 			=				BasicEnum(self.required_enum   )






@dataclass
class 			__a					:





       """simple docstring"""


       __UpperCamelCase					:      int
       __UpperCamelCase					:      "BasicEnum"    =						field()
       __UpperCamelCase					:      "Optional[bool]"    =						None
       __UpperCamelCase					:      "str"    =						field(default='toto'       ,	metadata={'help': 'help message'}       )
       __UpperCamelCase					:      "List[str]"    =						list_field(default=['Hallo', 'Bonjour', 'Hello']       )


if is_python_no_less_than_3_10:






    @dataclass
    class 			__a					:





           """simple docstring"""


           __UpperCamelCase					:      bool    =						False
           __UpperCamelCase					:      bool    =						True
           __UpperCamelCase					:      bool | None    =						None






    @dataclass
    class 			__a					:





           """simple docstring"""


           __UpperCamelCase					:      int | None    =						None
           __UpperCamelCase					:      float | None    =						field(default=__magic_name__       ,	metadata={'help': 'help message'}       )
           __UpperCamelCase					:      str | None    =						None
           __UpperCamelCase					:      list[str] | None    =						list_field(default=[]       )
           __UpperCamelCase					:      list[int] | None    =						list_field(default=[]       )






class 			__a					(						unittest.TestCase       ):





       """simple docstring"""


       def 			SCREAMING_SNAKE_CASE_	(   self			,					snake_case			,					snake_case   ):





        """simple docstring"""

        self.assertEqual(len(a._actions   )			,					len(b._actions   )   )
        for x, y in zip(a._actions			,					b._actions   ):
         lowerCAmelCase__   :				Union[str, Any] 			=				{k: v for k, v in vars(snake_case   ).items() if k != "container"}
         lowerCAmelCase__   :				Union[str, Any] 			=				{k: v for k, v in vars(snake_case   ).items() if k != "container"}

         # Choices with mixed type have custom function as "type"
         # So we need to compare results directly for equality
         if xx.get("choices"			,					snake_case   ) and yy.get("choices"			,					snake_case   ):
          for expected_choice in yy["choices"] + xx["choices"]:
           self.assertEqual(xx["type"](snake_case   )			,					yy["type"](snake_case   )   )
          del xx["type"], yy["type"]

         self.assertEqual(snake_case			,					snake_case   )






       def 			SCREAMING_SNAKE_CASE_	(   self   ):





        """simple docstring"""

        lowerCAmelCase__   :				Union[str, Any] 			=				HfArgumentParser(snake_case   )

        lowerCAmelCase__   :				Optional[int] 			=				argparse.ArgumentParser()
        expected.add_argument("--foo"			,					type=snake_case			,					required=snake_case   )
        expected.add_argument("--bar"			,					type=snake_case			,					required=snake_case   )
        expected.add_argument("--baz"			,					type=snake_case			,					required=snake_case   )
        expected.add_argument("--flag"			,					type=snake_case			,					default=snake_case			,					const=snake_case			,					nargs="?"   )
        self.argparsersEqual(snake_case			,					snake_case   )

        lowerCAmelCase__   :				List[Any] 			=				["--foo", "1", "--baz", "quux", "--bar", "0.5"]
        ((lowerCAmelCase__)			,						)   :				Dict 			=				parser.parse_args_into_dataclasses(snake_case			,					look_for_args_file=snake_case   )
        self.assertFalse(example.flag   )






       def 			SCREAMING_SNAKE_CASE_	(   self   ):





        """simple docstring"""

        lowerCAmelCase__   :				Tuple 			=				HfArgumentParser(snake_case   )

        lowerCAmelCase__   :				Optional[int] 			=				argparse.ArgumentParser()
        expected.add_argument("--foo"			,					default=42			,					type=snake_case   )
        expected.add_argument("--baz"			,					default="toto"			,					type=snake_case			,					help="help message"   )
        self.argparsersEqual(snake_case			,					snake_case   )






       def 			SCREAMING_SNAKE_CASE_	(   self   ):





        """simple docstring"""

        lowerCAmelCase__   :				Optional[Any] 			=				argparse.ArgumentParser()
        expected.add_argument("--foo"			,					type=snake_case			,					default=snake_case			,					const=snake_case			,					nargs="?"   )
        expected.add_argument("--baz"			,					type=snake_case			,					default=snake_case			,					const=snake_case			,					nargs="?"   )
        # A boolean no_* argument always has to come after its "default: True" regular counter-part
        # and its default must be set to False
        expected.add_argument("--no_baz"			,					action="store_false"			,					default=snake_case			,					dest="baz"   )
        expected.add_argument("--opt"			,					type=snake_case			,					default=snake_case   )

        lowerCAmelCase__   :				Optional[Any] 			=				[WithDefaultBoolExample]
        if is_python_no_less_than_3_10:
         dataclass_types.append(snake_case   )

        for dataclass_type in dataclass_types:
         lowerCAmelCase__   :				Optional[Any] 			=				HfArgumentParser(snake_case   )
         self.argparsersEqual(snake_case			,					snake_case   )

         lowerCAmelCase__   :				Union[str, Any] 			=				parser.parse_args([]   )
         self.assertEqual(snake_case			,					Namespace(foo=snake_case			,					baz=snake_case			,					opt=snake_case   )   )

         lowerCAmelCase__   :				List[str] 			=				parser.parse_args(["--foo", "--no_baz"]   )
         self.assertEqual(snake_case			,					Namespace(foo=snake_case			,					baz=snake_case			,					opt=snake_case   )   )

         lowerCAmelCase__   :				Optional[Any] 			=				parser.parse_args(["--foo", "--baz"]   )
         self.assertEqual(snake_case			,					Namespace(foo=snake_case			,					baz=snake_case			,					opt=snake_case   )   )

         lowerCAmelCase__   :				List[Any] 			=				parser.parse_args(["--foo", "True", "--baz", "True", "--opt", "True"]   )
         self.assertEqual(snake_case			,					Namespace(foo=snake_case			,					baz=snake_case			,					opt=snake_case   )   )

         lowerCAmelCase__   :				Optional[int] 			=				parser.parse_args(["--foo", "False", "--baz", "False", "--opt", "False"]   )
         self.assertEqual(snake_case			,					Namespace(foo=snake_case			,					baz=snake_case			,					opt=snake_case   )   )






       def 			SCREAMING_SNAKE_CASE_	(   self   ):





        """simple docstring"""

        lowerCAmelCase__   :				Tuple 			=				HfArgumentParser(snake_case   )

        lowerCAmelCase__   :				Optional[Any] 			=				argparse.ArgumentParser()
        expected.add_argument(
            "--foo"			,					default="toto"			,					choices=["titi", "toto", 42]			,					type=make_choice_type_function(["titi", "toto", 42]   )			,					)
        self.argparsersEqual(snake_case			,					snake_case   )

        lowerCAmelCase__   :				Union[str, Any] 			=				parser.parse_args([]   )
        self.assertEqual(args.foo			,					"toto"   )
        lowerCAmelCase__   :				List[str] 			=				parser.parse_args_into_dataclasses([]   )[0]
        self.assertEqual(enum_ex.foo			,					MixedTypeEnum.toto   )

        lowerCAmelCase__   :				List[Any] 			=				parser.parse_args(["--foo", "titi"]   )
        self.assertEqual(args.foo			,					"titi"   )
        lowerCAmelCase__   :				Any 			=				parser.parse_args_into_dataclasses(["--foo", "titi"]   )[0]
        self.assertEqual(enum_ex.foo			,					MixedTypeEnum.titi   )

        lowerCAmelCase__   :				Tuple 			=				parser.parse_args(["--foo", "42"]   )
        self.assertEqual(args.foo			,					42   )
        lowerCAmelCase__   :				Tuple 			=				parser.parse_args_into_dataclasses(["--foo", "42"]   )[0]
        self.assertEqual(enum_ex.foo			,					MixedTypeEnum.fourtytwo   )






       def 			SCREAMING_SNAKE_CASE_	(   self   ):





        """simple docstring"""

        @dataclass
        class 			__a					:





               """simple docstring"""


               __UpperCamelCase					:      Literal["titi", "toto", 42]    =						"toto"




        lowerCAmelCase__   :				Dict 			=				HfArgumentParser(snake_case   )

        lowerCAmelCase__   :				Dict 			=				argparse.ArgumentParser()
        expected.add_argument(
            "--foo"			,					default="toto"			,					choices=("titi", "toto", 42)			,					type=make_choice_type_function(["titi", "toto", 42]   )			,					)
        self.argparsersEqual(snake_case			,					snake_case   )

        lowerCAmelCase__   :				int 			=				parser.parse_args([]   )
        self.assertEqual(args.foo			,					"toto"   )

        lowerCAmelCase__   :				Optional[int] 			=				parser.parse_args(["--foo", "titi"]   )
        self.assertEqual(args.foo			,					"titi"   )

        lowerCAmelCase__   :				int 			=				parser.parse_args(["--foo", "42"]   )
        self.assertEqual(args.foo			,					42   )






       def 			SCREAMING_SNAKE_CASE_	(   self   ):





        """simple docstring"""

        lowerCAmelCase__   :				str 			=				HfArgumentParser(snake_case   )

        lowerCAmelCase__   :				Union[str, Any] 			=				argparse.ArgumentParser()
        expected.add_argument("--foo_int"			,					nargs="+"			,					default=[]			,					type=snake_case   )
        expected.add_argument("--bar_int"			,					nargs="+"			,					default=[1, 2, 3]			,					type=snake_case   )
        expected.add_argument("--foo_str"			,					nargs="+"			,					default=["Hallo", "Bonjour", "Hello"]			,					type=snake_case   )
        expected.add_argument("--foo_float"			,					nargs="+"			,					default=[0.1, 0.2, 0.3]			,					type=snake_case   )

        self.argparsersEqual(snake_case			,					snake_case   )

        lowerCAmelCase__   :				Tuple 			=				parser.parse_args([]   )
        self.assertEqual(
            snake_case			,					Namespace(foo_int=[]			,					bar_int=[1, 2, 3]			,					foo_str=["Hallo", "Bonjour", "Hello"]			,					foo_float=[0.1, 0.2, 0.3]   )			,					)

        lowerCAmelCase__   :				List[Any] 			=				parser.parse_args("--foo_int 1 --bar_int 2 3 --foo_str a b c --foo_float 0.1 0.7".split()   )
        self.assertEqual(snake_case			,					Namespace(foo_int=[1]			,					bar_int=[2, 3]			,					foo_str=["a", "b", "c"]			,					foo_float=[0.1, 0.7]   )   )






       def 			SCREAMING_SNAKE_CASE_	(   self   ):





        """simple docstring"""

        lowerCAmelCase__   :				Dict 			=				argparse.ArgumentParser()
        expected.add_argument("--foo"			,					default=snake_case			,					type=snake_case   )
        expected.add_argument("--bar"			,					default=snake_case			,					type=snake_case			,					help="help message"   )
        expected.add_argument("--baz"			,					default=snake_case			,					type=snake_case   )
        expected.add_argument("--ces"			,					nargs="+"			,					default=[]			,					type=snake_case   )
        expected.add_argument("--des"			,					nargs="+"			,					default=[]			,					type=snake_case   )

        lowerCAmelCase__   :				Optional[Any] 			=				[OptionalExample]
        if is_python_no_less_than_3_10:
         dataclass_types.append(snake_case   )

        for dataclass_type in dataclass_types:
         lowerCAmelCase__   :				Tuple 			=				HfArgumentParser(snake_case   )

         self.argparsersEqual(snake_case			,					snake_case   )

         lowerCAmelCase__   :				Any 			=				parser.parse_args([]   )
         self.assertEqual(snake_case			,					Namespace(foo=snake_case			,					bar=snake_case			,					baz=snake_case			,					ces=[]			,					des=[]   )   )

         lowerCAmelCase__   :				List[str] 			=				parser.parse_args("--foo 12 --bar 3.14 --baz 42 --ces a b c --des 1 2 3".split()   )
         self.assertEqual(snake_case			,					Namespace(foo=12			,					bar=3.14			,					baz="42"			,					ces=["a", "b", "c"]			,					des=[1, 2, 3]   )   )






       def 			SCREAMING_SNAKE_CASE_	(   self   ):





        """simple docstring"""

        lowerCAmelCase__   :				Optional[Any] 			=				HfArgumentParser(snake_case   )

        lowerCAmelCase__   :				List[Any] 			=				argparse.ArgumentParser()
        expected.add_argument("--required_list"			,					nargs="+"			,					type=snake_case			,					required=snake_case   )
        expected.add_argument("--required_str"			,					type=snake_case			,					required=snake_case   )
        expected.add_argument(
            "--required_enum"			,					type=make_choice_type_function(["titi", "toto"]   )			,					choices=["titi", "toto"]			,					required=snake_case			,					)
        self.argparsersEqual(snake_case			,					snake_case   )






       def 			SCREAMING_SNAKE_CASE_	(   self   ):





        """simple docstring"""

        lowerCAmelCase__   :				Dict 			=				HfArgumentParser(snake_case   )

        lowerCAmelCase__   :				Optional[Any] 			=				argparse.ArgumentParser()
        expected.add_argument("--foo"			,					type=snake_case			,					required=snake_case   )
        expected.add_argument(
            "--required_enum"			,					type=make_choice_type_function(["titi", "toto"]   )			,					choices=["titi", "toto"]			,					required=snake_case			,					)
        expected.add_argument("--opt"			,					type=snake_case			,					default=snake_case   )
        expected.add_argument("--baz"			,					default="toto"			,					type=snake_case			,					help="help message"   )
        expected.add_argument("--foo_str"			,					nargs="+"			,					default=["Hallo", "Bonjour", "Hello"]			,					type=snake_case   )
        self.argparsersEqual(snake_case			,					snake_case   )






       def 			SCREAMING_SNAKE_CASE_	(   self   ):





        """simple docstring"""

        lowerCAmelCase__   :				Any 			=				HfArgumentParser(snake_case   )

        lowerCAmelCase__   :				List[Any] 			=				{
            "foo": 12,
            "bar": 3.14,
            "baz": "42",
            "flag": True,
        }

        lowerCAmelCase__   :				Tuple 			=				parser.parse_dict(snake_case   )[0]
        lowerCAmelCase__   :				Any 			=				BasicExample(**snake_case   )
        self.assertEqual(snake_case			,					snake_case   )






       def 			SCREAMING_SNAKE_CASE_	(   self   ):





        """simple docstring"""

        lowerCAmelCase__   :				Union[str, Any] 			=				HfArgumentParser(snake_case   )

        lowerCAmelCase__   :				List[Any] 			=				{
            "foo": 12,
            "bar": 3.14,
            "baz": "42",
            "flag": True,
            "extra": 42,
        }

        self.assertRaises(snake_case			,					parser.parse_dict			,					snake_case			,					allow_extra_keys=snake_case   )






       def 			SCREAMING_SNAKE_CASE_	(   self   ):





        """simple docstring"""

        lowerCAmelCase__   :				Union[str, Any] 			=				HfArgumentParser(snake_case   )

        lowerCAmelCase__   :				Optional[Any] 			=				{
            "foo": 12,
            "bar": 3.14,
            "baz": "42",
            "flag": True,
        }
        with tempfile.TemporaryDirectory() as tmp_dir:
         lowerCAmelCase__   :				int 			=				os.path.join(snake_case			,					"temp_json"   )
         os.mkdir(snake_case   )
         with open(temp_local_path + ".json"			,					"w+"   ) as f:
          json.dump(snake_case			,					snake_case   )
         lowerCAmelCase__   :				Any 			=				parser.parse_yaml_file(Path(temp_local_path + ".json"   )   )[0]

        lowerCAmelCase__   :				str 			=				BasicExample(**snake_case   )
        self.assertEqual(snake_case			,					snake_case   )






       def 			SCREAMING_SNAKE_CASE_	(   self   ):





        """simple docstring"""

        lowerCAmelCase__   :				str 			=				HfArgumentParser(snake_case   )

        lowerCAmelCase__   :				int 			=				{
            "foo": 12,
            "bar": 3.14,
            "baz": "42",
            "flag": True,
        }
        with tempfile.TemporaryDirectory() as tmp_dir:
         lowerCAmelCase__   :				Optional[Any] 			=				os.path.join(snake_case			,					"temp_yaml"   )
         os.mkdir(snake_case   )
         with open(temp_local_path + ".yaml"			,					"w+"   ) as f:
          yaml.dump(snake_case			,					snake_case   )
         lowerCAmelCase__   :				Dict 			=				parser.parse_yaml_file(Path(temp_local_path + ".yaml"   )   )[0]
        lowerCAmelCase__   :				int 			=				BasicExample(**snake_case   )
        self.assertEqual(snake_case			,					snake_case   )






       def 			SCREAMING_SNAKE_CASE_	(   self   ):





        """simple docstring"""

        lowerCAmelCase__   :				Union[str, Any] 			=				HfArgumentParser(snake_case   )
        self.assertIsNotNone(snake_case   )






