Skip to content

Commit 0c4cb18

Browse files
committed
[Espressif] Enable linear quantization with its ESP-NN kernel
Register the prepared linear quantizer in the default recipe alongside the matching operator lowering and kernel. Exercise the public quantizer in its existing bias tests so the preceding preparation branch remains independently usable. Validated 36 quantizer and host linear tests, including the public export recipe and serialization. Authored with OpenAI Codex.
1 parent 1504dc8 commit 0c4cb18

2 files changed

Lines changed: 6 additions & 44 deletions

File tree

‎backends/espressif/quantizer/esp_fused_quantizer.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ class _EspFusedQuantizer(FusedQuantQuantizer):
8787
"""Fused Quant quantizer for the Espressif operator-library backend.
8888
8989
Uses int8 per-tensor affine activations and symmetric weights, with
90-
per-channel weights for convolution.
90+
per-channel weights for linear and convolution. Constant linear biases use
91+
int32 with scales derived from the input and weight observers.
9192
AvgPool2d without divisor_override shares input and output qparams.
9293
EspPassManager lowers supported Fused Quant operators to ESP-NN and
9394
decomposes the rest for portable execution. Use EspNNQuantizer for the
@@ -99,6 +100,7 @@ def __init__(self) -> None:
99100
weighted_config = get_symmetric_quantization_config(is_per_channel=True)
100101
super().__init__(
101102
[
103+
_LinearQuantizer(weighted_config),
102104
_AvgPoolQuantizer(
103105
torch.ops.aten.avg_pool2d.default,
104106
torch.ops.fused_quant.avg_pool2d.default,

‎backends/espressif/test/test_fused_quant_quantizer.py‎

Lines changed: 3 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,10 @@
99

1010
import pytest
1111
import torch
12-
from executorch.backends.espressif import get_esp_fused_quant_recipe
1312
from executorch.backends.espressif.quantizer import get_esp_fused_quant_quantizer
14-
from executorch.backends.espressif.quantizer.esp_fused_quantizer import _LinearQuantizer
1513
from executorch.backends.espressif.test.tester import EspTester
1614
from executorch.backends.transforms.fused_quant.graph_utils import get_qparams_from_node
1715
from executorch.backends.transforms.permute_pass_utils import get_arg
18-
from executorch.backends.xnnpack.quantizer.xnnpack_quantizer import (
19-
get_symmetric_quantization_config,
20-
)
21-
from executorch.exir._serialize._program import deserialize_pte_binary
22-
from executorch.export import export
23-
from executorch.frontends.fused_quant.quantizer.quantizer import FusedQuantQuantizer
2416
from torchao.quantization.pt2e.quantizer import QuantizationAnnotation
2517
from torchao.quantization.pt2e.quantizer.quantizer import Q_ANNOTATION_KEY
2618

@@ -34,19 +26,8 @@ def linear():
3426
return model
3527

3628

37-
def _linear_quantizer():
38-
return FusedQuantQuantizer(
39-
[
40-
_LinearQuantizer(get_symmetric_quantization_config(is_per_channel=True)),
41-
*get_esp_fused_quant_quantizer().child_quantizers,
42-
]
43-
)
44-
45-
4629
def _quantize(model, inputs):
47-
program = (
48-
EspTester(model, inputs).export_fused_quant(_linear_quantizer()).get_artifact()
49-
)
30+
program = EspTester(model, inputs).export_fused_quant().get_artifact()
5031
interpreter = torch.fx.Interpreter(program.module(), garbage_collect_values=False)
5132
interpreter.run(*inputs)
5233
return interpreter
@@ -100,7 +81,7 @@ def forward(self, x, weight):
10081

10182
module = torch.export.export(Model(), (torch.ones(2, 4), torch.ones(3, 4))).module()
10283
with pytest.raises(ValueError, match="ESP-NN linear.*requires constant weights"):
103-
_linear_quantizer().annotate(module)
84+
get_esp_fused_quant_quantizer().annotate(module)
10485

10586

10687
@pytest.mark.parametrize("shared_weight", [False, True])
@@ -219,7 +200,7 @@ def forward(self, x):
219200
annotation = QuantizationAnnotation(_annotated=True)
220201
node.meta[Q_ANNOTATION_KEY] = annotation
221202
bias = get_arg(node, "bias")
222-
_linear_quantizer().annotate(module)
203+
get_esp_fused_quant_quantizer().annotate(module)
223204
assert node.meta[Q_ANNOTATION_KEY] is annotation
224205
assert not annotation.input_qspec_map
225206
assert get_arg(node, "bias") is bias
@@ -235,24 +216,3 @@ def test_convolution_bias_remains_float():
235216
)
236217
assert interpreter.env[get_arg(node, "bias")].dtype == torch.float32
237218
assert get_qparams_from_node(node, "bias") is None
238-
239-
240-
@torch.no_grad()
241-
def test_default_recipe_keeps_linear_portable(linear):
242-
inputs = (torch.tensor([[-2.0, 3.0]]),)
243-
session = export(
244-
linear,
245-
example_inputs=[inputs],
246-
export_recipe=get_esp_fused_quant_recipe(),
247-
)
248-
torch.testing.assert_close(
249-
session.get_edge_program_manager().exported_program().module()(*inputs),
250-
linear(*inputs),
251-
)
252-
plan = deserialize_pte_binary(
253-
session.get_executorch_program_manager().buffer
254-
).program.execution_plan[0]
255-
assert {op.name for op in plan.operators} == {
256-
"aten::permute_copy",
257-
"aten::addmm",
258-
}

0 commit comments

Comments
 (0)