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

enzyme.broadcast conversion #197

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
26 changes: 26 additions & 0 deletions src/enzyme_ad/jax/Passes/ArithRaising.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
// ops.
//===----------------------------------------------------------------------===//

#include "Enzyme/MLIR/Dialect/Dialect.h"
#include "Enzyme/MLIR/Dialect/Ops.h"
#include "mlir/Dialect/Arith/IR/Arith.h"
#include "mlir/Dialect/Complex/IR/Complex.h"
#include "mlir/IR/Builders.h"
Expand Down Expand Up @@ -91,6 +93,30 @@ struct ArithRaisingPass : public ArithRaisingPassBase<ArithRaisingPass> {
constOp.erase();
}
});
op->walk([=](enzyme::BroadcastOp broadcastOp) {
OpBuilder builder(broadcastOp);
Value newBroadcastOp;
if (use_stablehlo) {
SmallVector<int64_t> broadcastDims;
auto shape =
broadcastOp.getInput().getType().cast<TensorType>().getShape();
broadcastDims.reserve(shape.size());
for (auto en : llvm::enumerate(shape)) {
// original dimensions end up one further because the batch dimension
// is prepended:
broadcastDims.push_back(en.index() + 1);
}
newBroadcastOp = builder.create<stablehlo::BroadcastInDimOp>(
broadcastOp.getLoc(), broadcastOp.getType(), broadcastOp.getInput(),
builder.getDenseI64ArrayAttr(broadcastDims));
} else {
newBroadcastOp = builder.create<mhlo::BroadcastOp>(
broadcastOp.getLoc(), broadcastOp.getInput(),
builder.getI64TensorAttr({broadcastOp.getWidth()}));
}
broadcastOp.replaceAllUsesWith(newBroadcastOp);
broadcastOp.erase();
});
}
};

Expand Down
3 changes: 2 additions & 1 deletion src/enzyme_ad/jax/Passes/Passes.td
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ def ArithRaisingPass : Pass<"arith-raise"> {
"arith::ArithDialect",
"mhlo::MhloDialect",
"stablehlo::StablehloDialect",
"chlo::ChloDialect"
"chlo::ChloDialect",
"enzyme::EnzymeDialect",
];
let constructor = "mlir::enzyme::createArithRaisingPass()";
let options = [
Expand Down
16 changes: 16 additions & 0 deletions test/lit_tests/broadcastdiff.mlir
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// RUN: enzymexlamlir-opt --arith-raise %s | FileCheck %s

module {
func.func @main(%arg0: tensor<f64>, %arg1: tensor<2xf64>) -> tensor<2xf64> {
%0 = "enzyme.broadcast"(%arg0) <{width = 2 : i64}> : (tensor<f64>) -> tensor<2xf64>
%1 = arith.addf %0, %arg1 : tensor<2xf64>
return %1 : tensor<2xf64>
}
}

// CHECK: func.func @main(%arg0: tensor<f64>, %arg1: tensor<2xf64>) -> tensor<2xf64> {
// CHECK-NEXT: %[[i0:.+]] = stablehlo.broadcast_in_dim %arg0, dims = [] : (tensor<f64>) -> tensor<2xf64>
// CHECK-NEXT: %[[i1:.+]] = stablehlo.add %[[i0:.+]], %arg1 : tensor<2xf64>
// CHECK-NEXT: return %[[i1:.+]] : tensor<2xf64>
// CHECK-NEXT: }
// CHECK-NEXT: }
Loading