From b440c23a7582e5836ad8b3ed68bdc3bc59a1512a Mon Sep 17 00:00:00 2001 From: duydl Date: Tue, 18 Feb 2025 16:01:16 +0700 Subject: [PATCH 1/7] Move save_hparams_to_yaml to log_hparams instead of auto save with metric --- src/lightning/pytorch/loggers/csv_logs.py | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/src/lightning/pytorch/loggers/csv_logs.py b/src/lightning/pytorch/loggers/csv_logs.py index 8606264dc3cdb..c0faaf7fe93f7 100644 --- a/src/lightning/pytorch/loggers/csv_logs.py +++ b/src/lightning/pytorch/loggers/csv_logs.py @@ -55,16 +55,11 @@ def __init__(self, log_dir: str) -> None: self.hparams: dict[str, Any] = {} def log_hparams(self, params: dict[str, Any]) -> None: - """Record hparams.""" + """Record hparams and save into files.""" self.hparams.update(params) - - @override - def save(self) -> None: - """Save recorded hparams and metrics into files.""" hparams_file = os.path.join(self.log_dir, self.NAME_HPARAMS_FILE) save_hparams_to_yaml(hparams_file, self.hparams) - return super().save() - + class CSVLogger(Logger, FabricCSVLogger): r"""Log to local file system in yaml and CSV format. From f03b006fa4797966d399d145b133cf7a25cff1a8 Mon Sep 17 00:00:00 2001 From: duydl Date: Tue, 18 Feb 2025 16:21:41 +0700 Subject: [PATCH 2/7] Fix params to be optional --- src/lightning/pytorch/loggers/csv_logs.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lightning/pytorch/loggers/csv_logs.py b/src/lightning/pytorch/loggers/csv_logs.py index c0faaf7fe93f7..54323e96e5e29 100644 --- a/src/lightning/pytorch/loggers/csv_logs.py +++ b/src/lightning/pytorch/loggers/csv_logs.py @@ -139,7 +139,7 @@ def save_dir(self) -> str: @override @rank_zero_only - def log_hyperparams(self, params: Union[dict[str, Any], Namespace]) -> None: + def log_hyperparams(self, params: Optional[Union[dict[str, Any], Namespace]] = None) -> None: params = _convert_params(params) self.experiment.log_hparams(params) From 9f09af69fb66b1ea3854f4f79ca217f1c4bd67f7 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 18 Feb 2025 12:50:42 +0000 Subject: [PATCH 3/7] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/lightning/pytorch/loggers/csv_logs.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lightning/pytorch/loggers/csv_logs.py b/src/lightning/pytorch/loggers/csv_logs.py index 54323e96e5e29..5ad7353310af4 100644 --- a/src/lightning/pytorch/loggers/csv_logs.py +++ b/src/lightning/pytorch/loggers/csv_logs.py @@ -59,7 +59,7 @@ def log_hparams(self, params: dict[str, Any]) -> None: self.hparams.update(params) hparams_file = os.path.join(self.log_dir, self.NAME_HPARAMS_FILE) save_hparams_to_yaml(hparams_file, self.hparams) - + class CSVLogger(Logger, FabricCSVLogger): r"""Log to local file system in yaml and CSV format. From b5d3c51eb058cc937d5436d8042d193b586ad00f Mon Sep 17 00:00:00 2001 From: duydl Date: Wed, 19 Feb 2025 18:50:04 +0700 Subject: [PATCH 4/7] Add to CHANGELOG --- src/lightning/pytorch/CHANGELOG.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/lightning/pytorch/CHANGELOG.md b/src/lightning/pytorch/CHANGELOG.md index ef0f3dc73c9e0..f8ff2b742d335 100644 --- a/src/lightning/pytorch/CHANGELOG.md +++ b/src/lightning/pytorch/CHANGELOG.md @@ -4,6 +4,19 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/). +## [unreleased] - YYYY-MM-DD + +### Added + +### Changed + +### Removed + +### Fixed + +- Fix CSVLogger loggging hyperparameter at every write which increase latency ([#20594](https://github.com/Lightning-AI/pytorch-lightning/pull/20594)) + + ## [2.5.0] - 2024-12-19 ### Added From f0dcda8671ecfe86404a6e906a30a9122e4ea787 Mon Sep 17 00:00:00 2001 From: duydl Date: Wed, 19 Feb 2025 19:27:09 +0700 Subject: [PATCH 5/7] Fix changelog --- src/lightning/pytorch/CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lightning/pytorch/CHANGELOG.md b/src/lightning/pytorch/CHANGELOG.md index f8ff2b742d335..8bc8e45989f77 100644 --- a/src/lightning/pytorch/CHANGELOG.md +++ b/src/lightning/pytorch/CHANGELOG.md @@ -14,7 +14,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/). ### Fixed -- Fix CSVLogger loggging hyperparameter at every write which increase latency ([#20594](https://github.com/Lightning-AI/pytorch-lightning/pull/20594)) +- Fix CSVLogger logging hyperparameter at every write which increase latency ([#20594](https://github.com/Lightning-AI/pytorch-lightning/pull/20594)) ## [2.5.0] - 2024-12-19 From f2b082b93c35e157dfe88c8953d50e178079103e Mon Sep 17 00:00:00 2001 From: duydl Date: Fri, 21 Feb 2025 20:58:29 +0700 Subject: [PATCH 6/7] Adjust test --- tests/tests_pytorch/loggers/test_csv.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/tests_pytorch/loggers/test_csv.py b/tests/tests_pytorch/loggers/test_csv.py index 1b09302ffb74a..b18d72584245a 100644 --- a/tests/tests_pytorch/loggers/test_csv.py +++ b/tests/tests_pytorch/loggers/test_csv.py @@ -116,7 +116,6 @@ def test_log_hyperparams(tmp_path): "layer": torch.nn.BatchNorm1d, } logger.log_hyperparams(hparams) - logger.save() path_yaml = os.path.join(logger.log_dir, ExperimentWriter.NAME_HPARAMS_FILE) params = load_hparams_from_yaml(path_yaml) From dd828c42a12ebd04346d94b4726b6d10aa1d1b49 Mon Sep 17 00:00:00 2001 From: duydl Date: Sat, 22 Feb 2025 20:14:27 +0700 Subject: [PATCH 7/7] Fix test_csv, test_no_name --- tests/tests_pytorch/loggers/test_csv.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/tests_pytorch/loggers/test_csv.py b/tests/tests_pytorch/loggers/test_csv.py index b18d72584245a..c131d03d38245 100644 --- a/tests/tests_pytorch/loggers/test_csv.py +++ b/tests/tests_pytorch/loggers/test_csv.py @@ -75,7 +75,6 @@ def test_named_version(tmp_path): logger = CSVLogger(save_dir=tmp_path, name=exp_name, version=expected_version) logger.log_hyperparams({"a": 1, "b": 2}) - logger.save() assert logger.version == expected_version assert os.listdir(tmp_path / exp_name) == [expected_version] assert os.listdir(tmp_path / exp_name / expected_version) @@ -85,7 +84,7 @@ def test_named_version(tmp_path): def test_no_name(tmp_path, name): """Verify that None or empty name works.""" logger = CSVLogger(save_dir=tmp_path, name=name) - logger.save() + logger.log_hyperparams() assert os.path.normpath(logger.root_dir) == str(tmp_path) # use os.path.normpath to handle trailing / assert os.listdir(tmp_path / "version_0")