Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WebNN] Ignore empty optional input tensor #19235

Merged
merged 3 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions onnxruntime/core/providers/webnn/builders/helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ std::string GetShapeString(std::vector<T>& shape) {
return shape_info.str();
}

inline std::string GetTensorName(const ConstPointerContainer<std::vector<NodeArg *>>& input_defs, const size_t index) {
Honry marked this conversation as resolved.
Show resolved Hide resolved
return (input_defs.size() > index) ? std::string(input_defs[index]->Name()) : "";
}

inline std::vector<uint32_t> GetVecUint32FromVecInt64(const std::vector<int64_t>& int64_vec) {
std::vector<uint32_t> uint32_vec;
uint32_vec.reserve(int64_vec.size());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,11 +179,9 @@ bool PadOpBuilder::IsOpSupportedImpl(const InitializedTensorSet& initializers,
}
for (size_t i = 1; i < input_defs.size(); i++) {
// Optional tensors (constant_value, axes) can be indicated by an empty name, just ignore it.
if (i > 1 && input_defs[i]->Name().empty()) {
continue;
}
if (!Contains(initializers, input_defs[i]->Name())) {
LOGS(logger, VERBOSE) << "Input [" << input_defs[i]->Name() << "] must be known as initializer";
const std::string input_name = GetTensorName(input_defs, i);
if (!input_name.empty() && !Contains(initializers, input_name)) {
LOGS(logger, VERBOSE) << "Input [" << input_name << "] must be known as initializer";
return false;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,9 @@ bool ReductionOpBuilder::IsOpSupportedImpl(const InitializedTensorSet& initializ
return false;

const auto& op_type = node.OpType();
const auto& axes_name = input_defs[1]->Name();
const std::string axes_name = GetTensorName(input_defs, 1);
// If the optional input 'axes' is provided, it must be an initializer.
if (input_defs.size() > 1 && !axes_name.empty() && !Contains(initializers, axes_name)) {
if (!axes_name.empty() && !Contains(initializers, axes_name)) {
LOGS(logger, VERBOSE) << "Input axes of " << op_type << " must be a constant";
return false;
}
Expand Down
38 changes: 15 additions & 23 deletions onnxruntime/core/providers/webnn/builders/impl/resize_op_builder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -120,16 +120,17 @@ Status ResizeOpBuilder::AddToModelBuilderImpl(ModelBuilder& model_builder,
std::vector<float> scales_hw;
std::vector<int32_t> sizes_hw;
std::vector<int32_t> axes;
std::string scales_name = GetTensorName(input_defs, 2);
const bool is_nhwc = model_builder.GetPreferredLayout() == DataLayout::NHWC;
if (input_defs.size() == 3 || !input_defs[2]->Name().empty()) { // Use scales.
if (!scales_name.empty()) { // Use scales.
ORT_RETURN_IF_NOT(GetResizeScales(initializers, node, scales, logger), "Error getting resize scales");
if (is_nhwc) {
scales_hw = {scales[1], scales[2]};
} else {
scales_hw = {scales[2], scales[3]};
}
options.set("scales", emscripten::val::array(scales_hw));
} else { // We already checked number of inputs in IsOpSupportedImpl.
} else { // Use sizes, we already checked inputs in IsOpSupportedImpl.
std::vector<int64_t> output_sizes;
ORT_RETURN_IF_NOT(GetResizeOutputSizes(initializers, node, output_sizes, logger),
"Error getting resize output_sizes");
Expand Down Expand Up @@ -203,30 +204,21 @@ bool ResizeOpBuilder::IsOpSupportedImpl(const InitializedTensorSet& initializers
}

{ // scales and sizes (if present) must be initializers.
if (input_defs.size() < 3 && node.SinceVersion() == 11) {
LOGS(logger, VERBOSE) << "Input scales of Resize must be known for opset 11";
return false;
}
const std::string scales_name = GetTensorName(input_defs, 2);
const std::string sizes_name = GetTensorName(input_defs, 3);

// scales (scales maybe empty tensor)
bool has_scales = false;
if (input_defs.size() == 3) {
has_scales = !input_defs[2]->Name().empty();
if ((has_scales && !Contains(initializers, input_defs[2]->Name())) ||
(!has_scales && node.SinceVersion() == 11)) {
LOGS(logger, VERBOSE) << "Input scales of Resize must be known";
return false;
}
// scales (scales may be empty tensor)
bool has_scales = !scales_name.empty();
if ((has_scales && !Contains(initializers, scales_name)) || (!has_scales && node.SinceVersion() == 11)) {
LOGS(logger, VERBOSE) << "Input scales of Resize must be known";
return false;
}

// sizes (sizes maybe empty tensor)
bool has_sizes = false;
if (input_defs.size() > 3) {
has_sizes = !input_defs[3]->Name().empty();
if (has_sizes && !Contains(initializers, input_defs[3]->Name())) {
LOGS(logger, VERBOSE) << "Input sizes of Resize must be known";
return false;
}
// sizes (sizes may be empty tensor)
bool has_sizes = !sizes_name.empty();
if (has_sizes && !Contains(initializers, sizes_name)) {
LOGS(logger, VERBOSE) << "Input sizes of Resize must be known";
return false;
}

if (has_scales && has_sizes) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,9 @@ bool SliceOpBuilder::IsOpSupportedImpl(const InitializedTensorSet& initializers,
// Inputs: starts, ends, axes, and steps must be constant initializers if present.
for (size_t i = 1; i < input_defs.size(); i++) {
// Optional tensors (axes, steps) can be indicated by an empty name, just ignore it.
if (i > 3 && input_defs[i]->Name().empty()) {
continue;
}
if (!Contains(initializers, input_defs[i]->Name())) {
LOGS(logger, VERBOSE) << "Input [" << input_defs[i]->Name() << "] of " << op_type
const std::string input_name = GetTensorName(input_defs, i);
if (!input_name.empty() && !Contains(initializers, input_name)) {
LOGS(logger, VERBOSE) << "Input [" << input_name << "] of " << op_type
<< " [" << name << "] must be known as initializer";
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,10 @@ bool SplitOpBuilder::IsOpSupportedImpl(const InitializedTensorSet& initializers,
int32_t axis = helper.Get("axis", 0);
axis = SafeInt<int32_t>(HandleNegativeAxis(axis, rank));

if (input_defs.size() == 2) {
// Inputs contain optional 'split' input.
const auto& split_name = input_defs[1]->Name();
if (!split_name.empty() && !Contains(initializers, split_name)) {
const std::string split_name = GetTensorName(input_defs, 1);
// Inputs contain optional 'split' input.
if (!split_name.empty()) {
if (!Contains(initializers, split_name)) {
LOGS(logger, VERBOSE) << "The split must be a constant initializer.";
return false;
}
Expand All @@ -166,7 +166,7 @@ bool SplitOpBuilder::IsOpSupportedImpl(const InitializedTensorSet& initializers,
LOGS(logger, VERBOSE) << "Sum of the split's values must be equal to the dim value at 'axis' specified.";
return false;
}
} else if (input_defs.size() == 1) {
} else {
if (helper.HasAttr("num_outputs")) {
// Split has 'num_outputs' attribute when opset is 18.
const int32_t num_outputs = helper.Get("num_outputs", 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,9 @@ bool SqueezeUnsqueezeOpBuilder::IsOpSupportedImpl(const InitializedTensorSet& in

// Squeeze/Unsqueeze opset 13 uses input 1 as axes, it needs to be an initializer.
if (node.SinceVersion() >= 13) {
if (input_defs.size() > 1) {
const auto& axes_name = input_defs[1]->Name();
if (!axes_name.empty() && !Contains(initializers, axes_name)) {
const std::string axes_name = GetTensorName(input_defs, 1);
if (!axes_name.empty()) {
if (!Contains(initializers, axes_name)) {
LOGS(logger, ERROR) << "Input axes of " << op_type << " is not present and constant";
return false;
}
Expand Down
Loading