Search in sources :

Example 21 with ReorgOp

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

the class RewriteAlgebraicSimplificationStatic method simplifyTransposeAggBinBinaryChains.

/**
 * Patterns: t(t(A)%*%t(B)+C) -> B%*%A+t(C)
 *
 * @param parent parent high-level operator
 * @param hi high-level operator
 * @param pos position
 * @return high-level operator
 */
private static Hop simplifyTransposeAggBinBinaryChains(Hop parent, Hop hi, int pos) {
    if (HopRewriteUtils.isTransposeOperation(hi) && // basic binary
    hi.getInput().get(0) instanceof BinaryOp && ((BinaryOp) hi.getInput().get(0)).supportsMatrixScalarOperations()) {
        Hop left = hi.getInput().get(0).getInput().get(0);
        Hop C = hi.getInput().get(0).getInput().get(1);
        // check matrix mult and both inputs transposes w/ single consumer
        if (left instanceof AggBinaryOp && C.getDataType().isMatrix() && HopRewriteUtils.isTransposeOperation(left.getInput().get(0)) && left.getInput().get(0).getParent().size() == 1 && HopRewriteUtils.isTransposeOperation(left.getInput().get(1)) && left.getInput().get(1).getParent().size() == 1) {
            Hop A = left.getInput().get(0).getInput().get(0);
            Hop B = left.getInput().get(1).getInput().get(0);
            AggBinaryOp abop = HopRewriteUtils.createMatrixMultiply(B, A);
            ReorgOp rop = HopRewriteUtils.createTranspose(C);
            BinaryOp bop = HopRewriteUtils.createBinary(abop, rop, OpOp2.PLUS);
            HopRewriteUtils.replaceChildReference(parent, hi, bop, pos);
            hi = bop;
            LOG.debug("Applied simplifyTransposeAggBinBinaryChains (line " + hi.getBeginLine() + ").");
        }
    }
    return hi;
}
Also used : AggBinaryOp(org.apache.sysml.hops.AggBinaryOp) Hop(org.apache.sysml.hops.Hop) ReorgOp(org.apache.sysml.hops.ReorgOp) AggBinaryOp(org.apache.sysml.hops.AggBinaryOp) BinaryOp(org.apache.sysml.hops.BinaryOp)

Example 22 with ReorgOp

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

the class RewriteAlgebraicSimplificationStatic method simplifyTraceMatrixMult.

private static Hop simplifyTraceMatrixMult(Hop parent, Hop hi, int pos) {
    if (// trace()
    hi instanceof AggUnaryOp && ((AggUnaryOp) hi).getOp() == AggOp.TRACE) {
        Hop hi2 = hi.getInput().get(0);
        if (// X%*%Y
        HopRewriteUtils.isMatrixMultiply(hi2)) {
            Hop left = hi2.getInput().get(0);
            Hop right = hi2.getInput().get(1);
            // create new operators (incl refresh size inside for transpose)
            ReorgOp trans = HopRewriteUtils.createTranspose(right);
            BinaryOp mult = HopRewriteUtils.createBinary(left, trans, OpOp2.MULT);
            AggUnaryOp sum = HopRewriteUtils.createSum(mult);
            // rehang new subdag under parent node
            HopRewriteUtils.replaceChildReference(parent, hi, sum, pos);
            HopRewriteUtils.cleanupUnreferenced(hi, hi2);
            hi = sum;
            LOG.debug("Applied simplifyTraceMatrixMult");
        }
    }
    return hi;
}
Also used : AggUnaryOp(org.apache.sysml.hops.AggUnaryOp) Hop(org.apache.sysml.hops.Hop) ReorgOp(org.apache.sysml.hops.ReorgOp) AggBinaryOp(org.apache.sysml.hops.AggBinaryOp) BinaryOp(org.apache.sysml.hops.BinaryOp)

Example 23 with ReorgOp

use of org.apache.sysml.hops.ReorgOp 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)

Example 24 with ReorgOp

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

the class RewriteAlgebraicSimplificationStatic method removeUnnecessaryReorgOperation.

