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

Feat/generic save load #1070

Merged
merged 21 commits into from
Aug 12, 2022
Merged

Feat/generic save load #1070

merged 21 commits into from
Aug 12, 2022

Conversation

brunnedu
Copy link
Contributor

@brunnedu brunnedu commented Jul 12, 2022

Addresses #936

First version of generic save and load methods for ForecastingModels

For now, we have decided to implement generic save and load methods for all ForecastingModels using joblib, as long as there is no better alternative for a particular model subclass, as is currently the case only for TorchForecastingModels. We decided to take this approach because most libraries either suggest doing it this way (sklearn) or wrap a very similar solution themselves (statsmodels).

There are obvious downsides to this solution regarding maintainability and security such as the following ones:

  • ForecastingModels saved in one version of darts might not be loadable in another version of darts or they could exhibit unexpected behavior.
  • One should never call ForecastingModel.load_model() on untrusted data as it could lead to malicious code being executed upon loading.

I'm open to hearing your opinion on the current implementation and the additional functionality you believe those methods should contain.

darts/models/forecasting/torch_forecasting_model.py Outdated Show resolved Hide resolved
darts/models/forecasting/forecasting_model.py Outdated Show resolved Hide resolved
darts/models/forecasting/forecasting_model.py Outdated Show resolved Hide resolved
darts/models/forecasting/forecasting_model.py Outdated Show resolved Hide resolved
darts/models/forecasting/forecasting_model.py Outdated Show resolved Hide resolved
darts/models/forecasting/forecasting_model.py Outdated Show resolved Hide resolved
@hrzn
Copy link
Contributor

hrzn commented Jul 13, 2022

Do you think it's worth relying on joblib (vs pickle)? It seems starting with Python 3.8 the gains are not so clear: https://stackoverflow.com/questions/12615525/what-are-the-different-use-cases-of-joblib-versus-pickle

Maybe we could make a few quick trials (with a few models of different sizes) to see if there's a difference in practice? If there's no clear advantage I would vote for sticking to the simplest option.

@brunnedu
Copy link
Contributor Author

Yeah this sounds like a good idea, I'll make a few trials 👍

@hrzn
Copy link
Contributor

hrzn commented Jul 18, 2022

Yeah this sounds like a good idea, I'll make a few trials 👍

I don't have a strong opinion so feel also free to leave joblib.

@brunnedu
Copy link
Contributor Author

brunnedu commented Aug 2, 2022

Did a few comparisons between pickle and joblib but wasn't able to tell a difference, so I think we should use pickle for now.

@hrzn
Copy link
Contributor

hrzn commented Aug 7, 2022

@brunnedu what's the status of this PR? Anything more remaining to be done?

@codecov-commenter
Copy link

codecov-commenter commented Aug 9, 2022

Codecov Report

Merging #1070 (ddf05e2) into master (4dc592b) will decrease coverage by 0.08%.
The diff coverage is 80.48%.

@@            Coverage Diff             @@
##           master    #1070      +/-   ##
==========================================
- Coverage   93.73%   93.64%   -0.09%     
==========================================
  Files          80       80              
  Lines        8266     8282      +16     
==========================================
+ Hits         7748     7756       +8     
- Misses        518      526       +8     
Impacted Files Coverage Δ
...arts/models/forecasting/torch_forecasting_model.py 87.45% <63.15%> (-1.26%) ⬇️
darts/models/forecasting/forecasting_model.py 96.10% <95.45%> (-0.07%) ⬇️
darts/timeseries.py 92.23% <0.00%> (-0.07%) ⬇️
darts/models/forecasting/block_rnn_model.py 98.24% <0.00%> (-0.04%) ⬇️
darts/models/forecasting/nhits.py 98.55% <0.00%> (-0.02%) ⬇️
darts/datasets/__init__.py 100.00% <0.00%> (ø)

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@brunnedu brunnedu marked this pull request as ready for review August 11, 2022 14:53
Copy link
Contributor

@hrzn hrzn left a comment

Choose a reason for hiding this comment

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

Looks good! Only a few minor comments :)

darts/models/forecasting/forecasting_model.py Show resolved Hide resolved
darts/models/forecasting/torch_forecasting_model.py Outdated Show resolved Hide resolved
darts/models/forecasting/torch_forecasting_model.py Outdated Show resolved Hide resolved
darts/models/forecasting/torch_forecasting_model.py Outdated Show resolved Hide resolved
darts/models/forecasting/torch_forecasting_model.py Outdated Show resolved Hide resolved
darts/models/forecasting/torch_forecasting_model.py Outdated Show resolved Hide resolved
darts/models/forecasting/torch_forecasting_model.py Outdated Show resolved Hide resolved
Copy link
Collaborator

@dennisbader dennisbader left a comment

Choose a reason for hiding this comment

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

Looks good to me as well :) Thanks for this, finally we have built in saving and loading for all models!
Just had a few minor comments/additions to @hrzn's comments

Copy link
Contributor

@hrzn hrzn left a comment

Choose a reason for hiding this comment

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

Awesome 🚀


Creates two files under ``path`` (model object) and ``path``.ckpt (checkpoint).

Example for saving and loading a :class:`RNNModel`:
Copy link
Contributor

Choose a reason for hiding this comment

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

💯

@hrzn hrzn merged commit f6b52db into master Aug 12, 2022
@madtoinou madtoinou deleted the feat/generic_save_load branch July 5, 2023 21:52
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.

4 participants