Skip to content

Commit c784092

Browse files
authored
deprecate flush_logs_every_n_steps on Trainer (#9366)
1 parent ec828b8 commit c784092

File tree

7 files changed

+47
-8
lines changed

7 files changed

+47
-8
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
215215
- Deprecated passing `process_position` to the `Trainer` constructor in favor of adding the `ProgressBar` callback with `process_position` directly to the list of callbacks ([#9222](https://github.com/PyTorchLightning/pytorch-lightning/pull/9222))
216216

217217

218+
- Deprecated passing `flush_logs_every_n_steps` as a Trainer argument, instead pass it to the logger init if supported ([#9366](https://github.com/PyTorchLightning/pytorch-lightning/pull/9366))
219+
220+
218221

219222
### Removed
220223

pytorch_lightning/loggers/csv_logs.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ class CSVLogger(LightningLoggerBase):
118118
version: Experiment version. If version is not specified the logger inspects the save
119119
directory for existing versions, then automatically assigns the next available version.
120120
prefix: A string to put at the beginning of metric keys.
121+
flush_logs_every_n_steps: How often to flush logs to disk (defaults to every 100 steps).
121122
"""
122123

123124
LOGGER_JOIN_CHAR = "-"
@@ -128,13 +129,15 @@ def __init__(
128129
name: Optional[str] = "default",
129130
version: Optional[Union[int, str]] = None,
130131
prefix: str = "",
132+
flush_logs_every_n_steps: int = 100,
131133
):
132134
super().__init__()
133135
self._save_dir = save_dir
134136
self._name = name or ""
135137
self._version = version
136138
self._prefix = prefix
137139
self._experiment = None
140+
self._flush_logs_every_n_steps = flush_logs_every_n_steps
138141

139142
@property
140143
def root_dir(self) -> str:
@@ -154,7 +157,7 @@ def log_dir(self) -> str:
154157
By default, it is named ``'version_${self.version}'`` but it can be overridden by passing a string value for the
155158
constructor's version parameter instead of ``None`` or an int.
156159
"""
157-
# create a pseudo standard path ala test-tube
160+
# create a pseudo standard path
158161
version = self.version if isinstance(self.version, str) else f"version_{self.version}"
159162
log_dir = os.path.join(self.root_dir, version)
160163
return log_dir
@@ -197,6 +200,8 @@ def log_hyperparams(self, params: Union[Dict[str, Any], Namespace]) -> None:
197200
def log_metrics(self, metrics: Dict[str, float], step: Optional[int] = None) -> None:
198201
metrics = self._add_prefix(metrics)
199202
self.experiment.log_metrics(metrics, step)
203+
if step is not None and (step + 1) % self._flush_logs_every_n_steps == 0:
204+
self.save()
200205

201206
@rank_zero_only
202207
def save(self) -> None:

pytorch_lightning/loggers/tensorboard.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,17 +64,19 @@ class TensorBoardLogger(LightningLoggerBase):
6464
directory for existing versions, then automatically assigns the next available version.
6565
If it is a string then it is used as the run-specific subdirectory name,
6666
otherwise ``'version_${version}'`` is used.
67-
sub_dir: Sub-directory to group TensorBoard logs. If a sub_dir argument is passed
68-
then logs are saved in ``/save_dir/version/sub_dir/``. Defaults to ``None`` in which
69-
logs are saved in ``/save_dir/version/``.
7067
log_graph: Adds the computational graph to tensorboard. This requires that
7168
the user has defined the `self.example_input_array` attribute in their
7269
model.
7370
default_hp_metric: Enables a placeholder metric with key `hp_metric` when `log_hyperparams` is
7471
called without a metric (otherwise calls to log_hyperparams without a metric are ignored).
7572
prefix: A string to put at the beginning of metric keys.
76-
\**kwargs: Additional arguments like `comment`, `filename_suffix`, etc. used by
77-
:class:`SummaryWriter` can be passed as keyword arguments in this logger.
73+
sub_dir: Sub-directory to group TensorBoard logs. If a sub_dir argument is passed
74+
then logs are saved in ``/save_dir/version/sub_dir/``. Defaults to ``None`` in which
75+
logs are saved in ``/save_dir/version/``.
76+
\**kwargs: Additional arguments used by :class:`SummaryWriter` can be passed as keyword
77+
arguments in this logger. To automatically flush to disk, `max_queue` sets the size
78+
of the queue for pending logs before flushing. `flush_secs` determines how many seconds
79+
elapses before flushing.
7880
7981
"""
8082
NAME_HPARAMS_FILE = "hparams.yaml"

pytorch_lightning/trainer/connectors/logger_connector/logger_connector.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from pytorch_lightning.utilities import DeviceType, memory
2424
from pytorch_lightning.utilities.apply_func import apply_to_collection, move_data_to_device
2525
from pytorch_lightning.utilities.metrics import metrics_to_scalars
26+
from pytorch_lightning.utilities.warnings import rank_zero_deprecation
2627

2728

2829
class LoggerConnector:
@@ -44,11 +45,18 @@ def __init__(self, trainer: "pl.Trainer", log_gpu_memory: Optional[str] = None)
4445
def on_trainer_init(
4546
self,
4647
logger: Union[bool, LightningLoggerBase, Iterable[LightningLoggerBase]],
47-
flush_logs_every_n_steps: int,
48+
flush_logs_every_n_steps: Optional[int],
4849
log_every_n_steps: int,
4950
move_metrics_to_cpu: bool,
5051
) -> None:
5152
self.configure_logger(logger)
53+
if flush_logs_every_n_steps is not None:
54+
rank_zero_deprecation(
55+
f"Setting `Trainer(flush_logs_every_n_steps={flush_logs_every_n_steps})` is deprecated in v1.5 "
56+
"and will be removed in v1.7. Please configure flushing in the logger instead."
57+
)
58+
else:
59+
flush_logs_every_n_steps = 100 # original default parameter
5260
self.trainer.flush_logs_every_n_steps = flush_logs_every_n_steps
5361
self.trainer.log_every_n_steps = log_every_n_steps
5462
self.trainer.move_metrics_to_cpu = move_metrics_to_cpu

pytorch_lightning/trainer/trainer.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ def __init__(
130130
limit_test_batches: Union[int, float] = 1.0,
131131
limit_predict_batches: Union[int, float] = 1.0,
132132
val_check_interval: Union[int, float] = 1.0,
133-
flush_logs_every_n_steps: int = 100,
133+
flush_logs_every_n_steps: Optional[int] = None,
134134
log_every_n_steps: int = 50,
135135
accelerator: Optional[Union[str, Accelerator]] = None,
136136
sync_batchnorm: bool = False,
@@ -213,6 +213,10 @@ def __init__(
213213
214214
flush_logs_every_n_steps: How often to flush logs to disk (defaults to every 100 steps).
215215
216+
.. deprecated:: v1.5
217+
``flush_logs_every_n_steps`` has been deprecated in v1.5 and will be removed in v1.7.
218+
Please configure flushing directly in the logger instead.
219+
216220
gpus: Number of GPUs to train on (int) or which GPUs to train on (list or str) applied per node
217221
218222
gradient_clip_val: The value at which to clip gradients. Passing ``gradient_clip_val=0`` disables gradient

tests/deprecated_api/test_remove_1-7.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,11 @@ def test_v1_7_0_process_position_trainer_constructor(tmpdir):
196196
_ = Trainer(process_position=5)
197197

198198

199+
def test_v1_7_0_flush_logs_every_n_steps_trainer_constructor(tmpdir):
200+
with pytest.deprecated_call(match=r"Setting `Trainer\(flush_logs_every_n_steps=10\)` is deprecated in v1.5"):
201+
_ = Trainer(flush_logs_every_n_steps=10)
202+
203+
199204
class BoringCallbackDDPSpawnModel(BoringModel):
200205
def __init__(self):
201206
super().__init__()

tests/loggers/test_csv.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# limitations under the License.
1414
import os
1515
from argparse import Namespace
16+
from unittest.mock import MagicMock
1617

1718
import pytest
1819
import torch
@@ -103,3 +104,14 @@ def test_file_logger_log_hyperparams(tmpdir):
103104
path_yaml = os.path.join(logger.log_dir, ExperimentWriter.NAME_HPARAMS_FILE)
104105
params = load_hparams_from_yaml(path_yaml)
105106
assert all(n in params for n in hparams)
107+
108+
109+
def test_flush_n_steps(tmpdir):
110+
logger = CSVLogger(tmpdir, flush_logs_every_n_steps=2)
111+
metrics = {"float": 0.3, "int": 1, "FloatTensor": torch.tensor(0.1), "IntTensor": torch.tensor(1)}
112+
logger.save = MagicMock()
113+
logger.log_metrics(metrics, step=0)
114+
115+
logger.save.assert_not_called()
116+
logger.log_metrics(metrics, step=1)
117+
logger.save.assert_called_once()

0 commit comments

Comments
 (0)