Skip to content

Commit 56f685f

Browse files
larryliu0820facebook-github-bot
authored andcommitted
Add proper error message when shared library is missing
Summary: Make sure the user has good enough error message, when they don't pass in the shared library for registering out operators. Reviewed By: guangy10 Differential Revision: D48664907 fbshipit-source-id: 70e2019904de9075c98465adfd5411faf5092d35
1 parent 02fbd74 commit 56f685f

File tree

4 files changed

+47
-6
lines changed

4 files changed

+47
-6
lines changed

examples/README.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,17 @@ Here is the [Quantization Flow Docs](/docs/website/docs/tutorials/quantization_f
6060

6161
You can run quantization test with the following command:
6262
```bash
63-
python3 -m examples.quantization.example --model_name "mv2" # for MobileNetv2
63+
python3 -m examples.quantization.example --model_name "mv2" --so-library "<path/to/so/lib>" # for MobileNetv2
6464
```
65-
It will print both the original model after capture and quantized model.
65+
66+
Note that the shared library being passed into `example.py` is required to register the out variants of the quantized operators (e.g., `quantized_decomposed::add.out`)into EXIR. To build this library, run the following command if using buck2:
67+
```bash
68+
buck2 build //kernels/quantized:aot_lib --show-output
69+
```
70+
71+
If on cmake, follow the instructions in `test_quantize.sh` to build it, the default path is `cmake-out/kernels/quantized/libquantized_ops_lib.so`.
72+
73+
This command will print both the original model after capture and quantized model.
6674

6775
The flow produces a quantized model that could be lowered through partitioner or the runtime directly.
6876

examples/custom_ops/custom_ops_2.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,24 @@ def main():
3737
help="Provide path to so library. E.g., cmake-out/examples/custom_ops/libcustom_ops_aot_lib.so",
3838
)
3939
args = parser.parse_args()
40-
torch.ops.load_library(args.so_library)
40+
# See if we have custom op my_ops::mul4.out registered
41+
has_out_ops = True
42+
try:
43+
op = torch.ops.my_ops.mul4.out
44+
except AttributeError:
45+
print("No registered custom op my_ops::mul4.out")
46+
has_out_ops = False
47+
if not has_out_ops:
48+
if args.so_library:
49+
torch.ops.load_library(args.so_library)
50+
else:
51+
raise RuntimeError(
52+
"Need to specify shared library path to register custom op my_ops::mul4.out into"
53+
"EXIR. The required shared library is defined as `custom_ops_aot_lib` in "
54+
"examples/custom_ops/CMakeLists.txt if you are using CMake build, or `custom_ops_aot_lib_2` in "
55+
"examples/custom_ops/targets.bzl for buck2. One example path would be cmake-out/examples/custom_ops/"
56+
"libcustom_ops_aot_lib.[so|dylib]."
57+
)
4158
print(args.so_library)
4259

4360
main()

examples/quantization/example.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,24 @@ def verify_xnnpack_quantizer_matching_fx_quant_model(model_name, model, example_
9999
)
100100

101101
args = parser.parse_args()
102-
if args.so_library:
103-
torch.ops.load_library(args.so_library)
102+
# See if we have quantized op out variants registered
103+
has_out_ops = True
104+
try:
105+
op = torch.ops.quantized_decomposed.add.out
106+
except AttributeError:
107+
print("No registered quantized ops")
108+
has_out_ops = False
109+
if not has_out_ops:
110+
if args.so_library:
111+
torch.ops.load_library(args.so_library)
112+
else:
113+
raise RuntimeError(
114+
"Need to specify shared library path to register quantized ops (and their out variants) into"
115+
"EXIR. The required shared library is defined as `quantized_ops_aot_lib` in "
116+
"kernels/quantized/CMakeLists.txt if you are using CMake build, or `aot_lib` in "
117+
"kernels/quantized/targets.bzl for buck2. One example path would be cmake-out/kernels/quantized/"
118+
"libquantized_ops_aot_lib.[so|dylib]."
119+
)
104120
if not args.verify and args.model_name not in MODEL_NAME_TO_OPTIONS:
105121
raise RuntimeError(
106122
f"Model {args.model_name} is not a valid name. or not quantizable right now, "

examples/quantization/test_quantize.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
# Test the end-to-end quantization flow.
99

10-
set -eu
10+
set -e
1111

1212
get_shared_lib_ext() {
1313
UNAME=$(uname)

0 commit comments

Comments
 (0)