-
Notifications
You must be signed in to change notification settings - Fork 13.5k
[mlir] Convert expand_shape
to more static form
#112265
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 all commits
6764919
3f4c7bb
4e16a97
c6e1139
8cf255b
f8670eb
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 | ||
---|---|---|---|---|
|
@@ -24,6 +24,7 @@ | |||
#include "mlir/IR/TypeUtilities.h" | ||||
#include "mlir/Interfaces/DestinationStyleOpInterface.h" | ||||
#include "mlir/Interfaces/LoopLikeInterface.h" | ||||
#include "mlir/Support/LLVM.h" | ||||
#include "llvm/ADT/DenseSet.h" | ||||
#include "llvm/ADT/STLExtras.h" | ||||
#include "llvm/ADT/SmallBitVector.h" | ||||
|
@@ -1982,14 +1983,94 @@ struct FoldDimOfCollapseShape : public OpRewritePattern<DimOp> { | |||
return success(); | ||||
} | ||||
}; | ||||
|
||||
/// Fold/sink a producer `tensor.cast` with a consumer `tensor.expand_shape` by | ||||
/// matching constant output_shape operands of the expand. This makes the | ||||
/// `tensor.expand_shape` more static and creates a consumer cast that can be | ||||
/// propagated further. | ||||
struct ConvertToStaticExpandShape : public OpRewritePattern<ExpandShapeOp> { | ||||
using OpRewritePattern<ExpandShapeOp>::OpRewritePattern; | ||||
|
||||
LogicalResult matchAndRewrite(ExpandShapeOp expandOp, | ||||
PatternRewriter &rewriter) const override { | ||||
auto castOp = expandOp.getSrc().getDefiningOp<CastOp>(); | ||||
if (!canFoldIntoConsumerOp(castOp)) | ||||
return failure(); | ||||
|
||||
ArrayRef<int64_t> castSrcShape = castOp.getSource().getType().getShape(); | ||||
SmallVector<ReassociationIndices, 4> reassoc = | ||||
expandOp.getReassociationIndices(); | ||||
|
||||
SmallVector<int64_t> newOutputShape(expandOp.getResultType().getShape()); | ||||
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. You should look for source of
|
||||
SmallVector<Value> dynamicOutputShape; | ||||
auto outputIt = expandOp.getOutputShape().begin(); | ||||
|
||||
for (const auto &[inputDim, innerReassoc] : llvm::enumerate(reassoc)) { | ||||
for (uint64_t outDim : innerReassoc) { | ||||
if (!ShapedType::isDynamic(newOutputShape[outDim])) | ||||
continue; | ||||
|
||||
// If the cast's src type is dynamic, don't infer any of the | ||||
// corresponding expanded dimensions. `tensor.expand_shape` requires at | ||||
// least one of the expanded dimensions to be dynamic if the input is | ||||
// dynamic. | ||||
Value val = *outputIt; | ||||
++outputIt; | ||||
if (ShapedType::isDynamic(castSrcShape[inputDim])) { | ||||
dynamicOutputShape.push_back(val); | ||||
continue; | ||||
} | ||||
|
||||
APInt cst; | ||||
if (matchPattern(val, m_ConstantInt(&cst))) { | ||||
newOutputShape[outDim] = cst.getSExtValue(); | ||||
} else { | ||||
dynamicOutputShape.push_back(val); | ||||
} | ||||
} | ||||
} | ||||
|
||||
// Couldn't match any values, nothing to change | ||||
if (expandOp.getOutputShape().size() == dynamicOutputShape.size()) | ||||
return failure(); | ||||
|
||||
// Calculate the input shape from the output | ||||
SmallVector<int64_t> newInputShape(expandOp.getSrcType().getRank(), 1l); | ||||
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. Do you need this? Isnt the input shape the same as the source of the cast operation? 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. From the %0 = tensor.cast %arg0 : tensor<10x10xf32> to tensor<?x?xf32>
%1 = tensor.expand_shape %0 [[0, 1], [2]] output_shape [%arg1, %arg2, %c10]
: tensor<?x?xf32> into tensor<?x?x?xf32>
|
||||
for (auto inDim : llvm::seq<int>(0, newInputShape.size())) { | ||||
for (auto outDim : reassoc[inDim]) { | ||||
auto ofr = newOutputShape[outDim]; | ||||
if (ShapedType::isDynamic(ofr)) { | ||||
newInputShape[inDim] = ShapedType::kDynamic; | ||||
break; | ||||
} | ||||
newInputShape[inDim] *= ofr; | ||||
} | ||||
} | ||||
|
||||
SmallVector<OpFoldResult> outputOfr = | ||||
getMixedValues(newOutputShape, dynamicOutputShape, rewriter); | ||||
auto inputType = RankedTensorType::get( | ||||
newInputShape, expandOp.getSrcType().getElementType()); | ||||
auto outputType = RankedTensorType::get( | ||||
newOutputShape, expandOp.getSrcType().getElementType()); | ||||
auto inputCast = rewriter.create<CastOp>(expandOp.getLoc(), inputType, | ||||
expandOp.getSrc()); | ||||
auto newExpand = rewriter.create<ExpandShapeOp>( | ||||
expandOp.getLoc(), outputType, inputCast.getResult(), | ||||
expandOp.getReassociationIndices(), outputOfr); | ||||
rewriter.replaceOpWithNewOp<CastOp>(expandOp, expandOp.getType(), | ||||
newExpand.getResult()); | ||||
return success(); | ||||
} | ||||
}; | ||||
} // namespace | ||||
|
||||
void ExpandShapeOp::getCanonicalizationPatterns(RewritePatternSet &results, | ||||
MLIRContext *context) { | ||||
results.add< | ||||
ComposeReassociativeReshapeOps<ExpandShapeOp, ReshapeOpKind::kExpand>, | ||||
ComposeExpandOfCollapseOp<ExpandShapeOp, CollapseShapeOp>, | ||||
FoldReshapeWithConstant<ExpandShapeOp>, | ||||
ConvertToStaticExpandShape, FoldReshapeWithConstant<ExpandShapeOp>, | ||||
FoldReshapeWithSplat<ExpandShapeOp>, | ||||
FoldReshapeWithFromElements<ExpandShapeOp>, FoldDimOfExpandShape, | ||||
FoldDimOfCollapseShape>(context); | ||||
|
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.
Can you please document the pattern with a high-level description of what this pattern is doing? That'll be useful to future folks having to debug or improve this :)
(or just skimming the codebase).
Thanks.