Search in sources :

Example 26 with ValueType

use of org.apache.sysml.parser.Expression.ValueType in project incubator-systemml by apache.

the class FrameSchemaReadTest method runFrameSchemaReadTest.

/**
 * @param testname
 * @param schema
 * @param wildcard
 */
private void runFrameSchemaReadTest(String testname, ValueType[] schema, boolean wildcard) {
    try {
        TestConfiguration config = getTestConfiguration(testname);
        loadTestConfiguration(config);
        String HOME = SCRIPT_DIR + TEST_DIR;
        fullDMLScriptName = HOME + testname + ".dml";
        programArgs = new String[] { "-explain", "-args", input("A"), getSchemaString(schema, wildcard), Integer.toString(rows), Integer.toString(schema.length), output("B") };
        // data generation
        double[][] A = getRandomMatrix(rows, schema.length, -10, 10, 0.9, 2373);
        // prepare input/output infos
        FrameBlock frame1 = new FrameBlock(schema);
        initFrameData(frame1, A, schema);
        // write frame data to hdfs
        FrameWriter writer = FrameWriterFactory.createFrameWriter(OutputInfo.CSVOutputInfo);
        writer.writeFrameToHDFS(frame1, input("A"), rows, schema.length);
        // run testcase
        runTest(true, false, null, -1);
        // read frame data from hdfs (not via readers to test physical schema)
        FrameReader reader = FrameReaderFactory.createFrameReader(InputInfo.BinaryBlockInputInfo);
        FrameBlock frame2 = ((FrameReaderBinaryBlock) reader).readFirstBlock(output("B"));
        // verify output schema
        ValueType[] schemaExpected = (testname.equals(TEST_NAME2) || wildcard) ? Collections.nCopies(schema.length, ValueType.STRING).toArray(new ValueType[0]) : schema;
        for (int i = 0; i < schemaExpected.length; i++) {
            Assert.assertEquals("Wrong result: " + frame2.getSchema()[i] + ".", schemaExpected[i], frame2.getSchema()[i]);
        }
    } catch (Exception ex) {
        ex.printStackTrace();
        throw new RuntimeException(ex);
    }
}
Also used : FrameReaderBinaryBlock(org.apache.sysml.runtime.io.FrameReaderBinaryBlock) FrameBlock(org.apache.sysml.runtime.matrix.data.FrameBlock) ValueType(org.apache.sysml.parser.Expression.ValueType) TestConfiguration(org.apache.sysml.test.integration.TestConfiguration) FrameReader(org.apache.sysml.runtime.io.FrameReader) FrameWriter(org.apache.sysml.runtime.io.FrameWriter)

Example 27 with ValueType

use of org.apache.sysml.parser.Expression.ValueType in project incubator-systemml by apache.

the class ValueTypeMatrixScalarBuiltinTest method runTest.

private void runTest(String testName, ValueType vtIn) {
    loadTestConfiguration(getTestConfiguration(testName));
    // setup arguments and run test
    String RI_HOME = SCRIPT_DIR + TEST_DIR;
    fullDMLScriptName = RI_HOME + testName + ".dml";
    programArgs = new String[] { "-args", vtIn == ValueType.DOUBLE ? "7.7" : "7", output("R") };
    runTest(true, false, null, -1);
    // check output value type
    ValueType vtOut = readDMLMetaDataValueType("R");
    Assert.assertTrue("Wrong output value type: " + vtOut.name(), vtOut.equals(ValueType.DOUBLE));
}
Also used : ValueType(org.apache.sysml.parser.Expression.ValueType)

Example 28 with ValueType

use of org.apache.sysml.parser.Expression.ValueType in project incubator-systemml by apache.

the class MLContextFrameTest method testInputFrameAndMatrixOutputMatrix.

