Repository: incubator-systemml Updated Branches: refs/heads/master 19eed8f38 -> 9dad2fe8b
[SYSTEMML-1644] Fix correctness dense-dense matrix mult w/ short lhs This patch fixes an issue of incorrect results for special cases of dense-dense matrix multiplication with short left-hand-side (1 < x <= 16 rows) and a right-hand-side of more than 1024 features. The underlying issues were (1) incorrect indexing in the rhs for all but the first column cache block of 1024 columns, and (2) wrong rhs row boundaries. Hence, it affects, for example, Mlogreg and Kmeans with a number of classes/ centroids <= 16 and a feature matrix with > 1024 features. Project: http://git-wip-us.apache.org/repos/asf/incubator-systemml/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-systemml/commit/8738121b Tree: http://git-wip-us.apache.org/repos/asf/incubator-systemml/tree/8738121b Diff: http://git-wip-us.apache.org/repos/asf/incubator-systemml/diff/8738121b Branch: refs/heads/master Commit: 8738121b42bbca5a9fa1e4b661e09d0be5ec65ae Parents: 19eed8f Author: Matthias Boehm <[email protected]> Authored: Mon May 29 16:15:57 2017 -0700 Committer: Matthias Boehm <[email protected]> Committed: Mon May 29 22:36:12 2017 -0700 ---------------------------------------------------------------------- .../runtime/matrix/data/LibMatrixMult.java | 12 +-- .../MatrixMultShortLhsTest.java | 103 +++++++++++++++++++ .../matrix_full_other/MatrixMultShortLhs.R | 35 +++++++ .../matrix_full_other/MatrixMultShortLhs.dml | 27 +++++ 4 files changed, 171 insertions(+), 6 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-systemml/blob/8738121b/src/main/java/org/apache/sysml/runtime/matrix/data/LibMatrixMult.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/sysml/runtime/matrix/data/LibMatrixMult.java b/src/main/java/org/apache/sysml/runtime/matrix/data/LibMatrixMult.java index f7d2d54..f0f2196 100644 --- a/src/main/java/org/apache/sysml/runtime/matrix/data/LibMatrixMult.java +++ b/src/main/java/org/apache/sysml/runtime/matrix/data/LibMatrixMult.java @@ -1013,19 +1013,19 @@ public class LibMatrixMult for( int k=rl, bix=rl*n; k<rl+kn; k++, bix+=n ) if( a[aix+k] != 0 ) vectMultiplyAdd(a[aix+k], b, c, bix, cix, n); - - final int blocksizeK = 48; - final int blocksizeJ = 1024; + + final int blocksizeK = 48; + final int blocksizeJ = 1024; //blocked execution for( int bk = rl+kn; bk < ru; bk+=blocksizeK ) - for( int bj = 0, bkmin = Math.min(cd, bk+blocksizeK); bj < n; bj+=blocksizeJ ) + for( int bj = 0, bkmin = Math.min(ru, bk+blocksizeK); bj < n; bj+=blocksizeJ ) { //compute blocks of 4 rows in rhs w/ IKJ int bjlen = Math.min(n, bj+blocksizeJ)-bj; for( int i=0, aix=0, cix=bj; i<m; i++, aix+=cd, cix+=n ) - for( int k=bk, bix=bk*n; k<bkmin; k+=4, bix+=4*n ) { - vectMultiplyAdd4(a[aix+k], a[aix+k+1], a[aix+k+2], a[aix+k+3], + for( int k=bk, bix=bk*n+bj; k<bkmin; k+=4, bix+=4*n ) { + vectMultiplyAdd4(a[aix+k], a[aix+k+1], a[aix+k+2], a[aix+k+3], b, c, bix, bix+n, bix+2*n, bix+3*n, cix, bjlen); } } http://git-wip-us.apache.org/repos/asf/incubator-systemml/blob/8738121b/src/test/java/org/apache/sysml/test/integration/functions/binary/matrix_full_other/MatrixMultShortLhsTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/sysml/test/integration/functions/binary/matrix_full_other/MatrixMultShortLhsTest.java b/src/test/java/org/apache/sysml/test/integration/functions/binary/matrix_full_other/MatrixMultShortLhsTest.java new file mode 100644 index 0000000..985ea6c --- /dev/null +++ b/src/test/java/org/apache/sysml/test/integration/functions/binary/matrix_full_other/MatrixMultShortLhsTest.java @@ -0,0 +1,103 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.sysml.test.integration.functions.binary.matrix_full_other; + +import java.util.HashMap; + +import org.junit.Test; + +import org.apache.sysml.lops.LopProperties.ExecType; +import org.apache.sysml.runtime.matrix.data.MatrixValue.CellIndex; +import org.apache.sysml.test.integration.AutomatedTestBase; +import org.apache.sysml.test.integration.TestConfiguration; +import org.apache.sysml.test.utils.TestUtils; + +public class MatrixMultShortLhsTest extends AutomatedTestBase +{ + private final static String TEST_NAME = "MatrixMultShortLhs"; + private final static String TEST_DIR = "functions/binary/matrix_full_other/"; + private final static String TEST_CLASS_DIR = TEST_DIR + MatrixMultShortLhsTest.class.getSimpleName() + "/"; + private final static double eps = 1e-10; + + private final static int rowsA = 10; + private final static int colsA = 2023; + private final static int rowsB = 2023; + private final static int colsB = 1997; + + private final static double sparsity1 = 0.9; + private final static double sparsity2 = 0.1; + + @Override + public void setUp() { + TestUtils.clearAssertionInformation(); + addTestConfiguration(TEST_NAME, new TestConfiguration(TEST_CLASS_DIR, TEST_NAME, new String[] { "C" })); + } + + @Test + public void testMMDenseDenseCP() { + runMatrixMatrixMultiplicationTest(false, false, ExecType.CP); + } + + @Test + public void testMMDenseSparseCP() { + runMatrixMatrixMultiplicationTest(false, true, ExecType.CP); + } + + @Test + public void testMMSparseDenseCP() { + runMatrixMatrixMultiplicationTest(true, false, ExecType.CP); + } + + @Test + public void testMMSparseSparseCP() { + runMatrixMatrixMultiplicationTest(true, true, ExecType.CP); + } + + private void runMatrixMatrixMultiplicationTest( boolean sparseM1, boolean sparseM2, ExecType instType) + { + loadTestConfiguration(getTestConfiguration(TEST_NAME)); + double sparsityA = sparseM1?sparsity2:sparsity1; + double sparsityB = sparseM2?sparsity2:sparsity1; + + String HOME = SCRIPT_DIR + TEST_DIR; + fullDMLScriptName = HOME + TEST_NAME + ".dml"; + programArgs = new String[]{"-args", + input("A"), input("B"), output("C") }; + + fullRScriptName = HOME + TEST_NAME + ".R"; + rCmd = "Rscript" + " " + fullRScriptName + " " + + inputDir() + " " + expectedDir(); + + //generate datasets + double[][] A = getRandomMatrix(rowsA, colsA, 0, 1, sparsityA, 7); + writeInputMatrixWithMTD("A", A, true); + double[][] B = getRandomMatrix(rowsB, colsB, 0, 1, sparsityB, 3); + writeInputMatrixWithMTD("B", B, true); + + //run tests + runTest(true, false, null, -1); + runRScript(true); + + //compare matrices + HashMap<CellIndex, Double> dmlfile = readDMLMatrixFromHDFS("C"); + HashMap<CellIndex, Double> rfile = readRMatrixFromFS("C"); + TestUtils.compareMatrices(dmlfile, rfile, eps, "Stat-DML", "Stat-R"); + } +} http://git-wip-us.apache.org/repos/asf/incubator-systemml/blob/8738121b/src/test/scripts/functions/binary/matrix_full_other/MatrixMultShortLhs.R ---------------------------------------------------------------------- diff --git a/src/test/scripts/functions/binary/matrix_full_other/MatrixMultShortLhs.R b/src/test/scripts/functions/binary/matrix_full_other/MatrixMultShortLhs.R new file mode 100644 index 0000000..5c4f943 --- /dev/null +++ b/src/test/scripts/functions/binary/matrix_full_other/MatrixMultShortLhs.R @@ -0,0 +1,35 @@ +#------------------------------------------------------------- +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# +#------------------------------------------------------------- + + +args <- commandArgs(TRUE) +options(digits=22) + +library("Matrix") + +A = as.matrix(readMM(paste(args[1], "A.mtx", sep=""))) +B = as.matrix(readMM(paste(args[1], "B.mtx", sep=""))) + +C = A %*% B; + +writeMM(as(C, "CsparseMatrix"), paste(args[2], "C", sep="")); + + http://git-wip-us.apache.org/repos/asf/incubator-systemml/blob/8738121b/src/test/scripts/functions/binary/matrix_full_other/MatrixMultShortLhs.dml ---------------------------------------------------------------------- diff --git a/src/test/scripts/functions/binary/matrix_full_other/MatrixMultShortLhs.dml b/src/test/scripts/functions/binary/matrix_full_other/MatrixMultShortLhs.dml new file mode 100644 index 0000000..e55fd26 --- /dev/null +++ b/src/test/scripts/functions/binary/matrix_full_other/MatrixMultShortLhs.dml @@ -0,0 +1,27 @@ +#------------------------------------------------------------- +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# +#------------------------------------------------------------- + +A = read($1); +B = read($2); + +C = A %*% B; + +write(C, $3); \ No newline at end of file
