Skip to content
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

Merged
merged 17 commits into from
Apr 25, 2024

Conversation

inexorabletash
Copy link
Member

@inexorabletash inexorabletash commented Apr 18, 2024

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

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
@fdwr
Copy link
Collaborator

fdwr commented Apr 18, 2024

Thank you for championing my own issue in :).

FYI @Honry as this impacts your softmax implementation.

@fdwr
Copy link
Collaborator

fdwr commented Apr 18, 2024

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 softmax from WebNN with an axis parameter, and it fails. The spec is still a "living spec" candidate recommendation, not frozen, but it becomes increasingly challenging the older the operator is (the fillSequence operator is less impactful because it didn't exist yet anyway) and so clear messaging helps users.

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?

@inexorabletash
Copy link
Member Author

Thank you for championing my own issue in :).

Just trying to watch the world burn burn the issue list down. 😉

... the divergence between the Chromium implementation and the spec will bite people.

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?

index.bs Show resolved Hide resolved
@huningxin
Copy link
Contributor

this impacts your softmax implementation.

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.

Copy link
Contributor

@huningxin huningxin left a 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}}.
Copy link
Contributor

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.

Copy link
Member Author

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?

Copy link
Collaborator

@fdwr fdwr Apr 20, 2024

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.

Copy link
Contributor

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.

Copy link
Member Author

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:
    1. If axis is greater than or equal to input’s rank, then return false.
    2. 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:
    1. If options.activation exists, and running its validation steps with input returns false, then throw a TypeError.

Thoughts:

  • This could also be bundled into the validate activation steps - make those take an MLOperand in addition to or instead of MLGraphBuilder.
  • Is passing a single MLOperand (i.e. input) sufficient for validation?

WDYT?

Copy link
Contributor

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.

Copy link
Member Author

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.

Copy link
Collaborator

@fdwr fdwr Apr 23, 2024

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.

Copy link
Member Author

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 output MLOperand 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.

index.bs Outdated Show resolved Hide resolved
index.bs Outdated Show resolved Hide resolved
@inexorabletash
Copy link
Member Author

I cannot add inline comment, but "#### {{MLGraphBuilder/softmax(input)}} #### {#api-mlgraphbuilder-softmax-input}" at line 5300 needs to be updated with axis parameter.

Done in 66bf64a

@fdwr
Copy link
Collaborator

fdwr commented Apr 20, 2024

@huningxin: Does CoreML? I'm only seeing a classcoremltools.converters.mil.mil.ops.defs.iOS15.activation.softmax with an int32 axis. DirectML does, as axes are a more general superset of the single axis case, and since softmax is a div and reduceSumExp making it consistent with other reduction operators. However, while I like the elegance of axes, no other front-end or back-end I know of does so. So that's why I proposed a single axis.

I misread 😑 - yes, the input should be ND - I'll add a comment to that line.

@huningxin
Copy link
Contributor

Does CoreML? I'm only seeing a classcoremltools.converters.mil.mil.ops.defs.iOS15.activation.softmax with an int32 axis.

@fdwr , I meant whether the input should be N-D. I am fine with the single axis proposal.

index.bs Outdated Show resolved Hide resolved
Copy link
Collaborator

@fdwr fdwr left a 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.

index.bs Outdated Show resolved Hide resolved
index.bs Outdated Show resolved Hide resolved
inexorabletash and others added 2 commits April 22, 2024 09:23
Co-authored-by: Dwayne Robinson <dwayner@microsoft.com>
index.bs Show resolved Hide resolved
Copy link
Collaborator

@fdwr fdwr left a 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.

index.bs Outdated Show resolved Hide resolved
index.bs Outdated Show resolved Hide resolved
index.bs Outdated Show resolved Hide resolved
inexorabletash and others added 3 commits April 23, 2024 07:52
Co-authored-by: Dwayne Robinson <dwayner@microsoft.com>
Co-authored-by: Dwayne Robinson <dwayner@microsoft.com>
Co-authored-by: Dwayne Robinson <dwayner@microsoft.com>
Copy link
Collaborator

@fdwr fdwr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. TYJB. 👌

index.bs Outdated Show resolved Hide resolved
index.bs Outdated Show resolved Hide resolved
@inexorabletash
Copy link
Member Author

I noticed some related glitches when putting 622ce75 together - I'll factor those out into a separate PR

@inexorabletash inexorabletash marked this pull request as draft April 24, 2024 19:06
@inexorabletash
Copy link
Member Author

Copy link
Collaborator

@fdwr fdwr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@inexorabletash inexorabletash marked this pull request as ready for review April 25, 2024 00:31
@inexorabletash
Copy link
Member Author

Okay, back to a real PR. Final review @huningxin ? Do we want to discuss this more in the WG before merging?

Copy link
Contributor

@huningxin huningxin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@huningxin
Copy link
Contributor

@inexorabletash

Okay, back to a real PR. Final review @huningxin ?

Thanks! Done.

Do we want to discuss this more in the WG before merging?

I think we can merge it. Any thoughts @fdwr ?

@fdwr
Copy link
Collaborator

fdwr commented Apr 25, 2024

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 -_-).

@fdwr fdwr merged commit d57e4ef into webmachinelearning:main Apr 25, 2024
2 checks passed
github-actions bot added a commit that referenced this pull request Apr 25, 2024
SHA: d57e4ef
Reason: push, by fdwr

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
@inexorabletash inexorabletash deleted the softmax-axis-arg branch April 25, 2024 16:41
chromium-wpt-export-bot pushed a commit to web-platform-tests/wpt that referenced this pull request Jun 13, 2024
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
aarongable pushed a commit to chromium/chromium that referenced this pull request Jun 13, 2024
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}
chromium-wpt-export-bot pushed a commit to web-platform-tests/wpt that referenced this pull request Jun 13, 2024
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}
chromium-wpt-export-bot pushed a commit to web-platform-tests/wpt that referenced this pull request Jun 13, 2024
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}
moz-v2v-gh pushed a commit to mozilla/gecko-dev that referenced this pull request Jun 18, 2024
…, 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
gecko-dev-updater pushed a commit to marco-c/gecko-dev-wordified-and-comments-removed that referenced this pull request Jun 19, 2024
…, 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
gecko-dev-updater pushed a commit to marco-c/gecko-dev-wordified that referenced this pull request Jun 19, 2024
…, 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
i3roly pushed a commit to i3roly/firefox-dynasty that referenced this pull request Jun 21, 2024
…, 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
jamienicol pushed a commit to jamienicol/gecko that referenced this pull request Jun 24, 2024
…, 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Softmax axis absent
3 participants