




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







def 		lowercase__   (		__A:	List[str]=None							,__A:	Tuple=None				):





     '''simple docstring'''






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


@dataclass
class    lowerCamelCase    :






   '''simple docstring'''





   UpperCamelCase__								=4_2
   UpperCamelCase__								=4_2
   UpperCamelCase__								=4_2
   UpperCamelCase__								=4_2






@dataclass
class    lowerCamelCase    :






   '''simple docstring'''





   UpperCamelCase__								=4_2
   UpperCamelCase__								=field(default='''toto'''				,metadata={'''help''': '''help message'''}						)






@dataclass
class    lowerCamelCase    :






   '''simple docstring'''





   UpperCamelCase__								=False
   UpperCamelCase__								=True
   UpperCamelCase__								=None






class    lowerCamelCase    (		_lowerCamelCase						):






   '''simple docstring'''





   UpperCamelCase__								='''titi'''
   UpperCamelCase__								='''toto'''






class    lowerCamelCase    (		_lowerCamelCase						):






   '''simple docstring'''





   UpperCamelCase__								='''titi'''
   UpperCamelCase__								='''toto'''
   UpperCamelCase__								=4_2






@dataclass
class    lowerCamelCase    :






   '''simple docstring'''





   UpperCamelCase__								='''toto'''

   def 							UpperCAmelCase__       ( self			:   Optional[Any]  )						->   Dict:
        __magic_name__							:	List[Any]    							=							BasicEnum(self.foo  )






@dataclass
class    lowerCamelCase    :






   '''simple docstring'''





   UpperCamelCase__								='''toto'''

   def 							UpperCAmelCase__       ( self			:   str  )						->   Optional[int]:
        __magic_name__							:	Tuple    							=							MixedTypeEnum(self.foo  )






@dataclass
class    lowerCamelCase    :






   '''simple docstring'''





   UpperCamelCase__								=None
   UpperCamelCase__								=field(default=_lowerCamelCase				,metadata={'''help''': '''help message'''}						)
   UpperCamelCase__								=None
   UpperCamelCase__								=list_field(default=[]						)
   UpperCamelCase__								=list_field(default=[]						)






@dataclass
class    lowerCamelCase    :






   '''simple docstring'''





   UpperCamelCase__								=list_field(default=[]						)
   UpperCamelCase__								=list_field(default=[1, 2, 3]						)
   UpperCamelCase__								=list_field(default=['''Hallo''', '''Bonjour''', '''Hello''']						)
   UpperCamelCase__								=list_field(default=[0.1, 0.2, 0.3]						)






@dataclass
class    lowerCamelCase    :






   '''simple docstring'''





   UpperCamelCase__								=field()
   UpperCamelCase__								=field()
   UpperCamelCase__								=field()

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






@dataclass
class    lowerCamelCase    :






   '''simple docstring'''





   UpperCamelCase__								=4_2
   UpperCamelCase__								=field()
   UpperCamelCase__								=None
   UpperCamelCase__								=field(default='''toto'''				,metadata={'''help''': '''help message'''}						)
   UpperCamelCase__								=list_field(default=['''Hallo''', '''Bonjour''', '''Hello''']						)


if is_python_no_less_than_3_10:


 @dataclass
 class    lowerCamelCase    :






    '''simple docstring'''





    UpperCamelCase__								=False
    UpperCamelCase__								=True
    UpperCamelCase__								=None






 @dataclass
 class    lowerCamelCase    :






    '''simple docstring'''





    UpperCamelCase__								=None
    UpperCamelCase__								=field(default=_lowerCamelCase				,metadata={'''help''': '''help message'''}						)
    UpperCamelCase__								=None
    UpperCamelCase__								=list_field(default=[]						)
    UpperCamelCase__								=list_field(default=[]						)


