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

gather(): Address indices validation and other algorithm nits #642

Merged
merged 9 commits into from
Apr 16, 2024

Conversation

inexorabletash
Copy link
Member

@inexorabletash inexorabletash commented Apr 8, 2024

Fixes #486
Fixes #484


Preview | Diff

* webmachinelearning#486 points out that indices can't be validated at build-time,
    and clamping behavior with an implementation note is given
    instead.

* Fix a typo in the steps.

* Replace several map-like iterations over lists with list iteration.

Fixes webmachinelearning#486
@inexorabletash
Copy link
Member Author

I noticed the other glitches in gather(), then went looking for open bugs and tackled #486 at the same time. Wheeee!

@inexorabletash
Copy link
Member Author

@a-sully can you take a first look?

Copy link
Contributor

@a-sully a-sully 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
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 with a comment, thanks!

index.bs Outdated Show resolved Hide resolved
Co-authored-by: Ningxin Hu <ningxin.hu@intel.com>
@inexorabletash
Copy link
Member Author

Do we want to tweak this at all to also tackle #484 or leave that for another PR?

@a-sully
Copy link
Contributor

a-sully commented Apr 9, 2024

Ah good question...

To specify support for negative indices in WebNN, we need to assume it's reasonable that every WebNN backend (that doesn't already support negative indices) will be able to transform these values at runtime. For example, the simplest way for our WebNN implementation to polyfill is by inserting if and add operations:

if index > 0:
  return index
else:
  return index + input.dimensions[axis]

This assumes that an if operator is available. Looking at #484, TFLite and StableHLO are called out as the backends which don't support negative indices. Both of those backends appear to have an if operator 1 2, though I'm not sure whether e.g. if is supported on all TFLite backends we care about

This becomes easier if we say that WebNN must support control flow ops #559

There may be some other more complicated way to transform these values, too...

@huningxin any thoughts?

@huningxin
Copy link
Contributor

@a-sully

This becomes easier if we say that WebNN must support control flow ops #559

There may be some other more complicated way to transform these values, too...

It might be emulated by where:

add(indices, where(lesser(indices, constant(0)), constant(input.dimensions[axis]), constant(0)))

@a-sully
Copy link
Contributor

a-sully commented Apr 10, 2024

Thanks, @huningxin!

To specify support for negative indices in WebNN, we need to assume it's reasonable that every WebNN backend (that doesn't already support negative indices) will be able to transform these values at runtime

Seems reasonable. @inexorabletash shall we fold that into this PR (a nonnormative note to implementers, perhaps?) and close #484?

@inexorabletash
Copy link
Member Author

Seems reasonable. @inexorabletash shall we fold that into this PR (a nonnormative note to implementers, perhaps?) and close #484?

I added a sentence to the note in 446bc77 - I kept it short, and didn't provide the decomposition. WDYT?

@a-sully
Copy link
Contributor

a-sully commented Apr 10, 2024

LGTM, especially since there are multiple reasonable decompositions

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!

@inexorabletash
Copy link
Member Author

@fdwr can you do a final review and merge if it looks good to you? Thanks!

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.

Approved with two suggestions. Thanks for adding Josh.

index.bs Outdated Show resolved Hide resolved
index.bs Outdated Show resolved Hide resolved
@fdwr
Copy link
Collaborator

fdwr commented Apr 16, 2024

It might be emulated by where:

add(indices, where(lesser(indices, constant(0)), constant(input.dimensions[axis]), constant(0)))

Yep, that looks right. An alternative would be...

where(lesser(indices, constant(0)), indices, add(indices, constant(input.dimensions[axis])))

...which is slightly shorter (one less constant(0)), but it's a tossup which one is more efficient 🤔.

This assumes that an if operator is available

Btw, the former name of where was elementwiseIf, which makes it more immediately clear to those less familiar with ML that it's the common select or ternary operator. Though, the new name is more discoverable to people who already know TF and PT ⚖️.

inexorabletash and others added 5 commits April 16, 2024 08:53
@inexorabletash
Copy link
Member Author

Thanks @fdwr - I incorporated your change then fixed one grammar glitch.

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.

👍 Arigatou Josh.

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

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
@inexorabletash inexorabletash deleted the bugfix-gather-validation branch April 16, 2024 22:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants