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

mlir: Add Enzyme ops removal on structured control flow #2200

Merged
merged 10 commits into from
Jan 5, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,22 @@ using namespace mlir;
using namespace mlir::enzyme;

namespace {

static mlir::Type batchType(mlir::Type type, int64_t width) {
if (width == 1)
return type;

if (auto TT = dyn_cast<mlir::TensorType>(type)) {
SmallVector<int64_t> shape;
shape.reserve(TT.getShape().size() + 1);
shape.push_back(width);
shape.append(TT.getShape().begin(), TT.getShape().end());
return TT.clone(shape);
}

return RankedTensorType::get({width}, type);
}

class FloatTypeInterface
: public AutoDiffTypeInterface::ExternalModel<FloatTypeInterface,
FloatType> {
Expand All @@ -44,12 +60,8 @@ class FloatTypeInterface
return a;
}

Type getShadowType(Type self, unsigned width) const {
if (width > 1) {
return RankedTensorType::get({width}, self);
} else {
return self;
}
Type getShadowType(Type self, int64_t width) const {
return batchType(self, width);
}

bool isMutable(Type self) const { return false; }
Expand Down Expand Up @@ -108,16 +120,8 @@ class TensorTypeInterface
return added;
}

Type getShadowType(Type self, unsigned width) const {
if (width != 1) {
auto tenType = self.cast<TensorType>();
auto shape = tenType.getShape();
SmallVector<int64_t, 4> newShape;
newShape.push_back(width);
newShape.append(shape.begin(), shape.end());
return RankedTensorType::get(newShape, tenType.getElementType());
}
return self;
Type getShadowType(Type self, int64_t width) const {
return batchType(self, width);
}

bool isMutable(Type self) const { return false; }
Expand Down Expand Up @@ -148,9 +152,8 @@ class IntegerTypeInterface
return a;
}

Type getShadowType(Type self, unsigned width) const {
assert(width == 1 && "unsupported width != 1");
return self;
Type getShadowType(Type self, int64_t width) const {
return batchType(self, width);
}

bool isMutable(Type self) const { return false; }
Expand Down Expand Up @@ -182,9 +185,8 @@ class ComplexTypeInterface
return builder.create<complex::ConjOp>(loc, a)->getResult(0);
}

Type getShadowType(Type self, unsigned width) const {
assert(width == 1 && "unsupported width != 1");
return self;
Type getShadowType(Type self, int64_t width) const {
return batchType(self, width);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in a separate different PR, it may be worthwhile switching getShadowType and the likes to take an ArrayRef<int64_t> indices to batch on (@jumerckx did something similar when adding batched differentiation broadcast earlier)

}

bool isMutable(Type self) const { return false; }
Expand Down
Loading
Loading