Skip to content

Commit

Permalink
[WebNN EP] Make sure optional input is provided (microsoft#19686)
Browse files Browse the repository at this point in the history
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:
<img width="514" alt="image"
src="https://github.com/microsoft/onnxruntime/assets/3271201/cc3b06fe-46b9-4ee7-aca5-157bdf112856">
  • Loading branch information
Honry authored and Zhenze Wang committed Mar 7, 2024
1 parent 99f8f91 commit ed01070
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<int64_t> axes;
const auto& axes_tensor = *initializers.at(input_defs[3]->Name());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ Status SplitOpBuilder::AddToModelBuilderImpl(ModelBuilder& model_builder,
axis = SafeInt<int32_t>(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<int32_t> splits;
const auto& initializers(model_builder.GetInitializerTensors());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ Status SqueezeUnsqueezeOpBuilder::AddToModelBuilderImpl(ModelBuilder& model_buil
std::vector<int32_t> 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());
Expand Down

0 comments on commit ed01070

Please sign in to comment.