Search in sources :

Example 1 with Scalar

use of org.apache.sysml.udf.Scalar in project incubator-systemml by apache.

the class ExternalFunctionProgramBlock method verifyAndAttachOutputs.

/**
	 * Method to verify that function outputs match with declared outputs
	 * 
	 * @param ec execution context
	 * @param returnFunc package function
	 * @param outputParams output parameters
	 * @throws DMLRuntimeException if DMLRuntimeException occurs
	 */
protected void verifyAndAttachOutputs(ExecutionContext ec, PackageFunction returnFunc, String outputParams) throws DMLRuntimeException {
    ArrayList<String> outputs = getParameters(outputParams);
    if (outputs.size() != returnFunc.getNumFunctionOutputs()) {
        throw new DMLRuntimeException("Number of function outputs (" + returnFunc.getNumFunctionOutputs() + ") " + "does not match with declaration (" + outputs.size() + ").");
    }
    // iterate over each output and verify that type matches
    for (int i = 0; i < outputs.size(); i++) {
        StringTokenizer tk = new StringTokenizer(outputs.get(i), ":");
        ArrayList<String> tokens = new ArrayList<String>();
        while (tk.hasMoreTokens()) {
            tokens.add(tk.nextToken());
        }
        if (returnFunc.getFunctionOutput(i).getType() == FunctionParameterType.Matrix) {
            Matrix m = (Matrix) returnFunc.getFunctionOutput(i);
            if (!(tokens.get(0).equals(getFunctionParameterDataTypeString(FunctionParameterType.Matrix))) || !(tokens.get(2).equals(getMatrixValueTypeString(m.getValueType())))) {
                throw new DMLRuntimeException("Function output '" + outputs.get(i) + "' does not match with declaration.");
            }
            // add result to variableMapping
            String varName = tokens.get(1);
            MatrixObject newVar = createOutputMatrixObject(m);
            newVar.setVarName(varName);
            //getVariables().put(varName, newVar); //put/override in local symbol table
            ec.setVariable(varName, newVar);
            continue;
        }
        if (returnFunc.getFunctionOutput(i).getType() == FunctionParameterType.Scalar) {
            Scalar s = (Scalar) returnFunc.getFunctionOutput(i);
            if (!tokens.get(0).equals(getFunctionParameterDataTypeString(FunctionParameterType.Scalar)) || !tokens.get(2).equals(getScalarValueTypeString(s.getScalarType()))) {
                throw new DMLRuntimeException("Function output '" + outputs.get(i) + "' does not match with declaration.");
            }
            // allocate and set appropriate object based on type
            ScalarObject scalarObject = null;
            ScalarValueType type = s.getScalarType();
            switch(type) {
                case Integer:
                    scalarObject = new IntObject(tokens.get(1), Long.parseLong(s.getValue()));
                    break;
                case Double:
                    scalarObject = new DoubleObject(tokens.get(1), Double.parseDouble(s.getValue()));
                    break;
                case Boolean:
                    scalarObject = new BooleanObject(tokens.get(1), Boolean.parseBoolean(s.getValue()));
                    break;
                case Text:
                    scalarObject = new StringObject(tokens.get(1), s.getValue());
                    break;
                default:
                    throw new DMLRuntimeException("Unknown scalar value type '" + type + "' of output '" + outputs.get(i) + "'.");
            }
            //this.getVariables().put(tokens.get(1), scalarObject);
            ec.setVariable(tokens.get(1), scalarObject);
            continue;
        }
        if (returnFunc.getFunctionOutput(i).getType() == FunctionParameterType.Object) {
            if (!tokens.get(0).equals(getFunctionParameterDataTypeString(FunctionParameterType.Object))) {
                throw new DMLRuntimeException("Function output '" + outputs.get(i) + "' does not match with declaration.");
            }
            throw new DMLRuntimeException("Object types not yet supported");
        // continue;
        }
        throw new DMLRuntimeException("Unknown data type '" + returnFunc.getFunctionOutput(i).getType() + "' " + "of output '" + outputs.get(i) + "'.");
    }
}
Also used : MatrixObject(org.apache.sysml.runtime.controlprogram.caching.MatrixObject) DoubleObject(org.apache.sysml.runtime.instructions.cp.DoubleObject) ArrayList(java.util.ArrayList) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException) Scalar(org.apache.sysml.udf.Scalar) ScalarObject(org.apache.sysml.runtime.instructions.cp.ScalarObject) StringTokenizer(java.util.StringTokenizer) Matrix(org.apache.sysml.udf.Matrix) IntObject(org.apache.sysml.runtime.instructions.cp.IntObject) ScalarValueType(org.apache.sysml.udf.Scalar.ScalarValueType) StringObject(org.apache.sysml.runtime.instructions.cp.StringObject) BooleanObject(org.apache.sysml.runtime.instructions.cp.BooleanObject)

