|
| 1 | +//===----------------------------------------------------------------------===// |
| 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 | +// Also available under a BSD-style license. See LICENSE. |
| 7 | +// |
| 8 | +//===----------------------------------------------------------------------===// |
| 9 | + |
| 10 | +#include "PassDetail.h" |
| 11 | + |
| 12 | +#include "mlir/Transforms/GreedyPatternRewriteDriver.h" |
| 13 | +#include "torch-mlir/Dialect/Torch/IR/TorchOps.h" |
| 14 | +#include "torch-mlir/Dialect/Torch/Transforms/Passes.h" |
| 15 | + |
| 16 | +using namespace mlir; |
| 17 | +using namespace mlir::torch; |
| 18 | +using namespace mlir::torch::Torch; |
| 19 | + |
| 20 | +namespace { |
| 21 | +class RecomposeSliceCopy_ : public OpRewritePattern<AtenCopy_Op> { |
| 22 | +public: |
| 23 | + using OpRewritePattern::OpRewritePattern; |
| 24 | + LogicalResult matchAndRewrite(AtenCopy_Op op, |
| 25 | + PatternRewriter &rewriter) const override { |
| 26 | + if (!op.getSelf().getDefiningOp() || |
| 27 | + !isa<AtenSliceTensorOp>(op.getSelf().getDefiningOp())) |
| 28 | + return failure(); |
| 29 | + auto sliceOp = cast<AtenSliceTensorOp>(op.getSelf().getDefiningOp()); |
| 30 | + |
| 31 | + // Get indices |
| 32 | + int64_t dim; |
| 33 | + if (!matchPattern(sliceOp.getDim(), m_TorchConstantInt(&dim))) |
| 34 | + return failure(); |
| 35 | + int64_t end; |
| 36 | + if (!matchPattern(sliceOp.getEnd(), m_TorchConstantInt(&end))) |
| 37 | + return failure(); |
| 38 | + |
| 39 | + Value newEnd = sliceOp.getEnd(); |
| 40 | + if (end < 0) { |
| 41 | + Value dimSize = rewriter.create<AtenSizeIntOp>( |
| 42 | + op.getLoc(), sliceOp.getSelf(), sliceOp.getDim()); |
| 43 | + newEnd = |
| 44 | + rewriter.create<AtenAddIntOp>(op.getLoc(), dimSize, sliceOp.getEnd()); |
| 45 | + } |
| 46 | + |
| 47 | + Value noneVal = rewriter.create<ConstantNoneOp>(op.getLoc()); |
| 48 | + Value falseVal = rewriter.create<ConstantBoolOp>(op.getLoc(), false); |
| 49 | + |
| 50 | + // Create IndexPut_Op |
| 51 | + BaseTensorType tensorType = op->getResultTypes()[0].cast<BaseTensorType>(); |
| 52 | + Value range = rewriter.create<AtenArangeStartStepOp>( |
| 53 | + op.getLoc(), tensorType, sliceOp.getStart(), newEnd, sliceOp.getStep(), |
| 54 | + /*dtype=*/noneVal, /*layout=*/noneVal, /*device=*/noneVal, |
| 55 | + /*pin_memory=*/noneVal); |
| 56 | + |
| 57 | + SmallVector<Value> indicesVector; |
| 58 | + for (auto i = 0; i < dim - 1; i++) |
| 59 | + indicesVector.push_back(noneVal); |
| 60 | + indicesVector.push_back(range); |
| 61 | + Value indices = rewriter.create<PrimListConstructOp>( |
| 62 | + op.getLoc(), |
| 63 | + Torch::ListType::get(op->getContext(), |
| 64 | + Torch::OptionalType::get(tensorType)), |
| 65 | + indicesVector); |
| 66 | + |
| 67 | + rewriter.replaceOpWithNewOp<Aten_IndexPutImpl_Op>( |
| 68 | + op, op->getResultTypes(), sliceOp.getSelf(), indices, op.getSrc(), |
| 69 | + /*accumulate=*/falseVal, /*unsafe=*/falseVal); |
| 70 | + |
| 71 | + return success(); |
| 72 | + } |
| 73 | +}; |
| 74 | +} // namespace |
| 75 | + |
| 76 | +namespace { |
| 77 | +class RecomposeComplexOps |
| 78 | + : public DecomposeComplexOpsBase<RecomposeComplexOps> { |
| 79 | +public: |
| 80 | + RecomposeComplexOps() = default; |
| 81 | + void runOnOperation() override { |
| 82 | + MLIRContext *context = &getContext(); |
| 83 | + RewritePatternSet patterns(context); |
| 84 | + |
| 85 | + // pattern.add calls go here |
| 86 | + patterns.add<RecomposeSliceCopy_>(context); |
| 87 | + |
| 88 | + GreedyRewriteConfig config; |
| 89 | + config.useTopDownTraversal = true; |
| 90 | + config.maxIterations = GreedyRewriteConfig::kNoLimit; |
| 91 | + |
| 92 | + if (failed(applyPatternsAndFoldGreedily(getOperation(), std::move(patterns), |
| 93 | + config))) { |
| 94 | + return signalPassFailure(); |
| 95 | + } |
| 96 | + } |
| 97 | +}; |
| 98 | +} // namespace |
| 99 | + |
| 100 | +std::unique_ptr<OperationPass<func::FuncOp>> |
| 101 | +mlir::torch::Torch::createRecomposeComplexOps() { |
| 102 | + return std::make_unique<RecomposeComplexOps>(); |
| 103 | +} |
0 commit comments