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