Skip to content
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

Refactor some APIs #3863

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions backends/cadence/aot/TARGETS
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ python_library(
],
deps = [
":passes",
":utils",
"//caffe2:torch",
"//executorch/exir:lib",
],
Expand Down
21 changes: 15 additions & 6 deletions backends/cadence/aot/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
ReplaceScalarTensorWithFullPass,
ReplaceSqueezeAndUnsqueezeWithViewPass,
)
from executorch.backends.cadence.aot.utils import model_is_quantized
from executorch.exir import EdgeCompileConfig, EdgeProgramManager, to_edge
from torch.ao.quantization.pt2e.export_utils import model_is_exported

from torch.export import export
from torch.export.exported_program import ExportedProgram
Expand All @@ -29,14 +31,21 @@ def export_program(
) -> ExportedProgram:
assert isinstance(model, torch.nn.Module), "model should be an nn.Module"

# If the model is already a GraphModule (most likely from quantization), call the
# suggested torch.ao.quantization API instead, which only does dropout and batchnorm.
if isinstance(model, torch.fx.GraphModule):
torch.ao.quantization.move_exported_model_to_eval(model)
else:
# We don't support training mode. Make it eval
# We don't support training mode. Make the model inference mode by
# calling model.eval() or an equivalent call for quantized models.
# GraphModules cannot call eval(), so we skip them.
if not isinstance(model, torch.fx.GraphModule):
if hasattr(model, "eval"):
model.eval()
else:
# If the model is quantized, call the suggested torch.ao.quantization API
# which only does dropout and batchnorm.
if model_is_quantized(model):
torch.ao.quantization.move_exported_model_to_eval(model)
else:
# If we get a GraphModule which is _not_ quantized, then it should already
# have been exported.
assert model_is_exported(model), "model should be from an ExportedProgram"

# Prevent mkldnn decompositions
torch._C._set_mkldnn_enabled(False)
Expand Down
21 changes: 21 additions & 0 deletions backends/cadence/aot/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,27 @@
from executorch.exir.dialects.edge._ops import EdgeOpOverload, EdgeOpOverloadPacket
from tabulate import tabulate

quant_ops = {
torch.ops.quantized_decomposed.quantize_per_tensor.default,
torch.ops.quantized_decomposed.dequantize_per_tensor.default,
torch.ops.quantized_decomposed.quantize_per_channel.default,
torch.ops.quantized_decomposed.dequantize_per_channel.default,
}


# Check if the model is quantized, by looking at the graph and finding quant/dequant ops
def model_is_quantized(model: torch.nn.Module) -> bool:
# Quantized models have to be GraphModules already, from prepare/convert calls.
# Return false if the model is not a GraphModule.
if not isinstance(model, torch.fx.GraphModule):
return False

# Walk through the graph and look for quant/dequant ops
for op in quant_ops:
if model.graph.find_nodes(op="call_function", target=op):
return True
return False


# Get the output size of a 1D convolution given the input size and parameters
def get_conv1d_output_size(
Expand Down
Loading