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

Fix dimensions for 0D scalars #476

Merged
merged 12 commits into from
Feb 13, 2024
Merged

Fix dimensions for 0D scalars #476

merged 12 commits into from
Feb 13, 2024

Conversation

fdwr
Copy link
Collaborator

@fdwr fdwr commented Nov 2, 2023

From issue: #390

A 0D tensor shape vs a 1D tensor with a single element are semantically and functionally distinct, but the current spec mistreats 0D scalars and 1D tensors like they are the same thing, and this messes with the shape inference for operators like reduceSum (with all axes and keepDimensions = false) or gather where the output rank depends on the correct rank for input and indices tensors. This caused models like Segment Anything and Stable Diffusion to fail while prototyping, and the issue arose again during Chromium and ORT WebNN EP code review.

[1,2,3] - 3D tensor shape
[1,2] - 2D tensor shape
[1] - 1D tensor shape
[] - 0D tensor shape (scalar) <--- (every known ML library represents 0D scalars via the shape [])

Fixing this in the Chromium fork needed changing just two lines, and we want to address this sooner than later before changing just a few lines becomes multiple lines.


Preview | Diff

@zolkis
Copy link
Collaborator

zolkis commented Nov 2, 2023

Thanks for spotting / correcting that.

index.bs Outdated Show resolved Hide resolved
index.bs Outdated Show resolved Hide resolved
index.bs Outdated Show resolved Hide resolved
@anssiko
Copy link
Member

anssiko commented Jan 18, 2024

@fdwr noticed while checking the PR queue this needs a rebase. Good discussion in this PR should be factored in too I believe.

@inexorabletash
Copy link
Member

Other things that should be rolled into this PR:

  • In constant() "Set descriptor.dimensions to undefined." - should set it to an empty list instead. And the note "In the case of a scalar constant, descriptor.dimensions is ignored." should either be removed or updated; it's not that dimensions is ignored, it's that a rank of 0 defines it as being a scalar.
  • validate MLOperand has "If desc.dimensions exists" - remove that clause?
  • lstm() accesses dimensions[0] without a size / rank check first (in step 2) - add a check? I couldn't spot any other instances.
  • Make the default for MLOperandDescriptor's dimensions be []

@fdwr
Copy link
Collaborator Author

fdwr commented Feb 9, 2024

There's a build error (No 'idl' refs found for 'MLCommandEncoder') presumably from https://github.com/webmachinelearning/webnn/pull/546/files. I'll leave the status text there but just unbracket it to hopefully appease bikeshed.

Build completes now. If I should remove status text containing MLCommandEncoder entirely instead, let me know.

index.bs Outdated Show resolved Hide resolved
index.bs Outdated Show resolved Hide resolved
index.bs Outdated Show resolved Hide resolved
index.bs Outdated Show resolved Hide resolved
index.bs Outdated Show resolved Hide resolved
index.bs Outdated Show resolved Hide resolved
index.bs Outdated Show resolved Hide resolved
index.bs Show resolved Hide resolved
Copy link
Member

@inexorabletash inexorabletash left a comment

Choose a reason for hiding this comment

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

LGTM!

Copy link
Collaborator

@zolkis zolkis left a comment

Choose a reason for hiding this comment

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

Thanks, looks good!

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

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
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.

6 participants