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] Support negative steps for slice #22871

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
4 changes: 2 additions & 2 deletions js/web/docs/webnn-operators.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ operators and the supported opset domain/versions in **WebNN EP** by ONNX Runtim
| ReduceSumSquare | ai.onnx(7-10, 11-12, 13-17, 18+) | reduceSumSquare | ✓ | ✓ | Input 'axes' if present should be a constant |
| Relu | ai.onnx(7-12, 13, 14+) | relu | ✓ | ✓ | |
| Reshape | ai.onnx(7-12, 13, 14-18, 19-20, 21+) | reshape | ✓ | ✓ | Input 'shape' should be a constant, 0 dimension value in 'shape' is not supported |
| Resize | ai.onnx(11-12, 13-17, 18, 19+) | resample2d | ✓ | ✓ | Only supports 4-D input, antialias == 0, coordinate_transformation_mode == 'half_pixel', exclude_outside == 0, keep_aspect_ratio_policy == 'stretch', 'linear' and 'nearest' modes, input 'scales' and 'sizes' if present must be a constant |
| Resize | ai.onnx(11-12, 13-17, 18, 19+) | resample2d | ✓ | ✓ | Only supports 4-D input, antialias == 0, exclude_outside == 0, keep_aspect_ratio_policy == 'stretch', 'linear' and 'nearest' modes, input 'scales' and 'sizes' if present must be a constant |
| ScatterElements | ai.onnx(11-12, 13-15, 16-17, 18+) | scatterElements | ✗ | ✓ | Only supports 'reduction' == 'none' |
| ScatterND | ai.onnx(11-12, 13-15, 16-17, 18+) | scatterND | ✗ | ✓ | Only supports 'reduction' == 'none' |
| Shape | ai.onnx(7-12, 13-14, 15-18, 19-20, 21+) | slice | ✓ | ✓ | |
Expand All @@ -95,7 +95,7 @@ operators and the supported opset domain/versions in **WebNN EP** by ONNX Runtim
| Softplus | ai.onnx(7+) | softplus | ✓ | ✓ | |
| Softsign | ai.onnx(7+) | softsign | ✓ | ✓ | |
| Sin | ai.onnx(7+) | sin | ✓ | ✓ | |
| Slice | ai.onnx(7-9, 10, 11-12, 13+) | slice | ✓ | ✓ | Input 'starts', 'ends', 'axes', and 'steps' if present must be a constant, only supports 'steps' value >= 1 |
| Slice | ai.onnx(7-9, 10, 11-12, 13+) | slice, reverse | ✓ | ✓ | Input 'starts', 'ends', 'axes', and 'steps' if present must be a constant |
| Softmax | ai.onnx(7-10, 11-12, 13+) | softmax | ✓ | ✓ | |
| Split | ai.onnx(7-10, 11-12, 13-17, 18+) | split | ✓ | ✓ | Input 'split' if present should be a constant |
| Sqrt | ai.onnx(7-12, 13+) | sqrt | ✓ | ✓ | |
Expand Down
114 changes: 54 additions & 60 deletions onnxruntime/core/providers/webnn/builders/impl/slice_op_builder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@
}
}

