Skip to content

add BroadcastIndexesRange #8864

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

Merged
merged 7 commits into from
Mar 5, 2025
Merged

add BroadcastIndexesRange #8864

merged 7 commits into from
Mar 5, 2025

Conversation

swolchok
Copy link
Contributor

@swolchok swolchok commented Mar 1, 2025

See class comment. In brief, this adds an iterable range to make broadcasting ops convenient and efficient to implement.

[ghstack-poisoned]
[ghstack-poisoned]
@swolchok
Copy link
Contributor Author

swolchok commented Mar 1, 2025

Copy link

pytorch-bot bot commented Mar 1, 2025

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/8864

Note: Links to docs will display an error until the docs builds have been completed.

❌ 1 New Failure

As of commit 8ade738 with merge base 5814a3b (image):

NEW FAILURE - The following job has failed:

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Mar 1, 2025
@swolchok swolchok marked this pull request as draft March 1, 2025 01:18
swolchok added 2 commits March 3, 2025 10:03
[ghstack-poisoned]
[ghstack-poisoned]
@swolchok swolchok changed the title add DelinearizedIndexesRange add BroadcastIndexesRange Mar 3, 2025
@swolchok swolchok requested a review from kimishpatel March 3, 2025 23:27
@swolchok swolchok marked this pull request as ready for review March 3, 2025 23:27
@swolchok swolchok force-pushed the gh/swolchok/299/head branch 2 times, most recently from 7bc4529 to 764977b Compare March 4, 2025 16:11
[ghstack-poisoned]
@swolchok swolchok changed the base branch from gh/swolchok/299/head to main March 4, 2025 17:45
[ghstack-poisoned]
// TODO: add optimization for particular input tensors not being
// broadcasted?
for (auto ii = output_dim_ - 1; ii >= 0; --ii) {
// You might wonder what happens if output_shape_[ii] == 0. In that case,
Copy link
Contributor

Choose a reason for hiding this comment

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

shouldn't we check for this before starting to iterate at all?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This comment is meant to be explaining why every caller does check for that already -- in that case begin() == end() and any loop that uses this thing won't be entered. I'll see if I can make that a bit clearer.

result[idx] = 0;
}
const auto t_sizes = t.sizes();
const auto t_strides = t.strides();
Copy link
Contributor

Choose a reason for hiding this comment

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

does this take dim order into account?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't recall how dim_order affects strides and sizes. if the tests pass, either it works or we have no tests for dim_order support (which would mean it didn't work before this diff).

Copy link
Contributor

Choose a reason for hiding this comment

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

at the minimal you should add checks for the dim order assumptions that are being made here. That is is assumes whatever the default dim order is, nothing fancy. If in future when the tests with dim order are added, at least this will be caught more gracefully rather than having to go down the debug rabbit hole

// output_dim. This is straightforwardly implementable with an
// adjusted stride array that contains 0s where the padded input
// shape would contain 1s.
std::array<ShapeType, kNumInputs> effective_input_broadcast_strides_ = {
Copy link
Contributor

Choose a reason for hiding this comment

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

I love this kNumInputs generalization. This is great!

// [1, W] -> [H, W]
// [H, 1] -> [H, W]
// [H, W] -> [H, W]
// Cover all these at the same time to also exercise multiple input tensors.
Copy link
Contributor

Choose a reason for hiding this comment

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

you are also covering [1, 1] -> [H, W] and [W] -> [H, W] here. would be good to mention as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

good catch, I'll rename it to OneAndTwoDExhaustive

}

// Here we assume that the previous tests established that padding
// with leading 1s is working, and test:
Copy link
Contributor

Choose a reason for hiding this comment

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

You are testing 5 out of 8 possibilities here. You might as well add the remaining 3:
[1, 1, 1] -> [C, H, W]
[1, 1, W] -> [C, H, W] (this one in particular would be good to have, i.e. multiple leading ones)
[1, H, W] -> [C, H, W]

EXPECT_EQ(expected, actual);
}

// 4-D should generalize, but we will go ahead and test:
Copy link
Contributor

Choose a reason for hiding this comment

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

this is great!

[ghstack-poisoned]
@facebook-github-bot
Copy link
Contributor

@swolchok has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@swolchok swolchok merged commit 2b90570 into main Mar 5, 2025
50 of 52 checks passed
@swolchok swolchok deleted the gh/swolchok/300/head branch March 5, 2025 05:31
// [1, C, 1, W] -> [N, C, H, W]
TEST(BroadcastIndexesRangeTest, FourDBroadcasting) {
TensorFactory<ScalarType::Int> tf;
Tensor out = tf.zeros({2, 3, 4, 5});
Copy link
Contributor

Choose a reason for hiding this comment

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

What about out = {2, 3, 1, 5} and a = {1, 3, 1, 5} and b = {2, 1, 1, 5}

Mainly highlighting that it there is size 1 dim in the output, is it taken care of? From cursory looks I presume the answer is yes, but not sure

Copy link
Contributor Author

Choose a reason for hiding this comment

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

it there is size 1 dim in the output, is it taken care of? From cursory looks I presume the answer is yes, but not sure

why would H == 1 be special? I'll add an exhaustive test for that for 1- and 2-D just in case in a follow-up.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

zonglinpeng pushed a commit that referenced this pull request Mar 6, 2025
See class comment. In brief, this adds an iterable range to make broadcasting ops convenient and efficient to implement.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. topic: not user facing
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants