Skip to content

Convert scalar to tensor before quantizer annoate #2958

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

Closed
wants to merge 2 commits into from
Closed
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
47 changes: 46 additions & 1 deletion backends/qualcomm/quantizer/quantizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

from torch import Tensor
from torch._ops import OpOverload
from torch.ao.quantization.fx.utils import get_new_attr_name_with_prefix
from torch.ao.quantization.observer import (
HistogramObserver,
MinMaxObserver,
Expand Down Expand Up @@ -371,14 +372,58 @@ def set_per_channel_weight_dtype(
def set_per_channel_quant(self, enable: bool) -> None:
self.enable_per_channel_conv_quant = enable

def _lift_constant_scalar_operands(self, gm: torch.fx.GraphModule) -> None:
"""
For the case like mul(x, 2), convert the the scalr to tensor
"""
for n in gm.graph.nodes:
if n.op != "call_function" or n.target not in (
torch.ops.aten.add.Tensor,
torch.ops.aten.sub.Tensor,
torch.ops.aten.mul.Tensor,
torch.ops.aten.mul.Scalar,
torch.ops.aten.rsub.Scalar,
):
continue

const_arg = None
non_const_arg = None
for arg in n.args:
if isinstance(arg, torch.fx.Node):
non_const_arg = arg
else:
const_arg = arg

if non_const_arg is None or const_arg is None:
continue

tensor_constant = torch.tensor([const_arg], dtype=torch.float32)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe we will want to infer data type from data.

tensor_constant_name = get_new_attr_name_with_prefix("_tensor_constant_")(
gm
)
gm.register_buffer(tensor_constant_name, tensor_constant)

fake_mode = n.meta["val"].fake_mode
with gm.graph.inserting_before(n):
get_attr_node = gm.graph.get_attr(tensor_constant_name)
get_attr_node.meta["val"] = fake_mode.from_tensor(tensor_constant)

if n.target == torch.ops.aten.rsub.Scalar:
n.args = (get_attr_node, non_const_arg) + n.args[2:]
n.target = torch.ops.aten.sub.Tensor
else:
n.args = (non_const_arg, get_attr_node) + n.args[2:]

gm.recompile()

def transform_for_annotation(self, model: GraphModule) -> GraphModule:
model = RemoveClone()(model).graph_module
model = ReduceDynamicRange()(model).graph_module
model = ConvertHardsigmoid(quantization_capture=True)(model).graph_module
model = DecomposeScaledDotProductAttention()(model).graph_module
model = DecomposeSilu()(model).graph_module
model = ReplaceInfBuffer()(model).graph_module

self._lift_constant_scalar_operands(model)
return model

def validate(self, model: GraphModule) -> None:
Expand Down