




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





def      a_							(      __snake_case							:			Union[str, Any]=None   ,       __snake_case							:			Union[str, Any]=None   )							->						int:



       """simple docstring"""
       return field(default_factory=lambda: default   ,       metadata=__snake_case   )


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





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





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





class   __UpperCamelCase       ( lowerCamelCase__				):
       lowercase	:    Union[str, Any]      						='titi'
       lowercase	:    Union[str, Any]      						='toto'





class   __UpperCamelCase       ( lowerCamelCase__				):
       lowercase	:    Optional[int]      						='titi'
       lowercase	:    Dict      						='toto'
       lowercase	:    int      						=42





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






       def      lowercase__     (							self  ):



              """simple docstring"""







              lowerCamelCase_							=BasicEnum(self.foo  )





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






       def      lowercase__     (							self  ):



              """simple docstring"""







              lowerCamelCase_							=MixedTypeEnum(self.foo  )





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





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





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






       def      lowercase__     (							self  ):



              """simple docstring"""







              lowerCamelCase_							=BasicEnum(self.required_enum  )





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


if is_python_no_less_than_3_10:


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





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


class   __UpperCamelCase       ( unittest.TestCase				):






       def      lowercase__     (							self,		lowerCAmelCase,		lowerCAmelCase  ):



              """simple docstring"""







              self.assertEqual(len(a._actions  ),		len(b._actions  )  )
              for x, y in zip(a._actions,		b._actions  ):
                     lowerCamelCase_							={k: v for k, v in vars(lowerCAmelCase  ).items() if k != '''container'''}
                     lowerCamelCase_							={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      lowercase__     (							self  ):



              """simple docstring"""







              lowerCamelCase_							=HfArgumentParser(lowerCAmelCase  )

              lowerCamelCase_							=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  )

              lowerCamelCase_							=['''--foo''', '''1''', '''--baz''', '''quux''', '''--bar''', '''0.5''']
              ((lowerCamelCase_),      )							=parser.parse_args_into_dataclasses(lowerCAmelCase,		look_for_args_file=lowerCAmelCase  )
              self.assertFalse(example.flag  )






       def      lowercase__     (							self  ):



              """simple docstring"""







              lowerCamelCase_							=HfArgumentParser(lowerCAmelCase  )

              lowerCamelCase_							=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      lowercase__     (							self  ):



              """simple docstring"""







              lowerCamelCase_							=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  )

              lowerCamelCase_							=[WithDefaultBoolExample]
              if is_python_no_less_than_3_10:
                     dataclass_types.append(lowerCAmelCase  )

              for dataclass_type in dataclass_types:
                     lowerCamelCase_							=HfArgumentParser(lowerCAmelCase  )
                     self.argparsersEqual(lowerCAmelCase,		lowerCAmelCase  )

                     lowerCamelCase_							=parser.parse_args([]  )
                     self.assertEqual(lowerCAmelCase,		Namespace(foo=lowerCAmelCase,		baz=lowerCAmelCase,		opt=lowerCAmelCase  )  )

                     lowerCamelCase_							=parser.parse_args(['''--foo''', '''--no_baz''']  )
                     self.assertEqual(lowerCAmelCase,		Namespace(foo=lowerCAmelCase,		baz=lowerCAmelCase,		opt=lowerCAmelCase  )  )

                     lowerCamelCase_							=parser.parse_args(['''--foo''', '''--baz''']  )
                     self.assertEqual(lowerCAmelCase,		Namespace(foo=lowerCAmelCase,		baz=lowerCAmelCase,		opt=lowerCAmelCase  )  )

                     lowerCamelCase_							=parser.parse_args(['''--foo''', '''True''', '''--baz''', '''True''', '''--opt''', '''True''']  )
                     self.assertEqual(lowerCAmelCase,		Namespace(foo=lowerCAmelCase,		baz=lowerCAmelCase,		opt=lowerCAmelCase  )  )

                     lowerCamelCase_							=parser.parse_args(['''--foo''', '''False''', '''--baz''', '''False''', '''--opt''', '''False''']  )
                     self.assertEqual(lowerCAmelCase,		Namespace(foo=lowerCAmelCase,		baz=lowerCAmelCase,		opt=lowerCAmelCase  )  )






       def      lowercase__     (							self  ):



              """simple docstring"""







              lowerCamelCase_							=HfArgumentParser(lowerCAmelCase  )

              lowerCamelCase_							=argparse.ArgumentParser()
              expected.add_argument(
                  '''--foo''',		default='''toto''',		choices=['''titi''', '''toto''', 42],		type=make_choice_type_function(['''titi''', '''toto''', 42]  ),		)
              self.argparsersEqual(lowerCAmelCase,		lowerCAmelCase  )

              lowerCamelCase_							=parser.parse_args([]  )
              self.assertEqual(args.foo,		'''toto'''  )
              lowerCamelCase_							=parser.parse_args_into_dataclasses([]  )[0]
              self.assertEqual(enum_ex.foo,		MixedTypeEnum.toto  )

              lowerCamelCase_							=parser.parse_args(['''--foo''', '''titi''']  )
              self.assertEqual(args.foo,		'''titi'''  )
              lowerCamelCase_							=parser.parse_args_into_dataclasses(['''--foo''', '''titi''']  )[0]
              self.assertEqual(enum_ex.foo,		MixedTypeEnum.titi  )

              lowerCamelCase_							=parser.parse_args(['''--foo''', '''42''']  )
              self.assertEqual(args.foo,		42  )
              lowerCamelCase_							=parser.parse_args_into_dataclasses(['''--foo''', '''42''']  )[0]
              self.assertEqual(enum_ex.foo,		MixedTypeEnum.fourtytwo  )






       def      lowercase__     (							self  ):



              """simple docstring"""







              @dataclass
              class   __UpperCamelCase       :
                     lowercase	:    Literal["titi", "toto", 42]      						="toto"





              lowerCamelCase_							=HfArgumentParser(lowerCAmelCase  )

              lowerCamelCase_							=argparse.ArgumentParser()
              expected.add_argument(
                  '''--foo''',		default='''toto''',		choices=('''titi''', '''toto''', 42),		type=make_choice_type_function(['''titi''', '''toto''', 42]  ),		)
              self.argparsersEqual(lowerCAmelCase,		lowerCAmelCase  )

              lowerCamelCase_							=parser.parse_args([]  )
              self.assertEqual(args.foo,		'''toto'''  )

              lowerCamelCase_							=parser.parse_args(['''--foo''', '''titi''']  )
              self.assertEqual(args.foo,		'''titi'''  )

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






       def      lowercase__     (							self  ):



              """simple docstring"""







              lowerCamelCase_							=HfArgumentParser(lowerCAmelCase  )

              lowerCamelCase_							=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  )

              lowerCamelCase_							=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]  ),		)

              lowerCamelCase_							=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      lowercase__     (							self  ):



              """simple docstring"""







              lowerCamelCase_							=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  )

              lowerCamelCase_							=[OptionalExample]
              if is_python_no_less_than_3_10:
                     dataclass_types.append(lowerCAmelCase  )

              for dataclass_type in dataclass_types:
                     lowerCamelCase_							=HfArgumentParser(lowerCAmelCase  )

                     self.argparsersEqual(lowerCAmelCase,		lowerCAmelCase  )

                     lowerCamelCase_							=parser.parse_args([]  )
                     self.assertEqual(lowerCAmelCase,		Namespace(foo=lowerCAmelCase,		bar=lowerCAmelCase,		baz=lowerCAmelCase,		ces=[],		des=[]  )  )

                     lowerCamelCase_							=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.1_4,		baz='''42''',		ces=['''a''', '''b''', '''c'''],		des=[1, 2, 3]  )  )






       def      lowercase__     (							self  ):



              """simple docstring"""







              lowerCamelCase_							=HfArgumentParser(lowerCAmelCase  )

              lowerCamelCase_							=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      lowercase__     (							self  ):



              """simple docstring"""







              lowerCamelCase_							=HfArgumentParser(lowerCAmelCase  )

              lowerCamelCase_							=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      lowercase__     (							self  ):



              """simple docstring"""







              lowerCamelCase_							=HfArgumentParser(lowerCAmelCase  )

              lowerCamelCase_							={
                  '''foo''': 12,
                  '''bar''': 3.1_4,
                  '''baz''': '''42''',
                  '''flag''': True,
              }

              lowerCamelCase_							=parser.parse_dict(lowerCAmelCase  )[0]
              lowerCamelCase_							=BasicExample(**lowerCAmelCase  )
              self.assertEqual(lowerCAmelCase,		lowerCAmelCase  )






       def      lowercase__     (							self  ):



              """simple docstring"""







              lowerCamelCase_							=HfArgumentParser(lowerCAmelCase  )

              lowerCamelCase_							={
                  '''foo''': 12,
                  '''bar''': 3.1_4,
                  '''baz''': '''42''',
                  '''flag''': True,
                  '''extra''': 42,
              }

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






       def      lowercase__     (							self  ):



              """simple docstring"""







              lowerCamelCase_							=HfArgumentParser(lowerCAmelCase  )

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

              lowerCamelCase_							=BasicExample(**lowerCAmelCase  )
              self.assertEqual(lowerCAmelCase,		lowerCAmelCase  )






       def      lowercase__     (							self  ):



              """simple docstring"""







              lowerCamelCase_							=HfArgumentParser(lowerCAmelCase  )

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






       def      lowercase__     (							self  ):



              """simple docstring"""







              lowerCamelCase_							=HfArgumentParser(lowerCAmelCase  )
              self.assertIsNotNone(lowerCAmelCase  )











