Skip to content

Commit

Permalink
[WebNN] Don't skip scalar tensor registration (#22688)
Browse files Browse the repository at this point in the history
ORT will optimize same scalar initializers into one, we should not skip
such scalar registration as a WebNN Constant.
  • Loading branch information
Honry authored Nov 4, 2024
1 parent 777fe79 commit c64459f
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions onnxruntime/core/providers/webnn/builders/model_builder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,15 @@ Status ModelBuilder::RegisterInitializers() {
for (const auto& pair : GetInitializerTensors()) {
const auto& tensor = *pair.second;
const auto& name = tensor.name();
// Optional tensors can be indicated by an empty name, just ignore it.
if (name.empty() || Contains(skipped_initializers_, name))
const auto& shape = tensor.dims();

// Ignore the following tensors:
// 1. Empty tensors: optional tensors can be indicated by an empty name.
// 2. Tensors in skipped_initializers_: These are tensors that are not used as WebNN Constants.
// Note: Scalar tensors are excluded because ONNX Runtime will optimize same scalar initializers into one.
if (name.empty() || (Contains(skipped_initializers_, name) && !shape.empty()))
continue;

const auto& shape = tensor.dims();
std::vector<int32_t> dims;
// When the shape is empty, it is scalar initializer that dims = {};
std::transform(shape.cbegin(), shape.cend(),
Expand Down

0 comments on commit c64459f

Please sign in to comment.