From b78e5d68a29c770b33ed60a840853cea4f3fa998 Mon Sep 17 00:00:00 2001 From: Kai Zhang Date: Wed, 16 Feb 2022 21:03:27 +0000 Subject: [PATCH 1/2] test API usage decorator --- torchvision/ops/boxes.py | 4 +++- torchvision/utils.py | 15 +++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/torchvision/ops/boxes.py b/torchvision/ops/boxes.py index 38cb4c1a836..7047db7096e 100644 --- a/torchvision/ops/boxes.py +++ b/torchvision/ops/boxes.py @@ -5,7 +5,7 @@ from torch import Tensor from torchvision.extension import _assert_has_ops -from ..utils import _log_api_usage_once +from ..utils import _log_api_usage_once, log_api_usage_once_dec from ._box_convert import _box_cxcywh_to_xyxy, _box_xyxy_to_cxcywh, _box_xywh_to_xyxy, _box_xyxy_to_xywh @@ -223,6 +223,7 @@ def _upcast(t: Tensor) -> Tensor: return t if t.dtype in (torch.int32, torch.int64) else t.int() +@log_api_usage_once_dec def box_area(boxes: Tensor) -> Tensor: """ Computes the area of a set of bounding boxes, which are specified by their @@ -259,6 +260,7 @@ def _box_inter_union(boxes1: Tensor, boxes2: Tensor) -> Tuple[Tensor, Tensor]: return inter, union +@log_api_usage_once_dec def box_iou(boxes1: Tensor, boxes2: Tensor) -> Tensor: """ Return intersection-over-union (Jaccard index) between two sets of boxes. diff --git a/torchvision/utils.py b/torchvision/utils.py index 6d3293d103d..387b77269ea 100644 --- a/torchvision/utils.py +++ b/torchvision/utils.py @@ -1,6 +1,7 @@ import math import pathlib import warnings +from functools import wraps from types import FunctionType from typing import Any, BinaryIO, List, Optional, Tuple, Union @@ -547,3 +548,17 @@ def _log_api_usage_once(obj: Any) -> None: if isinstance(obj, FunctionType): name = obj.__name__ torch._C._log_api_usage_once(f"{obj.__module__}.{name}") + +def log_api_usage_once_dec(f): + @wraps(f) + def wrapper(*args, **kwargs): + event = f.__module__ + if f.__name__.endswith("__init__"): + # inside module instantiation + event += args[0].__class__.__name__ + else: + event += f.__name__ + torch._C._log_api_usage_once(event) + return f(*args, **kwargs) + + return wrapper From 146a2972a5b08f6d39395b51f70c72bc0d0564d6 Mon Sep 17 00:00:00 2001 From: Kai Zhang Date: Wed, 16 Feb 2022 23:42:00 +0000 Subject: [PATCH 2/2] use log api from #5424 --- torchvision/utils.py | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/torchvision/utils.py b/torchvision/utils.py index 387b77269ea..fa727307566 100644 --- a/torchvision/utils.py +++ b/torchvision/utils.py @@ -3,7 +3,7 @@ import warnings from functools import wraps from types import FunctionType -from typing import Any, BinaryIO, List, Optional, Tuple, Union +from typing import Any, BinaryIO, List, Optional, Tuple, Union, TypeVar, Callable, cast import numpy as np import torch @@ -549,16 +549,14 @@ def _log_api_usage_once(obj: Any) -> None: name = obj.__name__ torch._C._log_api_usage_once(f"{obj.__module__}.{name}") -def log_api_usage_once_dec(f): - @wraps(f) + +F = TypeVar("F", bound=Callable[..., Any]) + + +def log_api_usage_once_dec(fn: F) -> F: + @wraps(fn) def wrapper(*args, **kwargs): - event = f.__module__ - if f.__name__.endswith("__init__"): - # inside module instantiation - event += args[0].__class__.__name__ - else: - event += f.__name__ - torch._C._log_api_usage_once(event) - return f(*args, **kwargs) + _log_api_usage_once(fn) + return fn(*args, **kwargs) - return wrapper + return cast(F, wrapper)