Skip to content

Commit 66b2834

Browse files
kyunggeunleeGitHub Enterprise
authored andcommitted
Stop incorrect encoding propagation through non-grid-preserving ops
Signed-off-by: Kyunggeun Lee <kyunggeu@qti.qualcomm.com>
1 parent ca7d3e0 commit 66b2834

2 files changed

Lines changed: 32 additions & 2 deletions

File tree

TrainingExtensions/torch/src/python/aimet_torch/experimental/quantsim_utils.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,10 @@ def _set_src_qtzr(x: Product, consumer: Op, qtzr: AffineQuantizerBase):
125125
if i < len(qmodule.output_quantizers) and qmodule.output_quantizers[i]:
126126
qmodule.output_quantizers[i] = qtzr
127127

128-
if not qmodule or _is_math_invariant_op(qmodule):
129-
# 1. There is no qmodule associated with the graph node ``producer``, or
128+
if (not qmodule and producer.is_grid_preserving_op()) or _is_math_invariant_op(
129+
qmodule
130+
):
131+
# 1. There is no qmodule associated with the grid-preserving ``producer``, or
130132
# 2. qmodule is a math invariant op (reshape, permute, etc).
131133
# In these cases, propagate encoding further to the ancestors
132134
for input in producer.inputs:

TrainingExtensions/torch/test/python/v2/quantsim/test_quantsim.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1500,6 +1500,34 @@ def test_htp_interpolation_tie_encodings(self, module_factory, input_factory):
15001500
out_enc.pop("name")
15011501
assert inp_enc == out_enc
15021502

1503+
def test_tie_encodings_functional_add(self):
1504+
"""
1505+
Given: Functional operator between Conv and Relu
1506+
When: Create quantsim with HTP V81 config
1507+
Then: Conv and Relu output quantizers should NOT be shared
1508+
"""
1509+
1510+
class Model(torch.nn.Module):
1511+
def __init__(self):
1512+
super().__init__()
1513+
self.conv1 = torch.nn.Conv2d(3, 3, 3)
1514+
self.conv2 = torch.nn.Conv2d(3, 3, 3)
1515+
self.relu = torch.nn.ReLU()
1516+
1517+
def forward(self, x):
1518+
x1 = self.conv1(x)
1519+
x2 = self.conv2(x)
1520+
return self.relu(x1 + x2)
1521+
1522+
model = Model()
1523+
x = torch.randn(1, 3, 10, 10)
1524+
sim = QuantizationSimModel(model, x, config_file="htp_v81")
1525+
1526+
# There should be no shared quantizers
1527+
assert list(sim.model.modules(remove_duplicate=True)) == list(
1528+
sim.model.modules(remove_duplicate=False)
1529+
)
1530+
15031531
def test_conv_relu_supergroup(self, tmp_path: pathlib.Path):
15041532
"""
15051533
When: Create quantsim with HTP V69 config or lower

0 commit comments

Comments
 (0)