-
Notifications
You must be signed in to change notification settings - Fork 3k
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] Add op support validation for decomposed WebNN ops #23370
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -194,8 +194,15 @@ std::unordered_set<const Node*> GetSupportedNodes(const GraphViewer& graph_viewe | |
const WebnnDeviceType device_type, | ||
const emscripten::val& wnn_limits, | ||
const logging::Logger& logger); | ||
// TODO(@Honry): Some ONNX ops are supported by decomposed WebNN ops, | ||
// we need to check the support of the decomposed ops. | ||
|
||
// Some ONNX ops are supported by decomposed WebNN ops. | ||
static const InlinedHashMap<std::string, std::vector<std::string>> decomposed_op_map = { | ||
{"LRN", {"add", "averagePool2d", "div", "mul", "pad", "pow", "transpose"}}, | ||
{"RotaryEmbedding", {"add", "concat", "gather", "mul", "reshape", "split"}}, | ||
{"SimplifiedLayerNormalization", {"add", "div", "mul", "pow", "reduceMean", "sqrt"}}, | ||
{"SkipSimplifiedLayerNormalization", {"add", "div", "mul", "pow", "reduceMean", "sqrt"}}, | ||
}; | ||
// ONNX op type to WebNN op type mapping. | ||
static const InlinedHashMap<std::string, std::string> op_map = { | ||
{"Abs", "abs"}, | ||
{"Add", "add"}, | ||
|
@@ -247,7 +254,6 @@ static const InlinedHashMap<std::string, std::string> op_map = { | |
{"Log", "log"}, | ||
{"LpPool", "l2Pool2d"}, | ||
{"LSTM", "lstm"}, | ||
{"LRN", "averagePool2d"}, | ||
{"MatMul", "matmul"}, | ||
{"MatMulInteger", "matmulInteger"}, | ||
{"Max", "max"}, | ||
|
@@ -275,17 +281,14 @@ static const InlinedHashMap<std::string, std::string> op_map = { | |
{"Relu", "relu"}, | ||
{"Reshape", "reshape"}, | ||
{"Resize", "resample2d"}, | ||
{"RotaryEmbedding", "gather"}, | ||
{"ScatterElements", "scatterElements"}, | ||
{"ScatterND", "scatterND"}, | ||
{"Shape", "slice"}, | ||
{"Sigmoid", "sigmoid"}, | ||
{"Sign", "sign"}, | ||
{"SimplifiedLayerNormalization", "layerNormalization"}, | ||
{"Softplus", "softplus"}, | ||
{"Softsign", "softsign"}, | ||
{"Sin", "sin"}, | ||
{"SkipSimplifiedLayerNormalization", "layerNormalization"}, | ||
{"Slice", "slice"}, | ||
{"Softmax", "softmax"}, | ||
{"Split", "split"}, | ||
|
@@ -302,16 +305,37 @@ static const InlinedHashMap<std::string, std::string> op_map = { | |
{"Xor", "logicalXor"}, | ||
}; | ||
|
||
inline bool CheckSingleOp(const std::string& op_type, const emscripten::val& wnn_builder, | ||
const WebnnDeviceType device_type) { | ||
auto op_map_entry = op_map.find(op_type); | ||
// Returns false if the op_type is not listed in the op_map or | ||
// if the WebNN op has not been implemented in MLGraphBuilder in current browser. | ||
if (op_map_entry == op_map.end() || !wnn_builder[op_map_entry->second].as<bool>()) { | ||
return false; | ||
} | ||
// WebNN op name to its first input name mapping, only record the name that is different from "input". | ||
// This map is used to determine the first input name of a WebNN op and is utilized by OpSupportLimits. | ||
static const InlinedHashMap<std::string, std::string> webnn_op_first_input_name_map = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (consider) All static const initializers add to DLL load time (or rather WASM module initialization), even if never used - death by a thousand paper cuts. Consider populating the Also can the map be |
||
{"add", "a"}, | ||
{"concat", "inputs"}, | ||
{"div", "a"}, | ||
{"equal", "a"}, | ||
{"gemm", "a"}, | ||
{"greater", "a"}, | ||
{"greaterOrEqual", "a"}, | ||
{"lesser", "a"}, | ||
{"lesserOrEqual", "a"}, | ||
{"logicalAnd", "a"}, | ||
{"logicalNot", "a"}, | ||
{"logicalOr", "a"}, | ||
{"logicalXor", "a"}, | ||
{"matmul", "a"}, | ||
{"max", "a"}, | ||
{"min", "a"}, | ||
{"mul", "a"}, | ||
{"pow", "a"}, | ||
{"sub", "a"}, | ||
{"where", "condition"}, | ||
}; | ||
|
||
return true; | ||
// Retrieve the first input name of a WebNN op used for validating supported input data types. | ||
// WebNN ops have various first input names such as 'a', 'input', 'inputs', etc. | ||
// Special names other than 'input' are recorded in the webnn_op_first_input_name_map. | ||
inline std::string GetWebNNOpFirstInputName(const std::string& webnn_op_type) { | ||
auto it = webnn_op_first_input_name_map.find(webnn_op_type); | ||
return (it != webnn_op_first_input_name_map.end()) ? it->second : "input"; | ||
} | ||
|
||
inline bool GetWebNNOpType(const std::string& op_type, std::string& webnn_op_type) { | ||
|
@@ -341,16 +365,16 @@ static const InlinedHashMap<ONNX_NAMESPACE::TensorProto_DataType, std::string> o | |
bool AreInputDataTypesSame(const std::string& op_type, | ||
gsl::span<const int32_t> input_types, | ||
const logging::Logger& logger); | ||
bool IsSupportedDataType(const int32_t onnx_data_type, const emscripten::val& webnn_supported_data_types); | ||
bool IsSupportedDataType(const int32_t& onnx_data_type, const emscripten::val& webnn_supported_data_types); | ||
bool IsDataTypeSupportedByOp(const std::string& onnx_op_type, | ||
const int32_t onnx_data_type, | ||
const int32_t& onnx_data_type, | ||
const emscripten::val& wnn_limits, | ||
const std::string& webnn_input_output_name, | ||
const std::string& onnx_input_output_name, | ||
const logging::Logger& logger); | ||
bool IsDataTypeSupportedByWebNNOp(const std::string& onnx_op_type, | ||
const std::string& webnn_op_type, | ||
const int32_t onnx_data_type, | ||
const int32_t& onnx_data_type, | ||
const emscripten::val& wnn_limits, | ||
const std::string& webnn_input_output_name, | ||
const std::string& onnx_input_output_name, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is an immutable simple value being passed by reference? 🤔