Skip to content

Make test_force_quant_dequant_fusion use GraphBuilder. #10926

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

Merged
merged 1 commit into from
May 20, 2025
Merged
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
42 changes: 19 additions & 23 deletions backends/cadence/aot/tests/test_fusion_ops_passes.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,32 +248,28 @@ def forward(self, x):
)

def test_force_quant_dequant_fusion(self):
class M(torch.nn.Module):
def __init__(self):
super().__init__()

def forward(self, x):
x = torch.ops.quantized_decomposed.quantize_per_tensor(
x, 1.2, 3, 0, 127, torch.int8
)
x = torch.permute(x, [2, 0, 1, 3])
x = torch.ops.quantized_decomposed.dequantize_per_tensor(
x, 4.5, 6, 0, 127, torch.int8
)
return x

inputs = torch.randn(2, 12, 1, 6)
model = M()
graph_module = export_to_edge(model, (inputs,)).exported_program().graph_module

graph_module = FuseQuantDequantToRequantizePass(
builder = GraphBuilder()
x = builder.placeholder("x", torch.randn(2, 12, 1, 6, dtype=torch.float32))
quant = builder.call_operator(
op=exir_ops.edge.quantized_decomposed.quantize_per_tensor.default,
args=(x, 1.2, 3, 0, 127, torch.int8),
)
permute = builder.call_operator(
op=exir_ops.edge.aten.permute_copy.default, args=(quant, [2, 0, 1, 3])
)
dequant = builder.call_operator(
op=exir_ops.edge.quantized_decomposed.dequantize_per_tensor.default,
args=(permute, 4.5, 6, 0, 127, torch.int8),
)
builder.output(dequant)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: use a variable for readability

Suggested change
builder.output(dequant)
builder.output(dequant)
graph_module = builder.get_graph_module()

original_graph = builder.get_graph_module()
converted_graph = FuseQuantDequantToRequantizePass(
force_quant_dequant_fusion=True
)(graph_module).graph_module
)(original_graph).graph_module
self.check_op_counts(
graph_module,
converted_graph,
expected_op_counts={
# Verify that no dequant/quant pair was replaced with requantize.
# quantize -> permute -> dequantize should not be replaced with requantize.
# Verify that dequant/quant pair was replaced with requantize.
exir_ops.edge.quantized_decomposed.quantize_per_tensor.default: 0,
exir_ops.edge.quantized_decomposed.dequantize_per_tensor.default: 0,
exir_ops.edge.cadence.requantize.default: 1,
Expand Down
Loading