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
1 change: 1 addition & 0 deletions backends/qualcomm/_passes/i64_to_i32.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class I64toI32(ExportPass):
I64_IN_OPS = {
exir_ops.edge.aten.gather.default: [2],
exir_ops.edge.aten.scatter.src: [2],
exir_ops.edge.aten.scatter.value: [2],
}
copy_op = exir_ops.edge.aten._to_copy.default

Expand Down
1 change: 1 addition & 0 deletions backends/qualcomm/_passes/layout_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ class LayoutTransform(ExportPass):
exir_ops.edge.aten.relu.default,
exir_ops.edge.aten.round.default,
exir_ops.edge.aten.scatter.src,
exir_ops.edge.aten.scatter.value,
exir_ops.edge.aten.sigmoid.default,
exir_ops.edge.aten.sign.default,
exir_ops.edge.aten.slice_copy.Tensor,
Expand Down
63 changes: 52 additions & 11 deletions backends/qualcomm/builders/op_scatter_elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@

import numpy as np
import torch
from executorch.backends.qualcomm.utils.constants import QCOM_AXIS_ORDER, QCOM_DATA
from executorch.backends.qualcomm.utils.constants import (
QCOM_AXIS_ORDER,
QCOM_DATA,
QCOM_QUANT_ATTRS,
QCOM_SCALE,
QCOM_ZERO_POINT,
)

from .node_visitor import NodeVisitor
from .node_visitor_manager import register_node_visitor
Expand All @@ -18,7 +24,7 @@

@register_node_visitor
class ScatterElements(NodeVisitor):
target = ["aten.scatter.src"]
target = ["aten.scatter.src", "aten.scatter.value"]

def __init__(self, *args) -> None:
super().__init__(*args)
Expand Down Expand Up @@ -48,15 +54,50 @@ def define_node(
nodes_to_wrappers,
)

updates_node = self.get_node(node.args[3])
updates_tensor = self.get_tensor(updates_node, node)
updates_tensor_wrapper = self.define_tensor(
updates_node,
node,
updates_tensor,
PyQnnManager.Qnn_TensorType_t.QNN_TENSOR_TYPE_NATIVE,
nodes_to_wrappers,
)
# Handle both scatter.src (args[3] is a Node/tensor) and
# scatter.value (args[3] is a scalar)
if isinstance(node.args[3], torch.fx.Node):
# scatter.src path: args[3] is a tensor node
updates_node = self.get_node(node.args[3])
updates_tensor = self.get_tensor(updates_node, node)
updates_tensor_wrapper = self.define_tensor(
updates_node,
node,
updates_tensor,
PyQnnManager.Qnn_TensorType_t.QNN_TENSOR_TYPE_NATIVE,
nodes_to_wrappers,
)
else:
# scatter.value: expand scalar to a static tensor matching index shape.
scalar_val = node.args[3]
quant_attrs = input_node.meta.get(QCOM_QUANT_ATTRS, None)

if quant_attrs is not None:
# Quantized: pre-quantize the scalar to match the data tensor's dtype.
# QNN accepts STATIC tensors with raw quantized data.
scale = quant_attrs[QCOM_SCALE]
zero_point = quant_attrs[QCOM_ZERO_POINT]
quant_dtype = quant_attrs.get("dtype", torch.uint8)
quantized_val = int(round(scalar_val / scale) + zero_point)
info = torch.iinfo(quant_dtype)
quantized_val = max(info.min, min(info.max, quantized_val))
updates_tensor = torch.full(
index_tensor.shape, quantized_val, dtype=quant_dtype
)
else:
updates_tensor = torch.full(
index_tensor.shape, scalar_val, dtype=input_tensor.dtype
)

updates_tensor_wrapper = self.define_tensor(
index_node,
node,
updates_tensor,
PyQnnManager.Qnn_TensorType_t.QNN_TENSOR_TYPE_STATIC,
nodes_to_wrappers,
node_name=f"{node.name}_updates_value",
wrapper_idx=2,
)

