Skip to content

[mlir][linalg] Decompose winograd operators #96183

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 26 commits into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
4240341
[mlir][linalg] Implement Conv2D using Winograd Conv2D algorithm
Hsiangkai Jun 17, 2024
374b0d5
[mlir][linalg] Add transform operator for Winograd Conv2D algorithm
Hsiangkai Jun 17, 2024
24c4f95
[mlir][linalg] Decompose winograd operators
Hsiangkai Jun 17, 2024
c94b1a3
Revert "[mlir][linalg] Add transform operator for Winograd Conv2D alg…
Hsiangkai Jun 26, 2024
5a39188
Revert "[mlir][linalg] Implement Conv2D using Winograd Conv2D algorithm"
Hsiangkai Jun 26, 2024
6906627
[mlir][linalg] Implement Conv2D using Winograd Conv2D algorithm
Hsiangkai Jun 17, 2024
bb80879
[mlir][linalg] Add transform operator for Winograd Conv2D algorithm
Hsiangkai Jun 17, 2024
cc23f43
Address ftynse's comments
Hsiangkai Jun 26, 2024
48e24b4
Revert "[mlir][linalg] Decompose winograd operators"
Hsiangkai Jun 26, 2024
afcddc2
Revert "[mlir][linalg] Add transform operator for Winograd Conv2D alg…
Hsiangkai Jun 26, 2024
67a5701
Revert "[mlir][linalg] Implement Conv2D using Winograd Conv2D algorithm"
Hsiangkai Jun 26, 2024
21afe38
[mlir][linalg] Implement Conv2D using Winograd Conv2D algorithm
Hsiangkai Jun 17, 2024
41c86ea
[mlir][linalg] Add transform operator for Winograd Conv2D algorithm
Hsiangkai Jun 17, 2024
6c4f432
[mlir][linalg] Decompose winograd operators
Hsiangkai Jun 17, 2024
cdf7647
Address ftynse's comments
Hsiangkai Jun 26, 2024
a93529d
fix failed test
Hsiangkai Jun 27, 2024
549029b
fix failed test
Hsiangkai Jun 27, 2024
b08accd
correct the way to broadcast a scalar value
Hsiangkai Jun 30, 2024
fd1568f
Merge branch 'main' into users/hsiangkai/winograd-ops-transform
Hsiangkai Jul 11, 2024
87bf103
Merge branch 'users/hsiangkai/winograd-ops-transform' into users/hsia…
Hsiangkai Jul 11, 2024
0bb0f05
clang-format
Hsiangkai Jul 11, 2024
6e9c95b
Merge branch 'users/hsiangkai/winograd-ops-transform' into users/hsia…
Hsiangkai Jul 11, 2024
dc9cda1
remove redundant include path
Hsiangkai Jul 11, 2024
e60957b
fix clang-format errors
Hsiangkai Jul 11, 2024
5b48c1c
Address Max191's comments
Hsiangkai Jul 12, 2024
f82ec22
Update comments
Hsiangkai Jul 17, 2024
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 @@ -2646,4 +2646,55 @@ def MapCopyToThreadsOp :
}];
}

//===----------------------------------------------------------------------===//
// Winograd Conv2D
//===----------------------------------------------------------------------===//