@Test
public void testInputFrameAndMatrixOutputMatrix() {
    System.out.println("MLContextFrameTest - input frame and matrix, output matrix");
    List<String> dataA = new ArrayList<String>();
    dataA.add("Test1,4.0");
    dataA.add("Test2,5.0");
    dataA.add("Test3,6.0");
    JavaRDD<String> javaRddStringA = sc.parallelize(dataA);
    ValueType[] schema = { ValueType.STRING, ValueType.DOUBLE };
    List<String> dataB = new ArrayList<String>();
    dataB.add("1.0");
    dataB.add("2.0");
    JavaRDD<String> javaRddStringB = sc.parallelize(dataB);
    JavaRDD<Row> javaRddRowA = FrameRDDConverterUtils.csvToRowRDD(sc, javaRddStringA, CSV_DELIM, schema);
    JavaRDD<Row> javaRddRowB = javaRddStringB.map(new CommaSeparatedValueStringToDoubleArrayRow());
    List<StructField> fieldsA = new ArrayList<StructField>();
    fieldsA.add(DataTypes.createStructField("1", DataTypes.StringType, true));
    fieldsA.add(DataTypes.createStructField("2", DataTypes.DoubleType, true));
    StructType schemaA = DataTypes.createStructType(fieldsA);
    Dataset<Row> dataFrameA = spark.createDataFrame(javaRddRowA, schemaA);
    List<StructField> fieldsB = new ArrayList<StructField>();
    fieldsB.add(DataTypes.createStructField("1", DataTypes.DoubleType, true));
    StructType schemaB = DataTypes.createStructType(fieldsB);
    Dataset<Row> dataFrameB = spark.createDataFrame(javaRddRowB, schemaB);
    String dmlString = "[tA, tAM] = transformencode (target = A, spec = \"{ids: true ,recode: [ 1, 2 ]}\");\n" + "C = tA %*% B;\n" + "M = s * C;";
    Script script = dml(dmlString).in("A", dataFrameA, new FrameMetadata(FrameFormat.CSV, dataFrameA.count(), (long) dataFrameA.columns().length)).in("B", dataFrameB, new MatrixMetadata(MatrixFormat.CSV, dataFrameB.count(), (long) dataFrameB.columns().length)).in("s", 2).out("M");
    MLResults results = ml.execute(script);
    double[][] matrix = results.getMatrixAs2DDoubleArray("M");
    Assert.assertEquals(6.0, matrix[0][0], 0.0);
    Assert.assertEquals(12.0, matrix[1][0], 0.0);
    Assert.assertEquals(18.0, matrix[2][0], 0.0);
}
Also used : Script(org.apache.sysml.api.mlcontext.Script) StructType(org.apache.spark.sql.types.StructType) ValueType(org.apache.sysml.parser.Expression.ValueType) MLResults(org.apache.sysml.api.mlcontext.MLResults) ArrayList(java.util.ArrayList) CommaSeparatedValueStringToDoubleArrayRow(org.apache.sysml.test.integration.mlcontext.MLContextTest.CommaSeparatedValueStringToDoubleArrayRow) StructField(org.apache.spark.sql.types.StructField) Row(org.apache.spark.sql.Row) CommaSeparatedValueStringToDoubleArrayRow(org.apache.sysml.test.integration.mlcontext.MLContextTest.CommaSeparatedValueStringToDoubleArrayRow) MatrixMetadata(org.apache.sysml.api.mlcontext.MatrixMetadata) FrameMetadata(org.apache.sysml.api.mlcontext.FrameMetadata) Test(org.junit.Test)

Example 29 with ValueType

use of org.apache.sysml.parser.Expression.ValueType in project incubator-systemml by apache.

the class MLContextConversionUtil method javaRDDStringIJVToFrameObject.

/**
 * Convert a {@code JavaRDD<String>} in IJV format to a {@code FrameObject}
 * . Note that metadata is required for IJV format.
 *
 * @param javaRDD
 *            the Java RDD of strings
 * @param frameMetadata
 *            frame metadata
 * @return the {@code JavaRDD<String>} converted to a {@code FrameObject}
 */
public static FrameObject javaRDDStringIJVToFrameObject(JavaRDD<String> javaRDD, FrameMetadata frameMetadata) {
    JavaPairRDD<LongWritable, Text> javaPairRDD = javaRDD.mapToPair(new ConvertStringToLongTextPair());
    MatrixCharacteristics mc = (frameMetadata != null) ? frameMetadata.asMatrixCharacteristics() : new MatrixCharacteristics();
    JavaPairRDD<LongWritable, Text> javaPairRDDText = javaPairRDD.mapToPair(new CopyTextInputFunction());
    FrameObject frameObject = new FrameObject(OptimizerUtils.getUniqueTempFileName(), new MetaDataFormat(mc, OutputInfo.BinaryBlockOutputInfo, InputInfo.BinaryBlockInputInfo), frameMetadata.getFrameSchema().getSchema().toArray(new ValueType[0]));
    JavaPairRDD<Long, FrameBlock> rdd;
    try {
        ValueType[] lschema = null;
        if (lschema == null)
            lschema = UtilFunctions.nCopies((int) mc.getCols(), ValueType.STRING);
        rdd = FrameRDDConverterUtils.textCellToBinaryBlock(jsc(), javaPairRDDText, mc, lschema);
    } catch (DMLRuntimeException e) {
        e.printStackTrace();
        return null;
    }
    frameObject.setRDDHandle(new RDDObject(rdd));
    return frameObject;
}
Also used : MetaDataFormat(org.apache.sysml.runtime.matrix.MetaDataFormat) ValueType(org.apache.sysml.parser.Expression.ValueType) Text(org.apache.hadoop.io.Text) FrameObject(org.apache.sysml.runtime.controlprogram.caching.FrameObject) MatrixCharacteristics(org.apache.sysml.runtime.matrix.MatrixCharacteristics) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException) CopyTextInputFunction(org.apache.sysml.runtime.instructions.spark.functions.CopyTextInputFunction) ConvertStringToLongTextPair(org.apache.sysml.runtime.instructions.spark.functions.ConvertStringToLongTextPair) FrameBlock(org.apache.sysml.runtime.matrix.data.FrameBlock) RDDObject(org.apache.sysml.runtime.instructions.spark.data.RDDObject) LongWritable(org.apache.hadoop.io.LongWritable)

