-
Notifications
You must be signed in to change notification settings - Fork 14.4k
[mlir][linalg] Fix and Refactor DecomposeOuterUnitDimsUnPackOpPattern #119379
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
banach-space
merged 2 commits into
llvm:main
from
banach-space:andrzej/update_decompose_unpack
Dec 16, 2024
Merged
Changes from 1 commit
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1252,64 +1252,88 @@ LogicalResult DecomposeOuterUnitDimsUnPackOpPattern::matchAndRewrite( | |
"require the tiled outer dimensions of the result are all 1s"); | ||
} | ||
|
||
// 1. Use rank-reduced tensor.extract_slice op to extract the tile. | ||
// 1. Use rank-reduced tensor.extract_slice op to extract the tile: | ||
// %extracted_tile = tensor.extract_slice(%unpack_op_input) | ||
Location loc = unpackOp.getLoc(); | ||
Value source = unpackOp.getSource(); | ||
DenseMap<int64_t, OpFoldResult> dimAndTileMapping = | ||
unpackOp.getDimAndTileMapping(); | ||
Attribute zeroIdxAttr = rewriter.getIndexAttr(0); | ||
Attribute oneIdxAttr = rewriter.getIndexAttr(1); | ||
SmallVector<OpFoldResult> readOffsets(srcRank, zeroIdxAttr); | ||
SmallVector<OpFoldResult> readStrides(srcRank, oneIdxAttr); | ||
SmallVector<OpFoldResult> readSizes; | ||
SmallVector<int64_t> readShape; | ||
SmallVector<Value> dynamicDims; | ||
|
||
// The sizes, affset and strides attributes for ExtractSliceOp. | ||
SmallVector<OpFoldResult> extractSliceSizes; | ||
SmallVector<OpFoldResult> extractSliceOffsets(srcRank, zeroIdxAttr); | ||
SmallVector<OpFoldResult> extractSliceStrides(srcRank, oneIdxAttr); | ||
// The shape for ExtractSliceOp (due to rank-reducing, this is likely != | ||
// extractSliceSizes). | ||
SmallVector<int64_t> readShapeForExtractSlice; | ||
|
||
// Shape for EmptyOp that's used as the init value for TransposeOp below. | ||
// This should match tile size + transposition. | ||
SmallVector<OpFoldResult> shapeForEmptyOp; | ||
|
||
for (auto i : llvm::seq<unsigned>(0, destRank)) { | ||
// Given the assumption that all outer tiled dims are 1, the corresponding | ||
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's more than an assumption here, since we have the check above. |
||
// slice size to read is also 1. As this will be rank-reducing "extract | ||
// slice" (i.e. the unit dims will be "collapsed"), there's no need to | ||
// update: | ||
// * the output shape for ExtractSliceOp, nor | ||
// * the shape for EmptyOp. | ||
if (dimAndTileMapping.count(i)) { | ||
readSizes.push_back(oneIdxAttr); | ||
extractSliceSizes.push_back(oneIdxAttr); | ||
continue; | ||
} | ||
|
||
// Compute sizes attribute for ExtractSliceOp + EmptyOp | ||
if (ShapedType::isDynamic(srcShape[i])) { | ||
Value dynamicDim = | ||
OpFoldResult dynamicDim = | ||
rewriter.create<tensor::DimOp>(loc, source, i).getResult(); | ||
readSizes.push_back(dynamicDim); | ||
dynamicDims.push_back(dynamicDim); | ||
extractSliceSizes.push_back(dynamicDim); | ||
shapeForEmptyOp.push_back(dynamicDim); | ||
} else { | ||
readSizes.push_back(rewriter.getIndexAttr(srcShape[i])); | ||
extractSliceSizes.push_back(rewriter.getIndexAttr(srcShape[i])); | ||
if (srcShape[i] != 1) | ||
shapeForEmptyOp.push_back(rewriter.getIndexAttr(srcShape[i])); | ||
} | ||
// Compute the output shape for ExtractSliceOp (take into account | ||
// rank-reducing) | ||
if (srcShape[i] != 1) { | ||
readShapeForExtractSlice.push_back(srcShape[i]); | ||
} | ||
if (srcShape[i] != 1) | ||
readShape.push_back(srcShape[i]); | ||
} | ||
auto mixedTiles = unpackOp.getMixedTiles(); | ||
readSizes.append(mixedTiles.begin(), mixedTiles.end()); | ||
// TODO: This effectively assumes that that tile sizes match the trailing | ||
// sizes for ExtractSliceOp and EmptyOp - document this. | ||
rengolin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
extractSliceSizes.append(mixedTiles.begin(), mixedTiles.end()); | ||
shapeForEmptyOp.append(mixedTiles.begin(), mixedTiles.end()); | ||
|
||
// Explicitly create the type for extract_slice op because the inner tile | ||
// size could be 1. We want to represent the whole inner tile in this case. | ||
auto tileShape = srcShape.drop_front(destRank); | ||
// Append the inner tile shape to the permuted and rank-reduced outer shape. | ||
readShape.append(tileShape.begin(), tileShape.end()); | ||
readShapeForExtractSlice.append(tileShape.begin(), tileShape.end()); | ||
Type elemType = unpackOp.getSourceType().getElementType(); | ||
auto readType = RankedTensorType::get(readShape, elemType); | ||
auto readType = RankedTensorType::get(readShapeForExtractSlice, elemType); | ||
Value innerTile = rewriter.create<tensor::ExtractSliceOp>( | ||
loc, readType, unpackOp.getSource(), readOffsets, readSizes, readStrides); | ||
loc, readType, unpackOp.getSource(), extractSliceOffsets, | ||
extractSliceSizes, extractSliceStrides); | ||
|
||
// 2. Transpose the tile to match the outer corresponding tile order. | ||
SmallVector<int64_t> perm = getPackUnpackRankReducedPerm( | ||
srcShape.take_front(destRank), innerDimsPos, unpackOp.getOuterDimsPerm()); | ||
// Unpack is a transition out of packed space so we invert the permutation. | ||
perm = invertPermutationVector(perm); | ||
SmallVector<int64_t> transpShape(readShape); | ||
applyPermutationToVector<int64_t>(transpShape, perm); | ||
applyPermutationToVector<OpFoldResult>(shapeForEmptyOp, perm); | ||
|
||
Value empty = | ||
rewriter.create<tensor::EmptyOp>(loc, transpShape, elemType, dynamicDims); | ||
rewriter.create<tensor::EmptyOp>(loc, shapeForEmptyOp, elemType); | ||
auto transposedOp = | ||
rewriter.create<linalg::TransposeOp>(loc, innerTile, empty, perm); | ||
|
||
// 3. Handle in-complete tiles if needed. It truncates trailing data from the | ||
// transposed tile. | ||
int numLoops = transpShape.size(); | ||
int numLoops = shapeForEmptyOp.size(); | ||
SmallVector<OpFoldResult> tileStrides(numLoops, oneIdxAttr); | ||
SmallVector<OpFoldResult> tileOffsets(numLoops, zeroIdxAttr); | ||
SmallVector<OpFoldResult> tileSizes; | ||
|
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.