Argument Parser to define and parse command-line args for training. Args: training_args: dict or list of dict which defines different parameters for training.
| 4 | |
| 5 | |
| 6 | class CliArgumentParser(ArgumentParser): |
| 7 | """ Argument Parser to define and parse command-line args for training. |
| 8 | |
| 9 | Args: |
| 10 | training_args: dict or list of dict which defines different |
| 11 | parameters for training. |
| 12 | """ |
| 13 | |
| 14 | def __init__(self, training_args=None, **kwargs): |
| 15 | if 'formatter_class' not in kwargs: |
| 16 | kwargs['formatter_class'] = ArgumentDefaultsHelpFormatter |
| 17 | super().__init__(**kwargs) |
| 18 | self.training_args = training_args |
| 19 | self.define_args() |
| 20 | |
| 21 | def get_manual_args(self, args): |
| 22 | return [arg[2:] for arg in args if arg.startswith('--')] |
| 23 | |
| 24 | def _parse_known_args(self, |
| 25 | args: List = None, |
| 26 | namespace=None, |
| 27 | *args_extra, |
| 28 | **kwargs): |
| 29 | self.model_id = namespace.model if namespace is not None else None |
| 30 | if '--model' in args: |
| 31 | self.model_id = args[args.index('--model') + 1] |
| 32 | self.manual_args = self.get_manual_args(args) |
| 33 | return super()._parse_known_args(args, namespace, *args_extra, |
| 34 | **kwargs) |
| 35 | |
| 36 | def print_help(self, file=None): |
| 37 | return super().print_help(file) |
| 38 | |
| 39 | def define_args(self): |
| 40 | if self.training_args is not None: |
| 41 | for f in fields(self.training_args): |
| 42 | arg_name = f.name |
| 43 | arg_attr = getattr(self.training_args, f.name) |
| 44 | name = f'--{arg_name}' |
| 45 | kwargs = dict(type=f.type, help=f.metadata['help']) |
| 46 | kwargs['default'] = arg_attr |
| 47 | |
| 48 | if 'choices' in f.metadata: |
| 49 | kwargs['choices'] = f.metadata['choices'] |
| 50 | |
| 51 | kwargs['action'] = SingleAction |
| 52 | self.add_argument(name, **kwargs) |
| 53 | |
| 54 | |
| 55 | class DictAction(Action): |
no outgoing calls
searching dependent graphs…