| 1 | //===- NamedOpConversions.cpp - Implements conversions between named ops --===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file implements conversions between named ops that can be seens as |
| 10 | // canonicalizations of named ops. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "mlir/Dialect/Linalg/Passes.h" |
| 15 | |
| 16 | #include "mlir/Dialect/Linalg/IR/Linalg.h" |
| 17 | #include "mlir/Dialect/Linalg/Transforms/Transforms.h" |
| 18 | #include "mlir/IR/PatternMatch.h" |
| 19 | #include "mlir/Transforms/GreedyPatternRewriteDriver.h" |
| 20 | #include "llvm/ADT/SmallVector.h" |
| 21 | #include "llvm/ADT/TypeSwitch.h" |
| 22 | |
| 23 | namespace mlir { |
| 24 | #define GEN_PASS_DEF_SIMPLIFYDEPTHWISECONVPASS |
| 25 | #include "mlir/Dialect/Linalg/Passes.h.inc" |
| 26 | } // namespace mlir |
| 27 | |
| 28 | using namespace mlir; |
| 29 | using namespace mlir::linalg; |
| 30 | |
| 31 | static llvm::SmallVector<int64_t> getIndicesVector(int start, int end) { |
| 32 | return llvm::to_vector<2>(Range: llvm::seq<int64_t>(Begin: start, End: end)); |
| 33 | } |
| 34 | |
| 35 | static LogicalResult |
| 36 | matchAndReplaceDepthwiseConv(Operation *operation, Value input, Value kernel, |
| 37 | Value iZp, Value kZp, Value init, Attribute stride, |
| 38 | Attribute dilation, PatternRewriter &rewriter) { |
| 39 | Location loc = operation->getLoc(); |
| 40 | auto linalgOp = dyn_cast<LinalgOp>(Val: operation); |
| 41 | // Exit out on the memref version of this operation. |
| 42 | if (!linalgOp || !linalgOp.hasPureTensorSemantics()) |
| 43 | return failure(); |
| 44 | |
| 45 | auto result = operation->getResult(idx: 0); |
| 46 | |
| 47 | auto kernelTy = dyn_cast<RankedTensorType>(Val: kernel.getType()); |
| 48 | auto initTy = dyn_cast<RankedTensorType>(Val: init.getType()); |
| 49 | auto resultTy = dyn_cast<RankedTensorType>(Val: result.getType()); |
| 50 | if (!kernelTy || !initTy || !resultTy) |
| 51 | return failure(); |
| 52 | |
| 53 | if (kernelTy.getDimSize(idx: 3) != 1) |
| 54 | return failure(); |
| 55 | |
| 56 | // Collapse kernel dims. |
| 57 | SmallVector<ReassociationIndices, 4> collapsedKernelDims = { |
| 58 | getIndicesVector(start: 0, end: 1), getIndicesVector(start: 1, end: 2), getIndicesVector(start: 2, end: 4)}; |
| 59 | auto newKernelTy = RankedTensorType::get( |
| 60 | shape: {kernelTy.getDimSize(idx: 0), kernelTy.getDimSize(idx: 1), kernelTy.getDimSize(idx: 2)}, |
| 61 | elementType: kernelTy.getElementType()); |
| 62 | auto collapsedKernel = tensor::CollapseShapeOp::create( |
| 63 | builder&: rewriter, location: loc, resultType: newKernelTy, src: kernel, reassociation: collapsedKernelDims); |
| 64 | |
| 65 | // Collapse init dims. |
| 66 | SmallVector<ReassociationIndices, 4> collapsedInitDims = { |
| 67 | getIndicesVector(start: 0, end: 1), getIndicesVector(start: 1, end: 2), getIndicesVector(start: 2, end: 3), |
| 68 | getIndicesVector(start: 3, end: 5)}; |
| 69 | auto newInitTy = |
| 70 | RankedTensorType::get(shape: {initTy.getDimSize(idx: 0), initTy.getDimSize(idx: 1), |
| 71 | initTy.getDimSize(idx: 2), initTy.getDimSize(idx: 3)}, |
| 72 | elementType: initTy.getElementType()); |
| 73 | auto collapsedInit = tensor::CollapseShapeOp::create(builder&: rewriter, location: loc, resultType: newInitTy, |
| 74 | src: init, reassociation: collapsedInitDims); |
| 75 | |
| 76 | SmallVector<NamedAttribute> preservedAttrs; |
| 77 | Operation *newConv = |
| 78 | TypeSwitch<Operation *, Operation *>(operation) |
| 79 | .Case(caseFn: [&](DepthwiseConv2DNhwcHwcmOp op) { |
| 80 | preservedAttrs = getPrunedAttributeList(op); |
| 81 | return DepthwiseConv2DNhwcHwcOp::create( |
| 82 | builder&: rewriter, location: loc, resultTensorTypes: newInitTy, inputs: ValueRange{input, collapsedKernel}, |
| 83 | outputs: ValueRange{collapsedInit}, strides: stride, dilations: dilation); |
| 84 | }) |
| 85 | .Case(caseFn: [&](DepthwiseConv2DNhwcHwcmQOp op) { |
| 86 | preservedAttrs = getPrunedAttributeList(op); |
| 87 | return DepthwiseConv2DNhwcHwcQOp::create( |
| 88 | builder&: rewriter, location: loc, resultTensorTypes: newInitTy, |
| 89 | inputs: ValueRange{input, collapsedKernel, iZp, kZp}, |
| 90 | outputs: ValueRange{collapsedInit}, strides: stride, dilations: dilation); |
| 91 | }) |
| 92 | .Default(nullptr); |
| 93 | if (!newConv) |
| 94 | return failure(); |
| 95 | for (auto attr : preservedAttrs) |
| 96 | newConv->setDiscardableAttr(name: attr.getName(), value: attr.getValue()); |
| 97 | |
| 98 | // Expand dimensions back out to |
| 99 | rewriter.replaceOpWithNewOp<tensor::ExpandShapeOp>( |
| 100 | op: operation, args&: resultTy, args: newConv->getResult(idx: 0), args&: collapsedInitDims); |
| 101 | return success(); |
| 102 | } |
| 103 | |
| 104 | namespace { |
| 105 | struct SimplifyDepthwiseConvOp |
| 106 | : public OpRewritePattern<DepthwiseConv2DNhwcHwcmOp> { |
| 107 | using OpRewritePattern<DepthwiseConv2DNhwcHwcmOp>::OpRewritePattern; |
| 108 | |
| 109 | LogicalResult matchAndRewrite(DepthwiseConv2DNhwcHwcmOp op, |
| 110 | PatternRewriter &rewriter) const override { |
| 111 | Operation *operation = op.getOperation(); |
| 112 | Value input = op.getDpsInputOperand(i: 0)->get(); |
| 113 | Value kernel = op.getDpsInputOperand(i: 1)->get(); |
| 114 | Value init = op.getDpsInitOperand(i: 0)->get(); |
| 115 | |
| 116 | auto stride = op.getStrides(); |
| 117 | auto dilation = op.getDilations(); |
| 118 | |
| 119 | return matchAndReplaceDepthwiseConv(operation, input, kernel, iZp: nullptr, |
| 120 | kZp: nullptr, init, stride, dilation, |
| 121 | rewriter); |
| 122 | } |
| 123 | }; |
| 124 | |
| 125 | struct SimplifyDepthwiseConvQOp |
| 126 | : public OpRewritePattern<DepthwiseConv2DNhwcHwcmQOp> { |
| 127 | using OpRewritePattern<DepthwiseConv2DNhwcHwcmQOp>::OpRewritePattern; |
| 128 | |
| 129 | LogicalResult matchAndRewrite(DepthwiseConv2DNhwcHwcmQOp op, |
| 130 | PatternRewriter &rewriter) const override { |
| 131 | Operation *operation = op.getOperation(); |
| 132 | Value input = op.getDpsInputOperand(i: 0)->get(); |
| 133 | Value kernel = op.getDpsInputOperand(i: 1)->get(); |
| 134 | Value iZp = op.getDpsInputOperand(i: 2)->get(); |
| 135 | Value kZp = op.getDpsInputOperand(i: 3)->get(); |
| 136 | Value init = op.getDpsInitOperand(i: 0)->get(); |
| 137 | |
| 138 | auto stride = op.getStrides(); |
| 139 | auto dilation = op.getDilations(); |
| 140 | |
| 141 | return matchAndReplaceDepthwiseConv(operation, input, kernel, iZp, kZp, |
| 142 | init, stride, dilation, rewriter); |
| 143 | } |
| 144 | }; |
| 145 | |
| 146 | struct SimplifyDepthwiseConvPass |
| 147 | : public impl::SimplifyDepthwiseConvPassBase<SimplifyDepthwiseConvPass> { |
| 148 | using impl::SimplifyDepthwiseConvPassBase< |
| 149 | SimplifyDepthwiseConvPass>::SimplifyDepthwiseConvPassBase; |
| 150 | |
| 151 | void runOnOperation() override { |
| 152 | Operation *op = getOperation(); |
| 153 | RewritePatternSet patterns(op->getContext()); |
| 154 | populateSimplifyDepthwiseConvPatterns(patterns); |
| 155 | if (failed(Result: applyPatternsGreedily(op, patterns: std::move(patterns)))) |
| 156 | return signalPassFailure(); |
| 157 | } |
| 158 | }; |
| 159 | } // namespace |
| 160 | |
| 161 | void mlir::linalg::populateSimplifyDepthwiseConvPatterns( |
| 162 | RewritePatternSet &patterns) { |
| 163 | patterns.add<SimplifyDepthwiseConvOp, SimplifyDepthwiseConvQOp>( |
| 164 | arg: patterns.getContext()); |
| 165 | } |
| 166 | |