Captures the CLI parameters passed to `function` without running `function` and saves them to the checkpoint.
(function: callable, checkpoint_dir: Path)
| 465 | |
| 466 | |
| 467 | def save_hyperparameters(function: callable, checkpoint_dir: Path) -> None: |
| 468 | """Captures the CLI parameters passed to `function` without running `function` and saves them to the checkpoint.""" |
| 469 | from jsonargparse import capture_parser |
| 470 | |
| 471 | # TODO: Make this more robust |
| 472 | # This hack strips away the subcommands from the top-level CLI |
| 473 | # to parse the file as if it was called as a script |
| 474 | known_commands = [ |
| 475 | ('finetune', 'full'), |
| 476 | ('finetune', 'lora'), |
| 477 | ('finetune', 'adapter'), |
| 478 | ('finetune', 'adapter_v2'), |
| 479 | ('pretrain',), |
| 480 | ] |
| 481 | for known_command in known_commands: |
| 482 | unwanted = slice(1, 1 + len(known_command)) |
| 483 | if tuple(sys.argv[unwanted]) == known_command: |
| 484 | sys.argv[unwanted] = [] |
| 485 | |
| 486 | parser = capture_parser(lambda: CLI(function)) |
| 487 | config = parser.parse_args() |
| 488 | parser.save(config, checkpoint_dir / 'hyperparameters.yaml', overwrite=True) |
| 489 | |
| 490 | |
| 491 | def save_config(config: 'Config', checkpoint_dir: Path) -> None: |