Skip to content
Open
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
4 changes: 2 additions & 2 deletions backends/qualcomm/_passes/insert_io_qdq.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
insert_quant_node,
)

from executorch.backends.qualcomm.builders.node_visitor import q_dq_map, q_ops
from executorch.backends.qualcomm.builders.node_visitor import q_ops, to_dq_op

from executorch.backends.qualcomm.builders.utils import (
is_mutable_buffer_input,
Expand Down Expand Up @@ -76,7 +76,7 @@ def _insert(self, graph_module: torch.fx.GraphModule) -> torch.fx.GraphModule:
graph_module=graph_module,
input_node=n,
output_node=user,
target=q_dq_map[n.meta[QCOM_QUANT_ATTRS][QCOM_ENCODING]],
target=to_dq_op(n.meta[QCOM_QUANT_ATTRS][QCOM_ENCODING]),
)

def call(self, graph_module: torch.fx.GraphModule):
Expand Down
27 changes: 17 additions & 10 deletions backends/qualcomm/_passes/lpai_partition_fallback_support.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@
insert_dequant_node,
insert_quant_node,
)
from executorch.backends.qualcomm.builders.node_visitor import dq_ops, q_dq_map, q_ops
from executorch.backends.qualcomm.builders.node_visitor import (
dq_ops,
q_ops,
to_dq_op,
to_q_op,
)

from executorch.backends.qualcomm.builders.utils import is_graph_input, is_graph_output

Expand Down Expand Up @@ -108,15 +113,15 @@ def preserve_io_qdq(self, graph_module: torch.fx.GraphModule) -> None:
graph_module=graph_module,
input_node=n,
output_node=user,
target=n.meta[QCOM_QUANT_ATTRS][QCOM_ENCODING],
target=to_q_op(n.meta[QCOM_QUANT_ATTRS][QCOM_ENCODING]),
pop_quant_attrs=False,
)
q_node.meta[QCOM_FALLBACK_NODE] = True
dq_node = insert_dequant_node(
graph_module=graph_module,
input_node=q_node,
output_node=user,
target=q_dq_map[q_node.target],
target=to_dq_op(q_node.target),
)
dq_node.meta[QCOM_BYPASS_NODE] = True
elif (
Expand All @@ -131,15 +136,17 @@ def preserve_io_qdq(self, graph_module: torch.fx.GraphModule) -> None:
graph_module=graph_module,
input_node=output_node,
output_node=getitem_node,
target=output_node.meta[QCOM_QUANT_ATTRS][QCOM_ENCODING],
target=to_q_op(
output_node.meta[QCOM_QUANT_ATTRS][QCOM_ENCODING]
),
pop_quant_attrs=False,
)
q_node.meta[QCOM_BYPASS_NODE] = True
dq_node = insert_dequant_node(
graph_module=graph_module,
input_node=q_node,
output_node=getitem_node,
target=q_dq_map[q_node.target],
target=to_dq_op(q_node.target),
)
dq_node.meta[QCOM_FALLBACK_NODE] = True

