use of org.apache.sysml.hops.BinaryOp 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.BinaryOp 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;
}
use of org.apache.sysml.hops.BinaryOp in project incubator-systemml by apache.
the class RewriteAlgebraicSimplificationStatic method simplifyBinaryToUnaryOperation.
/**
* Handle simplification of binary operations (relies on previous common subexpression elimination).
* At the same time this servers as a canonicalization for more complex rewrites.
*
* X+X -> X*2, X*X -> X^2, (X>0)-(X<0) -> sign(X)
*
* @param parent parent high-level operator
* @param hi high-level operator
* @param pos position
* @return high-level operator
*/
private static Hop simplifyBinaryToUnaryOperation(Hop parent, Hop hi, int pos) {
if (hi instanceof BinaryOp) {
BinaryOp bop = (BinaryOp) hi;
Hop left = hi.getInput().get(0);
Hop right = hi.getInput().get(1);
// patterns: X+X -> X*2, X*X -> X^2,
if (left == right && left.getDataType() == DataType.MATRIX) {
// however, we later compile specific LOPS for X*2 and X^2
if (// X+X -> X*2
bop.getOp() == OpOp2.PLUS) {
bop.setOp(OpOp2.MULT);
HopRewriteUtils.replaceChildReference(hi, right, new LiteralOp(2), 1);
LOG.debug("Applied simplifyBinaryToUnaryOperation1 (line " + hi.getBeginLine() + ").");
} else if (// X*X -> X^2
bop.getOp() == OpOp2.MULT) {
bop.setOp(OpOp2.POW);
HopRewriteUtils.replaceChildReference(hi, right, new LiteralOp(2), 1);
LOG.debug("Applied simplifyBinaryToUnaryOperation2 (line " + hi.getBeginLine() + ").");
}
} else // patterns: (X>0)-(X<0) -> sign(X)
if (bop.getOp() == OpOp2.MINUS && HopRewriteUtils.isBinary(left, OpOp2.GREATER) && HopRewriteUtils.isBinary(right, OpOp2.LESS) && left.getInput().get(0) == right.getInput().get(0) && left.getInput().get(1) instanceof LiteralOp && HopRewriteUtils.getDoubleValue((LiteralOp) left.getInput().get(1)) == 0 && right.getInput().get(1) instanceof LiteralOp && HopRewriteUtils.getDoubleValue((LiteralOp) right.getInput().get(1)) == 0) {
UnaryOp uop = HopRewriteUtils.createUnary(left.getInput().get(0), OpOp1.SIGN);
HopRewriteUtils.replaceChildReference(parent, hi, uop, pos);
HopRewriteUtils.cleanupUnreferenced(hi, left, right);
hi = uop;
LOG.debug("Applied simplifyBinaryToUnaryOperation3 (line " + hi.getBeginLine() + ").");
}
}
return hi;
}
use of org.apache.sysml.hops.BinaryOp in project incubator-systemml by apache.
the class RewriteAlgebraicSimplificationStatic method simplifyDistributiveBinaryOperation.
/**
* (X-Y*X) -> (1-Y)*X, (Y*X-X) -> (Y-1)*X
* (X+Y*X) -> (1+Y)*X, (Y*X+X) -> (Y+1)*X
*
* @param parent parent high-level operator
* @param hi high-level operator
* @param pos position
* @return high-level operator
*/
private static Hop simplifyDistributiveBinaryOperation(Hop parent, Hop hi, int pos) {
if (hi instanceof BinaryOp) {
BinaryOp bop = (BinaryOp) hi;
Hop left = bop.getInput().get(0);
Hop right = bop.getInput().get(1);
// (X+Y*X) -> (1+Y)*X, (Y*X+X) -> (Y+1)*X
// (X-Y*X) -> (1-Y)*X, (Y*X-X) -> (Y-1)*X
boolean applied = false;
if (left.getDataType() == DataType.MATRIX && right.getDataType() == DataType.MATRIX && HopRewriteUtils.isValidOp(bop.getOp(), LOOKUP_VALID_DISTRIBUTIVE_BINARY)) {
Hop X = null;
Hop Y = null;
if (// (Y*X-X) -> (Y-1)*X
HopRewriteUtils.isBinary(left, OpOp2.MULT)) {
Hop leftC1 = left.getInput().get(0);
Hop leftC2 = left.getInput().get(1);
if (leftC1.getDataType() == DataType.MATRIX && leftC2.getDataType() == DataType.MATRIX && (right == leftC1 || right == leftC2) && leftC1 != leftC2) {
// any mult order
X = right;
Y = (right == leftC1) ? leftC2 : leftC1;
}
if (X != null) {
// rewrite 'binary +/-'
LiteralOp literal = new LiteralOp(1);
BinaryOp plus = HopRewriteUtils.createBinary(Y, literal, bop.getOp());
BinaryOp mult = HopRewriteUtils.createBinary(plus, X, OpOp2.MULT);
HopRewriteUtils.replaceChildReference(parent, hi, mult, pos);
HopRewriteUtils.cleanupUnreferenced(hi, left);
hi = mult;
applied = true;
LOG.debug("Applied simplifyDistributiveBinaryOperation1");
}
}
if (// (X-Y*X) -> (1-Y)*X
!applied && HopRewriteUtils.isBinary(right, OpOp2.MULT)) {
Hop rightC1 = right.getInput().get(0);
Hop rightC2 = right.getInput().get(1);
if (rightC1.getDataType() == DataType.MATRIX && rightC2.getDataType() == DataType.MATRIX && (left == rightC1 || left == rightC2) && rightC1 != rightC2) {
// any mult order
X = left;
Y = (left == rightC1) ? rightC2 : rightC1;
}
if (X != null) {
// rewrite '+/- binary'
LiteralOp literal = new LiteralOp(1);
BinaryOp plus = HopRewriteUtils.createBinary(literal, Y, bop.getOp());
BinaryOp mult = HopRewriteUtils.createBinary(plus, X, OpOp2.MULT);
HopRewriteUtils.replaceChildReference(parent, hi, mult, pos);
HopRewriteUtils.cleanupUnreferenced(hi, right);
hi = mult;
LOG.debug("Applied simplifyDistributiveBinaryOperation2");
}
}
}
}
return hi;
}
use of org.apache.sysml.hops.BinaryOp in project incubator-systemml by apache.
the class RewriteAlgebraicSimplificationStatic method simplifyTransposedAppend.
private static Hop simplifyTransposedAppend(Hop parent, Hop hi, int pos) {
// e.g., t(cbind(t(A),t(B))) --> rbind(A,B), t(rbind(t(A),t(B))) --> cbind(A,B)
if (// t() rooted
HopRewriteUtils.isTransposeOperation(hi) && hi.getInput().get(0) instanceof BinaryOp && (// append (cbind/rbind)
((BinaryOp) hi.getInput().get(0)).getOp() == OpOp2.CBIND || ((BinaryOp) hi.getInput().get(0)).getOp() == OpOp2.RBIND) && // single consumer of append
hi.getInput().get(0).getParent().size() == 1) {
BinaryOp bop = (BinaryOp) hi.getInput().get(0);
// both inputs transpose ops, where transpose is single consumer
if (HopRewriteUtils.isTransposeOperation(bop.getInput().get(0), 1) && HopRewriteUtils.isTransposeOperation(bop.getInput().get(1), 1)) {
Hop left = bop.getInput().get(0).getInput().get(0);
Hop right = bop.getInput().get(1).getInput().get(0);
// create new subdag (no in-place dag update to prevent anomalies with
// multiple consumers during rewrite process)
OpOp2 binop = (bop.getOp() == OpOp2.CBIND) ? OpOp2.RBIND : OpOp2.CBIND;
BinaryOp bopnew = HopRewriteUtils.createBinary(left, right, binop);
HopRewriteUtils.replaceChildReference(parent, hi, bopnew, pos);
hi = bopnew;
LOG.debug("Applied simplifyTransposedAppend (line " + hi.getBeginLine() + ").");
}
}
return hi;
}
Aggregations