Skip to content

[QNN EP] Add ONNX ScatterElements support #24811

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
2 changes: 2 additions & 0 deletions onnxruntime/core/providers/qnn/builder/op_builder_factory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ OpBuilderRegistrations::OpBuilderRegistrations() {
CreateSimpleOpBuilder("GridSample", *this);

CreateSimpleOpBuilder("LpNormalization", *this);

CreateSimpleOpBuilder("ScatterElements", *this);
}

{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,8 @@ class BaseOpBuilder : public IOpBuilder {

{"Pad", QNN_OP_PAD},

{"ScatterElements", QNN_OP_SCATTER_ELEMENTS},

{"Expand", QNN_OP_ELEMENT_WISE_MULTIPLY}};
auto it = onnx_op_type_to_qnn_op_type.find(onnx_op_type);
ORT_ENFORCE(it != onnx_op_type_to_qnn_op_type.end());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class SimpleOpBuilder : public BaseOpBuilder {

static constexpr std::array<std::string_view, 2> gridsample_supported_modes = {"bilinear", "nearest"};
static constexpr std::array<std::string_view, 3> gridsample_supported_padding_modes = {"zeros", "border", "reflection"};
static constexpr std::array<std::string_view, 4> scatterelements_supported_reduction = {"none", "add", "mul", "max"};
};

Status SimpleOpBuilder::ExplicitOpCheck(QnnModelWrapper& qnn_model_wrapper,
Expand Down Expand Up @@ -101,6 +102,14 @@ Status SimpleOpBuilder::ExplicitOpCheck(QnnModelWrapper& qnn_model_wrapper,
}
}

// QNN ScatterElements doesn't support MIN reduction
if (op_type == "ScatterElements") {
NodeAttrHelper node_helper(node_unit);
std::string reduction = node_helper.Get("reduction", "none");
ORT_RETURN_IF_NOT(utils::ArrayHasString(scatterelements_supported_reduction, reduction), "ScatterElements does not support reduction ",
reduction.c_str());
}

return Status::OK();
}

Expand Down Expand Up @@ -254,6 +263,33 @@ Status ProcessGridSampleAttributes(QnnModelWrapper& qnn_model_wrapper,
return Status::OK();
}

// Process Reduction attribute of ScatterElements op
Status ProcessReductionAttribute(QnnModelWrapper& qnn_model_wrapper,
const NodeUnit& node_unit,
std::vector<std::string>& param_tensor_names) {
NodeAttrHelper node_helper(node_unit);
std::string reduction = node_helper.Get("reduction", "none");
Qnn_Scalar_t reduction_qnn_scalar = QNN_SCALAR_INIT;
reduction_qnn_scalar.dataType = QNN_DATATYPE_UINT_32;
if ("none" == reduction) {
reduction_qnn_scalar.uint32Value = QNN_OP_SCATTER_ELEMENTS_REDUCTION_NONE;
} else if ("add" == reduction) {
reduction_qnn_scalar.uint32Value = QNN_OP_SCATTER_ELEMENTS_REDUCTION_ADD;
} else if ("mul" == reduction) {
reduction_qnn_scalar.uint32Value = QNN_OP_SCATTER_ELEMENTS_REDUCTION_MUL;
} else if ("max" == reduction) {
reduction_qnn_scalar.uint32Value = QNN_OP_SCATTER_ELEMENTS_REDUCTION_MAX;
} else {
return ORT_MAKE_STATUS(ONNXRUNTIME, FAIL, "ScatterElements support only reduction:{none, add, mul, max}.");
}
QnnParamWrapper reduction_param(node_unit.Index(), node_unit.Name(), QNN_OP_SCATTER_ELEMENTS_PARAM_REDUCTION,
reduction_qnn_scalar);
param_tensor_names.push_back(reduction_param.GetParamTensorName());
qnn_model_wrapper.AddParamWrapper(std::move(reduction_param));

return Status::OK();
}

