|
| 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.runtime.instructions.ooc; |
| 21 | + |
| 22 | + |
| 23 | +import org.apache.sysds.common.Opcodes; |
| 24 | +import org.apache.sysds.common.Types.DataType; |
| 25 | +import org.apache.sysds.lops.WeightedDivMM.WDivMMType; |
| 26 | +import org.apache.sysds.runtime.DMLRuntimeException; |
| 27 | +import org.apache.sysds.runtime.controlprogram.context.ExecutionContext; |
| 28 | +import org.apache.sysds.runtime.functionobjects.Multiply; |
| 29 | +import org.apache.sysds.runtime.functionobjects.Plus; |
| 30 | +import org.apache.sysds.runtime.instructions.InstructionUtils; |
| 31 | +import org.apache.sysds.runtime.instructions.cp.CPOperand; |
| 32 | +import org.apache.sysds.runtime.instructions.spark.data.IndexedMatrixValue; |
| 33 | +import org.apache.sysds.runtime.matrix.data.MatrixBlock; |
| 34 | +import org.apache.sysds.runtime.matrix.data.MatrixIndexes; |
| 35 | +import org.apache.sysds.runtime.matrix.operators.AggregateBinaryOperator; |
| 36 | +import org.apache.sysds.runtime.matrix.operators.AggregateOperator; |
| 37 | +import org.apache.sysds.runtime.matrix.operators.BinaryOperator; |
| 38 | +import org.apache.sysds.runtime.matrix.operators.QuaternaryOperator; |
| 39 | +import org.apache.sysds.runtime.meta.DataCharacteristics; |
| 40 | + |
| 41 | +import java.util.function.Function; |
| 42 | + |
| 43 | + |
| 44 | +public class WDivMMOOCInstruction extends QuaternaryOOCInstruction |
| 45 | +{ |
| 46 | + |
| 47 | + protected WDivMMOOCInstruction(QuaternaryOperator op, CPOperand in1, CPOperand in2, CPOperand in3, CPOperand in4, |
| 48 | + CPOperand out, String opcode, String istr) { |
| 49 | + super(op, in1, in2, in3, in4, out, opcode, istr); |
| 50 | + } |
| 51 | + |
| 52 | + public static WDivMMOOCInstruction parseInstruction(QuaternaryOOCInstruction instr) { |
| 53 | + String instrStr = instr.getInstructionString(); |
| 54 | + String opcode = InstructionUtils.getInstructionPartsWithValueType(instr.getInstructionString())[0]; |
| 55 | + return new WDivMMOOCInstruction((QuaternaryOperator) instr.getOperator(), instr.input1, instr.input2, |
| 56 | + instr.input3, instr.input4, instr.output, opcode, instrStr); |
| 57 | + } |
| 58 | + |
| 59 | + |
| 60 | + @Override |
| 61 | + public void processInstruction(ExecutionContext ec) { |
| 62 | + QuaternaryOperator _qop = ((QuaternaryOperator)_optr); |
| 63 | + final WDivMMType wt = _qop.wtype3; |
| 64 | + |
| 65 | + if(!(wt.hasFourInputs()&&wt.hasScalar()) || wt.isBasic() || wt.isMult() || wt.isMinus()) throw new DMLRuntimeException("Not implemented: only pnmf supported yet"); |
| 66 | + |
| 67 | + CachingStream X = new CachingStream(ec.getMatrixObject(input1).getStreamHandle()); |
| 68 | + CachingStream U = new CachingStream(ec.getMatrixObject(input2).getStreamHandle()); |
| 69 | + CachingStream V = new CachingStream(ec.getMatrixObject(input3).getStreamHandle()); |
| 70 | + |
| 71 | + double eps = 0.0; |
| 72 | + if(_qop.hasFourInputs()) { |
| 73 | + if (input4.getDataType() == DataType.SCALAR) |
| 74 | + eps = ec.getScalarInput(input4).getDoubleValue(); |
| 75 | + } |
| 76 | + |
| 77 | + OOCStream<IndexedMatrixValue> mmt = matMultOOC(U.getReadStream(), V.getReadStream(), U.getDataCharacteristics(), V.getDataCharacteristics(), false, true); |
| 78 | + OOCStream<IndexedMatrixValue> plus = elemPlusOOC(mmt, eps); |
| 79 | + OOCStream<IndexedMatrixValue> inter = elemDivOOC(X.getReadStream(), plus); |
| 80 | + OOCStream<IndexedMatrixValue> out; |
| 81 | + |
| 82 | + if(wt.isLeft()) |
| 83 | + out = matMultOOC(inter, U.getReadStream(), X.getDataCharacteristics(), U.getDataCharacteristics(), true, false); |
| 84 | + else |
| 85 | + out = matMultOOC(inter, V.getReadStream(), X.getDataCharacteristics(), V.getDataCharacteristics(), false, false); |
| 86 | + |
| 87 | + ec.getMatrixObject(output).setStreamHandle(out); |
| 88 | + } |
| 89 | + |
| 90 | + private OOCStream<IndexedMatrixValue> matMultOOC(OOCStream<IndexedMatrixValue> m1, OOCStream<IndexedMatrixValue> m2, DataCharacteristics dc1, DataCharacteristics dc2, boolean leftTranspose, boolean rightTranspose){ |
| 91 | + |
| 92 | + int emitLeftThreshold = rightTranspose? (int) dc2.getNumRowBlocks() : (int) dc2.getNumColBlocks(); |
| 93 | + int emitRightThreshold = leftTranspose? (int) dc1.getNumColBlocks() : (int) dc1.getNumRowBlocks(); |
| 94 | + |
| 95 | + OOCStream<IndexedMatrixValue> intermediateStream = createWritableStream(); |
| 96 | + OOCStream<IndexedMatrixValue> out = createWritableStream(); |
| 97 | + |
| 98 | + AggregateOperator agg = new AggregateOperator(0, Plus.getPlusFnObject()); |
| 99 | + AggregateBinaryOperator op = new AggregateBinaryOperator(Multiply.getMultiplyFnObject(), agg); |
| 100 | + |
| 101 | + joinManyOOC(m1, m2, intermediateStream, |
| 102 | + (left, right) -> { |
| 103 | + MatrixBlock leftBlock = (MatrixBlock) left.getValue(); |
| 104 | + MatrixBlock rightBlock = (MatrixBlock) right.getValue(); |
| 105 | + if(leftTranspose) leftBlock = leftBlock.transpose(); |
| 106 | + if(rightTranspose) rightBlock = rightBlock.transpose(); |
| 107 | + |
| 108 | + MatrixBlock partialResult = leftBlock.aggregateBinaryOperations(leftBlock, rightBlock, |
| 109 | + new MatrixBlock(), op); |
| 110 | + int lidx = (int) (leftTranspose? left.getIndexes().getColumnIndex() : left.getIndexes().getRowIndex()); |
| 111 | + int ridx = (int) (rightTranspose? right.getIndexes().getRowIndex() : right.getIndexes().getColumnIndex()); |
| 112 | + return new IndexedMatrixValue(new MatrixIndexes(lidx, ridx), partialResult); |
| 113 | + }, |
| 114 | + tmp -> leftTranspose? tmp.getIndexes().getRowIndex() : tmp.getIndexes().getColumnIndex(), |
| 115 | + tmp -> rightTranspose? tmp.getIndexes().getColumnIndex() : tmp.getIndexes().getRowIndex(), |
| 116 | + emitLeftThreshold, emitRightThreshold); |
| 117 | + |
| 118 | + BinaryOperator plus = InstructionUtils.parseBinaryOperator(Opcodes.PLUS.toString()); |
| 119 | + int emitAggThreshold = leftTranspose? (int) dc1.getNumRowBlocks() : (int) dc1.getNumColBlocks(); |
| 120 | + |
| 121 | + groupedReduceOOC(intermediateStream, out, (left, right) -> { |
| 122 | + MatrixBlock mb = ((MatrixBlock)left.getValue()).binaryOperationsInPlace(plus, right.getValue()); |
| 123 | + left.setValue(mb); |
| 124 | + return left; |
| 125 | + }, emitAggThreshold); |
| 126 | + |
| 127 | + return out; |
| 128 | + } |
| 129 | + |
| 130 | + private OOCStream<IndexedMatrixValue> elemDivOOC(OOCStream<IndexedMatrixValue> m1, OOCStream<IndexedMatrixValue> m2){ |
| 131 | + SubscribableTaskQueue<IndexedMatrixValue> out = new SubscribableTaskQueue<>(); |
| 132 | + BinaryOperator div = InstructionUtils.parseBinaryOperator(Opcodes.DIV.toString()); |
| 133 | + Function<IndexedMatrixValue, MatrixIndexes> key = imv -> new MatrixIndexes(imv.getIndexes().getRowIndex(), imv.getIndexes().getColumnIndex()); |
| 134 | + |
| 135 | + joinOOC(m1, m2, out, (left, right) -> { |
| 136 | + MatrixBlock lb = (MatrixBlock) left.getValue(); |
| 137 | + MatrixBlock rb = (MatrixBlock) right.getValue(); |
| 138 | + MatrixBlock combined = lb.binaryOperations(div, rb); |
| 139 | + return new IndexedMatrixValue( |
| 140 | + new MatrixIndexes(left.getIndexes().getRowIndex(), left.getIndexes().getColumnIndex()), combined); |
| 141 | + }, key); |
| 142 | + |
| 143 | + return out; |
| 144 | + } |
| 145 | + |
| 146 | + private OOCStream<IndexedMatrixValue> elemPlusOOC(OOCStream<IndexedMatrixValue> m1, double eps){ |
| 147 | + SubscribableTaskQueue<IndexedMatrixValue> out = new SubscribableTaskQueue<>(); |
| 148 | + mapOOC(m1, out, blk -> new IndexedMatrixValue( |
| 149 | + new MatrixIndexes(blk.getIndexes().getRowIndex(), blk.getIndexes().getColumnIndex()), plusDouble((MatrixBlock) blk.getValue(), eps))); |
| 150 | + return out; |
| 151 | + } |
| 152 | + |
| 153 | + private MatrixBlock plusDouble(MatrixBlock blk, double eps){ |
| 154 | + for(int i=0; i<blk.getNumRows(); i++){ |
| 155 | + for(int j=0; j<blk.getNumColumns(); j++){ |
| 156 | + blk.set(i, j, blk.get(i, j) + eps); |
| 157 | + } |
| 158 | + } |
| 159 | + return blk; |
| 160 | + } |
| 161 | +} |
0 commit comments