Skip to content

.webnn to graph builder JS code conversion requires some special handling for API mismatches #4

@miaobin

Description

@miaobin

When converting ONNX transformer models like all-MiniLM-L6-v2 to WebNN graph builder JS code,
we discovered that some special case handling is required due to API design mismatches between
ONNX semantics and WebNN API conventions. For example:

  1. WebNN spec requires lowercase type names, but generated intermediate representations often use capitalized forms (Impact cast, constant ops):
// Required by WebNN
"float32", "float16", "int32"

// Generated from AST
"Float32", "Float16", "Int32"
  1. WebNN doesn't support negative axis indices, but ONNX commonly uses them (Impact concat, split, gather, reduceMean, reduceSum, softmax, layerNormalization ops):
# ONNX semantics
axis = -1  # means "last dimension"

# Must convert to WebNN
axis = rank - 1  # explicit positive index
  1. For slice operation we must compute sizes = ends - starts and expand sparse axes to full rank:
// ONNX: starts, ends, steps, axes (sparse - only specified dims)
slice(input, starts=[0], ends=[128], axes=[1])

// WebNN: starts, sizes, strides (dense - all dims required)
slice(input, [0,0], [1,128], {strides: [1,1]})
  1. For constant operation we need detect base64 encoded data and adds atob() to decode it into an ArrayBuffer.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions