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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix/4156 filter hparams for yaml - fsspec #4158

Merged
merged 5 commits into from
Oct 15, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).

- Fixed `hparams` saving - save the state when `save_hyperparameters()` is called [in `__init__`] ([#4163](https://github.com/PyTorchLightning/pytorch-lightning/pull/4163))

- Fixed runtime failure while exporting `hparams` to yaml ([#4158](https://github.com/PyTorchLightning/pytorch-lightning/pull/4158))


## [1.0.1] - 2020-10-14
Expand Down
16 changes: 14 additions & 2 deletions pytorch_lightning/core/saving.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import os
from argparse import Namespace
from typing import Union, Dict, Any, Optional, Callable, MutableMapping
from warnings import warn

import fsspec
import torch
Expand Down Expand Up @@ -372,10 +373,21 @@ def save_hparams_to_yaml(config_yaml, hparams: Union[dict, Namespace]) -> None:
OmegaConf.save(OmegaConf.create(hparams), fp, resolve=True)
Borda marked this conversation as resolved.
Show resolved Hide resolved
return

# saving the standard way
assert isinstance(hparams, dict)
hparams_allowed = {}
# drop paramaters which contain some strange datatypes as fsspec
for k, v in hparams.items():
try:
Copy link
Contributor

Choose a reason for hiding this comment

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

Hey @Borda, I was wondering if it makes sense to force dumping to yaml file. It might break reproducibility for some use cases. But for the yaml format, we could also support some conversion like: tensor and numpy array to list, etc...

Copy link
Member Author

Choose a reason for hiding this comment

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

so how else would you test that some value/data are not possible to dum, I have not found any method like is_dumpable and most examples use this construct...

yaml.dump(v)
except TypeError as err:
warn(f"Skipping '{k}' parameter because it is not possible to safely dump to YAML.")
Borda marked this conversation as resolved.
Show resolved Hide resolved
hparams[k] = type(v).__name__
else:
hparams_allowed[k] = v

# saving the standard way
with fs.open(config_yaml, "w", newline="") as fp:
yaml.dump(hparams, fp)
yaml.dump(hparams_allowed, fp)


def convert(val: str) -> Union[int, float, bool, str]:
Expand Down
20 changes: 20 additions & 0 deletions tests/models/test_hparams.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import cloudpickle
import pytest
import torch
from fsspec.implementations.local import LocalFileSystem
from omegaconf import OmegaConf, Container
from torch.nn import functional as F
from torch.utils.data import DataLoader
Expand Down Expand Up @@ -579,3 +580,22 @@ def test_init_arg_with_runtime_change(tmpdir):
path_yaml = os.path.join(trainer.logger.log_dir, trainer.logger.NAME_HPARAMS_FILE)
hparams = load_hparams_from_yaml(path_yaml)
assert hparams.get('running_arg') == 123


class UnsafeParamModel(BoringModel):
def __init__(self, my_path, any_param=123):
Borda marked this conversation as resolved.
Show resolved Hide resolved
super().__init__()
self.save_hyperparameters()


def test_model_with_fsspec_as_parameter(tmpdir):
model = UnsafeParamModel(LocalFileSystem(tmpdir))
trainer = Trainer(
default_root_dir=tmpdir,
limit_train_batches=2,
limit_val_batches=2,
limit_test_batches=2,
max_epochs=1,
)
trainer.fit(model)
trainer.test()