Skip to content

Commit f7c1d8b

Browse files
authored
Merge branch 'master' into docs/wandb
2 parents 6270d2c + f1ed6a2 commit f7c1d8b

File tree

7 files changed

+133
-132
lines changed

7 files changed

+133
-132
lines changed

.azure/gpu-tests-pytorch.yml

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
# Python package
2-
# Create and test a Python package on multiple Python versions.
3-
# Add steps that analyze code, save the dist with the build record, publish to a PyPI-compatible index, and more:
4-
# https://docs.microsoft.com/azure/devops/pipelines/languages/python
5-
61
trigger:
72
tags:
83
include: ["*"]
@@ -24,18 +19,18 @@ pr:
2419
- "examples/run_pl_examples.sh"
2520
- "examples/pytorch/basics/backbone_image_classifier.py"
2621
- "examples/pytorch/basics/autoencoder.py"
22+
- "requirements/fabric/**"
2723
- "requirements/pytorch/**"
2824
- "src/lightning/__init__.py"
2925
- "src/lightning/__setup__.py"
3026
- "src/lightning/__version__.py"
31-
- "src/lightning/pytorch/**"
27+
- "src/lightning_fabric/*"
28+
- "src/lightning/fabric/**"
3229
- "src/pytorch_lightning/*"
30+
- "src/lightning/pytorch/**"
3331
- "tests/tests_pytorch/**"
3432
- "tests/run_standalone_*.sh"
3533
- "pyproject.toml" # includes pytest config
36-
- "requirements/fabric/**"
37-
- "src/lightning/fabric/**"
38-
- "src/lightning_fabric/*"
3934
exclude:
4035
- "requirements/*/docs.txt"
4136
- "*.md"

docs/source-pytorch/common/trainer.rst

