Skip to content

Commit 1ba1621

Browse files
committed
[WebNN] Add limit to QDQ ops
WebNN requires the `scale_shape` to be a subsample of the `input_shape`.
1 parent b14b4ec commit 1ba1621

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

onnxruntime/core/providers/webnn/builders/impl/qdq_op_builder.cc

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ class QDQOpBuilder : public BaseOpBuilder {
2222
const logging::Logger& logger) const override ORT_MUST_USE_RESULT;
2323

2424
// Operator support related.
25+
bool IsOpSupportedImpl(const InitializedTensorSet& /* initializers */, const Node& node,
26+
const WebnnDeviceType /* device_type */, const logging::Logger& logger) const override;
2527
bool HasSupportedInputsImpl(const Node& node, const emscripten::val& wnn_limits,
2628
const logging::Logger& logger) const override;
2729
};
@@ -118,6 +120,38 @@ Status QDQOpBuilder::AddToModelBuilderImpl(ModelBuilder& model_builder,
118120
return Status::OK();
119121
}
120122

123+
// Operator support related.
124+
bool QDQOpBuilder::IsOpSupportedImpl(const InitializedTensorSet& /* initializers */,
125+
const Node& node,
126+
const WebnnDeviceType /* device_type */,
127+
const logging::Logger& logger) const {
128+
const auto& input_defs = node.InputDefs();
129+
130+
std::vector<int64_t> input_shape;
131+
std::vector<int64_t> scale_shape;
132+
133+
if (!GetShape(*input_defs[0], input_shape, logger) || !GetShape(*input_defs[1], scale_shape, logger)) {
134+
return false;
135+
}
136+
137+
// WebNN requires the scale_shape to be a subsample of the input_shape.
138+
if (scale_shape.size() > input_shape.size()) {
139+
LOGS(logger, VERBOSE) << "The rank of scale is larger than the rank of input";
140+
return false;
141+
}
142+
143+
for (size_t i = 0; i < scale_shape.size(); ++i) {
144+
auto scale_dim = scale_shape[scale_shape.size() - i - 1];
145+
auto input_dim = input_shape[input_shape.size() - i - 1];
146+
if (input_dim % scale_dim != 0) {
147+
LOGS(logger, VERBOSE) << "The shape of scale is not a subsample of the shape of input";
148+
return false;
149+
}
150+
}
151+
152+
return true;
153+
}
154+
121155
bool QDQOpBuilder::HasSupportedInputsImpl(const Node& node, const emscripten::val& wnn_limits,
122156
const logging::Logger& logger) const {
123157
const auto& input_defs = node.InputDefs();

0 commit comments

Comments
 (0)