Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
e00c7e1
feat(pt_expt): add rank-aware TensorBoard training observer
OutisLi-Bot Oct 9, 2026
f720591
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 9, 2026
0d5ddfd
feat(pt_expt): add TorchProfilerObserver for profiler lifecycle
OutisLi-Bot Oct 9, 2026
67cda60
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 9, 2026
01918f6
fix(pt_expt): keep step-owned train tags on coinciding display
OutisLi-Bot Oct 9, 2026
ad942aa
merge: stack #6067 onto #6066 tip (display train/lr ownership)
OutisLi-Bot Oct 9, 2026
8393ffe
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 9, 2026
18a7083
fix(pt_expt): export combined profiler traces once for both sinks
OutisLi-Bot Oct 9, 2026
d7a006e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 9, 2026
fec294c
test(train): annotate expected RuntimeError in observer cleanup test
OutisLi-Bot Oct 9, 2026
d8c2013
fix(train): harden observer cleanup and profiler factory fallbacks
OutisLi-Bot Oct 9, 2026
b80efbb
style(profiler): move type-only imports into TYPE_CHECKING
OutisLi-Bot Oct 10, 2026
08d896c
fix(train): break observer↔trainer CodeQL cyclic import
OutisLi-Bot Oct 10, 2026
7d4303a
merge: pick up #6066 observer↔trainer CodeQL cycle break
OutisLi-Bot Oct 10, 2026
625b4bb
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 10, 2026
04a0543
fix(train): restore DEFAULT_TASK_KEY re-export after pre-commit
cmpatter Oct 10, 2026
15d540c
merge: pick up #6066 DEFAULT_TASK_KEY re-export after pre-commit
cmpatter Oct 10, 2026
331e94d
merge: pick up #6066 DEFAULT_TASK_KEY re-export
cmpatter Oct 10, 2026
c4c408b
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 10, 2026
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
22 changes: 18 additions & 4 deletions deepmd/dpmodel/train/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@
from .metrics import (
TrainingMetricAccumulator,
)
from .observer import (
CheckpointObservation,
DisplayObservation,
StepObservation,
TrainingObserver,
TrainingObserverList,
)
from .schedule import (
StepSchedule,
resolve_step_schedule,
Expand All @@ -31,32 +38,39 @@
TrainingTimer,
)
from .trainer import (
DEFAULT_TASK_KEY,
AbstractTrainer,
LearningCurveWriter,
RankContext,
TrainerConfig,
change_model_out_bias,
change_model_out_bias_by_task,
)
from .types import (
DEFAULT_TASK_KEY,
RankContext,
TrainingTask,
TrainingTaskCollection,
TrainStepResult,
change_model_out_bias,
change_model_out_bias_by_task,
)

__all__ = [
"DEFAULT_TASK_KEY",
"AbstractTrainEntrypoint",
"AbstractTrainer",
"CheckpointObservation",
"CheckpointStore",
"DisplayInterval",
"DisplayObservation",
"LearningCurveWriter",
"RankContext",
"ShardingPolicy",
"StepObservation",
"StepSchedule",
"TrainEntrypointOptions",
"TrainStepResult",
"TrainerConfig",
"TrainingMetricAccumulator",
"TrainingObserver",
"TrainingObserverList",
"TrainingTask",
"TrainingTaskCollection",
"TrainingTaskConfig",
Expand Down
2 changes: 1 addition & 1 deletion deepmd/dpmodel/train/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
StatFileSpec,
)

