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

Fix metric attribute lookup #8181

Merged
merged 3 commits into from
Jun 28, 2021
Merged

Fix metric attribute lookup #8181

merged 3 commits into from
Jun 28, 2021

Conversation

ethanwharris
Copy link
Member

@ethanwharris ethanwharris commented Jun 28, 2021

What does this PR do?

Fixes #8179

Before submitting

  • Was this discussed/approved via a GitHub issue? (not for typos and docs)
  • 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?
  • Did you make sure to update the documentation with your changes? (if necessary)
  • Did you write any new necessary tests? (not for typos and docs)
  • Did you verify new and existing tests pass locally with your changes?
  • Did you update the CHANGELOG? (not for typos, docs, test updates, or internal minor changes/refactorings)

PR review

Anyone in the community is free to review the PR once the tests have passed.
Before you start reviewing make sure you have read Review guidelines. In short, see the following bullet-list:

  • Is this pull request ready for review? (if not, please submit in draft mode)
  • Check that all items from Before submitting are resolved
  • Make sure the title is self-explanatory and the description concisely explains the PR
  • Add labels and milestones (and optionally projects) to the PR so it can be classified

Did you have fun?

Make sure you had fun coding 🙃

@codecov
Copy link

codecov bot commented Jun 28, 2021

Codecov Report

Merging #8181 (de7c25a) into master (c4492ad) will decrease coverage by 0%.
The diff coverage is 100%.

@@           Coverage Diff           @@
##           master   #8181    +/-   ##
=======================================
- Coverage      93%     92%    -0%     
=======================================
  Files         211     212     +1     
  Lines       13450   13810   +360     
=======================================
+ Hits        12486   12766   +280     
- Misses        964    1044    +80     

Copy link
Contributor

@tchaton tchaton left a comment

Choose a reason for hiding this comment

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

LGTM ! Great catch !

@SkafteNicki SkafteNicki enabled auto-merge (squash) June 28, 2021 19:43
@SkafteNicki SkafteNicki merged commit b1d8840 into master Jun 28, 2021
@SkafteNicki SkafteNicki deleted the bugfix/metrics_list_dict branch June 28, 2021 20:17
Comment on lines +236 to +245
self.a_metric = SumMetric()

def training_step(self, *args):
metric = SumMetric()
self.log("foo", metric)

trainer = Trainer(default_root_dir=tmpdir, fast_dev_run=1)
model = TestModel()
with pytest.raises(MisconfigurationException, match=escape("where `name` is one of ['a_metric']")):
trainer.fit(model)
Copy link
Contributor

Choose a reason for hiding this comment

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

@carmocca why do we need this dependency on the attribute name in the module vs the key name used for publishing? if it's solely for restoration, could we think of other approaches? this style of metrics logging is a very common pattern for us

Choose a reason for hiding this comment

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

Not @carmocca, but I'll take a shot at this - I don't think there is a dependency on key name for publishing. This test is failing because the metric being logged isn't the same instance as the self.a_metric metric that's an attribute in the module.

test_log_metric_dict below logs some metrics that have key name != attribute name and still passes

Copy link
Contributor

Choose a reason for hiding this comment

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

@mooey5775 is correct.

This is to match the logged metric with the metric attribute in the LightningModule so the state can be restored.

could we think of other approaches?

Open to ideas!

@carmocca carmocca mentioned this pull request Jun 29, 2021
10 tasks
@awaelchli awaelchli added the bug Something isn't working label Jun 29, 2021
@awaelchli awaelchli added this to the v1.3.x milestone Jun 29, 2021
@awaelchli awaelchli removed the bug Something isn't working label Jun 29, 2021
@awaelchli awaelchli removed this from the v1.3.x milestone Jun 29, 2021
@carmocca carmocca added this to the v1.4 milestone Jun 29, 2021
@kandluis
Copy link
Contributor

Let's say I have some metrics which I don't want to restore/track (and therefore, this metric attribute is no needed).

What's the recommended way to log these metrics in that case?

@carmocca
Copy link
Contributor

which I don't want to restore/track (and therefore, this metric attribute is no needed).

But it's still a stateful metric? Can't change to a functional counterpart?

What's the recommended way to log these metrics in that case?

There isn't currently for this as the log method requires it if a torchmetrics.Metric is passed.
Can you open a feature request about it?

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

Successfully merging this pull request may close these issues.

.log no longer supports nested metric attributes (e.g. if stored in ModuleDict)
9 participants