


'''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		=sys.version_info >= (3, 10)







def       _UpperCamelCase (						UpperCamelCase__=None	, UpperCamelCase__=None   ):
			return field(default_factory=lambda: default	, metadata=UpperCamelCase__   )


@dataclass
class 						_snake_case   :
					lowerCAmelCase :int
					lowerCAmelCase :float
					lowerCAmelCase :str
					lowerCAmelCase :bool


@dataclass
class 						_snake_case   :
					lowerCAmelCase :int						=   42
					lowerCAmelCase :str						=   field(default='''toto'''    ,				metadata={'''help''': '''help message'''}		)


@dataclass
class 						_snake_case   :
					lowerCAmelCase :bool						=   False
					lowerCAmelCase :bool						=   True
					lowerCAmelCase :Optional[bool]						=   None


class 						_snake_case   (       a__		):
					lowerCAmelCase :int						=   '''titi'''
					lowerCAmelCase :Any						=   '''toto'''


class 						_snake_case   (       a__		):
					lowerCAmelCase :Dict						=   '''titi'''
					lowerCAmelCase :List[Any]						=   '''toto'''
					lowerCAmelCase :Union[str, Any]						=   42


@dataclass
class 						_snake_case   :
					lowerCAmelCase :BasicEnum						=   "toto"




					def 			snake_case__  ( self) ->    Union[str, Any]:
								UpperCAmelCase__					:   Tuple       				=       BasicEnum(self.foo)


@dataclass
class 						_snake_case   :
					lowerCAmelCase :MixedTypeEnum						=   "toto"




					def 			snake_case__  ( self) ->    str:
								UpperCAmelCase__					:   Union[str, Any]       				=       MixedTypeEnum(self.foo)


@dataclass
class 						_snake_case   :
					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 						_snake_case   :
					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 						_snake_case   :
					lowerCAmelCase :List[int]						=   field()
					lowerCAmelCase :str						=   field()
					lowerCAmelCase :BasicEnum						=   field()




					def 			snake_case__  ( self) ->    int:
								UpperCAmelCase__					:   Dict       				=       BasicEnum(self.required_enum)


@dataclass
class 						_snake_case   :
					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 						_snake_case   :
							lowerCAmelCase :bool						=   False
							lowerCAmelCase :bool						=   True
							lowerCAmelCase :bool | None						=   None


		@dataclass
		class 						_snake_case   :
							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 						_snake_case   (       unittest.TestCase		):




					def 			snake_case__  ( self							,							_lowerCamelCase							,							_lowerCamelCase) ->    List[str]:
								self.assertEqual(len(a._actions)							,							len(b._actions))
								for x, y in zip(a._actions							,							b._actions):
											UpperCAmelCase__					:   int       				=       {k: v for k, v in vars(_lowerCamelCase).items() if k != """container"""}
											UpperCAmelCase__					:   List[Any]       				=       {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 			snake_case__  ( self) ->    List[str]:
								UpperCAmelCase__					:   List[str]       				=       HfArgumentParser(_lowerCamelCase)

								UpperCAmelCase__					:   Dict       				=       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)

								UpperCAmelCase__					:   List[str]       				=       ["""--foo""", """1""", """--baz""", """quux""", """--bar""", """0.5"""]
								(UpperCAmelCase__    )					:   Tuple       				=       parser.parse_args_into_dataclasses(_lowerCamelCase							,							look_for_args_file=_lowerCamelCase)
								self.assertFalse(example.flag)




					def 			snake_case__  ( self) ->    List[str]:
								UpperCAmelCase__					:   List[str]       				=       HfArgumentParser(_lowerCamelCase)

								UpperCAmelCase__					:   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 			snake_case__  ( self) ->    Any:
								UpperCAmelCase__					:   int       				=       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)

								UpperCAmelCase__					:   Dict       				=       [WithDefaultBoolExample]
								if is_python_no_less_than_3_10:
											dataclass_types.append(_lowerCamelCase)

								for dataclass_type in dataclass_types:
											UpperCAmelCase__					:   str       				=       HfArgumentParser(_lowerCamelCase)
											self.argparsersEqual(_lowerCamelCase							,							_lowerCamelCase)

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

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

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

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

											UpperCAmelCase__					:   Tuple       				=       parser.parse_args(["""--foo""", """False""", """--baz""", """False""", """--opt""", """False"""])
											self.assertEqual(_lowerCamelCase							,							Namespace(foo=_lowerCamelCase							,							baz=_lowerCamelCase							,							opt=_lowerCamelCase))




					def 			snake_case__  ( self) ->    List[Any]:
								UpperCAmelCase__					:   Union[str, Any]       				=       HfArgumentParser(_lowerCamelCase)

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

								UpperCAmelCase__					:   Any       				=       parser.parse_args([])
								self.assertEqual(args.foo							,							"""toto""")
								UpperCAmelCase__					:   Tuple       				=       parser.parse_args_into_dataclasses([])[0]
								self.assertEqual(enum_ex.foo							,							MixedTypeEnum.toto)

								UpperCAmelCase__					:   int       				=       parser.parse_args(["""--foo""", """titi"""])
								self.assertEqual(args.foo							,							"""titi""")
								UpperCAmelCase__					:   Tuple       				=       parser.parse_args_into_dataclasses(["""--foo""", """titi"""])[0]
								self.assertEqual(enum_ex.foo							,							MixedTypeEnum.titi)

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




					def 			snake_case__  ( self) ->    str:


								@dataclass
								class 						_snake_case   :
													lowerCAmelCase :Literal["titi", "toto", 42]						=   "toto"

								UpperCAmelCase__					:   List[Any]       				=       HfArgumentParser(_lowerCamelCase)

								UpperCAmelCase__					:   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)

								UpperCAmelCase__					:   List[str]       				=       parser.parse_args([])
								self.assertEqual(args.foo							,							"""toto""")

								UpperCAmelCase__					:   Union[str, Any]       				=       parser.parse_args(["""--foo""", """titi"""])
								self.assertEqual(args.foo							,							"""titi""")

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




					def 			snake_case__  ( self) ->    Optional[int]:
								UpperCAmelCase__					:   int       				=       HfArgumentParser(_lowerCamelCase)

								UpperCAmelCase__					:   str       				=       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)

								UpperCAmelCase__					:   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])							,							)

								UpperCAmelCase__					:   int       				=       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 			snake_case__  ( self) ->    List[str]:
								UpperCAmelCase__					:   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)

								UpperCAmelCase__					:   Optional[Any]       				=       [OptionalExample]
								if is_python_no_less_than_3_10:
											dataclass_types.append(_lowerCamelCase)

								for dataclass_type in dataclass_types:
											UpperCAmelCase__					:   str       				=       HfArgumentParser(_lowerCamelCase)

											self.argparsersEqual(_lowerCamelCase							,							_lowerCamelCase)

											UpperCAmelCase__					:   Optional[int]       				=       parser.parse_args([])
											self.assertEqual(_lowerCamelCase							,							Namespace(foo=_lowerCamelCase							,							bar=_lowerCamelCase							,							baz=_lowerCamelCase							,							ces=[]							,							des=[]))

											UpperCAmelCase__					:   List[str]       				=       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 			snake_case__  ( self) ->    str:
								UpperCAmelCase__					:   List[str]       				=       HfArgumentParser(_lowerCamelCase)

								UpperCAmelCase__					:   Tuple       				=       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 			snake_case__  ( self) ->    Tuple:
								UpperCAmelCase__					:   Optional[Any]       				=       HfArgumentParser(_lowerCamelCase)

								UpperCAmelCase__					:   str       				=       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 			snake_case__  ( self) ->    Dict:
								UpperCAmelCase__					:   List[Any]       				=       HfArgumentParser(_lowerCamelCase)

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

								UpperCAmelCase__					:   Dict       				=       parser.parse_dict(_lowerCamelCase)[0]
								UpperCAmelCase__					:   Union[str, Any]       				=       BasicExample(**_lowerCamelCase)
								self.assertEqual(_lowerCamelCase							,							_lowerCamelCase)




					def 			snake_case__  ( self) ->    Optional[Any]:
								UpperCAmelCase__					:   Any       				=       HfArgumentParser(_lowerCamelCase)

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

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




					def 			snake_case__  ( self) ->    Any:
								UpperCAmelCase__					:   int       				=       HfArgumentParser(_lowerCamelCase)

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

								UpperCAmelCase__					:   Tuple       				=       BasicExample(**_lowerCamelCase)
								self.assertEqual(_lowerCamelCase							,							_lowerCamelCase)




					def 			snake_case__  ( self) ->    Optional[Any]:
								UpperCAmelCase__					:   Optional[Any]       				=       HfArgumentParser(_lowerCamelCase)

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




					def 			snake_case__  ( self) ->    List[str]:
								UpperCAmelCase__					:   Union[str, Any]       				=       HfArgumentParser(_lowerCamelCase)
								self.assertIsNotNone(_lowerCamelCase)



'''simple docstring'''



from __future__ import annotations

import unittest

from transformers import is_tf_available
from transformers.testing_utils import require_sentencepiece, require_tf, require_tokenizers, slow


if is_tf_available():
		import tensorflow as tf

		from transformers import AutoTokenizer, TFAutoModelForSeqaSeqLM


@require_tf
@require_sentencepiece
@require_tokenizers
class 						_snake_case   (       unittest.TestCase		):




					@slow
					def 			snake_case__  ( self):
								UpperCAmelCase__					:   Optional[int]       				=       TFAutoModelForSeqaSeqLM.from_pretrained("""google/mt5-small""")
								UpperCAmelCase__					:   str       				=       AutoTokenizer.from_pretrained("""google/mt5-small""")

								UpperCAmelCase__					:   Optional[Any]       				=       tokenizer("""Hello there"""							,							return_tensors="""tf""").input_ids
								UpperCAmelCase__					:   Tuple       				=       tokenizer("""Hi I am"""							,							return_tensors="""tf""").input_ids

								UpperCAmelCase__					:   List[Any]       				=       model(_lowerCamelCase							,							labels=_lowerCamelCase).loss
								UpperCAmelCase__					:   Tuple       				=       -tf.math.reduce_mean(_lowerCamelCase).numpy()

								UpperCAmelCase__					:   Optional[int]       				=       -21.228168
								self.assertTrue(abs(mtf_score - EXPECTED_SCORE) < 2e-4)