Skip to content
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

fx quant: hook up ConvTranspose{n}d #49717

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
35 changes: 35 additions & 0 deletions test/quantization/test_quantize_fx.py
Expand Up @@ -78,6 +78,7 @@
import operator
import unittest
import io
from typing import Callable

class TestFuseFx(QuantizationTestCase):
def test_fuse_conv_bn_relu(self):
Expand Down Expand Up @@ -2361,6 +2362,40 @@ def test_rnn(self):
[100, -155]], dtype=torch.float).unsqueeze(0).repeat(niter, 1, 1)
self._test_rnn_impl(qconfigs, RNNDynamicModel, module_type_strs, module_types, sample_input)

def _test_conv_transpose_impl(
Copy link
Contributor

Choose a reason for hiding this comment

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

I see, we should probably add eager mode numerics check to checkGraphModeFxOp

self, float_cls: Callable, q_cls: Callable, data: torch.Tensor):
if 'qnnpack' not in torch.backends.quantized.supported_engines:
return
torch.backends.quantized.engine = 'qnnpack'
# Create fp32 versions of FX and Eager models
m1 = torch.nn.Sequential(float_cls(1, 1, 1))
m2 = torch.nn.Sequential(float_cls(1, 1, 1))
m2.load_state_dict(m1.state_dict())
m2 = torch.quantization.QuantWrapper(m2)
# FX graph
q_result1 = self.checkGraphModeFxOp(
m1, (data,), QuantType.STATIC,
expected_node_occurrence={
ns.call_module(q_cls): 1,
})
# Eager
m2.qconfig = get_default_qconfig(torch.backends.quantized.engine)
m2.eval()
m2p = torch.quantization.prepare(m2)
m2p(data)
m2q = torch.quantization.convert(m2p)
q_result2 = m2q(data)
# verify results match
self.assertTrue(torch.allclose(q_result1, q_result2))

def test_conv_transpose_1d(self):
self._test_conv_transpose_impl(
torch.nn.ConvTranspose1d, nnq.ConvTranspose1d, torch.randn(4, 1, 4))

def test_conv_transpose_2d(self):
self._test_conv_transpose_impl(
torch.nn.ConvTranspose2d, nnq.ConvTranspose2d, torch.randn(4, 1, 4, 4))


class TestQuantizeFxModels(QuantizationTestCase):
def _test_model_impl(
Expand Down
2 changes: 2 additions & 0 deletions torch/quantization/fx/quantization_patterns.py
Expand Up @@ -537,6 +537,8 @@ def convert(self, quantizer: QuantizerCls, node: Node, load_arg: Callable,
torch._ops.ops.quantized.instance_norm:
['running_mean', 'running_var', 'use_input_stats', 'momentum'],
}
@register_quant_pattern(torch.nn.ConvTranspose1d)
@register_quant_pattern(torch.nn.ConvTranspose2d)
@register_quant_pattern(torch.nn.ELU)
@register_quant_pattern(torch.nn.LeakyReLU)
@register_quant_pattern(torch.nn.Hardswish)
Expand Down