Skip to content

Commit c64459f

Browse files
authored
[WebNN] Don't skip scalar tensor registration (microsoft#22688)
ORT will optimize same scalar initializers into one, we should not skip such scalar registration as a WebNN Constant.
1 parent 777fe79 commit c64459f

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

onnxruntime/core/providers/webnn/builders/model_builder.cc

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,15 @@ Status ModelBuilder::RegisterInitializers() {
8888
for (const auto& pair : GetInitializerTensors()) {
8989
const auto& tensor = *pair.second;
9090
const auto& name = tensor.name();
91-
// Optional tensors can be indicated by an empty name, just ignore it.
92-
if (name.empty() || Contains(skipped_initializers_, name))
91+
const auto& shape = tensor.dims();
92+
93+
// Ignore the following tensors:
94+
// 1. Empty tensors: optional tensors can be indicated by an empty name.
95+
// 2. Tensors in skipped_initializers_: These are tensors that are not used as WebNN Constants.
96+
// Note: Scalar tensors are excluded because ONNX Runtime will optimize same scalar initializers into one.
97+
if (name.empty() || (Contains(skipped_initializers_, name) && !shape.empty()))
9398
continue;
9499

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

0 commit comments

Comments
 (0)