Search in sources :

Example 41 with MatrixObject

use of org.apache.sysml.runtime.controlprogram.caching.MatrixObject in project incubator-systemml by apache.

the class VariableCPInstruction method writeMMFile.

/**
 * Helper function to write MM files to HDFS.
 *
 * @param ec execution context
 * @param fname file name
 */
private void writeMMFile(ExecutionContext ec, String fname) {
    MatrixObject mo = ec.getMatrixObject(getInput1().getName());
    String outFmt = "matrixmarket";
    if (mo.isDirty()) {
        // there exist data computed in CP that is not backed up on HDFS
        // i.e., it is either in-memory or in evicted space
        mo.exportData(fname, outFmt);
    } else {
        OutputInfo oi = ((MetaDataFormat) mo.getMetaData()).getOutputInfo();
        MatrixCharacteristics mc = mo.getMatrixCharacteristics();
        if (oi == OutputInfo.TextCellOutputInfo) {
            try {
                WriterMatrixMarket.mergeTextcellToMatrixMarket(mo.getFileName(), fname, mc.getRows(), mc.getCols(), mc.getNonZeros());
            } catch (IOException e) {
                throw new DMLRuntimeException(e);
            }
        } else if (oi == OutputInfo.BinaryBlockOutputInfo) {
            mo.exportData(fname, outFmt);
        } else {
            throw new DMLRuntimeException("Unexpected data format (" + OutputInfo.outputInfoToString(oi) + "): can not export into MatrixMarket format.");
        }
    }
}
Also used : OutputInfo(org.apache.sysml.runtime.matrix.data.OutputInfo) MetaDataFormat(org.apache.sysml.runtime.matrix.MetaDataFormat) MatrixObject(org.apache.sysml.runtime.controlprogram.caching.MatrixObject) IOException(java.io.IOException) MatrixCharacteristics(org.apache.sysml.runtime.matrix.MatrixCharacteristics) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException)

Example 42 with MatrixObject

use of org.apache.sysml.runtime.controlprogram.caching.MatrixObject in project incubator-systemml by apache.

the class MatrixIndexingCPFileInstruction method processInstruction.

@Override
public void processInstruction(ExecutionContext ec) {
    String opcode = getOpcode();
    IndexRange ixrange = getIndexRange(ec).add(1);
    MatrixObject mo = ec.getMatrixObject(input1.getName());
    if (mo.isPartitioned() && opcode.equalsIgnoreCase(RightIndex.OPCODE)) {
        MetaDataFormat meta = (MetaDataFormat) mo.getMetaData();
        MatrixCharacteristics mc = meta.getMatrixCharacteristics();
        String pfname = mo.getPartitionFileName(ixrange, mc.getRowsPerBlock(), mc.getColsPerBlock());
        if (MapReduceTool.existsFileOnHDFS(pfname)) {
            // create output matrix object
            MatrixObject mobj = new MatrixObject(mo.getValueType(), pfname);
            MatrixCharacteristics mcNew = null;
            switch(mo.getPartitionFormat()) {
                case ROW_WISE:
                    mcNew = new MatrixCharacteristics(1, mc.getCols(), mc.getRowsPerBlock(), mc.getColsPerBlock());
                    break;
                case ROW_BLOCK_WISE_N:
                    mcNew = new MatrixCharacteristics(mo.getPartitionSize(), mc.getCols(), mc.getRowsPerBlock(), mc.getColsPerBlock());
                    break;
                case COLUMN_WISE:
                    mcNew = new MatrixCharacteristics(mc.getRows(), 1, mc.getRowsPerBlock(), mc.getColsPerBlock());
                    break;
                case COLUMN_BLOCK_WISE_N:
                    mcNew = new MatrixCharacteristics(mc.getRows(), mo.getPartitionSize(), mc.getRowsPerBlock(), mc.getColsPerBlock());
                    break;
                default:
                    throw new DMLRuntimeException("Unsupported partition format for CP_FILE " + RightIndex.OPCODE + ": " + mo.getPartitionFormat());
            }
            MetaDataFormat metaNew = new MetaDataFormat(mcNew, meta.getOutputInfo(), meta.getInputInfo());
            mobj.setMetaData(metaNew);
            // put output object into symbol table
            ec.setVariable(output.getName(), mobj);
        } else {
            // will return an empty matrix partition
            MatrixBlock resultBlock = mo.readMatrixPartition(ixrange);
            ec.setMatrixOutput(output.getName(), resultBlock, getExtendedOpcode());
        }
    } else {
        throw new DMLRuntimeException("Invalid opcode or index predicate for MatrixIndexingCPFileInstruction: " + instString);
    }
}
Also used : IndexRange(org.apache.sysml.runtime.util.IndexRange) MetaDataFormat(org.apache.sysml.runtime.matrix.MetaDataFormat) MatrixBlock(org.apache.sysml.runtime.matrix.data.MatrixBlock) MatrixObject(org.apache.sysml.runtime.controlprogram.caching.MatrixObject) MatrixCharacteristics(org.apache.sysml.runtime.matrix.MatrixCharacteristics) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException)