Example 30 with ValueType

use of org.apache.sysml.parser.Expression.ValueType in project incubator-systemml by apache.

the class RewriteRemoveUnnecessaryCasts method rule_RemoveUnnecessaryCasts.

@SuppressWarnings("unchecked")
private void rule_RemoveUnnecessaryCasts(Hop hop) {
    // check mark processed
    if (hop.isVisited())
        return;
    // recursively process childs
    ArrayList<Hop> inputs = hop.getInput();
    for (int i = 0; i < inputs.size(); i++) rule_RemoveUnnecessaryCasts(inputs.get(i));
    // remove unnecessary value type cast
    if (hop instanceof UnaryOp && HopRewriteUtils.isValueTypeCast(((UnaryOp) hop).getOp())) {
        Hop in = hop.getInput().get(0);
        // type cast input
        ValueType vtIn = in.getValueType();
        // type cast output
        ValueType vtOut = hop.getValueType();
        // if input/output types match, no need to cast
        if (vtIn == vtOut && vtIn != ValueType.UNKNOWN) {
            ArrayList<Hop> parents = hop.getParent();
            for (// for all parents
            int i = 0; // for all parents
            i < parents.size(); // for all parents
            i++) {
                Hop p = parents.get(i);
                ArrayList<Hop> pin = p.getInput();
                for (// for all parent childs
                int j = 0; // for all parent childs
                j < pin.size(); // for all parent childs
                j++) {
                    Hop pinj = pin.get(j);
                    if (// found parent ref
                    pinj == hop) {
                        // rehang cast input as child of cast consumer
                        // remove cast ref
                        pin.remove(j);
                        // add ref to cast input
                        pin.add(j, in);
                        // remove cast from cast input parents
                        in.getParent().remove(hop);
                        // add parent to cast input parents
                        in.getParent().add(p);
                    }
                }
            }
            parents.clear();
        }
    }
    // remove unnecessary data type casts
    if (hop instanceof UnaryOp && hop.getInput().get(0) instanceof UnaryOp) {
        UnaryOp uop1 = (UnaryOp) hop;
        UnaryOp uop2 = (UnaryOp) hop.getInput().get(0);
        if ((uop1.getOp() == OpOp1.CAST_AS_MATRIX && uop2.getOp() == OpOp1.CAST_AS_SCALAR) || (uop1.getOp() == OpOp1.CAST_AS_SCALAR && uop2.getOp() == OpOp1.CAST_AS_MATRIX)) {
            Hop input = uop2.getInput().get(0);
            // rewire parents
            ArrayList<Hop> parents = (ArrayList<Hop>) hop.getParent().clone();
            for (Hop p : parents) HopRewriteUtils.replaceChildReference(p, hop, input);
        }
    }
    // mark processed
    hop.setVisited();
}
Also used : UnaryOp(org.apache.sysml.hops.UnaryOp) ValueType(org.apache.sysml.parser.Expression.ValueType) Hop(org.apache.sysml.hops.Hop) ArrayList(java.util.ArrayList)

Aggregations

ValueType (org.apache.sysml.parser.Expression.ValueType)55 FrameBlock (org.apache.sysml.runtime.matrix.data.FrameBlock)23 MatrixCharacteristics (org.apache.sysml.runtime.matrix.MatrixCharacteristics)19 DMLRuntimeException (org.apache.sysml.runtime.DMLRuntimeException)18 DataType (org.apache.sysml.parser.Expression.DataType)11 MetaDataFormat (org.apache.sysml.runtime.matrix.MetaDataFormat)10 IOException (java.io.IOException)9 LongWritable (org.apache.hadoop.io.LongWritable)7 FrameObject (org.apache.sysml.runtime.controlprogram.caching.FrameObject)7 RDDObject (org.apache.sysml.runtime.instructions.spark.data.RDDObject)7 ArrayList (java.util.ArrayList)6 Text (org.apache.hadoop.io.Text)6 MatrixBlock (org.apache.sysml.runtime.matrix.data.MatrixBlock)6 RUNTIME_PLATFORM (org.apache.sysml.api.DMLScript.RUNTIME_PLATFORM)5 ConvertStringToLongTextPair (org.apache.sysml.runtime.instructions.spark.functions.ConvertStringToLongTextPair)5 OutputInfo (org.apache.sysml.runtime.matrix.data.OutputInfo)5 TestConfiguration (org.apache.sysml.test.integration.TestConfiguration)5 Row (org.apache.spark.sql.Row)4 StructType (org.apache.spark.sql.types.StructType)4 MatrixObject (org.apache.sysml.runtime.controlprogram.caching.MatrixObject)4