|
| 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 java.util.ArrayList; |
| 23 | +import java.util.HashMap; |
| 24 | +import java.util.List; |
| 25 | +import java.util.Map.Entry; |
| 26 | + |
| 27 | +import org.apache.sysds.common.Types.OpOp1; |
| 28 | +import org.apache.sysds.common.Types.OpOp2; |
| 29 | +import org.apache.sysds.hops.UnaryOp; |
| 30 | +import org.apache.sysds.runtime.instructions.cp.DoubleObject; |
| 31 | +import org.apache.sysds.runtime.instructions.cp.ScalarObject; |
| 32 | +import org.apache.sysds.hops.BinaryOp; |
| 33 | + |
| 34 | +import org.apache.sysds.common.Types.DataType; |
| 35 | +import org.apache.sysds.common.Types.ValueType; |
| 36 | + |
| 37 | +import org.apache.sysds.hops.Hop; |
| 38 | + |
| 39 | +/** |
| 40 | + * Rule: RewriteFloorCompress. Detects the sequence `M2 = floor(M * S)` followed by `C = compress(M2)` and prepares for |
| 41 | + * fusion into a single operation. This rewrite improves performance by avoiding intermediate results. Currently, it |
| 42 | + * identifies the pattern without applying fusion. |
| 43 | + */ |
| 44 | +public class RewriteQuantizationFusedCompression extends HopRewriteRule { |
| 45 | + @Override |
| 46 | + public ArrayList<Hop> rewriteHopDAGs(ArrayList<Hop> roots, ProgramRewriteStatus state) { |
| 47 | + if(roots == null) |
| 48 | + return null; |
| 49 | + |
| 50 | + // traverse the HOP DAG |
| 51 | + HashMap<String, Hop> floors = new HashMap<>(); |
| 52 | + HashMap<String, Hop> compresses = new HashMap<>(); |
| 53 | + for(Hop h : roots) |
| 54 | + collectFloorCompressSequences(h, floors, compresses); |
| 55 | + |
| 56 | + Hop.resetVisitStatus(roots); |
| 57 | + |
| 58 | + // check compresses for compress-after-floor pattern |
| 59 | + for(Entry<String, Hop> e : compresses.entrySet()) { |
| 60 | + String inputname = e.getKey(); |
| 61 | + Hop compresshop = e.getValue(); |
| 62 | + |
| 63 | + if(floors.containsKey(inputname) // floors same name |
| 64 | + && ((floors.get(inputname).getBeginLine() < compresshop.getBeginLine()) || |
| 65 | + (floors.get(inputname).getEndLine() < compresshop.getEndLine()) || |
| 66 | + (floors.get(inputname).getBeginLine() == compresshop.getBeginLine() && |
| 67 | + floors.get(inputname).getEndLine() == compresshop.getBeginLine() && |
| 68 | + floors.get(inputname).getBeginColumn() < compresshop.getBeginColumn()))) { |
| 69 | + |
| 70 | + // retrieve the floor hop and inputs |
| 71 | + Hop floorhop = floors.get(inputname); |
| 72 | + Hop floorInput = floorhop.getInput().get(0); |
| 73 | + |
| 74 | + // check if the input of the floor operation is a matrix |
| 75 | + if(floorInput.getDataType() == DataType.MATRIX) { |
| 76 | + |
| 77 | + // Check if the input of the floor operation involves a multiplication operation |
| 78 | + if(floorInput instanceof BinaryOp && ((BinaryOp) floorInput).getOp() == OpOp2.MULT) { |
| 79 | + Hop initialMatrix = floorInput.getInput().get(0); |
| 80 | + Hop sf = floorInput.getInput().get(1); |
| 81 | + |
| 82 | + // create fused hop |
| 83 | + BinaryOp fusedhop = new BinaryOp("test", DataType.MATRIX, ValueType.FP64, |
| 84 | + OpOp2.QUANTIZE_COMPRESS, initialMatrix, sf); |
| 85 | + |
| 86 | + // rewire compress consumers to fusedHop |
| 87 | + List<Hop> parents = new ArrayList<>(compresshop.getParent()); |
| 88 | + for(Hop p : parents) { |
| 89 | + HopRewriteUtils.replaceChildReference(p, compresshop, fusedhop); |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + return roots; |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + public Hop rewriteHopDAG(Hop root, ProgramRewriteStatus state) { |
| 100 | + // do nothing, floor/compress do not occur in predicates |
| 101 | + return root; |
| 102 | + } |
| 103 | + |
| 104 | + private void collectFloorCompressSequences(Hop hop, HashMap<String, Hop> floors, HashMap<String, Hop> compresses) { |
| 105 | + if(hop.isVisited()) |
| 106 | + return; |
| 107 | + |
| 108 | + // process childs |
| 109 | + if(!hop.getInput().isEmpty()) |
| 110 | + for(Hop c : hop.getInput()) |
| 111 | + collectFloorCompressSequences(c, floors, compresses); |
| 112 | + |
| 113 | + // process current hop |
| 114 | + if(hop instanceof UnaryOp) { |
| 115 | + UnaryOp uop = (UnaryOp) hop; |
| 116 | + if(uop.getOp() == OpOp1.FLOOR) { |
| 117 | + floors.put(uop.getName(), uop); |
| 118 | + } |
| 119 | + else if(uop.getOp() == OpOp1.COMPRESS) { |
| 120 | + compresses.put(uop.getInput(0).getName(), uop); |
| 121 | + } |
| 122 | + } |
| 123 | + hop.setVisited(); |
| 124 | + } |
| 125 | +} |
0 commit comments