Search in sources :

Example 6 with DataGenOp

use of org.apache.sysml.hops.DataGenOp in project incubator-systemml by apache.

the class HopRewriteUtils method createSeqDataGenOp.

public static DataGenOp createSeqDataGenOp(Hop input, boolean asc) {
    Hop to = input.rowsKnown() ? new LiteralOp(input.getDim1()) : new UnaryOp("tmprows", DataType.SCALAR, ValueType.INT, OpOp1.NROW, input);
    HashMap<String, Hop> params = new HashMap<>();
    if (asc) {
        params.put(Statement.SEQ_FROM, new LiteralOp(1));
        params.put(Statement.SEQ_TO, to);
        params.put(Statement.SEQ_INCR, new LiteralOp(1));
    } else {
        params.put(Statement.SEQ_FROM, to);
        params.put(Statement.SEQ_TO, new LiteralOp(1));
        params.put(Statement.SEQ_INCR, new LiteralOp(-1));
    }
    // note internal refresh size information
    DataGenOp datagen = new DataGenOp(DataGenMethod.SEQ, new DataIdentifier("tmp"), params);
    datagen.setOutputBlocksizes(input.getRowsInBlock(), input.getColsInBlock());
    copyLineNumbers(input, datagen);
    return datagen;
}
Also used : AggUnaryOp(org.apache.sysml.hops.AggUnaryOp) UnaryOp(org.apache.sysml.hops.UnaryOp) DataIdentifier(org.apache.sysml.parser.DataIdentifier) HashMap(java.util.HashMap) DataGenOp(org.apache.sysml.hops.DataGenOp) Hop(org.apache.sysml.hops.Hop) LiteralOp(org.apache.sysml.hops.LiteralOp)

Example 7 with DataGenOp

use of org.apache.sysml.hops.DataGenOp in project incubator-systemml by apache.

the class RewriteAlgebraicSimplificationDynamic method simplifyIdentityRepMatrixMult.

private static Hop simplifyIdentityRepMatrixMult(Hop parent, Hop hi, int pos) {
    if (// X%*%Y -> X, if y is matrix(1,1,1)
    HopRewriteUtils.isMatrixMultiply(hi)) {
        Hop left = hi.getInput().get(0);
        Hop right = hi.getInput().get(1);
        // X %*% y -> X
        if (// scalar right
        HopRewriteUtils.isDimsKnown(right) && right.getDim1() == 1 && right.getDim2() == 1 && right instanceof DataGenOp && ((DataGenOp) right).getOp() == DataGenMethod.RAND && // matrix(1,)
        ((DataGenOp) right).hasConstantValue(1.0)) {
            HopRewriteUtils.replaceChildReference(parent, hi, left, pos);
            hi = left;
            LOG.debug("Applied simplifyIdentiyMatrixMult");
        }
    }
    return hi;
}
Also used : DataGenOp(org.apache.sysml.hops.DataGenOp) Hop(org.apache.sysml.hops.Hop)

Example 8 with DataGenOp

use of org.apache.sysml.hops.DataGenOp in project incubator-systemml by apache.

the class RewriteAlgebraicSimplificationStatic method removeUnnecessaryVectorizeOperation.

private static Hop removeUnnecessaryVectorizeOperation(Hop hi) {
    // applies to all binary matrix operations, if one input is unnecessarily vectorized
    if (hi instanceof BinaryOp && hi.getDataType() == DataType.MATRIX && ((BinaryOp) hi).supportsMatrixScalarOperations()) {
        BinaryOp bop = (BinaryOp) hi;
        Hop left = bop.getInput().get(0);
        Hop right = bop.getInput().get(1);
        if (// no outer
        !(left.getDim1() > 1 && left.getDim2() == 1 && right.getDim1() == 1 && right.getDim2() > 1)) {
            // check and remove right vectorized scalar
            if (left.getDataType() == DataType.MATRIX && right instanceof DataGenOp) {
                DataGenOp dright = (DataGenOp) right;
                if (dright.getOp() == DataGenMethod.RAND && dright.hasConstantValue()) {
                    Hop drightIn = dright.getInput().get(dright.getParamIndex(DataExpression.RAND_MIN));
                    HopRewriteUtils.replaceChildReference(bop, dright, drightIn, 1);
                    HopRewriteUtils.cleanupUnreferenced(dright);
                    LOG.debug("Applied removeUnnecessaryVectorizeOperation1");
                }
            } else // check and remove left vectorized scalar
            if (right.getDataType() == DataType.MATRIX && left instanceof DataGenOp) {
                DataGenOp dleft = (DataGenOp) left;
                if (dleft.getOp() == DataGenMethod.RAND && dleft.hasConstantValue() && (left.getDim2() == 1 || right.getDim2() > 1) && (left.getDim1() == 1 || right.getDim1() > 1)) {
                    Hop dleftIn = dleft.getInput().get(dleft.getParamIndex(DataExpression.RAND_MIN));
                    HopRewriteUtils.replaceChildReference(bop, dleft, dleftIn, 0);
                    HopRewriteUtils.cleanupUnreferenced(dleft);
                    LOG.debug("Applied removeUnnecessaryVectorizeOperation2");
                }
            }
        // Note: we applied this rewrite to at most one side in order to keep the
        // output semantically equivalent. However, future extensions might consider
        // to remove vectors from both side, compute the binary op on scalars and
        // finally feed it into a datagenop of the original dimensions.
        }
    }
    return hi;
}
Also used : DataGenOp(org.apache.sysml.hops.DataGenOp) Hop(org.apache.sysml.hops.Hop) AggBinaryOp(org.apache.sysml.hops.AggBinaryOp) BinaryOp(org.apache.sysml.hops.BinaryOp)

