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

Batched autodiff #2181

Merged
merged 22 commits into from
Dec 27, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
add enzyme broadcastop
  • Loading branch information
jumerckx committed Dec 16, 2024
commit 396a29b8efe1f13efdb5aaf11c452affc169ae1b
16 changes: 16 additions & 0 deletions enzyme/Enzyme/MLIR/Dialect/EnzymeOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -192,4 +192,20 @@ def GenericAdjointOp : Enzyme_Op<"genericAdjoint", [AttrSizedOperandSegments]> {

}

def BroadcastOp : Enzyme_Op<"broadcast"> {
let description = [{
Broadcast the operand by adding an extra dimension the frond with a size equal to the width attribute.
For scalar operands, a one-dimensional ranked tensor is created.

NOTE: Only works for scalars and *ranked* tensors for now.
}];

let arguments = (ins AnyType:$input, I64Attr:$width);
jumerckx marked this conversation as resolved.
Show resolved Hide resolved
let results = (outs AnyRankedTensor:$output);

let builders = [
OpBuilder<(ins "Value":$input, "int64_t":$width)>
];
}

#endif // ENZYME_OPS
20 changes: 20 additions & 0 deletions enzyme/Enzyme/MLIR/Dialect/Ops.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,3 +191,23 @@ LogicalResult BatchOp::verifySymbolUses(SymbolTableCollection &symbolTable) {

return success();
}

//===----------------------------------------------------------------------===//
// BroadcastOp
//===----------------------------------------------------------------------===//

void BroadcastOp::build(OpBuilder &builder, OperationState &result, Value input, int64_t width) {
auto widthAttr = builder.getI64IntegerAttr(width);
RankedTensorType output;
// TODO: support things other than scalars and ranked tensors, maybe reuse getShadowType here?
jumerckx marked this conversation as resolved.
Show resolved Hide resolved
if (auto tensorType = input.getType().dyn_cast<TensorType>()) {
auto shape = tensorType.getShape();
SmallVector<int64_t, 4> newShape;
newShape.push_back(width);
newShape.append(shape.begin(), shape.end());
output = RankedTensorType::get(newShape, tensorType.getElementType());
} else {
output = RankedTensorType::get({width}, input.getType());
}
build(builder, result, output, input, widthAttr);
}