| 1 | //===- Simplify.cpp - Shard Simplify ----------------------------*- C++ -*-===// |
| 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 | #include "mlir/Dialect/Shard/Transforms/Simplify.h" |
| 10 | #include "TransformsDetail.h" |
| 11 | #include "mlir/Dialect/Arith/IR/Arith.h" |
| 12 | #include "mlir/Dialect/Shard/IR/ShardDialect.h" |
| 13 | #include "mlir/Dialect/Shard/IR/ShardOps.h" |
| 14 | #include "mlir/Dialect/Shard/Transforms/Passes.h" |
| 15 | #include "mlir/IR/BuiltinTypeInterfaces.h" |
| 16 | #include "mlir/IR/PatternMatch.h" |
| 17 | #include "mlir/IR/SymbolTable.h" |
| 18 | #include "mlir/Transforms/GreedyPatternRewriteDriver.h" |
| 19 | #include "llvm/ADT/STLExtras.h" |
| 20 | #include "llvm/ADT/SmallVector.h" |
| 21 | #include <numeric> |
| 22 | #include <type_traits> |
| 23 | |
| 24 | namespace mlir { |
| 25 | namespace shard { |
| 26 | |
| 27 | #define GEN_PASS_DEF_SHARDSIMPLIFY |
| 28 | #include "mlir/Dialect/Shard/Transforms/Passes.h.inc" |
| 29 | |
| 30 | namespace { |
| 31 | |
| 32 | template <typename LhsOp, typename RhsOp> |
| 33 | static bool haveSameGridAndGridAxes(LhsOp lhsOp, RhsOp rhsOp) { |
| 34 | return lhsOp.getGrid() == rhsOp.getGrid() && |
| 35 | lhsOp.getGridAxes() == rhsOp.getGridAxes(); |
| 36 | } |
| 37 | |
| 38 | static bool isAllGatherAllSliceFoldable(AllGatherOp gatherOp, |
| 39 | AllSliceOp sliceOp) { |
| 40 | return haveSameGridAndGridAxes(lhsOp: gatherOp, rhsOp: sliceOp) && |
| 41 | gatherOp.getGatherAxis() == sliceOp.getSliceAxis(); |
| 42 | } |
| 43 | |
| 44 | template <typename OuterOp, typename InnerOp> |
| 45 | static LogicalResult foldAllGatherAllSlice(OuterOp outerOp, InnerOp innerOp, |
| 46 | PatternRewriter &rewriter) { |
| 47 | if (!innerOp) |
| 48 | return failure(); |
| 49 | |
| 50 | AllGatherOp gatherOp; |
| 51 | AllSliceOp sliceOp; |
| 52 | if constexpr (std::is_same_v<OuterOp, AllGatherOp>) { |
| 53 | gatherOp = outerOp; |
| 54 | sliceOp = innerOp; |
| 55 | } else { |
| 56 | gatherOp = innerOp; |
| 57 | sliceOp = outerOp; |
| 58 | } |
| 59 | |
| 60 | if (!isAllGatherAllSliceFoldable(gatherOp, sliceOp)) |
| 61 | return failure(); |
| 62 | |
| 63 | rewriter.replaceOp(outerOp, innerOp.getInput()); |
| 64 | return success(); |
| 65 | } |
| 66 | |
| 67 | // This folding can not be done with an operation's fold method or |
| 68 | // DialectFoldInterface, because it needs a SymbolTableCollection to cache the |
| 69 | // symbol tables. |
| 70 | // We can't use DialectFoldInterface since the cache may be invalidated by some |
| 71 | // pass changing the referenced GridOp ops. |
| 72 | struct GridShapeFolder |
| 73 | : OpRewritePatternWithSymbolTableCollection<GridShapeOp> { |
| 74 | using OpRewritePatternWithSymbolTableCollection:: |
| 75 | OpRewritePatternWithSymbolTableCollection; |
| 76 | LogicalResult matchAndRewrite(GridShapeOp op, |
| 77 | PatternRewriter &rewriter) const override { |
| 78 | ImplicitLocOpBuilder builder(op->getLoc(), rewriter); |
| 79 | GridOp grid = symbolTableCollection.lookupNearestSymbolFrom<shard::GridOp>( |
| 80 | from: op.getOperation(), symbol: op.getGridAttr()); |
| 81 | if (!grid) { |
| 82 | return failure(); |
| 83 | } |
| 84 | ArrayRef<GridAxis> opGridAxes = op.getAxes(); |
| 85 | SmallVector<GridAxis> opAxesIota; |
| 86 | if (opGridAxes.empty()) { |
| 87 | opAxesIota.resize(N: grid.getRank()); |
| 88 | std::iota(first: opAxesIota.begin(), last: opAxesIota.end(), value: 0); |
| 89 | opGridAxes = opAxesIota; |
| 90 | } |
| 91 | if (llvm::all_of(Range&: opGridAxes, P: [&grid](GridAxis axis) { |
| 92 | return ShapedType::isDynamic(dValue: grid.getShape()[axis]); |
| 93 | })) { |
| 94 | // All grid dimensions are dynamic. Nothing to fold. |
| 95 | return failure(); |
| 96 | } |
| 97 | |
| 98 | SmallVector<Value> newResults(op->getResults().size()); |
| 99 | SmallVector<GridAxis> newShapeOpGridAxes; |
| 100 | SmallVector<size_t> newToOldResultsIndexMap; |
| 101 | |
| 102 | for (size_t i = 0; i < opGridAxes.size(); ++i) { |
| 103 | auto gridAxisSize = grid.getShape()[opGridAxes[i]]; |
| 104 | if (ShapedType::isDynamic(dValue: gridAxisSize)) { |
| 105 | newToOldResultsIndexMap.push_back(Elt: i); |
| 106 | newShapeOpGridAxes.push_back(Elt: opGridAxes[i]); |
| 107 | } else { |
| 108 | // Fold static grid axes. |
| 109 | newResults[i] = arith::ConstantOp::create( |
| 110 | builder, value: builder.getIndexAttr(value: gridAxisSize)); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | // Leave only the dynamic grid axes to be queried. |
| 115 | if (!newShapeOpGridAxes.empty()) { |
| 116 | GridShapeOp newShapeOp = |
| 117 | GridShapeOp::create(builder, grid: grid.getSymName(), axes: newShapeOpGridAxes); |
| 118 | for (size_t i = 0; i < newShapeOp->getResults().size(); ++i) { |
| 119 | newResults[newToOldResultsIndexMap[i]] = newShapeOp->getResults()[i]; |
| 120 | } |
| 121 | } |
| 122 | rewriter.replaceOp(op, newValues: newResults); |
| 123 | |
| 124 | return success(); |
| 125 | } |
| 126 | }; |
| 127 | |
| 128 | // Simplify AllSliceOp(AllReduceOp) -> ReduceScatterOp when both ops share the |
| 129 | // same grid and grid_axes. |
| 130 | // |
| 131 | // AllReduceOp performs an element-wise reduction across all devices in the |
| 132 | // group, and AllSliceOp then slices (scatters) the result along a tensor |
| 133 | // dimension. This is exactly what ReduceScatterOp does in a single collective. |
| 134 | // |
| 135 | // With a ring algorithm over N ranks and M elements: |
| 136 | // AllReduce: 2*(N-1) steps of M/N each => ~2M total data transferred |
| 137 | // AllSlice: local slice, no communication |
| 138 | // ReduceScatter: (N-1) steps of M/N each => ~M total data transferred |
| 139 | // So this fusion roughly halves the communication volume. |
| 140 | // |
| 141 | // Memory-wise, AllReduce produces a full-sized M-element result that the |
| 142 | // subsequent AllSlice must keep alive until the slice is taken. ReduceScatter |
| 143 | // only materializes the M/N-element local slice, reducing peak memory by |
| 144 | // a factor of N. |
| 145 | struct AllReduceAllSliceSimplification : OpRewritePattern<AllSliceOp> { |
| 146 | using Base::Base; |
| 147 | |
| 148 | LogicalResult matchAndRewrite(AllSliceOp sliceOp, |
| 149 | PatternRewriter &rewriter) const override { |
| 150 | // Check if the input to AllSliceOp is produced by an AllReduceOp. |
| 151 | auto reduceOp = sliceOp.getInput().getDefiningOp<AllReduceOp>(); |
| 152 | if (!reduceOp || !reduceOp->hasOneUse()) |
| 153 | return failure(); |
| 154 | |
| 155 | // Both ops must operate on the same grid and grid axes. |
| 156 | if (!haveSameGridAndGridAxes(lhsOp: reduceOp, rhsOp: sliceOp)) |
| 157 | return failure(); |
| 158 | |
| 159 | // Replace with a single ReduceScatterOp. |
| 160 | rewriter.replaceOpWithNewOp<ReduceScatterOp>( |
| 161 | op: sliceOp, args: sliceOp.getResult().getType(), args: sliceOp.getGridAttr(), |
| 162 | args: sliceOp.getGridAxesAttr(), args: reduceOp.getInput(), |
| 163 | args: reduceOp.getReductionAttr(), args: sliceOp.getSliceAxisAttr()); |
| 164 | |
| 165 | return success(); |
| 166 | } |
| 167 | }; |
| 168 | |
| 169 | // Simplify all_slice(all_gather(x)) and all_gather(all_slice(x)) to x when |
| 170 | // both ops share grid, grid_axes, and axis. |
| 171 | template <typename OuterOp, typename InnerOp> |
| 172 | struct AllGatherAllSliceSimplification : OpRewritePattern<OuterOp> { |
| 173 | using OpRewritePattern<OuterOp>::OpRewritePattern; |
| 174 | |
| 175 | LogicalResult matchAndRewrite(OuterOp outerOp, |
| 176 | PatternRewriter &rewriter) const override { |
| 177 | auto innerOp = outerOp.getInput().template getDefiningOp<InnerOp>(); |
| 178 | return foldAllGatherAllSlice(outerOp, innerOp, rewriter); |
| 179 | } |
| 180 | }; |
| 181 | |
| 182 | } // namespace |
| 183 | |
| 184 | void populateSimplifyPatterns(RewritePatternSet &patterns, |
| 185 | SymbolTableCollection &symbolTableCollection) { |
| 186 | populateAllReduceEndomorphismSimplifyPatterns<arith::AddFOp>( |
| 187 | patterns, reduction: ReductionKind::Sum); |
| 188 | populateAllReduceEndomorphismSimplifyPatterns<arith::AddIOp>( |
| 189 | patterns, reduction: ReductionKind::Sum); |
| 190 | |
| 191 | populateAllReduceEndomorphismSimplifyPatterns<arith::MinimumFOp>( |
| 192 | patterns, reduction: ReductionKind::Min); |
| 193 | populateAllReduceEndomorphismSimplifyPatterns<arith::MinSIOp>( |
| 194 | patterns, reduction: ReductionKind::Min); |
| 195 | populateAllReduceEndomorphismSimplifyPatterns<arith::MinUIOp>( |
| 196 | patterns, reduction: ReductionKind::Min); |
| 197 | |
| 198 | populateAllReduceEndomorphismSimplifyPatterns<arith::MaximumFOp>( |
| 199 | patterns, reduction: ReductionKind::Max); |
| 200 | populateAllReduceEndomorphismSimplifyPatterns<arith::MaxSIOp>( |
| 201 | patterns, reduction: ReductionKind::Max); |
| 202 | populateAllReduceEndomorphismSimplifyPatterns<arith::MaxUIOp>( |
| 203 | patterns, reduction: ReductionKind::Max); |
| 204 | |
| 205 | patterns.add<AllReduceAllSliceSimplification, |
| 206 | AllGatherAllSliceSimplification<AllSliceOp, AllGatherOp>, |
| 207 | AllGatherAllSliceSimplification<AllGatherOp, AllSliceOp>>( |
| 208 | arg: patterns.getContext()); |
| 209 | |
| 210 | // TODO: add simplify patterns for all-gather and other collectives. |
| 211 | |
| 212 | populateFoldingPatterns(patterns, symbolTableCollection); |
| 213 | } |
| 214 | |
| 215 | void populateFoldingPatterns(RewritePatternSet &patterns, |
| 216 | SymbolTableCollection &symbolTableCollection) { |
| 217 | patterns.add<GridShapeFolder>(arg&: symbolTableCollection, args: patterns.getContext()); |
| 218 | } |
| 219 | |
| 220 | namespace { |
| 221 | |
| 222 | struct ShardSimplifyPass : public impl::ShardSimplifyBase<ShardSimplifyPass> { |
| 223 | |
| 224 | void runOnOperation() override { |
| 225 | RewritePatternSet patterns(&getContext()); |
| 226 | SymbolTableCollection symbolTableCollection; |
| 227 | populateSimplifyPatterns(patterns, symbolTableCollection); |
| 228 | if (failed(Result: applyPatternsGreedily(op: getOperation(), patterns: std::move(patterns)))) |
| 229 | signalPassFailure(); |
| 230 | } |
| 231 | }; |
| 232 | |
| 233 | } // namespace |
| 234 | |
| 235 | } // namespace shard |
| 236 | } // namespace mlir |
| 237 | |