|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.apache.sysds.hops.rewrite; |
| 21 | + |
| 22 | +import org.apache.sysds.api.DMLScript; |
| 23 | +import org.apache.sysds.common.Types; |
| 24 | +import org.apache.sysds.common.Types.OpOpData; |
| 25 | +import org.apache.sysds.hops.DataOp; |
| 26 | +import org.apache.sysds.hops.Hop; |
| 27 | + |
| 28 | +import java.util.ArrayList; |
| 29 | +import java.util.HashMap; |
| 30 | +import java.util.HashSet; |
| 31 | +import java.util.List; |
| 32 | +import java.util.Map; |
| 33 | +import java.util.Set; |
| 34 | + |
| 35 | +/** |
| 36 | + * This Rewrite rule injects a Tee Operator for specific Out-Of-Core (OOC) patterns |
| 37 | + * where a value or an intermediate result is shared twice. Since for OOC we data streams |
| 38 | + * can only be consumed once. |
| 39 | + * |
| 40 | + * <p> |
| 41 | + * Pattern identified {@code t(X) %*% X}, where the data {@code X} will be shared by |
| 42 | + * {@code t(X)} and {@code %*%} multiplication. |
| 43 | + * </p> |
| 44 | + * |
| 45 | + * The rewrite uses a stable two-pass approach: |
| 46 | + * 1. <b>Find candidates (Read-Only):</b> Traverse the entire HOP DAG to identify candidates |
| 47 | + * the fit the target pattern. |
| 48 | + * 2. <b>Apply Rewrites (Modification):</b> Iterate over the collected candidate and put |
| 49 | + * {@code TeeOp}, and safely rewire the graph. |
| 50 | + */ |
| 51 | +public class RewriteInjectOOCTee extends HopRewriteRule { |
| 52 | + |
| 53 | + private static final Set<Long> rewrittenHops = new HashSet<>(); |
| 54 | + private static final Map<Long, Hop> handledHop = new HashMap<>(); |
| 55 | + |
| 56 | + // Maintain a list of candidates to rewrite in the second pass |
| 57 | + private final List<Hop> rewriteCandidates = new ArrayList<>(); |
| 58 | + |
| 59 | + /** |
| 60 | + * Handle a generic (last-level) hop DAG with multiple roots. |
| 61 | + * |
| 62 | + * @param roots high-level operator roots |
| 63 | + * @param state program rewrite status |
| 64 | + * @return list of high-level operators |
| 65 | + */ |
| 66 | + @Override |
| 67 | + public ArrayList<Hop> rewriteHopDAGs(ArrayList<Hop> roots, ProgramRewriteStatus state) { |
| 68 | + if (roots == null) { |
| 69 | + return null; |
| 70 | + } |
| 71 | + |
| 72 | + // Clear candidates for this pass |
| 73 | + rewriteCandidates.clear(); |
| 74 | + |
| 75 | + // PASS 1: Identify candidates without modifying the graph |
| 76 | + for (Hop root : roots) { |
| 77 | + root.resetVisitStatus(); |
| 78 | + findRewriteCandidates(root); |
| 79 | + } |
| 80 | + |
| 81 | + // PASS 2: Apply rewrites to identified candidates |
| 82 | + for (Hop candidate : rewriteCandidates) { |
| 83 | + applyTopDownTeeRewrite(candidate); |
| 84 | + } |
| 85 | + |
| 86 | + return roots; |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Handle a predicate hop DAG with exactly one root. |
| 91 | + * |
| 92 | + * @param root high-level operator root |
| 93 | + * @param state program rewrite status |
| 94 | + * @return high-level operator |
| 95 | + */ |
| 96 | + @Override |
| 97 | + public Hop rewriteHopDAG(Hop root, ProgramRewriteStatus state) { |
| 98 | + if (root == null) { |
| 99 | + return null; |
| 100 | + } |
| 101 | + |
| 102 | + // Clear candidates for this pass |
| 103 | + rewriteCandidates.clear(); |
| 104 | + |
| 105 | + // PASS 1: Identify candidates without modifying the graph |
| 106 | + root.resetVisitStatus(); |
| 107 | + findRewriteCandidates(root); |
| 108 | + |
| 109 | + // PASS 2: Apply rewrites to identified candidates |
| 110 | + for (Hop candidate : rewriteCandidates) { |
| 111 | + applyTopDownTeeRewrite(candidate); |
| 112 | + } |
| 113 | + |
| 114 | + return root; |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * First pass: Find candidates for rewrite without modifying the graph. |
| 119 | + * This method traverses the graph and identifies nodes that need to be |
| 120 | + * rewritten based on the transpose-matrix multiply pattern. |
| 121 | + * |
| 122 | + * @param hop current hop being examined |
| 123 | + */ |
| 124 | + private void findRewriteCandidates(Hop hop) { |
| 125 | + if (hop.isVisited()) { |
| 126 | + return; |
| 127 | + } |
| 128 | + |
| 129 | + // Mark as visited to avoid processing the same hop multiple times |
| 130 | + hop.setVisited(true); |
| 131 | + |
| 132 | + // Recursively traverse the graph (depth-first) |
| 133 | + for (Hop input : hop.getInput()) { |
| 134 | + findRewriteCandidates(input); |
| 135 | + } |
| 136 | + |
| 137 | + // Check if this hop is a candidate for OOC Tee injection |
| 138 | + if (DMLScript.USE_OOC |
| 139 | + && hop.getDataType().isMatrix() |
| 140 | + && !HopRewriteUtils.isData(hop, OpOpData.TEE) |
| 141 | + && hop.getParent().size() > 1) |
| 142 | + { |
| 143 | + rewriteCandidates.add(hop); |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * Second pass: Apply the TeeOp transformation to a candidate hop. |
| 149 | + * This safely rewires the graph by creating a TeeOp node and placeholders. |
| 150 | + * |
| 151 | + * @param sharedInput the hop to be rewritten |
| 152 | + */ |
| 153 | + private void applyTopDownTeeRewrite(Hop sharedInput) { |
| 154 | + // Only process if not already handled |
| 155 | + if (handledHop.containsKey(sharedInput.getHopID())) { |
| 156 | + return; |
| 157 | + } |
| 158 | + |
| 159 | + // Take a defensive copy of consumers before modifying the graph |
| 160 | + ArrayList<Hop> consumers = new ArrayList<>(sharedInput.getParent()); |
| 161 | + |
| 162 | + // Create the new TeeOp with the original hop as input |
| 163 | + DataOp teeOp = new DataOp("tee_out_" + sharedInput.getName(), |
| 164 | + sharedInput.getDataType(), sharedInput.getValueType(), Types.OpOpData.TEE, null, |
| 165 | + sharedInput.getDim1(), sharedInput.getDim2(), sharedInput.getNnz(), sharedInput.getBlocksize()); |
| 166 | + HopRewriteUtils.addChildReference(teeOp, sharedInput); |
| 167 | + |
| 168 | + // Rewire the graph: replace original connections with TeeOp outputs |
| 169 | + for (Hop consumer : consumers) { |
| 170 | + HopRewriteUtils.replaceChildReference(consumer, sharedInput, teeOp); |
| 171 | + } |
| 172 | + |
| 173 | + // Record that we've handled this hop |
| 174 | + handledHop.put(sharedInput.getHopID(), teeOp); |
| 175 | + rewrittenHops.add(sharedInput.getHopID()); |
| 176 | + } |
| 177 | +} |
0 commit comments