Search in sources :

Example 16 with DataGenOp

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

the class HopRewriteUtils method isBasicN1Sequence.

public static boolean isBasicN1Sequence(Hop hop) {
    boolean ret = false;
    if (hop instanceof DataGenOp) {
        DataGenOp dgop = (DataGenOp) hop;
        if (dgop.getOp() == DataGenMethod.SEQ) {
            Hop to = dgop.getInput().get(dgop.getParamIndex(Statement.SEQ_TO));
            Hop incr = dgop.getInput().get(dgop.getParamIndex(Statement.SEQ_INCR));
            ret = (to instanceof LiteralOp && getDoubleValueSafe((LiteralOp) to) == 1) && (incr instanceof LiteralOp && getDoubleValueSafe((LiteralOp) incr) == -1);
        }
    }
    return ret;
}
Also used : DataGenOp(org.apache.sysml.hops.DataGenOp) Hop(org.apache.sysml.hops.Hop) LiteralOp(org.apache.sysml.hops.LiteralOp)

Example 17 with DataGenOp

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

the class RewriteAlgebraicSimplificationDynamic method fuseDatagenAndReorgOperation.

@SuppressWarnings("unchecked")
private static Hop fuseDatagenAndReorgOperation(Hop parent, Hop hi, int pos) {
    if (HopRewriteUtils.isTransposeOperation(hi) && // datagen
    hi.getInput().get(0) instanceof DataGenOp && // transpose only consumer
    hi.getInput().get(0).getParent().size() == 1) {
        DataGenOp dop = (DataGenOp) hi.getInput().get(0);
        if ((dop.getOp() == DataGenMethod.RAND || dop.getOp() == DataGenMethod.SINIT) && (dop.getDim1() == 1 || dop.getDim2() == 1)) {
            // relink all parents and dataop (remove transpose)
            HopRewriteUtils.removeAllChildReferences(hi);
            ArrayList<Hop> parents = (ArrayList<Hop>) hi.getParent().clone();
            for (int i = 0; i < parents.size(); i++) {
                Hop lparent = parents.get(i);
                int ppos = HopRewriteUtils.getChildReferencePos(lparent, hi);
                HopRewriteUtils.removeChildReferenceByPos(lparent, hi, ppos);
                HopRewriteUtils.addChildReference(lparent, dop, pos);
            }
            // flip rows/cols attributes in datagen
            HashMap<String, Integer> rparams = dop.getParamIndexMap();
            int pos1 = rparams.get(DataExpression.RAND_ROWS);
            int pos2 = rparams.get(DataExpression.RAND_COLS);
            rparams.put(DataExpression.RAND_ROWS, pos2);
            rparams.put(DataExpression.RAND_COLS, pos1);
            dop.refreshSizeInformation();
            hi = dop;
            LOG.debug("Applied fuseDatagenReorgOperation.");
        }
    }
    return hi;
}
Also used : DataGenOp(org.apache.sysml.hops.DataGenOp) Hop(org.apache.sysml.hops.Hop) ArrayList(java.util.ArrayList)

Example 18 with DataGenOp

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

the class RewriteAlgebraicSimplificationStatic method simplifyCTableWithConstMatrixInputs.

private static Hop simplifyCTableWithConstMatrixInputs(Hop hi) {
    // pattern: table(X, matrix(1,...), matrix(7, ...)) -> table(X, 1, 7)
    if (HopRewriteUtils.isTernary(hi, OpOp3.CTABLE)) {
        // note: the first input always expected to be a matrix
        for (int i = 1; i < hi.getInput().size(); i++) {
            Hop inCurr = hi.getInput().get(i);
            if (HopRewriteUtils.isDataGenOpWithConstantValue(inCurr)) {
                Hop inNew = ((DataGenOp) inCurr).getInput(DataExpression.RAND_MIN);
                HopRewriteUtils.replaceChildReference(hi, inCurr, inNew, i);
                LOG.debug("Applied simplifyCTableWithConstMatrixInputs" + i + " (line " + hi.getBeginLine() + ").");
            }
        }
    }
    return hi;
}
Also used : DataGenOp(org.apache.sysml.hops.DataGenOp) Hop(org.apache.sysml.hops.Hop)

Example 19 with DataGenOp

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

the class RewriteAlgebraicSimplificationStatic method fuseDatagenAndBinaryOperation.

/**
 * Handle removal of unnecessary binary operations over rand data
 *
 * rand*7 -> rand(min*7,max*7); rand+7 -> rand(min+7,max+7); rand-7 -> rand(min+(-7),max+(-7))
 * 7*rand -> rand(min*7,max*7); 7+rand -> rand(min+7,max+7);
 *
 * @param hi high-order operation
 * @return high-level operator
 */