Example 9 with DataGenOp

use of org.apache.sysml.hops.DataGenOp in project incubator-systemml by apache.

the class RewriteAlgebraicSimplificationStatic method fuseDatagenAndMinusOperation.

private static Hop fuseDatagenAndMinusOperation(Hop hi) {
    if (hi instanceof BinaryOp) {
        BinaryOp bop = (BinaryOp) hi;
        Hop left = bop.getInput().get(0);
        Hop right = bop.getInput().get(1);
        if (right instanceof DataGenOp && ((DataGenOp) right).getOp() == DataGenMethod.RAND && left instanceof LiteralOp && ((LiteralOp) left).getDoubleValue() == 0.0) {
            DataGenOp inputGen = (DataGenOp) right;
            HashMap<String, Integer> params = inputGen.getParamIndexMap();
            Hop pdf = right.getInput().get(params.get(DataExpression.RAND_PDF));
            int ixMin = params.get(DataExpression.RAND_MIN);
            int ixMax = params.get(DataExpression.RAND_MAX);
            Hop min = right.getInput().get(ixMin);
            Hop max = right.getInput().get(ixMax);
            // apply rewrite under additional conditions (for simplicity)
            if (inputGen.getParent().size() == 1 && min instanceof LiteralOp && max instanceof LiteralOp && pdf instanceof LiteralOp && DataExpression.RAND_PDF_UNIFORM.equals(((LiteralOp) pdf).getStringValue())) {
                // exchange and *-1 (special case 0 stays 0 instead of -0 for consistency)
                double newMinVal = (((LiteralOp) max).getDoubleValue() == 0) ? 0 : (-1 * ((LiteralOp) max).getDoubleValue());
                double newMaxVal = (((LiteralOp) min).getDoubleValue() == 0) ? 0 : (-1 * ((LiteralOp) min).getDoubleValue());
                Hop newMin = new LiteralOp(newMinVal);
                Hop newMax = new LiteralOp(newMaxVal);
                HopRewriteUtils.removeChildReferenceByPos(inputGen, min, ixMin);
                HopRewriteUtils.addChildReference(inputGen, newMin, ixMin);
                HopRewriteUtils.removeChildReferenceByPos(inputGen, max, ixMax);
                HopRewriteUtils.addChildReference(inputGen, newMax, ixMax);
                // rewire all parents (avoid anomalies with replicated datagen)
                List<Hop> parents = new ArrayList<>(bop.getParent());
                for (Hop p : parents) HopRewriteUtils.replaceChildReference(p, bop, inputGen);
                hi = inputGen;
                LOG.debug("Applied fuseDatagenAndMinusOperation (line " + bop.getBeginLine() + ").");
            }
        }
    }
    return hi;
}
Also used : DataGenOp(org.apache.sysml.hops.DataGenOp) Hop(org.apache.sysml.hops.Hop) ArrayList(java.util.ArrayList) LiteralOp(org.apache.sysml.hops.LiteralOp) AggBinaryOp(org.apache.sysml.hops.AggBinaryOp) BinaryOp(org.apache.sysml.hops.BinaryOp)

Example 10 with DataGenOp

use of org.apache.sysml.hops.DataGenOp in project incubator-systemml by apache.

the class DMLTranslator method processDataExpression.

/**
 * Construct Hops from parse tree : Process ParameterizedExpression in a
 * read/write/rand statement
 *
 * @param source data expression
 * @param target data identifier
 * @param hops map of high-level operators
 * @return high-level operator
 */