Lines changed: 119 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,35 @@ Number of devices to train on (``int``), which devices to train on (``list`` or
413413
# Training with GPU Accelerator using total number of gpus available on the system
414414
Trainer(accelerator="gpu")
415415
416+
417+
enable_autolog_hparams
418+
^^^^^^^^^^^^^^^^^^^^^^
419+
420+
Whether to log hyperparameters at the start of a run. Defaults to True.
421+
422+
.. testcode::
423+
424+
# default used by the Trainer
425+
trainer = Trainer(enable_autolog_hparams=True)
426+
427+
# disable logging hyperparams
428+
trainer = Trainer(enable_autolog_hparams=False)
429+
430+
With the parameter set to false, you can add custom code to log hyperparameters.
431+
432+
.. code-block:: python
433+
434+
model = LitModel()
435+
trainer = Trainer(enable_autolog_hparams=False)
436+
for logger in trainer.loggers:
437+
if isinstance(logger, lightning.pytorch.loggers.CSVLogger):
438+
logger.log_hyperparams(hparams_dict_1)
439+
else:
440+
logger.log_hyperparams(hparams_dict_2)
441+
442+
You can also use `self.logger.log_hyperparams(...)` inside `LightningModule` to log.
443+
444+
416445
enable_checkpointing
417446
^^^^^^^^^^^^^^^^^^^^
418447

@@ -443,6 +472,40 @@ See :doc:`Saving and Loading Checkpoints <../common/checkpointing>` for how to c
443472
# Add your callback to the callbacks list
444473
trainer = Trainer(callbacks=[checkpoint_callback])
445474

475+
476+
enable_model_summary
477+
^^^^^^^^^^^^^^^^^^^^
478+
479+
Whether to enable or disable the model summarization. Defaults to True.
480+
481+
.. testcode::
482+
483+
# default used by the Trainer
484+
trainer = Trainer(enable_model_summary=True)
485+
486+
# disable summarization
487+
trainer = Trainer(enable_model_summary=False)
488+
489+
# enable custom summarization
490+
from lightning.pytorch.callbacks import ModelSummary
491+
492+
trainer = Trainer(enable_model_summary=True, callbacks=[ModelSummary(max_depth=-1)])
493+
494+
495+
enable_progress_bar
496+
^^^^^^^^^^^^^^^^^^^
497+
498+
Whether to enable or disable the progress bar. Defaults to True.
499+
500+
.. testcode::
501+
502+
# default used by the Trainer
503+
trainer = Trainer(enable_progress_bar=True)
504+
505+
# disable progress bar
506+
trainer = Trainer(enable_progress_bar=False)
507+
508+
446509
fast_dev_run
447510
^^^^^^^^^^^^
448511

@@ -500,6 +563,39 @@ Gradient clipping value
500563
# default used by the Trainer
501564
trainer = Trainer(gradient_clip_val=None)
502565

566+
567+
inference_mode
568+
^^^^^^^^^^^^^^
569+
570+
Whether to use :func:`torch.inference_mode` or :func:`torch.no_grad` mode during evaluation
571+
(``validate``/``test``/``predict``)
572+
573+
.. testcode::
574+
575+
# default used by the Trainer
576+
trainer = Trainer(inference_mode=True)
577+
578+
# Use `torch.no_grad` instead
579+
trainer = Trainer(inference_mode=False)
580+
581+
582+
With :func:`torch.inference_mode` disabled, you can enable the grad of your model layers if required.
583+
584+
.. code-block:: python
585+
586+
class LitModel(LightningModule):
587+
def validation_step(self, batch, batch_idx):
588+
preds = self.layer1(batch)
589+
with torch.enable_grad():
590+
grad_preds = preds.requires_grad_()
591+
preds2 = self.layer2(grad_preds)
592+
593+
594+
model = LitModel()
595+
trainer = Trainer(inference_mode=False)
596+
trainer.validate(model)
597+
598+
503599
limit_train_batches
504600
^^^^^^^^^^^^^^^^^^^
505601

@@ -871,18 +967,6 @@ See the :doc:`profiler documentation <../tuning/profiler>` for more details.
871967
# advanced profiler for function-level stats, equivalent to `profiler=AdvancedProfiler()`
872968
trainer = Trainer(profiler="advanced")
873969

874-
enable_progress_bar
875-
^^^^^^^^^^^^^^^^^^^
876-
877-
Whether to enable or disable the progress bar. Defaults to True.
878-
879-
.. testcode::
880-
881-
# default used by the Trainer
882-
trainer = Trainer(enable_progress_bar=True)
883-
884-
# disable progress bar
885-
trainer = Trainer(enable_progress_bar=False)
886970

887971
reload_dataloaders_every_n_epochs
888972
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -917,28 +1001,6 @@ The pseudocode applies also to the ``val_dataloader``.
9171001

9181002
.. _replace-sampler-ddp:
9191003

920-
use_distributed_sampler
921-
^^^^^^^^^^^^^^^^^^^^^^^
922-
923-
See :paramref:`lightning.pytorch.trainer.Trainer.params.use_distributed_sampler`.
924-
925-
.. testcode::
926-
927-
# default used by the Trainer
928-
trainer = Trainer(use_distributed_sampler=True)
929-
930-
By setting to False, you have to add your own distributed sampler:
931-
932-
.. code-block:: python
933-
934-
# in your LightningModule or LightningDataModule
935-
def train_dataloader(self):
936-
dataset = ...
937-
# default used by the Trainer
938-
sampler = torch.utils.data.DistributedSampler(dataset, shuffle=True)
939-
dataloader = DataLoader(dataset, batch_size=32, sampler=sampler)
940-
return dataloader
941-
9421004

9431005
strategy
9441006
^^^^^^^^
@@ -982,6 +1044,29 @@ Enable synchronization between batchnorm layers across all GPUs.
9821044
trainer = Trainer(sync_batchnorm=True)
9831045

9841046

1047+
use_distributed_sampler
1048+
^^^^^^^^^^^^^^^^^^^^^^^
1049+
1050+
See :paramref:`lightning.pytorch.trainer.Trainer.params.use_distributed_sampler`.
1051+
1052+
.. testcode::
1053+
1054+
# default used by the Trainer
1055+
trainer = Trainer(use_distributed_sampler=True)
1056+
1057+
By setting to False, you have to add your own distributed sampler:
1058+
1059+
.. code-block:: python
1060+
1061+
# in your LightningModule or LightningDataModule
1062+
def train_dataloader(self):
1063+
dataset = ...
1064+
# default used by the Trainer
1065+
sampler = torch.utils.data.DistributedSampler(dataset, shuffle=True)
1066+
dataloader = DataLoader(dataset, batch_size=32, sampler=sampler)
1067+
return dataloader
1068+
1069+
9851070
val_check_interval
9861071
^^^^^^^^^^^^^^^^^^
9871072

@@ -1058,84 +1143,6 @@ Can specify as float, int, or a time-based duration.
10581143
# Total number of batches run
10591144
total_fit_batches = total_train_batches + total_val_batches
10601145
1061-
1062-
enable_model_summary
1063-
^^^^^^^^^^^^^^^^^^^^
1064-
1065-
Whether to enable or disable the model summarization. Defaults to True.
1066-
1067-
.. testcode::
1068-
1069-
# default used by the Trainer
1070-
trainer = Trainer(enable_model_summary=True)
1071-
1072-
# disable summarization
1073-
trainer = Trainer(enable_model_summary=False)
1074-
1075-
# enable custom summarization
1076-
from lightning.pytorch.callbacks import ModelSummary
1077-
1078-
trainer = Trainer(enable_model_summary=True, callbacks=[ModelSummary(max_depth=-1)])
1079-
1080-
1081-
inference_mode
1082-
^^^^^^^^^^^^^^
1083-
1084-
Whether to use :func:`torch.inference_mode` or :func:`torch.no_grad` mode during evaluation
1085-
(``validate``/``test``/``predict``)
1086-
1087-
.. testcode::
1088-
1089-
# default used by the Trainer
1090-
trainer = Trainer(inference_mode=True)
1091-
1092-
# Use `torch.no_grad` instead
1093-
trainer = Trainer(inference_mode=False)
1094-
1095-
1096-
With :func:`torch.inference_mode` disabled, you can enable the grad of your model layers if required.
1097-
1098-
.. code-block:: python
1099-
1100-
class LitModel(LightningModule):
1101-
def validation_step(self, batch, batch_idx):
1102-
preds = self.layer1(batch)
1103-
with torch.enable_grad():
1104-
grad_preds = preds.requires_grad_()
1105-
preds2 = self.layer2(grad_preds)
1106-
1107-
1108-
model = LitModel()
1109-
trainer = Trainer(inference_mode=False)
1110-
trainer.validate(model)
1111-
1112-
enable_autolog_hparams
1113-
^^^^^^^^^^^^^^^^^^^^^^
1114-
1115-
Whether to log hyperparameters at the start of a run. Defaults to True.
1116-
1117-
.. testcode::
1118-
1119-
# default used by the Trainer
1120-
trainer = Trainer(enable_autolog_hparams=True)
1121-
1122-
# disable logging hyperparams
1123-
trainer = Trainer(enable_autolog_hparams=False)
1124-
1125-
With the parameter set to false, you can add custom code to log hyperparameters.
1126-
1127-
.. code-block:: python
1128-
1129-
model = LitModel()
1130-
trainer = Trainer(enable_autolog_hparams=False)
1131-
for logger in trainer.loggers:
1132-
if isinstance(logger, lightning.pytorch.loggers.CSVLogger):
1133-
logger.log_hyperparams(hparams_dict_1)
1134-
else:
1135-
logger.log_hyperparams(hparams_dict_2)
1136-
1137-
You can also use `self.logger.log_hyperparams(...)` inside `LightningModule` to log.
1138-
11391146
-----
11401147

11411148
Trainer class API

pyproject.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,6 @@ ignore = [
120120
"S607", # todo: Starting a process with a partial executable path
121121
"RET504", # todo:Unnecessary variable assignment before `return` statement
122122
"PT004", # todo: Fixture `tmpdir_unittest_fixture` does not return anything, add leading underscore
123-
"PT011", # todo: `pytest.raises(ValueError)` is too broad, set the `match` parameter or use a more specific exception
124123
"PT012", # todo: `pytest.raises()` block should contain a single simple statement
125124
"PT019", # todo: Fixture `_` without value is injected as parameter, use `@pytest.mark.usefixtures` instead
126125
]

tests/tests_pytorch/loggers/test_neptune.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -121,19 +121,19 @@ def test_online_with_wrong_kwargs(neptune_mock):
121121
init."""
122122
run = neptune_mock.init_run()
123123

124-
with pytest.raises(ValueError):
124+
with pytest.raises(ValueError, match="Run parameter expected to be of type `neptune.Run`*"):
125125
NeptuneLogger(run="some string")
126126

127-
with pytest.raises(ValueError):
127+
with pytest.raises(ValueError, match="When an already initialized run object is provided*"):
128128
NeptuneLogger(run=run, project="redundant project")
129129

130-
with pytest.raises(ValueError):
130+
with pytest.raises(ValueError, match="When an already initialized run object is provided*"):
131131
NeptuneLogger(run=run, api_key="redundant api key")
132132

133-
with pytest.raises(ValueError):
133+
with pytest.raises(ValueError, match="When an already initialized run object is provided*"):
134134
NeptuneLogger(run=run, name="redundant api name")
135135

136-
with pytest.raises(ValueError):
136+
with pytest.raises(ValueError, match="When an already initialized run object is provided*"):
137137
NeptuneLogger(run=run, foo="random **kwarg")
138138

139139
# this should work

tests/tests_pytorch/models/test_hparams.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,7 @@ def __init__(self, arg1, arg2):
527527
)
528528
def test_single_config_models_fail(tmp_path, cls, config):
529529
"""Test fail on passing unsupported config type."""
530-
with pytest.raises(ValueError):
530+
with pytest.raises(ValueError, match=r"Primitives \(<class 'bool'>*"):
531531
_ = cls(**config)
532532

533533

tests/tests_pytorch/profilers/test_profiler.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,12 @@ def test_simple_profiler_overhead(simple_profiler):
8686
def test_simple_profiler_value_errors(simple_profiler):
8787
"""Ensure errors are raised where expected."""
8888
action = "test"
89-
with pytest.raises(ValueError):
89+
with pytest.raises(ValueError, match="Attempting to stop recording an action*"):
9090
simple_profiler.stop(action)
9191

9292
simple_profiler.start(action)
9393

94-
with pytest.raises(ValueError):
94+
with pytest.raises(ValueError, match="Attempted to start test*"):
9595
simple_profiler.start(action)
9696

9797
simple_profiler.stop(action)
@@ -325,7 +325,7 @@ def test_advanced_profiler_dump_states(tmp_path):
325325
def test_advanced_profiler_value_errors(advanced_profiler):
326326
"""Ensure errors are raised where expected."""
327327
action = "test"
328-
with pytest.raises(ValueError):
328+
with pytest.raises(ValueError, match="Attempting to stop recording*"):
329329
advanced_profiler.stop(action)
330330

331331
advanced_profiler.start(action)

tests/tests_pytorch/test_cli.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ def test_lightning_cli_save_config_seed_everything(cleandir):
339339

340340

341341
def test_save_to_log_dir_false_error():
342-
with pytest.raises(ValueError):
342+
with pytest.raises(ValueError, match="`save_to_log_dir=False` only makes sense*"):
343343
SaveConfigCallback(
344344
LightningArgumentParser(),
345345
Namespace(),

0 commit comments

Comments
 (0)