-
Notifications
You must be signed in to change notification settings - Fork 1k
Draft: Integration of QuantizeFusedConvBnBiasPass to NXP conversion pipeline
#17523
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,6 +13,9 @@ | |
| from typing import Any, Dict, List, Tuple, Type | ||
|
|
||
| import torch | ||
| from executorch.backends.transforms.quantize_fused_convbn_bias_pass import ( | ||
| QuantizeFusedConvBnBiasPass, | ||
| ) | ||
| from torch import fx | ||
| from torch._ops import OpOverload | ||
| from torch.export import ExportedProgram | ||
|
|
@@ -162,15 +165,15 @@ def find_sequential_partitions_aten( | |
|
|
||
|
|
||
| def calibrate_and_quantize( | ||
| model: ExportedProgram | fx.GraphModule, | ||
| model: ExportedProgram, | ||
| calibration_inputs: Iterable[tuple[torch.Tensor, ...]], | ||
| quantizer: Quantizer, | ||
| is_qat: bool = False, | ||
| ) -> fx.GraphModule: | ||
| """Quantize the provided model. | ||
|
|
||
| :param model: Aten model (or it's GraphModule representation) to quantize. | ||
| :param calibration_inputs: Either a tuple of calibration input tensors where each element corresponds to a model | ||
| :param model: Aten exported model to quantize. | ||
| :param calibration_inputs: Either a tuple of calibration input tensors where each element corresponds to a module | ||
| input. Or an iterator over such tuples. | ||
| :param quantizer: Quantizer to use. | ||
| :param is_qat: Whether quantization is done using Quantization Aware Training (QAT) or not. | ||
|
|
@@ -179,17 +182,21 @@ def calibrate_and_quantize( | |
| :return: Quantized GraphModule. | ||
| """ | ||
|
|
||
| if isinstance(model, ExportedProgram): | ||
| model = model.module() | ||
| module = model.module() | ||
|
|
||
| if is_qat: | ||
| m = prepare_qat_pt2e(model, quantizer) | ||
| m = move_exported_model_to_eval(m) | ||
| module = prepare_qat_pt2e(module, quantizer) | ||
| module = move_exported_model_to_eval(module) | ||
| else: | ||
| m = prepare_pt2e(model, quantizer) | ||
| module = prepare_pt2e(module, quantizer) | ||
|
|
||
| for data in calibration_inputs: | ||
| m(*data) | ||
| m = convert_pt2e(m) | ||
| module(*data) | ||
| module = convert_pt2e(module) | ||
|
|
||
| # Without this export, conv bias is not in the graph_signature. | ||
| model = torch.export.export(module, calibration_inputs[0], strict=True) | ||
| bias_quant_pass = QuantizeFusedConvBnBiasPass(model) | ||
| model = bias_quant_pass(model.graph_module) | ||
|
|
||
| return m | ||
| return model.graph_module | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is the change to use graph_module desired outcome or currently just needed for the pass as is?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The function previously returned |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,29 @@ | ||
| load("@fbcode_macros//build_defs:build_file_migration.bzl", "fbcode_target", "non_fbcode_target") | ||
| load("@fbcode_macros//build_defs:python_pytest.bzl", "python_pytest") | ||
| load(":targets.bzl", "define_common_targets") | ||
|
|
||
| oncall("executorch") | ||
|
|
||
| fbcode_target(_kind = define_common_targets,) | ||
|
|
||
| fbcode_target(_kind = python_pytest, | ||
| name = "test_quantize_fused_convbn_bias_pass", | ||
| srcs = [ | ||
| "test/test_quantize_fused_convbn_bias_pass.py", | ||
| ], | ||
| deps = [ | ||
| "//caffe2:torch", | ||
| ":quantize_fused_convbn_bias_pass", | ||
| "//executorch/backends/arm/quantizer:arm_quantizer", | ||
| "//executorch/backends/arm/test:arm_tester_lib", | ||
| "//executorch/backends/arm/test:arm_tester_serialize", | ||
| "//executorch/backends/arm/test:common", | ||
| "//executorch/backends/arm/tosa:tosa", | ||
| "//executorch/backends/nxp:quantizer", | ||
| "//executorch/backends/nxp:neutron_backend", | ||
| "//executorch/backends/xnnpack/test/tester:tester", | ||
| "//executorch/exir:lib", | ||
| "//executorch/kernels/quantized:custom_ops_generated_lib", | ||
| "fbsource//third-party/pypi/pytest:pytest", | ||
| ], | ||
| ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So you want to try to drop needing this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just wanted to be clear as export wasn't used before in this function. It is okay to keep it.