Skip to content

Commit ef72f4b

Browse files
dennisbaderhrzn
andauthored
Fix/ptl fixes (#806)
* fixed lr schedulers and added tests * added check for invalid model creation params * additional lr scheduler fix * added deprecated to docs * move torch import in tests for flavors * fix wrong parameter docs * updated torch device docs Co-authored-by: Julien Herzen <[email protected]>
1 parent 6da5957 commit ef72f4b

9 files changed

+266
-24
lines changed

darts/models/forecasting/block_rnn_model.py

+17-2
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,23 @@ def __init__(
213213
Number of epochs to wait before evaluating the validation loss (if a validation
214214
``TimeSeries`` is passed to the :func:`fit()` method).
215215
torch_device_str
216-
Optionally, a string indicating the torch device to use. (default: "cuda:0" if a GPU
217-
is available, otherwise "cpu")
216+
Optionally, a string indicating the torch device to use. By default, ``torch_device_str`` is ``None``
217+
which will run on CPU. Set it to ``"cuda"`` to use all available GPUs or ``"cuda:i"`` to only use
218+
GPU ``i`` (``i`` must be an integer). For example "cuda:0" will use the first GPU only.
219+
220+
.. deprecated:: v0.17.0
221+
``torch_device_str`` has been deprecated in v0.17.0 and will be removed in a future version.
222+
Instead, specify this with keys ``"accelerator", "gpus", "auto_select_gpus"`` in your
223+
``pl_trainer_kwargs`` dict. Some examples for setting the devices inside the ``pl_trainer_kwargs``
224+
dict:
225+
226+
- ``{"accelerator": "cpu"}`` for CPU,
227+
- ``{"accelerator": "gpu", "gpus": [i]}`` to use only GPU ``i`` (``i`` must be an integer),
228+
- ``{"accelerator": "gpu", "gpus": -1, "auto_select_gpus": True}`` to use all available GPUS.
229+
230+
For more info, see here:
231+
https://pytorch-lightning.readthedocs.io/en/stable/common/trainer.html#trainer-flags , and
232+
https://pytorch-lightning.readthedocs.io/en/stable/advanced/multi_gpu.html#select-gpu-devices
218233
force_reset
219234
If set to ``True``, any previously-existing model with the same name will be reset (all checkpoints will
220235
be discarded).

darts/models/forecasting/nbeats.py

+17-2
Original file line numberDiff line numberDiff line change
@@ -546,8 +546,23 @@ def __init__(
546546
Number of epochs to wait before evaluating the validation loss (if a validation
547547
``TimeSeries`` is passed to the :func:`fit()` method).
548548
torch_device_str
549-
Optionally, a string indicating the torch device to use. (default: "cuda:0" if a GPU
550-
is available, otherwise "cpu")
549+
Optionally, a string indicating the torch device to use. By default, ``torch_device_str`` is ``None``
550+
which will run on CPU. Set it to ``"cuda"`` to use all available GPUs or ``"cuda:i"`` to only use
551+
GPU ``i`` (``i`` must be an integer). For example "cuda:0" will use the first GPU only.
552+
553+
.. deprecated:: v0.17.0
554+
``torch_device_str`` has been deprecated in v0.17.0 and will be removed in a future version.
555+
Instead, specify this with keys ``"accelerator", "gpus", "auto_select_gpus"`` in your
556+
``pl_trainer_kwargs`` dict. Some examples for setting the devices inside the ``pl_trainer_kwargs``
557+
dict:
558+
559+
- ``{"accelerator": "cpu"}`` for CPU,
560+
- ``{"accelerator": "gpu", "gpus": [i]}`` to use only GPU ``i`` (``i`` must be an integer),
561+
- ``{"accelerator": "gpu", "gpus": -1, "auto_select_gpus": True}`` to use all available GPUS.
562+
563+
For more info, see here:
564+
https://pytorch-lightning.readthedocs.io/en/stable/common/trainer.html#trainer-flags , and
565+
https://pytorch-lightning.readthedocs.io/en/stable/advanced/multi_gpu.html#select-gpu-devices
551566
force_reset
552567
If set to ``True``, any previously-existing model with the same name will be reset (all checkpoints will
553568
be discarded).

darts/models/forecasting/pl_forecasting_module.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -252,10 +252,17 @@ def _create_from_cls_and_kwargs(cls, kws):
252252
if self.lr_scheduler_cls is not None:
253253
lr_sched_kws = {k: v for k, v in self.lr_scheduler_kwargs.items()}
254254
lr_sched_kws["optimizer"] = optimizer
255+
256+
# ReduceLROnPlateau requires a metric to "monitor" which must be set separately, most others do not
257+
lr_monitor = lr_sched_kws.pop("monitor", None)
258+
255259
lr_scheduler = _create_from_cls_and_kwargs(
256260
self.lr_scheduler_cls, lr_sched_kws
257261
)
258-
return [optimizer], [lr_scheduler]
262+
return [optimizer], {
263+
"scheduler": lr_scheduler,
264+
"monitor": lr_monitor if lr_monitor is not None else "val_loss",
265+
}
259266
else:
260267
return optimizer
261268

darts/models/forecasting/rnn_model.py

+17-2
Original file line numberDiff line numberDiff line change
@@ -279,8 +279,23 @@ def __init__(
279279
Number of epochs to wait before evaluating the validation loss (if a validation
280280
``TimeSeries`` is passed to the :func:`fit()` method).
281281
torch_device_str
282-
Optionally, a string indicating the torch device to use. (default: "cuda:0" if a GPU
283-
is available, otherwise "cpu")
282+
Optionally, a string indicating the torch device to use. By default, ``torch_device_str`` is ``None``
283+
which will run on CPU. Set it to ``"cuda"`` to use all available GPUs or ``"cuda:i"`` to only use
284+
GPU ``i`` (``i`` must be an integer). For example "cuda:0" will use the first GPU only.
285+
286+
.. deprecated:: v0.17.0
287+
``torch_device_str`` has been deprecated in v0.17.0 and will be removed in a future version.
288+
Instead, specify this with keys ``"accelerator", "gpus", "auto_select_gpus"`` in your
289+
``pl_trainer_kwargs`` dict. Some examples for setting the devices inside the ``pl_trainer_kwargs``
290+
dict:
291+
292+
- ``{"accelerator": "cpu"}`` for CPU,
293+
- ``{"accelerator": "gpu", "gpus": [i]}`` to use only GPU ``i`` (``i`` must be an integer),
294+
- ``{"accelerator": "gpu", "gpus": -1, "auto_select_gpus": True}`` to use all available GPUS.
295+
296+
For more info, see here:
297+
https://pytorch-lightning.readthedocs.io/en/stable/common/trainer.html#trainer-flags , and
298+
https://pytorch-lightning.readthedocs.io/en/stable/advanced/multi_gpu.html#select-gpu-devices
284299
force_reset
285300
If set to ``True``, any previously-existing model with the same name will be reset (all checkpoints will
286301
be discarded).

darts/models/forecasting/tcn_model.py

+17-2
Original file line numberDiff line numberDiff line change
@@ -332,8 +332,23 @@ def __init__(
332332
Number of epochs to wait before evaluating the validation loss (if a validation
333333
``TimeSeries`` is passed to the :func:`fit()` method).
334334
torch_device_str
335-
Optionally, a string indicating the torch device to use. (default: "cuda:0" if a GPU
336-
is available, otherwise "cpu")
335+
Optionally, a string indicating the torch device to use. By default, ``torch_device_str`` is ``None``
336+
which will run on CPU. Set it to ``"cuda"`` to use all available GPUs or ``"cuda:i"`` to only use
337+
GPU ``i`` (``i`` must be an integer). For example "cuda:0" will use the first GPU only.
338+
339+
.. deprecated:: v0.17.0
340+
``torch_device_str`` has been deprecated in v0.17.0 and will be removed in a future version.
341+
Instead, specify this with keys ``"accelerator", "gpus", "auto_select_gpus"`` in your
342+
``pl_trainer_kwargs`` dict. Some examples for setting the devices inside the ``pl_trainer_kwargs``
343+
dict:
344+
345+
- ``{"accelerator": "cpu"}`` for CPU,
346+
- ``{"accelerator": "gpu", "gpus": [i]}`` to use only GPU ``i`` (``i`` must be an integer),
347+
- ``{"accelerator": "gpu", "gpus": -1, "auto_select_gpus": True}`` to use all available GPUS.
348+
349+
For more info, see here:
350+
https://pytorch-lightning.readthedocs.io/en/stable/common/trainer.html#trainer-flags , and
351+
https://pytorch-lightning.readthedocs.io/en/stable/advanced/multi_gpu.html#select-gpu-devices
337352
force_reset
338353
If set to ``True``, any previously-existing model with the same name will be reset (all checkpoints will
339354
be discarded).

darts/models/forecasting/tft_model.py

+17-2
Original file line numberDiff line numberDiff line change
@@ -639,8 +639,23 @@ def __init__(
639639
Number of epochs to wait before evaluating the validation loss (if a validation
640640
``TimeSeries`` is passed to the :func:`fit()` method).
641641
torch_device_str
642-
Optionally, a string indicating the torch device to use. (default: "cuda:0" if a GPU
643-
is available, otherwise "cpu")
642+
Optionally, a string indicating the torch device to use. By default, ``torch_device_str`` is ``None``
643+
which will run on CPU. Set it to ``"cuda"`` to use all available GPUs or ``"cuda:i"`` to only use
644+
GPU ``i`` (``i`` must be an integer). For example "cuda:0" will use the first GPU only.
645+
646+
.. deprecated:: v0.17.0
647+
``torch_device_str`` has been deprecated in v0.17.0 and will be removed in a future version.
648+
Instead, specify this with keys ``"accelerator", "gpus", "auto_select_gpus"`` in your
649+
``pl_trainer_kwargs`` dict. Some examples for setting the devices inside the ``pl_trainer_kwargs``
650+
dict:
651+
652+
- ``{"accelerator": "cpu"}`` for CPU,
653+
- ``{"accelerator": "gpu", "gpus": [i]}`` to use only GPU ``i`` (``i`` must be an integer),
654+
- ``{"accelerator": "gpu", "gpus": -1, "auto_select_gpus": True}`` to use all available GPUS.
655+
656+
For more info, see here:
657+
https://pytorch-lightning.readthedocs.io/en/stable/common/trainer.html#trainer-flags , and
658+
https://pytorch-lightning.readthedocs.io/en/stable/advanced/multi_gpu.html#select-gpu-devices
644659
force_reset
645660
If set to ``True``, any previously-existing model with the same name will be reset (all checkpoints will
646661
be discarded).

darts/models/forecasting/torch_forecasting_model.py

+76-11
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,23 @@ def __init__(
161161
Number of epochs to wait before evaluating the validation loss (if a validation
162162
``TimeSeries`` is passed to the :func:`fit()` method).
163163
torch_device_str
164-
Optionally, a string indicating the torch device to use. (default: "cuda:0" if a GPU
165-
is available, otherwise "cpu")
164+
Optionally, a string indicating the torch device to use. By default, ``torch_device_str`` is ``None``
165+
which will run on CPU. Set it to ``"cuda"`` to use all available GPUs or ``"cuda:i"`` to only use
166+
GPU ``i`` (``i`` must be an integer). For example "cuda:0" will use the first GPU only.
167+
168+
.. deprecated:: v0.17.0
169+
``torch_device_str`` has been deprecated in v0.17.0 and will be removed in a future version.
170+
Instead, specify this with keys ``"accelerator", "gpus", "auto_select_gpus"`` in your
171+
``pl_trainer_kwargs`` dict. Some examples for setting the devices inside the ``pl_trainer_kwargs``
172+
dict:
173+
174+
- ``{"accelerator": "cpu"}`` for CPU,
175+
- ``{"accelerator": "gpu", "gpus": [i]}`` to use only GPU ``i`` (``i`` must be an integer),
176+
- ``{"accelerator": "gpu", "gpus": -1, "auto_select_gpus": True}`` to use all available GPUS.
177+
178+
For more info, see here:
179+
https://pytorch-lightning.readthedocs.io/en/stable/common/trainer.html#trainer-flags , and
180+
https://pytorch-lightning.readthedocs.io/en/stable/advanced/multi_gpu.html#select-gpu-devices
166181
force_reset
167182
If set to ``True``, any previously-existing model with the same name will be reset (all checkpoints will
168183
be discarded).
@@ -336,7 +351,9 @@ def __init__(
336351
self.pl_module_params: Optional[Dict] = None
337352

338353
@staticmethod
339-
def _extract_torch_devices(torch_device_str) -> Tuple[str, Optional[list], bool]:
354+
def _extract_torch_devices(
355+
torch_device_str,
356+
) -> Tuple[str, Optional[Union[list, int]], bool]:
340357
"""This method handles the deprecated `torch_device_str` and should be removed in a future Darts version.
341358
342359
Returns
@@ -346,7 +363,7 @@ def _extract_torch_devices(torch_device_str) -> Tuple[str, Optional[list], bool]
346363
"""
347364

348365
if torch_device_str is None:
349-
return "auto", None, False
366+
return "cpu", None, False
350367

351368
device_warning = (
352369
"`torch_device_str` is deprecated and will be removed in a coming Darts version. For full support "
@@ -372,13 +389,13 @@ def _extract_torch_devices(torch_device_str) -> Tuple[str, Optional[list], bool]
372389

373390
gpus = None
374391
auto_select_gpus = False
375-
accelerator = device_split[0]
376-
if len(device_split) == 2 and accelerator == "cuda":
392+
accelerator = "gpu" if device_split[0] == "cuda" else device_split[0]
393+
394+
if len(device_split) == 2 and accelerator == "gpu":
377395
gpus = device_split[1]
378396
gpus = [int(gpus)]
379397
elif len(device_split) == 1:
380-
if accelerator == "cuda":
381-
accelerator = "gpu"
398+
if accelerator == "gpu":
382399
gpus = -1
383400
auto_select_gpus = True
384401
else:
@@ -389,9 +406,29 @@ def _extract_torch_devices(torch_device_str) -> Tuple[str, Optional[list], bool]
389406
)
390407
return accelerator, gpus, auto_select_gpus
391408

392-
@staticmethod
393-
def _extract_torch_model_params(**kwargs):
409+
@classmethod
410+
def _validate_model_params(cls, **kwargs):
411+
"""validate that parameters used at model creation are part of :class:`TorchForecastingModel`,
412+
:class:`PLForecastingModule` or cls __init__ methods.
413+
"""
414+
valid_kwargs = (
415+
set(inspect.signature(TorchForecastingModel.__init__).parameters.keys())
416+
| set(inspect.signature(PLForecastingModule.__init__).parameters.keys())
417+
| set(inspect.signature(cls.__init__).parameters.keys())
418+
)
419+
420+
invalid_kwargs = [kwarg for kwarg in kwargs if kwarg not in valid_kwargs]
421+
422+
raise_if(
423+
len(invalid_kwargs) > 0,
424+
f"Invalid model creation parameters. Model `{cls.__name__}` has no args/kwargs `{invalid_kwargs}`",
425+
logger=logger,
426+
)
427+
428+
@classmethod
429+
def _extract_torch_model_params(cls, **kwargs):
394430
"""extract params from model creation to set up TorchForecastingModels"""
431+
cls._validate_model_params(**kwargs)
395432
get_params = list(
396433
inspect.signature(TorchForecastingModel.__init__).parameters.keys()
397434
)
@@ -619,6 +656,13 @@ def fit(
619656
override Darts' default trainer.
620657
verbose
621658
Optionally, whether to print progress.
659+
660+
.. deprecated:: v0.17.0
661+
``verbose`` has been deprecated in v0.17.0 and will be removed in a future version.
662+
Instead, control verbosity with PyTorch Lightning Trainer parameters ``enable_progress_bar``,
663+
``progress_bar_refresh_rate`` and ``enable_model_summary`` in the ``pl_trainer_kwargs`` dict
664+
at model creation. See for example here:
665+
https://pytorch-lightning.readthedocs.io/en/stable/common/trainer.html#enable-progress-bar
622666
epochs
623667
If specified, will train the model for ``epochs`` (additional) epochs, irrespective of what ``n_epochs``
624668
was provided to the model constructor.
@@ -764,6 +808,13 @@ def fit_from_dataset(
764808
override Darts' default trainer.
765809
verbose
766810
Optionally, whether to print progress.
811+
812+
.. deprecated:: v0.17.0
813+
``verbose`` has been deprecated in v0.17.0 and will be removed in a future version.
814+
Instead, control verbosity with PyTorch Lightning Trainer parameters ``enable_progress_bar``,
815+
``progress_bar_refresh_rate`` and ``enable_model_summary`` in the ``pl_trainer_kwargs`` dict
816+
at model creation. See for example here:
817+
https://pytorch-lightning.readthedocs.io/en/stable/common/trainer.html#enable-progress-bar
767818
epochs
768819
If specified, will train the model for ``epochs`` (additional) epochs, irrespective of what ``n_epochs``
769820
was provided to the model constructor.
@@ -965,6 +1016,13 @@ def predict(
9651016
Size of batches during prediction. Defaults to the models' training ``batch_size`` value.
9661017
verbose
9671018
Optionally, whether to print progress.
1019+
1020+
.. deprecated:: v0.17.0
1021+
``verbose`` has been deprecated in v0.17.0 and will be removed in a future version.
1022+
Instead, control verbosity with PyTorch Lightning Trainer parameters ``enable_progress_bar``,
1023+
``progress_bar_refresh_rate`` and ``enable_model_summary`` in the ``pl_trainer_kwargs`` dict
1024+
at model creation. See for example here:
1025+
https://pytorch-lightning.readthedocs.io/en/stable/common/trainer.html#enable-progress-bar
9681026
n_jobs
9691027
The number of jobs to run in parallel. ``-1`` means using all processors. Defaults to ``1``.
9701028
roll_size
@@ -1084,7 +1142,14 @@ def predict_from_dataset(
10841142
batch_size
10851143
Size of batches during prediction. Defaults to the models ``batch_size`` value.
10861144
verbose
1087-
Shows the progress bar for batch predicition. Off by default.
1145+
Optionally, whether to print progress.
1146+
1147+
.. deprecated:: v0.17.0
1148+
``verbose`` has been deprecated in v0.17.0 and will be removed in a future version.
1149+
Instead, control verbosity with PyTorch Lightning Trainer parameters ``enable_progress_bar``,
1150+
``progress_bar_refresh_rate`` and ``enable_model_summary`` in the ``pl_trainer_kwargs`` dict
1151+
at model creation. See for example here:
1152+
https://pytorch-lightning.readthedocs.io/en/stable/common/trainer.html#enable-progress-bar
10881153
n_jobs
10891154
The number of jobs to run in parallel. ``-1`` means using all processors. Defaults to ``1``.
10901155
roll_size

darts/models/forecasting/transformer_model.py

+17-2
Original file line numberDiff line numberDiff line change
@@ -289,8 +289,23 @@ def __init__(
289289
Number of epochs to wait before evaluating the validation loss (if a validation
290290
``TimeSeries`` is passed to the :func:`fit()` method).
291291
torch_device_str
292-
Optionally, a string indicating the torch device to use. (default: "cuda:0" if a GPU
293-
is available, otherwise "cpu")
292+
Optionally, a string indicating the torch device to use. By default, ``torch_device_str`` is ``None``
293+
which will run on CPU. Set it to ``"cuda"`` to use all available GPUs or ``"cuda:i"`` to only use
294+
GPU ``i`` (``i`` must be an integer). For example "cuda:0" will use the first GPU only.
295+
296+
.. deprecated:: v0.17.0
297+
``torch_device_str`` has been deprecated in v0.17.0 and will be removed in a future version.
298+
Instead, specify this with keys ``"accelerator", "gpus", "auto_select_gpus"`` in your
299+
``pl_trainer_kwargs`` dict. Some examples for setting the devices inside the ``pl_trainer_kwargs``
300+
dict:
301+
302+
- ``{"accelerator": "cpu"}`` for CPU,
303+
- ``{"accelerator": "gpu", "gpus": [i]}`` to use only GPU ``i`` (``i`` must be an integer),
304+
- ``{"accelerator": "gpu", "gpus": -1, "auto_select_gpus": True}`` to use all available GPUS.
305+
306+
For more info, see here:
307+
https://pytorch-lightning.readthedocs.io/en/stable/common/trainer.html#trainer-flags , and
308+
https://pytorch-lightning.readthedocs.io/en/stable/advanced/multi_gpu.html#select-gpu-devices
294309
force_reset
295310
If set to ``True``, any previously-existing model with the same name will be reset (all checkpoints will
296311
be discarded).

0 commit comments

Comments
 (0)