Skip to content

Commit 32e164a

Browse files
Arm backend: Avoid requantization around fused flow sampling (pytorch#21785)
Use a consistent SNORM-safe activation range for VGF PTQ and QAT models that use fused flow-offset grid sampling. The pattern-specific VGF config gives fused samplers observed [-127, 127] qparams. Surrounding operators otherwise use the default [-128, 127] range, which creates DQ-to-Q requantization boundaries before and after each sampler: ReLU [-128, 127] -> DQ -> Q [-127, 127] -> fused grid sampler -> DQ -> Q [-128, 127] -> ReLU Add get_vgf_snorm_quantization_config() and use [-127, 127] for all activations in this flow. Reuse qspec objects that already use the SNORM-safe range. PT2E can then keep the producer, sampler, and consumer edges in one quantization domain: ReLU [-127, 127] -> fused grid sampler [-127, 127] -> ReLU [-127, 127] For the RIFE QAT export, this removes 22 DQ/Q boundary pairs. The TOSA graph has 22 fewer RESCALE and 88 fewer CONST ops. Model conversion also removes four RESCALE-only VGF GRAPH segments. All 18 custom-shader COMPUTE segments remain. This range also avoids the ambiguous -128 SNORM value. Vulkan signed normalized 8-bit images map both -128 and -127 to -1.0. Update the VGF QAT example and documentation, and add a converted-graph regression test covering the sampler chain. Authored with Codex. Change-Id: I570b598de00d5893b5494377033ebdec8491e94f Signed-off-by: Yufeng Shi <yufeng.shi@arm.com>
1 parent 3b5bf0f commit 32e164a

7 files changed

Lines changed: 142 additions & 2 deletions

File tree

‎backends/arm/quantizer/__init__.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
get_symmetric_a16w8_quantization_config,
1717
get_symmetric_quantization_config,
1818
get_uint8_io_quantization_config,
19+
get_vgf_snorm_quantization_config,
1920
TOSAQuantizer,
2021
VgfQuantizer,
2122
)

‎backends/arm/quantizer/arm_quantizer.py‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@
108108
"get_symmetric_a16w8_quantization_config",
109109
"get_symmetric_quantization_config",
110110
"get_uint8_io_quantization_config",
111+
"get_vgf_snorm_quantization_config",
111112
]
112113

113114
logger = logging.getLogger(__name__)
@@ -314,6 +315,48 @@ def get_symmetric_quantization_config(
314315
)
315316

316317

