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

Add ckpt_path option to LightningModule.test() introduced robustness issues #2275

Closed
mstewart141 opened this issue Jun 19, 2020 · 5 comments
Closed
Labels
bug Something isn't working help wanted Open to be worked on priority: 0 High priority task
Milestone

Comments

@mstewart141
Copy link

🐛 Bug

Trainer.test(), passing no explicit model, now looks for the best model checkpoint. However, this can fail for models that have __init__ methods that take more than just hparams, which is common.

If I have a pl model with init (kw)args (self, hparams: Namespace, fancy_thing: FancyThing) one runs into problems outlined below.

With the new codepath, Trainer.test() calls down via this path

        # if model is not given (None), ckpt_path is given,
        # load the given checkpoint for testing
        if model is None and ckpt_path is not None:
            # ckpt_path is 'best' so load the best model
            if ckpt_path == 'best':
                ckpt_path = self.checkpoint_callback.best_model_path
            model = self.get_model().load_from_checkpoint(ckpt_path)

Which soon calls down to pytorch_lightning/core/saving.py @ _load_model_state.

This correctly collects init_args_name (here hparams, fancy_thing) but the subsequent logic does not check whether all the requisite params have been found

            if args_name == 'kwargs':
                cls_kwargs = {k: v for k, v in model_args.items() if k in init_args_name}
                kwargs.update(**cls_kwargs)
            elif args_name:
                if args_name in init_args_name:
                    kwargs.update({args_name: model_args})
            else:
                args = (model_args, ) + args

        # load the state_dict on the model automatically
        model = cls(*args, **kwargs)

This means the final line of that snippet fails because fancy_thing is not passed to cls.

Even if it were checked, though, there is not a mechanism here to recover fancy_thing. Lightning might need to e.g. pickle the model init args to rehydrate them later, or provide a user hook specifying how the rehydration here should occur. That said, there may be a simple work around I do not see immediately.

To Reproduce

Call Trainer.test() passing no model, where the implicit model has init args that are not just hparams.

Code sample

I've tried to document very clearly above the code flow that triggers this, but please @ me for any more context.

Expected behavior

Rehydration should occur properly, or, as a stopgap, an error should be raised explaining that model must be explicitly be passed to Trainer.test in this case.

Additional context

Thanks for the great library; really appreciate the team's work!

@mstewart141 mstewart141 added bug Something isn't working help wanted Open to be worked on labels Jun 19, 2020
@mstewart141 mstewart141 changed the title [Trainer.test] Add ckpt_path option to LightningModule.test() introduced robustness issues [Trainer.test][0.8.x release] Add ckpt_path option to LightningModule.test() introduced robustness issues Jun 19, 2020
@Borda Borda changed the title [Trainer.test][0.8.x release] Add ckpt_path option to LightningModule.test() introduced robustness issues Add ckpt_path option to LightningModule.test() introduced robustness issues Jun 19, 2020
@Borda Borda added this to the 0.8.x milestone Jun 19, 2020
@Borda Borda added the priority: 0 High priority task label Jun 19, 2020
@williamFalcon
Copy link
Contributor

@yukw777

@yukw777
Copy link
Contributor

yukw777 commented Jun 22, 2020

@williamFalcon @Borda it seems like this is related to the hparams deprecation work you guys did? I thought we pickled the init arguments though... why wouldn't fancy_thing get pickled from the example?

@yukw777
Copy link
Contributor

yukw777 commented Jun 24, 2020

Ahh I think I know what’s going on.. would it be OK for us to accept extra args and kwargs that’d be passed into the model init function in test()?

@yukw777
Copy link
Contributor

yukw777 commented Jul 24, 2020

We can close this per #2423 (comment)

@Borda
Copy link
Member

Borda commented Jul 25, 2020

resolved in #2681

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Open to be worked on priority: 0 High priority task
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants