Skip to content

[WIP] Add remove-able warnings for Beta modules or functions #6173

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

Closed
wants to merge 4 commits into from
Closed
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
6 changes: 5 additions & 1 deletion test/test_backbone_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ def get_available_models():
return [
k
for k, v in models.__dict__.items()
if callable(v) and k[0].lower() == k[0] and k[0] != "_" and k != "get_weight"
if callable(v)
and k[0].lower() == k[0]
and k[0] != "_"
and k != "get_weight"
and not k.startswith("enable_beta")
]


Expand Down
6 changes: 5 additions & 1 deletion test/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ def get_models_from_module(module):
return [
v
for k, v in module.__dict__.items()
if callable(v) and k[0].lower() == k[0] and k[0] != "_" and k != "get_weight"
if callable(v)
and k[0].lower() == k[0]
and k[0] != "_"
and k != "get_weight"
and not k.startswith("enable_beta")
Comment on lines +29 to +33
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes here are irrelevant - this is specific to pass our tests

]


Expand Down
8 changes: 8 additions & 0 deletions torchvision/io/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@
from .video_reader import VideoReader


_BETA_VIDEO_API_IS_ENABLED = False


def enable_beta_video_api():
global _BETA_VIDEO_API_IS_ENABLED
_BETA_VIDEO_API_IS_ENABLED = True


__all__ = [
"write_video",
"read_video",
Expand Down
11 changes: 11 additions & 0 deletions torchvision/io/video_reader.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import warnings
from typing import Any, Dict, Iterator

import torch
Expand Down Expand Up @@ -92,6 +93,16 @@ class VideoReader:

def __init__(self, path: str, stream: str = "video", num_threads: int = 0, device: str = "cpu") -> None:
_log_api_usage_once(self)
from . import _BETA_VIDEO_API_IS_ENABLED # import here to avoid circular import

if not _BETA_VIDEO_API_IS_ENABLED:
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we mark the whole class as Beta. A warning is raised unless torchvision.io.enable_beta_video_api() is called first.

Functions could be treated similarly. We could have a more general decorator if needed.
This can also be used to mark single attributes or single parameters as Beta.

warnings.warn(
"The VideoReader class is still in Beta stage, which means "
"that backward compatibility isn't fully guaranteed. "
"Please visit <SOME_URL> to learn more about what we are planning to change in future versions. "
"To silence this warning, please call torchvision.io.enable_beta_video_api."
)

self.is_cuda = False
device = torch.device(device)
if device.type == "cuda":
Expand Down
18 changes: 17 additions & 1 deletion torchvision/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import importlib

from .alexnet import *
from .convnext import *
from .densenet import *
Expand All @@ -13,9 +15,23 @@
from .vgg import *
from .vision_transformer import *
from .swin_transformer import *
from . import detection
from . import optical_flow
from . import quantization
from . import segmentation
from . import video
from ._api import get_weight


def __getattr__(name):
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This adds the detection attribute lazily. If we imported it like the others (e.g. quantization above), users would get the "beta detection warning" even when just importing torchvision.models. See more details about __getattr__ at https://peps.python.org/pep-0562/

Note: the detection attribute is needed when doing e.g.

import torchvision.models as M
M.detection  # <- directly accessing the attribute

but in a lot of patterns this gets bypassed, typically when doing

from torchvision.models.detection import ssd

or even just

import torchvision.models.detection

if name == "detection":
return importlib.import_module("." + name, __name__)
else:
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")


_BETA_DETECTION_IS_ENABLED = False


def enable_beta_detection():
global _BETA_DETECTION_IS_ENABLED
_BETA_DETECTION_IS_ENABLED = True
12 changes: 12 additions & 0 deletions torchvision/models/detection/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,15 @@
from .retinanet import *
from .ssd import *
from .ssdlite import *

import warnings

from .. import _BETA_DETECTION_IS_ENABLED

if not _BETA_DETECTION_IS_ENABLED:
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we mark the entire torchvision.models.detection as Beta and accessing anything within its namespace will throw a warning unless torchvision.models.enable_beta_detection() is called.

warnings.warn(
"The torchvision.models.detection module is still in Beta stage, which means "
"that backward compatibility isn't fully guaranteed. "
"Please visit <SOME_URL> to learn more about what we are planning to change in future versions. "
"To silence this warning, please call torchvision.models.enable_beta_detection."
)