Repository navigation
feat(pt_expt): add TorchProfilerObserver for profiler lifecycle #6067
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
OutisLi-Bot
wants to merge
19
commits into
deepmodeling:master
from
OutisLi-Bot:feat/5820-torch-profiler-observer
Closed
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 f720591
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 0d5ddfd
feat(pt_expt): add TorchProfilerObserver for profiler lifecycle
OutisLi-Bot 67cda60
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 01918f6
fix(pt_expt): keep step-owned train tags on coinciding display
OutisLi-Bot ad942aa
merge: stack #6067 onto #6066 tip (display train/lr ownership)
OutisLi-Bot 8393ffe
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 18a7083
fix(pt_expt): export combined profiler traces once for both sinks
OutisLi-Bot d7a006e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] fec294c
test(train): annotate expected RuntimeError in observer cleanup test
OutisLi-Bot d8c2013
fix(train): harden observer cleanup and profiler factory fallbacks
OutisLi-Bot b80efbb
style(profiler): move type-only imports into TYPE_CHECKING
OutisLi-Bot 08d896c
fix(train): break observer↔trainer CodeQL cyclic import
OutisLi-Bot 7d4303a
merge: pick up #6066 observer↔trainer CodeQL cycle break
OutisLi-Bot 625b4bb
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 04a0543
fix(train): restore DEFAULT_TASK_KEY re-export after pre-commit
cmpatter 15d540c
merge: pick up #6066 DEFAULT_TASK_KEY re-export after pre-commit
cmpatter 331e94d
merge: pick up #6066 DEFAULT_TASK_KEY re-export
cmpatter c4c408b
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,7 +19,7 @@ | |
| StatFileSpec, | ||
| ) | ||
|
|
||
| from .trainer import ( | ||
| from .types import ( | ||
| DEFAULT_TASK_KEY, | ||
| ) | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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, | ||
| TrainingTaskCollection, | ||
|
github-advanced-security[bot] marked this conversation as resolved.
Fixed
|
||
| TrainStepResult, | ||
|
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 | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.