Visitar URL original
fix WandbLoggerConfig include additional kwargs in pydantic schema by deruyter92 · Pull Request #3469 · DeepLabCut/DeepLabCut · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from 10 commits
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
49 changes: 48 additions & 1 deletion deeplabcut/pose_estimation_pytorch/config/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@
#
"""Logger configuration classes for DeepLabCut training runs."""

from __future__ import annotations

from enum import Enum
from typing import Literal
from typing import Any, ClassVar, Literal

from pydantic import field_validator, model_validator

from deeplabcut.core.config import DLCBaseConfig

Expand All @@ -37,6 +41,10 @@ class WandbLoggerConfig(LoggerConfig): #
This logger tracks experiments and logs data to Weights & Biases.
Refer to: https://docs.wandb.ai/guides for more information.

Notes:
Different config versions might have top-level WandB options instead of
wandb_kwargs. Unknown top-level fields are scooped into wandb_kwargs.

Attributes:
type: Logger type (should be 'WandbLogger')
project_name: The name of the wandb project
Expand All @@ -48,6 +56,9 @@ class WandbLoggerConfig(LoggerConfig): #
wandb_kwargs: Additional keyword arguments to pass to wandb.init
"""

# These options are reserved since they are explicitly passed to wandb.init (from `project_name` and `run_name`)
_FORBIDDEN_WANDB_KWARGS: ClassVar[frozenset[str]] = frozenset({"project", "name"})

type: Literal[LoggerType.WandbLogger]
project_name: str = "deeplabcut"
run_name: str = "tmp"
Expand All @@ -56,6 +67,42 @@ class WandbLoggerConfig(LoggerConfig): #
train_folder: str | None = None
wandb_kwargs: dict | None = None

@model_validator(mode="before")
@classmethod
def scoop_extra_wandb_kwargs(cls, data: Any) -> Any:
"""Move unknown top-level keys into ``wandb_kwargs``"""
if not isinstance(data, dict):
return data

data = dict(data)
known = set(cls.model_fields) | set(cls._alias_map())
provided = set(data)
extras = {key: data.pop(key) for key in provided - known}
Comment thread
deruyter92 marked this conversation as resolved.
Resolved
if not extras:
return data

nested = data.get("wandb_kwargs") or {}
if not isinstance(nested, dict):
raise ValueError("wandb_kwargs must be a dictionary when provided")

overlap = set(extras) & set(nested)
if overlap:
raise ValueError(f"Duplicate wandb.init options at top-level and in wandb_kwargs: {sorted(overlap)}")

data["wandb_kwargs"] = {**nested, **extras}
return data

@field_validator("wandb_kwargs")
@classmethod
def validate_wandb_kwargs(cls, v: dict | None) -> dict | None:
Comment thread
C-Achard marked this conversation as resolved.
Resolved
"""Reject nested keys that collide with reserved wandb.init options."""
if not v:
return v
if set(v) & cls._FORBIDDEN_WANDB_KWARGS:
raise ValueError(
f"wandb_kwargs cannot include any of the reserved options: {sorted(cls._FORBIDDEN_WANDB_KWARGS)}"
)


class CSVLoggerConfig(LoggerConfig): #
"""Configuration for CSV logger.
Expand Down
7 changes: 6 additions & 1 deletion deeplabcut/pose_estimation_pytorch/runners/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,9 @@ def __init__(
None, train/test inputs are never logged).
model: The model to log. Defaults to None.
train_folder: path to the train folder (used to store the W&B run identifiers)
wandb_kwargs: extra arguments to pass to ``wb.init``
wandb_kwargs: extra arguments to pass to ``wandb.init``. These can be given as
keyword arguments, or collected in a single ``wandb_kwargs`` mapping (as
declared by ``WandbLoggerConfig``).

Example:
logger = WandbLogger(project_name="mice", run_name="exp1", model=my_model)
Expand All @@ -291,6 +293,9 @@ def __init__(
if wandb.run is not None:
wandb.finish()

# A nested ``wandb_kwargs`` mapping must be flattened
wandb_kwargs.update(wandb_kwargs.pop("wandb_kwargs", None) or {})

self.run = wandb.init(
project=project_name,
name=run_name,
Expand Down
85 changes: 85 additions & 0 deletions tests/pose_estimation_pytorch/config/test_logger_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#
# DeepLabCut Toolbox (deeplabcut.org)
# © A. & M.W. Mathis Labs
# https://github.com/DeepLabCut/DeepLabCut
#
# Please see AUTHORS for contributors.
# https://github.com/DeepLabCut/DeepLabCut/blob/main/AUTHORS
#
# Licensed under GNU Lesser General Public License v3.0
#
"""Tests for WandbLoggerConfig validation."""

from __future__ import annotations

import pytest
from pydantic import ValidationError

from deeplabcut.pose_estimation_pytorch.config.logger import LoggerType, WandbLoggerConfig


def test_wandb_kwargs_accepts_nested_extras() -> None:
cfg = WandbLoggerConfig.from_dict(
{
"type": LoggerType.WandbLogger,
"wandb_kwargs": {
"entity": "team",
"mode": "offline",
"tags": ["a"],
"settings": {"_service_wait": 60},
},
}
)
assert cfg.wandb_kwargs["entity"] == "team"
assert cfg.wandb_kwargs["settings"] == {"_service_wait": 60}


def test_scoops_top_level_extras_into_wandb_kwargs() -> None:
cfg = WandbLoggerConfig.from_dict(
{
"type": LoggerType.WandbLogger,
"project_name": "proj",
"group": "group_a",
"tags": ["mytag"],
}
)
assert cfg.project_name == "proj"
assert cfg.wandb_kwargs == {"group": "group_a", "tags": ["mytag"]}


def test_mixed_top_level_and_nested_wandb_kwargs() -> None:
cfg = WandbLoggerConfig.from_dict(
{
"type": LoggerType.WandbLogger,
"group": "group_a",
"wandb_kwargs": {"tags": ["mytag"], "mode": "offline", "id": "run-1"},
}
)
assert cfg.wandb_kwargs == {
"tags": ["mytag"],
"mode": "offline",
"id": "run-1",
"group": "group_a",
}


def test_rejects_duplicate_top_level_and_nested() -> None:
with pytest.raises(ValidationError, match="Duplicate wandb.init options"):
WandbLoggerConfig.from_dict(
{
"type": LoggerType.WandbLogger,
"tags": ["a"],
"wandb_kwargs": {"tags": ["b"], "mode": "offline"},
}
)


@pytest.mark.parametrize("overlap_key", ["project", "name"])
def test_wandb_kwargs_rejects_reserved_keys(overlap_key: str) -> None:
with pytest.raises(ValidationError, match="reserved options"):
WandbLoggerConfig.from_dict(
{
"type": LoggerType.WandbLogger,
"wandb_kwargs": {overlap_key: "x", "mode": "offline"},
}
)
Loading