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;
}
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;
}
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;
}
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;
}
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;
}
Aggregations