Skip to content

Commit

Permalink
Fix pickling error with CSVLogger (#10388)
Browse files Browse the repository at this point in the history
* Don't store csv.Dictwriter in ExperimentWriter

* Add test for pickle after .save()

* Add entry in changelog
  • Loading branch information
awaelchli committed Nov 9, 2021
1 parent df5081f commit b9a8f74
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
- Fixed `apply_to_collection(defaultdict)` ([#10316](https://github.com/PyTorchLightning/pytorch-lightning/issues/10316))
- Fixed failure when `DataLoader(batch_size=None)` is passed ([#10345](https://github.com/PyTorchLightning/pytorch-lightning/issues/10345))
- Fixed interception of `__init__` arguments for sub-classed DataLoader re-instantiation in Lite ([#10334](https://github.com/PyTorchLightning/pytorch-lightning/issues/10334))
- Fixed issue with pickling `CSVLogger` after a call to `CSVLogger.save` ([#10388](https://github.com/PyTorchLightning/pytorch-lightning/pull/10388))


## [1.5.0] - 2021-11-02
Expand Down
6 changes: 3 additions & 3 deletions pytorch_lightning/loggers/csv_logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,9 @@ def save(self) -> None:
metrics_keys = list(last_m.keys())

with open(self.metrics_file_path, "w", newline="") as f:
self.writer = csv.DictWriter(f, fieldnames=metrics_keys)
self.writer.writeheader()
self.writer.writerows(self.metrics)
writer = csv.DictWriter(f, fieldnames=metrics_keys)
writer.writeheader()
writer.writerows(self.metrics)


class CSVLogger(LightningLoggerBase):
Expand Down
4 changes: 4 additions & 0 deletions tests/loggers/test_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,10 @@ def _test_loggers_pickle(tmpdir, monkeypatch, logger_class):
# the logger needs to remove it from the state before pickle
_ = logger.experiment

# logger also has to avoid adding un-picklable attributes to self in .save
logger.log_metrics({"a": 1})
logger.save()

# test pickling loggers
pickle.dumps(logger)

Expand Down

0 comments on commit b9a8f74

Please sign in to comment.