-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce TTIR traits to track partial or full implicit folding of bi…
…nary eltwise operations. Add pass to apply folding when these traits are present.
- Loading branch information
Showing
7 changed files
with
134 additions
and
8 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
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,73 @@ | ||
// SPDX-FileCopyrightText: (c) 2024 Tenstorrent AI ULC | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#include "mlir/Transforms/GreedyPatternRewriteDriver.h" | ||
#include "ttmlir/Dialect/TT/IR/TT.h" | ||
#include "ttmlir/Dialect/TTIR/Transforms/Passes.h" | ||
#include <mlir/Transforms/GreedyPatternRewriteDriver.h> | ||
namespace mlir::tt::ttir { | ||
#define GEN_PASS_DEF_TTIRBROADCASTFOLD | ||
#include "ttmlir/Dialect/TTIR/Transforms/Passes.h.inc" | ||
|
||
class TTIRBroadcastFoldRewriter : public RewritePattern { | ||
public: | ||
TTIRBroadcastFoldRewriter(MLIRContext *ctx) | ||
: RewritePattern(MatchAnyOpTypeTag(), /*benefit=*/1, ctx) {} | ||
|
||
LogicalResult matchAndRewrite(Operation *op, | ||
PatternRewriter &rewriter) const override { | ||
|
||
// First check if the op itself has any broadcastable traits | ||
if (op->hasTrait<partiallyBroadcastable::Trait>()) { | ||
|
||
// This operation can only fold broadcast operation for Operand 0. | ||
ttir::BroadcastOp broadcastOp = | ||
op->getOperand(0).getDefiningOp<ttir::BroadcastOp>(); | ||
if (broadcastOp) { | ||
rewriter.replaceOp(broadcastOp, broadcastOp.getInput()); | ||
return success(); | ||
} | ||
} else if (op->hasTrait<fullyBroadcastable::Trait>()) { | ||
bool changed = false; | ||
// Check all operands for this op | ||
ttir::BroadcastOp broadcastOp0 = | ||
op->getOperand(0).getDefiningOp<ttir::BroadcastOp>(); | ||
ttir::BroadcastOp broadcastOp1 = | ||
op->getOperand(1).getDefiningOp<ttir::BroadcastOp>(); | ||
if (broadcastOp0) { | ||
rewriter.replaceOp(broadcastOp0, broadcastOp0.getInput()); | ||
changed = true; | ||
} else if (broadcastOp1) { | ||
rewriter.replaceOp(broadcastOp1, broadcastOp1.getInput()); | ||
changed = true; | ||
} | ||
return changed ? success() : failure(); | ||
} | ||
|
||
return failure(); | ||
} | ||
}; | ||
|
||
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 | ||
HoistCPUOps.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
12 changes: 12 additions & 0 deletions
12
test/ttmlir/Dialect/TTIR/broadcast/ttir_implicit_broadcast.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// RUN: not ttmlir-opt --split-input-file %s 2>&1 | FileCheck %s | ||
|
||
module attributes {tt.device = #device, tt.system_desc = #system_desc} { | ||
func.func @main(%arg0: tensor<1x16x32xf32>, %arg1: tensor<1x1x32xf32>) -> tensor<1x16x32xf32> { | ||
// CHECK-NOT: ttir.broadcast | ||
%0 = tensor.empty() : tensor<1x16x32xf32> | ||
%1 = "ttir.broadcast"(%arg1, %0) <{broadcast_dimensions = array<i32: 1, 16, 1>}> : (tensor<1x1x32xf32>, tensor<1x16x32xf32>) -> tensor<1x16x32xf32> | ||
%2 = tensor.empty() : tensor<1x16x32xf32> | ||
%3 = "ttir.multiply"(%arg0, %1, %2) <{operandSegmentSizes = array<i32: 2, 1>}> : (tensor<1x16x32xf32>, tensor<1x16x32xf32>, tensor<1x16x32xf32>) -> tensor<1x16x32xf32> | ||
return %3 : tensor<1x16x32xf32> | ||
} | ||
} |