-
Notifications
You must be signed in to change notification settings - Fork 2.9k
[BACKEND] Optimization to sink broadcast ops #2274
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -915,7 +915,7 @@ static void backwardRematerialization(ConvertLayoutOp convertOp) { | |
|
|
||
| // For convert left we try to hoist them above type extension to reduce the cost | ||
| // of the convert. | ||
| static void hoistConvertOnTopOfExt(ConvertLayoutOp convertOp) { | ||
| static void hoistConvertOnTopOfExtOrBroadcast(ConvertLayoutOp convertOp) { | ||
| // we don't want to rematerialize any conversion to/from shared | ||
| if (triton::gpu::isSharedEncoding(convertOp.getResult()) || | ||
| triton::gpu::isSharedEncoding(convertOp.getOperand())) | ||
|
|
@@ -926,25 +926,27 @@ static void hoistConvertOnTopOfExt(ConvertLayoutOp convertOp) { | |
| if (targetType.getEncoding().isa<triton::gpu::DotOperandEncodingAttr>()) | ||
| return; | ||
|
|
||
| auto isExtOp = [](Operation *op) { | ||
| return isa<arith::ExtSIOp, arith::ExtUIOp, arith::ExtFOp>(op); | ||
| auto isExtOrBroadcastOp = [](Operation *op) { | ||
| return isa<arith::ExtSIOp, arith::ExtUIOp, arith::ExtFOp, | ||
| triton::BroadcastOp>(op); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could this apply to
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Splat takes a scalar source so it is already handled. I'll extend to ExpandDim |
||
| }; | ||
| // 1. Take a backward slice of all the tensor dependencies. | ||
| SetVector<Value> slice; | ||
| DenseMap<Value, Attribute> layout; | ||
| LogicalResult result = getRematerializableSlice( | ||
| convertOp.getOperand(), targetType.getEncoding(), slice, layout, isExtOp); | ||
| LogicalResult result = | ||
| getRematerializableSlice(convertOp.getOperand(), targetType.getEncoding(), | ||
| slice, layout, isExtOrBroadcastOp); | ||
| if (result.failed()) | ||
| return; | ||
|
|
||
| Operation *extOp = nullptr; | ||
| Operation *extOrBroadcatOp = nullptr; | ||
| unsigned sliceSize = slice.size(); | ||
| for (unsigned i = 0; i < sliceSize; i++) { | ||
| Value v = slice[i]; | ||
| Operation *op = v.getDefiningOp(); | ||
| if (!op) | ||
| continue; | ||
| if (isExtOp(op)) { | ||
| if (isExtOrBroadcastOp(op)) { | ||
| SetVector<Value> tempSlice; | ||
| DenseMap<Value, Attribute> tempLayout; | ||
| LogicalResult result = getRematerializableSlice( | ||
|
|
@@ -958,24 +960,25 @@ static void hoistConvertOnTopOfExt(ConvertLayoutOp convertOp) { | |
| } | ||
| // Only apply it if there is a single ext op otherwise we would have to | ||
| // duplicate the convert. | ||
| if (extOp != nullptr) | ||
| if (extOrBroadcatOp != nullptr) | ||
| return; | ||
| extOp = op; | ||
| extOrBroadcatOp = op; | ||
| } | ||
| } | ||
|
|
||
| if (extOp == nullptr) | ||
| if (extOrBroadcatOp == nullptr) | ||
| return; | ||
| // Move the convert before the ext op and rewrite the slice. | ||
| OpBuilder builder(extOp); | ||
| auto tensorType = extOp->getOperand(0).getType().cast<RankedTensorType>(); | ||
| OpBuilder builder(extOrBroadcatOp); | ||
| auto tensorType = | ||
| extOrBroadcatOp->getOperand(0).getType().cast<RankedTensorType>(); | ||
| auto newType = | ||
| RankedTensorType::get(tensorType.getShape(), tensorType.getElementType(), | ||
| layout[extOp->getResult(0)]); | ||
| layout[extOrBroadcatOp->getResult(0)]); | ||
| auto newConvertOp = builder.create<ConvertLayoutOp>( | ||
| convertOp.getLoc(), newType, extOp->getOperand(0)); | ||
| convertOp.getLoc(), newType, extOrBroadcatOp->getOperand(0)); | ||
| IRMapping mapping; | ||
| mapping.map(extOp->getOperand(0), newConvertOp.getResult()); | ||
| mapping.map(extOrBroadcatOp->getOperand(0), newConvertOp.getResult()); | ||
| // 3. Rewrite the slice. | ||
| rewriteSlice(slice, layout, convertOp, mapping); | ||
| } | ||
|
|
@@ -994,7 +997,7 @@ static void hoistConvert(ModuleOp module) { | |
| module.walk( | ||
| [&](ConvertLayoutOp convertOp) { convertOps.push_back(convertOp); }); | ||
| for (ConvertLayoutOp convertOp : convertOps) { | ||
| hoistConvertOnTopOfExt(convertOp); | ||
| hoistConvertOnTopOfExtOrBroadcast(convertOp); | ||
| } | ||
| } | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This is a nice improvement. I think you can generalize it slightly further by having a common
srcShapeand allowing the scalar types to differ. This would allow the pattern to work forAddPtrOpfor example.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.
sorry for the delay, thanks for the advice. I'm sending another PR to handle this.