mirror of
https://github.com/saymrwulf/pytorch.git
synced 2026-05-15 21:00:47 +00:00
Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/60476 # Context Add tests for Lite modules that are quantized using fx API Read this posts for details about why we need a test bench for quantized lite modules https://fb.workplace.com/groups/2322282031156145/permalink/4289792691071726/ https://github.com/pytorch/pytorch/pull/60226#discussion_r654615851 moved common code to `caffe2/torch/testing/_internal/common_quantization.py` ghstack-source-id: 133144292 Test Plan: ``` ~/fbsource/fbcode] buck test caffe2/test:fx_quantization_lite Downloaded 0/2 artifacts, 0.00 bytes, 100.0% cache miss Building: finished in 8.3 sec (100%) 11892/11892 jobs, 2 updated Total time: 8.6 sec More details at https://www.internalfb.com/intern/buck/build/ffb7d517-d85e-4c8f-9531-5e5d9ca1d34c Tpx test run coordinator for Facebook. See https://fburl.com/tpx for details. Running with tpx session id: d79a5713-bd29-4bbf-ae76-33a413869a09 Trace available for this run at /tmp/tpx-20210630-105547.675980/trace.log Started reporting to test run: https://www.internalfb.com/intern/testinfra/testrun/3096224749578707 ✓ ListingSuccess: caffe2/test:fx_quantization_lite - main (9.423) ✓ Pass: caffe2/test:fx_quantization_lite - test_embedding (mobile.test_quantize_fx_lite_script_module.TestFuseFx) (10.630) ✓ Pass: caffe2/test:fx_quantization_lite - test_submodule (mobile.test_quantize_fx_lite_script_module.TestFuseFx) (12.464) ✓ Pass: caffe2/test:fx_quantization_lite - test_conv2d (mobile.test_quantize_fx_lite_script_module.TestFuseFx) (12.728) Summary Pass: 3 ListingSuccess: 1 If you need help understanding your runs, please follow the wiki: https://fburl.com/posting_in_tpx_users Finished test run: https://www.internalfb.com/intern/testinfra/testrun/3096224749578707 ``` Reviewed By: iseeyuan Differential Revision: D29306402 fbshipit-source-id: aa481e0f696b7e9b04b9dcc6516e8a390f7dc1be
93 lines
2.8 KiB
Python
93 lines
2.8 KiB
Python
import torch
|
|
import torch.nn as nn
|
|
import torch.nn.quantized as nnq
|
|
import torch.utils.bundled_inputs
|
|
from torch.quantization import (
|
|
default_qconfig,
|
|
float_qparams_weight_only_qconfig,
|
|
)
|
|
|
|
# graph mode quantization based on fx
|
|
from torch.quantization.quantize_fx import (
|
|
prepare_fx,
|
|
convert_fx,
|
|
)
|
|
from torch.testing._internal.common_quantization import NodeSpec as ns
|
|
from torch.testing._internal.common_quantization import (
|
|
QuantizationLiteTestCase,
|
|
LinearModelWithSubmodule,
|
|
)
|
|
|
|
|
|
class TestLiteFuseFx(QuantizationLiteTestCase):
|
|
|
|
# Tests from:
|
|
# ./caffe2/test/quantization/fx/test_quantize_fx.py
|
|
|
|
def test_embedding(self):
|
|
class M(torch.nn.Module):
|
|
def __init__(self):
|
|
super().__init__()
|
|
self.emb = torch.nn.Embedding(num_embeddings=10, embedding_dim=12)
|
|
|
|
def forward(self, indices):
|
|
return self.emb(indices)
|
|
|
|
model = M().eval()
|
|
indices = torch.randint(low=0, high=10, size=(20,))
|
|
|
|
quantized_node = ns.call_module(nnq.Embedding)
|
|
configs = [
|
|
(float_qparams_weight_only_qconfig, ns.call_module(nnq.Embedding)),
|
|
(None, ns.call_module(nn.Embedding)),
|
|
(default_qconfig, ns.call_module(nn.Embedding)),
|
|
]
|
|
|
|
for qconfig, node in configs:
|
|
qconfig_dict = {"": qconfig}
|
|
m = prepare_fx(model, qconfig_dict)
|
|
m = convert_fx(m)
|
|
self._compare_script_and_mobile(m, input=indices)
|
|
|
|
def test_conv2d(self):
|
|
class M(torch.nn.Module):
|
|
def __init__(self):
|
|
super(M, self).__init__()
|
|
self.conv1 = nn.Conv2d(1, 1, 1)
|
|
self.conv2 = nn.Conv2d(1, 1, 1)
|
|
|
|
def forward(self, x):
|
|
x = self.conv1(x)
|
|
x = self.conv2(x)
|
|
return x
|
|
|
|
m = M().eval()
|
|
qconfig_dict = {"": default_qconfig, "module_name": [("conv1", None)]}
|
|
m = prepare_fx(m, qconfig_dict)
|
|
data = torch.randn(1, 1, 1, 1)
|
|
m = convert_fx(m)
|
|
# first conv is quantized, second conv is not quantized
|
|
self._compare_script_and_mobile(m, input=data)
|
|
|
|
def test_submodule(self):
|
|
# test quantizing complete module, submodule and linear layer
|
|
configs = [
|
|
{},
|
|
{"module_name": [("subm", None)]},
|
|
{"module_name": [("fc", None)]},
|
|
]
|
|
for config in configs:
|
|
model = LinearModelWithSubmodule().eval()
|
|
qconfig_dict = {
|
|
"": torch.quantization.get_default_qconfig("qnnpack"),
|
|
**config,
|
|
}
|
|
model = prepare_fx(model, qconfig_dict)
|
|
quant = convert_fx(model)
|
|
|
|
x = torch.randn(5, 5)
|
|
self._compare_script_and_mobile(quant, input=x)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
run_tests()
|