@SuppressWarnings("incomplete-switch")
private static Hop fuseDatagenAndBinaryOperation(Hop hi) {
    if (hi instanceof BinaryOp) {
        BinaryOp bop = (BinaryOp) hi;
        Hop left = bop.getInput().get(0);
        Hop right = bop.getInput().get(1);
        // left input rand and hence output matrix double, right scalar literal
        if (HopRewriteUtils.isDataGenOp(left, DataGenMethod.RAND) && right instanceof LiteralOp && left.getParent().size() == 1) {
            DataGenOp inputGen = (DataGenOp) left;
            Hop pdf = inputGen.getInput(DataExpression.RAND_PDF);
            Hop min = inputGen.getInput(DataExpression.RAND_MIN);
            Hop max = inputGen.getInput(DataExpression.RAND_MAX);
            double sval = ((LiteralOp) right).getDoubleValue();
            boolean pdfUniform = pdf instanceof LiteralOp && DataExpression.RAND_PDF_UNIFORM.equals(((LiteralOp) pdf).getStringValue());
            if (HopRewriteUtils.isBinary(bop, OpOp2.MULT, OpOp2.PLUS, OpOp2.MINUS, OpOp2.DIV) && min instanceof LiteralOp && max instanceof LiteralOp && pdfUniform) {
                // create fused data gen operator
                DataGenOp gen = null;
                switch(// fuse via scale and shift
                bop.getOp()) {
                    case MULT:
                        gen = HopRewriteUtils.copyDataGenOp(inputGen, sval, 0);
                        break;
                    case PLUS:
                    case MINUS:
                        gen = HopRewriteUtils.copyDataGenOp(inputGen, 1, sval * ((bop.getOp() == OpOp2.MINUS) ? -1 : 1));
                        break;
                    case DIV:
                        gen = HopRewriteUtils.copyDataGenOp(inputGen, 1 / sval, 0);
                        break;
                }
                // rewire all parents (avoid anomalies with replicated datagen)
                List<Hop> parents = new ArrayList<>(bop.getParent());
                for (Hop p : parents) HopRewriteUtils.replaceChildReference(p, bop, gen);
                hi = gen;
                LOG.debug("Applied fuseDatagenAndBinaryOperation1 " + "(" + bop.getFilename() + ", line " + bop.getBeginLine() + ").");
            }
        } else // right input rand and hence output matrix double, left scalar literal
        if (right instanceof DataGenOp && ((DataGenOp) right).getOp() == DataGenMethod.RAND && left instanceof LiteralOp && right.getParent().size() == 1) {
            DataGenOp inputGen = (DataGenOp) right;
            Hop pdf = inputGen.getInput(DataExpression.RAND_PDF);
            Hop min = inputGen.getInput(DataExpression.RAND_MIN);
            Hop max = inputGen.getInput(DataExpression.RAND_MAX);
            double sval = ((LiteralOp) left).getDoubleValue();
            boolean pdfUniform = pdf instanceof LiteralOp && DataExpression.RAND_PDF_UNIFORM.equals(((LiteralOp) pdf).getStringValue());
            if ((bop.getOp() == OpOp2.MULT || bop.getOp() == OpOp2.PLUS) && min instanceof LiteralOp && max instanceof LiteralOp && pdfUniform) {
                // create fused data gen operator
                DataGenOp gen = null;
                if (bop.getOp() == OpOp2.MULT)
                    gen = HopRewriteUtils.copyDataGenOp(inputGen, sval, 0);
                else {
                    // OpOp2.PLUS
                    gen = HopRewriteUtils.copyDataGenOp(inputGen, 1, sval);
                }
                // rewire all parents (avoid anomalies with replicated datagen)
                List<Hop> parents = new ArrayList<>(bop.getParent());
                for (Hop p : parents) HopRewriteUtils.replaceChildReference(p, bop, gen);
                hi = gen;
                LOG.debug("Applied fuseDatagenAndBinaryOperation2 " + "(" + bop.getFilename() + ", line " + bop.getBeginLine() + ").");
            }
        } else // left input rand and hence output matrix double, right scalar variable
        if (HopRewriteUtils.isDataGenOp(left, DataGenMethod.RAND) && right.getDataType().isScalar() && left.getParent().size() == 1) {
            DataGenOp gen = (DataGenOp) left;
            Hop min = gen.getInput(DataExpression.RAND_MIN);
            Hop max = gen.getInput(DataExpression.RAND_MAX);
            Hop pdf = gen.getInput(DataExpression.RAND_PDF);
            boolean pdfUniform = pdf instanceof LiteralOp && DataExpression.RAND_PDF_UNIFORM.equals(((LiteralOp) pdf).getStringValue());
            if (HopRewriteUtils.isBinary(bop, OpOp2.PLUS) && HopRewriteUtils.isLiteralOfValue(min, 0) && HopRewriteUtils.isLiteralOfValue(max, 0)) {
                gen.setInput(DataExpression.RAND_MIN, right);
                gen.setInput(DataExpression.RAND_MAX, right);
                // rewire all parents (avoid anomalies with replicated datagen)
                List<Hop> parents = new ArrayList<>(bop.getParent());
                for (Hop p : parents) HopRewriteUtils.replaceChildReference(p, bop, gen);
                hi = gen;
                LOG.debug("Applied fuseDatagenAndBinaryOperation3a " + "(" + bop.getFilename() + ", line " + bop.getBeginLine() + ").");
            } else if (HopRewriteUtils.isBinary(bop, OpOp2.MULT) && ((HopRewriteUtils.isLiteralOfValue(min, 0) && pdfUniform) || HopRewriteUtils.isLiteralOfValue(min, 1)) && HopRewriteUtils.isLiteralOfValue(max, 1)) {
                if (HopRewriteUtils.isLiteralOfValue(min, 1))
                    gen.setInput(DataExpression.RAND_MIN, right);
                gen.setInput(DataExpression.RAND_MAX, right);
                // rewire all parents (avoid anomalies with replicated datagen)
                List<Hop> parents = new ArrayList<>(bop.getParent());
                for (Hop p : parents) HopRewriteUtils.replaceChildReference(p, bop, gen);
                hi = gen;
                LOG.debug("Applied fuseDatagenAndBinaryOperation3b " + "(" + bop.getFilename() + ", line " + bop.getBeginLine() + ").");
            }
        }
    }
    return hi;
}
Also used : DataGenOp(org.apache.sysml.hops.DataGenOp) Hop(org.apache.sysml.hops.Hop) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) LiteralOp(org.apache.sysml.hops.LiteralOp) AggBinaryOp(org.apache.sysml.hops.AggBinaryOp) BinaryOp(org.apache.sysml.hops.BinaryOp)