Example 43 with MatrixObject

use of org.apache.sysml.runtime.controlprogram.caching.MatrixObject in project incubator-systemml by apache.

the class ParameterizedBuiltinCPFileInstruction method processInstruction.

@Override
public void processInstruction(ExecutionContext ec) {
    String opcode = getOpcode();
    if (opcode.equalsIgnoreCase("rmempty")) {
        // get inputs
        MatrixObject src = ec.getMatrixObject(params.get("target"));
        MatrixObject out = ec.getMatrixObject(output.getName());
        String margin = params.get("margin");
        // export input matrix (if necessary)
        src.exportData();
        // core execution
        RemoveEmpty rm = new RemoveEmpty(margin, src, out);
        out = rm.execute();
        // put output
        ec.setVariable(output.getName(), out);
    } else {
        throw new DMLRuntimeException("Unknown opcode : " + opcode);
    }
}
Also used : MatrixObject(org.apache.sysml.runtime.controlprogram.caching.MatrixObject) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException)

Example 44 with MatrixObject

use of org.apache.sysml.runtime.controlprogram.caching.MatrixObject in project incubator-systemml by apache.

the class ConvolutionGPUInstruction method processChannelSumsInstruction.

public void processChannelSumsInstruction(ExecutionContext ec) {
    GPUStatistics.incrementNoOfExecutedGPUInst();
    MatrixObject input = getMatrixInputForGPUInstruction(ec, _input1.getName());
    int C = (int) ec.getScalarInput(_input2.getName(), _input2.getValueType(), _input2.isLiteral()).getLongValue();
    int HW = (int) ec.getScalarInput(_input3.getName(), _input3.getValueType(), _input3.isLiteral()).getLongValue();
    if (C * HW != input.getNumColumns()) {
        throw new DMLRuntimeException("Expected rows*cols" + C + "*" + HW + " to be equal to number of columns of input " + input.getNumColumns());
    }
    MatrixObject outputBlock = getDenseMatrixOutputForGPUInstruction(ec, _output.getName(), C, 1);
    LibMatrixCUDA.channelSums(ec.getGPUContext(0), getExtendedOpcode(), input, outputBlock, C, HW);
    // release inputs/outputs
    ec.releaseMatrixInputForGPUInstruction(_input1.getName());
    ec.releaseMatrixOutputForGPUInstruction(_output.getName());
}
Also used : MatrixObject(org.apache.sysml.runtime.controlprogram.caching.MatrixObject) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException)

Example 45 with MatrixObject

use of org.apache.sysml.runtime.controlprogram.caching.MatrixObject in project incubator-systemml by apache.

the class ConvolutionGPUInstruction method processInstruction.

