Search in sources :

Example 26 with PreparedScript

use of org.apache.sysml.api.jmlc.PreparedScript in project incubator-systemml by apache.

the class ReuseModelVariablesTest method execDMLScriptviaJMLC.

private static ArrayList<double[][]> execDMLScriptviaJMLC(String testname, ArrayList<double[][]> X, boolean modelReuse) throws IOException {
    Timing time = new Timing(true);
    ArrayList<double[][]> ret = new ArrayList<double[][]>();
    // establish connection to SystemML
    Connection conn = new Connection();
    try {
        // For now, JMLC pipeline only allows dml
        boolean parsePyDML = false;
        // read and precompile script
        String script = conn.readScript(SCRIPT_DIR + TEST_DIR + testname + ".dml");
        PreparedScript pstmt = conn.prepareScript(script, new String[] { "X", "W" }, new String[] { "predicted_y" }, parsePyDML);
        // read model
        String modelData = conn.readScript(SCRIPT_DIR + TEST_DIR + MODEL_FILE);
        double[][] W = conn.convertToDoubleMatrix(modelData, rows, cols);
        if (modelReuse)
            pstmt.setMatrix("W", W, true);
        // execute script multiple times
        for (int i = 0; i < nRuns; i++) {
            // bind input parameters
            if (!modelReuse)
                pstmt.setMatrix("W", W);
            pstmt.setMatrix("X", X.get(i));
            // execute script
            ResultVariables rs = pstmt.executeScript();
            // get output parameter
            double[][] Y = rs.getMatrix("predicted_y");
            // keep result for comparison
            ret.add(Y);
        }
    } catch (Exception ex) {
        ex.printStackTrace();
        throw new IOException(ex);
    } finally {
        IOUtilFunctions.closeSilently(conn);
    }
    System.out.println("JMLC scoring w/ " + nRuns + " runs in " + time.stop() + "ms.");
    return ret;
}
Also used : PreparedScript(org.apache.sysml.api.jmlc.PreparedScript) ResultVariables(org.apache.sysml.api.jmlc.ResultVariables) ArrayList(java.util.ArrayList) Connection(org.apache.sysml.api.jmlc.Connection) Timing(org.apache.sysml.runtime.controlprogram.parfor.stat.Timing) IOException(java.io.IOException) IOException(java.io.IOException)

Example 27 with PreparedScript

use of org.apache.sysml.api.jmlc.PreparedScript in project incubator-systemml by apache.

the class BuildLiteExecution method jmlcScoringExample.

public static void jmlcScoringExample() throws Exception {
    String scriptString = "X = read(\"./tmp/X\", rows=-1, cols=-1);\n" + "W = read(\"./tmp/W\", rows=-1, cols=-1);\n" + "\n" + "numRows = nrow(X);\n" + "numCols = ncol(X);\n" + "b = W[numCols+1,]\n" + "scores = X %*% W[1:numCols,] + b;\n" + "predicted_y = rowIndexMax(scores);\n" + "\n" + "print('pred:' + toString(predicted_y))\n" + "\n" + "write(predicted_y, \"./tmp\", format=\"text\");\n";
    File file = new File(getRoot() + "scoring-example.dml");
    System.out.println(file.toString());
    FileUtils.writeStringToFile(file, scriptString);
    Connection conn = getConfiguredConnection();
    String dml = conn.readScript(getRoot() + "scoring-example.dml");
    PreparedScript script = conn.prepareScript(dml, new String[] { "W", "X" }, new String[] { "predicted_y" }, false);
    double[][] mtx = matrix(4, 3, new double[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 });
    double[][] result = null;
    script.setMatrix("W", mtx);
    script.setMatrix("X", randomMatrix(3, 3, -1, 1, 0.7));
    result = script.executeScript().getMatrix("predicted_y");
    log.debug(displayMatrix(result));
    script.setMatrix("W", mtx);
    script.setMatrix("X", randomMatrix(3, 3, -1, 1, 0.7));
    result = script.executeScript().getMatrix("predicted_y");
    log.debug(displayMatrix(result));
    script.setMatrix("W", mtx);
    script.setMatrix("X", randomMatrix(3, 3, -1, 1, 0.7));
    result = script.executeScript().getMatrix("predicted_y");
    log.debug(displayMatrix(result));
    conn.close();
}
Also used : PreparedScript(org.apache.sysml.api.jmlc.PreparedScript) Connection(org.apache.sysml.api.jmlc.Connection) File(java.io.File)

