Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@huningxin
Copy link
Contributor

@huningxin huningxin commented Apr 29, 2024

Fix #662


Preview | Diff

@fdwr
Copy link
Collaborator

fdwr commented Apr 29, 2024

The current definition is correct given the interpretation of the first/input tensor (A) being broadcasted to the second/target tensor (B), and the text elsewhere in the spec matches that interpretation. So for expand, the inputShape (A) is broadcasted to newShape (B), and for prelu, the slope (A) is broadcasted to the input (B) shape. Current text:

partial interface MLGraphBuilder {
  MLOperand expand(MLOperand input, sequence<[EnforceRange] unsigned long> newShape);
};
...
Set outputDescriptor.dimensions to the result of unidirectionally broadcasting the shapes input’s shape and newShape.
...
partial interface MLGraphBuilder {
  MLOperand prelu(MLOperand input, MLOperand slope);
};
...
Set descriptor.dimensions to the result of unidirectionally broadcasting the shapes slope’s shape and input’s shape.

So if we're going to flip the interpretation, then we'd need to adjust the wording in two places too:

- Set outputDescriptor.dimensions to the result of unidirectionally broadcasting the shapes input’s shape and newShape.
+ Set outputDescriptor.dimensions to the result of unidirectionally broadcasting the shapes newShape and input’s shape.
...
- Set descriptor.dimensions to the result of unidirectionally broadcasting the shapes slope’s shape and input’s shape.
+ Set descriptor.dimensions to the result of unidirectionally broadcasting the shapes input’s shape and slope’s shape.

I see for ONNX, rather than the parameter order of (input -> output) or (original shape -> target shape), it lists parameters as (target shape <- original shape), which feels oddly backwards to me, but I can accept that (technically both definitions are functionally equivalent, just a different parameter order). The bigger point of confusion is that A and B are simply unclear names, and we could pick some that are clearer. How about originalShape and targetShape or oldShape and newShape or another less ambiguous pair?

@huningxin
Copy link
Contributor Author

huningxin commented Apr 29, 2024

@fdwr

So if we're going to flip the interpretation, then we'd need to adjust the wording in two places too:

We also need to change another caller "unidirectionally broadcastable":

-shapeA is unidirectionally broadcastable to shapeB if [unidirectionally broadcasting the shapes](https://www.w3.org/TR/webnn/#unidirectionally-broadcasting-the-shapes) shapeA and shapeB does not result in failure.
+shapeA is unidirectionally broadcastable to shapeB if [unidirectionally broadcasting the shapes](https://www.w3.org/TR/webnn/#unidirectionally-broadcasting-the-shapes) shapeB and shapeA does not result in failure.

And we need to flip one internal step of "unidirectionally broadcast the shapes":

-If dimA is not equal to dimB and dimA is not equal to 1, then return failure.
+If dimB is not equal to dimA and dimB is not equal to 1, then return failure.

@huningxin
Copy link
Contributor Author

@fdwr

How about originalShape and targetShape or oldShape and newShape or another less ambiguous pair?

+1. I suppose this ambiguity comes from the readers cannot easily tell the broadcasting direction. How about using fromShape and toShape?

@huningxin
Copy link
Contributor Author

The bigger point of confusion is that A and B are simply unclear names, and we could pick some that are clearer.

7f2c783 tries to use shapeFrom and shapeTo. Hope they are clearer. Please take another look. Thanks!

index.bs Outdated
1. [=list/For each=] |index| in [=the range=] 0 to |sizeB|, exclusive:
1. Let |dimA| be |paddedA|[|index|].
1. Let |dimB| be |shapeB|[|index|].
1. If |dimA| is not equal to |dimB| and |dimA| is not equal to 1, then return failure.
Copy link
Contributor

Choose a reason for hiding this comment

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

I want to defer to @fdwr but call out:

If the claim "The current definition is correct given the interpretation of ..." is correct, and this change is turning into a strict (1) reorder and (2) rename of the params, then it seems like this line needs to be altered to maintain the current behavior.

Also, @fdwr's note about updating the "call sites" needs to happen as well?

@inexorabletash
Copy link
Contributor

Can we move forward here? I'd recommend starting from a fresh change (force-push) that just renames the parameters from A and B (or is that B and A?) to shapeFrom and shapeTo and then we can decide if we want to swap the order or make other changes.

@huningxin huningxin force-pushed the fix_uni_broadcast branch from 7f2c783 to 9bc36ba Compare June 2, 2024 09:04
@huningxin
Copy link
Contributor Author

Can we move forward here? I'd recommend starting from a fresh change (force-push) that just renames the parameters from A and B (or is that B and A?) to shapeFrom and shapeTo

@inexorabletash @fdwr

9bc36ba is a force-push, just renames the parameters from "A" to "To", "B" to "From".

I'd like to point out if we just rename the following line, it is incorrect:

- If |dimA| is not equal to |dimB| and |dimA| is not equal to 1, then return failure.
+ If |dimTo| is not equal to |dimFrom| and |dimTo| is not equal to 1, then return failure.

because we should check whether dimFrom is equal to 1 rather than dimTo, the following one is correct:

- If |dimA| is not equal to |dimB| and |dimA| is not equal to 1, then return failure.
+ If |dimTo| is not equal to |dimFrom| and |dimFrom| is not equal to 1, then return failure.

The following commit c565f4a updates the call sites according to the change.

PTAL.

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.

πŸ‘ Thanks Ningxin - it reads much clearer with from and to than a and b (ordering still feels backwards putting a to parameter before the from, but shrug, there are precedents either way). Merge at your convenience.

@huningxin
Copy link
Contributor Author

huningxin commented Jun 5, 2024

@fdwr

ordering still feels backwards putting a to parameter before the from

I also feel backwards. a3976ae reverses the order of the two parameters. That actually becomes the same as my previous proposal: #663 (comment).

Copy link
Contributor

@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.

Thanks for walking through this patiently. LGTM!

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.

πŸ‘ Thanks N.

@fdwr fdwr merged commit 4eda710 into webmachinelearning:main Jun 5, 2024
github-actions bot added a commit that referenced this pull request Jun 5, 2024
SHA: 4eda710
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.

"Unidirectionally broadcast the shapes" steps have issues

3 participants