Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Argument parsing depends on input argument order #328

Open
RaulPPelaez opened this issue May 23, 2024 · 0 comments
Open

Argument parsing depends on input argument order #328

RaulPPelaez opened this issue May 23, 2024 · 0 comments

Comments

@RaulPPelaez
Copy link
Collaborator

The argument parsing mechanism assumes that arguments (CLI or in the yaml file) will be processed in always the same order, but Argparse offers no such guarantee.

    parser.add_argument('--load-model', action=LoadFromCheckpoint, help='Restart training using a model checkpoint')  # keep first
    parser.add_argument('--conf', '-c', type=open, action=LoadFromFile, help='Configuration yaml file')  # keep second
    parser.add_argument('--num-epochs', default=300, type=int, help='number of epochs')

This assumes --load-model will always be handled before the rest, but it seems like the actual order is the one given in the CLI.
Argparse offers no way to change this behavior, so instead we could make these options store a string and then manually process them in the order we need, so something like:

    parser.add_argument('--load-model', type=str, help='Restart training using a model checkpoint')  # keep first
    parser.add_argument('--conf', '-c', type=str, help='Configuration yaml file')  # keep second
    parser.add_argument('--num-epochs', default=300, type=int, help='number of epochs')
...
   input_args = parser.parse_args()
   args = {}
   if input_args.load_model:
       args = LoadFromCheckpoint(input_args.load_model)
       del input_args.load_model
   if input_args.conf:
      args.update(LoadFromFile(input_args.conf))
      del input_args.conf
   for k,v in input_args.dict():
       args[k] = v
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant