-
Notifications
You must be signed in to change notification settings - Fork 46
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
Add axis argument to softmax() #649
Add axis argument to softmax() #649
Conversation
Frameworks (TensorFlow, PyTorch, ONNX) all accept an axis parameter. Most backends also support an axis, or it can be emulated with a reshape. As @fdwr wrote: So it's achievable in each backend... but it would move the pain from the caller down to where it can be handled efficiently. Fixes webmachinelearning#466
Thank you for championing my own issue in :). FYI @Honry as this impacts your softmax implementation. |
A tangential high-level consideration (cc @anssiko too) - as we make changes to existing operators, the divergence between the Chromium implementation and the spec will bite people. I wonder how we point people to specific frozen versions of the spec to say Chrome/Edge v3.14159 matches with spec v42? Otherwise they'll try It appears we have versions produced near daily (or as often as changes are made) like here https://www.w3.org/TR/2024/CRD-webnn-20240416/ with an alarmingly red disclaimer that the spec is red. So maybe we can point to those for a mapping. Are they persistent for some time frame? |
Just trying to
See #650 for a more severe case - it's going to be painful. That's one of the reasons I'm trying to pick off lots of issues - get the pain behind us, instead of in front of us. Tracking Canary vs. spec dates is probably too much work, but we could try and match the big releases (e.g. Chrome/Edge 124 mostly matches spec v20240101) ? I don't know if we can do that historically, but maybe that's work we take on as part of upcoming browser branch dates? |
The ONNXRuntime WebNN EP softmax implementation also does the reshape N-D input to 2-D and reshape the 2-D output to N-D. Should WebNN softmax supports N-D input? It looks like DirectML and CoreML support N-D input. |
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.
I cannot add inline comment, but "#### {{MLGraphBuilder/softmax(input)}} #### {#api-mlgraphbuilder-softmax-input}" at line 5300 needs to be updated with axis
parameter.
index.bs
Outdated
|
||
**Returns:** | ||
- an {{MLOperand}}. The output 2-D tensor that contains the softmax results, of the same shape as *input*. | ||
</div> | ||
|
||
<details open algorithm> | ||
<summary> | ||
The <dfn method for=MLGraphBuilder>softmax(|input|)</dfn> method steps are: | ||
The <dfn method for=MLGraphBuilder>softmax(|input|, |axis|)</dfn> method steps are: | ||
</summary> | ||
1. If [=MLGraphBuilder/validating operand=] with [=this=] and |input| returns false, then [=exception/throw=] a {{TypeError}}. | ||
1. If |input|'s [=MLOperand/rank=] is not 2, then [=exception/throw=] a {{TypeError}}. |
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.
The axis
needs to be validated according to input's rank.
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.
Added for the operator version in 99e8773
For the activation version - would that be done synchronously when the activation is provided in a graph builder call, or at build time? Do we have other cases of activation validation?
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.
For the activation version - would that be done synchronously when the activation is provided in a graph builder call,
All the information should be available at construction time (pre-build
), since WebNN requires explicit shapes during construction.
Do we have other cases of activation validation?
None come to mind, except possibly softplus steepness, but that's not impacted by the tensor shape constraints.
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.
For the activation version - would that be done synchronously when the activation is provided in a graph builder call,
All the information should be available at construction time (pre-
build
), since WebNN requires explicit shapes during construction.
Agreed. In particular, I suppose the validation should be done at the build method of operators that accept a fused activation function, like conv2d.
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.
Here's a sketch of what this could look like:
- In create an
MLActivation
, add optional validation steps parameter, store the validation steps in an internal slot. The default validation steps are to return true. - When creating softmax activation, pass these steps as the validation steps:
- If axis is greater than or equal to input’s rank, then return false.
- Otherwise, return true.
- Note that if this were C++, this would be a lambda function, capturing axis.
- In all 7 methods that take activation functions (singular or multiple), add steps like this:
- If options.activation exists, and running its validation steps with input returns false, then throw a
TypeError
.
- If options.activation exists, and running its validation steps with input returns false, then throw a
Thoughts:
- This could also be bundled into the validate activation steps - make those take an
MLOperand
in addition to or instead ofMLGraphBuilder
. - Is passing a single
MLOperand
(i.e. input) sufficient for validation?
WDYT?
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.
- Is passing a single
MLOperand
(i.e. input) sufficient for validation?
I suppose it should pass output MLOperand since the activation follows the operator that fuses it.
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.
I guess we'll need to pass the output descriptor.
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.
In particular, I suppose the validation should be done at the build method of operators that accept a fused activation function, like conv2d.
Yep, since stand-alone activations adopt whatever the size is of the operator they are joined to or used inside. So full validation must be deferred for these.
MLConv2dOptions::activation
MLConvTranspose2dOptions::activation
MLBatchNormalizationOptions::activation
MLGruOptions::activations
MLGruCellOptions::activations
MLLstmOptions::activations
MLLstmCellOptions::activations
Is passing a single MLOperand (i.e. input) sufficient for validation?
Sounds fine, the MLActivation and at least either the MLOperand.shape()
or the MLOperand
itself for more generality.
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.
Okay - 7981fa5 sketches out the validation. Notes:
- I went with passing an
MLOperandDescriptor
, because earlier we decided that we'd do all validation before creating the operator and outputMLOperand
to pass. - The phrasing around creating/passing a lambda is based on Fetch
- The GRU/LSTM outputs are complicated. I'm not sure I'm passing the right thing, so please please double check.
Done in 66bf64a |
@huningxin: I misread 😑 - yes, the input should be ND - I'll add a comment to that line. |
@fdwr , I meant whether the input should be N-D. I am fine with the single axis proposal. |
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.
3 suggestions, and rest looks good to me. Thanks Josh.
Co-authored-by: Dwayne Robinson <dwayner@microsoft.com>
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.
LGTM given Ningxin's validation comment and some single-click ```suggestion blocks.
Co-authored-by: Dwayne Robinson <dwayner@microsoft.com>
Co-authored-by: Dwayne Robinson <dwayner@microsoft.com>
Co-authored-by: Dwayne Robinson <dwayner@microsoft.com>
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.
LGTM. TYJB. 👌
I noticed some related glitches when putting 622ce75 together - I'll factor those out into a separate PR |
|
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.
👍
Okay, back to a real PR. Final review @huningxin ? Do we want to discuss this more in the WG before merging? |
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.
LGTM
Thanks! Done.
I think we can merge it. Any thoughts @fdwr ? |
@huningxin What are we discussing? It already looked good to me 👍. So I'll merge it now (and then sleep -_-). |
SHA: d57e4ef Reason: push, by fdwr Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
This CL adds axis parameter into the IDL and mojo definitions of softmax operator [1]. It also updates the backends implementation to support the new axis parameter. In addition, the corresponding tests have also been updated. [1] webmachinelearning/webnn#649 Bug: 338094927 Change-Id: Ib08ecbba61c27c94256953a952357eeda80241e6 Cq-Include-Trybots: luci.chromium.try:win11-blink-rel,mac14.arm64-blink-rel,mac14-blink-rel
This CL adds axis parameter into the IDL and mojo definitions of softmax operator [1]. It also updates the backends implementation to support the new axis parameter. In addition, the corresponding tests have also been updated. [1] webmachinelearning/webnn#649 Bug: 338094927 Change-Id: Ib08ecbba61c27c94256953a952357eeda80241e6 Cq-Include-Trybots: luci.chromium.try:win11-blink-rel,mac14.arm64-blink-rel,mac14-blink-rel Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5495877 Reviewed-by: ningxin hu <ningxin.hu@intel.com> Commit-Queue: Bin Miao <bin.miao@intel.com> Reviewed-by: Austin Sullivan <asully@chromium.org> Cr-Commit-Position: refs/heads/main@{#1314418}
This CL adds axis parameter into the IDL and mojo definitions of softmax operator [1]. It also updates the backends implementation to support the new axis parameter. In addition, the corresponding tests have also been updated. [1] webmachinelearning/webnn#649 Bug: 338094927 Change-Id: Ib08ecbba61c27c94256953a952357eeda80241e6 Cq-Include-Trybots: luci.chromium.try:win11-blink-rel,mac14.arm64-blink-rel,mac14-blink-rel Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5495877 Reviewed-by: ningxin hu <ningxin.hu@intel.com> Commit-Queue: Bin Miao <bin.miao@intel.com> Reviewed-by: Austin Sullivan <asully@chromium.org> Cr-Commit-Position: refs/heads/main@{#1314418}
This CL adds axis parameter into the IDL and mojo definitions of softmax operator [1]. It also updates the backends implementation to support the new axis parameter. In addition, the corresponding tests have also been updated. [1] webmachinelearning/webnn#649 Bug: 338094927 Change-Id: Ib08ecbba61c27c94256953a952357eeda80241e6 Cq-Include-Trybots: luci.chromium.try:win11-blink-rel,mac14.arm64-blink-rel,mac14-blink-rel Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5495877 Reviewed-by: ningxin hu <ningxin.hu@intel.com> Commit-Queue: Bin Miao <bin.miao@intel.com> Reviewed-by: Austin Sullivan <asully@chromium.org> Cr-Commit-Position: refs/heads/main@{#1314418}
…, a=testonly Automatic update from web-platform-tests WebNN: Support axis for softmax operator This CL adds axis parameter into the IDL and mojo definitions of softmax operator [1]. It also updates the backends implementation to support the new axis parameter. In addition, the corresponding tests have also been updated. [1] webmachinelearning/webnn#649 Bug: 338094927 Change-Id: Ib08ecbba61c27c94256953a952357eeda80241e6 Cq-Include-Trybots: luci.chromium.try:win11-blink-rel,mac14.arm64-blink-rel,mac14-blink-rel Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5495877 Reviewed-by: ningxin hu <ningxin.hu@intel.com> Commit-Queue: Bin Miao <bin.miao@intel.com> Reviewed-by: Austin Sullivan <asully@chromium.org> Cr-Commit-Position: refs/heads/main@{#1314418} -- wpt-commits: 06bcaad19c8cdb96751ff3d80698533e85044a1d wpt-pr: 46725
…, a=testonly Automatic update from web-platform-tests WebNN: Support axis for softmax operator This CL adds axis parameter into the IDL and mojo definitions of softmax operator [1]. It also updates the backends implementation to support the new axis parameter. In addition, the corresponding tests have also been updated. [1] webmachinelearning/webnn#649 Bug: 338094927 Change-Id: Ib08ecbba61c27c94256953a952357eeda80241e6 Cq-Include-Trybots: luci.chromium.try:win11-blink-rel,mac14.arm64-blink-rel,mac14-blink-rel Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5495877 Reviewed-by: ningxin hu <ningxin.huintel.com> Commit-Queue: Bin Miao <bin.miaointel.com> Reviewed-by: Austin Sullivan <asullychromium.org> Cr-Commit-Position: refs/heads/main{#1314418} -- wpt-commits: 06bcaad19c8cdb96751ff3d80698533e85044a1d wpt-pr: 46725 UltraBlame original commit: 3c6317019e23fea6674e0b736fa01889f21a8a9b
…, a=testonly Automatic update from web-platform-tests WebNN: Support axis for softmax operator This CL adds axis parameter into the IDL and mojo definitions of softmax operator [1]. It also updates the backends implementation to support the new axis parameter. In addition, the corresponding tests have also been updated. [1] webmachinelearning/webnn#649 Bug: 338094927 Change-Id: Ib08ecbba61c27c94256953a952357eeda80241e6 Cq-Include-Trybots: luci.chromium.try:win11-blink-rel,mac14.arm64-blink-rel,mac14-blink-rel Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5495877 Reviewed-by: ningxin hu <ningxin.huintel.com> Commit-Queue: Bin Miao <bin.miaointel.com> Reviewed-by: Austin Sullivan <asullychromium.org> Cr-Commit-Position: refs/heads/main{#1314418} -- wpt-commits: 06bcaad19c8cdb96751ff3d80698533e85044a1d wpt-pr: 46725 UltraBlame original commit: 3c6317019e23fea6674e0b736fa01889f21a8a9b
…, a=testonly Automatic update from web-platform-tests WebNN: Support axis for softmax operator This CL adds axis parameter into the IDL and mojo definitions of softmax operator [1]. It also updates the backends implementation to support the new axis parameter. In addition, the corresponding tests have also been updated. [1] webmachinelearning/webnn#649 Bug: 338094927 Change-Id: Ib08ecbba61c27c94256953a952357eeda80241e6 Cq-Include-Trybots: luci.chromium.try:win11-blink-rel,mac14.arm64-blink-rel,mac14-blink-rel Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5495877 Reviewed-by: ningxin hu <ningxin.hu@intel.com> Commit-Queue: Bin Miao <bin.miao@intel.com> Reviewed-by: Austin Sullivan <asully@chromium.org> Cr-Commit-Position: refs/heads/main@{#1314418} -- wpt-commits: 06bcaad19c8cdb96751ff3d80698533e85044a1d wpt-pr: 46725
…, a=testonly Automatic update from web-platform-tests WebNN: Support axis for softmax operator This CL adds axis parameter into the IDL and mojo definitions of softmax operator [1]. It also updates the backends implementation to support the new axis parameter. In addition, the corresponding tests have also been updated. [1] webmachinelearning/webnn#649 Bug: 338094927 Change-Id: Ib08ecbba61c27c94256953a952357eeda80241e6 Cq-Include-Trybots: luci.chromium.try:win11-blink-rel,mac14.arm64-blink-rel,mac14-blink-rel Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5495877 Reviewed-by: ningxin hu <ningxin.hu@intel.com> Commit-Queue: Bin Miao <bin.miao@intel.com> Reviewed-by: Austin Sullivan <asully@chromium.org> Cr-Commit-Position: refs/heads/main@{#1314418} -- wpt-commits: 06bcaad19c8cdb96751ff3d80698533e85044a1d wpt-pr: 46725
Frameworks (TensorFlow, PyTorch, ONNX) all accept an axis parameter.
Most backends also support an axis, or it can be emulated with a reshape. As @fdwr wrote: So it's achievable in each backend... but it would move the pain from the caller down to where it can be handled efficiently.
Fixes #466
Preview | Diff