mirror of
https://github.com/pytorch/pytorch.git
synced 2025-10-20 21:14:14 +08:00
See #145101 for details. Pull Request resolved: https://github.com/pytorch/pytorch/pull/145137 Approved by: https://github.com/bobrenjc93
31 lines
639 B
Python
31 lines
639 B
Python
# mypy: allow-untyped-defs
|
|
import contextlib
|
|
from typing import Callable, TYPE_CHECKING
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
import torch
|
|
|
|
# Executed in the order they're registered
|
|
INTERMEDIATE_HOOKS: list[Callable[[str, "torch.Tensor"], None]] = []
|
|
|
|
|
|
@contextlib.contextmanager
|
|
def intermediate_hook(fn):
|
|
INTERMEDIATE_HOOKS.append(fn)
|
|
try:
|
|
yield
|
|
finally:
|
|
INTERMEDIATE_HOOKS.pop()
|
|
|
|
|
|
def run_intermediate_hooks(name, val):
|
|
global INTERMEDIATE_HOOKS
|
|
hooks = INTERMEDIATE_HOOKS
|
|
INTERMEDIATE_HOOKS = []
|
|
try:
|
|
for hook in hooks:
|
|
hook(name, val)
|
|
finally:
|
|
INTERMEDIATE_HOOKS = hooks
|