class    lowerCamelCase    (		unittest.TestCase						):






   '''simple docstring'''





   def 							UpperCAmelCase__       ( self			:   int       ,						lowerCamelCase_			:   argparse.ArgumentParser       ,						lowerCamelCase_			:   argparse.ArgumentParser  )						->   List[str]:
        self.assertEqual(len(a._actions  )       ,						len(b._actions  )  )
        for x, y in zip(a._actions       ,						b._actions  ):
             __magic_name__							:	List[str]    							=							{k: v for k, v in vars(__A  ).items() if k != "container"}
             __magic_name__							:	List[Any]    							=							{k: v for k, v in vars(__A  ).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'''       ,						__A  ) and yy.get('''choices'''       ,						__A  ):
                  for expected_choice in yy["choices"] + xx["choices"]:
                       self.assertEqual(xx['''type'''](__A  )       ,						yy['''type'''](__A  )  )
                  del xx["type"], yy["type"]

             self.assertEqual(__A       ,						__A  )

   def 							UpperCAmelCase__       ( self			:   Union[str, Any]  )						->   Optional[int]:
        __magic_name__							:	Optional[int]    							=							HfArgumentParser(__A  )

        __magic_name__							:	Union[str, Any]    							=							argparse.ArgumentParser()
        expected.add_argument('''--foo'''       ,						type=__A       ,						required=__A  )
        expected.add_argument('''--bar'''       ,						type=__A       ,						required=__A  )
        expected.add_argument('''--baz'''       ,						type=__A       ,						required=__A  )
        expected.add_argument('''--flag'''       ,						type=__A       ,						default=__A       ,						const=__A       ,						nargs='''?'''  )
        self.argparsersEqual(__A       ,						__A  )

        __magic_name__							:	Any    							=							["--foo", "1", "--baz", "quux", "--bar", "0.5"]
        (__magic_name__		)							:	Tuple    							=							parser.parse_args_into_dataclasses(__A       ,						look_for_args_file=__A  )
        self.assertFalse(example.flag  )

   def 							UpperCAmelCase__       ( self			:   Union[str, Any]  )						->   List[str]:
        __magic_name__							:	Optional[int]    							=							HfArgumentParser(__A  )

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

   def 							UpperCAmelCase__       ( self			:   Tuple  )						->   str:
        __magic_name__							:	List[Any]    							=							argparse.ArgumentParser()
        expected.add_argument('''--foo'''       ,						type=__A       ,						default=__A       ,						const=__A       ,						nargs='''?'''  )
        expected.add_argument('''--baz'''       ,						type=__A       ,						default=__A       ,						const=__A       ,						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=__A       ,						dest='''baz'''  )
        expected.add_argument('''--opt'''       ,						type=__A       ,						default=__A  )

        __magic_name__							:	Tuple    							=							[WithDefaultBoolExample]
        if is_python_no_less_than_3_10:
             dataclass_types.append(__A  )

        for dataclass_type in dataclass_types:
             __magic_name__							:	str    							=							HfArgumentParser(__A  )
             self.argparsersEqual(__A       ,						__A  )

             __magic_name__							:	Optional[Any]    							=							parser.parse_args([]  )
             self.assertEqual(__A       ,						Namespace(foo=__A       ,						baz=__A       ,						opt=__A  )  )

             __magic_name__							:	Dict    							=							parser.parse_args(['''--foo''', '''--no_baz''']  )
             self.assertEqual(__A       ,						Namespace(foo=__A       ,						baz=__A       ,						opt=__A  )  )

             __magic_name__							:	Optional[Any]    							=							parser.parse_args(['''--foo''', '''--baz''']  )
             self.assertEqual(__A       ,						Namespace(foo=__A       ,						baz=__A       ,						opt=__A  )  )

             __magic_name__							:	Optional[int]    							=							parser.parse_args(['''--foo''', '''True''', '''--baz''', '''True''', '''--opt''', '''True''']  )
             self.assertEqual(__A       ,						Namespace(foo=__A       ,						baz=__A       ,						opt=__A  )  )

             __magic_name__							:	List[str]    							=							parser.parse_args(['''--foo''', '''False''', '''--baz''', '''False''', '''--opt''', '''False''']  )
             self.assertEqual(__A       ,						Namespace(foo=__A       ,						baz=__A       ,						opt=__A  )  )

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

        __magic_name__							:	Union[str, Any]    							=							argparse.ArgumentParser()
        expected.add_argument(
            '''--foo'''       ,						default='''toto'''       ,						choices=['''titi''', '''toto''', 42]       ,						type=make_choice_type_function(['''titi''', '''toto''', 42]  )       ,						)
        self.argparsersEqual(__A       ,						__A  )

        __magic_name__							:	Optional[Any]    							=							parser.parse_args([]  )
        self.assertEqual(args.foo       ,						'''toto'''  )
        __magic_name__							:	str    							=							parser.parse_args_into_dataclasses([]  )[0]
        self.assertEqual(enum_ex.foo       ,						MixedTypeEnum.toto  )

        __magic_name__							:	int    							=							parser.parse_args(['''--foo''', '''titi''']  )
        self.assertEqual(args.foo       ,						'''titi'''  )
        __magic_name__							:	Optional[Any]    							=							parser.parse_args_into_dataclasses(['''--foo''', '''titi''']  )[0]
        self.assertEqual(enum_ex.foo       ,						MixedTypeEnum.titi  )

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

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


        @dataclass
        class    lowerCamelCase    :






           '''simple docstring'''





           UpperCamelCase__								='''toto'''






        __magic_name__							:	int    							=							HfArgumentParser(__A  )

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

        __magic_name__							:	Any    							=							parser.parse_args([]  )
        self.assertEqual(args.foo       ,						'''toto'''  )

        __magic_name__							:	Optional[Any]    							=							parser.parse_args(['''--foo''', '''titi''']  )
        self.assertEqual(args.foo       ,						'''titi'''  )

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

   def 							UpperCAmelCase__       ( self			:   int  )						->   List[str]:
        __magic_name__							:	Optional[int]    							=							HfArgumentParser(__A  )

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

        self.argparsersEqual(__A       ,						__A  )

        __magic_name__							:	Union[str, Any]    							=							parser.parse_args([]  )
        self.assertEqual(
            __A       ,						Namespace(foo_int=[]       ,						bar_int=[1, 2, 3]       ,						foo_str=['''Hallo''', '''Bonjour''', '''Hello''']       ,						foo_float=[0.1, 0.2, 0.3]  )       ,						)

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

   def 							UpperCAmelCase__       ( self			:   Tuple  )						->   Union[str, Any]:
        __magic_name__							:	Optional[Any]    							=							argparse.ArgumentParser()
        expected.add_argument('''--foo'''       ,						default=__A       ,						type=__A  )
        expected.add_argument('''--bar'''       ,						default=__A       ,						type=__A       ,						help='''help message'''  )
        expected.add_argument('''--baz'''       ,						default=__A       ,						type=__A  )
        expected.add_argument('''--ces'''       ,						nargs='''+'''       ,						default=[]       ,						type=__A  )
        expected.add_argument('''--des'''       ,						nargs='''+'''       ,						default=[]       ,						type=__A  )

        __magic_name__							:	Optional[int]    							=							[OptionalExample]
        if is_python_no_less_than_3_10:
             dataclass_types.append(__A  )

        for dataclass_type in dataclass_types:
             __magic_name__							:	Any    							=							HfArgumentParser(__A  )

             self.argparsersEqual(__A       ,						__A  )

             __magic_name__							:	Union[str, Any]    							=							parser.parse_args([]  )
             self.assertEqual(__A       ,						Namespace(foo=__A       ,						bar=__A       ,						baz=__A       ,						ces=[]       ,						des=[]  )  )

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

   def 							UpperCAmelCase__       ( self			:   str  )						->   Optional[Any]:
        __magic_name__							:	Optional[Any]    							=							HfArgumentParser(__A  )

        __magic_name__							:	str    							=							argparse.ArgumentParser()
        expected.add_argument('''--required_list'''       ,						nargs='''+'''       ,						type=__A       ,						required=__A  )
        expected.add_argument('''--required_str'''       ,						type=__A       ,						required=__A  )
        expected.add_argument(
            '''--required_enum'''       ,						type=make_choice_type_function(['''titi''', '''toto''']  )       ,						choices=['''titi''', '''toto''']       ,						required=__A       ,						)
        self.argparsersEqual(__A       ,						__A  )

   def 							UpperCAmelCase__       ( self			:   Tuple  )						->   Any:
        __magic_name__							:	Optional[int]    							=							HfArgumentParser(__A  )

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

   def 							UpperCAmelCase__       ( self			:   List[str]  )						->   Any:
        __magic_name__							:	Tuple    							=							HfArgumentParser(__A  )

        __magic_name__							:	Optional[Any]    							=							{
            "foo": 12,
            "bar": 3.1_4,
            "baz": "42",
            "flag": True,
        }

        __magic_name__							:	Tuple    							=							parser.parse_dict(__A  )[0]
        __magic_name__							:	str    							=							BasicExample(**__A  )
        self.assertEqual(__A       ,						__A  )

   def 							UpperCAmelCase__       ( self			:   List[str]  )						->   Dict:
        __magic_name__							:	Tuple    							=							HfArgumentParser(__A  )

        __magic_name__							:	str    							=							{
            "foo": 12,
            "bar": 3.1_4,
            "baz": "42",
            "flag": True,
            "extra": 42,
        }

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

   def 							UpperCAmelCase__       ( self			:   Union[str, Any]  )						->   Any:
        __magic_name__							:	Optional[int]    							=							HfArgumentParser(__A  )

        __magic_name__							:	List[str]    							=							{
            "foo": 12,
            "bar": 3.1_4,
            "baz": "42",
            "flag": True,
        }
        with tempfile.TemporaryDirectory() as tmp_dir:
             __magic_name__							:	Dict    							=							os.path.join(__A       ,						'''temp_json'''  )
             os.mkdir(__A  )
             with open(temp_local_path + '''.json'''       ,						'''w+'''  ) as f:
                  json.dump(__A       ,						__A  )
             __magic_name__							:	Tuple    							=							parser.parse_yaml_file(Path(temp_local_path + '''.json'''  )  )[0]

        __magic_name__							:	List[Any]    							=							BasicExample(**__A  )
        self.assertEqual(__A       ,						__A  )

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

        __magic_name__							:	List[str]    							=							{
            "foo": 12,
            "bar": 3.1_4,
            "baz": "42",
            "flag": True,
        }
        with tempfile.TemporaryDirectory() as tmp_dir:
             __magic_name__							:	Tuple    							=							os.path.join(__A       ,						'''temp_yaml'''  )
             os.mkdir(__A  )
             with open(temp_local_path + '''.yaml'''       ,						'''w+'''  ) as f:
                  yaml.dump(__A       ,						__A  )
             __magic_name__							:	List[str]    							=							parser.parse_yaml_file(Path(temp_local_path + '''.yaml'''  )  )[0]
        __magic_name__							:	Optional[int]    							=							BasicExample(**__A  )
        self.assertEqual(__A       ,						__A  )


   def 							UpperCAmelCase__       ( self			:   List[Any]  )						->   Tuple:
        __magic_name__							:	List[str]    							=							HfArgumentParser(__A  )
        self.assertIsNotNone(__A  )









from ...configuration_utils import PretrainedConfig
from ...utils import logging


__lowerCamelCase   :						Optional[int]				=			logging.get_logger(__name__)

__lowerCamelCase   :						Dict				=			{}


class    lowerCamelCase    (		_lowerCamelCase						):






			'''simple docstring'''





			UpperCamelCase__								='''llama'''
			UpperCamelCase__								=['''past_key_values''']

			def __init__( self			:   Dict       ,						lowerCamelCase_			:   List[str]=32000       ,						lowerCamelCase_			:   List[Any]=4096       ,						lowerCamelCase_			:   Any=11008       ,						lowerCamelCase_			:   Optional[Any]=32       ,						lowerCamelCase_			:   Dict=32       ,						lowerCamelCase_			:   Optional[Any]=None       ,						lowerCamelCase_			:   Dict="silu"       ,						lowerCamelCase_			:   Optional[int]=2048       ,						lowerCamelCase_			:   int=0.0_2       ,						lowerCamelCase_			:   Tuple=1E-6       ,						lowerCamelCase_			:   Tuple=True       ,						lowerCamelCase_			:   List[str]=0       ,						lowerCamelCase_			:   List[str]=1       ,						lowerCamelCase_			:   Optional[int]=2       ,						lowerCamelCase_			:   List[str]=1       ,						lowerCamelCase_			:   Dict=False       ,						lowerCamelCase_			:   int=None       ,						**lowerCamelCase_			:   Tuple       ,						)						->   Tuple:
								__magic_name__							:	Any    							=							vocab_size
								__magic_name__							:	Optional[int]    							=							max_position_embeddings
								__magic_name__							:	Union[str, Any]    							=							hidden_size
								__magic_name__							:	Dict    							=							intermediate_size
								__magic_name__							:	Union[str, Any]    							=							num_hidden_layers
								__magic_name__							:	Tuple    							=							num_attention_heads

								# for backward compatibility
								if num_key_value_heads is None:
													__magic_name__							:	Optional[int]    							=							num_attention_heads

								__magic_name__							:	Optional[int]    							=							num_key_value_heads
								__magic_name__							:	List[str]    							=							hidden_act
								__magic_name__							:	List[str]    							=							initializer_range
								__magic_name__							:	Tuple    							=							rms_norm_eps
								__magic_name__							:	Optional[int]    							=							pretraining_tp
								__magic_name__							:	Any    							=							use_cache
								__magic_name__							:	int    							=							rope_scaling
								self._rope_scaling_validation()

								super().__init__(
								    pad_token_id=lowerCamelCase_       ,						bos_token_id=lowerCamelCase_       ,						eos_token_id=lowerCamelCase_       ,						tie_word_embeddings=lowerCamelCase_       ,						**lowerCamelCase_       ,						)


			def 							UpperCAmelCase__       ( self			:   str  )						->   Tuple:
								if self.rope_scaling is None:
													return

								if not isinstance(self.rope_scaling       ,						lowerCamelCase_  ) or len(self.rope_scaling  ) != 2:
													raise ValueError(
													    '''`rope_scaling` must be a dictionary with with two fields, `name` and `factor`, '''
													    F'''got {self.rope_scaling}'''  )
								__magic_name__							:	List[str]    							=							self.rope_scaling.get('''type'''       ,						lowerCamelCase_  )
								__magic_name__							:	str    							=							self.rope_scaling.get('''factor'''       ,						lowerCamelCase_  )
								if rope_scaling_type is None or rope_scaling_type not in ["linear", "dynamic"]:
													raise ValueError(
													    F'''`rope_scaling`\'s name field must be one of [\'linear\', \'dynamic\'], got {rope_scaling_type}'''  )
								if rope_scaling_factor is None or not isinstance(lowerCamelCase_       ,						lowerCamelCase_  ) or rope_scaling_factor <= 1.0:
													raise ValueError(F'''`rope_scaling`\'s factor field must be an float > 1, got {rope_scaling_factor}'''  )



