|
1 | 1 | package org.apache.sysds.hops.rewriter.estimators; |
2 | 2 |
|
3 | 3 | import org.apache.sysds.hops.rewriter.ConstantFoldingFunctions; |
| 4 | +import org.apache.sysds.hops.rewriter.RewriterDataType; |
4 | 5 | import org.apache.sysds.hops.rewriter.RewriterInstruction; |
5 | 6 | import org.apache.sysds.hops.rewriter.RewriterStatement; |
| 7 | +import org.apache.sysds.hops.rewriter.RewriterUtils; |
6 | 8 | import org.apache.sysds.hops.rewriter.RuleContext; |
7 | 9 | import org.apache.sysds.hops.rewriter.utils.StatementUtils; |
8 | 10 |
|
| 11 | +import java.util.HashMap; |
9 | 12 | import java.util.Map; |
10 | 13 | import java.util.UUID; |
11 | 14 |
|
12 | 15 | public class RewriterSparsityEstimator { |
13 | | - public static RewriterStatement estimateNNZ(RewriterStatement stmt, Map<RewriterStatement, Long> matrixNNZs, final RuleContext ctx) { |
14 | | - long[] nnzs = stmt.getOperands().stream().mapToLong(matrixNNZs::get).toArray(); |
15 | 16 |
|
| 17 | + /*public static RewriterStatement getCanonicalized(RewriterStatement instr, final RuleContext ctx) { |
| 18 | + RewriterStatement cpy = instr.copyNode(); |
| 19 | + Map<RewriterStatement, RewriterStatement> mmap = new HashMap<>(); |
| 20 | +
|
| 21 | + for (int i = 0; i < cpy.getOperands().size(); i++) { |
| 22 | + RewriterStatement existing = mmap.get(cpy.getOperands().get(i)); |
| 23 | +
|
| 24 | + if (existing != null) { |
| 25 | + cpy.getOperands().set(i, existing); |
| 26 | + } else { |
| 27 | + RewriterStatement mDat = new RewriterDataType().as(UUID.randomUUID().toString()).ofType(cpy.getOperands().get(i).getResultingDataType(ctx)).consolidate(ctx); |
| 28 | + } |
| 29 | + } |
| 30 | +
|
| 31 | + RewriterUtils.prepareForSparsityEstimation(); |
| 32 | + }*/ |
| 33 | + |
| 34 | + public static RewriterStatement rollupSparsities(RewriterStatement sparsityEstimate, Map<RewriterStatement, RewriterStatement> sparsityMap, final RuleContext ctx) { |
| 35 | + sparsityEstimate.forEachPreOrder(cur -> { |
| 36 | + for (int i = 0; i < cur.getOperands().size(); i++) { |
| 37 | + RewriterStatement child = cur.getChild(i); |
| 38 | + |
| 39 | + if (child.isInstruction() && child.trueInstruction().equals("_nnz")) { |
| 40 | + RewriterStatement subEstimate = sparsityMap.get(child.getChild(0)); |
| 41 | + |
| 42 | + if (subEstimate != null) { |
| 43 | + cur.getOperands().set(i, subEstimate); |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + return true; |
| 48 | + }, false); |
| 49 | + |
| 50 | + return sparsityEstimate; |
| 51 | + } |
| 52 | + |
| 53 | + public static Map<RewriterStatement, RewriterStatement> estimateAllNNZ(RewriterStatement stmt, final RuleContext ctx) { |
| 54 | + Map<RewriterStatement, RewriterStatement> map = new HashMap<>(); |
| 55 | + stmt.forEachPostOrder((cur, pred) -> { |
| 56 | + RewriterStatement estimation = estimateNNZ(cur, ctx); |
| 57 | + if (estimation != null) |
| 58 | + map.put(cur, estimation); |
| 59 | + }, false); |
| 60 | + |
| 61 | + return map; |
| 62 | + } |
| 63 | + |
| 64 | + public static RewriterStatement estimateNNZ(RewriterStatement stmt, final RuleContext ctx) { |
| 65 | + if (!stmt.isInstruction()) |
| 66 | + return null; |
16 | 67 | switch (stmt.trueInstruction()) { |
17 | 68 | case "%*%": |
18 | 69 | return new RewriterInstruction("*", ctx, StatementUtils.min(ctx, new RewriterInstruction("*", ctx, stmt.getNRow(), stmt.getNCol()), RewriterStatement.nnz(stmt.getChild(1), ctx)), new RewriterInstruction("*", ctx, stmt.getNRow(), stmt.getNCol()), RewriterStatement.nnz(stmt.getChild(0), ctx)); |
@@ -49,6 +100,31 @@ public static RewriterStatement estimateNNZ(RewriterStatement stmt, Map<Rewriter |
49 | 100 |
|
50 | 101 | case "sqrt(MATRIX)": |
51 | 102 | return RewriterStatement.nnz(stmt.getChild(0), ctx); |
| 103 | + |
| 104 | + case "diag(MATRIX)": |
| 105 | + return StatementUtils.min(ctx, stmt.getNRow(), RewriterStatement.nnz(stmt.getChild(0), ctx)); |
| 106 | + |
| 107 | + case "/(MATRIX,FLOAT)": |
| 108 | + case "/(MATRIX,MATRIX)": |
| 109 | + return RewriterStatement.nnz(stmt.getChild(0), ctx); |
| 110 | + case "/(FLOAT,MATRIX)": |
| 111 | + if (stmt.getChild(0).isLiteral() && ConstantFoldingFunctions.isNeutralElement(stmt.getChild(0).getLiteral(), "+")) |
| 112 | + return RewriterStatement.literal(ctx, 0L); |
| 113 | + return StatementUtils.length(ctx, stmt); |
| 114 | + |
| 115 | + |
| 116 | + // Fused operators |
| 117 | + case "log_nz(MATRIX)": |
| 118 | + case "*2(MATRIX)": |
| 119 | + case "sq(MATRIX)": |
| 120 | + return RewriterStatement.nnz(stmt.getChild(0), ctx); |
| 121 | + case "1-*(MATRIX,MATRIX)": |
| 122 | + return StatementUtils.length(ctx, stmt); |
| 123 | + case "+*(MATRIX,FLOAT,MATRIX)": |
| 124 | + case "-*(MATRIX,FLOAT,MATRIX)": |
| 125 | + if (stmt.getChild(1).isLiteral() && ConstantFoldingFunctions.isNeutralElement(stmt.getChild(1).getLiteral(), "+")) |
| 126 | + return RewriterStatement.nnz(stmt.getChild(0), ctx); |
| 127 | + return StatementUtils.min(ctx, new RewriterInstruction("+", ctx, RewriterStatement.nnz(stmt.getChild(0), ctx), RewriterStatement.nnz(stmt.getChild(2), ctx)), StatementUtils.length(ctx, stmt)); |
52 | 128 | } |
53 | 129 |
|
54 | 130 | return StatementUtils.length(ctx, stmt); |
|
0 commit comments