Example 2 with Scalar

use of org.apache.sysml.udf.Scalar in project incubator-systemml by apache.

the class Caffe2DMLVisualizeWrapper method execute.

@Override
public void execute() {
    String layerName = ((Scalar) this.getFunctionInput(0)).getValue();
    String varType = ((Scalar) this.getFunctionInput(1)).getValue();
    String aggFn = ((Scalar) this.getFunctionInput(2)).getValue();
    double x = Double.parseDouble(((Scalar) this.getFunctionInput(3)).getValue());
    double y = Double.parseDouble(((Scalar) this.getFunctionInput(4)).getValue());
    String logDir = ((Scalar) this.getFunctionInput(5)).getValue();
    String key = null;
    if (aggFn.equals("training_loss") || aggFn.equals("validation_loss") || aggFn.equals("training_accuracy") || aggFn.equals("validation_accuracy"))
        key = aggFn;
    else
        key = aggFn + "_" + varType + "_" + layerName;
    TensorboardLogger.writeScalar(logDir, key, (long) x, (float) y);
    _ret = new Scalar(ScalarValueType.Double, String.valueOf(1));
}
Also used : Scalar(org.apache.sysml.udf.Scalar)

Example 3 with Scalar

use of org.apache.sysml.udf.Scalar in project incubator-systemml by apache.

the class BinningWrapper method execute.

@Override
public void execute() {
    try {
        // get input parameters (input matrix assumed to be sorted)
        Matrix inM = (Matrix) getFunctionInput(0);
        double[][] col = inM.getMatrixAsDoubleArray();
        int binsize = Integer.parseInt(((Scalar) getFunctionInput(1)).getValue());
        int numbins = Integer.parseInt(((Scalar) getFunctionInput(2)).getValue());
        int nrowX = (int) inM.getNumRows();
        // execute binning (extend bins for duplicates)
        double[] col_bins = new double[numbins + 1];
        int pos_col = 0;
        int bin_id = 0;
        col_bins[0] = col[0][0];
        while (pos_col < nrowX - 1 && bin_id < numbins) {
            // for all bins
            pos_col = (pos_col + binsize >= nrowX) ? nrowX - 1 : pos_col + binsize;
            double end_val = col[pos_col][0];
            col_bins[bin_id + 1] = end_val;
            // pull all duplicates in current bin
            boolean cont = true;
            while (cont && pos_col < nrowX - 1) {
                if (end_val == col[pos_col + 1][0])
                    pos_col++;
                else
                    cont = false;
            }
            bin_id++;
        }
        // prepare results
        int num_bins_defined = bin_id;
        for (int i = 0; i < num_bins_defined; i++) col_bins[i] = (col_bins[i] + col_bins[i + 1]) / 2;
        // create and copy output matrix
        String dir = createOutputFilePathAndName(OUTPUT_FILE);
        _bins = new Matrix(dir, col_bins.length, 1, ValueType.Double);
        _bins.setMatrixDoubleArray(col_bins);
        _defBins = new Scalar(ScalarValueType.Integer, String.valueOf(num_bins_defined));
    } catch (Exception e) {
        throw new RuntimeException("Error executing external order function", e);
    }
}
Also used : Matrix(org.apache.sysml.udf.Matrix) Scalar(org.apache.sysml.udf.Scalar)

Example 4 with Scalar

use of org.apache.sysml.udf.Scalar in project incubator-systemml by apache.

the class CumSumProd method execute.

@Override
public void execute() {
    X = ((Matrix) getFunctionInput(0)).getMatrixObject().acquireRead();
    C = ((Matrix) getFunctionInput(1)).getMatrixObject().acquireRead();
    if (X.getNumRows() != C.getNumRows())
        throw new RuntimeException("Number of rows of X and C should match");
    if (X.getNumColumns() != C.getNumColumns() && C.getNumColumns() != 1)
        throw new RuntimeException("Incorrect Number of columns of X and C (Expected C to be of same dimension or a vector)");
    start = Double.parseDouble(((Scalar) getFunctionInput(2)).getValue());
    isReverse = Boolean.parseBoolean(((Scalar) getFunctionInput(3)).getValue());
    numRetRows = X.getNumRows();
    numRetCols = X.getNumColumns();
    allocateOutput();
    // Copy X to Y
    denseBlock = retMB.getDenseBlockValues();
    if (X.isInSparseFormat()) {
        Iterator<IJV> iter = X.getSparseBlockIterator();
        while (iter.hasNext()) {
            IJV ijv = iter.next();
            denseBlock[ijv.getI() * numRetCols + ijv.getJ()] = ijv.getV();
        }
    } else {
        if (X.getDenseBlock() != null)
            System.arraycopy(X.getDenseBlockValues(), 0, denseBlock, 0, denseBlock.length);
    }
    if (!isReverse) {
        // Y [1, ] = X [1, ] + C [1, ] * start;
        // Y [i+1, ] = X [i+1, ] + C [i+1, ] * Y [i, ]
        addCNConstant(0, start);
        for (int i = 1; i < numRetRows; i++) {
            addC(i, true);
        }
    } else {
        // Y [m, ] = X [m, ] + C [m, ] * start;
        // Y [i-1, ] = X [i-1, ] + C [i-1, ] * Y [i, ]
        addCNConstant(numRetRows - 1, start);
        for (int i = numRetRows - 2; i >= 0; i--) {
            addC(i, false);
        }
    }
    ((Matrix) getFunctionInput(1)).getMatrixObject().release();
    ((Matrix) getFunctionInput(0)).getMatrixObject().release();
    retMB.recomputeNonZeros();
    try {
        retMB.examSparsity();
        ret.setMatrixDoubleArray(retMB, OutputInfo.BinaryBlockOutputInfo, InputInfo.BinaryBlockInputInfo);
    } catch (DMLRuntimeException e) {
        throw new RuntimeException("Error while executing CumSumProd", e);
    } catch (IOException e) {
        throw new RuntimeException("Error while executing CumSumProd", e);
    }
}
Also used : Matrix(org.apache.sysml.udf.Matrix) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException) IJV(org.apache.sysml.runtime.matrix.data.IJV) IOException(java.io.IOException) Scalar(org.apache.sysml.udf.Scalar) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException)

