Skip to content

Commit

Permalink
[WebNN EP] TFLite backend only supports limit ranges for Clip (#20863)
Browse files Browse the repository at this point in the history
  • Loading branch information
Honry authored Jun 6, 2024
1 parent c749bd9 commit da1f8f9
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
2 changes: 1 addition & 1 deletion js/web/docs/webnn-operators.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ operators and the supported opset domain/versions in **WebNN EP** by ONNX Runtim
| BatchNormalization | ai.onnx(7-8, 9-13, 14, 15+) | batchNormalization ||| Only supports 'training_mode' value is 0, one output |
| Cast | ai.onnx(7-8, 9-12, 13-18, 19-20, 21+) | cast ||| |
| Ceil | ai.onnx(7-12, 13+) | ceil ||| |
| Clip | ai.onnx(7-10, 11, 12, 13+) | clamp ||| |
| Clip | ai.onnx(7-10, 11, 12, 13+) | clamp ||| WebNN CPU backend only supports 3 specific ranges: [0.0, infinity], [-1.0, 1.0], [0.0, 6.0] (Chromium issue: https://issues.chromium.org/issues/326156496) |
| Concat | ai.onnx(7-10, 11-12, 13+) | concat ||| |
| Conv | ai.onnx(7-10, 11+) | conv2d ||| Only supports 3-D or 4-D input and 'W' (weight). WebNN CPU requires the 'W' (weight) input to be a constant |
| ConvTranspose | ai.onnx(7-10, 11+) | convTranspose2d ||| Only supports 3-D or 4-D input and 'W' (weight). |
Expand Down
26 changes: 23 additions & 3 deletions onnxruntime/core/providers/webnn/builders/impl/clip_op_builder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class ClipOpBuilder : public BaseOpBuilder {
// Operator support related.
private:
bool IsOpSupportedImpl(const InitializedTensorSet& initializers, const Node& node,
const WebnnDeviceType /* device_type */, const logging::Logger& logger) const override;
const WebnnDeviceType device_type, const logging::Logger& logger) const override;
bool HasSupportedInputsImpl(const Node& node, const WebnnDeviceType device_type,
const logging::Logger& logger) const override;
};
Expand Down Expand Up @@ -64,13 +64,33 @@ Status ClipOpBuilder::AddToModelBuilderImpl(ModelBuilder& model_builder,

bool ClipOpBuilder::IsOpSupportedImpl(const InitializedTensorSet& initializers,
const Node& node,
const WebnnDeviceType /* device_type */,
const WebnnDeviceType device_type,
const logging::Logger& logger) const {
// TODO: Update IsOpSupportedImpl to pass GraphViewer instead of InitializedTensorSet so the implementations
// can ensure initializers are constant. See #19401 for details of how this update was made to the NNAPI EP.
// GetClipMinMax(graph_viewer, node, minValue, maxValue, logger)
float min, max;
return GetClipMinMax(initializers, node, min, max, logger);
if (GetClipMinMax(initializers, node, min, max, logger)) {
// WebNN CPU backend only supports 3 specific ranges: [0.0, infinity], [-1.0, 1.0], [0.0, 6.0].
// TODO: Remove this workaround once the associated issue is resolved in Chromium:
// https://issues.chromium.org/issues/326156496.
if (device_type == WebnnDeviceType::CPU) {
if ((min == 0.0f && max == std::numeric_limits<float>::infinity()) ||
(min == -1.0f && max == 1.0f) ||
(min == 0.0f && max == 6.0f)) {
return true;
} else {
LOGS(logger, VERBOSE) << "Clip min and max values ("
<< min << ", "
<< max << ") are not supported for WebNN CPU backend";
return false;
}
}

return true;
} else {
return false;
};
}

bool ClipOpBuilder::HasSupportedInputsImpl(const Node& node, const WebnnDeviceType device_type,
Expand Down

0 comments on commit da1f8f9

Please sign in to comment.