use of org.apache.sysml.hops.LiteralOp in project incubator-systemml by apache.
the class RewriteAlgebraicSimplificationDynamic method fuseLeftIndexingChainToAppend.
private static Hop fuseLeftIndexingChainToAppend(Hop parent, Hop hi, int pos) {
boolean applied = false;
// pattern1: X[,1]=A; X[,2]=B -> X=cbind(A,B); matrix / frame
if (// first lix
hi instanceof LeftIndexingOp && HopRewriteUtils.isFullColumnIndexing((LeftIndexingOp) hi) && // second lix
hi.getInput().get(0) instanceof LeftIndexingOp && HopRewriteUtils.isFullColumnIndexing((LeftIndexingOp) hi.getInput().get(0)) && // first lix is single consumer
hi.getInput().get(0).getParent().size() == 1 && // two column matrix
hi.getInput().get(0).getInput().get(0).getDim2() == 2) {
// rhs matrix
Hop input2 = hi.getInput().get(1);
// cl=cu
Hop pred2 = hi.getInput().get(4);
// lhs matrix
Hop input1 = hi.getInput().get(0).getInput().get(1);
// cl=cu
Hop pred1 = hi.getInput().get(0).getInput().get(4);
if (pred1 instanceof LiteralOp && HopRewriteUtils.getDoubleValueSafe((LiteralOp) pred1) == 1 && pred2 instanceof LiteralOp && HopRewriteUtils.getDoubleValueSafe((LiteralOp) pred2) == 2 && input1.getDataType() != DataType.SCALAR && input2.getDataType() != DataType.SCALAR) {
// create new cbind operation and rewrite inputs
BinaryOp bop = HopRewriteUtils.createBinary(input1, input2, OpOp2.CBIND);
HopRewriteUtils.replaceChildReference(parent, hi, bop, pos);
hi = bop;
applied = true;
}
}
// pattern1: X[1,]=A; X[2,]=B -> X=rbind(A,B)
if (// first lix
!applied && hi instanceof LeftIndexingOp && HopRewriteUtils.isFullRowIndexing((LeftIndexingOp) hi) && // second lix
hi.getInput().get(0) instanceof LeftIndexingOp && HopRewriteUtils.isFullRowIndexing((LeftIndexingOp) hi.getInput().get(0)) && // first lix is single consumer
hi.getInput().get(0).getParent().size() == 1 && // two column matrix
hi.getInput().get(0).getInput().get(0).getDim1() == 2) {
// rhs matrix
Hop input2 = hi.getInput().get(1);
// rl=ru
Hop pred2 = hi.getInput().get(2);
// lhs matrix
Hop input1 = hi.getInput().get(0).getInput().get(1);
// rl=ru
Hop pred1 = hi.getInput().get(0).getInput().get(2);
if (pred1 instanceof LiteralOp && HopRewriteUtils.getDoubleValueSafe((LiteralOp) pred1) == 1 && pred2 instanceof LiteralOp && HopRewriteUtils.getDoubleValueSafe((LiteralOp) pred2) == 2 && input1.getDataType() != DataType.SCALAR && input2.getDataType() != DataType.SCALAR) {
// create new cbind operation and rewrite inputs
BinaryOp bop = HopRewriteUtils.createBinary(input1, input2, OpOp2.RBIND);
HopRewriteUtils.replaceChildReference(parent, hi, bop, pos);
hi = bop;
applied = true;
LOG.debug("Applied fuseLeftIndexingChainToAppend2 (line " + hi.getBeginLine() + ")");
}
}
return hi;
}
use of org.apache.sysml.hops.LiteralOp in project incubator-systemml by apache.
the class RewriteAlgebraicSimplificationDynamic method simplifyNrowNcolComputation.
private static Hop simplifyNrowNcolComputation(Hop parent, Hop hi, int pos) {
// even if the intermediate is otherwise not required, e.g., when part of a fused operator)
if (hi instanceof UnaryOp) {
if (((UnaryOp) hi).getOp() == OpOp1.NROW && hi.getInput().get(0).rowsKnown()) {
Hop hnew = new LiteralOp(hi.getInput().get(0).getDim1());
HopRewriteUtils.replaceChildReference(parent, hi, hnew, pos, false);
HopRewriteUtils.cleanupUnreferenced(hi);
LOG.debug("Applied simplifyNrowComputation nrow(" + hi.getHopID() + ") -> " + hnew.getName() + " (line " + hi.getBeginLine() + ").");
hi = hnew;
} else if (((UnaryOp) hi).getOp() == OpOp1.NCOL && hi.getInput().get(0).colsKnown()) {
Hop hnew = new LiteralOp(hi.getInput().get(0).getDim2());
HopRewriteUtils.replaceChildReference(parent, hi, hnew, pos, false);
HopRewriteUtils.cleanupUnreferenced(hi);
LOG.debug("Applied simplifyNcolComputation ncol(" + hi.getHopID() + ") -> " + hnew.getName() + " (line " + hi.getBeginLine() + ").");
hi = hnew;
}
}
return hi;
}
use of org.apache.sysml.hops.LiteralOp 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.LiteralOp 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.LiteralOp 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;
}
Aggregations