@Override
public void processInstruction(ExecutionContext ec) {
    if (instOpcode.equalsIgnoreCase("bias_add") || instOpcode.equalsIgnoreCase("bias_multiply")) {
        processBiasInstruction(instOpcode, ec);
        return;
    } else if (instOpcode.equalsIgnoreCase("relu_backward")) {
        processReLUBackwardInstruction(ec);
        return;
    } else if (instOpcode.equalsIgnoreCase("channel_sums")) {
        processChannelSumsInstruction(ec);
        return;
    }
    GPUStatistics.incrementNoOfExecutedGPUInst();
    int pad_h = getScalarInput(ec, _padding, 0);
    int pad_w = getScalarInput(ec, _padding, 1);
    int stride_h = getScalarInput(ec, _stride, 0);
    int stride_w = getScalarInput(ec, _stride, 1);
    int N = getScalarInput(ec, _input_shape, 0);
    int C = getScalarInput(ec, _input_shape, 1);
    int H = getScalarInput(ec, _input_shape, 2);
    int W = getScalarInput(ec, _input_shape, 3);
    int K = getScalarInput(ec, _filter_shape, 0);
    int R = getScalarInput(ec, _filter_shape, 2);
    int S = getScalarInput(ec, _filter_shape, 3);
    int P = (int) ConvolutionUtils.getP(H, R, stride_h, pad_h);
    int Q = (int) ConvolutionUtils.getQ(W, S, stride_w, pad_w);
    if (instOpcode.equalsIgnoreCase("conv2d")) {
        MatrixObject image = getMatrixInputForGPUInstruction(ec, _input1.getName());
        MatrixObject filter = getMatrixInputForGPUInstruction(ec, _input2.getName());
        if (image.getNumRows() != N || image.getNumColumns() != C * H * W)
            throw new DMLRuntimeException("Incorrect dimensions for image in conv2d");
        if (filter.getNumRows() != K || filter.getNumColumns() != C * R * S)
            throw new DMLRuntimeException("Incorrect dimensions for filter in conv2d");
        MatrixObject out = getDenseMatrixOutputForGPUInstruction(ec, _output.getName(), N, K * P * Q);
        LibMatrixCuDNN.conv2d(ec.getGPUContext(0), getExtendedOpcode(), image, filter, out, N, C, H, W, K, R, S, pad_h, pad_w, stride_h, stride_w, P, Q, _intermediateMemoryBudget);
    } else if (instOpcode.equalsIgnoreCase("conv2d_bias_add")) {
        MatrixObject image = getMatrixInputForGPUInstruction(ec, _input1.getName());
        MatrixObject bias = getMatrixInputForGPUInstruction(ec, _input2.getName());
        MatrixObject filter = getMatrixInputForGPUInstruction(ec, _input3.getName());
        if (image.getNumRows() != N || image.getNumColumns() != C * H * W)
            throw new DMLRuntimeException("Incorrect dimensions for image in conv2d");
        if (filter.getNumRows() != K || filter.getNumColumns() != C * R * S)
            throw new DMLRuntimeException("Incorrect dimensions for filter in conv2d");
        MatrixObject out = getDenseMatrixOutputForGPUInstruction(ec, _output.getName(), N, K * P * Q);
        LibMatrixCuDNN.conv2dBiasAdd(ec.getGPUContext(0), getExtendedOpcode(), image, bias, filter, out, N, C, H, W, K, R, S, pad_h, pad_w, stride_h, stride_w, P, Q, _intermediateMemoryBudget);
    } else if (instOpcode.equalsIgnoreCase("conv2d_backward_filter")) {
        MatrixObject image = getMatrixInputForGPUInstruction(ec, _input1.getName());
        MatrixObject dout = getMatrixInputForGPUInstruction(ec, _input2.getName());
        if (image.getNumRows() != N || image.getNumColumns() != C * H * W)
            throw new DMLRuntimeException("Incorrect dimensions for image in conv2d_backward_filter");
        if (dout.getNumRows() != N || dout.getNumColumns() != K * P * Q)
            throw new DMLRuntimeException("Incorrect dimensions for dout in conv2d_backward_filter: " + dout.getNumRows() + " != " + N + " || " + dout.getNumColumns() + " != " + K * P * Q);
        MatrixObject out = getDenseMatrixOutputForGPUInstruction(ec, _output.getName(), K, C * R * S);
        LibMatrixCuDNN.conv2dBackwardFilter(ec.getGPUContext(0), getExtendedOpcode(), image, dout, out, N, C, H, W, K, R, S, pad_h, pad_w, stride_h, stride_w, P, Q, _intermediateMemoryBudget);
    // TODO: For now always copy the device data to host
    // ec.gpuCtx.copyDeviceToHost(outputBlock);
    } else if (instOpcode.equalsIgnoreCase("conv2d_backward_data")) {
        MatrixObject filter = getMatrixInputForGPUInstruction(ec, _input1.getName());
        MatrixObject dout = getMatrixInputForGPUInstruction(ec, _input2.getName());
        if (filter.getNumRows() != K || filter.getNumColumns() != C * R * S)
            throw new DMLRuntimeException("Incorrect dimensions for filter in convolution_backward_data");
        if (dout.getNumRows() != N || dout.getNumColumns() != K * P * Q)
            throw new DMLRuntimeException("Incorrect dimensions for dout in conv2d_backward_data: " + dout.getNumRows() + " != " + N + " || " + dout.getNumColumns() + " != " + K * P * Q);
        MatrixObject out = getDenseMatrixOutputForGPUInstruction(ec, _output.getName(), N, C * H * W);
        LibMatrixCuDNN.conv2dBackwardData(ec.getGPUContext(0), getExtendedOpcode(), filter, dout, out, N, C, H, W, K, R, S, pad_h, pad_w, stride_h, stride_w, P, Q, _intermediateMemoryBudget);
    } else if (instOpcode.equalsIgnoreCase("maxpooling") || instOpcode.equalsIgnoreCase("avgpooling")) {
        MatrixObject image = getMatrixInputForGPUInstruction(ec, _input1.getName());
        if (image.getNumRows() != N || image.getNumColumns() != C * H * W)
            throw new DMLRuntimeException("Incorrect dimensions for image in maxpooling: " + image.getNumRows() + " != " + N + " || " + image.getNumColumns() + " != " + C * H * W);
        MatrixObject out = getDenseMatrixOutputForGPUInstruction(ec, _output.getName(), N, C * P * Q);
        PoolingType poolType = instOpcode.equalsIgnoreCase("maxpooling") ? PoolingType.MAX : PoolingType.AVG;
        LibMatrixCuDNN.pooling(ec.getGPUContext(0), getExtendedOpcode(), image, out, N, C, H, W, K, R, S, pad_h, pad_w, stride_h, stride_w, P, Q, poolType, _intermediateMemoryBudget);
    } else if (instOpcode.equalsIgnoreCase("maxpooling_backward") || instOpcode.equalsIgnoreCase("avgpooling_backward")) {
        MatrixObject image = getMatrixInputForGPUInstruction(ec, _input1.getName());
        MatrixObject dout = getMatrixInputForGPUInstruction(ec, _input2.getName());
        MatrixObject maxPoolOutput = _input3 != null ? getMatrixInputForGPUInstruction(ec, _input3.getName()) : null;
        if (dout.getNumRows() != N || dout.getNumColumns() != C * P * Q)
            throw new DMLRuntimeException("Incorrect dimensions for dout in maxpooling_backward");
        if (image.getNumRows() != N || image.getNumColumns() != C * H * W)
            throw new DMLRuntimeException("Incorrect dimensions for image in maxpooling_backward: " + image.getNumRows() + " != " + N + " || " + image.getNumColumns() + " != " + K * P * Q);
        MatrixObject out = getDenseMatrixOutputForGPUInstruction(ec, _output.getName(), N, C * H * W);
        PoolingType poolType = instOpcode.equalsIgnoreCase("maxpooling_backward") ? PoolingType.MAX : PoolingType.AVG;
        LibMatrixCuDNN.poolingBackward(ec.getGPUContext(0), getExtendedOpcode(), image, dout, maxPoolOutput, out, N, C, H, W, K, R, S, pad_h, pad_w, stride_h, stride_w, P, Q, poolType, _intermediateMemoryBudget);
    } else {
        throw new DMLRuntimeException("Unsupported GPU context for " + instOpcode);
    }
    // release inputs/outputs
    ec.releaseMatrixInputForGPUInstruction(_input1.getName());
    boolean isPool = instOpcode.equalsIgnoreCase("maxpooling") || instOpcode.equalsIgnoreCase("avgpooling");
    boolean isPoolBackward = instOpcode.equalsIgnoreCase("maxpooling_backward") || instOpcode.equalsIgnoreCase("avgpooling_backward");
    if (!isPool)
        ec.releaseMatrixInputForGPUInstruction(_input2.getName());
    if (instOpcode.equalsIgnoreCase("conv2d_bias_add") || (isPoolBackward && _input3 != null))
        ec.releaseMatrixInputForGPUInstruction(_input3.getName());
    ec.releaseMatrixOutputForGPUInstruction(_output.getName());
}
Also used : PoolingType(org.apache.sysml.runtime.matrix.data.LibMatrixDNN.PoolingType) MatrixObject(org.apache.sysml.runtime.controlprogram.caching.MatrixObject) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException)

