webmachinelearning / webnn

🧠 Web Neural Network API
https://www.w3.org/TR/webnn/
Other
341 stars 42 forks source link

Shall we update the spec to add constraints on the axes and the input rank for the reduction operator? #681

Closed mei1127 closed 1 month ago

mei1127 commented 1 month ago

(This was raised by @a-sully in Chromium CL-5496412 review.)

For WebNN's reduction operator: Austion proposed to update the spec to add constraints on the axes and the input rank.

The Chromium code which validates reductions also fails if the input rank is zero values in axes are duplicated Shall we update the spec and add WPTs accordingly?

/cc @a-sully

inexorabletash commented 1 month ago

Makes sense to me - I put up a PR

fdwr commented 1 month ago

also fails if the input rank is zero

(repeating here too) Nop reductions are valid (where axes are empty), and scalars are just another instance of a nop:

x = tensorflow.constant([3,3,3], dtype=tensorflow.float32)
y = tensorflow.reduce_sum(x, axis=[])

# value: tf.Tensor([3. 3. 3.], shape=(3,), dtype=float32)
# shape: (3,)

x = tensorflow.constant([], dtype=tensorflow.float32)
y = tensorflow.reduce_sum(x, axis=[])

# value: tf.Tensor([], shape=(0,), dtype=float32)
# shape: (0,)

What's not valid is a scalar reduction with non-empty axes, because that would index axes beyond the rank, but the existing check that each axis < inputRank already validates that condition, and so checking input rank is redundant and unnecessarily exclusionary. So, we should remove that line in Chromium.

inexorabletash commented 1 month ago

Groovy. I have a Chromium CL (with WPT changes) up to remove the rank check crrev.com/c/5529350 but I'm a bit rusty so it may not land for a bit.

fdwr commented 1 month ago

chromium-wpt-export-bot pushed a commit to web-platform-tests/wpt

Oh interesting. So making the change in Chromium land actually pushes it back into web-platform-tests/wpt.

inexorabletash commented 1 month ago

Yeah, we have two-way sync set up with WPT. Mozilla and Apple have something similar. It's not perfect and requires some babysitting, but it's super great when it works.