Visitar URL original
Expose DataLoader kwargs (`TrainSettingsConfig.dataloader_kwargs`) + add validation helper `reject_reserved_keys` by deruyter92 · Pull Request #3554 · DeepLabCut/DeepLabCut · GitHub
Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
revert Dataloader througput settings
Reverts acbf42a and 658becf.
  • Loading branch information
deruyter92 committed Oct 16, 2026
commit 63ce13c1af8e74c40faf662ebe53c304f2a2861e
32 changes: 0 additions & 32 deletions deeplabcut/pose_estimation_pytorch/apis/training.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,30 +41,6 @@
from deeplabcut.pose_estimation_pytorch.task import Task


def build_dataloader_kwargs(
num_workers: int,
persistent_workers: bool = False,
prefetch_factor: int | None = None,
) -> dict:
"""Builds extra DataLoader kwargs for the throughput settings.

``persistent_workers``/``prefetch_factor`` only apply with
``num_workers > 0``; otherwise they are dropped with a warning.
"""
kwargs: dict = {}
if num_workers > 0:
if persistent_workers:
kwargs["persistent_workers"] = True
if prefetch_factor is not None:
kwargs["prefetch_factor"] = prefetch_factor
elif persistent_workers or prefetch_factor is not None:
logging.warning(
"dataloader_persistent_workers/prefetch_factor require "
"dataloader_workers > 0; ignoring them for single-process loading."
)
return kwargs


def train(
loader: Loader,
run_config: dict,
Expand Down Expand Up @@ -177,21 +153,13 @@ def train(
batch_size = run_config["train_settings"]["batch_size"]
num_workers = run_config["train_settings"]["dataloader_workers"]
pin_memory = run_config["train_settings"]["dataloader_pin_memory"]
persistent_workers = run_config["train_settings"].get("dataloader_persistent_workers", False)
prefetch_factor = run_config["train_settings"].get("dataloader_prefetch_factor")
dataloader_kwargs = build_dataloader_kwargs(
num_workers,
persistent_workers=persistent_workers,
prefetch_factor=prefetch_factor,
)
train_dataloader = DataLoader(
train_dataset,
batch_size=batch_size,
shuffle=True,
collate_fn=collate_fn,
num_workers=num_workers,
pin_memory=pin_memory,
**dataloader_kwargs,
)
valid_dataloader = DataLoader(valid_dataset, batch_size=1, shuffle=False)

Expand Down
2 changes: 0 additions & 2 deletions deeplabcut/pose_estimation_pytorch/config/base/base.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ train_settings:
batch_size: 8
dataloader_workers: 0
dataloader_pin_memory: false
dataloader_persistent_workers: false
dataloader_prefetch_factor: null
display_iters: 500
epochs: 200
seed: 42
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,5 @@ train_settings:
batch_size: 1
dataloader_workers: 0
dataloader_pin_memory: false
dataloader_persistent_workers: false
dataloader_prefetch_factor: null
display_iters: 500
epochs: 250
9 changes: 0 additions & 9 deletions deeplabcut/pose_estimation_pytorch/config/training.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
"""Training configuration classes for DeepLabCut pose estimation models."""

from deeplabcut.core.config import DLCBaseConfig
from deeplabcut.core.config.validation import StrictPositiveInt
from deeplabcut.core.weight_init import WeightInitialization


Expand All @@ -22,12 +21,6 @@ class TrainSettingsConfig(DLCBaseConfig):
batch_size: Training batch size
dataloader_workers: Number of data loader workers
dataloader_pin_memory: Whether to pin memory in data loader
dataloader_persistent_workers: Keep DataLoader workers alive between
epochs instead of respawning them (only applies with
``dataloader_workers > 0``)
dataloader_prefetch_factor: Batches prefetched per worker (only
applies with ``dataloader_workers > 0``; None uses the PyTorch
default of 2)
display_iters: Display interval for training progress
epochs: Number of training epochs
seed: Random seed for reproducibility
Expand All @@ -37,8 +30,6 @@ class TrainSettingsConfig(DLCBaseConfig):
batch_size: int = 8
dataloader_workers: int = 0
dataloader_pin_memory: bool = False
dataloader_persistent_workers: bool = False
dataloader_prefetch_factor: StrictPositiveInt | None = None
display_iters: int = 500
epochs: int = 200
seed: int = 42
Expand Down
7 changes: 0 additions & 7 deletions docs/pytorch/pytorch_config.md
Original file line number Diff line number Diff line change
Expand Up @@ -438,19 +438,12 @@ and [memory pinning](https://pytorch.org/docs/stable/data.html#memory-pinning).
`dataloader_workers: 0` uses single-process data loading, while setting it to 1 or more
will use multi-process data loading. You should always keep
`dataloader_pin_memory: true` when training on an NVIDIA GPU.
`dataloader_persistent_workers: true` keeps workers alive between epochs instead of
respawning them, which helps small datasets trained over many epochs; it requires
`dataloader_workers > 0` and is ignored otherwise. `dataloader_prefetch_factor` controls
how many batches each worker prefetches (`null` uses the PyTorch default of 2, values
must be >= 1); it also requires `dataloader_workers > 0`.

```yaml
train_settings:
batch_size: 1 # the batch size used for training
dataloader_workers: 0 # the number of workers for the PyTorch Dataloader
dataloader_pin_memory: true # pin DataLoader memory
dataloader_persistent_workers: false # keep workers alive between epochs (needs workers > 0)
dataloader_prefetch_factor: null # batches prefetched per worker (needs workers > 0, null = PyTorch default 2)
display_iters: 500 # the number of iterations (steps) between each log print
epochs: 200 # the maximum number of epochs for which to train the model
seed: 42 # the random seed to set for reproducibility
Expand Down
56 changes: 1 addition & 55 deletions tests/pose_estimation_pytorch/apis/test_apis_training.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,10 @@
#
"""Tests for the training API."""

import logging
from pathlib import Path
from unittest.mock import Mock, patch

import pytest

from deeplabcut.pose_estimation_pytorch.apis.training import build_dataloader_kwargs, train
from deeplabcut.pose_estimation_pytorch.apis.training import train
from deeplabcut.pose_estimation_pytorch.config import make_pytorch_pose_config
from deeplabcut.pose_estimation_pytorch.config.pose import PoseConfig
from deeplabcut.pose_estimation_pytorch.task import Task
Expand Down Expand Up @@ -58,54 +55,3 @@ def test_train_uses_resume_training_from_config(
train(loader=loader, run_config=run_config, task=Task.BOTTOM_UP, device="cpu", snapshot_path=None)

assert mock_build_runner.call_args.kwargs["snapshot_path"] == "/train/snapshot-010.pt"


@patch("deeplabcut.pose_estimation_pytorch.apis.training.DataLoader")
@patch("deeplabcut.pose_estimation_pytorch.apis.training.build_transforms", return_value=Mock())
@patch("deeplabcut.pose_estimation_pytorch.apis.training.PoseModel.build", return_value=Mock())
@patch("deeplabcut.pose_estimation_pytorch.apis.training.build_training_runner", return_value=Mock())
def test_train_passes_dataloader_throughput_settings(
mock_build_runner: Mock,
mock_build_model: Mock,
mock_build_transforms: Mock,
mock_dataloader: Mock,
tmp_path: Path,
) -> None:
"""persistent_workers/prefetch_factor from train_settings reach DataLoader."""
run_config = _minimal_run_config(tmp_path)
run_config["train_settings"]["dataloader_workers"] = 4
run_config["train_settings"]["dataloader_persistent_workers"] = True
run_config["train_settings"]["dataloader_prefetch_factor"] = 4

loader = Mock()
loader.model_folder = tmp_path
loader.model_cfg = run_config
loader.create_dataset = Mock(
side_effect=[Mock(__len__=Mock(return_value=2)), Mock(__len__=Mock(return_value=1))]
)

train(loader=loader, run_config=run_config, task=Task.BOTTOM_UP, device="cpu", snapshot_path=None)

train_kwargs = mock_dataloader.call_args_list[0].kwargs
assert train_kwargs["num_workers"] == 4
assert train_kwargs["persistent_workers"] is True
assert train_kwargs["prefetch_factor"] == 4


def test_build_dataloader_kwargs_forwards_throughput_settings() -> None:
"""Helper passes the settings through when workers are used."""
assert build_dataloader_kwargs(4, persistent_workers=True, prefetch_factor=4) == {
"persistent_workers": True,
"prefetch_factor": 4,
}
assert build_dataloader_kwargs(4) == {}


def test_build_dataloader_kwargs_ignores_throughput_settings_without_workers(
caplog: pytest.LogCaptureFixture,
) -> None:
"""Helper drops the settings (with a warning) for single-process loading."""
with caplog.at_level(logging.WARNING):
assert build_dataloader_kwargs(0, persistent_workers=True, prefetch_factor=4) == {}

assert "dataloader_workers > 0" in caplog.text
57 changes: 0 additions & 57 deletions tests/pose_estimation_pytorch/config/test_train_settings.py

This file was deleted.