





"""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
a		:					Optional[int]	      =     sys.version_info >= (3, 10)






def  _SCREAMING_SNAKE_CASE						(      _lowercase		:       int=None       ,			_lowercase		:       Dict=None   )					->Any:






	'''simple docstring'''






	return field(default_factory=lambda: default       ,			metadata=_lowercase   )


@dataclass
class 			__UpperCamelCase							:
							lowerCamelCase  :   int
							lowerCamelCase  :   float
							lowerCamelCase  :   str
							lowerCamelCase  :   bool


@dataclass
class 			__UpperCamelCase							:
							lowerCamelCase  :   int    =42
							lowerCamelCase  :   str    =field(default="""toto"""						,			metadata={"""help""": """help message"""}		)


@dataclass
class 			__UpperCamelCase							:
							lowerCamelCase  :   bool    =False
							lowerCamelCase  :   bool    =True
							lowerCamelCase  :   Optional[bool]    =None


class 			__UpperCamelCase							(		a__		):
							lowerCamelCase  :   int    ="""titi"""
							lowerCamelCase  :   Optional[int]    ="""toto"""


class 			__UpperCamelCase							(		a__		):
							lowerCamelCase  :   str    ="""titi"""
							lowerCamelCase  :   Dict    ="""toto"""
							lowerCamelCase  :   Dict    =42


@dataclass
class 			__UpperCamelCase							:
							lowerCamelCase  :   BasicEnum    ="toto"






							def        __a  (  self )      ->					Optional[Any]:
								a :						Tuple      =							BasicEnum(self.foo )


@dataclass
class 			__UpperCamelCase							:
							lowerCamelCase  :   MixedTypeEnum    ="toto"






							def        __a  (  self )      ->					List[str]:
								a :						Dict      =							MixedTypeEnum(self.foo )


@dataclass
class 			__UpperCamelCase							:
							lowerCamelCase  :   Optional[int]    =None
							lowerCamelCase  :   Optional[float]    =field(default=a__						,			metadata={"""help""": """help message"""}		)
							lowerCamelCase  :   Optional[str]    =None
							lowerCamelCase  :   Optional[List[str]]    =list_field(default=[]		)
							lowerCamelCase  :   Optional[List[int]]    =list_field(default=[]		)


@dataclass
class 			__UpperCamelCase							:
							lowerCamelCase  :   List[int]    =list_field(default=[]		)
							lowerCamelCase  :   List[int]    =list_field(default=[1, 2, 3]		)
							lowerCamelCase  :   List[str]    =list_field(default=["""Hallo""", """Bonjour""", """Hello"""]		)
							lowerCamelCase  :   List[float]    =list_field(default=[0.1, 0.2, 0.3]		)


@dataclass
class 			__UpperCamelCase							:
							lowerCamelCase  :   List[int]    =field()
							lowerCamelCase  :   str    =field()
							lowerCamelCase  :   BasicEnum    =field()






							def        __a  (  self )      ->					Union[str, Any]:
								a :						List[Any]      =							BasicEnum(self.required_enum )


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


if is_python_no_less_than_3_10:


		@dataclass
		class 			__UpperCamelCase							:
									lowerCamelCase  :   bool    =False
									lowerCamelCase  :   bool    =True
									lowerCamelCase  :   bool | None    =None


		@dataclass
		class 			__UpperCamelCase							:
									lowerCamelCase  :   int | None    =None
									lowerCamelCase  :   float | None    =field(default=a__						,			metadata={"""help""": """help message"""}		)
									lowerCamelCase  :   str | None    =None
									lowerCamelCase  :   list[str] | None    =list_field(default=[]		)
									lowerCamelCase  :   list[int] | None    =list_field(default=[]		)







class 			__UpperCamelCase							(		unittest.TestCase		):






							def        __a  (  self ,   lowerCAmelCase__ ,   lowerCAmelCase__ )      ->					Dict:
								self.assertEqual(len(a._actions ) ,   len(b._actions ) )
								for x, y in zip(a._actions ,   b._actions ):
									a :						Optional[int]      =							{k: v for k, v in vars(lowerCAmelCase__ ).items() if k != "container"}
									a :						Optional[int]      =							{k: v for k, v in vars(lowerCAmelCase__ ).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" ,   lowerCAmelCase__ ) and yy.get("choices" ,   lowerCAmelCase__ ):
										for expected_choice in yy["choices"] + xx["choices"]:
											self.assertEqual(xx["type"](lowerCAmelCase__ ) ,   yy["type"](lowerCAmelCase__ ) )
										del xx["type"], yy["type"]

									self.assertEqual(lowerCAmelCase__ ,   lowerCAmelCase__ )






							def        __a  (  self )      ->					List[Any]:
								a :						int      =							HfArgumentParser(lowerCAmelCase__ )

								a :						Tuple      =							argparse.ArgumentParser()
								expected.add_argument("--foo" ,   type=lowerCAmelCase__ ,   required=lowerCAmelCase__ )
								expected.add_argument("--bar" ,   type=lowerCAmelCase__ ,   required=lowerCAmelCase__ )
								expected.add_argument("--baz" ,   type=lowerCAmelCase__ ,   required=lowerCAmelCase__ )
								expected.add_argument("--flag" ,   type=lowerCAmelCase__ ,   default=lowerCAmelCase__ ,   const=lowerCAmelCase__ ,   nargs="?" )
								self.argparsersEqual(lowerCAmelCase__ ,   lowerCAmelCase__ )

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






							def        __a  (  self )      ->					str:
								a :						str      =							HfArgumentParser(lowerCAmelCase__ )

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






							def        __a  (  self )      ->					int:
								a :						List[str]      =							argparse.ArgumentParser()
								expected.add_argument("--foo" ,   type=lowerCAmelCase__ ,   default=lowerCAmelCase__ ,   const=lowerCAmelCase__ ,   nargs="?" )
								expected.add_argument("--baz" ,   type=lowerCAmelCase__ ,   default=lowerCAmelCase__ ,   const=lowerCAmelCase__ ,   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=lowerCAmelCase__ ,   dest="baz" )
								expected.add_argument("--opt" ,   type=lowerCAmelCase__ ,   default=lowerCAmelCase__ )

								a :						List[str]      =							[WithDefaultBoolExample]
								if is_python_no_less_than_3_10:
									dataclass_types.append(lowerCAmelCase__ )

								for dataclass_type in dataclass_types:
									a :						int      =							HfArgumentParser(lowerCAmelCase__ )
									self.argparsersEqual(lowerCAmelCase__ ,   lowerCAmelCase__ )

									a :						Optional[int]      =							parser.parse_args([] )
									self.assertEqual(lowerCAmelCase__ ,   Namespace(foo=lowerCAmelCase__ ,   baz=lowerCAmelCase__ ,   opt=lowerCAmelCase__ ) )

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

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

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

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






							def        __a  (  self )      ->					Optional[int]:
								a :						Any      =							HfArgumentParser(lowerCAmelCase__ )

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

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

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

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






							def        __a  (  self )      ->					int:


								@dataclass
								class 			__UpperCamelCase							:
															lowerCamelCase  :   Literal["titi", "toto", 42]    ="toto"
								a :						Any      =							HfArgumentParser(lowerCAmelCase__ )

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

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

								a :						List[str]      =							parser.parse_args(["--foo", "titi"] )
								self.assertEqual(args.foo ,   "titi" )

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






							def        __a  (  self )      ->					Any:
								a :						List[Any]      =							HfArgumentParser(lowerCAmelCase__ )

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

								self.argparsersEqual(lowerCAmelCase__ ,   lowerCAmelCase__ )

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

								a :						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(lowerCAmelCase__ ,   Namespace(foo_int=[1] ,   bar_int=[2, 3] ,   foo_str=["a", "b", "c"] ,   foo_float=[0.1, 0.7] ) )






							def        __a  (  self )      ->					Optional[Any]:
								a :						Dict      =							argparse.ArgumentParser()
								expected.add_argument("--foo" ,   default=lowerCAmelCase__ ,   type=lowerCAmelCase__ )
								expected.add_argument("--bar" ,   default=lowerCAmelCase__ ,   type=lowerCAmelCase__ ,   help="help message" )
								expected.add_argument("--baz" ,   default=lowerCAmelCase__ ,   type=lowerCAmelCase__ )
								expected.add_argument("--ces" ,   nargs="+" ,   default=[] ,   type=lowerCAmelCase__ )
								expected.add_argument("--des" ,   nargs="+" ,   default=[] ,   type=lowerCAmelCase__ )

								a :						str      =							[OptionalExample]
								if is_python_no_less_than_3_10:
									dataclass_types.append(lowerCAmelCase__ )

								for dataclass_type in dataclass_types:
									a :						Optional[int]      =							HfArgumentParser(lowerCAmelCase__ )

									self.argparsersEqual(lowerCAmelCase__ ,   lowerCAmelCase__ )

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

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






							def        __a  (  self )      ->					str:
								a :						Optional[Any]      =							HfArgumentParser(lowerCAmelCase__ )

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






							def        __a  (  self )      ->					int:
								a :						int      =							HfArgumentParser(lowerCAmelCase__ )

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






							def        __a  (  self )      ->					int:
								a :						Union[str, Any]      =							HfArgumentParser(lowerCAmelCase__ )

								a :						Any      =							{
								    "foo": 12,
								    "bar": 3.14,
								    "baz": "42",
								    "flag": True,
								}

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






							def        __a  (  self )      ->					str:
								a :						Dict      =							HfArgumentParser(lowerCAmelCase__ )

								a :						str      =							{
								    "foo": 12,
								    "bar": 3.14,
								    "baz": "42",
								    "flag": True,
								    "extra": 42,
								}

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






							def        __a  (  self )      ->					Any:
								a :						int      =							HfArgumentParser(lowerCAmelCase__ )

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

								a :						Dict      =							BasicExample(**lowerCAmelCase__ )
								self.assertEqual(lowerCAmelCase__ ,   lowerCAmelCase__ )






							def        __a  (  self )      ->					Any:
								a :						List[Any]      =							HfArgumentParser(lowerCAmelCase__ )

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






							def        __a  (  self )      ->					str:
								a :						Optional[int]      =							HfArgumentParser(lowerCAmelCase__ )
								self.assertIsNotNone(lowerCAmelCase__ )









"""simple docstring"""




import gc
import threading
import time

import psutil
import torch


class 			__UpperCamelCase							:






							def __init__(  self )      ->					Any:
								a :						str      =							psutil.Process()
								a :						str      =							False






							def        __a  (  self )      ->					str:
								a :						List[Any]      =							-1

								while True:
									a :						str      =							max(self.process.memory_info().rss ,   self.cpu_memory_peak )

									# can't sleep or will not catch the peak right (this comment is here on purpose)
									if not self.peak_monitoring:
										break






							def        __a  (  self )      ->					List[str]:
								a :						Dict      =							True
								a :						Optional[Any]      =							threading.Thread(target=self.peak_monitor )
								a :						List[Any]      =							True
								self.thread.start()






							def        __a  (  self )      ->					List[str]:
								a :						Optional[int]      =							False
								self.thread.join()
								return self.cpu_memory_peak
a		:					Tuple	      =     PeakCPUMemory()






def  _SCREAMING_SNAKE_CASE						(      )					->List[Any]:






	'''simple docstring'''






	a :						List[str]      =							{"time": time.time()}

	gc.collect()
	torch.cuda.empty_cache()

	# CPU mem
	a :						Tuple      =							psutil.Process().memory_info().rss
	cpu_peak_tracker.start()

	# GPU mem
	for i in range(torch.cuda.device_count()   ):
		a :						Any      =							torch.cuda.memory_allocated(_lowercase   )
	torch.cuda.reset_peak_memory_stats()

	return measures






def  _SCREAMING_SNAKE_CASE						(      _lowercase		:       List[str]   )					->Union[str, Any]:






	'''simple docstring'''






	a :						int      =							{"time": time.time() - start_measures["time"]}

	gc.collect()
	torch.cuda.empty_cache()

	# CPU mem
	a :						List[Any]      =							(psutil.Process().memory_info().rss - start_measures["cpu"]) / 2**20
	a :						Union[str, Any]      =							(cpu_peak_tracker.stop() - start_measures["cpu"]) / 2**20

	# GPU mem
	for i in range(torch.cuda.device_count()   ):
		a :						Optional[Any]      =							(torch.cuda.memory_allocated(_lowercase   ) - start_measures[str(_lowercase   )]) / 2**20
		a :						Optional[Any]      =							(torch.cuda.max_memory_allocated(_lowercase   ) - start_measures[str(_lowercase   )]) / 2**20

	return measures







def  _SCREAMING_SNAKE_CASE						(      _lowercase		:       str       ,			_lowercase		:       Union[str, Any]   )					->Optional[int]:






	'''simple docstring'''






	print(F"""{description}:"""   )
	print(F"""- Time: {measures['time']:.2f}s"""   )
	for i in range(torch.cuda.device_count()   ):
		print(F"""- GPU {i} allocated: {measures[str(_lowercase   )]:.2f}MiB"""   )
		a :						str      =							measures[F"""{i}-peak"""]
		print(F"""- GPU {i} peak: {peak:.2f}MiB"""   )
	print(F"""- CPU RAM allocated: {measures['cpu']:.2f}MiB"""   )
	print(F"""- CPU RAM peak: {measures['cpu-peak']:.2f}MiB"""   )


