Skip to content

[MLIR][Linalg] Fixes for Winograd decomposition and for tiling #123675

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

Merged
merged 2 commits into from
Jan 29, 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
6 changes: 4 additions & 2 deletions mlir/include/mlir/Dialect/Linalg/IR/LinalgOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ def Linalg_SoftmaxOp : Linalg_Op<"softmax",
}

def Linalg_WinogradFilterTransformOp : Linalg_Op<"winograd_filter_transform",
[AllElementTypesMatch<["filter", "output"]>,
[AllElementTypesMatch<["filter", "output"]>, DestinationStyleOpInterface,
DeclareOpInterfaceMethods<TilingInterface,
["getIterationDomain",
"getLoopIteratorTypes",
Expand Down Expand Up @@ -220,12 +220,13 @@ def Linalg_WinogradFilterTransformOp : Linalg_Op<"winograd_filter_transform",
int64_t getFilterCDim() {
return 3;
}
MutableOperandRange getDpsInitsMutable() { return getOutputMutable(); }
}];
let hasVerifier = 1;
}

def Linalg_WinogradInputTransformOp : Linalg_Op<"winograd_input_transform",
[AllElementTypesMatch<["input", "output"]>,
[AllElementTypesMatch<["input", "output"]>, DestinationStyleOpInterface,
DeclareOpInterfaceMethods<TilingInterface,
["getIterationDomain",
"getLoopIteratorTypes",
Expand Down Expand Up @@ -308,6 +309,7 @@ def Linalg_WinogradInputTransformOp : Linalg_Op<"winograd_input_transform",
int64_t getOutputCDim() {
return 5;
}
MutableOperandRange getDpsInitsMutable() { return getOutputMutable(); }
}];
let hasVerifier = 1;
}
Expand Down
78 changes: 42 additions & 36 deletions mlir/lib/Dialect/Linalg/IR/LinalgOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3063,8 +3063,11 @@ LogicalResult WinogradInputTransformOp::verify() {
int m = getM();
int r = getR();
int64_t tileSize = m + r - 1;
bool leftTransform = inputH != 1;
bool rightTransform = inputW != 1;

auto outputType = cast<ShapedType>(getOutput().getType());
ArrayRef<int64_t> outputShape = outputType.getShape();
bool leftTransform = outputShape[getOutputAlphaHDim()] != 1;
bool rightTransform = outputShape[getOutputAlphaWDim()] != 1;

SmallVector<int64_t> expectedOutputShape(6, inputH);
if (ShapedType::isDynamic(inputH)) {
Expand All @@ -3073,21 +3076,19 @@ LogicalResult WinogradInputTransformOp::verify() {
} else {
expectedOutputShape[getOutputAlphaHDim()] = leftTransform ? tileSize : 1;
expectedOutputShape[getOutputTileHDim()] =
leftTransform ? (inputH - (r - 1)) / m : 1;
leftTransform ? (inputH - (r - 1)) / m : inputH;
}
if (ShapedType::isDynamic(inputW)) {
expectedOutputShape[getOutputAlphaWDim()] = tileSize;
expectedOutputShape[getOutputTileWDim()] = ShapedType::kDynamic;
} else {
expectedOutputShape[getOutputAlphaWDim()] = rightTransform ? tileSize : 1;
expectedOutputShape[getOutputTileWDim()] =
rightTransform ? (inputW - (r - 1)) / m : 1;
rightTransform ? (inputW - (r - 1)) / m : inputW;
}
expectedOutputShape[getOutputNDim()] = inputShape[getInputNDim()];
expectedOutputShape[getOutputCDim()] = inputShape[getInputCDim()];

auto outputType = cast<ShapedType>(getOutput().getType());
ArrayRef<int64_t> outputShape = outputType.getShape();
if (failed(verifyCompatibleShape(expectedOutputShape, outputShape))) {
return emitOpError("the output shape is not expected");
}
Expand Down Expand Up @@ -3124,15 +3125,17 @@ LogicalResult WinogradInputTransformOp::getResultTilePosition(
ArrayRef<OpFoldResult> sizes, SmallVector<OpFoldResult> &resultOffsets,
SmallVector<OpFoldResult> &resultSizes) {
IntegerAttr zeroAttr = builder.getI64IntegerAttr(0);
ShapedType inputType = getInputOperandType();
ArrayRef<int64_t> inputShape = inputType.getShape();
int64_t inputH = inputShape[getInputHDim()];
int64_t inputW = inputShape[getInputWDim()];
ShapedType outputType = getOutputOperandType();
ArrayRef<int64_t> outputShape = outputType.getShape();
int64_t outputAlphaH = outputShape[getOutputAlphaHDim()];
int64_t outputAlphaW = outputShape[getOutputAlphaWDim()];

int64_t m = getM();
int64_t r = getR();
int64_t alpha = m + r - 1;
int64_t alphaH = inputH != 1 ? alpha : 1;
int64_t alphaW = inputW != 1 ? alpha : 1;
int64_t alphaH = outputAlphaH != 1 ? alpha : 1;
int64_t alphaW = outputAlphaW != 1 ? alpha : 1;

IntegerAttr alphaHAttr = builder.getI64IntegerAttr(alphaH);
IntegerAttr alphaWAttr = builder.getI64IntegerAttr(alphaW);

Expand All @@ -3157,22 +3160,26 @@ WinogradInputTransformOp::getTiledImplementation(OpBuilder &builder,
ArrayRef<OpFoldResult> offsets,
ArrayRef<OpFoldResult> sizes) {
IntegerAttr oneAttr = builder.getI64IntegerAttr(1);
IntegerAttr zeroAttr = builder.getI64IntegerAttr(0);
ShapedType inputType = getInputOperandType();
ArrayRef<int64_t> inputShape = inputType.getShape();
int64_t inputH = inputShape[getInputHDim()];
int64_t inputW = inputShape[getInputWDim()];
int64_t m = getM();
int64_t r = getR();

ShapedType outputType = getOutputOperandType();
ArrayRef<int64_t> outputShape = outputType.getShape();
int64_t alphaH = outputShape[getOutputAlphaHDim()];
int64_t alphaW = outputShape[getOutputAlphaWDim()];

Location loc = getLoc();
MLIRContext *context = builder.getContext();
auto identityAffineMap =
AffineMap::get(1, 0, {builder.getAffineDimExpr(0)}, context);
auto offsetAffineMap =
AffineMap::get(1, 0, {builder.getAffineDimExpr(0) * m}, context);
Value mappedOffsetH = affine::makeComposedAffineApply(
builder, loc, offsetAffineMap, offsets[getOutputTileHDim()]);
builder, loc, (alphaH != 1 ? offsetAffineMap : identityAffineMap),
offsets[getOutputTileHDim()]);
Value mappedOffsetW = affine::makeComposedAffineApply(
builder, loc, offsetAffineMap, offsets[getOutputTileWDim()]);
builder, loc, (alphaW != 1 ? offsetAffineMap : identityAffineMap),
offsets[getOutputTileWDim()]);
auto sizeAffineMap = AffineMap::get(
1, 0, {builder.getAffineDimExpr(0) * m + (r - 1)}, context);
Value mappedSizeH = affine::makeComposedAffineApply(
Expand All @@ -3183,16 +3190,14 @@ WinogradInputTransformOp::getTiledImplementation(OpBuilder &builder,
SmallVector<Value> tiledOperands;
SmallVector<OpFoldResult> sliceOffsets, sliceSizes;

OpFoldResult offsetH =
inputH != 1 ? OpFoldResult(mappedOffsetH) : OpFoldResult(zeroAttr);
OpFoldResult offsetW =
inputW != 1 ? OpFoldResult(mappedOffsetW) : OpFoldResult(zeroAttr);
OpFoldResult offsetH = OpFoldResult(mappedOffsetH);
OpFoldResult offsetW = OpFoldResult(mappedOffsetW);
sliceOffsets.append(
{offsets[getOutputNDim()], offsetH, offsetW, offsets[getOutputCDim()]});
OpFoldResult sizeH =
inputH != 1 ? OpFoldResult(mappedSizeH) : OpFoldResult(oneAttr);
alphaH != 1 ? OpFoldResult(mappedSizeH) : OpFoldResult(oneAttr);
OpFoldResult sizeW =
inputW != 1 ? OpFoldResult(mappedSizeW) : OpFoldResult(oneAttr);
alphaW != 1 ? OpFoldResult(mappedSizeW) : OpFoldResult(oneAttr);
sliceSizes.append(
{sizes[getOutputNDim()], sizeH, sizeW, sizes[getOutputCDim()]});
int64_t inputRank = getInputOperandRank();
Expand Down Expand Up @@ -3300,28 +3305,29 @@ LogicalResult WinogradOutputTransformOp::getResultTilePosition(

Location loc = getLoc();
MLIRContext *context = builder.getContext();
auto identityAffineMap =
AffineMap::get(1, 0, {builder.getAffineDimExpr(0)}, context);
auto affineMap =
AffineMap::get(1, 0, {builder.getAffineDimExpr(0) * m}, context);

ShapedType valueType = getValueOperandType();
ArrayRef<int64_t> valueShape = valueType.getShape();
int64_t valueH = valueShape[0];
int64_t valueW = valueShape[1];
Value mappedOffsetH = affine::makeComposedAffineApply(
builder, loc, affineMap, offsets[getValueTileHDim()]);
builder, loc, (valueH != 1 ? affineMap : identityAffineMap),
offsets[getValueTileHDim()]);
Value mappedOffsetW = affine::makeComposedAffineApply(
builder, loc, affineMap, offsets[getValueTileWDim()]);
builder, loc, (valueW != 1 ? affineMap : identityAffineMap),
offsets[getValueTileWDim()]);
Value mappedSizeH = affine::makeComposedAffineApply(
builder, loc, affineMap, sizes[getValueTileHDim()]);
Value mappedSizeW = affine::makeComposedAffineApply(
builder, loc, affineMap, sizes[getValueTileWDim()]);

ShapedType valueType = getValueOperandType();
ArrayRef<int64_t> valueShape = valueType.getShape();
int64_t valueH = valueShape[0];
int64_t valueW = valueShape[1];
IntegerAttr oneAttr = builder.getI64IntegerAttr(1);
IntegerAttr zeroAttr = builder.getI64IntegerAttr(0);
OpFoldResult offsetH =
valueH != 1 ? OpFoldResult(mappedOffsetH) : OpFoldResult(zeroAttr);
OpFoldResult offsetW =
valueW != 1 ? OpFoldResult(mappedOffsetW) : OpFoldResult(zeroAttr);
OpFoldResult offsetH = OpFoldResult(mappedOffsetH);
OpFoldResult offsetW = OpFoldResult(mappedOffsetW);
OpFoldResult sizeH =
valueH != 1 ? OpFoldResult(mappedSizeH) : OpFoldResult(oneAttr);
OpFoldResult sizeW =
Expand Down
34 changes: 19 additions & 15 deletions mlir/lib/Dialect/Linalg/Transforms/WinogradConv2D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -514,12 +514,14 @@ Value inputTransform(RewriterBase &rewriter, Location loc, Value input,
Value CIter = ivs[3];

auto context = builder.getContext();

auto identityAffineMap = rewriter.getMultiDimIdentityMap(1);
auto affineMap =
AffineMap::get(1, 0, {builder.getAffineDimExpr(0) * m}, context);
Value heightOffset =
builder.create<affine::AffineApplyOp>(loc, affineMap, tileHIter);
Value widthOffset =
builder.create<affine::AffineApplyOp>(loc, affineMap, tileWIter);
Value heightOffset = builder.create<affine::AffineApplyOp>(
loc, leftTransform ? affineMap : identityAffineMap, tileHIter);
Value widthOffset = builder.create<affine::AffineApplyOp>(
loc, rightTransform ? affineMap : identityAffineMap, tileWIter);

// Extract (H, W) from (N, H, W, C).
auto extractInput =
Expand Down Expand Up @@ -753,12 +755,13 @@ Value outputTransform(RewriterBase &rewriter, Location loc, Value value,
Value zero = builder.create<arith::ConstantOp>(
loc, rewriter.getZeroAttr(elementType));

auto identityAffineMap = rewriter.getMultiDimIdentityMap(1);
auto affineMap =
AffineMap::get(1, 0, {builder.getAffineDimExpr(0) * m}, context);
Value heightOffset =
builder.create<affine::AffineApplyOp>(loc, affineMap, tileHIter);
Value widthOffset =
builder.create<affine::AffineApplyOp>(loc, affineMap, tileWIter);
Value heightOffset = builder.create<affine::AffineApplyOp>(
loc, leftTransform ? affineMap : identityAffineMap, tileHIter);
Value widthOffset = builder.create<affine::AffineApplyOp>(
loc, rightTransform ? affineMap : identityAffineMap, tileWIter);

Value outInitVal =
extract2DDataFrom4D(builder, loc, args[0], NIter, FIter, heightOffset,
Expand Down Expand Up @@ -1075,16 +1078,17 @@ FailureOr<Operation *>
decomposeWinogradInputTransformHelper(RewriterBase &rewriter,
linalg::WinogradInputTransformOp op) {
Location loc = op.getLoc();
Value input = op.getInput();
auto inputType = cast<ShapedType>(input.getType());
auto inputShape = inputType.getShape();
int64_t inputH = inputShape[1];
int64_t inputW = inputShape[2];
Value output = op.getOutput();
auto outputType = cast<ShapedType>(output.getType());
auto outputShape = outputType.getShape();

int64_t outputH = outputShape[0];
int64_t outputW = outputShape[1];

// For F(m x 1, r x 1), we only need to do left side transform.
bool leftTransform = inputH != 1;
bool leftTransform = outputH != 1;
// For F(1 x m, 1 x r), we only need to do right side transform.
bool rightTransform = inputW != 1;
bool rightTransform = outputW != 1;
Value transformedInput =
inputTransform(rewriter, loc, op.getInput(), op.getOutput(), op.getM(),
op.getR(), leftTransform, rightTransform);
Expand Down
Loading
Loading