Status SliceOpBuilder::AddToModelBuilderImpl(ModelBuilder& model_builder,
const Node& node,
Status SliceOpBuilder::AddToModelBuilderImpl(ModelBuilder& model_builder, const Node& node,
const logging::Logger& logger) const {
const auto& input_defs = node.InputDefs();
std::vector<int64_t> input_shape;
Expand All @@ -50,9 +49,6 @@
NodeAttrHelper helper(node);

emscripten::val inputs = model_builder.GetOperand(input_defs[0]->Name());
shiyi9801 marked this conversation as resolved.
Show resolved Hide resolved
std::vector<int32_t> starts(rank);
std::vector<int32_t> sizes(rank);
std::vector<int32_t> steps(rank);

// Copy the data from the starts/ends/axes/steps initializers.
std::vector<int64_t> input_starts;
Expand All @@ -66,18 +62,15 @@
std::string input_name;
// This is an optional input, return empty vector.
if (!is_required) {
if (input_defs.size() <= input_idx)
return Status::OK();
if (input_defs.size() <= input_idx) return Status::OK();
input_name = input_defs[input_idx]->Name();
if (input_name.empty())
return Status::OK();
if (input_name.empty()) return Status::OK();
shiyi9801 marked this conversation as resolved.
Show resolved Hide resolved
}
input_name = input_defs[input_idx]->Name();
const auto& initializers(model_builder.GetInitializerTensors());
const auto& tensor = *initializers.at(input_name);
if (!ReadIntArrayFrom1DTensor(tensor, data, logger)) {
return ORT_MAKE_STATUS(ONNXRUNTIME, FAIL,
"Data type for starts and ends inputs is not supported in this build.");
return ORT_MAKE_STATUS(ONNXRUNTIME, FAIL, "Data type for starts and ends inputs is not supported in this build.");
}

return Status::OK();
Expand All @@ -89,31 +82,59 @@
ORT_RETURN_IF_ERROR(
SliceOp::PrepareForComputeHelper(input_starts, input_ends, input_axes, input_steps, compute_metadata));

std::transform(compute_metadata.starts_.cbegin(), compute_metadata.starts_.cend(),
starts.begin(),
[](int64_t i) { return SafeInt<uint32_t>(i); });
std::transform(compute_metadata.ends_.cbegin(), compute_metadata.ends_.cend(), compute_metadata.starts_.cbegin(),
sizes.begin(),
[](int64_t i, int64_t j) { return SafeInt<uint32_t>(i - j); });
std::transform(compute_metadata.steps_.cbegin(), compute_metadata.steps_.cend(), steps.begin(),
[](int64_t i) { return SafeInt<uint32_t>(i); });

emscripten::val options = emscripten::val::object();
options.set("strides", emscripten::val::array(steps));
options.set("label", node.Name());
emscripten::val output = model_builder.GetBuilder().call<emscripten::val>("slice", inputs,
emscripten::val::array(starts),
emscripten::val::array(sizes),
options);
// Check if reverse op is needed.
std::vector<uint32_t> reverse_axes;
emscripten::val reverse_output = inputs;
for (size_t i = 0; i < rank; ++i) {
if (compute_metadata.steps_[i] < 0) {
reverse_axes.push_back(SafeInt<uint32_t>(i));
compute_metadata.steps_[i] = -compute_metadata.steps_[i];
compute_metadata.starts_[i] = input_shape[i] - 1 - compute_metadata.starts_[i];
compute_metadata.ends_[i] = input_shape[i] - 1 - compute_metadata.ends_[i];
}
}
if (!reverse_axes.empty()) {
emscripten::val reverse_options = emscripten::val::object();
reverse_options.set("axes", emscripten::val::array(reverse_axes));
reverse_options.set("label", node.Name() + "_reverse");
reverse_output = model_builder.GetBuilder().call<emscripten::val>("reverse", inputs, reverse_options);
fdwr marked this conversation as resolved.
Show resolved Hide resolved
}

// Check if slice op is needed.
bool is_slice_required = false;
for (size_t i = 0; i < rank; ++i) {
if (compute_metadata.steps_[i] != 1 || compute_metadata.starts_[i] != 0 ||
compute_metadata.ends_[i] != input_shape[i]) {
is_slice_required = true;
break;
}
}

emscripten::val output = reverse_output;
if (is_slice_required) {
std::vector<uint32_t> starts(rank);
std::vector<uint32_t> sizes(rank);
std::vector<uint32_t> steps(rank);
std::transform(compute_metadata.starts_.cbegin(), compute_metadata.starts_.cend(), starts.begin(),
shiyi9801 marked this conversation as resolved.
Show resolved Hide resolved
[](int64_t i) { return SafeInt<uint32_t>(i); });
std::transform(compute_metadata.ends_.cbegin(), compute_metadata.ends_.cend(), compute_metadata.starts_.cbegin(),
sizes.begin(), [](int64_t i, int64_t j) { return SafeInt<uint32_t>(i - j); });
std::transform(compute_metadata.steps_.cbegin(), compute_metadata.steps_.cend(), steps.begin(),

Check warning on line 122 in onnxruntime/core/providers/webnn/builders/impl/slice_op_builder.cc

View workflow job for this annotation

GitHub Actions / Optional Lint C++

[cpplint] reported by reviewdog 🐶 Add #include <algorithm> for transform [build/include_what_you_use] [4] Raw Output: onnxruntime/core/providers/webnn/builders/impl/slice_op_builder.cc:122: Add #include <algorithm> for transform [build/include_what_you_use] [4]
[](int64_t i) { return SafeInt<uint32_t>(i); });

emscripten::val options = emscripten::val::object();
options.set("strides", emscripten::val::array(steps));
options.set("label", node.Name());
output = model_builder.GetBuilder().call<emscripten::val>("slice", inputs, emscripten::val::array(starts),
emscripten::val::array(sizes), options);
}

model_builder.AddOperand(node.OutputDefs()[0]->Name(), std::move(output));
return Status::OK();
}

bool SliceOpBuilder::IsOpSupportedImpl(const InitializedTensorSet& initializers,
const Node& node,
const WebnnDeviceType /* device_type */,
const logging::Logger& logger) const {
bool SliceOpBuilder::IsOpSupportedImpl(const InitializedTensorSet& initializers, const Node& node,
const WebnnDeviceType /* device_type */, const logging::Logger& logger) const {
const auto& name = node.Name();
const auto& op_type = node.OpType();
const auto& input_defs = node.InputDefs();
Expand All @@ -133,37 +154,10 @@
// Optional tensors (axes, steps) can be indicated by an empty name, just ignore it.
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;
}
}

if (input_defs.size() == 5) { // Check steps.
const auto& steps_tensor = *initializers.at(input_defs[4]->Name());
std::vector<uint8_t> unpacked_tensor;
auto status = onnxruntime::utils::UnpackInitializerData(steps_tensor, unpacked_tensor);
if (!status.IsOK()) {
LOGS(logger, ERROR) << "Error while unpacking steps_tensor: " << status.ErrorMessage();
LOGS(logger, VERBOSE) << "Input [" << input_name << "] of " << op_type << " [" << name
<< "] must be known as initializer";
return false;
}
const auto data_type = steps_tensor.data_type();
// WebNN doesn't support steps less than 1.
if (data_type == ONNX_NAMESPACE::TensorProto_DataType_INT64) {
if (std::any_of(reinterpret_cast<int64_t*>(unpacked_tensor.data()),
reinterpret_cast<int64_t*>(unpacked_tensor.data() + unpacked_tensor.size()),
[](int64_t i) { return i < 1; })) {
LOGS(logger, VERBOSE) << "WebNN slice doesn't support steps less than 1";
return false;
}
} else if (data_type == ONNX_NAMESPACE::TensorProto_DataType_INT32) {
if (std::any_of(reinterpret_cast<int32_t*>(unpacked_tensor.data()),
reinterpret_cast<int32_t*>(unpacked_tensor.data()) + unpacked_tensor.size() / sizeof(int32_t),
[](int32_t i) { return i < 1; })) {
LOGS(logger, VERBOSE) << "WebNN slice doesn't support steps less than 1";
return false;
}
}
}

return true;
Expand Down
Loading