Skip to content

Commit 4a29383

Browse files
YufengShi-duduper
andauthored
Arm backend: Preserve qparams for fused flow-offset sampling (pytorch#21777)
Use observed activation qparams for the image and output of the exact flow-offset pattern handled by the fused VGF shader. This preserves activation precision instead of forcing fixed grid-sampler qparams, reducing clipping and wasted int8 resolution. Keep image and output ranges at [-127, 127] because Vulkan SNORM cannot distinguish -128 from -127. Keep flow at the full int8 range because the shader reads it directly as int8_t. Align quantizer pattern matching with the fusion contract, including sampling modes, graph structure, shapes, slice semantics, and supported dtypes. Leave unsupported variants on the standard grid-sampler path. Avoid unnecessary RESCALE operations and their additional rounding error between paired warps. Cover supported patterns, malformed shapes, near misses, fixed-qparams fallback, and A16W8 fallback. Authored with Codex. Change-Id: Icd9bc31e21c8f90fc1b7fa7265b532ff03a393b9 Signed-off-by: Yufeng Shi <yufeng.shi@arm.com> Co-authored-by: Per Åstrand <per.astrand@arm.com>
1 parent 8c05cf8 commit 4a29383

2 files changed

Lines changed: 349 additions & 4 deletions

File tree

‎backends/arm/quantizer/quantization_config.py‎

Lines changed: 169 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@
1111
1212
"""
1313

14-
15-
from dataclasses import dataclass
14+
from dataclasses import dataclass, replace
1615
from typing import Any, Callable, cast, Optional
1716

1817
import torch
@@ -28,6 +27,124 @@
2827
)
2928

3029

30+
def _is_canonical_flow_offset_grid_sampler(node: Node) -> bool: # noqa: C901
31+
if node.target != torch.ops.aten.grid_sampler.default or len(node.args) < 5:
32+
return False
33+
if tuple(node.args[2:5]) != (0, 1, True):
34+
return False
35+
image = node.args[0]
36+
if not isinstance(image, Node):
37+
return False
38+
image_value = image.meta.get("val")
39+
output_value = node.meta.get("val")
40+
if (
41+
not isinstance(image_value, torch.Tensor)
42+
or len(image_value.shape) != 4
43+
or int(image_value.shape[0]) != 1
44+
or int(image_value.shape[1]) not in (3, 4)
45+
or not isinstance(output_value, torch.Tensor)
46+
or tuple(output_value.shape) != tuple(image_value.shape)
47+
):
48+
return False
49+
grid = node.args[1]
50+
if not isinstance(grid, Node) or grid.target != torch.ops.aten.permute.default:
51+
return False
52+
permutation = grid.args[1]
53+
if not isinstance(permutation, (list, tuple)) or list(permutation) != [0, 2, 3, 1]:
54+
return False
55+
add = grid.args[0]
56+
if not isinstance(add, Node) or add.target != torch.ops.aten.add.Tensor:
57+
return False
58+
if len(add.args) > 2 and add.args[2] != 1:
59+
return False
60+
if add.kwargs.get("alpha", 1) != 1:
61+
return False
62+
for base_grid, flow_slice in (
63+
(add.args[0], add.args[1]),
64+
(add.args[1], add.args[0]),
65+
):
66+
if not isinstance(flow_slice, Node) or flow_slice.target not in (
67+
torch.ops.aten.slice.Tensor,
68+
torch.ops.aten.slice_copy.Tensor,
69+
):
70+
continue
71+
flow, dim, start, end, *step = flow_slice.args
72+
if dim != 1 or (start, end) not in ((0, 2), (2, 4)):
73+
continue
74+
if (
75+
step not in ([], [1])
76+
or flow_slice.kwargs.get("step", 1) != 1
77+
or not isinstance(flow, Node)
78+
):
79+
continue
80+
flow_value = flow.meta.get("val")
81+
while isinstance(base_grid, Node) and base_grid.target in (
82+
torch.ops.aten.to.dtype,
83+
torch.ops.aten.to.dtype_layout,
84+
torch.ops.aten.alias.default,
85+
):
86+
base_grid = base_grid.args[0]
87+
if not isinstance(base_grid, Node):
88+
continue
89+
base_value = base_grid.meta.get("val")
90+
if (
91+
not isinstance(flow_value, torch.Tensor)
92+
or len(flow_value.shape) != 4
93+
or tuple(flow_value.shape[:2]) != (1, 4)
94+
):
95+
continue
96+
height, width = map(int, flow_value.shape[2:])
97+
if height <= 1 or width <= 1:
98+
continue
99+
if tuple(image_value.shape[2:]) != (height, width):
100+
continue
101+
if not isinstance(base_value, torch.Tensor) or tuple(base_value.shape) != (
102+
1,
103+
2,
104+
height,
105+
width,
106+
):
107+
continue
108+
if base_grid.target != torch.ops.aten.cat.default or base_grid.args[1] != 1:
109+
continue
110+
axes = base_grid.args[0]
111+
if not isinstance(axes, (list, tuple)) or len(axes) != 2:
112+
continue
113+
114+
def matches_axis(axis_node, axis):
115+
if not isinstance(axis_node, Node) or axis_node.target not in (
116+
torch.ops.aten.expand.default,
117+
torch.ops.aten.expand_copy.default,
118+
):
119+
return False
120+
view = axis_node.args[0]
121+
if not isinstance(view, Node) or view.target not in (
122+
torch.ops.aten.view.default,
123+
torch.ops.aten.view_copy.default,
124+
):
125+
return False
126+
linspace = view.args[0]
127+
if (
128+
not isinstance(linspace, Node)
129+
or linspace.target != torch.ops.aten.linspace.default
130+
):
131+
return False
132+
if linspace.kwargs.get("dtype") not in (None, torch.float32):
133+
return False
134+
extent = width if axis == 3 else height # noqa: B023
135+
expected_view = [1, 1, 1, width] if axis == 3 else [1, 1, height, 1] # noqa: B023 # fmt: skip
136+
expected_expand = [1, -1, height, -1] if axis == 3 else [1, -1, -1, width] # noqa: B023 # fmt: skip
137+
return (
138+
tuple(linspace.args[:3]) == (-1.0, 1.0, extent)
139+
and list(view.args[1]) == expected_view
140+
and list(axis_node.args[1]) == expected_expand
141+
)
142+
143+
if matches_axis(axes[0], 3) and matches_axis(axes[1], 2):
144+
return True
145+
return False
146+
147+
31148
@dataclass(eq=True, frozen=True)
32149
class QuantizationConfig:
33150
"""Provide a container for quantization specs.
@@ -156,7 +273,6 @@ def get_bias_qspec(
156273
floating-point.
157274
158275
"""
159-
160276
if self.bias is None or node is None:
161277
return None
162278

@@ -363,7 +479,6 @@ def get_output_act_qspec(
363479
If node is a `to.dtype` operator, returns a fixed quantization spec if the input is integer and the output is float32.
364480
365481
"""
366-
367482
if node is None:
368483
return super().get_output_act_qspec()
369484
# MLETORCH-1853: Fix lazy import when moving files around
@@ -421,4 +536,54 @@ def get_output_act_qspec(
421536

422537

423538
class VGFQuantizationConfig(TOSAQuantizationConfig):
539+
"""Configure quantization for VGF-specific lowering paths."""
540+
424541
quantize_grid_sampler_grid = True
542+
543+
@staticmethod
544+
def _snorm_compatible_qspec(
545+
qspec: Optional[QuantizationSpecBase],
546+
) -> Optional[QuantizationSpecBase]:
547+
if qspec is None:
548+
return None
549+
if not isinstance(qspec, QuantizationSpec):
550+
raise ValueError("SNORM-compatible qparams require a QuantizationSpec.")
551+
return replace(qspec, quant_min=-127, quant_max=127)
552+
553+
def get_input_act_qspec(self, node=None, input_node=None):
554+
"""Return the input activation spec for a VGF graph node.
555+
556+
Args:
557+
node (Optional[Node]): Node whose input spec is requested.
558+
input_node (Optional[Node]): Input node whose spec is requested.
559+
560+
Returns:
561+
Optional[QuantizationSpecBase]: Input activation spec, or ``None``.
562+
563+
"""
564+
if (
565+
node is not None
566+
and input_node is not None
567+
and _is_canonical_flow_offset_grid_sampler(node)
568+
and input_node == node.args[0]
569+
):
570+
qspec = QuantizationConfig.get_input_act_qspec(self, node, input_node)
571+
if isinstance(qspec, QuantizationSpec) and qspec.dtype == torch.int8:
572+
return self._snorm_compatible_qspec(qspec)
573+
return super().get_input_act_qspec(node, input_node)
574+
575+
def get_output_act_qspec(self, node=None):
576+
"""Return the output activation spec for a VGF graph node.
577+
578+
Args:
579+
node (Optional[Node]): Node whose output spec is requested.
580+
581+
Returns:
582+
Optional[QuantizationSpecBase]: Output activation spec, or ``None``.
583+
584+
"""
585+
if node is not None and _is_canonical_flow_offset_grid_sampler(node):
586+
qspec = QuantizationConfig.get_output_act_qspec(self, node)
587+
if isinstance(qspec, QuantizationSpec) and qspec.dtype == torch.int8:
588+
return self._snorm_compatible_qspec(qspec)
589+
return super().get_output_act_qspec(node)

0 commit comments

Comments
 (0)