318+
@functools.lru_cache
319+
def get_vgf_snorm_quantization_config(
320+
is_per_channel: bool = True,
321+
is_qat: bool = False,
322+
weight_qmin: int = -127,
323+
weight_qmax: int = 127,
324+
eps: float = 2**-16,
325+
) -> VGFQuantizationConfig:
326+
"""Create a VGF config compatible with signed normalized sampled images.
327+
328+
Args:
329+
is_per_channel (bool): Whether to use per-channel quantization for
330+
weights.
331+
is_qat (bool): Whether the configuration targets quantization aware
332+
training.
333+
weight_qmin (int): Minimum weight quantization value.
334+
weight_qmax (int): Maximum weight quantization value.
335+
eps (float): Minimum scale value used by observers.
336+
337+
Returns:
338+
VGFQuantizationConfig: VGF quantization settings using the SNORM-safe
339+
activation range ``[-127, 127]``.
340+
341+
"""
342+
config = get_symmetric_quantization_config(
343+
is_per_channel=is_per_channel,
344+
is_qat=is_qat,
345+
act_qmin=-127,
346+
act_qmax=127,
347+
weight_qmin=weight_qmin,
348+
weight_qmax=weight_qmax,
349+
eps=eps,
350+
)
351+
return VGFQuantizationConfig(
352+
config.input_activation,
353+
config.output_activation,
354+
config.weight,
355+
config.bias,
356+
config.label,
357+
)
358+
359+
317360
@functools.lru_cache
318361
def get_uint8_io_quantization_config(
319362
is_qat: bool = False,

‎backends/arm/quantizer/quantization_config.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,8 @@ def _snorm_compatible_qspec(
548548
return None
549549
if not isinstance(qspec, QuantizationSpec):
550550
raise ValueError("SNORM-compatible qparams require a QuantizationSpec.")
551+
if qspec.quant_min == -127 and qspec.quant_max == 127:
552+
return qspec
551553
return replace(qspec, quant_min=-127, quant_max=127)
552554

553555
def get_input_act_qspec(self, node=None, input_node=None):
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# Copyright 2026 Arm Limited and/or its affiliates.
2+
#
3+
# This source code is licensed under the BSD-style license found in the
4+
# LICENSE file in the root directory of this source tree.
5+
6+
import torch
7+
import torch.nn.functional as F
8+
from executorch.backends.arm.quantizer import (
9+
get_vgf_snorm_quantization_config,
10+
VgfQuantizer,
11+
)
12+
from executorch.backends.arm.quantizer.quantization_config import (
13+
_is_canonical_flow_offset_grid_sampler,
14+
VGFQuantizationConfig,
15+
)
16+
from executorch.backends.arm.vgf import VgfCompileSpec
17+
from torch.export import export
18+
from torchao.quantization.pt2e.quantize_pt2e import convert_pt2e, prepare_pt2e
19+
from torchao.quantization.pt2e.quantizer import QuantizationSpec
20+
21+
22+
class FlowOffsetSamplerChain(torch.nn.Module):
23+
def forward(self, image: torch.Tensor, flow: torch.Tensor) -> torch.Tensor:
24+
image = torch.relu(image)
25+
height, width = image.shape[-2:]
26+
horizontal = torch.linspace(
27+
-1.0, 1.0, width, device=image.device, dtype=image.dtype
28+
).view(1, 1, 1, width)
29+
vertical = torch.linspace(
30+
-1.0, 1.0, height, device=image.device, dtype=image.dtype
31+
).view(1, 1, height, 1)
32+
base_grid = torch.cat(
33+
(
34+
horizontal.expand(1, -1, height, -1),
35+
vertical.expand(1, -1, -1, width),
36+
),
37+
dim=1,
38+
)
39+
grid = (base_grid + flow[:, :2]).permute(0, 2, 3, 1)
40+
sampled = F.grid_sample(
41+
image,
42+
grid,
43+
mode="bilinear",
44+
padding_mode="border",
45+
align_corners=True,
46+
)
47+
return torch.relu(sampled)
48+
49+
50+
def test_vgf_snorm_quantization_avoids_requantization_boundaries():
51+
inputs = (
52+
torch.randn(1, 4, 8, 12),
53+
torch.randn(1, 4, 8, 12) * 0.01,
54+
)
55+
exported = export(FlowOffsetSamplerChain(), inputs, strict=True).module()
56+
grid_sampler = next(
57+
node
58+
for node in exported.graph.nodes
59+
if node.target == torch.ops.aten.grid_sampler.default
60+
)
61+
assert _is_canonical_flow_offset_grid_sampler(grid_sampler)
62+
63+
config = get_vgf_snorm_quantization_config(is_per_channel=False)
64+
assert isinstance(config, VGFQuantizationConfig)
65+
for qspec in (config.input_activation, config.output_activation):
66+
assert isinstance(qspec, QuantizationSpec)
67+
assert (qspec.quant_min, qspec.quant_max) == (-127, 127)
68+
69+
quantizer = VgfQuantizer(
70+
VgfCompileSpec()._set_preserve_io_quantization(True),
71+
use_composable_quantizer=True,
72+
)
73+
quantizer.set_global(config)
74+
quantizer.set_io(config)
75+
prepared = prepare_pt2e(exported, quantizer)
76+
prepared(*inputs)
77+
converted = convert_pt2e(prepared)
78+
79+
quantize = torch.ops.quantized_decomposed.quantize_per_tensor.default
80+
dequantize = torch.ops.quantized_decomposed.dequantize_per_tensor.default
81+
requantize_nodes = [
82+
node
83+
for node in converted.graph.nodes
84+
if node.target == quantize
85+
and isinstance(node.args[0], torch.fx.Node)
86+
and node.args[0].target == dequantize
87+
]
88+
assert not requantize_nodes

‎backends/arm/test/targets.bzl‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ def define_arm_tests():
5252
test_files += [
5353
"quantizer/test_generic_annotater.py",
5454
"quantizer/test_uint8_io_quantization.py",
55+
"quantizer/test_vgf_snorm_quantization.py",
5556
]
5657

5758
# Misc tests

‎examples/arm/QAT_example/README.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ The output directory contains:
5252
- `metrics.csv`: tabular per-sample metrics.
5353
- `quantization_coverage/`: PTQ and QAT graph coverage details.
5454

55+
The flow uses the VGF SNORM-safe activation range `[-127, 127]`. Keeping this
56+
range consistent across the graph avoids requantization boundaries around the
57+
fused flow-offset samplers while ensuring sampled images never use the
58+
ambiguous SNORM value `-128`.
59+
5560
## Smoke Runs
5661

5762
Small random or short QAT runs are useful only for checking that the flow runs.

‎examples/arm/QAT_example/qat_loop.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@
5151
sys.path.insert(0, str(EXECUTORCH_ROOT))
5252

5353
from executorch.backends.arm.quantizer import ( # noqa: E402
54-
get_symmetric_quantization_config,
5554
get_uint8_io_quantization_config,
55+
get_vgf_snorm_quantization_config,
5656
VgfQuantizer,
5757
)
5858
from executorch.backends.arm.vgf import VgfCompileSpec, VgfPartitioner # noqa: E402
@@ -663,7 +663,7 @@ def make_quantizer(
663663
VgfCompileSpec, VgfCompileSpec()._set_preserve_io_quantization(True)
664664
)
665665
quantizer = VgfQuantizer(compile_spec, use_composable_quantizer=True)
666-
global_config = get_symmetric_quantization_config(is_qat=is_qat)
666+
global_config = get_vgf_snorm_quantization_config(is_qat=is_qat)
667667
quantizer.set_global(global_config)
668668

669669
if io_quantization == "int8":

0 commit comments

Comments
 (0)