From 6352cb38b291daaef6151c5520e5778310ce6378 Mon Sep 17 00:00:00 2001 From: shiyi Date: Wed, 30 Oct 2024 23:18:21 +0800 Subject: [PATCH] [WebNN EP] Check if the tensor shape has 0 dimension (#22573) WebNN doesn't support empty tensor. --- onnxruntime/core/providers/webnn/builders/helper.cc | 4 ++++ .../core/providers/webnn/builders/impl/expand_op_builder.cc | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/onnxruntime/core/providers/webnn/builders/helper.cc b/onnxruntime/core/providers/webnn/builders/helper.cc index 4b39e03ffc788..3c9faef9998e0 100644 --- a/onnxruntime/core/providers/webnn/builders/helper.cc +++ b/onnxruntime/core/providers/webnn/builders/helper.cc @@ -89,6 +89,10 @@ bool IsTensorShapeSupported(const NodeArg& node_arg, const std::string& parent_n << "use sessionOptions.FreeDimensionOverrides to set a fixed shape: " << node_arg_name; return false; } + if (dim.dim_value() == 0) { + LOGS(logger, VERBOSE) << "The shape of [" << node_arg_name << "] has 0 dimension which is not supported by WebNN"; + return false; + } } return true; diff --git a/onnxruntime/core/providers/webnn/builders/impl/expand_op_builder.cc b/onnxruntime/core/providers/webnn/builders/impl/expand_op_builder.cc index 5e99551fe6e7d..f5e1f59602c5d 100644 --- a/onnxruntime/core/providers/webnn/builders/impl/expand_op_builder.cc +++ b/onnxruntime/core/providers/webnn/builders/impl/expand_op_builder.cc @@ -88,6 +88,10 @@ bool ExpandOpBuilder::IsOpSupportedImpl(const InitializedTensorSet& initializers LOGS(logger, VERBOSE) << "Cannot get shape."; return false; } + if (std::any_of(new_shape.begin(), new_shape.end(), [](int64_t dimension) { return dimension == 0; })) { + LOGS(logger, VERBOSE) << "WebNN expand does not support new shape with 0 dimension."; + return false; + } std::vector input_shape; if (!GetShape(*input_defs[0], input_shape, logger)) {