-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move BroadcastOp folding to a seperate TTIR pass. (#1353)
* Move BroadcastOp folding to a seperate TTIR pass. Add more tests.
- Loading branch information
Showing
9 changed files
with
150 additions
and
55 deletions.
There are no files selected for viewing
This file contains 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 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 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 |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// SPDX-FileCopyrightText: (c) 2024 Tenstorrent AI ULC | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#include "ttmlir/Dialect/TT/IR/TT.h" | ||
#include "ttmlir/Dialect/TTIR/Transforms/Passes.h" | ||
|
||
#include "mlir/Transforms/GreedyPatternRewriteDriver.h" | ||
#include <mlir/Transforms/GreedyPatternRewriteDriver.h> | ||
|
||
namespace mlir::tt::ttir { | ||
#define GEN_PASS_DEF_TTIRBROADCASTFOLD | ||
#include "ttmlir/Dialect/TTIR/Transforms/Passes.h.inc" | ||
|
||
//===----------------------------------------------------------------------===// | ||
// Broadcast Folding pass | ||
// Our backend supports implicit broadcast of operands, so explicit broadcast | ||
// instructions are folded. | ||
// | ||
// For Example: | ||
// | ||
// %0 = tensor.empty() : tensor<512xf32> | ||
// %1 = "ttir.broadcast"(%arg0, %0) (tensor<1xf32>, tensor<512xf32>) -> | ||
// tensor<512xf32> %2 = tensor.empty() : tensor<512xf32> %3 = "ttir.maximum"(%1, | ||
// %arg1, %2) (tensor<512xf32>, tensor<512xf32>, tensor<512xf32>) -> | ||
// tensor<512xf32> | ||
// | ||
// After folding: | ||
// | ||
// %0 = tensor.empty() : tensor<512xf32> | ||
// %1 = "ttir.maximum"(%arg0, %arg1, %0) (tensor<1xf32>, tensor<512xf32>, | ||
// tensor<512xf32>) -> tensor<512xf32> | ||
//===----------------------------------------------------------------------===// | ||
|
||
class TTIRBroadcastFoldRewriter : public OpRewritePattern<BroadcastOp> { | ||
public: | ||
using OpRewritePattern<BroadcastOp>::OpRewritePattern; | ||
|
||
LogicalResult matchAndRewrite(BroadcastOp op, | ||
PatternRewriter &rewriter) const final { | ||
|
||
rewriter.replaceOp(op, op->getOperand(0)); | ||
return success(); | ||
} | ||
}; | ||
|
||
class TTIRBroadcastFold | ||
: public impl::TTIRBroadcastFoldBase<TTIRBroadcastFold> { | ||
public: | ||
using impl::TTIRBroadcastFoldBase<TTIRBroadcastFold>::TTIRBroadcastFoldBase; | ||
|
||
void runOnOperation() final { | ||
RewritePatternSet patterns(&getContext()); | ||
patterns.add<TTIRBroadcastFoldRewriter>(&getContext()); | ||
FrozenRewritePatternSet patternSet(std::move(patterns)); | ||
if (failed(applyPatternsAndFoldGreedily(getOperation(), patternSet))) { | ||
signalPassFailure(); | ||
return; | ||
} | ||
} | ||
|
||
void getDependentDialects(mlir::DialectRegistry ®istry) const override { | ||
registry.insert<mlir::tt::ttir::TTIRDialect>(); | ||
registry.insert<mlir::tt::TTDialect>(); | ||
} | ||
}; | ||
|
||
} // namespace mlir::tt::ttir |
This file contains 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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
add_mlir_dialect_library(MLIRTTIRTransforms | ||
Allocate.cpp | ||
Broadcast.cpp | ||
Constant.cpp | ||
Generic.cpp | ||
Layout.cpp | ||
|
This file contains 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 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 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
2 changes: 2 additions & 0 deletions
2
test/ttmlir/Silicon/TTNN/arange/simple_device_arange_dim2.mlir
This file contains 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 was deleted.
Oops, something went wrong.