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] loading model on machine with different accelerator #1509

Merged
merged 4 commits into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions neuralprophet/forecaster.py
Original file line number Diff line number Diff line change
Expand Up @@ -1005,7 +1005,7 @@
# Only display the plot if the session is interactive, eg. do not show in github actions since it
# causes an error in the Windows and MacOS environment
if matplotlib.is_interactive():
fig

Check warning on line 1008 in neuralprophet/forecaster.py

View workflow job for this annotation

GitHub Actions / pyright

Expression value is unused (reportUnusedExpression)

self.fitted = True
return metrics_df
Expand Down Expand Up @@ -2734,7 +2734,12 @@
metrics_df = pd.DataFrame(self.metrics_logger.history)
return metrics_df

def restore_trainer(self):
def restore_trainer(self, accelerator: Optional[str] = None):
"""
If no accelerator was provided, use accelerator stored in model.
"""
if accelerator is None:
accelerator = self.accelerator
"""
Restore the trainer based on the forecaster configuration.
"""
Expand All @@ -2743,7 +2748,7 @@
config=self.trainer_config,
metrics_logger=self.metrics_logger,
early_stopping=self.early_stopping,
accelerator=self.accelerator,
accelerator=accelerator,
metrics_enabled=bool(self.metrics),
)

Expand Down
8 changes: 5 additions & 3 deletions neuralprophet/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,12 @@ def load(path: str, map_location=None):
>>> from neuralprophet import load
>>> model = load("test_save_model.np")
"""
torch_map_location = None
if map_location is not None:
map_location = torch.device(map_location)
m = torch.load(path, map_location=map_location)
m.restore_trainer()
torch_map_location = torch.device(map_location)

m = torch.load(path, map_location=torch_map_location)
m.restore_trainer(accelerator=map_location)
return m


Expand Down
Loading