From 1ce5bfb0ecc94a4a98eb093a53cd248ab6b7167b Mon Sep 17 00:00:00 2001 From: Wanming Lin Date: Thu, 7 Mar 2024 08:19:59 +0800 Subject: [PATCH] [WebNN EP] Make sure optional input is provided (#19686) Some optional input is presented as empty string, we should not only check if the input size is correct, but also check if the optional input is not empty. e.g. Pad node has empty optional input in sam-b-encoder.onnx model: image --- .../core/providers/webnn/builders/impl/pad_op_builder.cc | 6 +++--- .../providers/webnn/builders/impl/reduction_op_builder.cc | 2 +- .../core/providers/webnn/builders/impl/split_op_builder.cc | 2 +- .../webnn/builders/impl/squeeze_unsqueeze_op_builder.cc | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/onnxruntime/core/providers/webnn/builders/impl/pad_op_builder.cc b/onnxruntime/core/providers/webnn/builders/impl/pad_op_builder.cc index 52b5518857773..9852db0abc9d2 100644 --- a/onnxruntime/core/providers/webnn/builders/impl/pad_op_builder.cc +++ b/onnxruntime/core/providers/webnn/builders/impl/pad_op_builder.cc @@ -88,15 +88,15 @@ Status PadOpBuilder::AddToModelBuilderImpl(ModelBuilder& model_builder, const auto& pads_tensor = *initializers.at(input_defs[1]->Name()); ORT_RETURN_IF_NOT(ReadIntArrayFrom1DTensor(pads_tensor, pads, logger), "Error while read pads tensor"); - // Constant value and axes are optional. - if (input_defs.size() >= 3) { + // Constant value and axes are optional. Make sure they are not empty. + if (!GetTensorName(input_defs, 2).empty()) { const auto value_tensor = *initializers.at(input_defs[2]->Name()); emscripten::val value = emscripten::val::object(); ORT_RETURN_IF_NOT(ReadScalarTensorData(value_tensor, value, logger), "Cannot read constant value"); options.set("value", value); } - if (input_defs.size() == 4) { + if (!GetTensorName(input_defs, 3).empty()) { const auto input_rank = input_shape.size(); std::vector axes; const auto& axes_tensor = *initializers.at(input_defs[3]->Name()); diff --git a/onnxruntime/core/providers/webnn/builders/impl/reduction_op_builder.cc b/onnxruntime/core/providers/webnn/builders/impl/reduction_op_builder.cc index f446a7b81d1c0..c0954f7cf6fb1 100644 --- a/onnxruntime/core/providers/webnn/builders/impl/reduction_op_builder.cc +++ b/onnxruntime/core/providers/webnn/builders/impl/reduction_op_builder.cc @@ -65,7 +65,7 @@ Status ReductionOpBuilder::AddToModelBuilderImpl(ModelBuilder& model_builder, if (opset >= 18 || (op_type == "ReduceSum" && opset >= 13)) { // 'axes' is an optional input. const auto noop_with_empty_axes = helper.Get("noop_with_empty_axes", 0); - if (input_defs.size() > 1) { + if (!GetTensorName(input_defs, 1).empty()) { // Optional input axes is provided, use axes initializer data. const auto& initializers(model_builder.GetInitializerTensors()); const auto& axes_tensor = *initializers.at(input_defs[1]->Name()); diff --git a/onnxruntime/core/providers/webnn/builders/impl/split_op_builder.cc b/onnxruntime/core/providers/webnn/builders/impl/split_op_builder.cc index 91f21b196be54..9819e4ce7ac5b 100644 --- a/onnxruntime/core/providers/webnn/builders/impl/split_op_builder.cc +++ b/onnxruntime/core/providers/webnn/builders/impl/split_op_builder.cc @@ -57,7 +57,7 @@ Status SplitOpBuilder::AddToModelBuilderImpl(ModelBuilder& model_builder, axis = SafeInt(HandleNegativeAxis(axis, rank)); options.set("axis", axis); - if (input_defs.size() == 2) { + if (!GetTensorName(input_defs, 1).empty()) { // Inputs contains optional 'split' input std::vector splits; const auto& initializers(model_builder.GetInitializerTensors()); diff --git a/onnxruntime/core/providers/webnn/builders/impl/squeeze_unsqueeze_op_builder.cc b/onnxruntime/core/providers/webnn/builders/impl/squeeze_unsqueeze_op_builder.cc index 15149bd8fe821..8e6feb62fa8c4 100644 --- a/onnxruntime/core/providers/webnn/builders/impl/squeeze_unsqueeze_op_builder.cc +++ b/onnxruntime/core/providers/webnn/builders/impl/squeeze_unsqueeze_op_builder.cc @@ -58,7 +58,7 @@ Status SqueezeUnsqueezeOpBuilder::AddToModelBuilderImpl(ModelBuilder& model_buil std::vector axes_data; auto rank = input_rank; - if (node.SinceVersion() >= 13 && input_defs.size() > 1) { + if (node.SinceVersion() >= 13 && !GetTensorName(input_defs, 1).empty()) { // Input axes is provided, use axes initializer data. const auto& initializers = model_builder.GetInitializerTensors(); const auto& axes_tensor = *initializers.at(input_defs[1]->Name());