Example 28 with PreparedScript

use of org.apache.sysml.api.jmlc.PreparedScript in project incubator-systemml by apache.

the class BuildLiteExecution method jmlcALS.

public static void jmlcALS() throws Exception {
    Connection conn = getConfiguredConnection();
    String dataGen = conn.readScript("scripts/datagen/genRandData4ALS.dml");
    Map<String, String> m = new HashMap<>();
    m.put("$rows", "1000");
    m.put("$cols", "1000");
    m.put("$rank", "100");
    m.put("$nnz", "10000");
    PreparedScript dataGenScript = conn.prepareScript(dataGen, m, new String[] {}, new String[] { "X", "W", "H" }, false);
    ResultVariables dataGenResults = dataGenScript.executeScript();
    double[][] x = dataGenResults.getMatrix("X");
    log.debug(displayMatrix(x));
    Map<String, String> m2 = new HashMap<>();
    m2.put("$rank", "100");
    String alsCg = conn.readScript("scripts/algorithms/ALS-CG.dml");
    PreparedScript alsCgScript = conn.prepareScript(alsCg, m2, new String[] { "X" }, new String[] { "U", "V" }, false);
    alsCgScript.setMatrix("X", x);
    ResultVariables alsCgResults = alsCgScript.executeScript();
    double[][] u = alsCgResults.getMatrix("U");
    log.debug("u:" + u);
    log.debug(displayMatrix(u));
    double[][] v = alsCgResults.getMatrix("V");
    log.debug("v:" + v);
    log.debug(displayMatrix(v));
    String alsDs = conn.readScript("scripts/algorithms/ALS-DS.dml");
    PreparedScript alsDsScript = conn.prepareScript(alsDs, m2, new String[] { "V" }, new String[] { "L", "Rt" }, false);
    alsDsScript.setMatrix("V", x);
    ResultVariables alsDsResults = alsDsScript.executeScript();
    double[][] l = alsDsResults.getMatrix("L");
    log.debug("l:" + l);
    log.debug(displayMatrix(l));
    double[][] rt = alsDsResults.getMatrix("Rt");
    log.debug("rt:" + rt);
    log.debug(displayMatrix(rt));
    conn.close();
}
Also used : PreparedScript(org.apache.sysml.api.jmlc.PreparedScript) HashMap(java.util.HashMap) ResultVariables(org.apache.sysml.api.jmlc.ResultVariables) Connection(org.apache.sysml.api.jmlc.Connection)

Example 29 with PreparedScript

use of org.apache.sysml.api.jmlc.PreparedScript in project incubator-systemml by apache.

the class BuildLiteExecution method jmlcHelloWorld.

public static void jmlcHelloWorld() throws Exception {
    Connection conn = getConfiguredConnection();
    PreparedScript script = conn.prepareScript("print('hello world');", new String[] {}, new String[] {}, false);
    script.executeScript();
    conn.close();
}
Also used : PreparedScript(org.apache.sysml.api.jmlc.PreparedScript) Connection(org.apache.sysml.api.jmlc.Connection)

Example 30 with PreparedScript

use of org.apache.sysml.api.jmlc.PreparedScript in project incubator-systemml by apache.

the class BuildLiteExecution method jmlcBasics.

