-
Notifications
You must be signed in to change notification settings - Fork 14.6k
[mlir][vector] Clarify the semantics of BroadcastOp #101928
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
Changes from 1 commit
7c08a8b
74d843c
89be55b
001a348
ab40831
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 |
---|---|---|
|
@@ -68,9 +68,13 @@ enum class BroadcastableToResult { | |
DimensionMismatch = 2, | ||
SourceTypeNotAVector = 3 | ||
}; | ||
struct VectorDim { | ||
int64_t dim; | ||
bool scalableFlag; | ||
banach-space marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}; | ||
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. 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. This PR is unrelated to that discussion. I'm only adding this here to avoid adding new set of params to I believe that before we commit to any new wider API, we should discuss the internal representation of 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. Ouuh, exciting. |
||
BroadcastableToResult | ||
isBroadcastableTo(Type srcType, VectorType dstVectorType, | ||
std::pair<int, int> *mismatchingDims = nullptr); | ||
std::pair<VectorDim, VectorDim> *mismatchingDims = nullptr); | ||
|
||
/// Collect a set of vector-to-vector canonicalization patterns. | ||
void populateVectorToVectorCanonicalizationPatterns(RewritePatternSet &patterns, | ||
|
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -2371,9 +2371,9 @@ Value BroadcastOp::createOrFoldBroadcastOp( | |||||||
return res; | ||||||||
} | ||||||||
|
||||||||
BroadcastableToResult | ||||||||
mlir::vector::isBroadcastableTo(Type srcType, VectorType dstVectorType, | ||||||||
std::pair<int, int> *mismatchingDims) { | ||||||||
BroadcastableToResult mlir::vector::isBroadcastableTo( | ||||||||
Type srcType, VectorType dstVectorType, | ||||||||
std::pair<VectorDim, VectorDim> *mismatchingDims) { | ||||||||
// Broadcast scalar to vector of the same element type. | ||||||||
if (srcType.isIntOrIndexOrFloat() && dstVectorType && | ||||||||
getElementTypeOrSelf(srcType) == getElementTypeOrSelf(dstVectorType)) | ||||||||
|
@@ -2391,12 +2391,28 @@ mlir::vector::isBroadcastableTo(Type srcType, VectorType dstVectorType, | |||||||
// (all leading dimensions are simply duplicated). | ||||||||
int64_t lead = dstRank - srcRank; | ||||||||
for (int64_t r = 0; r < srcRank; ++r) { | ||||||||
kuhar marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
bool mismatch = false; | ||||||||
kuhar marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
|
||||||||
// Check fixed-width dims | ||||||||
banach-space marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
int64_t srcDim = srcVectorType.getDimSize(r); | ||||||||
int64_t dstDim = dstVectorType.getDimSize(lead + r); | ||||||||
if (srcDim != 1 && srcDim != dstDim) { | ||||||||
if ((srcDim != 1 && srcDim != dstDim)) | ||||||||
banach-space marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
mismatch = true; | ||||||||
|
||||||||
// Check scalable flags | ||||||||
banach-space marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
bool srcDimScalableFlag = srcVectorType.getScalableDims()[r]; | ||||||||
bool dstDimScalableFlag = dstVectorType.getScalableDims()[lead + r]; | ||||||||
if ((srcDim == 1 && srcDimScalableFlag && dstDim != 1) || | ||||||||
(srcDimScalableFlag && !dstDimScalableFlag)) | ||||||||
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. It got me thinking, what would be the expected behaviour of something like:
IMO it should not be supported as physically equivalent to a usecase
Which is not invalid for fixed dimensions. Do you think this handles the cases ?
Suggested change
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. If you have e.g.
Is that the case you had in mind? 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. The case I pointed out was more src =
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. Ah, nice, great catch! In my head I had one case that wouldn't work with Let me send an update - thanks very much for pointing this out 🙏🏻 |
||||||||
mismatch = true; | ||||||||
|
||||||||
if (mismatch) { | ||||||||
if (mismatchingDims) { | ||||||||
mismatchingDims->first = srcDim; | ||||||||
mismatchingDims->second = dstDim; | ||||||||
mismatchingDims->first.dim = srcDim; | ||||||||
mismatchingDims->first.scalableFlag = srcDimScalableFlag; | ||||||||
|
||||||||
mismatchingDims->second.dim = dstDim; | ||||||||
mismatchingDims->second.scalableFlag = dstDimScalableFlag; | ||||||||
} | ||||||||
return BroadcastableToResult::DimensionMismatch; | ||||||||
} | ||||||||
|
@@ -2406,16 +2422,25 @@ mlir::vector::isBroadcastableTo(Type srcType, VectorType dstVectorType, | |||||||
} | ||||||||
|
||||||||
LogicalResult BroadcastOp::verify() { | ||||||||
std::pair<int, int> mismatchingDims; | ||||||||
std::pair<VectorDim, VectorDim> mismatchingDims; | ||||||||
BroadcastableToResult res = isBroadcastableTo( | ||||||||
getSourceType(), getResultVectorType(), &mismatchingDims); | ||||||||
if (res == BroadcastableToResult::Success) | ||||||||
return success(); | ||||||||
if (res == BroadcastableToResult::SourceRankHigher) | ||||||||
return emitOpError("source rank higher than destination rank"); | ||||||||
if (res == BroadcastableToResult::DimensionMismatch) | ||||||||
return emitOpError("dimension mismatch (") | ||||||||
<< mismatchingDims.first << " vs. " << mismatchingDims.second << ")"; | ||||||||
if (res == BroadcastableToResult::DimensionMismatch) { | ||||||||
std::string msg = | ||||||||
(Twine("dimension mismatch (") + | ||||||||
(mismatchingDims.first.scalableFlag ? "[" : "") + | ||||||||
std::to_string(mismatchingDims.first.dim) + | ||||||||
banach-space marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
(mismatchingDims.first.scalableFlag ? "]" : "") + " vs. " + | ||||||||
(mismatchingDims.second.scalableFlag ? "[" : "") + | ||||||||
std::to_string(mismatchingDims.second.dim) + | ||||||||
(mismatchingDims.second.scalableFlag ? "]" : "") + ")") | ||||||||
.str(); | ||||||||
return emitOpError(msg); | ||||||||
} | ||||||||
if (res == BroadcastableToResult::SourceTypeNotAVector) | ||||||||
return emitOpError("source type is not a vector"); | ||||||||
llvm_unreachable("unexpected vector.broadcast op error"); | ||||||||
|
Uh oh!
There was an error while loading. Please reload this page.