import argparse
from pathlib import Path

import torch

from transformers import OPTConfig, OPTModel
from transformers.utils import logging


logging.set_verbosity_info()
lowercase      :       Any									=  logging.get_logger(__name__)



def A_	(		A__				)  ->		List[str]:
       a__						:	Union[str, Any] =				torch.load(A__		,      map_location='cpu'				)
       if "model" in sd.keys():
              a__						:	Tuple =				torch.load(A__		,      map_location='cpu'				)['model']

       # pop unnecessary weights
       a__						:	Optional[Any] =				[
           'decoder.version',
           'decoder.output_projection.weight',
       ]
       for key in keys_to_delete:
              if key in sd:
                     sd.pop(A__				)

       a__						:	Union[str, Any] =				{
           'decoder.project_in_dim.weight': 'decoder.project_in.weight',
           'decoder.project_out_dim.weight': 'decoder.project_out.weight',
           'decoder.layer_norm.weight': 'decoder.final_layer_norm.weight',
           'decoder.layer_norm.bias': 'decoder.final_layer_norm.bias',
       }
       for old_key, new_key in keys_to_rename.items():
              if old_key in sd:
                     a__						:	Any =				sd.pop(A__				)

       a__						:	Optional[int] =				list(sd.keys()				)
       for key in keys:
              if ".qkv_proj." in key:
                     a__						:	str =				sd[key]
                     # We split QKV in separate Q,K,V

                     a__						:	Union[str, Any] =				key.replace('.qkv_proj.'		,      '.q_proj.'				)
                     a__						:	int =				key.replace('.qkv_proj.'		,      '.k_proj.'				)
                     a__						:	Optional[int] =				key.replace('.qkv_proj.'		,      '.v_proj.'				)

                     a__						:	List[Any] =				value.shape[0]
                     assert depth % 3 == 0
                     # `SequeuceParallelTransformerBlock` has QKV weight is separated in K,V,Q despite the naming:
                     # https://cs.github.com/facebookresearch/metaseq/blob/51871bd73cd04c038f239ea2a26db1d7f6b37927/metaseq/modules/sequence_parallel_transformer_layer.py#L97
                     a__ ,							a__ ,							a__						:	Any =				torch.split(A__		,      depth // 3		,      dim=0				)

                     a__						:	int =				q
                     a__						:	List[Any] =				k
                     a__						:	int =				v
                     del sd[key]

       return sd



@torch.no_grad()
def A_	(		A__		,      A__		,      A__=None				)  ->		Union[str, Any]:
       a__						:	Union[str, Any] =				load_checkpoint(A__				)

       if config is not None:
              a__						:	List[Any] =				OPTConfig.from_pretrained(A__				)
       else:
              a__						:	int =				OPTConfig()

       a__						:	Optional[int] =				OPTModel(A__				).half().eval()
       model.load_state_dict(A__				)

       # Check results
       Path(A__				).mkdir(exist_ok=A__				)
       model.save_pretrained(A__				)


if __name__ == "__main__":
    lowercase      :       str									=  argparse.ArgumentParser()
    # Required parameters
    parser.add_argument(
        """--fairseq_path""",
        type=str,
        help=(
            """path to fairseq checkpoint in correct format. You can find all checkpoints in the correct format here:"""
            """ https://huggingface.co/models?other=opt_metasq"""
        ),
    )
    parser.add_argument("""--pytorch_dump_folder_path""", default=None, type=str, help="""Path to the output PyTorch model.""")
    parser.add_argument("""--hf_config""", default=None, type=str, help="""Define HF config.""")
    lowercase      :       Any									=  parser.parse_args()
    convert_opt_checkpoint(args.fairseq_path, args.pytorch_dump_folder_path, config=args.hf_config)


