Skip to content

Simplified usage log API #5095

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

Merged
merged 11 commits into from
Dec 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions torchvision/csrc/ops/deform_conv2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ at::Tensor deform_conv2d(
int64_t groups,
int64_t offset_groups,
bool use_mask) {
C10_LOG_API_USAGE_ONCE("torchvision.csrc.ops.deform_conv2d.deform_conv2d");
static auto op = c10::Dispatcher::singleton()
.findSchemaOrThrow("torchvision::deform_conv2d", "")
.typed<decltype(deform_conv2d)>();
Expand Down
1 change: 1 addition & 0 deletions torchvision/csrc/ops/nms.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ at::Tensor nms(
const at::Tensor& dets,
const at::Tensor& scores,
double iou_threshold) {
C10_LOG_API_USAGE_ONCE("torchvision.csrc.ops.nms.nms");
static auto op = c10::Dispatcher::singleton()
.findSchemaOrThrow("torchvision::nms", "")
.typed<decltype(nms)>();
Expand Down
1 change: 1 addition & 0 deletions torchvision/csrc/ops/ps_roi_align.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ std::tuple<at::Tensor, at::Tensor> ps_roi_align(
int64_t pooled_height,
int64_t pooled_width,
int64_t sampling_ratio) {
C10_LOG_API_USAGE_ONCE("torchvision.csrc.ops.ps_roi_align.ps_roi_align");
static auto op = c10::Dispatcher::singleton()
.findSchemaOrThrow("torchvision::ps_roi_align", "")
.typed<decltype(ps_roi_align)>();
Expand Down
1 change: 1 addition & 0 deletions torchvision/csrc/ops/ps_roi_pool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ std::tuple<at::Tensor, at::Tensor> ps_roi_pool(
double spatial_scale,
int64_t pooled_height,
int64_t pooled_width) {
C10_LOG_API_USAGE_ONCE("torchvision.csrc.ops.ps_roi_pool.ps_roi_pool");
static auto op = c10::Dispatcher::singleton()
.findSchemaOrThrow("torchvision::ps_roi_pool", "")
.typed<decltype(ps_roi_pool)>();
Expand Down
1 change: 1 addition & 0 deletions torchvision/csrc/ops/roi_align.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ at::Tensor roi_align(
bool aligned) // The flag for pixel shift
// along each axis.
{
C10_LOG_API_USAGE_ONCE("torchvision.csrc.ops.roi_align.roi_align");
static auto op = c10::Dispatcher::singleton()
.findSchemaOrThrow("torchvision::roi_align", "")
.typed<decltype(roi_align)>();
Expand Down
1 change: 1 addition & 0 deletions torchvision/csrc/ops/roi_pool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ std::tuple<at::Tensor, at::Tensor> roi_pool(
double spatial_scale,
int64_t pooled_height,
int64_t pooled_width) {
C10_LOG_API_USAGE_ONCE("torchvision.csrc.ops.roi_pool.roi_pool");
static auto op = c10::Dispatcher::singleton()
.findSchemaOrThrow("torchvision::roi_pool", "")
.typed<decltype(roi_pool)>();
Expand Down
2 changes: 1 addition & 1 deletion torchvision/datasets/vision.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def __init__(
transform: Optional[Callable] = None,
target_transform: Optional[Callable] = None,
) -> None:
_log_api_usage_once("datasets", self.__class__.__name__)
_log_api_usage_once(self)
if isinstance(root, torch._six.string_classes):
root = os.path.expanduser(root)
self.root = root
Expand Down
2 changes: 1 addition & 1 deletion torchvision/models/alexnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
class AlexNet(nn.Module):
def __init__(self, num_classes: int = 1000, dropout: float = 0.5) -> None:
super().__init__()
_log_api_usage_once("models", self.__class__.__name__)
_log_api_usage_once(self)
self.features = nn.Sequential(
nn.Conv2d(3, 64, kernel_size=11, stride=4, padding=2),
nn.ReLU(inplace=True),
Expand Down
2 changes: 1 addition & 1 deletion torchvision/models/densenet.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ def __init__(
) -> None:

super().__init__()
_log_api_usage_once("models", self.__class__.__name__)
_log_api_usage_once(self)

# First convolution
self.features = nn.Sequential(
Expand Down
2 changes: 1 addition & 1 deletion torchvision/models/detection/generalized_rcnn.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class GeneralizedRCNN(nn.Module):

def __init__(self, backbone: nn.Module, rpn: nn.Module, roi_heads: nn.Module, transform: nn.Module) -> None:
super().__init__()
_log_api_usage_once("models", self.__class__.__name__)
_log_api_usage_once(self)
self.transform = transform
self.backbone = backbone
self.rpn = rpn
Expand Down
2 changes: 1 addition & 1 deletion torchvision/models/detection/retinanet.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ def __init__(
topk_candidates=1000,
):
super().__init__()
_log_api_usage_once("models", self.__class__.__name__)
_log_api_usage_once(self)

if not hasattr(backbone, "out_channels"):
raise ValueError(
Expand Down
2 changes: 1 addition & 1 deletion torchvision/models/detection/ssd.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ def __init__(
positive_fraction: float = 0.25,
):
super().__init__()
_log_api_usage_once("models", self.__class__.__name__)
_log_api_usage_once(self)

self.backbone = backbone

Expand Down
2 changes: 1 addition & 1 deletion torchvision/models/detection/ssdlite.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def __init__(
min_depth: int = 16,
):
super().__init__()
_log_api_usage_once("models", self.__class__.__name__)
_log_api_usage_once(self)

assert not backbone[c4_pos].use_res_connect
self.features = nn.Sequential(
Expand Down
2 changes: 1 addition & 1 deletion torchvision/models/efficientnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ def __init__(
norm_layer (Optional[Callable[..., nn.Module]]): Module specifying the normalization layer to use
"""
super().__init__()
_log_api_usage_once("models", self.__class__.__name__)
_log_api_usage_once(self)

if not inverted_residual_setting:
raise ValueError("The inverted_residual_setting should not be empty")
Expand Down
2 changes: 1 addition & 1 deletion torchvision/models/googlenet.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def __init__(
dropout_aux: float = 0.7,
) -> None:
super().__init__()
_log_api_usage_once("models", self.__class__.__name__)
_log_api_usage_once(self)
if blocks is None:
blocks = [BasicConv2d, Inception, InceptionAux]
if init_weights is None:
Expand Down
2 changes: 1 addition & 1 deletion torchvision/models/inception.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def __init__(
dropout: float = 0.5,
) -> None:
super().__init__()
_log_api_usage_once("models", self.__class__.__name__)
_log_api_usage_once(self)
if inception_blocks is None:
inception_blocks = [BasicConv2d, InceptionA, InceptionB, InceptionC, InceptionD, InceptionE, InceptionAux]
if init_weights is None:
Expand Down
2 changes: 1 addition & 1 deletion torchvision/models/mnasnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class MNASNet(torch.nn.Module):

def __init__(self, alpha: float, num_classes: int = 1000, dropout: float = 0.2) -> None:
super().__init__()
_log_api_usage_once("models", self.__class__.__name__)
_log_api_usage_once(self)
assert alpha > 0.0
self.alpha = alpha
self.num_classes = num_classes
Expand Down
2 changes: 1 addition & 1 deletion torchvision/models/mobilenetv2.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def __init__(

"""
super().__init__()
_log_api_usage_once("models", self.__class__.__name__)
_log_api_usage_once(self)

if block is None:
block = InvertedResidual
Expand Down
2 changes: 1 addition & 1 deletion torchvision/models/mobilenetv3.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ def __init__(
dropout (float): The droupout probability
"""
super().__init__()
_log_api_usage_once("models", self.__class__.__name__)
_log_api_usage_once(self)

if not inverted_residual_setting:
raise ValueError("The inverted_residual_setting should not be empty")
Expand Down
2 changes: 1 addition & 1 deletion torchvision/models/optical_flow/raft.py
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ def __init__(self, *, feature_encoder, context_encoder, corr_block, update_block
If ``None`` (default), the flow is upsampled using interpolation.
"""
super().__init__()
_log_api_usage_once("models", self.__class__.__name__)
_log_api_usage_once(self)

self.feature_encoder = feature_encoder
self.context_encoder = context_encoder
Expand Down
2 changes: 1 addition & 1 deletion torchvision/models/regnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ def __init__(
activation: Optional[Callable[..., nn.Module]] = None,
) -> None:
super().__init__()
_log_api_usage_once("models", self.__class__.__name__)
_log_api_usage_once(self)

if stem_type is None:
stem_type = SimpleStemIN
Expand Down
2 changes: 1 addition & 1 deletion torchvision/models/resnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ def __init__(
norm_layer: Optional[Callable[..., nn.Module]] = None,
) -> None:
super().__init__()
_log_api_usage_once("models", self.__class__.__name__)
_log_api_usage_once(self)
if norm_layer is None:
norm_layer = nn.BatchNorm2d
self._norm_layer = norm_layer
Expand Down
2 changes: 1 addition & 1 deletion torchvision/models/segmentation/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class _SimpleSegmentationModel(nn.Module):

def __init__(self, backbone: nn.Module, classifier: nn.Module, aux_classifier: Optional[nn.Module] = None) -> None:
super().__init__()
_log_api_usage_once("models", self.__class__.__name__)
_log_api_usage_once(self)
self.backbone = backbone
self.classifier = classifier
self.aux_classifier = aux_classifier
Expand Down
2 changes: 1 addition & 1 deletion torchvision/models/segmentation/lraspp.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def __init__(
self, backbone: nn.Module, low_channels: int, high_channels: int, num_classes: int, inter_channels: int = 128
) -> None:
super().__init__()
_log_api_usage_once("models", self.__class__.__name__)
_log_api_usage_once(self)
self.backbone = backbone
self.classifier = LRASPPHead(low_channels, high_channels, num_classes, inter_channels)

Expand Down
2 changes: 1 addition & 1 deletion torchvision/models/shufflenetv2.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def __init__(
inverted_residual: Callable[..., nn.Module] = InvertedResidual,
) -> None:
super().__init__()
_log_api_usage_once("models", self.__class__.__name__)
_log_api_usage_once(self)

if len(stages_repeats) != 3:
raise ValueError("expected stages_repeats as list of 3 positive ints")
Expand Down
2 changes: 1 addition & 1 deletion torchvision/models/squeezenet.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def forward(self, x: torch.Tensor) -> torch.Tensor:
class SqueezeNet(nn.Module):
def __init__(self, version: str = "1_0", num_classes: int = 1000, dropout: float = 0.5) -> None:
super().__init__()
_log_api_usage_once("models", self.__class__.__name__)
_log_api_usage_once(self)
self.num_classes = num_classes
if version == "1_0":
self.features = nn.Sequential(
Expand Down
2 changes: 1 addition & 1 deletion torchvision/models/vgg.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def __init__(
self, features: nn.Module, num_classes: int = 1000, init_weights: bool = True, dropout: float = 0.5
) -> None:
super().__init__()
_log_api_usage_once("models", self.__class__.__name__)
_log_api_usage_once(self)
self.features = features
self.avgpool = nn.AdaptiveAvgPool2d((7, 7))
self.classifier = nn.Sequential(
Expand Down
2 changes: 1 addition & 1 deletion torchvision/models/video/resnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ def __init__(
zero_init_residual (bool, optional): Zero init bottleneck residual BN. Defaults to False.
"""
super().__init__()
_log_api_usage_once("models", self.__class__.__name__)
_log_api_usage_once(self)
self.inplanes = 64

self.stem = stem()
Expand Down
29 changes: 18 additions & 11 deletions torchvision/ops/boxes.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ def nms(boxes: Tensor, scores: Tensor, iou_threshold: float) -> Tensor:
Tensor: int64 tensor with the indices of the elements that have been kept
by NMS, sorted in decreasing order of scores
"""
_log_api_usage_once("ops", "nms")
if not torch.jit.is_scripting() and not torch.jit.is_tracing():
_log_api_usage_once(nms)
_assert_has_ops()
return torch.ops.torchvision.nms(boxes, scores, iou_threshold)

Expand Down Expand Up @@ -63,7 +64,8 @@ def batched_nms(
Tensor: int64 tensor with the indices of the elements that have been kept by NMS, sorted
in decreasing order of scores
"""
_log_api_usage_once("ops", "batched_nms")
if not torch.jit.is_scripting() and not torch.jit.is_tracing():
_log_api_usage_once(batched_nms)
# Benchmarks that drove the following thresholds are at
# https://github.com/pytorch/vision/issues/1311#issuecomment-781329339
if boxes.numel() > (4000 if boxes.device.type == "cpu" else 20000) and not torchvision._is_tracing():
Expand Down Expand Up @@ -122,7 +124,8 @@ def remove_small_boxes(boxes: Tensor, min_size: float) -> Tensor:
Tensor[K]: indices of the boxes that have both sides
larger than min_size
"""
_log_api_usage_once("ops", "remove_small_boxes")
if not torch.jit.is_scripting() and not torch.jit.is_tracing():
_log_api_usage_once(remove_small_boxes)
ws, hs = boxes[:, 2] - boxes[:, 0], boxes[:, 3] - boxes[:, 1]
keep = (ws >= min_size) & (hs >= min_size)
keep = torch.where(keep)[0]
Expand All @@ -141,7 +144,8 @@ def clip_boxes_to_image(boxes: Tensor, size: Tuple[int, int]) -> Tensor:
Returns:
Tensor[N, 4]: clipped boxes
"""
_log_api_usage_once("ops", "clip_boxes_to_image")
if not torch.jit.is_scripting() and not torch.jit.is_tracing():
_log_api_usage_once(clip_boxes_to_image)
dim = boxes.dim()
boxes_x = boxes[..., 0::2]
boxes_y = boxes[..., 1::2]
Expand Down Expand Up @@ -181,8 +185,8 @@ def box_convert(boxes: Tensor, in_fmt: str, out_fmt: str) -> Tensor:
Returns:
Tensor[N, 4]: Boxes into converted format.
"""

_log_api_usage_once("ops", "box_convert")
if not torch.jit.is_scripting() and not torch.jit.is_tracing():
_log_api_usage_once(box_convert)
allowed_fmts = ("xyxy", "xywh", "cxcywh")
if in_fmt not in allowed_fmts or out_fmt not in allowed_fmts:
raise ValueError("Unsupported Bounding Box Conversions for given in_fmt and out_fmt")
Expand Down Expand Up @@ -232,7 +236,8 @@ def box_area(boxes: Tensor) -> Tensor:
Returns:
Tensor[N]: the area for each box
"""
_log_api_usage_once("ops", "box_area")
if not torch.jit.is_scripting() and not torch.jit.is_tracing():
_log_api_usage_once(box_area)
boxes = _upcast(boxes)
return (boxes[:, 2] - boxes[:, 0]) * (boxes[:, 3] - boxes[:, 1])

Expand Down Expand Up @@ -268,7 +273,8 @@ def box_iou(boxes1: Tensor, boxes2: Tensor) -> Tensor:
Returns:
Tensor[N, M]: the NxM matrix containing the pairwise IoU values for every element in boxes1 and boxes2
"""
_log_api_usage_once("ops", "box_iou")
if not torch.jit.is_scripting() and not torch.jit.is_tracing():
_log_api_usage_once(box_iou)
inter, union = _box_inter_union(boxes1, boxes2)
iou = inter / union
return iou
Expand All @@ -290,8 +296,8 @@ def generalized_box_iou(boxes1: Tensor, boxes2: Tensor) -> Tensor:
Tensor[N, M]: the NxM matrix containing the pairwise generalized IoU values
for every element in boxes1 and boxes2
"""

_log_api_usage_once("ops", "generalized_box_iou")
if not torch.jit.is_scripting() and not torch.jit.is_tracing():
_log_api_usage_once(generalized_box_iou)
# degenerate boxes gives inf / nan results
# so do an early check
assert (boxes1[:, 2:] >= boxes1[:, :2]).all()
Expand Down Expand Up @@ -323,7 +329,8 @@ def masks_to_boxes(masks: torch.Tensor) -> torch.Tensor:
Returns:
Tensor[N, 4]: bounding boxes
"""
_log_api_usage_once("ops", "masks_to_boxes")
if not torch.jit.is_scripting() and not torch.jit.is_tracing():
_log_api_usage_once(masks_to_boxes)
if masks.numel() == 0:
return torch.zeros((0, 4), device=masks.device, dtype=torch.float)

Expand Down
5 changes: 3 additions & 2 deletions torchvision/ops/deform_conv.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ def deform_conv2d(
>>> # returns
>>> torch.Size([4, 5, 8, 8])
"""

_log_api_usage_once("ops", "deform_conv2d")
if not torch.jit.is_scripting() and not torch.jit.is_tracing():
_log_api_usage_once(deform_conv2d)
_assert_has_ops()
out_channels = weight.shape[0]

Expand Down Expand Up @@ -124,6 +124,7 @@ def __init__(
bias: bool = True,
):
super().__init__()
_log_api_usage_once(self)

if in_channels % groups != 0:
raise ValueError("in_channels must be divisible by groups")
Expand Down
2 changes: 1 addition & 1 deletion torchvision/ops/feature_pyramid_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def __init__(
extra_blocks: Optional[ExtraFPNBlock] = None,
):
super().__init__()
_log_api_usage_once("ops", self.__class__.__name__)
_log_api_usage_once(self)
self.inner_blocks = nn.ModuleList()
self.layer_blocks = nn.ModuleList()
for in_channels in in_channels_list:
Expand Down
3 changes: 2 additions & 1 deletion torchvision/ops/focal_loss.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ def sigmoid_focal_loss(
Returns:
Loss tensor with the reduction option applied.
"""
_log_api_usage_once("ops", "sigmoid_focal_loss")
if not torch.jit.is_scripting() and not torch.jit.is_tracing():
_log_api_usage_once(sigmoid_focal_loss)
p = torch.sigmoid(inputs)
ce_loss = F.binary_cross_entropy_with_logits(inputs, targets, reduction="none")
p_t = p * targets + (1 - p) * (1 - targets)
Expand Down
Loading