Skip to content

Commit c8d1802

Browse files
q10facebook-github-bot
authored andcommitted
Add version compatibility checks on library load (#4875)
Summary: X-link: facebookresearch/FBGEMM#1900 - Add version compatibility checks on library load - This is simliar to the code changes made in https://github.com/pytorch/ao/blob/main/torchao/__init__.py#L30, to help diagnose crashes that occur bc the library is loading against a version of torch that is older or newer than the version it was built against Pull Request resolved: #4875 Reviewed By: ionuthristodorescu Differential Revision: D82480734 Pulled By: q10 fbshipit-source-id: f37ee0c2a10d23ac86832025936a192f13e647cf
1 parent 8c36bca commit c8d1802

File tree

1 file changed

+71
-2
lines changed

1 file changed

+71
-2
lines changed

fbgemm_gpu/fbgemm_gpu/__init__.py

Lines changed: 71 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,84 @@
77

88
import logging
99
import os
10+
import re
1011

1112
import torch
1213

14+
# Based on the FBGEMM-PyTorch compatibility table at
15+
# https://docs.pytorch.org/FBGEMM/general/Releases.html#fbgemm-releases-compatibility
16+
_fbgemm_torch_compat_table = {
17+
"1.3": "2.8",
18+
"1.2": "2.7",
19+
"1.1": "2.6",
20+
"1.0": "2.5",
21+
"0.8": "2.4",
22+
"0.7": "2.3",
23+
"0.6": "2.2",
24+
"0.5": "2.1",
25+
"0.4": "2.0",
26+
}
27+
1328

14-
def _load_library(filename: str, no_throw: bool = False) -> None:
29+
def _load_library(filename: str, version: str, no_throw: bool = False) -> None:
1530
"""Load a shared library from the given filename."""
31+
32+
# Check if the version of PyTorch is compatible with the version of FBGEMM
33+
# that we are trying to load, and print a loud warning if not. This is
34+
# useful for the OSS build, where we have a single FBGEMM library that is
35+
# compatible with multiple versions of PyTorch.
36+
#
37+
# Based on: https://github.com/pytorch/ao/blob/main/torchao/__init__.py#L30
38+
39+
keys = [
40+
key
41+
for key in _fbgemm_torch_compat_table.keys()
42+
if version.startswith(f"{key}.")
43+
]
44+
45+
if version == "INTERNAL" or "+git" in version:
46+
# if FBGEMM version has "+git", assume it's locally built and we don't know
47+
# anything about the PyTorch version used to build it
48+
logging.info(
49+
"FBGEMM version is INTERNAL or local, ignoring version compatibility check with PyTorch"
50+
)
51+
52+
elif re.match(r"^\d{4}\.\d{1,2}\.\d{1,2}.*$", version):
53+
# if FBGEMM version is a date, assume it's a nightly build and that we
54+
# know what we're doing
55+
logging.info(
56+
"FBGEMM version is a nightly version, ignoring version compatibility check with PyTorch"
57+
)
58+
59+
elif not keys:
60+
logging.warning(
61+
f"""
62+
\033[33m
63+
_fbgemm_torch_compat_table has no entry for {version} of FBGEMM;
64+
cannot determine compatibility with PyTorch {torch.__version__}
65+
\033[0m
66+
"""
67+
)
68+
69+
elif str(torch.__version__) != _fbgemm_torch_compat_table[keys[0]]:
70+
logging.warning(
71+
f"""
72+
\033[31m
73+
FBGEMM_GPU version is {version}, which is not guaranteed to be
74+
compatible with PyTorch {torch.__version__}; library loading might
75+
crash!
76+
77+
Please refer to
78+
https://docs.pytorch.org/FBGEMM/general/Releases.html#fbgemm-releases-compatibility
79+
for the FBGEMM-PyTorch compatibility table.
80+
\033[0m
81+
"""
82+
)
83+
1684
try:
1785
torch.ops.load_library(os.path.join(os.path.dirname(__file__), filename))
1886
logging.info(f"Successfully loaded: '{filename}'")
87+
1988
except Exception as error:
2089
logging.error(f"Could not load the library '{filename}'!\n\n\n{error}\n\n\n")
2190
if not no_throw:
@@ -87,7 +156,7 @@ def _load_library(filename: str, no_throw: bool = False) -> None:
87156
#
88157
# https://github.com/pytorch/FBGEMM/pull/3477
89158
# https://github.com/pytorch/FBGEMM/pull/3717
90-
_load_library(f"{library}.so", __variant__ == "docs")
159+
_load_library(f"{library}.so", __version__, __variant__ == "docs")
91160

92161
try:
93162
# Trigger meta operator registrations

0 commit comments

Comments
 (0)