From f8cc179530b465a0bbd42ffe700ceffa65f0a1cc Mon Sep 17 00:00:00 2001 From: Frederic Zoepffel Date: Mon, 22 Jan 2024 15:45:49 +0545 Subject: [PATCH] added one input and two input ifft to dml --- .../instructions/CPInstructionParser.java | 8 +- .../instructions/cp/CPInstruction.java | 2 +- .../cp/MultiReturnBuiltinCPInstruction.java | 9 - ...urnComplexMatrixBuiltinCPInstruction.java} | 49 +++- .../runtime/matrix/data/LibCommonsMath.java | 271 +++++++++--------- .../runtime/matrix/data/LibMatrixFourier.java | 7 - .../propagation/PrivacyPropagator.java | 2 +- 7 files changed, 184 insertions(+), 164 deletions(-) rename src/main/java/org/apache/sysds/runtime/instructions/cp/{MultiReturnMatrixMatrixBuiltinCPInstruction.java => MultiReturnComplexMatrixBuiltinCPInstruction.java} (67%) diff --git a/src/main/java/org/apache/sysds/runtime/instructions/CPInstructionParser.java b/src/main/java/org/apache/sysds/runtime/instructions/CPInstructionParser.java index aacbe73f7b8..cfc4d18b1f9 100644 --- a/src/main/java/org/apache/sysds/runtime/instructions/CPInstructionParser.java +++ b/src/main/java/org/apache/sysds/runtime/instructions/CPInstructionParser.java @@ -56,7 +56,7 @@ import org.apache.sysds.runtime.instructions.cp.MMChainCPInstruction; import org.apache.sysds.runtime.instructions.cp.MMTSJCPInstruction; import org.apache.sysds.runtime.instructions.cp.MultiReturnBuiltinCPInstruction; -import org.apache.sysds.runtime.instructions.cp.MultiReturnMatrixMatrixBuiltinCPInstruction; +import org.apache.sysds.runtime.instructions.cp.MultiReturnComplexMatrixBuiltinCPInstruction; import org.apache.sysds.runtime.instructions.cp.MultiReturnParameterizedBuiltinCPInstruction; import org.apache.sysds.runtime.instructions.cp.PMMJCPInstruction; import org.apache.sysds.runtime.instructions.cp.ParameterizedBuiltinCPInstruction; @@ -331,7 +331,7 @@ public class CPInstructionParser extends InstructionParser { String2CPInstructionType.put("lu", CPType.MultiReturnBuiltin); String2CPInstructionType.put("eigen", CPType.MultiReturnBuiltin); String2CPInstructionType.put("fft", CPType.MultiReturnBuiltin); - String2CPInstructionType.put("ifft", CPType.MultiReturnMatrixMatrixBuiltin); + String2CPInstructionType.put("ifft", CPType.MultiReturnComplexMatrixBuiltin); String2CPInstructionType.put("svd", CPType.MultiReturnBuiltin); String2CPInstructionType.put("partition", CPType.Partition); @@ -423,8 +423,8 @@ public static CPInstruction parseSingleInstruction(CPType cptype, String str) { case MultiReturnParameterizedBuiltin: return MultiReturnParameterizedBuiltinCPInstruction.parseInstruction(str); - case MultiReturnMatrixMatrixBuiltin: - return MultiReturnMatrixMatrixBuiltinCPInstruction.parseInstruction(str); + case MultiReturnComplexMatrixBuiltin: + return MultiReturnComplexMatrixBuiltinCPInstruction.parseInstruction(str); case MultiReturnBuiltin: return MultiReturnBuiltinCPInstruction.parseInstruction(str); diff --git a/src/main/java/org/apache/sysds/runtime/instructions/cp/CPInstruction.java b/src/main/java/org/apache/sysds/runtime/instructions/cp/CPInstruction.java index 434650d18a4..1847cc0cae6 100644 --- a/src/main/java/org/apache/sysds/runtime/instructions/cp/CPInstruction.java +++ b/src/main/java/org/apache/sysds/runtime/instructions/cp/CPInstruction.java @@ -41,7 +41,7 @@ public abstract class CPInstruction extends Instruction { public enum CPType { AggregateUnary, AggregateBinary, AggregateTernary, Unary, Binary, Ternary, Quaternary, BuiltinNary, Ctable, - MultiReturnParameterizedBuiltin, ParameterizedBuiltin, MultiReturnBuiltin, MultiReturnMatrixMatrixBuiltin, + MultiReturnParameterizedBuiltin, ParameterizedBuiltin, MultiReturnBuiltin, MultiReturnComplexMatrixBuiltin, Builtin, Reorg, Variable, FCall, Append, Rand, QSort, QPick, Local, MatrixIndexing, MMTSJ, PMMJ, MMChain, Reshape, Partition, Compression, DeCompression, SpoofFused, StringInit, CentralMoment, Covariance, UaggOuterChain, Dnn, Sql, Prefetch, Broadcast, TrigRemote, diff --git a/src/main/java/org/apache/sysds/runtime/instructions/cp/MultiReturnBuiltinCPInstruction.java b/src/main/java/org/apache/sysds/runtime/instructions/cp/MultiReturnBuiltinCPInstruction.java index a2497d07a2e..26874cc8708 100644 --- a/src/main/java/org/apache/sysds/runtime/instructions/cp/MultiReturnBuiltinCPInstruction.java +++ b/src/main/java/org/apache/sysds/runtime/instructions/cp/MultiReturnBuiltinCPInstruction.java @@ -95,15 +95,6 @@ public static MultiReturnBuiltinCPInstruction parseInstruction(String str) { return new MultiReturnBuiltinCPInstruction(null, in1, outputs, opcode, str); - // } else if (opcode.equalsIgnoreCase("ifft")) { - // // one input and two outputs - // CPOperand in1 = new CPOperand(parts[1]); - // CPOperand in2 = new CPOperand(parts[2]); - // outputs.add(new CPOperand(parts[3], ValueType.FP64, DataType.MATRIX)); - // outputs.add(new CPOperand(parts[4], ValueType.FP64, DataType.MATRIX)); - - // return new MultiReturnBuiltinCPInstruction(null, in1, in2, outputs, opcode, - // str); } else if (opcode.equalsIgnoreCase("svd")) { CPOperand in1 = new CPOperand(parts[1]); diff --git a/src/main/java/org/apache/sysds/runtime/instructions/cp/MultiReturnMatrixMatrixBuiltinCPInstruction.java b/src/main/java/org/apache/sysds/runtime/instructions/cp/MultiReturnComplexMatrixBuiltinCPInstruction.java similarity index 67% rename from src/main/java/org/apache/sysds/runtime/instructions/cp/MultiReturnMatrixMatrixBuiltinCPInstruction.java rename to src/main/java/org/apache/sysds/runtime/instructions/cp/MultiReturnComplexMatrixBuiltinCPInstruction.java index d4a02173a28..ae045dbc3e5 100644 --- a/src/main/java/org/apache/sysds/runtime/instructions/cp/MultiReturnMatrixMatrixBuiltinCPInstruction.java +++ b/src/main/java/org/apache/sysds/runtime/instructions/cp/MultiReturnComplexMatrixBuiltinCPInstruction.java @@ -34,17 +34,24 @@ import org.apache.sysds.runtime.matrix.data.MatrixBlock; import org.apache.sysds.runtime.matrix.operators.Operator; -public class MultiReturnMatrixMatrixBuiltinCPInstruction extends ComputationCPInstruction { +public class MultiReturnComplexMatrixBuiltinCPInstruction extends ComputationCPInstruction { protected ArrayList _outputs; - private MultiReturnMatrixMatrixBuiltinCPInstruction(Operator op, CPOperand input1, CPOperand input2, + private MultiReturnComplexMatrixBuiltinCPInstruction(Operator op, CPOperand input1, CPOperand input2, ArrayList outputs, String opcode, String istr) { super(CPType.MultiReturnBuiltin, op, input1, input2, outputs.get(0), opcode, istr); _outputs = outputs; } + private MultiReturnComplexMatrixBuiltinCPInstruction(Operator op, CPOperand input1, ArrayList outputs, + String opcode, + String istr) { + super(CPType.MultiReturnBuiltin, op, input1, null, outputs.get(0), opcode, istr); + _outputs = outputs; + } + public CPOperand getOutput(int i) { return _outputs.get(i); } @@ -57,21 +64,30 @@ public String[] getOutputNames() { return _outputs.parallelStream().map(output -> output.getName()).toArray(String[]::new); } - public static MultiReturnMatrixMatrixBuiltinCPInstruction parseInstruction(String str) { + public static MultiReturnComplexMatrixBuiltinCPInstruction parseInstruction(String str) { String[] parts = InstructionUtils.getInstructionPartsWithValueType(str); ArrayList outputs = new ArrayList<>(); // first part is always the opcode String opcode = parts[0]; - if (opcode.equalsIgnoreCase("ifft")) { + if (parts.length == 5 && opcode.equalsIgnoreCase("ifft")) { // one input and two outputs CPOperand in1 = new CPOperand(parts[1]); CPOperand in2 = new CPOperand(parts[2]); outputs.add(new CPOperand(parts[3], ValueType.FP64, DataType.MATRIX)); outputs.add(new CPOperand(parts[4], ValueType.FP64, DataType.MATRIX)); - return new MultiReturnMatrixMatrixBuiltinCPInstruction(null, in1, in2, outputs, opcode, str); - } else { + return new MultiReturnComplexMatrixBuiltinCPInstruction(null, in1, in2, outputs, opcode, str); + } else if (parts.length == 4 && opcode.equalsIgnoreCase("ifft")) { + // one input and two outputs + CPOperand in1 = new CPOperand(parts[1]); + outputs.add(new CPOperand(parts[2], ValueType.FP64, DataType.MATRIX)); + outputs.add(new CPOperand(parts[3], ValueType.FP64, DataType.MATRIX)); + + return new MultiReturnComplexMatrixBuiltinCPInstruction(null, in1, outputs, opcode, str); + } + + { throw new DMLRuntimeException("Invalid opcode in MultiReturnBuiltin instruction: " + opcode); } @@ -83,6 +99,27 @@ public int getNumOutputs() { @Override public void processInstruction(ExecutionContext ec) { + if (input2 == null) + processOneInputInstruction(ec); + else + processTwoInputInstruction(ec); + } + + private void processOneInputInstruction(ExecutionContext ec) { + if (!LibCommonsMath.isSupportedMultiReturnOperation(getOpcode())) + throw new DMLRuntimeException("Invalid opcode in MultiReturnBuiltin instruction: " + getOpcode()); + + MatrixBlock in = ec.getMatrixInput(input1.getName()); + MatrixBlock[] out = LibCommonsMath.multiReturnOperations(in, getOpcode()); + + ec.releaseMatrixInput(input1.getName()); + + for (int i = 0; i < _outputs.size(); i++) { + ec.setMatrixOutput(_outputs.get(i).getName(), out[i]); + } + } + + private void processTwoInputInstruction(ExecutionContext ec) { if (!LibCommonsMath.isSupportedMultiReturnOperation(getOpcode())) throw new DMLRuntimeException("Invalid opcode in MultiReturnBuiltin instruction: " + getOpcode()); diff --git a/src/main/java/org/apache/sysds/runtime/matrix/data/LibCommonsMath.java b/src/main/java/org/apache/sysds/runtime/matrix/data/LibCommonsMath.java index d3491a8f986..924477100d7 100644 --- a/src/main/java/org/apache/sysds/runtime/matrix/data/LibCommonsMath.java +++ b/src/main/java/org/apache/sysds/runtime/matrix/data/LibCommonsMath.java @@ -53,27 +53,26 @@ import static org.apache.sysds.runtime.matrix.data.LibMatrixFourier.ifft; /** - * Library for matrix operations that need invocation of - * Apache Commons Math library. + * Library for matrix operations that need invocation of + * Apache Commons Math library. * * This library currently supports following operations: - * matrix inverse, matrix decompositions (QR, LU, Eigen), solve + * matrix inverse, matrix decompositions (QR, LU, Eigen), solve */ -public class LibCommonsMath -{ +public class LibCommonsMath { private static final Log LOG = LogFactory.getLog(LibCommonsMath.class.getName()); private static final double RELATIVE_SYMMETRY_THRESHOLD = 1e-6; private static final double EIGEN_LAMBDA = 1e-8; private LibCommonsMath() { - //prevent instantiation via private constructor + // prevent instantiation via private constructor } - - public static boolean isSupportedUnaryOperation( String opcode ) { - return ( opcode.equals("inverse") || opcode.equals("cholesky") ); + + public static boolean isSupportedUnaryOperation(String opcode) { + return (opcode.equals("inverse") || opcode.equals("cholesky")); } - - public static boolean isSupportedMultiReturnOperation( String opcode ) { + + public static boolean isSupportedMultiReturnOperation(String opcode) { switch (opcode) { case "qr": @@ -81,19 +80,21 @@ public static boolean isSupportedMultiReturnOperation( String opcode ) { case "eigen": case "fft": case "ifft": - case "svd": return true; - default: return false; + case "svd": + return true; + default: + return false; } } - - public static boolean isSupportedMatrixMatrixOperation( String opcode ) { - return ( opcode.equals("solve") ); + + public static boolean isSupportedMatrixMatrixOperation(String opcode) { + return (opcode.equals("solve")); } - + public static MatrixBlock unaryOperations(MatrixBlock inj, String opcode) { Array2DRowRealMatrix matrixInput = DataConverter.convertToArray2DRowRealMatrix(inj); - if(opcode.equals("inverse")) + if (opcode.equals("inverse")) return computeMatrixInverse(matrixInput); else if (opcode.equals("cholesky")) return computeCholesky(matrixInput); @@ -133,9 +134,8 @@ public static MatrixBlock[] multiReturnOperations(MatrixBlock in, String opcode, return computeEigenQR(in, threads); case "fft": return computeFFT(in); - // TODO: add ifft for only one input - // case "ifft": - // return computeIFFT(in); + case "ifft": + return computeIFFT(in, null); case "svd": return computeSvd(in); default: @@ -174,24 +174,26 @@ public static MatrixBlock matrixMatrixOperations(MatrixBlock in1, MatrixBlock in * @return matrix block */ private static MatrixBlock computeSolve(MatrixBlock in1, MatrixBlock in2) { - //convert to commons math BlockRealMatrix instead of Array2DRowRealMatrix - //to avoid unnecessary conversion as QR internally creates a BlockRealMatrix + // convert to commons math BlockRealMatrix instead of Array2DRowRealMatrix + // to avoid unnecessary conversion as QR internally creates a BlockRealMatrix BlockRealMatrix matrixInput = DataConverter.convertToBlockRealMatrix(in1); BlockRealMatrix vectorInput = DataConverter.convertToBlockRealMatrix(in2); - - /*LUDecompositionImpl ludecompose = new LUDecompositionImpl(matrixInput); - DecompositionSolver lusolver = ludecompose.getSolver(); - RealMatrix solutionMatrix = lusolver.solve(vectorInput);*/ - + + /* + * LUDecompositionImpl ludecompose = new LUDecompositionImpl(matrixInput); + * DecompositionSolver lusolver = ludecompose.getSolver(); + * RealMatrix solutionMatrix = lusolver.solve(vectorInput); + */ + // Setup a solver based on QR Decomposition QRDecomposition qrdecompose = new QRDecomposition(matrixInput); DecompositionSolver solver = qrdecompose.getSolver(); // Invoke solve RealMatrix solutionMatrix = solver.solve(vectorInput); - + return DataConverter.convertToMatrixBlock(solutionMatrix); } - + /** * Function to perform QR decomposition on a given matrix. * @@ -200,19 +202,19 @@ private static MatrixBlock computeSolve(MatrixBlock in1, MatrixBlock in2) { */ private static MatrixBlock[] computeQR(MatrixBlock in) { Array2DRowRealMatrix matrixInput = DataConverter.convertToArray2DRowRealMatrix(in); - + // Perform QR decomposition QRDecomposition qrdecompose = new QRDecomposition(matrixInput); RealMatrix H = qrdecompose.getH(); RealMatrix R = qrdecompose.getR(); - + // Read the results into native format MatrixBlock mbH = DataConverter.convertToMatrixBlock(H.getData()); MatrixBlock mbR = DataConverter.convertToMatrixBlock(R.getData()); return new MatrixBlock[] { mbH, mbR }; } - + /** * Function to perform LU decomposition on a given matrix. * @@ -220,20 +222,20 @@ private static MatrixBlock[] computeQR(MatrixBlock in) { * @return array of matrix blocks */ private static MatrixBlock[] computeLU(MatrixBlock in) { - if(in.getNumRows() != in.getNumColumns()) { + if (in.getNumRows() != in.getNumColumns()) { throw new DMLRuntimeException( - "LU Decomposition can only be done on a square matrix. Input matrix is rectangular (rows=" - + in.getNumRows() + ", cols=" + in.getNumColumns() + ")"); + "LU Decomposition can only be done on a square matrix. Input matrix is rectangular (rows=" + + in.getNumRows() + ", cols=" + in.getNumColumns() + ")"); } - + Array2DRowRealMatrix matrixInput = DataConverter.convertToArray2DRowRealMatrix(in); - + // Perform LUP decomposition LUDecomposition ludecompose = new LUDecomposition(matrixInput); RealMatrix P = ludecompose.getP(); RealMatrix L = ludecompose.getL(); RealMatrix U = ludecompose.getU(); - + // Read the results into native format MatrixBlock mbP = DataConverter.convertToMatrixBlock(P.getData()); MatrixBlock mbL = DataConverter.convertToMatrixBlock(L.getData()); @@ -241,7 +243,7 @@ private static MatrixBlock[] computeLU(MatrixBlock in) { return new MatrixBlock[] { mbP, mbL, mbU }; } - + /** * Function to perform Eigen decomposition on a given matrix. * Input must be a symmetric matrix. @@ -250,21 +252,20 @@ private static MatrixBlock[] computeLU(MatrixBlock in) { * @return array of matrix blocks */ private static MatrixBlock[] computeEigen(MatrixBlock in) { - if ( in.getNumRows() != in.getNumColumns() ) { + if (in.getNumRows() != in.getNumColumns()) { throw new DMLRuntimeException("Eigen Decomposition can only be done on a square matrix. " - + "Input matrix is rectangular (rows=" + in.getNumRows() + ", cols="+ in.getNumColumns() +")"); + + "Input matrix is rectangular (rows=" + in.getNumRows() + ", cols=" + in.getNumColumns() + ")"); } - + EigenDecomposition eigendecompose = null; try { Array2DRowRealMatrix matrixInput = DataConverter.convertToArray2DRowRealMatrix(in); eigendecompose = new EigenDecomposition(matrixInput); - } - catch(MaxCountExceededException ex) { - LOG.warn("Eigen: "+ ex.getMessage()+". Falling back to regularized eigen factorization."); + } catch (MaxCountExceededException ex) { + LOG.warn("Eigen: " + ex.getMessage() + ". Falling back to regularized eigen factorization."); eigendecompose = computeEigenRegularized(in); } - + RealMatrix eVectorsMatrix = eigendecompose.getV(); double[][] eVectors = eVectorsMatrix.getData(); double[] eValues = eigendecompose.getRealEigenvalues(); @@ -273,24 +274,24 @@ private static MatrixBlock[] computeEigen(MatrixBlock in) { } private static EigenDecomposition computeEigenRegularized(MatrixBlock in) { - if( in == null || in.isEmptyBlock(false) ) + if (in == null || in.isEmptyBlock(false)) throw new DMLRuntimeException("Invalid empty block"); - - //slightly modify input for regularization (pos/neg) + + // slightly modify input for regularization (pos/neg) MatrixBlock in2 = new MatrixBlock(in, false); DenseBlock a = in2.getDenseBlock(); - for( int i=0; i= 1e-7) + if (Math.abs(v1.sumSq() - 1.0) >= 1e-7) throw new DMLRuntimeException("v1 not correctly normalized (maybe try changing the seed)"); return v1; } /** - * Function to perform the Lanczos algorithm and then computes the Eigendecomposition. - * Caution: Lanczos is not numerically stable (see https://en.wikipedia.org/wiki/Lanczos_algorithm) + * Function to perform the Lanczos algorithm and then computes the + * Eigendecomposition. + * Caution: Lanczos is not numerically stable (see + * https://en.wikipedia.org/wiki/Lanczos_algorithm) * Input must be a symmetric (and square) matrix. * - * @param in matrix object + * @param in matrix object * @param threads number of threads - * @param seed seed for the random MatrixBlock generation + * @param seed seed for the random MatrixBlock generation * @return array of matrix blocks */ private static MatrixBlock[] computeEigenLanczos(MatrixBlock in, int threads, long seed) { - if(in.getNumRows() != in.getNumColumns()) { + if (in.getNumRows() != in.getNumColumns()) { throw new DMLRuntimeException( - "Lanczos algorithm and Eigen Decomposition can only be done on a square matrix. " - + "Input matrix is rectangular (rows=" + in.getNumRows() + ", cols=" + in.getNumColumns() + ")"); + "Lanczos algorithm and Eigen Decomposition can only be done on a square matrix. " + + "Input matrix is rectangular (rows=" + in.getNumRows() + ", cols=" + in.getNumColumns() + + ")"); } int m = in.getNumRows(); @@ -455,11 +449,12 @@ private static MatrixBlock[] computeEigenLanczos(MatrixBlock in, int threads, lo ScalarOperator op_div_scalar = new RightScalarOperator(Divide.getDivideFnObject(), 1, threads); MatrixBlock beta = new MatrixBlock(1, 1, 0.0); - for(int i = 0; i < m; i++) { + for (int i = 0; i < m; i++) { v1.putInto(TV, 0, i, false); w1 = in.aggregateBinaryOperations(in, v1, op_mul_agg); - MatrixBlock alpha = w1.aggregateBinaryOperations(v1.reorgOperations(op_t, new MatrixBlock(), 0, 0, m), w1, op_mul_agg); - if(i < m - 1) { + MatrixBlock alpha = w1.aggregateBinaryOperations(v1.reorgOperations(op_t, new MatrixBlock(), 0, 0, m), w1, + op_mul_agg); + if (i < m - 1) { w1 = w1.ternaryOperations(op_minus_mul, v1, alpha, new MatrixBlock()); w1 = w1.ternaryOperations(op_minus_mul, v0, beta, new MatrixBlock()); beta.setValue(0, 0, Math.sqrt(w1.sumSq())); @@ -474,7 +469,7 @@ private static MatrixBlock[] computeEigenLanczos(MatrixBlock in, int threads, lo } MatrixBlock[] e = computeEigen(T); - TV.setNonZeros((long) m*m); + TV.setNonZeros((long) m * m); e[1] = TV.aggregateBinaryOperations(TV, e[1], op_mul_agg); return e; } @@ -482,16 +477,17 @@ private static MatrixBlock[] computeEigenLanczos(MatrixBlock in, int threads, lo /** * Function to perform the QR decomposition. * Input must be a square matrix. - * TODO: use Householder transformation and implicit shifts to further speed up QR decompositions + * TODO: use Householder transformation and implicit shifts to further speed up + * QR decompositions * - * @param in matrix object + * @param in matrix object * @param threads number of threads * @return array of matrix blocks [Q, R] */ private static MatrixBlock[] computeQR2(MatrixBlock in, int threads) { - if(in.getNumRows() != in.getNumColumns()) { + if (in.getNumRows() != in.getNumColumns()) { throw new DMLRuntimeException("QR2 Decomposition can only be done on a square matrix. " - + "Input matrix is rectangular (rows=" + in.getNumRows() + ", cols=" + in.getNumColumns() + ")"); + + "Input matrix is rectangular (rows=" + in.getNumRows() + ", cols=" + in.getNumColumns() + ")"); } int m = in.rlen; @@ -500,7 +496,7 @@ private static MatrixBlock[] computeQR2(MatrixBlock in, int threads) { A_n.copy(in); MatrixBlock Q_n = new MatrixBlock(m, m, true); - for(int i = 0; i < m; i++) { + for (int i = 0; i < m; i++) { Q_n.setValue(i, i, 1.0); } @@ -510,7 +506,7 @@ private static MatrixBlock[] computeQR2(MatrixBlock in, int threads) { ScalarOperator op_div_scalar = new RightScalarOperator(Divide.getDivideFnObject(), 1, threads); ScalarOperator op_mult_2 = new LeftScalarOperator(Multiply.getMultiplyFnObject(), 2, threads); - for(int k = 0; k < m; k++) { + for (int k = 0; k < m; k++) { MatrixBlock z = A_n.slice(k, m - 1, k, k); MatrixBlock uk = new MatrixBlock(m - k, 1, 0.0); uk.copy(z); @@ -529,15 +525,16 @@ private static MatrixBlock[] computeQR2(MatrixBlock in, int threads) { Q_n = Q_n.binaryOperations(op_sub, Q_n.aggregateBinaryOperations(Q_n, vkvkt2, op_mul_agg)); } // QR decomp: Q: Q_n; R: A_n - return new MatrixBlock[] {Q_n, A_n}; + return new MatrixBlock[] { Q_n, A_n }; } /** * Function that computes the Eigen Decomposition using the QR algorithm. * Caution: check if the QR algorithm is converged, if not increase iterations - * Caution: if the input matrix has complex eigenvalues results will be incorrect + * Caution: if the input matrix has complex eigenvalues results will be + * incorrect * - * @param in Input matrix + * @param in Input matrix * @param threads number of threads * @return array of matrix blocks */ @@ -546,32 +543,33 @@ private static MatrixBlock[] computeEigenQR(MatrixBlock in, int threads) { } private static MatrixBlock[] computeEigenQR(MatrixBlock in, int num_iterations, double tol, int threads) { - if(in.getNumRows() != in.getNumColumns()) { + if (in.getNumRows() != in.getNumColumns()) { throw new DMLRuntimeException("Eigen Decomposition (QR) can only be done on a square matrix. " - + "Input matrix is rectangular (rows=" + in.getNumRows() + ", cols=" + in.getNumColumns() + ")"); + + "Input matrix is rectangular (rows=" + in.getNumRows() + ", cols=" + in.getNumColumns() + ")"); } int m = in.rlen; AggregateBinaryOperator op_mul_agg = InstructionUtils.getMatMultOperator(threads); MatrixBlock Q_prod = new MatrixBlock(m, m, 0.0); - for(int i = 0; i < m; i++) { + for (int i = 0; i < m; i++) { Q_prod.setValue(i, i, 1.0); } - for(int i = 0; i < num_iterations; i++) { + for (int i = 0; i < num_iterations; i++) { MatrixBlock[] QR = computeQR2(in, threads); Q_prod = Q_prod.aggregateBinaryOperations(Q_prod, QR[0], op_mul_agg); in = QR[1].aggregateBinaryOperations(QR[1], QR[0], op_mul_agg); } - // Is converged if all values are below tol and the there only is values on the diagonal. + // Is converged if all values are below tol and the there only is values on the + // diagonal. double[] check = in.getDenseBlockValues(); double[] eval = new double[m]; - for(int i = 0; i < m; i++) - eval[i] = check[i*m+i]; - + for (int i = 0; i < m; i++) + eval[i] = check[i * m + i]; + double[] evec = Q_prod.getDenseBlockValues(); return sortEVs(eval, evec); } @@ -579,7 +577,7 @@ private static MatrixBlock[] computeEigenQR(MatrixBlock in, int num_iterations, /** * Function to compute the Householder transformation of a Matrix. * - * @param in Input Matrix + * @param in Input Matrix * @param threads number of threads * @return transformed matrix */ @@ -590,14 +588,14 @@ private static MatrixBlock computeHouseholder(MatrixBlock in, int threads) { MatrixBlock A_n = new MatrixBlock(m, m, 0.0); A_n.copy(in); - for(int k = 0; k < m - 2; k++) { + for (int k = 0; k < m - 2; k++) { MatrixBlock ajk = A_n.slice(0, m - 1, k, k); - for(int i = 0; i <= k; i++) { + for (int i = 0; i <= k; i++) { ajk.setValue(i, 0, 0.0); } double alpha = Math.sqrt(ajk.sumSq()); double ak1k = A_n.getDouble(k + 1, k); - if(ak1k > 0.0) + if (ak1k > 0.0) alpha *= -1; double r = Math.sqrt(0.5 * (alpha * alpha - ak1k * alpha)); MatrixBlock v = new MatrixBlock(m, 1, 0.0); @@ -607,7 +605,7 @@ private static MatrixBlock computeHouseholder(MatrixBlock in, int threads) { v = v.scalarOperations(op_div_scalar, new MatrixBlock()); MatrixBlock P = new MatrixBlock(m, m, 0.0); - for(int i = 0; i < m; i++) { + for (int i = 0; i < m; i++) { P.setValue(i, i, 1.0); } @@ -626,7 +624,8 @@ private static MatrixBlock computeHouseholder(MatrixBlock in, int threads) { } /** - * Sort the eigen values (and vectors) in increasing order (to be compatible w/ LAPACK.DSYEVR()) + * Sort the eigen values (and vectors) in increasing order (to be compatible w/ + * LAPACK.DSYEVR()) * * @param eValues Eigenvalues * @param eVectors Eigenvectors @@ -634,19 +633,19 @@ private static MatrixBlock computeHouseholder(MatrixBlock in, int threads) { */ private static MatrixBlock[] sortEVs(double[] eValues, double[][] eVectors) { int n = eValues.length; - for(int i = 0; i < n; i++) { + for (int i = 0; i < n; i++) { int k = i; double p = eValues[i]; - for(int j = i + 1; j < n; j++) { - if(eValues[j] < p) { + for (int j = i + 1; j < n; j++) { + if (eValues[j] < p) { k = j; p = eValues[j]; } } - if(k != i) { + if (k != i) { eValues[k] = eValues[i]; eValues[i] = p; - for(int j = 0; j < n; j++) { + for (int j = 0; j < n; j++) { p = eVectors[j][i]; eVectors[j][i] = eVectors[j][k]; eVectors[j][k] = p; @@ -656,27 +655,27 @@ private static MatrixBlock[] sortEVs(double[] eValues, double[][] eVectors) { MatrixBlock eval = DataConverter.convertToMatrixBlock(eValues, true); MatrixBlock evec = DataConverter.convertToMatrixBlock(eVectors); - return new MatrixBlock[] {eval, evec}; + return new MatrixBlock[] { eval, evec }; } private static MatrixBlock[] sortEVs(double[] eValues, double[] eVectors) { int n = eValues.length; - for(int i = 0; i < n; i++) { + for (int i = 0; i < n; i++) { int k = i; double p = eValues[i]; - for(int j = i + 1; j < n; j++) { - if(eValues[j] < p) { + for (int j = i + 1; j < n; j++) { + if (eValues[j] < p) { k = j; p = eValues[j]; } } - if(k != i) { + if (k != i) { eValues[k] = eValues[i]; eValues[i] = p; - for(int j = 0; j < n; j++) { - p = eVectors[j*n+i]; - eVectors[j*n+i] = eVectors[j*n+k]; - eVectors[j*n+k] = p; + for (int j = 0; j < n; j++) { + p = eVectors[j * n + i]; + eVectors[j * n + i] = eVectors[j * n + k]; + eVectors[j * n + k] = p; } } } @@ -684,6 +683,6 @@ private static MatrixBlock[] sortEVs(double[] eValues, double[] eVectors) { MatrixBlock eval = DataConverter.convertToMatrixBlock(eValues, true); MatrixBlock evec = new MatrixBlock(n, n, false); evec.init(eVectors, n, n); - return new MatrixBlock[] {eval, evec}; + return new MatrixBlock[] { eval, evec }; } } diff --git a/src/main/java/org/apache/sysds/runtime/matrix/data/LibMatrixFourier.java b/src/main/java/org/apache/sysds/runtime/matrix/data/LibMatrixFourier.java index 9b424b74924..dc40b3039b0 100644 --- a/src/main/java/org/apache/sysds/runtime/matrix/data/LibMatrixFourier.java +++ b/src/main/java/org/apache/sysds/runtime/matrix/data/LibMatrixFourier.java @@ -42,9 +42,6 @@ public static MatrixBlock[] fft(MatrixBlock re, MatrixBlock im) { throw new RuntimeException("false dimensions"); fft(re.getDenseBlockValues(), im.getDenseBlockValues(), rows, cols); - System.out.println("fft result in java function:"); - System.out.println((new MatrixBlock[] { re, im })[0].toString()); - System.out.println("\n\n"); return new MatrixBlock[] { re, im }; } @@ -69,10 +66,6 @@ public static MatrixBlock[] ifft(MatrixBlock re, MatrixBlock im) { ifft(re.getDenseBlockValues(), im.getDenseBlockValues(), rows, cols); - System.out.println("IFFT result in function"); - System.out.println((new MatrixBlock[] { re, im })[0].toString()); - System.out.println("\n\n"); - return new MatrixBlock[] { re, im }; } diff --git a/src/main/java/org/apache/sysds/runtime/privacy/propagation/PrivacyPropagator.java b/src/main/java/org/apache/sysds/runtime/privacy/propagation/PrivacyPropagator.java index 26aadd17687..4d2996b7856 100644 --- a/src/main/java/org/apache/sysds/runtime/privacy/propagation/PrivacyPropagator.java +++ b/src/main/java/org/apache/sysds/runtime/privacy/propagation/PrivacyPropagator.java @@ -295,7 +295,7 @@ private static Instruction preprocessCPInstruction(CPInstruction inst, Execution case Ternary: case Unary: case MultiReturnBuiltin: - case MultiReturnMatrixMatrixBuiltin: + case MultiReturnComplexMatrixBuiltin: case MultiReturnParameterizedBuiltin: case MatrixIndexing: return mergePrivacyConstraintsFromInput(inst, ec, OperatorType.NonAggregate);