| 1 | //===- ShardingInterfaceImpl.cpp - Tosa sharding models -------------------===// |
| 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/Tosa/Extensions/ShardingInterfaceImpl.h" |
| 10 | #include "mlir/Dialect/Shard/IR/ShardOps.h" |
| 11 | #include "mlir/Dialect/Shard/Interfaces/ShardingInterface.h" |
| 12 | #include "mlir/Dialect/Shard/Interfaces/ShardingInterfaceImpl.h" |
| 13 | #include "mlir/Dialect/Tosa/IR/TosaOps.h" |
| 14 | #include "mlir/IR/AffineMap.h" |
| 15 | #include "mlir/IR/DialectRegistry.h" |
| 16 | |
| 17 | #define DEBUG_TYPE "tosa-sharding-impl" |
| 18 | #define DBGS() (llvm::dbgs() << "[" DEBUG_TYPE << "]: ") |
| 19 | |
| 20 | using namespace mlir; |
| 21 | using namespace mlir::tosa; |
| 22 | using namespace mlir::shard; |
| 23 | |
| 24 | namespace { |
| 25 | // loop types: [parallel, parallel, parallel, reduction_sum] |
| 26 | // indexing maps: |
| 27 | // (d0, d1, d2, d3) -> (d0, d1, d3) |
| 28 | // (d0, d1, d2, d3) -> (d0, d3, d2) |
| 29 | // (d0, d1, d2, d3) -> (d0, d1, d2) |
| 30 | struct MatMulOpSharding |
| 31 | : public ShardingInterface::ExternalModel<MatMulOpSharding, MatMulOp> { |
| 32 | SmallVector<utils::IteratorType> getLoopIteratorTypes(Operation *op) const { |
| 33 | auto tensorType = dyn_cast<RankedTensorType>(Val: op->getResult(idx: 0).getType()); |
| 34 | if (!tensorType) |
| 35 | return {}; |
| 36 | |
| 37 | SmallVector<utils::IteratorType> types(tensorType.getRank() + 1, |
| 38 | utils::IteratorType::parallel); |
| 39 | types[tensorType.getRank()] = utils::IteratorType::reduction; |
| 40 | return types; |
| 41 | } |
| 42 | |
| 43 | SmallVector<ReductionKind> |
| 44 | getReductionLoopIteratorKinds(Operation *op) const { |
| 45 | return SmallVector<ReductionKind>(1, ReductionKind::Sum); |
| 46 | } |
| 47 | |
| 48 | SmallVector<AffineMap> getIndexingMaps(Operation *op) const { |
| 49 | auto tensorType = dyn_cast<RankedTensorType>(Val: op->getResult(idx: 0).getType()); |
| 50 | if (!tensorType) |
| 51 | return {}; |
| 52 | MLIRContext *ctx = op->getContext(); |
| 53 | SmallVector<AffineMap> maps; |
| 54 | maps.push_back(Elt: AffineMap::getMultiDimMapWithTargets(numDims: 4, targets: {0, 1, 3}, context: ctx)); |
| 55 | maps.push_back(Elt: AffineMap::getMultiDimMapWithTargets(numDims: 4, targets: {0, 3, 2}, context: ctx)); |
| 56 | maps.push_back(Elt: AffineMap::get(dimCount: 0, symbolCount: 0, results: {}, context: ctx)); |
| 57 | maps.push_back(Elt: AffineMap::get(dimCount: 0, symbolCount: 0, results: {}, context: ctx)); |
| 58 | maps.push_back(Elt: AffineMap::getMultiDimMapWithTargets(numDims: 4, targets: {0, 1, 2}, context: ctx)); |
| 59 | return maps; |
| 60 | } |
| 61 | }; |
| 62 | |
| 63 | struct NegateOpSharding |
| 64 | : public ShardingInterface::ExternalModel<NegateOpSharding, NegateOp> { |
| 65 | SmallVector<utils::IteratorType> getLoopIteratorTypes(Operation *op) const { |
| 66 | Value val = op->getOperand(idx: 0); |
| 67 | auto type = dyn_cast<RankedTensorType>(Val: val.getType()); |
| 68 | if (!type) |
| 69 | return {}; |
| 70 | SmallVector<utils::IteratorType> types(type.getRank(), |
| 71 | utils::IteratorType::parallel); |
| 72 | return types; |
| 73 | } |
| 74 | |
| 75 | SmallVector<AffineMap> getIndexingMaps(Operation *op) const { |
| 76 | MLIRContext *ctx = op->getContext(); |
| 77 | Value val = op->getOperand(idx: 0); |
| 78 | auto type = dyn_cast<RankedTensorType>(Val: val.getType()); |
| 79 | if (!type) |
| 80 | return {}; |
| 81 | int64_t rank = type.getRank(); |
| 82 | SmallVector<AffineMap> maps = { |
| 83 | AffineMap::getMultiDimIdentityMap(numDims: rank, context: ctx), |
| 84 | AffineMap::get(dimCount: 0, symbolCount: 0, results: {}, context: ctx), AffineMap::get(dimCount: 0, symbolCount: 0, results: {}, context: ctx), |
| 85 | AffineMap::getMultiDimIdentityMap(numDims: rank, context: ctx)}; |
| 86 | return maps; |
| 87 | } |
| 88 | |
| 89 | LogicalResult partition(Operation *op, ArrayRef<Value> partitiondOperands, |
| 90 | ArrayRef<Sharding> operandShardings, |
| 91 | ArrayRef<Sharding> resultShardings, |
| 92 | IRMapping &partitionMap, |
| 93 | SymbolTableCollection &symbolTable, |
| 94 | OpBuilder &builder) const { |
| 95 | partitionTriviallyShardableOperation(op&: *op, partitionedOperands: partitiondOperands, |
| 96 | operandShardings, resultShardings, |
| 97 | partitionMap, symbolTable, builder); |
| 98 | return success(); |
| 99 | } |
| 100 | }; |
| 101 | |
| 102 | template <typename OpType> |
| 103 | static void registerElemwiseOne(MLIRContext *ctx) { |
| 104 | OpType::template attachInterface<ElementwiseShardingInterface<OpType>>(*ctx); |
| 105 | } |
| 106 | |
| 107 | /// Variadic helper function. |
| 108 | template <typename... OpTypes> |
| 109 | static void registerElemwiseAll(MLIRContext *ctx) { |
| 110 | (registerElemwiseOne<OpTypes>(ctx), ...); |
| 111 | } |
| 112 | } // namespace |
| 113 | |
| 114 | void mlir::tosa::registerShardingInterfaceExternalModels( |
| 115 | DialectRegistry ®istry) { |
| 116 | |
| 117 | registry.addExtension(extensionFn: +[](MLIRContext *ctx, TosaDialect *dialect) { |
| 118 | registerElemwiseAll< |
| 119 | ClampOp, SigmoidOp, TanhOp, AddOp, ArithmeticRightShiftOp, BitwiseAndOp, |
| 120 | BitwiseOrOp, BitwiseXorOp, IntDivOp, LogicalAndOp, LogicalLeftShiftOp, |
| 121 | LogicalRightShiftOp, LogicalOrOp, LogicalXorOp, MaximumOp, MinimumOp, |
| 122 | MulOp, PowOp, SubOp, AbsOp, BitwiseNotOp, CeilOp, ClzOp, ExpOp, FloorOp, |
| 123 | LogOp, LogicalNotOp, ReciprocalOp, RsqrtOp, SelectOp, EqualOp, |
| 124 | GreaterOp, GreaterEqualOp>(ctx); |
| 125 | |
| 126 | MatMulOp::attachInterface<MatMulOpSharding>(context&: *ctx); |
| 127 | NegateOp::attachInterface<NegateOpSharding>(context&: *ctx); |
| 128 | }); |
| 129 | } |
| 130 | |