|
| 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.test.functions.rewrite; |
| 21 | + |
| 22 | +import org.apache.sysds.common.Opcodes; |
| 23 | +import org.apache.sysds.hops.OptimizerUtils; |
| 24 | +import org.apache.sysds.runtime.matrix.data.MatrixValue; |
| 25 | +import org.apache.sysds.test.AutomatedTestBase; |
| 26 | +import org.apache.sysds.test.TestConfiguration; |
| 27 | +import org.apache.sysds.test.TestUtils; |
| 28 | +import org.junit.Assert; |
| 29 | +import org.junit.Test; |
| 30 | + |
| 31 | +import java.util.HashMap; |
| 32 | + |
| 33 | +public class RewriteRemoveUnnecessaryBinaryOperationTest extends AutomatedTestBase { |
| 34 | + |
| 35 | + private static final String TEST_NAME = "RewriteRemoveUnnecessaryBinaryOperation"; |
| 36 | + private static final String TEST_DIR = "functions/rewrite/"; |
| 37 | + private static final String TEST_CLASS_DIR = |
| 38 | + TEST_DIR + RewriteRemoveUnnecessaryBinaryOperationTest.class.getSimpleName() + "/"; |
| 39 | + |
| 40 | + private static final int rows = 500; |
| 41 | + private static final int cols = 500; |
| 42 | + private static final double eps = Math.pow(10, -10); |
| 43 | + |
| 44 | + @Override |
| 45 | + public void setUp() { |
| 46 | + TestUtils.clearAssertionInformation(); |
| 47 | + addTestConfiguration(TEST_NAME, new TestConfiguration(TEST_CLASS_DIR, TEST_NAME, new String[] {"R"})); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + public void testRemoveUnnecessaryBinaryOperationDivNoRewrite() { |
| 52 | + testRewriteRemoveUnnecessaryBinaryOperation(1, false); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + public void testRemoveUnnecessaryBinaryOperationDivRewrite() { |
| 57 | + testRewriteRemoveUnnecessaryBinaryOperation(1, true); // X/1 |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + public void testRemoveUnnecessaryBinaryOperationMultRightNoRewrite() { |
| 62 | + testRewriteRemoveUnnecessaryBinaryOperation(2, false); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + public void testRemoveUnnecessaryBinaryOperationMultRightRewrite() { |
| 67 | + testRewriteRemoveUnnecessaryBinaryOperation(2, true); // X*1 |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + public void testRemoveUnnecessaryBinaryOperationMultLeftNoRewrite() { |
| 72 | + testRewriteRemoveUnnecessaryBinaryOperation(3, false); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + public void testRemoveUnnecessaryBinaryOperationMultLeftRewrite() { |
| 77 | + testRewriteRemoveUnnecessaryBinaryOperation(3, true); // 1*X |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + public void testRemoveUnnecessaryBinaryOperationMinusNoRewrite() { |
| 82 | + testRewriteRemoveUnnecessaryBinaryOperation(4, false); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + public void testRemoveUnnecessaryBinaryOperationMinusRewrite() { |
| 87 | + testRewriteRemoveUnnecessaryBinaryOperation(4, true); // X-0 |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + public void testRemoveUnnecessaryBinaryOperationNegMultLeftNoRewrite() { |
| 92 | + testRewriteRemoveUnnecessaryBinaryOperation(5, false); |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + public void testRemoveUnnecessaryBinaryOperationNegMultLeftRewrite() { |
| 97 | + testRewriteRemoveUnnecessaryBinaryOperation(5, true); // -1*X |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + public void testRemoveUnnecessaryBinaryOperationNegMultRightNoRewrite() { |
| 102 | + testRewriteRemoveUnnecessaryBinaryOperation(6, false); |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + public void testRemoveUnnecessaryBinaryOperationNegMultRightRewrite() { |
| 107 | + testRewriteRemoveUnnecessaryBinaryOperation(6, true); // X*-1 |
| 108 | + } |
| 109 | + |
| 110 | + private void testRewriteRemoveUnnecessaryBinaryOperation(int ID, boolean rewrites) { |
| 111 | + boolean oldFlag = OptimizerUtils.ALLOW_ALGEBRAIC_SIMPLIFICATION; |
| 112 | + try { |
| 113 | + TestConfiguration config = getTestConfiguration(TEST_NAME); |
| 114 | + loadTestConfiguration(config); |
| 115 | + |
| 116 | + String HOME = SCRIPT_DIR + TEST_DIR; |
| 117 | + fullDMLScriptName = HOME + TEST_NAME + ".dml"; |
| 118 | + programArgs = new String[] {"-stats", "-args", input("X"), String.valueOf(ID), output("R")}; |
| 119 | + fullRScriptName = HOME + TEST_NAME + ".R"; |
| 120 | + rCmd = getRCmd(inputDir(), String.valueOf(ID), expectedDir()); |
| 121 | + |
| 122 | + OptimizerUtils.ALLOW_ALGEBRAIC_SIMPLIFICATION = rewrites; |
| 123 | + |
| 124 | + // create and write matrix |
| 125 | + double[][] X = getRandomMatrix(rows, cols, -1, 1, 0.70d, 5); |
| 126 | + writeInputMatrixWithMTD("X", X, true); |
| 127 | + |
| 128 | + runTest(true, false, null, -1); |
| 129 | + runRScript(true); |
| 130 | + |
| 131 | + //compare matrices |
| 132 | + HashMap<MatrixValue.CellIndex, Double> dmlfile = readDMLMatrixFromOutputDir("R"); |
| 133 | + HashMap<MatrixValue.CellIndex, Double> rfile = readRMatrixFromExpectedDir("R"); |
| 134 | + TestUtils.compareMatrices(dmlfile, rfile, eps, "Stat-DML", "Stat-R"); |
| 135 | + |
| 136 | + if(ID == 1) { |
| 137 | + if(rewrites) |
| 138 | + Assert.assertFalse(heavyHittersContainsString(Opcodes.DIV.toString())); |
| 139 | + else |
| 140 | + Assert.assertTrue(heavyHittersContainsString(Opcodes.DIV.toString())); |
| 141 | + } |
| 142 | + else if(ID == 2 || ID == 3) { |
| 143 | + if(rewrites) |
| 144 | + Assert.assertFalse(heavyHittersContainsString(Opcodes.MULT.toString())); |
| 145 | + else |
| 146 | + Assert.assertTrue(heavyHittersContainsString(Opcodes.MULT.toString())); |
| 147 | + } |
| 148 | + else if(ID == 4) { |
| 149 | + if(rewrites) |
| 150 | + Assert.assertFalse(heavyHittersContainsString(Opcodes.MINUS.toString())); |
| 151 | + else |
| 152 | + Assert.assertTrue(heavyHittersContainsString(Opcodes.MINUS.toString())); |
| 153 | + } |
| 154 | + else if(ID == 5 || ID == 6) { |
| 155 | + if(rewrites) |
| 156 | + Assert.assertTrue(heavyHittersContainsString(Opcodes.MINUS.toString()) && |
| 157 | + !heavyHittersContainsString(Opcodes.MULT.toString())); |
| 158 | + else |
| 159 | + Assert.assertTrue(!heavyHittersContainsString(Opcodes.MINUS.toString()) && |
| 160 | + heavyHittersContainsString(Opcodes.MULT.toString())); |
| 161 | + } |
| 162 | + |
| 163 | + } |
| 164 | + finally { |
| 165 | + OptimizerUtils.ALLOW_ALGEBRAIC_SIMPLIFICATION = oldFlag; |
| 166 | + } |
| 167 | + } |
| 168 | +} |
0 commit comments