Skip to content

Commit f5e1369

Browse files
authored
Require strict profitability before advancing quantization above slices
Summary: Require a strict profitability win before advancing quantization above slice or select operations. When the float slice outputs and the quantized input have equal estimated memory cost, keep the slice before quantization to avoid quantizing elements that will be discarded. Add coverage for the half-sized-slice boundary. Differential Revision: D119550687 Pull Request resolved: pytorch#22807
1 parent 6e2e55c commit f5e1369

2 files changed

Lines changed: 27 additions & 3 deletions

File tree

‎backends/cadence/aot/reorder_ops.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,16 +141,16 @@ def get_descendent_quant_ops(self, node: torch.fx.Node) -> List[torch.fx.Node]:
141141
get_overload_packet(x.target) in slice_or_select_overloadpkt
142142
for x in trivial_quantized_ops
143143
):
144-
# Profitability metric: the sum of all the output slices must be at
145-
# least half the input node slice.
144+
# Profitability metric: the sum of all the output slices must be
145+
# more than half the input node size.
146146
slice_sizes = [
147147
prod(list(y))
148148
for x in trivial_quantized_ops
149149
if (y := get_shape(self.graph_module, x)) is not None
150150
]
151151
node_shape = get_shape(self.graph_module, node)
152152
node_size = prod(list(node_shape)) if node_shape is not None else 0
153-
if node_size > 2 * sum(slice_sizes):
153+
if node_size >= 2 * sum(slice_sizes):
154154
descendent_quant_ops.clear()
155155

156156
return descendent_quant_ops

‎backends/cadence/aot/tests/test_reorder_ops_passes.py‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,30 @@ def test_advance_branched_quantize(self) -> None:
288288
1,
289289
)
290290

291+
def test_does_not_advance_quantize_for_half_size_slice(self) -> None:
292+
builder = GraphBuilder()
293+
x = builder.placeholder("x", torch.randn(4, 60, 1, 1))
294+
sliced = builder.call_operator(
295+
op=exir_ops.edge.aten.slice_copy.Tensor,
296+
args=(x, 0, 0, 4, 2),
297+
)
298+
quantized = builder.call_operator(
299+
op=exir_ops.edge.quantized_decomposed.quantize_per_tensor.default,
300+
args=(sliced, 0.1, 0, -32768, 32767, torch.int16),
301+
)
302+
builder.output([quantized])
303+
graph_module = builder.get_graph_module()
304+
305+
result = cast(PassResult, AdvanceQuantizeOpAboveDefInBranchPass()(graph_module))
306+
307+
self.assertTrue(
308+
get_node_pos(result.graph_module, exir_ops.edge.aten.slice_copy.Tensor)
309+
< get_node_pos(
310+
result.graph_module,
311+
exir_ops.edge.quantized_decomposed.quantize_per_tensor.default,
312+
)
313+
)
314+
291315
@torch.no_grad()
292316
def test_advance_quantize(self) -> None:
293317
builder = GraphBuilder()

0 commit comments

Comments
 (0)