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

warn user when dropping unpicklable hparams #2874

Merged
merged 7 commits into from
Aug 28, 2020
Merged

Conversation

monney
Copy link
Contributor

@monney monney commented Aug 7, 2020

What does this PR do?

PR changes namespace cleaning to be more general. Makes sure it only removes items which can't be pickled, and warns user when doing so.

Fixes #2676
Fixes #2444

Before submitting

  • Was this discussed/approved via a Github issue? (no need for typos and docs improvements)
  • Did you read the contributor guideline, Pull Request section?
  • Did you make sure your PR does only one thing, instead of bundling different changes together? Otherwise, we ask you to create a separate PR for every change.
  • Did you make sure to update the documentation with your changes?
  • Did you write any new necessary tests?
  • Did you verify new and existing tests pass locally with your changes?
  • If you made a notable change (that affects users), did you update the CHANGELOG?

PR review

Anyone in the community is free to review the PR once the tests have passed.
If we didn't discuss your PR in Github issues there's a high chance it will not be merged.

Did you have fun?

Make sure you had fun coding 🙃

@pep8speaks
Copy link

pep8speaks commented Aug 7, 2020

Hello @monney! Thanks for updating this PR.

There are currently no PEP 8 issues detected in this Pull Request. Cheers! 🍻

Comment last updated at 2020-08-11 18:44:01 UTC

@mergify mergify bot requested a review from a team August 7, 2020 23:59
@monney monney changed the title [WIPrefactored clean_namespace [WIP] refactored clean_namespace Aug 7, 2020
@awaelchli
Copy link
Contributor

@monney I updated your pr descripion. Since you added a warning, I believe this also solves a the issue #2444.

@ananyahjha93 ananyahjha93 self-requested a review August 8, 2020 02:54
@awaelchli
Copy link
Contributor

awaelchli commented Aug 8, 2020

@monney is this wip or ready to review? at this point I would suggest to extract a helper function is_pickleable(obj: object) -> bool to avoid duplicated code as it is useful in other places too. Thanks for your efforts.

EDIT: oops just saw tests are failing, I will check what's going on.

@awaelchli awaelchli added bug Something isn't working feature Is an improvement or enhancement labels Aug 8, 2020
@awaelchli awaelchli added this to the 0.9.0 milestone Aug 8, 2020
@monney monney changed the title [WIP] refactored clean_namespace refactored clean_namespace Aug 8, 2020
@monney
Copy link
Contributor Author

monney commented Aug 8, 2020

@awaelchli Should be good to go now. Build is failing, but I believe the test case is broken. Should be checking for nn.CosineEmbeddingLoss but is checking for nn.CrossEntropyLoss.

@monney monney changed the title refactored clean_namespace [WIP] refactored clean_namespace Aug 8, 2020
@awaelchli awaelchli self-assigned this Aug 9, 2020
Copy link
Contributor

@awaelchli awaelchli left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@monney I can confirm, test case needs to be fixed by checking for CosineEmbeddingLoss. While debugging this I wrote a test case for your bugfix, could you add it, maybe below test_hparams_pickle?

class UnpickleableArgsEvalModel(EvalModelTemplate):
    """ A model that has an attribute that cannot be pickled. """

    def __init__(self, foo='bar', pickle_me=(lambda x: x + 1), **kwargs):
        super().__init__(**kwargs)
        assert not is_picklable(pickle_me)
        self.save_hyperparameters()


def test_hparams_pickle_warning(tmpdir):
    model = UnpickleableArgsEvalModel()
    trainer = Trainer(default_root_dir=tmpdir, max_steps=1)
    with pytest.warns(UserWarning, match="attribute 'pickle_me' removed from hparams because it cannot be pickled"):
        trainer.fit(model)
    assert 'pickle_me' not in model.hparams

With this test case I think the PR is ready 👍

pytorch_lightning/utilities/parsing.py Outdated Show resolved Hide resolved
@mergify mergify bot requested a review from a team August 9, 2020 07:05
@monney monney changed the title [WIP] refactored clean_namespace refactored clean_namespace Aug 9, 2020
@monney
Copy link
Contributor Author

monney commented Aug 9, 2020

@awaelchli I added your test case and changed the warning to rank_zero_warn, I think everything should be good to go now!

@awaelchli
Copy link
Contributor

@monney There are test failures, they must be because of the new way of cleaning the namespace, but not sure what needs to be changed. Have you seen it?

@monney
Copy link
Contributor Author

monney commented Aug 11, 2020

@awaelchli it is because of the new way of cleaning. Something prevented that test case either from being run or failing previously. The property that it’s checking for would have been completely removed by the old way of cleaning, I’m not sure why that did not trigger a failure though. The new way causes it to fail because it should be checking for CosineEmbedding.

@awaelchli
Copy link
Contributor

Ah I thought I already confirmed it. Yes, please change to CosineEmbeddingLoss I agree.

@monney
Copy link
Contributor Author

monney commented Aug 11, 2020

You did. That’s my bad. Doesn’t it need to be changed in the CI server? Or if I change it in my code will it update

@awaelchli
Copy link
Contributor

It's in the same file you added the test, above in a fuction called test_collect_init_arguments. You change it there, push your commit and it will show here in the tab for changed files :)

@monney
Copy link
Contributor Author

monney commented Aug 11, 2020

I got it now, I didn't realize it ran the test cases, from the commit itself, and thought you had update it elsewhere. Thank you! :)

@codecov
Copy link

codecov bot commented Aug 18, 2020

Codecov Report

Merging #2874 into master will decrease coverage by 1%.
The diff coverage is 92%.

@@           Coverage Diff            @@
##           master   #2874     +/-   ##
========================================
- Coverage      90%     89%     -1%     
========================================
  Files          79      81      +2     
  Lines        7226    9204   +1978     
========================================
+ Hits         6519    8235   +1716     
- Misses        707     969    +262     

@awaelchli awaelchli changed the title refactored clean_namespace warn user when dropping unpicklable hparams Aug 19, 2020
Copy link
Contributor

@awaelchli awaelchli left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@monney thanks for the fix

@mergify mergify bot requested a review from a team August 19, 2020 00:03
Copy link
Contributor

@nateraw nateraw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 🚀

pytorch_lightning/utilities/parsing.py Show resolved Hide resolved
Copy link
Member

@SkafteNicki SkafteNicki left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

@edenlightning edenlightning modified the milestones: 0.9.0, 0.9.x Aug 20, 2020
@jeremyjordan jeremyjordan added the ready PRs ready to be merged label Aug 21, 2020
@@ -25,26 +28,28 @@ def str_to_bool(val):
raise ValueError(f'invalid truth value {val}')


def is_picklable(obj: object) -> bool:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this can be quite a time costly especially for large objects or calling this function quite often...

@Borda Borda merged commit d5254ff into Lightning-AI:master Aug 28, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working feature Is an improvement or enhancement ready PRs ready to be merged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Namespace Cleaning self.hparam silently removes params that are not serializable
8 participants