Expand Down Expand Up @@ -191,7 +198,7 @@ def insert_partition_qdq(
graph_module=graph_module,
input_node=input_node,
output_node=node,
target=input_node.meta[QCOM_QUANT_ATTRS][QCOM_ENCODING],
target=to_q_op(input_node.meta[QCOM_QUANT_ATTRS][QCOM_ENCODING]),
pop_quant_attrs=False,
)
for input_node in input_nodes
Expand All @@ -203,7 +210,7 @@ def insert_partition_qdq(
graph_module=graph_module,
input_node=input_q_node,
output_node=node,
target=q_dq_map[input_q_node.target],
target=to_dq_op(input_q_node.target),
)
for input_q_node in input_q_nodes
]
Expand Down Expand Up @@ -232,17 +239,17 @@ def insert_partition_qdq(
graph_module=graph_module,
input_node=output_node,
output_node=output_user_node,
target=output_node.meta[QCOM_QUANT_ATTRS][QCOM_ENCODING],
target=to_q_op(output_node.meta[QCOM_QUANT_ATTRS][QCOM_ENCODING]),
pop_quant_attrs=False,
)
output_q_node.meta[QCOM_FALLBACK_NODE] = True
output_dq_node = insert_dequant_node(
graph_module=graph_module,
input_node=output_q_node,
output_node=output_user_node,
target=q_dq_map[
target=to_dq_op(
output_q_node.meta[QCOM_QUANT_ATTRS][QCOM_ENCODING]
],
),
)
output_dq_node.meta[QCOM_BYPASS_NODE] = True
graph_module.graph.eliminate_dead_code()
Expand Down
8 changes: 7 additions & 1 deletion backends/qualcomm/_passes/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from typing import Callable, Dict, List

import torch
from executorch.backends.qualcomm.builders.node_visitor import q_ops
from executorch.backends.qualcomm.builders.node_visitor import dq_ops, q_ops
from executorch.backends.qualcomm.builders.utils import get_parameter
from executorch.backends.qualcomm.utils.constants import (
QCOM_DTYPE,
Expand Down Expand Up @@ -81,6 +81,9 @@ def insert_quant_node(
quant_attrs: Dict = None,
pop_quant_attrs: bool = True,
) -> torch.fx.Node:
assert (
target in q_ops
), f"insert_quant_node expects a quantize target, got: {target}"
with graph_module.graph.inserting_after(input_node):
inserted_node = _create_q_or_dq_node(
graph_module=graph_module,
Expand All @@ -101,6 +104,9 @@ def insert_dequant_node(
output_node: torch.fx.node,
target: torch.fx.node.Target,
) -> None:
assert (
target in dq_ops
), f"insert_dequant_node expects a dequantize target, got: {target}"
with graph_module.graph.inserting_after(input_node):
inserted_node = _create_q_or_dq_node(
graph_module=graph_module, node=input_node, target=target
Expand Down
14 changes: 11 additions & 3 deletions backends/qualcomm/builders/node_visitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,21 @@
q_dq_map = {
exir_ops.edge.quantized_decomposed.quantize_per_tensor.default: exir_ops.edge.quantized_decomposed.dequantize_per_tensor.default,
exir_ops.edge.quantized_decomposed.quantize_per_tensor.tensor: exir_ops.edge.quantized_decomposed.dequantize_per_tensor.tensor,
exir_ops.edge.quantized_decomposed.dequantize_per_tensor.default: exir_ops.edge.quantized_decomposed.dequantize_per_tensor.default,
exir_ops.edge.quantized_decomposed.dequantize_per_tensor.tensor: exir_ops.edge.quantized_decomposed.dequantize_per_tensor.tensor,
exir_ops.edge.quantized_decomposed.quantize_per_channel.default: exir_ops.edge.quantized_decomposed.dequantize_per_channel.default,
exir_ops.edge.quantized_decomposed.dequantize_per_channel.default: exir_ops.edge.quantized_decomposed.dequantize_per_channel.default,
exir_ops.edge.quantized_decomposed.dequantize_per_tensor.default: exir_ops.edge.quantized_decomposed.quantize_per_tensor.default,
exir_ops.edge.quantized_decomposed.dequantize_per_tensor.tensor: exir_ops.edge.quantized_decomposed.quantize_per_tensor.tensor,
exir_ops.edge.quantized_decomposed.dequantize_per_channel.default: exir_ops.edge.quantized_decomposed.quantize_per_channel.default,
}


def to_q_op(target):
return target if target in q_ops else q_dq_map[target]


def to_dq_op(target):
return target if target in dq_ops else q_dq_map[target]


class NodeVisitor:
"""
Node visitor pattern for visiting nodes in an edge IR graph
Expand Down
Loading