Status SimpleOpBuilder::ProcessAttributesAndOutputs(QnnModelWrapper& qnn_model_wrapper,
const NodeUnit& node_unit,
std::vector<std::string>&& input_names,
Expand Down Expand Up @@ -358,6 +394,19 @@ Status SimpleOpBuilder::ProcessAttributesAndOutputs(QnnModelWrapper& qnn_model_w
ORT_RETURN_IF_ERROR(ProcessGridSampleAttributes(qnn_model_wrapper, node_unit, param_tensor_names));
}

if (op_type == "ScatterElements") {
// Process axis attribute
int32_t default_axis = 0;
Qnn_Scalar_t axis_qnn_scalar = QNN_SCALAR_INIT;
ORT_RETURN_IF_ERROR(ProcessAxisAttribute(qnn_model_wrapper, node_unit, axis_qnn_scalar, default_axis));
QnnParamWrapper axis_param(node_unit.Index(), node_unit.Name(), QNN_OP_SCATTER_ELEMENTS_PARAM_AXIS, axis_qnn_scalar);
param_tensor_names.push_back(axis_param.GetParamTensorName());
qnn_model_wrapper.AddParamWrapper(std::move(axis_param));

// Process reduction attribute
ORT_RETURN_IF_ERROR(ProcessReductionAttribute(qnn_model_wrapper, node_unit, param_tensor_names));
}

return ProcessOutputs(qnn_model_wrapper, node_unit,
std::move(input_names),
std::move(param_tensor_names),
Expand Down
88 changes: 88 additions & 0 deletions onnxruntime/test/providers/qnn/simple_op_htp_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1017,6 +1017,94 @@ TEST_F(QnnHTPBackendTests, BinaryOp_HTP_Or_Unsupported) {
ExpectedEPNodeAssignment::All);
}

// Test ScatterElements with default attributes on HTP
TEST_F(QnnHTPBackendTests, ScatterElements_int64_int64) {
std::vector<int64_t> data = {0, 1, 2, 3};
std::vector<int64_t> indices = {1};
std::vector<int64_t> updates = {10};
RunOpTest<int64_t>("ScatterElements",
{
TestInputDef<int64_t>({4}, false, std::move(data)),
TestInputDef<int64_t>({1, 1}, false, std::move(indices)),
TestInputDef<int64_t>({1}, false, std::move(updates)),
},
{},
17,
ExpectedEPNodeAssignment::All);
}

// Test ScatterElements with reduction ADD on HTP
TEST_F(QnnHTPBackendTests, ScatterElements_int64_int64_reduction_add) {
std::vector<int64_t> data = {0, 1, 2, 3};
std::vector<int64_t> indices = {1};
std::vector<int64_t> updates = {10};
RunOpTest<int64_t>("ScatterElements",
{
TestInputDef<int64_t>({4}, false, std::move(data)),
TestInputDef<int64_t>({1, 1}, false, std::move(indices)),
TestInputDef<int64_t>({1}, false, std::move(updates)),
},
{
utils::MakeAttribute("reduction", "add"),
},
17,
ExpectedEPNodeAssignment::All);
}

// Test ScatterElements with reduction Mul on HTP
TEST_F(QnnHTPBackendTests, ScatterElements_int64_int64_reduction_mul) {
std::vector<int64_t> data = {0, 1, 2, 3};
std::vector<int64_t> indices = {1};
std::vector<int64_t> updates = {10};
RunOpTest<int64_t>("ScatterElements",
{
TestInputDef<int64_t>({4}, false, std::move(data)),
TestInputDef<int64_t>({1, 1}, false, std::move(indices)),
TestInputDef<int64_t>({1}, false, std::move(updates)),
},
{
utils::MakeAttribute("reduction", "mul"),
},
17,
ExpectedEPNodeAssignment::All);
}

// Test ScatterElements with reduction Max on HTP
TEST_F(QnnHTPBackendTests, ScatterElements_int64_int64_reduction_max) {
std::vector<int64_t> data = {0, 1, 2, 3};
std::vector<int64_t> indices = {1};
std::vector<int64_t> updates = {10};
RunOpTest<int64_t>("ScatterElements",
{
TestInputDef<int64_t>({4}, false, std::move(data)),
TestInputDef<int64_t>({1, 1}, false, std::move(indices)),
TestInputDef<int64_t>({1}, false, std::move(updates)),
},
{
utils::MakeAttribute("reduction", "max"),
},
17,
ExpectedEPNodeAssignment::All);
}

// Test ScatterElements with reduction Min on CPU Fallback
TEST_F(QnnHTPBackendTests, ScatterElements_int64_int64_reduction_min) {
std::vector<int64_t> data = {0, 1, 2, 3};
std::vector<int64_t> indices = {1};
std::vector<int64_t> updates = {10};
RunOpTest<int64_t>("ScatterElements",
{
TestInputDef<int64_t>({4}, false, std::move(data)),
TestInputDef<int64_t>({1, 1}, false, std::move(indices)),
TestInputDef<int64_t>({1}, false, std::move(updates)),
},
{
utils::MakeAttribute("reduction", "min"),
},
17,
ExpectedEPNodeAssignment::None);
}

// Test 8-bit QDQ GridSample with bilinear
TEST_F(QnnHTPBackendTests, GridSample_Bilinear) {
RunQDQOpTest<uint8_t>("GridSample",
Expand Down
Loading