-
Notifications
You must be signed in to change notification settings - Fork 631
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Global opt] add flag to generalize matmul ops (#17877)
Helps when the producer is a broadcast op. After adding the flag to sdxl scripts, I saw a decent decrease in the number of dispatches. Initially, I was trying to manually generalize+fuse broadcasts [branch here](https://github.com/IanWood1/iree/tree/broadcast_matmul), but quinn saw good results with just this. --------- Signed-off-by: Ian Wood <[email protected]>
- Loading branch information
Showing
7 changed files
with
78 additions
and
0 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
54 changes: 54 additions & 0 deletions
54
compiler/src/iree/compiler/Preprocessing/Common/GeneralizeLinalgMatMul.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// Copyright 2024 The IREE Authors | ||
// | ||
// Licensed under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
|
||
#include "iree/compiler/Dialect/Flow/Transforms/RegionOpUtils.h" | ||
#include "mlir/Dialect/Linalg/IR/Linalg.h" | ||
#include "mlir/Dialect/Linalg/Transforms/Transforms.h" | ||
#include "mlir/IR/PatternMatch.h" | ||
#include "mlir/Interfaces/FunctionInterfaces.h" | ||
#include "mlir/Pass/Pass.h" | ||
|
||
namespace mlir::iree_compiler::Preprocessing { | ||
|
||
#define GEN_PASS_DEF_GENERALIZELINALGMATMULPASS | ||
#include "iree/compiler/Preprocessing/Common/Passes.h.inc" // IWYU pragma: export | ||
|
||
namespace { | ||
|
||
struct GeneralizeLinalgMatMulPass | ||
: public iree_compiler::Preprocessing::impl::GeneralizeLinalgMatMulPassBase< | ||
GeneralizeLinalgMatMulPass> { | ||
using iree_compiler::Preprocessing::impl::GeneralizeLinalgMatMulPassBase< | ||
GeneralizeLinalgMatMulPass>::GeneralizeLinalgMatMulPassBase; | ||
void runOnOperation() override { | ||
auto funcOp = getOperation(); | ||
SmallVector<linalg::LinalgOp> namedOpCandidates; | ||
funcOp.walk([&](linalg::LinalgOp linalgOp) { | ||
if (!IREE::Flow::isNonNullAndOutsideDispatch(linalgOp)) { | ||
return; | ||
} | ||
if (isa_and_nonnull<linalg::MatmulOp, linalg::MatmulTransposeBOp, | ||
linalg::BatchMatmulOp, | ||
linalg::BatchMatmulTransposeBOp>(linalgOp)) { | ||
namedOpCandidates.push_back(linalgOp); | ||
} | ||
}); | ||
|
||
IRRewriter rewriter(&getContext()); | ||
|
||
for (auto linalgOp : namedOpCandidates) { | ||
rewriter.setInsertionPoint(linalgOp); | ||
FailureOr<linalg::GenericOp> generalizedOp = | ||
linalg::generalizeNamedOp(rewriter, linalgOp); | ||
if (failed(generalizedOp)) { | ||
linalgOp->emitOpError("failed to generalize operation"); | ||
return signalPassFailure(); | ||
} | ||
} | ||
} | ||
}; | ||
} // namespace | ||
} // namespace mlir::iree_compiler::Preprocessing |
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
12 changes: 12 additions & 0 deletions
12
compiler/src/iree/compiler/Preprocessing/Common/test/generalize_linalg_matmul.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: iree-opt --pass-pipeline="builtin.module(util.func(iree-preprocessing-generalize-linalg-matmul-experimental))" --verify-each --split-input-file %s | FileCheck %s | ||
|
||
util.func public @generalize_matmul(%arg0: tensor<1x128x128xf32>, %arg1: tensor<1x128x128xf32>) -> tensor<1x128x128xf32> { | ||
%0 = tensor.empty() : tensor<1x128x128xf32> | ||
%1 = linalg.batch_matmul ins(%arg0, %arg1: tensor<1x128x128xf32>, tensor<1x128x128xf32>) outs(%0 : tensor<1x128x128xf32>) -> tensor<1x128x128xf32> | ||
util.return %1 : tensor<1x128x128xf32> | ||
} | ||
|
||
// CHECK-LABEL: util.func public @generalize_matmul | ||
// CHECK-SAME: %[[ARG0:.+]]: tensor<1x128x128xf32>, %[[ARG1:.+]]: tensor<1x128x128xf32> | ||
// CHECK: %[[GENERIC:.+]] = linalg.generic | ||
// CHECK-SAME: %[[ARG0]], %[[ARG1]] |