|
17 | 17 | namespace mlir {
|
18 | 18 | namespace onednn_graph {
|
19 | 19 |
|
20 |
| -LogicalResult onednn_graph::AddOp::inferReturnTypeComponents( |
21 |
| - MLIRContext *context, ::std::optional<Location> location, |
22 |
| - ValueShapeRange operands, DictionaryAttr attributes, |
23 |
| - OpaqueProperties properties, RegionRange regions, |
| 20 | +//===----------------------------------------------------------------------===// |
| 21 | +// Binary ops shape infer |
| 22 | +//===----------------------------------------------------------------------===// |
| 23 | + |
| 24 | +#define BINARY_OP_SHAPE_INFER(OP) \ |
| 25 | + LogicalResult OP::inferReturnTypeComponents( \ |
| 26 | + MLIRContext *context, ::std::optional<Location> location, \ |
| 27 | + OP::Adaptor adaptor, \ |
| 28 | + SmallVectorImpl<ShapedTypeComponents> &inferredReturnShapes) { \ |
| 29 | + auto inputTy0 = dyn_cast<ShapedType>(adaptor.getInputA().getType()); \ |
| 30 | + auto inputTy1 = dyn_cast<ShapedType>(adaptor.getInputB().getType()); \ |
| 31 | + if (!adaptor.getAutoBroadcast() && (inputTy0 != inputTy1)) { \ |
| 32 | + return failure(); \ |
| 33 | + } \ |
| 34 | + llvm::SmallVector<int64_t> outShape; \ |
| 35 | + auto ret = OpTrait::util::getBroadcastedShape( \ |
| 36 | + inputTy0.getShape(), inputTy1.getShape(), outShape); \ |
| 37 | + inferredReturnShapes.push_back( \ |
| 38 | + ShapedTypeComponents(outShape, inputTy0.getElementType())); \ |
| 39 | + return LogicalResult::success(ret); \ |
| 40 | + } |
| 41 | + |
| 42 | +BINARY_OP_SHAPE_INFER(onednn_graph::AddOp) |
| 43 | +BINARY_OP_SHAPE_INFER(onednn_graph::MulOp) |
| 44 | +BINARY_OP_SHAPE_INFER(onednn_graph::SubOp) |
| 45 | +BINARY_OP_SHAPE_INFER(onednn_graph::DivOp) |
| 46 | + |
| 47 | +//===----------------------------------------------------------------------===// |
| 48 | +// Reduce ops shape infer |
| 49 | +//===----------------------------------------------------------------------===// |
| 50 | + |
| 51 | +SmallVector<int64_t> canonicalizeReduceAxes(ArrayRef<int64_t> axes, |
| 52 | + int64_t rank) { |
| 53 | + SmallVector<int64_t> ret(axes.size()); |
| 54 | + for (size_t i = 0; i < axes.size(); i++) { |
| 55 | + ret[i] = axes[i] < 0 ? axes[i] + rank : axes[i]; |
| 56 | + } |
| 57 | + llvm::sort(ret); |
| 58 | + ret.erase(std::unique(ret.begin(), ret.end()), ret.end()); |
| 59 | + return ret; |
| 60 | +} |
| 61 | + |
| 62 | +static LogicalResult InferReduceReturnTypes( |
| 63 | + ShapeAdaptor operandShape, Type elemType, ArrayRef<int64_t> axes, |
| 64 | + bool keep_dims, |
24 | 65 | SmallVectorImpl<ShapedTypeComponents> &inferredReturnShapes) {
|
25 |
| - llvm::SmallVector<int64_t> outShape; |
26 |
| - auto resultTy = dyn_cast<ShapedType>(operands.front().getType()); |
27 |
| - auto getShapeIdx = [&operands](size_t i) { |
28 |
| - return operands.getTypes()[i].dyn_cast<ShapedType>().getShape(); |
| 66 | + // no reduce axes |
| 67 | + if (axes.empty()) { |
| 68 | + inferredReturnShapes.push_back(ShapedTypeComponents(operandShape)); |
| 69 | + return success(); |
| 70 | + } |
| 71 | + // get reduce axis one by one |
| 72 | + size_t index = 0; |
| 73 | + auto getNextReduceAxis = [&]() { |
| 74 | + return (index >= axes.size()) ? -1 : axes[index++]; |
29 | 75 | };
|
30 |
| - |
31 |
| - auto ret = OpTrait::util::getBroadcastedShape(getShapeIdx(0), getShapeIdx(1), |
32 |
| - outShape); |
33 |
| - inferredReturnShapes.push_back( |
34 |
| - ShapedTypeComponents(outShape, resultTy.getElementType())); |
35 |
| - return LogicalResult::success(ret); |
| 76 | + // get reduced shape |
| 77 | + auto rank = operandShape.getRank(); |
| 78 | + auto axis = getNextReduceAxis(); |
| 79 | + SmallVector<int64_t> outputShape; |
| 80 | + for (int64_t idx = 0; idx < rank; idx++) { |
| 81 | + if (idx == axis) { |
| 82 | + axis = getNextReduceAxis(); |
| 83 | + if (keep_dims) { |
| 84 | + outputShape.push_back(1); |
| 85 | + } |
| 86 | + } else { |
| 87 | + outputShape.push_back(operandShape.getDimSize(idx)); |
| 88 | + } |
| 89 | + } |
| 90 | + inferredReturnShapes.push_back(ShapedTypeComponents(outputShape, elemType)); |
| 91 | + return success(); |
36 | 92 | }
|
37 | 93 |
|
| 94 | +template <typename ReduceOp> |
| 95 | +struct CanonicalizeReduceOp : public OpRewritePattern<ReduceOp> { |
| 96 | + using OpRewritePattern<ReduceOp>::OpRewritePattern; |
| 97 | + LogicalResult matchAndRewrite(ReduceOp op, |
| 98 | + PatternRewriter &rewriter) const override { |
| 99 | + auto rank = dyn_cast<ShapedType>(op.getOperand().getType()).getRank(); |
| 100 | + // consider canonicalized if all axes are non-negative in ascending order |
| 101 | + int64_t last = -1; |
| 102 | + bool canonicalized = true; |
| 103 | + for (const auto axis : op.getAxes()) { |
| 104 | + if (axis <= last) { |
| 105 | + canonicalized = false; |
| 106 | + break; |
| 107 | + } |
| 108 | + last = axis; |
| 109 | + } |
| 110 | + if (canonicalized) { |
| 111 | + return failure(); |
| 112 | + } |
| 113 | + // canonicalize the reduce axes |
| 114 | + auto axes = canonicalizeReduceAxes(op.getAxes(), rank); |
| 115 | + rewriter.replaceOpWithNewOp<ReduceOp>(op, op.getType(), op.getOperand(), |
| 116 | + axes, op.getKeepDims()); |
| 117 | + return success(); |
| 118 | + } |
| 119 | +}; |
| 120 | + |
| 121 | +#define REDUCE_OP_SHAPE_CANONICALIZE(OP) \ |
| 122 | + void OP::getCanonicalizationPatterns(RewritePatternSet &results, \ |
| 123 | + MLIRContext *context) { \ |
| 124 | + results.add<CanonicalizeReduceOp<OP>>(context); \ |
| 125 | + } |
| 126 | + |
| 127 | +#define REDUCE_OP_SHAPE_INFER(OP) \ |
| 128 | + LogicalResult OP::inferReturnTypeComponents( \ |
| 129 | + MLIRContext *context, ::std::optional<Location> location, \ |
| 130 | + OP::Adaptor adaptor, \ |
| 131 | + SmallVectorImpl<ShapedTypeComponents> &inferredReturnShapes) { \ |
| 132 | + llvm::SmallVector<int64_t> outShape; \ |
| 133 | + auto operandTy = dyn_cast<ShapedType>(adaptor.getOperand().getType()); \ |
| 134 | + auto rank = operandTy.getRank(); \ |
| 135 | + ShapeAdaptor inputShape(operandTy); \ |
| 136 | + return InferReduceReturnTypes( \ |
| 137 | + inputShape, operandTy.getElementType(), \ |
| 138 | + canonicalizeReduceAxes(adaptor.getAxes(), rank), \ |
| 139 | + adaptor.getKeepDims(), inferredReturnShapes); \ |
| 140 | + } |
| 141 | + |
| 142 | +#define REDUCE_OP_VERIFY(OP) \ |
| 143 | + LogicalResult OP::verify() { \ |
| 144 | + auto operandTy = dyn_cast<ShapedType>(getOperand().getType()); \ |
| 145 | + if (!operandTy.hasRank()) { \ |
| 146 | + return emitOpError("Invalid operand shape!\n"); \ |
| 147 | + } \ |
| 148 | + int64_t rank = operandTy.getRank(); \ |
| 149 | + for (const auto axis : canonicalizeReduceAxes(getAxes(), rank)) { \ |
| 150 | + if (axis >= rank || axis < 0) { \ |
| 151 | + return emitOpError("Reduce axis not valid!\n"); \ |
| 152 | + } \ |
| 153 | + } \ |
| 154 | + return success(); \ |
| 155 | + } |
| 156 | + |
| 157 | +#define REDUCE_OP_DEFINE(OP) \ |
| 158 | + REDUCE_OP_SHAPE_CANONICALIZE(OP) \ |
| 159 | + REDUCE_OP_SHAPE_INFER(OP) \ |
| 160 | + REDUCE_OP_VERIFY(OP) |
| 161 | + |
| 162 | +REDUCE_OP_DEFINE(onednn_graph::ReduceSumOp) |
| 163 | +REDUCE_OP_DEFINE(onednn_graph::ReduceMeanOp) |
| 164 | + |
| 165 | +//===----------------------------------------------------------------------===// |
| 166 | +// Matmul ops shape infer |
| 167 | +//===----------------------------------------------------------------------===// |
| 168 | + |
38 | 169 | LogicalResult onednn_graph::MatMulOp::inferReturnTypeComponents(
|
39 | 170 | MLIRContext *context, ::std::optional<Location> location,
|
40 | 171 | MatMulOp::Adaptor adaptor,
|
@@ -134,7 +265,7 @@ LogicalResult onednn_graph::MatMulOp::inferReturnTypeComponents(
|
134 | 265 | SmallVector<int64_t> resultShape;
|
135 | 266 | if (!biasRankMatch ||
|
136 | 267 | !OpTrait::util::getBroadcastedShape(
|
137 |
| - retShape.getDims(), biasType.dyn_cast<ShapedType>().getShape(), |
| 268 | + retShape.getDims(), dyn_cast<ShapedType>(biasType).getShape(), |
138 | 269 | resultShape)) {
|
139 | 270 | return failure();
|
140 | 271 | }
|
|
0 commit comments