Skip to content
Open
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
9 changes: 9 additions & 0 deletions mlir/include/mlir/Dialect/Linalg/Utils/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,15 @@ GenericOp makeMemRefCopyOp(OpBuilder &b, Location loc, Value from, Value to);
std::optional<SmallVector<ReassociationIndices>>
getReassociationMapForFoldingUnitDims(ArrayRef<OpFoldResult> mixedSizes);

//===----------------------------------------------------------------------===//
// Convolution matcher utility
//===----------------------------------------------------------------------===//

template <typename ConvOpTy>
bool isaConvolutionOpOfType(LinalgOp op,
SmallVector<int64_t> *dilations = nullptr,
SmallVector<int64_t> *strides = nullptr);

//===----------------------------------------------------------------------===//
// Fusion / Tiling utilities
//===----------------------------------------------------------------------===//
Expand Down
79 changes: 79 additions & 0 deletions mlir/lib/Dialect/Linalg/Transforms/Specialize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,80 @@ static FailureOr<LinalgOp> specializeLinalgContractions(RewriterBase &rewriter,
return replaceWithMatmulVariant<MatmulOp>(rewriter, genericOp);
}

/// Utility to specialize a `genericOp` with a convolution op of type `ConvOpTy`
/// with `dilations` and `strides`.
template <typename ConvOpTy>
static FailureOr<LinalgOp>
specializeToConvOp(RewriterBase &rewriter, GenericOp genericOp,
ArrayRef<int64_t> dilations, ArrayRef<int64_t> strides) {
SmallVector<Value> inputs = genericOp.getDpsInputs();
ValueRange outputs = genericOp.getDpsInits();
SmallVector<AffineMap> indexingMaps = genericOp.getIndexingMapsArray();
SmallVector<Type> resultTypes = genericOp.hasPureTensorSemantics()
? TypeRange(ValueRange(outputs))
: TypeRange{};
LinalgOp namedOp;
if constexpr (std::is_same_v<ConvOpTy, linalg::Conv1DOp> ||
std::is_same_v<ConvOpTy, linalg::Conv2DOp> ||
std::is_same_v<ConvOpTy, linalg::Conv3DOp>) {
namedOp = rewriter.replaceOpWithNewOp<ConvOpTy>(genericOp, resultTypes,
inputs, outputs);
} else {
Attribute stridesAttr = rewriter.getI64TensorAttr(strides);
Attribute dilationsAttr = rewriter.getI64TensorAttr(dilations);
namedOp = rewriter.replaceOpWithNewOp<ConvOpTy>(
genericOp, resultTypes, inputs, outputs, stridesAttr, dilationsAttr);
}
return namedOp;
}

// Converts linalg.generic to named linalg.*conv/pooling* where possible. To
// improve the search speed, the convolution ops have been segregated based on
// the rank of iterator types array.
static FailureOr<LinalgOp> specializeLinalgConvolutions(RewriterBase &rewriter,
GenericOp genericOp) {
SmallVector<int64_t> dilations, strides;
// -----------------------------
// Depthwise Convolution ops.
// -----------------------------
if (isaConvolutionOpOfType<linalg::DepthwiseConv1DNwcWcOp>(
genericOp, &dilations, &strides))
return specializeToConvOp<linalg::DepthwiseConv1DNwcWcOp>(
rewriter, genericOp, dilations, strides);
if (isaConvolutionOpOfType<linalg::DepthwiseConv2DNchwChwOp>(
genericOp, &dilations, &strides))
return specializeToConvOp<linalg::DepthwiseConv2DNchwChwOp>(
rewriter, genericOp, dilations, strides);
if (isaConvolutionOpOfType<linalg::DepthwiseConv3DNdhwcDhwcmOp>(
genericOp, &dilations, &strides))
return specializeToConvOp<linalg::DepthwiseConv3DNdhwcDhwcmOp>(
rewriter, genericOp, dilations, strides);
// -----------------------------
// Pooling ops.
// -----------------------------
if (isaConvolutionOpOfType<linalg::PoolingNhwcMaxOp>(genericOp, &dilations,
&strides))
return specializeToConvOp<linalg::PoolingNhwcMaxOp>(rewriter, genericOp,
dilations, strides);
if (isaConvolutionOpOfType<linalg::PoolingNhwcMinOp>(genericOp, &dilations,
&strides))
return specializeToConvOp<linalg::PoolingNhwcMinOp>(rewriter, genericOp,
dilations, strides);
if (isaConvolutionOpOfType<linalg::PoolingNhwcSumOp>(genericOp, &dilations,
&strides))
return specializeToConvOp<linalg::PoolingNhwcSumOp>(rewriter, genericOp,
dilations, strides);
if (isaConvolutionOpOfType<linalg::PoolingNhwcMaxUnsignedOp>(
genericOp, &dilations, &strides))
return specializeToConvOp<linalg::PoolingNhwcMaxUnsignedOp>(
rewriter, genericOp, dilations, strides);
if (isaConvolutionOpOfType<linalg::PoolingNhwcMinUnsignedOp>(
genericOp, &dilations, &strides))
return specializeToConvOp<linalg::PoolingNhwcMinUnsignedOp>(
rewriter, genericOp, dilations, strides);
return failure();
}

} // namespace

//===----------------------------------------------------------------------===//
Expand Down Expand Up @@ -316,6 +390,11 @@ FailureOr<LinalgOp> mlir::linalg::specializeGenericOp(RewriterBase &rewriter,
if (isaContractionOpInterface(genericOp)) {
return specializeLinalgContractions(rewriter, genericOp);
}

// Convolution - e.g. *conv/pooling*
if (isaConvolutionOpInterface(genericOp)) {
return specializeLinalgConvolutions(rewriter, genericOp);
}
return failure();
}

Expand Down
Loading