public static void jmlcBasics() throws Exception {
    String dml = "A = matrix(\"1 2 3 4 5 6\", rows=3, cols=2)\n" + "print(toString(A))\n" + "B = A + 4\n" + "B = t(B)\n" + "print(toString(B))\n" + "C = A %*% B\n" + "print(toString(C))\n" + "D = matrix(5, rows=nrow(C), cols=ncol(C))\n" + "D = (C - D) / 2\n" + "print(toString(D))\n" + "\n" + "A = matrix(\"1 2 3 4 5 6 7 8 9\", rows=3, cols=3)\n" + "print(toString(A))\n" + "B = A[3,3]\n" + "print(toString(B))\n" + "C = A[2,]\n" + "print(toString(C))\n" + "D = A[,3]\n" + "print(toString(D))\n" + "E = A[2:3,1:2]\n" + "print(toString(E))\n" + "\n" + "i = 1\n" + "while (i <= 3) {\n" + " if (i == 1) {\n" + " print(\'hello\')\n" + " } else if (i == 2) {\n" + " print(\'world\')\n" + " } else {\n" + " print(\'!!!\')\n" + " }\n" + " i = i + 1\n" + "}\n" + "\n" + "A = matrix(\"1 2 3 4 5 6\", rows=3, cols=2)\n" + "\n" + "for (i in 1:nrow(A)) {\n" + " print(\"for A[\" + i + \",1]:\" + as.scalar(A[i,1]))\n" + "}\n" + "\n" + "parfor(i in 1:nrow(A)) {\n" + " print(\"parfor A[\" + i + \",1]:\" + as.scalar(A[i,1]))\n" + "}\n" + "\n" + "doSomething = function(matrix[double] mat) return (matrix[double] ret) {\n" + " additionalCol = matrix(1, rows=nrow(mat), cols=1) # 1x3 matrix with 1 values\n" + " ret = cbind(mat, additionalCol) # concatenate column to matrix\n" + " ret = cbind(ret, seq(0, 2, 1)) # concatenate column (0,1,2) to matrix\n" + " ret = cbind(ret, rowMaxs(ret)) # concatenate column of max row values to matrix\n" + " ret = cbind(ret, rowSums(ret)) # concatenate column of row sums to matrix\n" + "}\n" + "\n" + "A = rand(rows=3, cols=2, min=0, max=2) # random 3x2 matrix with values 0 to 2\n" + "B = doSomething(A)\n" + "write(A, \"" + getRoot() + "A.csv\", format=\"csv\")\n" + "write(B, \"" + getRoot() + "B.csv\", format=\"csv\")\n";
    Connection conn = getConfiguredConnection();
    PreparedScript script = conn.prepareScript(dml, new String[] {}, new String[] {}, false);
    script.executeScript();
    conn.close();
}
Also used : PreparedScript(org.apache.sysml.api.jmlc.PreparedScript) Connection(org.apache.sysml.api.jmlc.Connection)

Aggregations

Connection (org.apache.sysml.api.jmlc.Connection)37 PreparedScript (org.apache.sysml.api.jmlc.PreparedScript)37 ResultVariables (org.apache.sysml.api.jmlc.ResultVariables)14 Test (org.junit.Test)14 IOException (java.io.IOException)12 HashMap (java.util.HashMap)11 ArrayList (java.util.ArrayList)10 Timing (org.apache.sysml.runtime.controlprogram.parfor.stat.Timing)9 DMLException (org.apache.sysml.api.DMLException)2 File (java.io.File)1 ExecutorService (java.util.concurrent.ExecutorService)1 Future (java.util.concurrent.Future)1 SparkConf (org.apache.spark.SparkConf)1 JavaSparkContext (org.apache.spark.api.java.JavaSparkContext)1 DMLScript (org.apache.sysml.api.DMLScript)1 MLContext (org.apache.sysml.api.mlcontext.MLContext)1 Script (org.apache.sysml.api.mlcontext.Script)1 ScalarObject (org.apache.sysml.runtime.instructions.cp.ScalarObject)1 FrameBlock (org.apache.sysml.runtime.matrix.data.FrameBlock)1 MatrixBlock (org.apache.sysml.runtime.matrix.data.MatrixBlock)1