Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion onnxruntime/core/providers/webnn/builders/helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ inline std::string GetTensorName(const ConstPointerContainer<std::vector<NodeArg
}

template <typename T>
inline std::vector<T> GetNarrowedIntfromInt64(gsl::span<const int64_t> int64_vec) {
inline std::vector<T> GetNarrowedIntFromInt64(gsl::span<const int64_t> int64_vec) {
std::vector<T> vec;
vec.reserve(int64_vec.size());
std::transform(int64_vec.begin(), int64_vec.end(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ namespace webnn {
/*
ScaledDotProductAttention Subgraph: The basis for MultiHeadAttention and GroupQueryAttention
inputs: query, key, value, scale, attention mask, and reshape_output_shape (for reshape)
Abbreviatios: B is batch_size, S is query sequence_length, kv_S is key/value sequence length,
N is number of attention heads, H is head size, W is hidden_size
Abbreviations: B is batch_size, S is query sequence_length, kv_S is key/value sequence length,
N is number of attention heads, H is head size, W is hidden_size

query key
| |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class BaseOpBuilder : public IOpBuilder {
// with opset version 7 or above for opset domain 'ai.onnx'.
// WebNN EP ignores node support for opset less than 7 by
// default as which will be fallback earlier by ONNX Runtime.
// We still set the mininal supported opset to 1 as we couldn't
// We still set the minimal supported opset to 1 as we couldn't
// get the model opset version at this stage.
virtual int GetMinSupportedOpSet(const Node& /* node */) const { return 1; }
virtual int GetMaxSupportedOpSet(const Node& /* node */) const { return 23; }
Expand Down
40 changes: 20 additions & 20 deletions onnxruntime/core/providers/webnn/builders/impl/conv_op_builder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ common::Status SetConvBaseOptions(ModelBuilder& model_builder,
if (output_padding.size() == 1 && is_conv1d) {
output_padding.push_back(0);
}
options.set("outputPadding", emscripten::val::array(GetNarrowedIntfromInt64<uint32_t>(output_padding)));
options.set("outputPadding", emscripten::val::array(GetNarrowedIntFromInt64<uint32_t>(output_padding)));

// If output shape is explicitly provided, compute the pads.
// Otherwise compute the output shape, as well as the pads if the auto_pad attribute is SAME_UPPER/SAME_LOWER.
Expand All @@ -87,7 +87,7 @@ common::Status SetConvBaseOptions(ModelBuilder& model_builder,
auto_pad_type, pads_out, output_shape, !is_nhwc));

if (output_shape[0] != -1 && output_shape[1] != -1) {
options.set("outputSizes", emscripten::val::array(GetNarrowedIntfromInt64<uint32_t>(output_shape)));
options.set("outputSizes", emscripten::val::array(GetNarrowedIntFromInt64<uint32_t>(output_shape)));
}
pads = pads_out;
} else {
Expand All @@ -97,13 +97,13 @@ common::Status SetConvBaseOptions(ModelBuilder& model_builder,

const auto group = helper.Get("group", static_cast<uint32_t>(1));
options.set("groups", group);
options.set("strides", emscripten::val::array(GetNarrowedIntfromInt64<uint32_t>(strides)));
options.set("dilations", emscripten::val::array(GetNarrowedIntfromInt64<uint32_t>(dilations)));
options.set("strides", emscripten::val::array(GetNarrowedIntFromInt64<uint32_t>(strides)));
options.set("dilations", emscripten::val::array(GetNarrowedIntFromInt64<uint32_t>(dilations)));

// Permute the ONNX's pads, which is [beginning_height, beginning_width, ending_height, ending_width],
// while WebNN's padding is [beginning_height, ending_height, beginning_width, ending_width].
const std::vector<int64_t> padding{pads[0], pads[2], pads[1], pads[3]};
options.set("padding", emscripten::val::array(GetNarrowedIntfromInt64<uint32_t>(padding)));
options.set("padding", emscripten::val::array(GetNarrowedIntFromInt64<uint32_t>(padding)));

// Add bias if present.
if (input_defs.size() > 2 && op_type != "ConvInteger") {
Expand All @@ -123,7 +123,7 @@ Status AddInitializerInNewLayout(ModelBuilder& model_builder,

const auto& shape = tensor.dims();
std::vector<uint32_t> dims =
GetNarrowedIntfromInt64<uint32_t>(std::vector<int64_t>(std::begin(shape), std::end(shape)));
GetNarrowedIntFromInt64<uint32_t>(std::vector<int64_t>(std::begin(shape), std::end(shape)));

if (is_conv1d) {
// Support conv1d by prepending a 1 size dimension.
Expand Down Expand Up @@ -172,21 +172,21 @@ Status AddInitializerInNewLayout(ModelBuilder& model_builder,
h * w_t +
w;

uint32_t nnapi_idx;
uint32_t wnn_idx;
if (is_conv == 1) { // L_0231
nnapi_idx = out * h_t * w_t * in_t +
h * w_t * in_t +
w * in_t +
in;
wnn_idx = out * h_t * w_t * in_t +
h * w_t * in_t +
w * in_t +
in;
} else { // L_1230 for depthwise conv weight
nnapi_idx = in * h_t * w_t * out_t +
h * w_t * out_t +
w * out_t +
out;
wnn_idx = in * h_t * w_t * out_t +
h * w_t * out_t +
w * out_t +
out;
}

for (size_t i = 0; i < element_size; i++) {
buffer[element_size * nnapi_idx + i] = src[element_size * onnx_idx + i];
buffer[element_size * wnn_idx + i] = src[element_size * onnx_idx + i];
}
}
}
Expand Down Expand Up @@ -234,7 +234,7 @@ Status ConvOpBuilder::AddToModelBuilderImpl(ModelBuilder& model_builder, const N
} else {
input_shape.push_back(1);
}
std::vector<uint32_t> new_shape = GetNarrowedIntfromInt64<uint32_t>(input_shape);
std::vector<uint32_t> new_shape = GetNarrowedIntFromInt64<uint32_t>(input_shape);
common_options.set("label", node.Name() + "_reshape_input");
input = model_builder.GetBuilder().call<emscripten::val>("reshape", input,
emscripten::val::array(new_shape), common_options);
Expand Down Expand Up @@ -283,7 +283,7 @@ Status ConvOpBuilder::AddToModelBuilderImpl(ModelBuilder& model_builder, const N
// Reshape weight to 4D for conv1d.
if (!is_nhwc || !is_constant_weight) {
// The weight_shape has been appended 1's, reshape weight operand.
std::vector<uint32_t> new_shape = GetNarrowedIntfromInt64<uint32_t>(weight_shape);
std::vector<uint32_t> new_shape = GetNarrowedIntFromInt64<uint32_t>(weight_shape);
common_options.set("label", node.Name() + "_reshape_filter");
filter = model_builder.GetBuilder().call<emscripten::val>("reshape",
filter,
Expand Down Expand Up @@ -338,7 +338,7 @@ Status ConvOpBuilder::AddToModelBuilderImpl(ModelBuilder& model_builder, const N
std::vector<int64_t> w_zero_point_shape;
ORT_RETURN_IF_NOT(GetShape(*input_defs[3], w_zero_point_shape, logger), "Cannot get shape of w_zero_point");
w_scale = model_builder.CreateOrGetConstant<float>(ONNX_NAMESPACE::TensorProto_DataType_FLOAT, 1.0f,
GetNarrowedIntfromInt64<uint32_t>(w_zero_point_shape));
GetNarrowedIntFromInt64<uint32_t>(w_zero_point_shape));
} else {
w_zero_point = model_builder.CreateOrGetConstant<uint8_t>(x_type, 0);
w_scale = x_scale;
Expand All @@ -363,7 +363,7 @@ Status ConvOpBuilder::AddToModelBuilderImpl(ModelBuilder& model_builder, const N
const auto& output_defs = node.OutputDefs();
std::vector<int64_t> output_shape;
ORT_RETURN_IF_NOT(GetShape(*output_defs[0], output_shape, logger), "Cannot get output shape");
std::vector<uint32_t> new_shape = GetNarrowedIntfromInt64<uint32_t>(output_shape);
std::vector<uint32_t> new_shape = GetNarrowedIntFromInt64<uint32_t>(output_shape);
common_options.set("label", node.Name() + "_reshape_output");
output = model_builder.GetBuilder().call<emscripten::val>("reshape",
output,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
if (output_defs.size() > 1) {
std::vector<int64_t> mask_shape;
ORT_RETURN_IF_NOT(GetShape(*output_defs[1], mask_shape, logger), "Cannot get mask output's shape");
std::vector<uint32_t> dims = GetNarrowedIntfromInt64<uint32_t>(mask_shape);
std::vector<uint32_t> dims = GetNarrowedIntFromInt64<uint32_t>(mask_shape);

Check warning on line 56 in onnxruntime/core/providers/webnn/builders/impl/dropout_op_builder.cc

View workflow job for this annotation

GitHub Actions / Optional Lint C++

[cpplint] reported by reviewdog 🐶 Add #include <vector> for vector<> [build/include_what_you_use] [4] Raw Output: onnxruntime/core/providers/webnn/builders/impl/dropout_op_builder.cc:56: Add #include <vector> for vector<> [build/include_what_you_use] [4]
emscripten::val one_constant = model_builder.CreateOrGetConstant<uint8_t>(
ONNX_NAMESPACE::TensorProto_DataType_BOOL, 1, dims);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ Status ExpandOpBuilder::AddToModelBuilderImpl(ModelBuilder& model_builder,
emscripten::val options = emscripten::val::object();
options.set("label", node.Name());

emscripten::val output_shape_arr = emscripten::val::array(GetNarrowedIntfromInt64<uint32_t>(output_shape));
emscripten::val output_shape_arr = emscripten::val::array(GetNarrowedIntFromInt64<uint32_t>(output_shape));
emscripten::val output = model_builder.GetBuilder().call<emscripten::val>("expand", input, output_shape_arr, options);
model_builder.AddOperand(node.OutputDefs()[0]->Name(), std::move(output));
return Status::OK();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,14 @@ Status GemmOpBuilder::AddToModelBuilderImpl(ModelBuilder& model_builder, const N
// If the input A is 1-D, it is promoted to a matrix by prepending a 1 to its dimensions.
if (a_shape.size() == 1) {
a_shape.insert(a_shape.begin(), 1);
emscripten::val a_shape_arr = emscripten::val::array(GetNarrowedIntfromInt64<uint32_t>(a_shape));
emscripten::val a_shape_arr = emscripten::val::array(GetNarrowedIntFromInt64<uint32_t>(a_shape));
common_options.set("label", node.Name() + "_reshape_a");
a = model_builder.GetBuilder().call<emscripten::val>("reshape", a, a_shape_arr, common_options);
}
// If the input B is 1-D, it is promoted to a matrix by appending a 1 to its dimensions.
if (b_shape.size() == 1) {
b_shape.push_back(1);
emscripten::val b_shape_arr = emscripten::val::array(GetNarrowedIntfromInt64<uint32_t>(b_shape));
emscripten::val b_shape_arr = emscripten::val::array(GetNarrowedIntFromInt64<uint32_t>(b_shape));
common_options.set("label", node.Name() + "_reshape_b");
b = model_builder.GetBuilder().call<emscripten::val>("reshape", b, b_shape_arr, common_options);
}
Expand All @@ -74,7 +74,7 @@ Status GemmOpBuilder::AddToModelBuilderImpl(ModelBuilder& model_builder, const N
// If A or B input is 1-D, we need to reshape the output back to its original shape.
if (a_shape.size() == 1 || b_shape.size() == 1) {
common_options.set("label", node.Name() + "_reshape_output");
emscripten::val output_shape_arr = emscripten::val::array(GetNarrowedIntfromInt64<uint32_t>(output_shape));
emscripten::val output_shape_arr = emscripten::val::array(GetNarrowedIntFromInt64<uint32_t>(output_shape));
output = model_builder.GetBuilder().call<emscripten::val>("reshape",
output,
output_shape_arr,
Expand All @@ -95,7 +95,7 @@ Status GemmOpBuilder::AddToModelBuilderImpl(ModelBuilder& model_builder, const N
// The scale input should have the same shape as the zero point input.
a_scale = model_builder.CreateOrGetConstant<float>(ONNX_NAMESPACE::TensorProto_DataType_FLOAT,
1.0f,
GetNarrowedIntfromInt64<uint32_t>(a_zero_point_shape));
GetNarrowedIntFromInt64<uint32_t>(a_zero_point_shape));
} else {
// If a_zero_point is not provided, create default scalar for zero_point and scale inputs.
a_zero_point = model_builder.CreateOrGetConstant<uint8_t>(a_type, 0);
Expand All @@ -115,7 +115,7 @@ Status GemmOpBuilder::AddToModelBuilderImpl(ModelBuilder& model_builder, const N
ORT_RETURN_IF_NOT(GetShape(*input_defs[3], b_zero_point_shape, logger), "Cannot get shape of b_zero_point");
b_scale = model_builder.CreateOrGetConstant<float>(ONNX_NAMESPACE::TensorProto_DataType_FLOAT,
1.0f,
GetNarrowedIntfromInt64<uint32_t>(b_zero_point_shape));
GetNarrowedIntFromInt64<uint32_t>(b_zero_point_shape));
} else {
b_zero_point = model_builder.CreateOrGetConstant<uint8_t>(a_type, 0);
b_scale = model_builder.CreateOrGetConstant<float>(ONNX_NAMESPACE::TensorProto_DataType_FLOAT, 1.0f);
Expand Down Expand Up @@ -143,7 +143,7 @@ Status GemmOpBuilder::AddToModelBuilderImpl(ModelBuilder& model_builder, const N
// If A or B input is 1-D, we need to reshape the output back to its original shape.
if (a_shape.size() == 1 || b_shape.size() == 1) {
common_options.set("label", node.Name() + "_reshape_output");
emscripten::val output_shape_arr = emscripten::val::array(GetNarrowedIntfromInt64<uint32_t>(output_shape));
emscripten::val output_shape_arr = emscripten::val::array(GetNarrowedIntFromInt64<uint32_t>(output_shape));
output = model_builder.GetBuilder().call<emscripten::val>("reshape",
output,
output_shape_arr,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ std::vector<int32_t> repeat_sequence(int32_t sequence_length, int32_t kv_num_hea
}

/** GroupQueryAttention SubGraph.
Abbreviatios: B is batch_size, S is sequence_length, W is hidden_size, P is past_sequence_length
N is number of attention heads, kv_N is number of attention heads for kv, H is head size
G is group size, and G=N/kv_N, W=N*H, h=Sqrt(H).
Abbreviations: B is batch_size, S is sequence_length, W is hidden_size, P is past_sequence_length
N is number of attention heads, kv_N is number of attention heads for kv, H is head size
G is group size, and G=N/kv_N, W=N*H, h=Sqrt(H).
GQA inputs: query, key, value, past_key, past_value, seqlens_k, total_sequence_length
Notes: cos_cache, sin_cache inputs are not supported. If the data type of the inputs (qkv and past kv) is float16,
we cast them to float32 to ensure data precision.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ class MultiHeadAttentionOpBuilder : public BaseOpBuilder {
};

/** MultiHeadAttention SubGraph.
Abbreviatios: B is batch_size, S is sequence_length, W is hidden_size
N is number of attention heads, H is head size
Abbreviations: B is batch_size, S is sequence_length, W is hidden_size
N is number of attention heads, H is head size
Notes: If the datatype of the inputs (qkv and past kv) is float16, we cast them to float32 to ensure data precision.

query key value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ Status NormalizationOpBuilder::AddToModelBuilderImpl(ModelBuilder& model_builder
output = model_builder.GetBuilder().call<emscripten::val>("instanceNormalization", input, options);
// Reshape back to the original output shape for 3D input.
if (input_shape.size() != 4) {
std::vector<uint32_t> output_shape = GetNarrowedIntfromInt64<uint32_t>(input_shape);
std::vector<uint32_t> output_shape = GetNarrowedIntFromInt64<uint32_t>(input_shape);
emscripten::val reshape_output_options = emscripten::val::object();
reshape_output_options.set("label", node.Name() + "reshape_output");
output = model_builder.GetBuilder().call<emscripten::val>("reshape",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ Status PoolOpBuilder::AddToModelBuilderImpl(ModelBuilder& model_builder,
}

// Add Padding.
// Usually using autopadding is more efficient than using explicit padding.
// Usually using auto padding is more efficient than using explicit padding.
// Try to see if we can map explicit padding to auto padding.
const auto onnx_strides = helper.Get("strides", std::vector<int64_t>{1, 1});
const auto onnx_pads = helper.Get("pads", std::vector<int64_t>{0, 0, 0, 0});
Expand All @@ -94,7 +94,7 @@ Status PoolOpBuilder::AddToModelBuilderImpl(ModelBuilder& model_builder,
auto_pad_type,
pads_out,
!is_nhwc));
pads = GetNarrowedIntfromInt64<uint32_t>(pads_out);
pads = GetNarrowedIntFromInt64<uint32_t>(pads_out);
}
// Permute the ONNX's pads, which is [beginning_height, beginning_width, ending_height, ending_width],
// while WebNN's padding is [beginning_height, ending_height, beginning_width, ending_width].
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ Status QDQOpBuilder::AddToModelBuilderImpl(ModelBuilder& model_builder,
if (!has_zero_point) {
if (zero_point_shape.empty()) {
// zero_point has the same shape as the scale tensor.
zero_point_shape = GetNarrowedIntfromInt64<uint32_t>(scale_shape);
zero_point_shape = GetNarrowedIntFromInt64<uint32_t>(scale_shape);
}
// Create a zero constant with the same shape as the scale tensor.
// The zero value has been pre-processed in the CreateOrGetConstant function,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,15 +223,15 @@ Status ResizeOpBuilder::AddToModelBuilderImpl(ModelBuilder& model_builder,
ORT_RETURN_IF_NOT(GetResizeSizesAndAxes(model_builder.GetGraphViewer(), node, sizes, axes, is_nhwc,
input_shape, logger),
"Error getting Resize sizes");
webnn_sizes = GetNarrowedIntfromInt64<uint32_t>(sizes);
webnn_sizes = GetNarrowedIntFromInt64<uint32_t>(sizes);
options.set("sizes", emscripten::val::array(webnn_sizes));
} else {
ORT_RETURN_IF_NOT(GetResizeScalesAndAxes(model_builder.GetGraphViewer(), node, scales, axes, is_nhwc, logger),
"Error getting Resize scales");
options.set("scales", emscripten::val::array(scales));
}

std::vector<uint32_t> webnn_axes = GetNarrowedIntfromInt64<uint32_t>(axes);
std::vector<uint32_t> webnn_axes = GetNarrowedIntFromInt64<uint32_t>(axes);
options.set("axes", emscripten::val::array(webnn_axes));

emscripten::val input = model_builder.GetOperand(input_defs[0]->Name());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -334,13 +334,13 @@ Status RotaryEmbeddingOpBuilder::AddToModelBuilderImpl(ModelBuilder& model_build
if (input_is_4d) {
// The output is in 4D shape, we need to transpose it back to the original shape.
// Reuse the transpose_options' permutation because the original permutation also
// happens to be its own inverse. (inserve({0, 2, 1, 3} == {0, 2, 1, 3})
// happens to be its own inverse. (inverse({0, 2, 1, 3} == {0, 2, 1, 3})
transpose_options.set("label", node_name + "_transpose_output");
output = wnn_builder.call<emscripten::val>("transpose", output, transpose_options);
} else {
// The output is in 3D shape, we need to reshape it back to the original shape.
// The output shape is same as the input shape.
const std::vector<uint32_t> output_shape = GetNarrowedIntfromInt64<uint32_t>(input_shape);
const std::vector<uint32_t> output_shape = GetNarrowedIntFromInt64<uint32_t>(input_shape);
emscripten::val reshape_output_options = emscripten::val::object();
reshape_output_options.set("label", node_name + "_reshape_output");
output = wnn_builder.call<emscripten::val>(
Expand Down
Loading
Loading