Skip to content
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

More optimization fixes #48

Merged
merged 1 commit into from
Mar 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 41 additions & 9 deletions src/enzyme_ad/jax/Passes/EnzymeHLOOpt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,16 +236,18 @@ struct ReduceToReshape final : OpRewritePattern<mlir::stablehlo::ReduceOp> {
if (inpTy.getShape()[idx] != 1)
return failure();
}
assert(op.getInitValues()[0].getType() == op.getResult(0).getType());

auto reshaped = rewriter.create<stablehlo::ReshapeOp>(
op.getLoc(), op.getInitValues()[0].getType(), op.getInputs()[0]);

Operation &innerOp = op.getBody().front().front();

IRMapping map;
map.map(innerOp.getOperand(0), op.getInitValues()[0]);
map.map(innerOp.getOperand(1), reshaped);
auto res = rewriter.clone(innerOp, map)->getResult(0);
Value vals[2] = {op.getInitValues()[0], reshaped};
auto res = rewriter.create(
op.getLoc(), innerOp.getName().getIdentifier(), ValueRange(vals),
TypeRange(op->getResult(0).getType()), innerOp.getAttrs(), {}, {});
assert(res.getType() == op->getResult(0).getType());

rewriter.replaceOp(op, res);
return success();
Expand All @@ -272,8 +274,6 @@ struct ReduceConcat final : OpRewritePattern<mlir::stablehlo::ReduceOp> {
if (!isEligibleForCompactPrint(op))
return failure();

Operation &innerOp = op.getBody().front().front();

Value prev = op.getInitValues()[0];

for (auto v : concat.getOperands()) {
Expand Down Expand Up @@ -624,7 +624,8 @@ struct BroadcastToReshape final
auto type = dyn_cast<RankedTensorType>(op.getType());
if (!type)
return failure();

assert(op.getBroadcastDimensions().size() ==
op.getOperand().getType().getShape().size());
DenseElementsAttr inp;
matchPattern(op->getOperand(0), m_Constant(&inp));
if (inp) {
Expand Down Expand Up @@ -1085,6 +1086,34 @@ struct SqrtSimplify : public OpRewritePattern<mlir::stablehlo::SqrtOp> {
}
};

template <typename T> struct BinBroadcastSplat final : OpRewritePattern<T> {
using OpRewritePattern<T>::OpRewritePattern;

LogicalResult matchAndRewrite(T op,
PatternRewriter &rewriter) const override {
for (int i = 0; i < 2; i++) {
mlir::Value opi = op->getOperand(i);
if (auto broadcast = opi.getDefiningOp<stablehlo::BroadcastInDimOp>()) {
SplatElementsAttr other;
if (matchPattern(op->getOperand(1 - i), m_Constant(&other))) {
IRMapping map;
mlir::Value vals[2];
vals[i] = broadcast.getOperand();
vals[1 - i] = rewriter.create<stablehlo::ConstantOp>(
op.getLoc(), broadcast.getOperand().getType(),
other.resizeSplat(broadcast.getOperand().getType()));
auto pushed = rewriter.create<T>(op.getLoc(), vals[0], vals[1]);
map.map(broadcast.getOperand(), pushed->getResult(0));
auto bc2 = rewriter.clone(*broadcast, map);
rewriter.replaceOp(op, bc2);
return success();
}
}
}
return failure();
}
};

struct EnzymeHLOOptPass : public EnzymeHLOOptPassBase<EnzymeHLOOptPass> {

void runOnOperation() override {
Expand All @@ -1095,8 +1124,11 @@ struct EnzymeHLOOptPass : public EnzymeHLOOptPassBase<EnzymeHLOOptPass> {
/*ScatterToPad, */ BroadcastToReshape, ReduceToReshape,
ReduceConcat, SliceConcat, SliceSimplification, CosSimplify,
SinSimplify, SqrtSimplify, AddSimplify, SubSimplify,
NegateSimplify, MulSimplify, DivSimplify, PowSimplify>(
context);
NegateSimplify, MulSimplify, DivSimplify, PowSimplify,
BinBroadcastSplat<stablehlo::AddOp>,
BinBroadcastSplat<stablehlo::SubtractOp>,
BinBroadcastSplat<stablehlo::DivOp>,
BinBroadcastSplat<stablehlo::MulOp>>(context);
mlir::stablehlo::populateStablehloCanonicalizationPatterns(context,
&patterns);

Expand Down
18 changes: 18 additions & 0 deletions test/lit_tests/broadcastadd.mlir
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// RUN: enzymexlamlir-opt --enzyme-hlo-opt %s | FileCheck %s

module {

func.func @main(%a : tensor<f32>) -> tensor<2xf32> {
%bc = stablehlo.broadcast_in_dim %a, dims = [] : (tensor<f32>) -> tensor<2xf32>
%cst0 = arith.constant dense<2.000000e+00> : tensor<2xf32>
%add = stablehlo.add %bc, %cst0 : (tensor<2xf32>, tensor<2xf32>) -> tensor<2xf32>
return %add : tensor<2xf32>
}
}

// CHECK: func.func @main(%arg0: tensor<f32>) -> tensor<2xf32> {
// CHECK-NEXT: %0 = stablehlo.constant dense<2.000000e+00> : tensor<f32>
// CHECK-NEXT: %1 = stablehlo.add %arg0, %0 : tensor<f32>
// CHECK-NEXT: %2 = stablehlo.broadcast_in_dim %1, dims = [] : (tensor<f32>) -> tensor<2xf32>
// CHECK-NEXT: return %2 : tensor<2xf32>
// CHECK-NEXT: }
Loading