|
15 | 15 | #include "mlir/Dialect/StandardOps/IR/Ops.h"
|
16 | 16 | #include "mlir/IR/Builders.h"
|
17 | 17 | #include "mlir/Transforms/DialectConversion.h"
|
18 |
| - |
19 | 18 | using namespace mlir;
|
20 | 19 |
|
21 |
| -namespace { |
22 | 20 | /// Expands tanh op into
|
23 | 21 | /// 1) 1-exp^{-2x} / 1+exp^{-2x}, if x => 0
|
24 | 22 | /// 2) exp^{2x}-1 / exp^{2x}+1 , if x < 0
|
25 |
| -struct TanhOpConverter : public OpRewritePattern<math::TanhOp> { |
26 |
| -public: |
27 |
| - using OpRewritePattern::OpRewritePattern; |
28 |
| - |
29 |
| - LogicalResult matchAndRewrite(math::TanhOp op, |
30 |
| - PatternRewriter &rewriter) const final { |
31 |
| - auto floatType = op.operand().getType(); |
32 |
| - Location loc = op.getLoc(); |
33 |
| - auto floatOne = rewriter.getFloatAttr(floatType, 1.0); |
34 |
| - auto floatTwo = rewriter.getFloatAttr(floatType, 2.0); |
35 |
| - Value one = rewriter.create<ConstantOp>(loc, floatOne); |
36 |
| - Value two = rewriter.create<ConstantOp>(loc, floatTwo); |
37 |
| - Value doubledX = rewriter.create<MulFOp>(loc, op.operand(), two); |
38 |
| - |
39 |
| - // Case 1: tanh(x) = 1-exp^{-2x} / 1+exp^{-2x} |
40 |
| - Value negDoubledX = rewriter.create<NegFOp>(loc, doubledX); |
41 |
| - Value exp2x = rewriter.create<math::ExpOp>(loc, negDoubledX); |
42 |
| - Value dividend = rewriter.create<SubFOp>(loc, one, exp2x); |
43 |
| - Value divisor = rewriter.create<AddFOp>(loc, one, exp2x); |
44 |
| - Value positiveRes = rewriter.create<DivFOp>(loc, dividend, divisor); |
45 |
| - |
46 |
| - // Case 2: tanh(x) = exp^{2x}-1 / exp^{2x}+1 |
47 |
| - exp2x = rewriter.create<math::ExpOp>(loc, doubledX); |
48 |
| - dividend = rewriter.create<SubFOp>(loc, exp2x, one); |
49 |
| - divisor = rewriter.create<AddFOp>(loc, exp2x, one); |
50 |
| - Value negativeRes = rewriter.create<DivFOp>(loc, dividend, divisor); |
51 |
| - |
52 |
| - // tanh(x) = x >= 0 ? positiveRes : negativeRes |
53 |
| - auto floatZero = rewriter.getFloatAttr(floatType, 0.0); |
54 |
| - Value zero = rewriter.create<ConstantOp>(loc, floatZero); |
55 |
| - Value cmpRes = |
56 |
| - rewriter.create<CmpFOp>(loc, CmpFPredicate::OGE, op.operand(), zero); |
57 |
| - rewriter.replaceOpWithNewOp<SelectOp>(op, cmpRes, positiveRes, negativeRes); |
58 |
| - return success(); |
59 |
| - } |
60 |
| -}; |
61 |
| -} // namespace |
| 23 | +static LogicalResult convertTanhOp(math::TanhOp op, PatternRewriter &rewriter) { |
| 24 | + auto floatType = op.operand().getType(); |
| 25 | + Location loc = op.getLoc(); |
| 26 | + auto floatOne = rewriter.getFloatAttr(floatType, 1.0); |
| 27 | + auto floatTwo = rewriter.getFloatAttr(floatType, 2.0); |
| 28 | + Value one = rewriter.create<ConstantOp>(loc, floatOne); |
| 29 | + Value two = rewriter.create<ConstantOp>(loc, floatTwo); |
| 30 | + Value doubledX = rewriter.create<MulFOp>(loc, op.operand(), two); |
| 31 | + |
| 32 | + // Case 1: tanh(x) = 1-exp^{-2x} / 1+exp^{-2x} |
| 33 | + Value negDoubledX = rewriter.create<NegFOp>(loc, doubledX); |
| 34 | + Value exp2x = rewriter.create<math::ExpOp>(loc, negDoubledX); |
| 35 | + Value dividend = rewriter.create<SubFOp>(loc, one, exp2x); |
| 36 | + Value divisor = rewriter.create<AddFOp>(loc, one, exp2x); |
| 37 | + Value positiveRes = rewriter.create<DivFOp>(loc, dividend, divisor); |
| 38 | + |
| 39 | + // Case 2: tanh(x) = exp^{2x}-1 / exp^{2x}+1 |
| 40 | + exp2x = rewriter.create<math::ExpOp>(loc, doubledX); |
| 41 | + dividend = rewriter.create<SubFOp>(loc, exp2x, one); |
| 42 | + divisor = rewriter.create<AddFOp>(loc, exp2x, one); |
| 43 | + Value negativeRes = rewriter.create<DivFOp>(loc, dividend, divisor); |
| 44 | + |
| 45 | + // tanh(x) = x >= 0 ? positiveRes : negativeRes |
| 46 | + auto floatZero = rewriter.getFloatAttr(floatType, 0.0); |
| 47 | + Value zero = rewriter.create<ConstantOp>(loc, floatZero); |
| 48 | + Value cmpRes = |
| 49 | + rewriter.create<CmpFOp>(loc, CmpFPredicate::OGE, op.operand(), zero); |
| 50 | + rewriter.replaceOpWithNewOp<SelectOp>(op, cmpRes, positiveRes, negativeRes); |
| 51 | + return success(); |
| 52 | +} |
62 | 53 |
|
63 | 54 | void mlir::populateExpandTanhPattern(OwningRewritePatternList &patterns) {
|
64 |
| - patterns.insert<TanhOpConverter>(patterns.getContext()); |
| 55 | + patterns.insert(convertTanhOp); |
65 | 56 | }
|
0 commit comments