from .trainer import (
from .types import (
DEFAULT_TASK_KEY,
)

Expand Down
216 changes: 216 additions & 0 deletions deepmd/dpmodel/train/observer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
# SPDX-License-Identifier: LGPL-3.0-or-later
"""Composable observation hooks for the common training loop.

Observers sit outside the optimizer step and outside any compiled model
graph. They receive rank context and already-produced step or display
payloads; each observer decides whether a given boundary is worth acting
on. The training loop never imports logging backends on their behalf.
"""

from __future__ import (
annotations,
)

from abc import (
ABC,
)
from dataclasses import (
dataclass,
)
from typing import (
TYPE_CHECKING,
Any,
)

if TYPE_CHECKING:
from collections.abc import (
Iterator,
Sequence,
)

from .timing import (
DisplayInterval,
)
from .types import (
RankContext,
Comment thread
github-advanced-security[bot] marked this conversation as resolved.
Fixed
TrainingTaskCollection,
Comment thread
github-advanced-security[bot] marked this conversation as resolved.
Fixed
TrainStepResult,
Comment thread
github-advanced-security[bot] marked this conversation as resolved.
Fixed
)

__all__ = [
"CheckpointObservation",
"DisplayObservation",
"StepObservation",
"TrainingObserver",
"TrainingObserverList",
]


@dataclass(frozen=True)
class StepObservation:
"""Payload delivered after one optimizer step.

Attributes
----------
step:
Zero-based index of the step that just finished.
display_step:
One-based index used for logging and checkpoint cadence.
task_key:
Task selected for the step.
learning_rate:
Learning rate associated with ``step``.
step_result:
Backend payload returned by :meth:`AbstractTrainer.train_step`.
rank_context:
Rank metadata for the calling process.
"""

step: int
display_step: int
task_key: str
learning_rate: float
step_result: TrainStepResult
rank_context: RankContext


@dataclass(frozen=True)
class DisplayObservation:
"""Payload delivered at a learning-curve display boundary.

Attributes
----------
step:
Zero-based index of the step that just finished.
display_step:
One-based index used for logging.
task_key:
Task selected for the step.
learning_rate:
Learning rate associated with ``step``.
train_results:
Training metrics prepared for the learning curve.
valid_results:
Validation metrics prepared for the learning curve, if any.
timing:
Wall-clock summary of the interval ending at ``display_step``.
rank_context:
Rank metadata for the calling process.
"""

step: int
display_step: int
task_key: str
learning_rate: float
train_results: Any
valid_results: Any | None
timing: DisplayInterval
rank_context: RankContext


@dataclass(frozen=True)
class CheckpointObservation:
"""Payload delivered when a checkpoint boundary is reached."""

display_step: int
rank_context: RankContext


class TrainingObserver(ABC):
"""Optional side-effect hooks around the common training loop.

Default implementations are no-ops so an observer can override only the
boundaries it cares about. Implementations must keep work outside
compiled model execution and must not force device-to-host traffic when
they choose not to emit.
"""

def wants_step(self, display_step: int) -> bool:
"""Return whether :meth:`on_step_end` should run for this step."""
return False

def on_train_begin(
self,
tasks: TrainingTaskCollection,
*,
rank_context: RankContext,
) -> None:
"""Called once before the first optimizer step."""
return None

def on_step_end(self, observation: StepObservation) -> None:
"""Called after an optimizer step when :meth:`wants_step` is true."""
return None

def on_display(self, observation: DisplayObservation) -> None:
"""Called after learning-curve metrics are collected on the chief."""
return None

def on_checkpoint(self, observation: CheckpointObservation) -> None:
"""Called after a checkpoint boundary has been handled."""
return None

def on_train_end(
self,
tasks: TrainingTaskCollection,
*,
rank_context: RankContext,
) -> None:
"""Called after the run finishes or aborts, once resources close."""
return None


class TrainingObserverList(TrainingObserver):
"""Fan-out wrapper that preserves observer order."""

def __init__(self, observers: Sequence[TrainingObserver] | None = None) -> None:
self._observers = tuple(observer for observer in (observers or ()) if observer)

def __bool__(self) -> bool:
return bool(self._observers)

def __iter__(self) -> Iterator[TrainingObserver]:
return iter(self._observers)

def wants_step(self, display_step: int) -> bool:
return any(observer.wants_step(display_step) for observer in self._observers)

def on_train_begin(
self,
tasks: TrainingTaskCollection,
*,
rank_context: RankContext,
) -> None:
for observer in self._observers:
observer.on_train_begin(tasks, rank_context=rank_context)

def on_step_end(self, observation: StepObservation) -> None:
for observer in self._observers:
if observer.wants_step(observation.display_step):
observer.on_step_end(observation)

def on_display(self, observation: DisplayObservation) -> None:
for observer in self._observers:
observer.on_display(observation)

def on_checkpoint(self, observation: CheckpointObservation) -> None:
for observer in self._observers:
observer.on_checkpoint(observation)

def on_train_end(
self,
tasks: TrainingTaskCollection,
*,
rank_context: RankContext,
) -> None:
# Cleanup must run for every observer even if an earlier one fails
# (e.g. TensorBoard close must not skip stopping an active profiler).
first_error: Exception | None = None
for observer in self._observers:
try:
observer.on_train_end(tasks, rank_context=rank_context)
except Exception as error:
if first_error is None:
first_error = error
if first_error is not None:
raise first_error
Loading
Loading