-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
test_logger_connector.py
506 lines (421 loc) · 18 KB
/
test_logger_connector.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
# Copyright The PyTorch Lightning team.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from unittest import mock
import pytest
import torch
from torch.utils.data import DataLoader
from torchmetrics import Accuracy, AveragePrecision
from pytorch_lightning import LightningModule
from pytorch_lightning.callbacks.base import Callback
from pytorch_lightning.trainer import Trainer
from pytorch_lightning.trainer.connectors.logger_connector.fx_validator import FxValidator
from pytorch_lightning.trainer.connectors.logger_connector.result import MetricSource, ResultCollection
from pytorch_lightning.utilities.exceptions import MisconfigurationException
from tests.helpers.boring_model import BoringModel, RandomDataset
from tests.helpers.runif import RunIf
def test_fx_validator(tmpdir):
funcs_name = sorted([f for f in dir(Callback) if not f.startswith('_')])
callbacks_func = [
'on_before_backward',
'on_after_backward',
'on_before_optimizer_step',
'on_batch_end',
'on_batch_start',
'on_before_accelerator_backend_setup',
'on_before_zero_grad',
'on_epoch_end',
'on_epoch_start',
'on_fit_end',
'on_configure_sharded_model',
'on_fit_start',
'on_init_end',
'on_init_start',
'on_keyboard_interrupt',
'on_load_checkpoint',
'on_pretrain_routine_end',
'on_pretrain_routine_start',
'on_sanity_check_end',
'on_sanity_check_start',
'on_save_checkpoint',
'on_test_batch_end',
'on_test_batch_start',
'on_test_end',
'on_test_epoch_end',
'on_test_epoch_start',
'on_test_start',
'on_train_batch_end',
'on_train_batch_start',
'on_train_end',
'on_train_epoch_end',
'on_train_epoch_start',
'on_train_start',
'on_validation_batch_end',
'on_validation_batch_start',
'on_validation_end',
'on_validation_epoch_end',
'on_validation_epoch_start',
'on_validation_start',
"on_predict_batch_end",
"on_predict_batch_start",
"on_predict_end",
"on_predict_epoch_end",
"on_predict_epoch_start",
"on_predict_start",
'setup',
'teardown',
]
not_supported = [
"on_before_accelerator_backend_setup",
"on_fit_end",
"on_fit_start",
"on_configure_sharded_model",
"on_init_end",
"on_init_start",
"on_keyboard_interrupt",
"on_load_checkpoint",
"on_pretrain_routine_end",
"on_pretrain_routine_start",
"on_sanity_check_end",
"on_sanity_check_start",
"on_predict_batch_end",
"on_predict_batch_start",
"on_predict_end",
"on_predict_epoch_end",
"on_predict_epoch_start",
"on_predict_start",
"on_save_checkpoint",
"on_test_end",
"on_train_end",
"on_validation_end",
"setup",
"teardown",
]
assert funcs_name == sorted(callbacks_func), (
"Detected new callback function. Need to add its logging"
" permission to FxValidator and update this test"
)
validator = FxValidator()
for func_name in funcs_name:
# This summarizes where and what is currently possible to log using `self.log`
is_stage = "train" in func_name or "test" in func_name or "validation" in func_name
is_start = "start" in func_name or "batch" in func_name
is_epoch = "epoch" in func_name
on_step = is_stage and not is_start and not is_epoch
on_epoch = True
# creating allowed condition
allowed = (
is_stage or "batch" in func_name or "epoch" in func_name or "grad" in func_name or "backward" in func_name
or "optimizer_step" in func_name
)
allowed = (
allowed and "pretrain" not in func_name and "predict" not in func_name
and func_name not in ["on_train_end", "on_test_end", "on_validation_end"]
)
if allowed:
validator.check_logging(fx_name=func_name, on_step=on_step, on_epoch=on_epoch)
if not is_start and is_stage:
with pytest.raises(MisconfigurationException, match="You can't"):
validator.check_logging(fx_name=func_name, on_step=True, on_epoch=on_epoch)
else:
assert func_name in not_supported
with pytest.raises(MisconfigurationException, match="function doesn't support"):
validator.check_logging(fx_name=func_name, on_step=on_step, on_epoch=on_epoch)
with pytest.raises(RuntimeError, match="`foo` but it is not implemented"):
validator.check_logging("foo", False, False)
@RunIf(min_gpus=2)
def test_epoch_results_cache_dp(tmpdir):
root_device = torch.device("cuda", 0)
class TestModel(BoringModel):
def training_step(self, *args, **kwargs):
result = super().training_step(*args, **kwargs)
self.log("train_loss_epoch", result["loss"], on_step=False, on_epoch=True)
return result
def training_step_end(self, training_step_outputs): # required for dp
loss = training_step_outputs["loss"].mean()
return loss
def training_epoch_end(self, outputs):
assert all(out["loss"].device == root_device for out in outputs)
assert self.trainer.callback_metrics["train_loss_epoch"].device == root_device
def validation_step(self, *args, **kwargs):
val_loss = torch.rand(1, device=torch.device("cuda", 1))
self.log("val_loss_epoch", val_loss, on_step=False, on_epoch=True)
return val_loss
def validation_epoch_end(self, outputs):
assert all(loss.device == root_device for loss in outputs)
assert self.trainer.callback_metrics["val_loss_epoch"].device == root_device
def test_step(self, *args, **kwargs):
test_loss = torch.rand(1, device=torch.device("cuda", 1))
self.log("test_loss_epoch", test_loss, on_step=False, on_epoch=True)
return test_loss
def test_epoch_end(self, outputs):
assert all(loss.device == root_device for loss in outputs)
assert self.trainer.callback_metrics["test_loss_epoch"].device == root_device
def train_dataloader(self):
return DataLoader(RandomDataset(32, 64), batch_size=4)
def val_dataloader(self):
return DataLoader(RandomDataset(32, 64), batch_size=4)
def test_dataloader(self):
return DataLoader(RandomDataset(32, 64), batch_size=4)
model = TestModel()
trainer = Trainer(
default_root_dir=tmpdir,
accelerator="dp",
gpus=2,
limit_train_batches=2,
limit_val_batches=2,
max_epochs=1,
)
trainer.fit(model)
trainer.test(model, ckpt_path=None)
def test_can_return_tensor_with_more_than_one_element(tmpdir):
"""Ensure {validation,test}_step return values are not included as callback metrics. #6623"""
class TestModel(BoringModel):
def validation_step(self, batch, *args, **kwargs):
return {"val": torch.tensor([0, 1])}
def validation_epoch_end(self, outputs):
# ensure validation step returns still appear here
assert len(outputs) == 2
assert all(list(d) == ["val"] for d in outputs) # check keys
assert all(torch.equal(d["val"], torch.tensor([0, 1])) for d in outputs) # check values
def test_step(self, batch, *args, **kwargs):
return {"test": torch.tensor([0, 1])}
def test_epoch_end(self, outputs):
assert len(outputs) == 2
assert all(list(d) == ["test"] for d in outputs) # check keys
assert all(torch.equal(d["test"], torch.tensor([0, 1])) for d in outputs) # check values
model = TestModel()
trainer = Trainer(default_root_dir=tmpdir, fast_dev_run=2, progress_bar_refresh_rate=0)
trainer.fit(model)
trainer.validate(model)
trainer.test(model)
def test_logging_to_progress_bar_with_reserved_key(tmpdir):
""" Test that logging a metric with a reserved name to the progress bar raises a warning. """
class TestModel(BoringModel):
def training_step(self, *args, **kwargs):
output = super().training_step(*args, **kwargs)
self.log("loss", output["loss"], prog_bar=True)
return output
model = TestModel()
trainer = Trainer(default_root_dir=tmpdir, fast_dev_run=True)
with pytest.warns(UserWarning, match="The progress bar already tracks a metric with the .* 'loss'"):
trainer.fit(model)
@pytest.mark.parametrize("add_dataloader_idx", [False, True])
def test_auto_add_dataloader_idx(tmpdir, add_dataloader_idx):
""" test that auto_add_dataloader_idx argument works """
class TestModel(BoringModel):
def val_dataloader(self):
dl = super().val_dataloader()
return [dl, dl]
def validation_step(self, *args, **kwargs):
output = super().validation_step(*args[:-1], **kwargs)
if add_dataloader_idx:
name = "val_loss"
else:
name = f"val_loss_custom_naming_{args[-1]}"
self.log(name, output["x"], add_dataloader_idx=add_dataloader_idx)
return output
model = TestModel()
model.validation_epoch_end = None
trainer = Trainer(default_root_dir=tmpdir, fast_dev_run=2)
trainer.fit(model)
logged = trainer.logged_metrics
# Check that the correct keys exist
if add_dataloader_idx:
assert 'val_loss/dataloader_idx_0' in logged
assert 'val_loss/dataloader_idx_1' in logged
else:
assert 'val_loss_custom_naming_0' in logged
assert 'val_loss_custom_naming_1' in logged
def test_metrics_reset(tmpdir):
"""Tests that metrics are reset correctly after the end of the train/val/test epoch."""
class TestModel(LightningModule):
def __init__(self):
super().__init__()
self.layer = torch.nn.Linear(32, 1)
def _create_metrics(self):
acc = Accuracy()
acc.reset = mock.Mock(side_effect=acc.reset)
ap = AveragePrecision(num_classes=1, pos_label=1)
ap.reset = mock.Mock(side_effect=ap.reset)
return acc, ap
def setup(self, stage):
fn = stage
if fn == 'fit':
for stage in ('train', 'validate'):
acc, ap = self._create_metrics()
self.add_module(f"acc_{fn}_{stage}", acc)
self.add_module(f"ap_{fn}_{stage}", ap)
else:
acc, ap = self._create_metrics()
stage = self.trainer.state.stage
self.add_module(f"acc_{fn}_{stage}", acc)
self.add_module(f"ap_{fn}_{stage}", ap)
def forward(self, x):
return self.layer(x)
def _step(self, batch):
fn, stage = self.trainer.state.fn, self.trainer.state.stage
logits = self(batch)
loss = logits.sum()
self.log(f"loss/{fn}_{stage}", loss)
acc = self._modules[f"acc_{fn}_{stage}"]
ap = self._modules[f"ap_{fn}_{stage}"]
preds = torch.rand(len(batch)) # Fake preds
labels = torch.randint(0, 1, [len(batch)]) # Fake targets
acc(preds, labels)
ap(preds, labels)
# Metric.forward calls reset so reset the mocks here
acc.reset.reset_mock()
ap.reset.reset_mock()
self.log(f"acc/{fn}_{stage}", acc)
self.log(f"ap/{fn}_{stage}", ap)
return loss
def training_step(self, batch, batch_idx, *args, **kwargs):
return self._step(batch)
def validation_step(self, batch, batch_idx, *args, **kwargs):
if self.trainer.sanity_checking:
return
return self._step(batch)
def test_step(self, batch, batch_idx, *args, **kwargs):
return self._step(batch)
def configure_optimizers(self):
optimizer = torch.optim.SGD(self.layer.parameters(), lr=0.1)
lr_scheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size=1)
return [optimizer], [lr_scheduler]
def train_dataloader(self):
return DataLoader(RandomDataset(32, 64))
def val_dataloader(self):
return DataLoader(RandomDataset(32, 64))
def test_dataloader(self):
return DataLoader(RandomDataset(32, 64))
def _assert_called(model, fn, stage):
acc = model._modules[f"acc_{fn}_{stage}"]
ap = model._modules[f"ap_{fn}_{stage}"]
acc.reset.assert_called_once()
ap.reset.assert_called_once()
model = TestModel()
trainer = Trainer(
default_root_dir=tmpdir,
limit_train_batches=2,
limit_val_batches=2,
limit_test_batches=2,
max_epochs=1,
progress_bar_refresh_rate=0,
num_sanity_val_steps=2,
checkpoint_callback=False,
)
trainer.fit(model)
_assert_called(model, 'fit', 'train')
_assert_called(model, 'fit', 'validate')
trainer.validate(model)
_assert_called(model, 'validate', 'validate')
trainer.test(model)
_assert_called(model, 'test', 'test')
def test_result_collection_on_tensor_with_mean_reduction():
result_collection = ResultCollection(True, torch.device("cpu"))
product = [(True, True), (False, True), (True, False), (False, False)]
values = torch.arange(1, 10).float() # need to convert to float() due to precision issues using torch 1.4
batches = values * values
for i, v in enumerate(values):
for prog_bar in [False, True]:
for logger in [False, True]:
for on_step, on_epoch in product:
name = "loss"
if on_step:
name += "_on_step"
if on_epoch:
name += "_on_epoch"
if prog_bar:
name += "_prog_bar"
if logger:
name += "_logger"
result_collection.log(
"training_step",
name,
v,
on_step=on_step,
on_epoch=on_epoch,
batch_size=batches[i],
prog_bar=prog_bar,
logger=logger,
)
total_value = sum(values * batches)
total_batches = sum(batches)
assert result_collection["training_step.loss_on_step_on_epoch"].value == total_value
assert result_collection["training_step.loss_on_step_on_epoch"].cumulated_batch_size == total_batches
batch_metrics = result_collection.metrics(True)
max_ = max(values)
assert batch_metrics[MetricSource.PBAR] == {
'loss_on_step_on_epoch_prog_bar_step': max_,
'loss_on_step_on_epoch_prog_bar_logger_step': max_,
'loss_on_step_prog_bar': max_,
'loss_on_step_prog_bar_logger': max_,
}
assert batch_metrics[MetricSource.LOG] == {
'loss_on_step_on_epoch_logger_step': max_,
'loss_on_step_logger': max_,
'loss_on_step_on_epoch_prog_bar_logger_step': max_,
'loss_on_step_prog_bar_logger': max_,
}
assert batch_metrics[MetricSource.CALLBACK] == {
'loss_on_step': max_,
'loss_on_step_logger': max_,
'loss_on_step_on_epoch': max_,
'loss_on_step_on_epoch_logger': max_,
'loss_on_step_on_epoch_logger_step': max_,
'loss_on_step_on_epoch_prog_bar': max_,
'loss_on_step_on_epoch_prog_bar_logger': max_,
'loss_on_step_on_epoch_prog_bar_logger_step': max_,
'loss_on_step_on_epoch_prog_bar_step': max_,
'loss_on_step_on_epoch_step': max_,
'loss_on_step_prog_bar': max_,
'loss_on_step_prog_bar_logger': max_,
}
epoch_metrics = result_collection.metrics(False)
mean = total_value / total_batches
assert epoch_metrics[MetricSource.PBAR] == {
'loss_on_epoch_prog_bar': mean,
'loss_on_epoch_prog_bar_logger': mean,
'loss_on_step_on_epoch_prog_bar_epoch': mean,
'loss_on_step_on_epoch_prog_bar_logger_epoch': mean,
}
assert epoch_metrics[MetricSource.LOG] == {
'loss_on_epoch_logger': mean,
'loss_on_epoch_prog_bar_logger': mean,
'loss_on_step_on_epoch_logger_epoch': mean,
'loss_on_step_on_epoch_prog_bar_logger_epoch': mean
}
assert epoch_metrics[MetricSource.CALLBACK] == {
'loss_on_epoch': mean,
'loss_on_epoch_logger': mean,
'loss_on_epoch_prog_bar': mean,
'loss_on_epoch_prog_bar_logger': mean,
'loss_on_step_on_epoch': mean,
'loss_on_step_on_epoch_epoch': mean,
'loss_on_step_on_epoch_logger': mean,
'loss_on_step_on_epoch_logger_epoch': mean,
'loss_on_step_on_epoch_prog_bar': mean,
'loss_on_step_on_epoch_prog_bar_epoch': mean,
'loss_on_step_on_epoch_prog_bar_logger': mean,
'loss_on_step_on_epoch_prog_bar_logger_epoch': mean
}
def test_logged_metrics_has_logged_epoch_value(tmpdir):
class TestModel(BoringModel):
def training_step(self, batch, batch_idx):
self.log('epoch', -batch_idx, logger=True)
return super().training_step(batch, batch_idx)
model = TestModel()
trainer = Trainer(default_root_dir=tmpdir, fast_dev_run=2)
trainer.fit(model)
# should not get overridden if logged manually
assert trainer.logged_metrics == {'epoch': -1}