diff --git a/onnxruntime/core/providers/webnn/builders/helper.cc b/onnxruntime/core/providers/webnn/builders/helper.cc index f758d27ea5..31453e0052 100644 --- a/onnxruntime/core/providers/webnn/builders/helper.cc +++ b/onnxruntime/core/providers/webnn/builders/helper.cc @@ -124,13 +124,13 @@ bool IsSupportedDataType(const int32_t data_type, const WebnnDeviceType device_t bool IsValidMultidirectionalBroadcast(std::vector& shape_a, std::vector& shape_b, const logging::Logger& logger) { - int64_t size_a = shape_a.size(); - int64_t size_b = shape_b.size(); - int64_t smaller_size = std::min(size_a, size_b); - for (int64_t i = 0; i < smaller_size; i++) { + size_t size_a = shape_a.size(); + size_t size_b = shape_b.size(); + size_t smaller_size = std::min(size_a, size_b); + for (size_t i = 0; i < smaller_size; i++) { // right alignment - int64_t axis_a = size_a - i - 1; - int64_t axis_b = size_b - i - 1; + size_t axis_a = size_a - i - 1; + size_t axis_b = size_b - i - 1; // Broadcastable tensors must either have each dimension the same size or equal to one. if (shape_a[axis_a] != shape_b[axis_b] && shape_a[axis_a] != 1 && shape_b[axis_b] != 1) { return false; diff --git a/onnxruntime/core/providers/webnn/builders/impl/conv_op_builder.cc b/onnxruntime/core/providers/webnn/builders/impl/conv_op_builder.cc index cb7d27f86f..d830e6465e 100644 --- a/onnxruntime/core/providers/webnn/builders/impl/conv_op_builder.cc +++ b/onnxruntime/core/providers/webnn/builders/impl/conv_op_builder.cc @@ -249,7 +249,7 @@ Status ConvOpBuilder::AddToModelBuilderImpl(ModelBuilder& model_builder, const N std::vector input_shape; ORT_RETURN_IF_NOT(GetShape(*input_defs[0], input_shape, logger), "Cannot get shape"); for (size_t i = 0; i < 2; i++) { - total_padding[i] = strides[i] * (input_shape[i + 1] - 1) + + total_padding[i] = strides[i] * (narrow(input_shape[i + 1]) - 1) + output_padding[i] + ((kernel_shape[i] - 1) * dilations[i] + 1) - output_shape[i]; } pads[0] = total_padding[0] - (total_padding[0] / 2); diff --git a/onnxruntime/core/providers/webnn/builders/impl/normalization_op_builder.cc b/onnxruntime/core/providers/webnn/builders/impl/normalization_op_builder.cc index 15c9b4acbe..6a9ad76982 100644 --- a/onnxruntime/core/providers/webnn/builders/impl/normalization_op_builder.cc +++ b/onnxruntime/core/providers/webnn/builders/impl/normalization_op_builder.cc @@ -119,7 +119,7 @@ Status NormalizationOpBuilder::AddToModelBuilderImpl(ModelBuilder& model_builder if (op_type == "LayerNormalization") { int64_t axis = helper.Get("axis", -1); axis = HandleNegativeAxis(axis, rank); - std::vector axes(rank - axis); + std::vector axes(rank - narrow(axis)); std::iota(axes.begin(), axes.end(), axis); options.set("axes", emscripten::val::array(axes)); output = model_builder.GetBuilder().call("meanVarianceNormalization", input, options); 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 54352cf8a7..eb271f3c49 100644 --- a/onnxruntime/core/providers/webnn/builders/impl/reduction_op_builder.cc +++ b/onnxruntime/core/providers/webnn/builders/impl/reduction_op_builder.cc @@ -2,6 +2,7 @@ // Copyright (c) Intel Corporation. All rights reserved. // Licensed under the MIT License. +#include "core/common/safeint.h" #include "core/providers/common.h" #include "core/providers/shared/utils/utils.h" #include "core/providers/webnn/builders/helper.h" @@ -71,7 +72,7 @@ Status ReductionOpBuilder::AddToModelBuilderImpl(ModelBuilder& model_builder, const auto axes_data_span = axes_initializer.DataAsSpan(); std::transform( axes_data_span.begin(), axes_data_span.end(), std::back_inserter(axes_data), - [input_rank](int64_t axis) -> int32_t { return HandleNegativeAxis(axis, input_rank); }); + [input_rank](int64_t axis) -> int32_t { return SafeInt(HandleNegativeAxis(axis, input_rank)); }); } else { if (noop_with_empty_axes) { // When axes is empty and this attribute is set to true, input tensor will not be reduced. @@ -85,7 +86,7 @@ Status ReductionOpBuilder::AddToModelBuilderImpl(ModelBuilder& model_builder, auto axes = helper.Get("axes", std::vector{}); std::transform( axes.begin(), axes.end(), std::back_inserter(axes_data), - [input_rank](int64_t axis) -> int32_t { return HandleNegativeAxis(axis, input_rank); }); + [input_rank](int64_t axis) -> int32_t { return SafeInt(HandleNegativeAxis(axis, input_rank)); }); } } if (axes_data.size() > 0) { diff --git a/onnxruntime/core/providers/webnn/builders/impl/softmax_op_builder.cc b/onnxruntime/core/providers/webnn/builders/impl/softmax_op_builder.cc index e3f481db65..e7e3cee21c 100644 --- a/onnxruntime/core/providers/webnn/builders/impl/softmax_op_builder.cc +++ b/onnxruntime/core/providers/webnn/builders/impl/softmax_op_builder.cc @@ -2,6 +2,7 @@ // Copyright (c) Intel Corporation. All rights reserved. // Licensed under the MIT License. +#include "core/common/safeint.h" #include "core/providers/common.h" #include "core/providers/shared/utils/utils.h" #include "core/providers/webnn/builders/helper.h" @@ -36,7 +37,7 @@ Status SoftmaxOpBuilder::AddToModelBuilderImpl(ModelBuilder& model_builder, const auto input_size = input_shape.size(); // WebNN Softmax only support 2d input shape, reshape input to 2d. if (input_size != 2) { - int32_t new_shape_0 = input_shape.data()[0]; + int32_t new_shape_0 = SafeInt(input_shape.data()[0]); for (size_t i = 1; i < input_size - 1; i++) { new_shape_0 *= input_shape.data()[i]; } 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 ff94496133..dfedce50d0 100644 --- a/onnxruntime/core/providers/webnn/builders/impl/split_op_builder.cc +++ b/onnxruntime/core/providers/webnn/builders/impl/split_op_builder.cc @@ -41,9 +41,9 @@ Status SplitOpBuilder::AddToModelBuilderImpl(ModelBuilder& model_builder, emscripten::val options = emscripten::val::object(); NodeAttrHelper helper(node); - int64_t axis = helper.Get("axis", 0); - axis = HandleNegativeAxis(axis, rank); - options.set("axis", static_cast(axis)); + auto axis = helper.Get("axis", 0); + axis = SafeInt(HandleNegativeAxis(axis, rank)); + options.set("axis", axis); if (input_defs.size() == 2) { // Inputs contains optional 'split' input @@ -59,7 +59,7 @@ Status SplitOpBuilder::AddToModelBuilderImpl(ModelBuilder& model_builder, "The size of outputs must be equal to the size of 'split' input."); } else { if (helper.HasAttr("num_outputs")) { - const int64_t num_outputs = helper.Get("num_outputs", 1); + const auto num_outputs = helper.Get("num_outputs", 1); ORT_RETURN_IF_NOT(num_outputs > 0, "The 'num_outputs' must be a positive integer."); if (input_shape[axis] % num_outputs == 0) { // The 'num_outputs' evenly divide the dim value at 'axis' specified. @@ -95,7 +95,7 @@ Status SplitOpBuilder::AddToModelBuilderImpl(ModelBuilder& model_builder, "The size of outputs must be equal to the count of output nodes."); } } - for (int64_t i = 0, count = output_array["length"].as(); i < count; i++) { + for (size_t i = 0, count = output_array["length"].as(); i < count; i++) { model_builder.AddOperand(node.OutputDefs()[i]->Name(), std::move(output_array[i])); } return Status::OK(); @@ -121,8 +121,8 @@ bool SplitOpBuilder::IsOpSupportedImpl(const InitializedTensorSet& initializers, const auto rank = input_shape.size(); NodeAttrHelper helper(node); - int64_t axis = helper.Get("axis", 0); - axis = HandleNegativeAxis(axis, rank); + auto axis = helper.Get("axis", 0); + axis = SafeInt(HandleNegativeAxis(axis, rank)); if (input_defs.size() == 2) { // Inputs contains optional 'split' input @@ -143,7 +143,7 @@ bool SplitOpBuilder::IsOpSupportedImpl(const InitializedTensorSet& initializers, return false; } int64_t sum = 0; - for (int64_t i = 0; i < split.size(); i++) { + for (size_t i = 0; i < split.size(); i++) { if (split[i] < 0) { LOGS(logger, VERBOSE) << "Value of split should be greater than or equal to 0."; return false; 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 e9ad02da7f..7d31fd6a68 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 @@ -2,6 +2,7 @@ // Copyright (c) Intel Corporation. All rights reserved. // Licensed under the MIT License. +#include "core/common/safeint.h" #include "core/providers/common.h" #include "core/providers/shared/utils/utils.h" #include "core/providers/webnn/builders/helper.h" @@ -65,7 +66,7 @@ Status SqueezeUnsqueezeOpBuilder::AddToModelBuilderImpl(ModelBuilder& model_buil const auto output_rank = input_rank + axes_data_span.size(); std::transform( axes_data_span.begin(), axes_data_span.end(), std::back_inserter(axes_data), - [output_rank](int64_t axis) -> int32_t { return HandleNegativeAxis(axis, output_rank); }); + [output_rank](int64_t axis) -> int32_t { return SafeInt(HandleNegativeAxis(axis, output_rank)); }); } else { NodeAttrHelper helper(node); if (helper.HasAttr("axes")) { @@ -73,7 +74,7 @@ Status SqueezeUnsqueezeOpBuilder::AddToModelBuilderImpl(ModelBuilder& model_buil const auto output_rank = input_rank + axes.size(); std::transform( axes.begin(), axes.end(), std::back_inserter(axes_data), - [output_rank](int64_t axis) -> int32_t { return HandleNegativeAxis(axis, output_rank); }); + [output_rank](int64_t axis) -> int32_t { return SafeInt(HandleNegativeAxis(axis, output_rank)); }); } }