Example 20 with DataGenOp

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

the class RewriteAlgebraicSimplificationStatic method simplifyOrderedSort.

private static Hop simplifyOrderedSort(Hop parent, Hop hi, int pos) {
    // order(seq(2,N+1,1), indexreturn=TRUE) -> seq(1,N,1)/seq(N,1,-1)
    if (// order
    hi instanceof ReorgOp && ((ReorgOp) hi).getOp() == ReOrgOp.SORT) {
        Hop hi2 = hi.getInput().get(0);
        if (hi2 instanceof DataGenOp && ((DataGenOp) hi2).getOp() == DataGenMethod.SEQ) {
            Hop incr = hi2.getInput().get(((DataGenOp) hi2).getParamIndex(Statement.SEQ_INCR));
            // check for known ascending ordering and known indexreturn
            if (incr instanceof LiteralOp && HopRewriteUtils.getDoubleValue((LiteralOp) incr) == 1 && // decreasing
            hi.getInput().get(2) instanceof LiteralOp && // indexreturn
            hi.getInput().get(3) instanceof LiteralOp) {
                if (// IXRET, ASC/DESC
                HopRewriteUtils.getBooleanValue((LiteralOp) hi.getInput().get(3))) {
                    // order(seq(2,N+1,1), indexreturn=TRUE) -> seq(1,N,1)/seq(N,1,-1)
                    boolean desc = HopRewriteUtils.getBooleanValue((LiteralOp) hi.getInput().get(2));
                    Hop seq = HopRewriteUtils.createSeqDataGenOp(hi2, !desc);
                    seq.refreshSizeInformation();
                    HopRewriteUtils.replaceChildReference(parent, hi, seq, pos);
                    HopRewriteUtils.cleanupUnreferenced(hi);
                    hi = seq;
                    LOG.debug("Applied simplifyOrderedSort1.");
                } else if (// DATA, ASC
                !HopRewriteUtils.getBooleanValue((LiteralOp) hi.getInput().get(2))) {
                    // order(seq(2,N+1,1), indexreturn=FALSE) -> seq(2,N+1,1)
                    HopRewriteUtils.replaceChildReference(parent, hi, hi2, pos);
                    HopRewriteUtils.cleanupUnreferenced(hi);
                    hi = hi2;
                    LOG.debug("Applied simplifyOrderedSort2.");
                }
            }
        }
    }
    return hi;
}
Also used : DataGenOp(org.apache.sysml.hops.DataGenOp) ReorgOp(org.apache.sysml.hops.ReorgOp) Hop(org.apache.sysml.hops.Hop) LiteralOp(org.apache.sysml.hops.LiteralOp)

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