def WinogradConv2DOp : Op<Transform_Dialect,
"structured.winograd_conv2d",
[FunctionalStyleTransformOpTrait, MemoryEffectsOpInterface,
TransformOpInterface, TransformEachOpTrait,
ReportTrackingListenerFailuresOpTrait]> {
let description = [{
Winograd Conv2D algorithm will convert linalg Conv2D operation into batched
matrix multiply. Before the matrix multiply, it will convert filter and
input into a format suitable for batched matrix multiply. After the matrix
multiply, it will convert output to the final result tensor.

The algorithm F(m x m, r x r) is

Y = A^T x [(G x g x G^T) @ (B^T x d x B)] x A

The size of output Y is m x m. The size of filter g is r x r. The size of
input d is (m + r - 1) x (m + r - 1). A^T, A, G^T, G, B^T, and B are
transformation matrices.

#### Return modes:

This operation produces a silenceable failure if `target` is unsupported.
Otherwise, the operation succeeds and returns a handle of the sequence that
replaces the original convolution.
}];

let arguments = (ins TransformHandleTypeInterface:$target,
I64Attr:$m,
I64Attr:$r);
let results = (outs TransformHandleTypeInterface:$transformed);

let assemblyFormat =
"$target attr-dict `:` functional-type($target, results)";

let builders = [
OpBuilder<(ins "Value":$target)>
];

let extraClassDeclaration = [{
::mlir::DiagnosedSilenceableFailure applyToOne(
::mlir::transform::TransformRewriter &rewriter,
::mlir::linalg::LinalgOp target,
::mlir::transform::ApplyToEachResultList &results,
::mlir::transform::TransformState &state);
}];
}

#endif // LINALG_TRANSFORM_OPS
10 changes: 10 additions & 0 deletions mlir/include/mlir/Dialect/Linalg/Transforms/Transforms.h
Original file line number Diff line number Diff line change
Expand Up @@ -1332,6 +1332,13 @@ FailureOr<Operation *> transposeBatchMatmul(RewriterBase &rewriter,
linalg::BatchMatmulOp op,
bool transposeLHS = true);

/// Convert linalg.conv_2d_nhwc_fhwc to Winograd Conv2D algorithm
/// F(m x m, r x r). m is the dimension size of output and r is the dimension
/// size of filter.
FailureOr<Operation *> winogradConv2D(RewriterBase &rewriter,
linalg::Conv2DNhwcFhwcOp op, int64_t m,
int64_t r);

//===----------------------------------------------------------------------===//
// Rewrite patterns wrapping transformations.
// TODO: every single such pattern should be a close to noop wrapper around a
Expand Down Expand Up @@ -1739,6 +1746,9 @@ void populateBlockPackMatmulPatterns(RewritePatternSet &patterns,
void populateWinogradConv2DPatterns(RewritePatternSet &patterns, int64_t m,
int64_t r);

/// Patterns to decompose Winograd operators.
void populateDecomposeWinogradOpsPatterns(RewritePatternSet &patterns);

/// Adds patterns that reduce the rank of named contraction ops that have
/// unit dimensions in the operand(s) by converting to a sequence of `collapse_shape`,
/// `<corresponding linalg named op>`, `expand_shape` (if on tensors). For example a
Expand Down
31 changes: 31 additions & 0 deletions mlir/lib/Dialect/Linalg/TransformOps/LinalgTransformOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3711,6 +3711,37 @@ DiagnosedSilenceableFailure transform::MapCopyToThreadsOp::applyToOne(
return DiagnosedSilenceableFailure::success();
}

//===----------------------------------------------------------------------===//
// WinogradConv2DOp
//===----------------------------------------------------------------------===//

DiagnosedSilenceableFailure transform::WinogradConv2DOp::applyToOne(
transform::TransformRewriter &rewriter, linalg::LinalgOp target,
transform::ApplyToEachResultList &results,
transform::TransformState &state) {
rewriter.setInsertionPoint(target);
FailureOr<Operation *> maybeTransformed = failure();
bool supported = TypeSwitch<Operation *, bool>(target)
.Case([&](linalg::Conv2DNhwcFhwcOp op) {
maybeTransformed =
winogradConv2D(rewriter, op, getM(), getR());
return true;
})
.Default([&](Operation *op) { return false; });

if (!supported) {
return emitSilenceableError()
<< "this operation is not supported to convert to Winograd Conv2D";
}

if (supported && failed(maybeTransformed)) {
return emitSilenceableError() << "apply Winograd Conv2D failed";
}

results.push_back(*maybeTransformed);
return DiagnosedSilenceableFailure::success();
}

#include "mlir/Dialect/Linalg/TransformOps/LinalgTransformOpsEnums.cpp.inc"

#define GET_OP_CLASSES
Expand Down
Loading