Search in sources :

Example 91 with Hop

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

the class RewriteAlgebraicSimplificationStatic method removeUnnecessaryMinus.

private static Hop removeUnnecessaryMinus(Hop parent, Hop hi, int pos) {
    if (hi.getDataType() == DataType.MATRIX && hi instanceof BinaryOp && // first minus
    ((BinaryOp) hi).getOp() == OpOp2.MINUS && hi.getInput().get(0) instanceof LiteralOp && ((LiteralOp) hi.getInput().get(0)).getDoubleValue() == 0) {
        Hop hi2 = hi.getInput().get(1);
        if (hi2.getDataType() == DataType.MATRIX && hi2 instanceof BinaryOp && // second minus
        ((BinaryOp) hi2).getOp() == OpOp2.MINUS && hi2.getInput().get(0) instanceof LiteralOp && ((LiteralOp) hi2.getInput().get(0)).getDoubleValue() == 0) {
            Hop hi3 = hi2.getInput().get(1);
            // remove unnecessary chain of -(-())
            HopRewriteUtils.replaceChildReference(parent, hi, hi3, pos);
            HopRewriteUtils.cleanupUnreferenced(hi, hi2);
            hi = hi3;
            LOG.debug("Applied removeUnecessaryMinus");
        }
    }
    return hi;
}
Also used : Hop(org.apache.sysml.hops.Hop) LiteralOp(org.apache.sysml.hops.LiteralOp) AggBinaryOp(org.apache.sysml.hops.AggBinaryOp) BinaryOp(org.apache.sysml.hops.BinaryOp)

Example 92 with Hop

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

the class RewriteAlgebraicSimplificationStatic method simplifyUnaryPPredOperation.

private static Hop simplifyUnaryPPredOperation(Hop parent, Hop hi, int pos) {
    if (// unaryop
    hi instanceof UnaryOp && hi.getDataType() == DataType.MATRIX && // binaryop - ppred
    hi.getInput().get(0) instanceof BinaryOp && ((BinaryOp) hi.getInput().get(0)).isPPredOperation()) {
        // valid unary op
        UnaryOp uop = (UnaryOp) hi;
        if (uop.getOp() == OpOp1.ABS || uop.getOp() == OpOp1.SIGN || uop.getOp() == OpOp1.CEIL || uop.getOp() == OpOp1.FLOOR || uop.getOp() == OpOp1.ROUND) {
            // clear link unary-binary
            Hop input = uop.getInput().get(0);
            HopRewriteUtils.replaceChildReference(parent, hi, input, pos);
            HopRewriteUtils.cleanupUnreferenced(hi);
            hi = input;
            LOG.debug("Applied simplifyUnaryPPredOperation.");
        }
    }
    return hi;
}
Also used : AggUnaryOp(org.apache.sysml.hops.AggUnaryOp) UnaryOp(org.apache.sysml.hops.UnaryOp) Hop(org.apache.sysml.hops.Hop) AggBinaryOp(org.apache.sysml.hops.AggBinaryOp) BinaryOp(org.apache.sysml.hops.BinaryOp)

Example 93 with Hop

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

the class RewriteAlgebraicSimplificationStatic method pushdownSumBinaryMult.

private static Hop pushdownSumBinaryMult(Hop parent, Hop hi, int pos) {
    // pattern:  sum(lamda*X) -> lamda*sum(X)
    if (hi instanceof AggUnaryOp && ((AggUnaryOp) hi).getDirection() == Direction.RowCol && // only one parent which is the sum
    ((AggUnaryOp) hi).getOp() == Hop.AggOp.SUM && HopRewriteUtils.isBinary(hi.getInput().get(0), OpOp2.MULT, 1) && ((hi.getInput().get(0).getInput().get(0).getDataType() == DataType.SCALAR && hi.getInput().get(0).getInput().get(1).getDataType() == DataType.MATRIX) || (hi.getInput().get(0).getInput().get(0).getDataType() == DataType.MATRIX && hi.getInput().get(0).getInput().get(1).getDataType() == DataType.SCALAR))) {
        Hop operand1 = hi.getInput().get(0).getInput().get(0);
        Hop operand2 = hi.getInput().get(0).getInput().get(1);
        // check which operand is the Scalar and which is the matrix
        Hop lamda = (operand1.getDataType() == DataType.SCALAR) ? operand1 : operand2;
        Hop matrix = (operand1.getDataType() == DataType.MATRIX) ? operand1 : operand2;
        AggUnaryOp aggOp = HopRewriteUtils.createAggUnaryOp(matrix, AggOp.SUM, Direction.RowCol);
        Hop bop = HopRewriteUtils.createBinary(lamda, aggOp, OpOp2.MULT);
        HopRewriteUtils.replaceChildReference(parent, hi, bop, pos);
        LOG.debug("Applied pushdownSumBinaryMult.");
        return bop;
    }
    return hi;
}
Also used : AggUnaryOp(org.apache.sysml.hops.AggUnaryOp) Hop(org.apache.sysml.hops.Hop)

