Skip to content

[stablehlo] Add attribute ceil_mode to reduce_window op #4196

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
16 changes: 16 additions & 0 deletions lib/Conversion/TorchToStablehlo/Pooling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,7 @@ class ConvertAtenMaxPoolOp : public ConvertAtenOp<AtenOpT> {
SmallVector<int64_t> stablehloDilation(inputRank, 1);
SmallVector<int64_t> stablehloKernelSize(inputRank, 1);
SmallVector<int64_t> stablehloPadding(inputRank * 2, 0);
SmallVector<int64_t> ceilModePadding(inputRank * 2, 0);
std::copy(dilation.begin(), dilation.end(),
stablehloDilation.begin() + inputRank - Dim);
std::copy(stride.begin(), stride.end(),
Expand Down Expand Up @@ -520,6 +521,8 @@ class ConvertAtenMaxPoolOp : public ConvertAtenOp<AtenOpT> {
const int64_t extraPadding = sizeDiff * stride[i];
stablehloPadding[frontPadIdx] += extraPadding / 2;
stablehloPadding[backPadIdx] += extraPadding - extraPadding / 2;
ceilModePadding[frontPadIdx] += extraPadding / 2;
ceilModePadding[backPadIdx] += extraPadding - extraPadding / 2;
}
}
}
Expand All @@ -539,6 +542,19 @@ class ConvertAtenMaxPoolOp : public ConvertAtenOp<AtenOpT> {
op->getLoc(), outTy, input, initVal, windowDimensions, windowStrides,
baseDilations, windowDilations, pad);

// *** ADD THE 'ceil_mode' ATTRIBUTE ***
// The 'ceilMode' boolean variable was extracted from the PyTorch op
// earlier.
mlir::BoolAttr ceilModeAttr = rewriter.getBoolAttr(ceilMode);
reduceWindowOp->setAttr(llvm::StringRef("ceil_mode"), ceilModeAttr);
DenseIntElementsAttr ceilModePad = DenseIntElementsAttr::get(
RankedTensorType::get(
{static_cast<int64_t>(inputRank), static_cast<int64_t>(2)},
rewriter.getI64Type()),
stablehloPadding);
// Also add the 'ceil_mode_padding' attribute to the op to distinguish
// original padding from the extra padding added for ceil_mode.
reduceWindowOp->setAttr(llvm::StringRef("ceil_mode_padding"), ceilModePad);
Block &block = reduceWindowOp.getBody().emplaceBlock();

// Add bb argument
Expand Down
Loading