Skip to content

Make metrics serializable#3001

Merged
vfdev-5 merged 10 commits intopytorch:masterfrom
sadra-barikbin:Feature-make-metrics-serializable
Aug 10, 2023
Merged

Make metrics serializable#3001
vfdev-5 merged 10 commits intopytorch:masterfrom
sadra-barikbin:Feature-make-metrics-serializable

Conversation

@sadra-barikbin
Copy link
Copy Markdown
Collaborator

@sadra-barikbin sadra-barikbin commented Jul 15, 2023

Fixes #2999

@github-actions github-actions bot added the module: metrics Metrics module label Jul 15, 2023
@sadra-barikbin sadra-barikbin requested a review from vfdev-5 July 15, 2023 21:42
@sadra-barikbin sadra-barikbin marked this pull request as ready for review July 16, 2023 11:56
@sadra-barikbin
Copy link
Copy Markdown
Collaborator Author

Shall I add _state_dict_all_req_keys attribute to all of metrics?
Shall I add tests for state_dict and load_state_dict for each metric?

Copy link
Copy Markdown
Collaborator

@vfdev-5 vfdev-5 left a comment

Choose a reason for hiding this comment

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

Thanks for the PR @sadra-barikbin !
The approach looks OK to me, let's update all metrics.

@sadra-barikbin sadra-barikbin requested a review from vfdev-5 August 7, 2023 06:38
@sadra-barikbin sadra-barikbin self-assigned this Aug 7, 2023
metric = ...

to_save = {'trainer': trainer, 'model': model, 'optimizer': optimizer, 'lr_scheduler': lr_scheduler}
to_save = {'trainer': trainer, 'model': model, 'optimizer': optimizer, 'lr_scheduler': lr_scheduler, 'metric': metric}
Copy link
Copy Markdown
Collaborator

@vfdev-5 vfdev-5 Aug 10, 2023

Choose a reason for hiding this comment

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

Ideally, we would like to get that in a more automatic way. Also if we want to save multiple metrics adding them one by one to save/load can be a pain.
Let's propose something in a follow-up PR

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Could you please reiterate what you mean? If we have multiple metrics, is it cumbersome to do:

to_save = {..., 'metric1': metric1, 'metric2': metric2, 'metric3': metric3}

?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Yes, it is too verbose, let's try to find a way to simplify that.

Copy link
Copy Markdown
Collaborator

@vfdev-5 vfdev-5 left a comment

Choose a reason for hiding this comment

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

LGTM, thanks @sadra-barikbin

@vfdev-5 vfdev-5 merged commit cf3fdd1 into pytorch:master Aug 10, 2023
required_output_keys = None
# TODO Shall we put `src` here? Then we should add a new branch for metric-typed attributes in `state_dict`
# and `load_state_dict`. Examples; This class; `Rouge` which has a `List[_BaseRouge]`.
_state_dict_all_req_keys = ("_value",)
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

@vfdev-5 , What's your thoughts on this?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Let's add src and in state_dict/load_state_dict put an explicit check for isinstance(attr, Metric) and call state[attr_name].update(attr.state_dict) or something like that.
Do you have any other options ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

module: metrics Metrics module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add state_dict and load_state_dict to metrics

2 participants