You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
32 lines
666 B
32 lines
666 B
# mypy: allow-untyped-defs
|
|
import contextlib
|
|
from collections.abc import Callable
|
|
from typing import 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
|