private Hop processDataExpression(DataExpression source, DataIdentifier target, HashMap<String, Hop> hops) {
    // this expression has multiple "named" parameters
    HashMap<String, Hop> paramHops = new HashMap<>();
    // -- construct hops for all input parameters
    // -- store them in hashmap so that their "name"s are maintained
    Hop pHop = null;
    for (String paramName : source.getVarParams().keySet()) {
        pHop = processExpression(source.getVarParam(paramName), null, hops);
        paramHops.put(paramName, pHop);
    }
    Hop currBuiltinOp = null;
    if (target == null) {
        target = createTarget(source);
    }
    // construct hop based on opcode
    switch(source.getOpCode()) {
        case READ:
            currBuiltinOp = new DataOp(target.getName(), target.getDataType(), target.getValueType(), DataOpTypes.PERSISTENTREAD, paramHops);
            ((DataOp) currBuiltinOp).setFileName(((StringIdentifier) source.getVarParam(DataExpression.IO_FILENAME)).getValue());
            break;
        case WRITE:
            currBuiltinOp = new DataOp(target.getName(), target.getDataType(), target.getValueType(), DataOpTypes.PERSISTENTWRITE, hops.get(target.getName()), paramHops);
            break;
        case RAND:
            // We limit RAND_MIN, RAND_MAX, RAND_SPARSITY, RAND_SEED, and RAND_PDF to be constants
            DataGenMethod method = (paramHops.get(DataExpression.RAND_MIN).getValueType() == ValueType.STRING) ? DataGenMethod.SINIT : DataGenMethod.RAND;
            currBuiltinOp = new DataGenOp(method, target, paramHops);
            break;
        case MATRIX:
            ArrayList<Hop> tmp = new ArrayList<>();
            tmp.add(0, paramHops.get(DataExpression.RAND_DATA));
            tmp.add(1, paramHops.get(DataExpression.RAND_ROWS));
            tmp.add(2, paramHops.get(DataExpression.RAND_COLS));
            tmp.add(3, paramHops.get(DataExpression.RAND_BY_ROW));
            currBuiltinOp = new ReorgOp(target.getName(), target.getDataType(), target.getValueType(), ReOrgOp.RESHAPE, tmp);
            break;
        default:
            LOG.error(source.printErrorLocation() + "processDataExpression():: Unknown operation:  " + source.getOpCode());
            throw new ParseException(source.printErrorLocation() + "processDataExpression():: Unknown operation:  " + source.getOpCode());
    }
    // set identifier meta data (incl dimensions and blocksizes)
    setIdentifierParams(currBuiltinOp, source.getOutput());
    if (source.getOpCode() == DataExpression.DataOp.READ)
        ((DataOp) currBuiltinOp).setInputBlockSizes(target.getRowsInBlock(), target.getColumnsInBlock());
    currBuiltinOp.setParseInfo(source);
    return currBuiltinOp;
}
Also used : HashMap(java.util.HashMap) DataGenOp(org.apache.sysml.hops.DataGenOp) Hop(org.apache.sysml.hops.Hop) ArrayList(java.util.ArrayList) ReorgOp(org.apache.sysml.hops.ReorgOp) DataGenMethod(org.apache.sysml.hops.Hop.DataGenMethod) DataOp(org.apache.sysml.hops.DataOp)

Aggregations

DataGenOp (org.apache.sysml.hops.DataGenOp)21 Hop (org.apache.sysml.hops.Hop)21 LiteralOp (org.apache.sysml.hops.LiteralOp)13 HashMap (java.util.HashMap)9 ArrayList (java.util.ArrayList)6 DataIdentifier (org.apache.sysml.parser.DataIdentifier)6 ReorgOp (org.apache.sysml.hops.ReorgOp)5 AggBinaryOp (org.apache.sysml.hops.AggBinaryOp)4 AggUnaryOp (org.apache.sysml.hops.AggUnaryOp)4 BinaryOp (org.apache.sysml.hops.BinaryOp)4 UnaryOp (org.apache.sysml.hops.UnaryOp)4 DataOp (org.apache.sysml.hops.DataOp)2 List (java.util.List)1 ConvolutionOp (org.apache.sysml.hops.ConvolutionOp)1 DataGenMethod (org.apache.sysml.hops.Hop.DataGenMethod)1 MultiThreadedHop (org.apache.sysml.hops.Hop.MultiThreadedHop)1 OpOp2 (org.apache.sysml.hops.Hop.OpOp2)1 OpOpN (org.apache.sysml.hops.Hop.OpOpN)1 HopsException (org.apache.sysml.hops.HopsException)1 IndexingOp (org.apache.sysml.hops.IndexingOp)1