-
Notifications
You must be signed in to change notification settings - Fork 70
Remove redundant Set operation from broadcast when no broadcasting occurs #5507
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
base: main
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -14,6 +14,7 @@ | |||||||
| #include <alias_analysis.h> | ||||||||
| #include <fusion.h> | ||||||||
| #include <fusion_profiler.h> | ||||||||
| #include <ir/internal_nodes.h> | ||||||||
| #include <ir/iostream.h> | ||||||||
| #include <ir/utils.h> | ||||||||
| #include <ops/alias.h> | ||||||||
|
|
@@ -1659,4 +1660,39 @@ TEST_F(AliasTest, SliceOfExpandedBroadcast) { | |||||||
| executor_cache.fusion(), out_tensors, {in_tensor}, __LINE__, __FILE__); | ||||||||
| } | ||||||||
|
|
||||||||
| TEST_F(AliasTest, BroadcastInDimNoRedundantSet) { | ||||||||
| // Test that broadcast with no actual broadcasting does not introduce | ||||||||
| // a redundant Set operation | ||||||||
| auto fusion = std::make_unique<Fusion>(); | ||||||||
| FusionGuard fg(fusion.get()); | ||||||||
|
|
||||||||
| TensorView* in = makeContigConcreteTensor({2, 3}); | ||||||||
| fusion->addInput(in); | ||||||||
|
|
||||||||
| // Call broadcast with all dims marked as non-broadcast | ||||||||
| // This should not introduce a Set operation | ||||||||
| std::vector<bool> is_broadcast_dim = {false, false}; | ||||||||
| TensorView* out = broadcast(in, is_broadcast_dim); | ||||||||
|
||||||||
| TensorView* out = broadcast(in, is_broadcast_dim); | |
| TensorView* maybe_bcast = broadcast(in, is_broadcast_dim); | |
| TensorView* out = abs(maybe_bcast); |
Or just check:
EXPECT_EQ(fusion->exprs(), 0);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot, please implement the change according to feedback from Ryan.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added abs() operation after the broadcast and updated the comment as suggested. This ensures the fusion has expressions to verify. Commit: 3b44991
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -667,6 +667,61 @@ def fusion_func_3(fd: FusionDefinition): | |||||||||||||||||||||||
| ) | ||||||||||||||||||||||||
| self.assertEqual(eager_out, nvf_out[0]) | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| def test_broadcast_in_dim_no_redundant_set(self): | ||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||
| Test that broadcast_in_dim doesn't introduce redundant Set operations | ||||||||||||||||||||||||
| when all input dimensions are in broadcast_dims (i.e., no actual broadcast). | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| This verifies the fix for the issue where broadcast_in_dim would create | ||||||||||||||||||||||||
| a redundant float-to-float cast operation via Set when the input already | ||||||||||||||||||||||||
| had the correct shape. | ||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||
| inputs = [ | ||||||||||||||||||||||||
| torch.ones(1, 4, device="cuda"), | ||||||||||||||||||||||||
| torch.randn(2, 4, device="cuda"), | ||||||||||||||||||||||||
| ] | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| def fusion_with_broadcast_in_dim(fd: FusionDefinition): | ||||||||||||||||||||||||
| t0 = fd.define_tensor(shape=[1, -1], contiguity=[None, True]) | ||||||||||||||||||||||||
| t1 = fd.define_tensor(shape=[-1, -1], contiguity=[True, True]) | ||||||||||||||||||||||||
| # broadcast_in_dim with broadcast_dims=[0, 1] means no new dims are added | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
| # broadcast_in_dim with broadcast_dims=[0, 1] means no new dims are added | |
| # broadcast_in_dim with all input dims in broadcast_dims means no broadcasting operation occurs |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The review comment is not correct, broadcasting operation occurs and it involves expanding 1-sized dimension.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These checks (and inputs) are probably not necessary since the point of the test is just that the IR should match exactly whether we use broadcast_in_dim or expand whenever there is no new broadcast.
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a better way to test what nodes a fusion definition consists of?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IIUC, the broadcast is a no-op, so the fusion definitions should be the same.
| # Check that the broadcast_in_dim fusion doesn't have a redundant Set operation | |
| # by comparing the IR string representations | |
| bid_str = str(fd_bid) | |
| exp_str = str(fd_exp) | |
| # Count tensor cast operations (not scalar casts) | |
| bid_tensor_casts = bid_str.count("fd.ops.cast(t") | |
| exp_tensor_casts = exp_str.count("fd.ops.cast(t") | |
| # Check that the broadcast_in_dim fusion doesn't have a redundant Set operation | |
| # by comparing the IR string representations | |
| assert str(fd_bid) == str(fd_exp) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated to use direct string comparison as suggested. Since the broadcast is a no-op, the fusion definitions should indeed be identical. Commit: 3b44991
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The change seems reasonable to me.