Example 94 with Hop

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

the class RewriteAlgebraicSimplificationStatic method fuseMinusNzBinaryOperation.

private static Hop fuseMinusNzBinaryOperation(Hop parent, Hop hi, int pos) {
    // memory estimate for X - tmp if X is sparse
    if (HopRewriteUtils.isBinary(hi, OpOp2.MINUS) && hi.getInput().get(0).getDataType() == DataType.MATRIX && hi.getInput().get(1).getDataType() == DataType.MATRIX && HopRewriteUtils.isBinary(hi.getInput().get(1), OpOp2.MULT)) {
        Hop X = hi.getInput().get(0);
        Hop s = hi.getInput().get(1).getInput().get(0);
        Hop pred = hi.getInput().get(1).getInput().get(1);
        if (s.getDataType() == DataType.SCALAR && pred.getDataType() == DataType.MATRIX && HopRewriteUtils.isBinary(pred, OpOp2.NOTEQUAL) && // depend on common subexpression elimination
        pred.getInput().get(0) == X && pred.getInput().get(1) instanceof LiteralOp && HopRewriteUtils.getDoubleValueSafe((LiteralOp) pred.getInput().get(1)) == 0) {
            Hop hnew = HopRewriteUtils.createBinary(X, s, OpOp2.MINUS_NZ);
            // relink new hop into original position
            HopRewriteUtils.replaceChildReference(parent, hi, hnew, pos);
            hi = hnew;
            LOG.debug("Applied fuseMinusNzBinaryOperation (line " + hi.getBeginLine() + ")");
        }
    }
    return hi;
}
Also used : Hop(org.apache.sysml.hops.Hop) LiteralOp(org.apache.sysml.hops.LiteralOp)

Example 95 with Hop

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

the class RewriteAlgebraicSimplificationStatic method pushdownCSETransposeScalarOperation.

private static Hop pushdownCSETransposeScalarOperation(Hop parent, Hop hi, int pos) {
    // (with support for left or right scalar operations)
    if (HopRewriteUtils.isTransposeOperation(hi, 1) && HopRewriteUtils.isBinaryMatrixScalarOperation(hi.getInput().get(0)) && hi.getInput().get(0).getParent().size() == 1) {
        int Xpos = hi.getInput().get(0).getInput().get(0).getDataType().isMatrix() ? 0 : 1;
        Hop X = hi.getInput().get(0).getInput().get(Xpos);
        BinaryOp binary = (BinaryOp) hi.getInput().get(0);
        if (HopRewriteUtils.containsTransposeOperation(X.getParent()) && !HopRewriteUtils.isValidOp(binary.getOp(), new OpOp2[] { OpOp2.CENTRALMOMENT, OpOp2.QUANTILE })) {
            // clear existing wiring
            HopRewriteUtils.removeChildReferenceByPos(parent, hi, pos);
            HopRewriteUtils.removeChildReference(hi, binary);
            HopRewriteUtils.removeChildReference(binary, X);
            // re-wire operators
            HopRewriteUtils.addChildReference(parent, binary, pos);
            HopRewriteUtils.addChildReference(binary, hi, Xpos);
            HopRewriteUtils.addChildReference(hi, X);
            // note: common subexpression later eliminated by dedicated rewrite
            hi = binary;
            LOG.debug("Applied pushdownCSETransposeScalarOperation (line " + hi.getBeginLine() + ").");
        }
    }
    return hi;
}
Also used : Hop(org.apache.sysml.hops.Hop) AggBinaryOp(org.apache.sysml.hops.AggBinaryOp) BinaryOp(org.apache.sysml.hops.BinaryOp)

Aggregations

Hop (org.apache.sysml.hops.Hop)307 LiteralOp (org.apache.sysml.hops.LiteralOp)94 AggBinaryOp (org.apache.sysml.hops.AggBinaryOp)65 BinaryOp (org.apache.sysml.hops.BinaryOp)63 ArrayList (java.util.ArrayList)61 AggUnaryOp (org.apache.sysml.hops.AggUnaryOp)61 HashMap (java.util.HashMap)44 DataOp (org.apache.sysml.hops.DataOp)41 UnaryOp (org.apache.sysml.hops.UnaryOp)41 HashSet (java.util.HashSet)39 ReorgOp (org.apache.sysml.hops.ReorgOp)32 MemoTableEntry (org.apache.sysml.hops.codegen.template.CPlanMemoTable.MemoTableEntry)28 StatementBlock (org.apache.sysml.parser.StatementBlock)28 IndexingOp (org.apache.sysml.hops.IndexingOp)24 ForStatementBlock (org.apache.sysml.parser.ForStatementBlock)23 WhileStatementBlock (org.apache.sysml.parser.WhileStatementBlock)23 IfStatementBlock (org.apache.sysml.parser.IfStatementBlock)22 DataGenOp (org.apache.sysml.hops.DataGenOp)21 DMLRuntimeException (org.apache.sysml.runtime.DMLRuntimeException)21 HopsException (org.apache.sysml.hops.HopsException)18