/**
 * Pattners: t(t(X)) -> X, rev(rev(X)) -> X
 *
 * @param parent parent high-level operator
 * @param hi high-level operator
 * @param pos position
 * @return high-level operator
 */
private static Hop removeUnnecessaryReorgOperation(Hop parent, Hop hi, int pos) {
    ReOrgOp[] lookup = new ReOrgOp[] { ReOrgOp.TRANSPOSE, ReOrgOp.REV };
    if (// first reorg
    hi instanceof ReorgOp && HopRewriteUtils.isValidOp(((ReorgOp) hi).getOp(), lookup)) {
        ReOrgOp firstOp = ((ReorgOp) hi).getOp();
        Hop hi2 = hi.getInput().get(0);
        if (// second reorg w/ same type
        hi2 instanceof ReorgOp && ((ReorgOp) hi2).getOp() == firstOp) {
            Hop hi3 = hi2.getInput().get(0);
            // remove unnecessary chain of t(t())
            HopRewriteUtils.replaceChildReference(parent, hi, hi3, pos);
            HopRewriteUtils.cleanupUnreferenced(hi, hi2);
            hi = hi3;
            LOG.debug("Applied removeUnecessaryReorgOperation.");
        }
    }
    return hi;
}
Also used : ReOrgOp(org.apache.sysml.hops.Hop.ReOrgOp) ReorgOp(org.apache.sysml.hops.ReorgOp) Hop(org.apache.sysml.hops.Hop)

Example 25 with ReorgOp

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

the class RewriteAlgebraicSimplificationStatic method simplifyConstantSort.

private static Hop simplifyConstantSort(Hop parent, Hop hi, int pos) {
    // order(matrix(7), indexreturn=TRUE) -> seq(1,nrow(X),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.RAND && ((DataGenOp) hi2).hasConstantValue() && // known indexreturn
        hi.getInput().get(3) instanceof LiteralOp) {
            if (HopRewriteUtils.getBooleanValue((LiteralOp) hi.getInput().get(3))) {
                // order(matrix(7), indexreturn=TRUE) -> seq(1,nrow(X),1)
                Hop seq = HopRewriteUtils.createSeqDataGenOp(hi2);
                seq.refreshSizeInformation();
                HopRewriteUtils.replaceChildReference(parent, hi, seq, pos);
                HopRewriteUtils.cleanupUnreferenced(hi);
                hi = seq;
                LOG.debug("Applied simplifyConstantSort1.");
            } else {
                // order(matrix(7), indexreturn=FALSE) -> matrix(7)
                HopRewriteUtils.replaceChildReference(parent, hi, hi2, pos);
                HopRewriteUtils.cleanupUnreferenced(hi);
                hi = hi2;
                LOG.debug("Applied simplifyConstantSort2.");
            }
        }
    }
    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

ReorgOp (org.apache.sysml.hops.ReorgOp)28 Hop (org.apache.sysml.hops.Hop)26 AggBinaryOp (org.apache.sysml.hops.AggBinaryOp)11 AggUnaryOp (org.apache.sysml.hops.AggUnaryOp)11 LiteralOp (org.apache.sysml.hops.LiteralOp)9 BinaryOp (org.apache.sysml.hops.BinaryOp)6 ArrayList (java.util.ArrayList)5 DataGenOp (org.apache.sysml.hops.DataGenOp)5 UnaryOp (org.apache.sysml.hops.UnaryOp)5 HashMap (java.util.HashMap)4 ParameterizedBuiltinOp (org.apache.sysml.hops.ParameterizedBuiltinOp)4 TernaryOp (org.apache.sysml.hops.TernaryOp)4 DataOp (org.apache.sysml.hops.DataOp)3 IndexingOp (org.apache.sysml.hops.IndexingOp)3 MultiThreadedHop (org.apache.sysml.hops.Hop.MultiThreadedHop)2 DMLRuntimeException (org.apache.sysml.runtime.DMLRuntimeException)2 ConvolutionOp (org.apache.sysml.hops.ConvolutionOp)1 FunctionOp (org.apache.sysml.hops.FunctionOp)1 DataGenMethod (org.apache.sysml.hops.Hop.DataGenMethod)1 OpOp2 (org.apache.sysml.hops.Hop.OpOp2)1