|
| 1 | +#include "lib/Transforms/SoftmaxToCgfSoftmax/SoftmaxToCgfSoftmax.h" |
| 2 | + |
| 3 | +#include <algorithm> |
| 4 | +#include <cmath> |
| 5 | +#include <cstdint> |
| 6 | +#include <utility> |
| 7 | + |
| 8 | +#include "lib/Dialect/MathExt/IR/MathExtOps.h" |
| 9 | +#include "mlir/include/mlir/Dialect/Arith/IR/Arith.h" // from @llvm-project |
| 10 | +#include "mlir/include/mlir/Dialect/Linalg/IR/Linalg.h" // from @llvm-project |
| 11 | +#include "mlir/include/mlir/Dialect/Math/IR/Math.h" // from @llvm-project |
| 12 | +#include "mlir/include/mlir/Dialect/Tensor/IR/Tensor.h" // from @llvm-project |
| 13 | +#include "mlir/include/mlir/Dialect/Utils/StructuredOpsUtils.h" // from @llvm-project |
| 14 | +#include "mlir/include/mlir/IR/AffineMap.h" // from @llvm-project |
| 15 | +#include "mlir/include/mlir/IR/BuiltinAttributes.h" // from @llvm-project |
| 16 | +#include "mlir/include/mlir/IR/BuiltinTypes.h" // from @llvm-project |
| 17 | +#include "mlir/include/mlir/IR/Location.h" // from @llvm-project |
| 18 | +#include "mlir/include/mlir/IR/PatternMatch.h" // from @llvm-project |
| 19 | +#include "mlir/include/mlir/IR/TypeRange.h" // from @llvm-project |
| 20 | +#include "mlir/include/mlir/IR/Types.h" // from @llvm-project |
| 21 | +#include "mlir/include/mlir/IR/Value.h" // from @llvm-project |
| 22 | +#include "mlir/include/mlir/Support/LLVM.h" // from @llvm-project |
| 23 | +#include "mlir/include/mlir/Transforms/WalkPatternRewriteDriver.h" // from @llvm-project |
| 24 | + |
| 25 | +namespace mlir { |
| 26 | +namespace heir { |
| 27 | + |
| 28 | +#define GEN_PASS_DEF_SOFTMAXTOCGFSOFTMAX |
| 29 | +#include "lib/Transforms/SoftmaxToCgfSoftmax/SoftmaxToCgfSoftmax.h.inc" |
| 30 | + |
| 31 | +namespace { |
| 32 | + |
| 33 | +// Helper to create a linalg.reduce sum operation. |
| 34 | +// Returns the reduced tensor. |
| 35 | +Value createSumReduction(PatternRewriter& rewriter, Location loc, Value input, |
| 36 | + Type elemType, int64_t reductionDim) { |
| 37 | + auto inputType = cast<RankedTensorType>(input.getType()); |
| 38 | + auto inputShape = inputType.getShape(); |
| 39 | + SmallVector<int64_t> outputShape; |
| 40 | + for (int i = 0; i < inputType.getRank(); ++i) { |
| 41 | + if (i != reductionDim) { |
| 42 | + outputShape.push_back(inputShape[i]); |
| 43 | + } |
| 44 | + } |
| 45 | + auto outputType = RankedTensorType::get(outputShape, elemType); |
| 46 | + auto splatAttr = |
| 47 | + DenseElementsAttr::get(outputType, rewriter.getFloatAttr(elemType, 0.0)); |
| 48 | + Value filled = arith::ConstantOp::create(rewriter, loc, splatAttr); |
| 49 | + |
| 50 | + SmallVector<int64_t> dimensions = {reductionDim}; |
| 51 | + auto reduceOp = |
| 52 | + linalg::ReduceOp::create(rewriter, loc, |
| 53 | + /*resultTypes=*/TypeRange{filled.getType()}, |
| 54 | + /*inputs=*/ValueRange{input}, |
| 55 | + /*inits=*/ValueRange{filled}, |
| 56 | + /*dimensions=*/dimensions); |
| 57 | + |
| 58 | + { |
| 59 | + OpBuilder::InsertionGuard guard(rewriter); |
| 60 | + Block* body = |
| 61 | + rewriter.createBlock(&reduceOp.getRegion(), reduceOp.getRegion().end(), |
| 62 | + TypeRange{elemType, elemType}, {loc, loc}); |
| 63 | + Value add = arith::AddFOp::create(rewriter, loc, body->getArgument(0), |
| 64 | + body->getArgument(1)); |
| 65 | + linalg::YieldOp::create(rewriter, loc, add); |
| 66 | + } |
| 67 | + return reduceOp.getResult(0); |
| 68 | +} |
| 69 | + |
| 70 | +struct SoftmaxToCgfSoftmaxPattern |
| 71 | + : public OpRewritePattern<math_ext::SoftmaxOp> { |
| 72 | + using OpRewritePattern<math_ext::SoftmaxOp>::OpRewritePattern; |
| 73 | + |
| 74 | + LogicalResult matchAndRewrite(math_ext::SoftmaxOp op, |
| 75 | + PatternRewriter& rewriter) const override { |
| 76 | + Location loc = op.getLoc(); |
| 77 | + Value input = op.getValue(); |
| 78 | + auto inputType = cast<RankedTensorType>(input.getType()); |
| 79 | + assert(inputType.hasStaticShape() && "only static shapes are supported"); |
| 80 | + int64_t rank = inputType.getRank(); |
| 81 | + assert((rank == 1 || rank == 2) && "only 1D and 2D tensors are supported"); |
| 82 | + |
| 83 | + Type elemType = inputType.getElementType(); |
| 84 | + auto inputShape = inputType.getShape(); |
| 85 | + int64_t n = inputShape[rank - 1]; |
| 86 | + double n_double = static_cast<double>(n); |
| 87 | + |
| 88 | + Value invNConst = arith::ConstantOp::create( |
| 89 | + rewriter, loc, rewriter.getFloatAttr(elemType, 1.0 / n_double)); |
| 90 | + Value halfConst = arith::ConstantOp::create( |
| 91 | + rewriter, loc, rewriter.getFloatAttr(elemType, 0.5)); |
| 92 | + Value lnNConst = arith::ConstantOp::create( |
| 93 | + rewriter, loc, rewriter.getFloatAttr(elemType, std::log(n_double))); |
| 94 | + |
| 95 | + int64_t reductionDim = rank - 1; |
| 96 | + SmallVector<int64_t> reductionShape(inputShape.begin(), inputShape.end()); |
| 97 | + reductionShape.erase(reductionShape.begin() + reductionDim); |
| 98 | + auto reductionType = RankedTensorType::get(reductionShape, elemType); |
| 99 | + |
| 100 | + // 1. Compute mean (mu) |
| 101 | + Value sum = |
| 102 | + createSumReduction(rewriter, loc, input, elemType, reductionDim); |
| 103 | + Value invNConstSplat = |
| 104 | + tensor::SplatOp::create(rewriter, loc, reductionType, invNConst); |
| 105 | + Value mu = arith::MulFOp::create(rewriter, loc, sum, invNConstSplat); |
| 106 | + |
| 107 | + // 2. Compute variance (sigma^2) |
| 108 | + Value initTensor = |
| 109 | + tensor::EmptyOp::create(rewriter, loc, inputShape, elemType); |
| 110 | + |
| 111 | + // Broadcast mu along the reduced dimension |
| 112 | + Value muBroadcast = |
| 113 | + linalg::BroadcastOp::create(rewriter, loc, mu, initTensor, |
| 114 | + ArrayRef<int64_t>{reductionDim}) |
| 115 | + .getResults()[0]; |
| 116 | + Value diff = arith::SubFOp::create(rewriter, loc, input, muBroadcast); |
| 117 | + Value diffSq = arith::MulFOp::create(rewriter, loc, diff, diff); |
| 118 | + |
| 119 | + Value sumDiffSq = |
| 120 | + createSumReduction(rewriter, loc, diffSq, elemType, reductionDim); |
| 121 | + Value sigmaSq = |
| 122 | + arith::MulFOp::create(rewriter, loc, sumDiffSq, invNConstSplat); |
| 123 | + |
| 124 | + // 3. Compute shift S = mu + sigma_sq / 2 + ln(n) |
| 125 | + Value halfSplat = |
| 126 | + tensor::SplatOp::create(rewriter, loc, reductionType, halfConst); |
| 127 | + Value lnNSplat = |
| 128 | + tensor::SplatOp::create(rewriter, loc, reductionType, lnNConst); |
| 129 | + Value halfSigmaSq = |
| 130 | + arith::MulFOp::create(rewriter, loc, sigmaSq, halfSplat); |
| 131 | + Value muPlusHalfSigmaSq = |
| 132 | + arith::AddFOp::create(rewriter, loc, mu, halfSigmaSq); |
| 133 | + Value shift = |
| 134 | + arith::AddFOp::create(rewriter, loc, muPlusHalfSigmaSq, lnNSplat); |
| 135 | + |
| 136 | + // 4. Shift inputs and apply exp: result = exp(input - shift) |
| 137 | + double L_val = |
| 138 | + op->hasAttr("domain_lower") |
| 139 | + ? cast<FloatAttr>(op->getAttr("domain_lower")).getValueAsDouble() |
| 140 | + : -1.0; |
| 141 | + double U_val = |
| 142 | + op->hasAttr("domain_upper") |
| 143 | + ? cast<FloatAttr>(op->getAttr("domain_upper")).getValueAsDouble() |
| 144 | + : 1.0; |
| 145 | + double est_lower = |
| 146 | + L_val - |
| 147 | + (U_val + (U_val - L_val) * (U_val - L_val) / 8.0 + std::log(n_double)); |
| 148 | + double safe_lower = std::max(est_lower, -16.0); |
| 149 | + |
| 150 | + Value shiftBroadcast = |
| 151 | + linalg::BroadcastOp::create(rewriter, loc, shift, initTensor, |
| 152 | + ArrayRef<int64_t>{reductionDim}) |
| 153 | + .getResults()[0]; |
| 154 | + Value shiftedInput = |
| 155 | + arith::SubFOp::create(rewriter, loc, input, shiftBroadcast); |
| 156 | + auto expOp = math::ExpOp::create(rewriter, loc, shiftedInput); |
| 157 | + expOp->setAttr("domain_lower", rewriter.getF64FloatAttr(safe_lower)); |
| 158 | + expOp->setAttr("domain_upper", rewriter.getF64FloatAttr(0.5)); |
| 159 | + |
| 160 | + rewriter.replaceOp(op, expOp->getResults()); |
| 161 | + return success(); |
| 162 | + } |
| 163 | +}; |
| 164 | + |
| 165 | +} // namespace |
| 166 | + |
| 167 | +struct SoftmaxToCgfSoftmaxPass |
| 168 | + : public impl::SoftmaxToCgfSoftmaxBase<SoftmaxToCgfSoftmaxPass> { |
| 169 | + void runOnOperation() override { |
| 170 | + MLIRContext* context = &getContext(); |
| 171 | + Operation* op = getOperation(); |
| 172 | + |
| 173 | + // Pre-check for errors/warnings on domain width. |
| 174 | + WalkResult walkResult = op->walk([](math_ext::SoftmaxOp softmaxOp) { |
| 175 | + auto lowerAttr = |
| 176 | + dyn_cast_or_null<FloatAttr>(softmaxOp->getAttr("domain_lower")); |
| 177 | + auto upperAttr = |
| 178 | + dyn_cast_or_null<FloatAttr>(softmaxOp->getAttr("domain_upper")); |
| 179 | + if (lowerAttr && upperAttr) { |
| 180 | + double L = lowerAttr.getValueAsDouble(); |
| 181 | + double U = upperAttr.getValueAsDouble(); |
| 182 | + double width = U - L; |
| 183 | + if (width > 4.0) { |
| 184 | + softmaxOp->emitOpError() |
| 185 | + << "input domain width (" << width |
| 186 | + << ") exceeds the maximum safe limit (4.0) for CGF-softmax " |
| 187 | + "approximation"; |
| 188 | + return WalkResult::interrupt(); |
| 189 | + } else if (width > 2.0) { |
| 190 | + softmaxOp->emitWarning() |
| 191 | + << "input domain width (" << width |
| 192 | + << ") exceeds the recommended safe limit (2.0) for CGF-softmax " |
| 193 | + "approximation. Accuracy may degrade."; |
| 194 | + } |
| 195 | + } |
| 196 | + return WalkResult::advance(); |
| 197 | + }); |
| 198 | + |
| 199 | + if (walkResult.wasInterrupted()) { |
| 200 | + signalPassFailure(); |
| 201 | + return; |
| 202 | + } |
| 203 | + |
| 204 | + RewritePatternSet patterns(context); |
| 205 | + patterns.add<SoftmaxToCgfSoftmaxPattern>(context); |
| 206 | + |
| 207 | + walkAndApplyPatterns(op, std::move(patterns)); |
| 208 | + } |
| 209 | +}; |
| 210 | + |
| 211 | +} // namespace heir |
| 212 | +} // namespace mlir |
0 commit comments