Example 5 with Scalar

use of org.apache.sysml.udf.Scalar in project incubator-systemml by apache.

the class DynamicReadMatrixCP method execute.

@Override
public void execute() {
    try {
        String fname = ((Scalar) this.getFunctionInput(0)).getValue();
        Integer m = Integer.parseInt(((Scalar) this.getFunctionInput(1)).getValue());
        Integer n = Integer.parseInt(((Scalar) this.getFunctionInput(2)).getValue());
        String format = ((Scalar) this.getFunctionInput(3)).getValue();
        InputInfo ii = InputInfo.stringToInputInfo(format);
        OutputInfo oi = OutputInfo.BinaryBlockOutputInfo;
        MatrixBlock mbTmp = DataConverter.readMatrixFromHDFS(fname, ii, m, n, ConfigurationManager.getBlocksize(), ConfigurationManager.getBlocksize());
        String fnameTmp = createOutputFilePathAndName("TMP");
        _ret = new Matrix(fnameTmp, m, n, ValueType.Double);
        _ret.setMatrixDoubleArray(mbTmp, oi, ii);
    // NOTE: The packagesupport wrapper creates a new MatrixObjectNew with the given
    // matrix block. This leads to a dirty state of the new object. Hence, the resulting
    // intermediate plan variable will be exported in front of MR jobs and during this export
    // the format will be changed to binary block (the contract of external functions),
    // no matter in which format the original matrix was.
    } catch (Exception e) {
        throw new RuntimeException("Error executing dynamic read of matrix", e);
    }
}
Also used : OutputInfo(org.apache.sysml.runtime.matrix.data.OutputInfo) MatrixBlock(org.apache.sysml.runtime.matrix.data.MatrixBlock) InputInfo(org.apache.sysml.runtime.matrix.data.InputInfo) Matrix(org.apache.sysml.udf.Matrix) Scalar(org.apache.sysml.udf.Scalar)

Aggregations

Scalar (org.apache.sysml.udf.Scalar)19 Matrix (org.apache.sysml.udf.Matrix)14 MatrixBlock (org.apache.sysml.runtime.matrix.data.MatrixBlock)8 MatrixObject (org.apache.sysml.runtime.controlprogram.caching.MatrixObject)6 OutputInfo (org.apache.sysml.runtime.matrix.data.OutputInfo)6 DMLRuntimeException (org.apache.sysml.runtime.DMLRuntimeException)5 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 IJV (org.apache.sysml.runtime.matrix.data.IJV)4 InputInfo (org.apache.sysml.runtime.matrix.data.InputInfo)4 MatrixCharacteristics (org.apache.sysml.runtime.matrix.MatrixCharacteristics)3 StringTokenizer (java.util.StringTokenizer)2 ExecutionContext (org.apache.sysml.runtime.controlprogram.context.ExecutionContext)2 BooleanObject (org.apache.sysml.runtime.instructions.cp.BooleanObject)2 CPOperand (org.apache.sysml.runtime.instructions.cp.CPOperand)2 DoubleObject (org.apache.sysml.runtime.instructions.cp.DoubleObject)2 FunctionCallCPInstruction (org.apache.sysml.runtime.instructions.cp.FunctionCallCPInstruction)2 IntObject (org.apache.sysml.runtime.instructions.cp.IntObject)2 ScalarObject (org.apache.sysml.runtime.instructions.cp.ScalarObject)2 StringObject (org.apache.sysml.runtime.instructions.cp.StringObject)2