mirror of
https://github.com/zebrajr/pytorch.git
synced 2025-12-06 12:20:52 +01:00
When using dmypy, this setting is enabled and cannot be turned off. Force it for regular mypy too. Signed-off-by: Edward Z. Yang <ezyang@meta.com> Pull Request resolved: https://github.com/pytorch/pytorch/pull/118467 Approved by: https://github.com/Skylion007 ghstack dependencies: #118414, #118418, #118432
29 lines
617 B
Python
29 lines
617 B
Python
import contextlib
|
|
from typing import Callable, List, 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
|