output_tensor = self.get_tensor(node, node)
output_tensor_wrapper = self.define_tensor(
Expand Down
2 changes: 1 addition & 1 deletion backends/qualcomm/quantizer/annotators/htp_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -1427,7 +1427,7 @@ class ScaledDotProductAttention(GeneralOpDef):


@register_annotator(
[torch.ops.aten.scatter.src],
[torch.ops.aten.scatter.src, torch.ops.aten.scatter.value],
qnn_op=None,
)
class ScatterElements(GeneralOpDef):
Expand Down
2 changes: 1 addition & 1 deletion backends/qualcomm/quantizer/annotators/lpai_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -874,7 +874,7 @@ class ScaledDotProductAttention(GeneralOpDef):


@register_annotator(
[torch.ops.aten.scatter.src],
[torch.ops.aten.scatter.src, torch.ops.aten.scatter.value],
qnn_op=None,
)
class ScatterElements(GeneralOpDef):
Expand Down
10 changes: 10 additions & 0 deletions backends/qualcomm/tests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2304,6 +2304,16 @@ def forward(self, data, index, src):
return torch.scatter(data, self.dim, index, src)


class ScatterValue(torch.nn.Module):
def __init__(self, dim=1, value=0.5):
super().__init__()
self.dim = dim
self.value = value

def forward(self, data, index):
return torch.scatter(data, self.dim, index, self.value)


class SelectCopy(torch.nn.Module):
def __init__(self):
super().__init__()
Expand Down
73 changes: 73 additions & 0 deletions backends/qualcomm/tests/test_qnn_delegate.py
Original file line number Diff line number Diff line change
Expand Up @@ -2194,6 +2194,42 @@ def test_qnn_backend_scatter_src(self):
index += 1
self.lower_module_and_test_output(module, sample_input)

def test_qnn_backend_scatter_value(self):
test_comb = [
{
QCOM_MODULE: [ScatterValue(dim=1, value=0.5)], # noqa: F405
QCOM_SAMPLE_INPUTS: [
(
torch.rand(3, 5),
torch.tensor(
[[0, 1, 2, 3, 4], [4, 3, 2, 1, 0], [1, 0, 3, 4, 2]],
dtype=torch.int64,
),
),
],
},
{
QCOM_MODULE: [ScatterValue(dim=0, value=1.0)], # noqa: F405
QCOM_SAMPLE_INPUTS: [
(
torch.rand(3, 5),
torch.tensor(
[[2, 1, 0, 1, 2], [0, 2, 1, 2, 0], [1, 0, 2, 0, 1]],
dtype=torch.int64,
),
),
],
},
]

index = 0
for comb in test_comb:
for module in comb[QCOM_MODULE]:
for sample_input in comb[QCOM_SAMPLE_INPUTS]:
with self.subTest(i=index):
index += 1
self.lower_module_and_test_output(module, sample_input)

def test_qnn_backend_rsqrt(self):
module = Rsqrt() # noqa: F405
sample_input = (torch.abs(torch.randn([3, 4])),)
Expand Down Expand Up @@ -5330,6 +5366,43 @@ def test_qnn_backend_scatter_src(self):
qdq_module = self.get_qdq_module(module, sample_input)
self.lower_module_and_test_output(qdq_module, sample_input)

def test_qnn_backend_scatter_value(self):
test_comb = [
{
QCOM_MODULE: [ScatterValue(dim=1, value=0.5)], # noqa: F405
QCOM_SAMPLE_INPUTS: [
(
torch.rand(3, 5),
torch.tensor(
[[0, 1, 2, 3, 4], [4, 3, 2, 1, 0], [1, 0, 3, 4, 2]],
dtype=torch.int64,
),
),
],
},
{
QCOM_MODULE: [ScatterValue(dim=0, value=1.0)], # noqa: F405
QCOM_SAMPLE_INPUTS: [
(
torch.rand(3, 5),
torch.tensor(
[[2, 1, 0, 1, 2], [0, 2, 1, 2, 0], [1, 0, 2, 0, 1]],
dtype=torch.int64,
),
),
],
},
]

index = 0
for comb in test_comb:
for module in comb[QCOM_MODULE]:
for sample_input in comb[QCOM_SAMPLE_INPUTS]:
with self.subTest(i=index):
index += 1
qdq_module = self.get_qdq_module(module, sample_input)
self.lower_module_and_test_output(qdq_module, sample_input)

def test_qnn_backend_sdpa(self):
modules = [
ScaledDotProductAttention(), # noqa: F405
Expand Down
Loading