Skip to content

Commit c547306

Browse files
authored
[WebNN] Fallback the node when its output doesn't have shape info (microsoft#22556)
WebNN requires that each input and output must have shape info.
1 parent b4afc62 commit c547306

File tree

5 files changed

+30
-14
lines changed

5 files changed

+30
-14
lines changed

onnxruntime/core/providers/webnn/builders/helper.cc

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -69,26 +69,24 @@ bool IsNodeSupported(const Node& node, const GraphViewer& graph_viewer, const We
6969
}
7070
}
7171

72-
bool IsInputSupported(const NodeArg& input, const std::string& parent_name, const logging::Logger& logger) {
73-
const auto& input_name = input.Name();
74-
const auto* shape_proto = input.Shape();
72+
bool IsTensorShapeSupported(const NodeArg& node_arg, const std::string& parent_name, const logging::Logger& logger) {
73+
const auto& node_arg_name = node_arg.Name();
74+
const auto* shape_proto = node_arg.Shape();
7575
// Optional tensors can be indicated by an empty name, just ignore it.
76-
if (input_name.empty()) {
76+
if (node_arg_name.empty()) {
7777
return true;
7878
}
79-
// We do not support input with no shape.
79+
// We do not support input/output with no shape.
8080
if (!shape_proto) {
81-
LOGS(logger, VERBOSE) << "Input [" << input_name << "] of [" << parent_name
82-
<< "] has not shape";
81+
LOGS(logger, VERBOSE) << "Node arg [" << node_arg_name << "] of [" << parent_name << "] has not shape";
8382
return false;
8483
}
8584

8685
for (const auto& dim : shape_proto->dim()) {
8786
// WebNN doesn't support dynamic shape - use sessionOptions.freeDimensionOverrides to fix the shape.
8887
if (!dim.has_dim_value()) {
8988
LOGS(logger, VERBOSE) << "Dynamic shape is not supported, "
90-
<< "use sessionOptions.FreeDimensionOverrides to set a fixed shape for input: "
91-
<< input_name;
89+
<< "use sessionOptions.FreeDimensionOverrides to set a fixed shape: " << node_arg_name;
9290
return false;
9391
}
9492
}
@@ -104,7 +102,12 @@ std::vector<std::vector<NodeIndex>> GetSupportedNodes(const GraphViewer& graph_v
104102
std::vector<std::vector<size_t>> supported_node_groups;
105103

106104
for (const auto* input : graph_viewer.GetInputs()) {
107-
if (!IsInputSupported(*input, "graph", logger)) {
105+
if (!IsTensorShapeSupported(*input, "graph", logger)) {
106+
return supported_node_groups;
107+
}
108+
}
109+
for (const auto* output : graph_viewer.GetOutputs()) {
110+
if (!IsTensorShapeSupported(*output, "graph", logger)) {
108111
return supported_node_groups;
109112
}
110113
}

onnxruntime/core/providers/webnn/builders/helper.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ inline bool IsEmptyTensor(const InitializedTensorSet& initializers, const std::s
180180
return std::any_of(dims.begin(), dims.end(), [](auto d) { return d == 0; });
181181
}
182182

183-
bool IsInputSupported(const NodeArg& node_arg, const std::string& parent_name, const logging::Logger& logger);
183+
bool IsTensorShapeSupported(const NodeArg& node_arg, const std::string& parent_name, const logging::Logger& logger);
184184

185185
// Get a list of groups of supported nodes, each group represents a subgraph supported by WebNN EP.
186186
std::vector<std::vector<NodeIndex>> GetSupportedNodes(const GraphViewer& graph_viewer,

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ bool BaseOpBuilder::IsOpSupported(const InitializedTensorSet& initializers, cons
3434
if (!HasSupportedInputs(node, wnn_limits, logger))
3535
return false;
3636

37-
if (!HasSupportedOutputsImpl(node, wnn_limits, logger))
37+
if (!HasSupportedOutputs(node, wnn_limits, logger))
3838
return false;
3939

4040
if (!HasSupportedOpSet(node, logger))
@@ -47,7 +47,7 @@ bool BaseOpBuilder::HasSupportedInputs(const Node& node, const emscripten::val&
4747
const logging::Logger& logger) const {
4848
const auto node_name = MakeString("Node [", node.Name(), "] type [", node.OpType(), "]");
4949
for (const auto* input : node.InputDefs()) {
50-
if (!IsInputSupported(*input, node_name, logger)) {
50+
if (!IsTensorShapeSupported(*input, node_name, logger)) {
5151
return false;
5252
}
5353
}
@@ -68,6 +68,18 @@ bool BaseOpBuilder::HasSupportedInputsImpl(const Node& node,
6868
return IsDataTypeSupportedByOp(op_type, input_type, wnn_limits, "input", "Input", logger);
6969
}
7070

71+
bool BaseOpBuilder::HasSupportedOutputs(const Node& node, const emscripten::val& wnn_limits,
72+
const logging::Logger& logger) const {
73+
const auto node_name = MakeString("Node [", node.Name(), "] type [", node.OpType(), "]");
74+
for (const auto* output : node.OutputDefs()) {
75+
if (!IsTensorShapeSupported(*output, node_name, logger)) {
76+
return false;
77+
}
78+
}
79+
80+
return HasSupportedOutputsImpl(node, wnn_limits, logger);
81+
}
82+
7183
bool BaseOpBuilder::HasSupportedOutputsImpl(const Node& node,
7284
const emscripten::val& wnn_limits,
7385
const logging::Logger& logger) const {

onnxruntime/core/providers/webnn/builders/impl/base_op_builder.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ class BaseOpBuilder : public IOpBuilder {
5454
private:
5555
bool HasSupportedOpSet(const Node& node, const logging::Logger& logger) const;
5656
bool HasSupportedInputs(const Node& node, const emscripten::val& wnn_limits, const logging::Logger& logger) const;
57+
bool HasSupportedOutputs(const Node& node, const emscripten::val& wnn_limits, const logging::Logger& logger) const;
5758
};
5859

5960
} // namespace webnn

onnxruntime/core/providers/webnn/builders/model_builder.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ Status ModelBuilder::RegisterModelInputOutput(const NodeArg& node_arg, bool is_i
214214
if (!shape.empty()) {
215215
dims.reserve(shape.size());
216216
for (const auto& dim : shape) {
217-
// dim_param free dimensions should have already been excluded by IsInputSupported().
217+
// dim_param free dimensions should have already been excluded by IsTensorShapeSupported().
218218
assert(dim.has_dim_value());
219219
dims.push_back(SafeInt<int32_t>(dim.dim_value()));
220220
}

0 commit comments

Comments
 (0)