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

Fast path slice contiguous constant #137

Merged
merged 2 commits into from
Oct 8, 2024
Merged
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
52 changes: 48 additions & 4 deletions src/enzyme_ad/jax/Passes/EnzymeHLOOpt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3011,6 +3011,21 @@ struct ConvertSimplify : public OpRewritePattern<mlir::stablehlo::ConvertOp> {
struct SliceSimplify : public OpRewritePattern<mlir::stablehlo::SliceOp> {
using OpRewritePattern<mlir::stablehlo::SliceOp>::OpRewritePattern;

static int64_t getSizeInBytes(Type type) {
if (auto shapedType = dyn_cast<ShapedType>(type))
return shapedType.getNumElements() *
getSizeInBytes(shapedType.getElementType());

if (type.isIntOrFloat())
return std::max(type.getIntOrFloatBitWidth(), (unsigned)8) / 8;

if (auto complexType = dyn_cast<mlir::ComplexType>(type))
return getSizeInBytes(complexType.getElementType()) * 2;

report_fatal_error(
invalidArgument("Unsupported type: %s", debugString(type).c_str()));
}

LogicalResult matchAndRewrite(mlir::stablehlo::SliceOp op,
PatternRewriter &rewriter) const final {
DenseElementsAttr inp;
Expand All @@ -3020,12 +3035,41 @@ struct SliceSimplify : public OpRewritePattern<mlir::stablehlo::SliceOp> {
if (inp.isSplat()) {
out = inp.resizeSplat(op.getType());
} else {
bool contiguous = true;
size_t offset = 0;
auto inshape = op.getOperand().getType().getShape();
size_t total = 1;
for (int i = inshape.size() - 1; i >= 0; i--) {
if (op.getStrides()[i] != 1) {
contiguous = false;
}
auto start = op.getStartIndices()[i];
auto lim = op.getLimitIndices()[i];
if (start != 0 || lim != inshape[i]) {
if (offset != 0) {
contiguous = false;
}
}
offset *= inshape[i];
offset += start;
total *= inshape[i];
}

auto ten = mlir::stablehlo::constantOp(inp);
out = fromTensor(mlir::stablehlo::sliceOp(
ten, stablehlo::Sizes(op.getStartIndices()),
stablehlo::Sizes(op.getStrides()), op.getType()));
}

if (contiguous) {
auto elementType = op.getOperand().getType().getElementType();
const char *elementPtr =
out.getRawData().data() + getSizeInBytes(elementType) * offset;

auto values = ArrayRef((char *)elementPtr, total);
out = DenseIntOrFPElementsAttr::getFromRawBuffer(op.getType(),
floatValues);
} else
out = fromTensor(mlir::stablehlo::sliceOp(
ten, stablehlo::Sizes(op.getStartIndices()),
stablehlo::Sizes(op.getStrides()), op.getType()));
}
rewriter.replaceOpWithNewOp<stablehlo::ConstantOp>(op, op.getType(), out);
return success();
}
Expand Down
Loading