Aggregations

MatrixObject (org.apache.sysml.runtime.controlprogram.caching.MatrixObject)201 DMLRuntimeException (org.apache.sysml.runtime.DMLRuntimeException)74 MatrixCharacteristics (org.apache.sysml.runtime.matrix.MatrixCharacteristics)45 MatrixBlock (org.apache.sysml.runtime.matrix.data.MatrixBlock)39 Data (org.apache.sysml.runtime.instructions.cp.Data)37 MetaDataFormat (org.apache.sysml.runtime.matrix.MetaDataFormat)26 Pointer (jcuda.Pointer)20 CSRPointer (org.apache.sysml.runtime.instructions.gpu.context.CSRPointer)20 IOException (java.io.IOException)17 ArrayList (java.util.ArrayList)16 ScalarObject (org.apache.sysml.runtime.instructions.cp.ScalarObject)14 OutputInfo (org.apache.sysml.runtime.matrix.data.OutputInfo)13 CacheableData (org.apache.sysml.runtime.controlprogram.caching.CacheableData)12 RDDObject (org.apache.sysml.runtime.instructions.spark.data.RDDObject)12 Hop (org.apache.sysml.hops.Hop)11 MatrixFormatMetaData (org.apache.sysml.runtime.matrix.MatrixFormatMetaData)11 ParForProgramBlock (org.apache.sysml.runtime.controlprogram.ParForProgramBlock)10 MatrixIndexes (org.apache.sysml.runtime.matrix.data.MatrixIndexes)10 Path (org.apache.hadoop.fs.Path)9 LongWritable (org.apache.hadoop.io.LongWritable)9