Search in sources :

Example 31 with UnaryOp

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

the class LiteralReplacement method getIntValueDataLiteral.

private static long getIntValueDataLiteral(Hop hop, LocalVariableMap vars) {
    long value = -1;
    try {
        if (hop instanceof LiteralOp) {
            value = HopRewriteUtils.getIntValue((LiteralOp) hop);
        } else if (hop instanceof UnaryOp && ((UnaryOp) hop).getOp() == OpOp1.NROW) {
            // get the dimension information from the matrix object because the hop
            // dimensions might not have been updated during recompile
            MatrixObject mo = (MatrixObject) vars.get(hop.getInput().get(0).getName());
            value = mo.getNumRows();
        } else if (hop instanceof UnaryOp && ((UnaryOp) hop).getOp() == OpOp1.NCOL) {
            // get the dimension information from the matrix object because the hop
            // dimensions might not have been updated during recompile
            MatrixObject mo = (MatrixObject) vars.get(hop.getInput().get(0).getName());
            value = mo.getNumColumns();
        } else {
            ScalarObject sdat = (ScalarObject) vars.get(hop.getName());
            value = sdat.getLongValue();
        }
    } catch (HopsException ex) {
        throw new DMLRuntimeException("Failed to get int value for literal replacement", ex);
    }
    return value;
}
Also used : ScalarObject(org.apache.sysml.runtime.instructions.cp.ScalarObject) AggUnaryOp(org.apache.sysml.hops.AggUnaryOp) UnaryOp(org.apache.sysml.hops.UnaryOp) MatrixObject(org.apache.sysml.runtime.controlprogram.caching.MatrixObject) HopsException(org.apache.sysml.hops.HopsException) LiteralOp(org.apache.sysml.hops.LiteralOp) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException)

Example 32 with UnaryOp

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

the class LiteralReplacement method replaceLiteralValueTypeCastRightIndexing.

private static LiteralOp replaceLiteralValueTypeCastRightIndexing(Hop c, LocalVariableMap vars) {
    LiteralOp ret = null;
    // as.scalar/right indexing w/ literals/vars and matrix less than 10^6 cells
    if (c instanceof UnaryOp && ((UnaryOp) c).getOp() == OpOp1.CAST_AS_SCALAR && c.getInput().get(0) instanceof IndexingOp && c.getInput().get(0).getDataType() == DataType.MATRIX) {
        IndexingOp rix = (IndexingOp) c.getInput().get(0);
        Hop data = rix.getInput().get(0);
        Hop rl = rix.getInput().get(1);
        Hop ru = rix.getInput().get(2);
        Hop cl = rix.getInput().get(3);
        Hop cu = rix.getInput().get(4);
        if (rix.dimsKnown() && rix.getDim1() == 1 && rix.getDim2() == 1 && data instanceof DataOp && vars.keySet().contains(data.getName()) && isIntValueDataLiteral(rl, vars) && isIntValueDataLiteral(ru, vars) && isIntValueDataLiteral(cl, vars) && isIntValueDataLiteral(cu, vars)) {
            long rlval = getIntValueDataLiteral(rl, vars);
            long clval = getIntValueDataLiteral(cl, vars);
            MatrixObject mo = (MatrixObject) vars.get(data.getName());
            // dimensions might not have been updated during recompile
            if (mo.getNumRows() * mo.getNumColumns() < REPLACE_LITERALS_MAX_MATRIX_SIZE) {
                MatrixBlock mBlock = mo.acquireRead();
                double value = mBlock.getValue((int) rlval - 1, (int) clval - 1);
                mo.release();
                // literal substitution (always double)
                ret = new LiteralOp(value);
            }
        }
    }
    return ret;
}
Also used : MatrixBlock(org.apache.sysml.runtime.matrix.data.MatrixBlock) AggUnaryOp(org.apache.sysml.hops.AggUnaryOp) UnaryOp(org.apache.sysml.hops.UnaryOp) MatrixObject(org.apache.sysml.runtime.controlprogram.caching.MatrixObject) IndexingOp(org.apache.sysml.hops.IndexingOp) Hop(org.apache.sysml.hops.Hop) LiteralOp(org.apache.sysml.hops.LiteralOp) DataOp(org.apache.sysml.hops.DataOp)

Example 33 with UnaryOp

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

the class Recompiler method extractDAGOutputStatistics.

public static void extractDAGOutputStatistics(Hop hop, LocalVariableMap vars, boolean overwrite) {
    if (// for all writes to symbol table
    hop instanceof DataOp && ((DataOp) hop).getDataOpType() == DataOpTypes.TRANSIENTWRITE) {
        String varName = hop.getName();
        if (// not existing so far
        !vars.keySet().contains(varName) || overwrite) {
            // extract matrix sizes for size propagation
            if (hop.getDataType() == DataType.MATRIX) {
                MatrixObject mo = new MatrixObject(ValueType.DOUBLE, null);
                MatrixCharacteristics mc = new MatrixCharacteristics(hop.getDim1(), hop.getDim2(), ConfigurationManager.getBlocksize(), ConfigurationManager.getBlocksize(), hop.getNnz());
                MetaDataFormat meta = new MetaDataFormat(mc, null, null);
                mo.setMetaData(meta);
                vars.put(varName, mo);
            } else // extract scalar constants for second constant propagation
            if (hop.getDataType() == DataType.SCALAR) {
                // extract literal assignments
                if (hop.getInput().size() == 1 && hop.getInput().get(0) instanceof LiteralOp) {
                    ScalarObject constant = HopRewriteUtils.getScalarObject((LiteralOp) hop.getInput().get(0));
                    if (constant != null)
                        vars.put(varName, constant);
                } else // extract constant variable assignments
                if (hop.getInput().size() == 1 && hop.getInput().get(0) instanceof DataOp) {
                    DataOp dop = (DataOp) hop.getInput().get(0);
                    String dopvarname = dop.getName();
                    if (dop.isRead() && vars.keySet().contains(dopvarname)) {
                        ScalarObject constant = (ScalarObject) vars.get(dopvarname);
                        // no clone because constant
                        vars.put(varName, constant);
                    }
                } else // extract ncol/nrow variable assignments
                if (hop.getInput().size() == 1 && hop.getInput().get(0) instanceof UnaryOp && (((UnaryOp) hop.getInput().get(0)).getOp() == OpOp1.NROW || ((UnaryOp) hop.getInput().get(0)).getOp() == OpOp1.NCOL)) {
                    UnaryOp uop = (UnaryOp) hop.getInput().get(0);
                    if (uop.getOp() == OpOp1.NROW && uop.getInput().get(0).getDim1() > 0)
                        vars.put(varName, new IntObject(uop.getInput().get(0).getDim1()));
                    else if (uop.getOp() == OpOp1.NCOL && uop.getInput().get(0).getDim2() > 0)
                        vars.put(varName, new IntObject(uop.getInput().get(0).getDim2()));
                } else // remove other updated scalars
                {
                    // we need to remove other updated scalars in order to ensure result
                    // correctness of recompilation w/o being too conservative
                    vars.remove(varName);
                }
            }
        } else // already existing: take largest
        {
            Data dat = vars.get(varName);
            if (dat instanceof MatrixObject) {
                MatrixObject mo = (MatrixObject) dat;
                MatrixCharacteristics mc = mo.getMatrixCharacteristics();
                if (OptimizerUtils.estimateSizeExactSparsity(mc.getRows(), mc.getCols(), (mc.getNonZeros() >= 0) ? ((double) mc.getNonZeros()) / mc.getRows() / mc.getCols() : 1.0) < OptimizerUtils.estimateSize(hop.getDim1(), hop.getDim2())) {
                    // update statistics if necessary
                    mc.setDimension(hop.getDim1(), hop.getDim2());
                    mc.setNonZeros(hop.getNnz());
                }
            } else // scalar (just overwrite)
            {
                if (hop.getInput().size() == 1 && hop.getInput().get(0) instanceof LiteralOp) {
                    ScalarObject constant = HopRewriteUtils.getScalarObject((LiteralOp) hop.getInput().get(0));
                    if (constant != null)
                        vars.put(varName, constant);
                }
            }
        }
    }
}
Also used : MetaDataFormat(org.apache.sysml.runtime.matrix.MetaDataFormat) ScalarObject(org.apache.sysml.runtime.instructions.cp.ScalarObject) MatrixObject(org.apache.sysml.runtime.controlprogram.caching.MatrixObject) UnaryOp(org.apache.sysml.hops.UnaryOp) IntObject(org.apache.sysml.runtime.instructions.cp.IntObject) CacheableData(org.apache.sysml.runtime.controlprogram.caching.CacheableData) Data(org.apache.sysml.runtime.instructions.cp.Data) LiteralOp(org.apache.sysml.hops.LiteralOp) DataOp(org.apache.sysml.hops.DataOp) MatrixCharacteristics(org.apache.sysml.runtime.matrix.MatrixCharacteristics)

Example 34 with UnaryOp

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

the class HopRewriteUtils method createDataGenOp.

public static Hop createDataGenOp(Hop input, double value) {
    Hop rows = input.rowsKnown() ? new LiteralOp(input.getDim1()) : new UnaryOp("tmprows", DataType.SCALAR, ValueType.INT, OpOp1.NROW, input);
    Hop cols = input.colsKnown() ? new LiteralOp(input.getDim2()) : new UnaryOp("tmpcols", DataType.SCALAR, ValueType.INT, OpOp1.NCOL, input);
    Hop val = new LiteralOp(value);
    HashMap<String, Hop> params = new HashMap<>();
    params.put(DataExpression.RAND_ROWS, rows);
    params.put(DataExpression.RAND_COLS, cols);
    params.put(DataExpression.RAND_MIN, val);
    params.put(DataExpression.RAND_MAX, val);
    params.put(DataExpression.RAND_PDF, new LiteralOp(DataExpression.RAND_PDF_UNIFORM));
    params.put(DataExpression.RAND_LAMBDA, new LiteralOp(-1.0));
    params.put(DataExpression.RAND_SPARSITY, new LiteralOp(1.0));
    params.put(DataExpression.RAND_SEED, new LiteralOp(DataGenOp.UNSPECIFIED_SEED));
    // note internal refresh size information
    Hop datagen = new DataGenOp(DataGenMethod.RAND, new DataIdentifier("tmp"), params);
    datagen.setOutputBlocksizes(input.getRowsInBlock(), input.getColsInBlock());
    copyLineNumbers(input, datagen);
    if (value == 0)
        datagen.setNnz(0);
    return datagen;
}
Also used : AggUnaryOp(org.apache.sysml.hops.AggUnaryOp) UnaryOp(org.apache.sysml.hops.UnaryOp) DataIdentifier(org.apache.sysml.parser.DataIdentifier) HashMap(java.util.HashMap) DataGenOp(org.apache.sysml.hops.DataGenOp) Hop(org.apache.sysml.hops.Hop) LiteralOp(org.apache.sysml.hops.LiteralOp)

Example 35 with UnaryOp

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

the class RewriteAlgebraicSimplificationDynamic method simplifyWeightedUnaryMM.

private static Hop simplifyWeightedUnaryMM(Hop parent, Hop hi, int pos) {
    Hop hnew = null;
    boolean appliedPattern = false;
    // Pattern 1) (W*uop(U%*%t(V)))
    if (hi instanceof BinaryOp && HopRewriteUtils.isValidOp(((BinaryOp) hi).getOp(), LOOKUP_VALID_WDIVMM_BINARY) && // prevent mv
    HopRewriteUtils.isEqualSize(hi.getInput().get(0), hi.getInput().get(1)) && // not applied for vector-vector mult
    hi.getDim2() > 1 && hi.getInput().get(0).getDataType() == DataType.MATRIX && hi.getInput().get(0).getDim2() > hi.getInput().get(0).getColsInBlock() && hi.getInput().get(1) instanceof UnaryOp && HopRewriteUtils.isValidOp(((UnaryOp) hi.getInput().get(1)).getOp(), LOOKUP_VALID_WUMM_UNARY) && hi.getInput().get(1).getInput().get(0) instanceof AggBinaryOp && // BLOCKSIZE CONSTRAINT
    HopRewriteUtils.isSingleBlock(hi.getInput().get(1).getInput().get(0).getInput().get(0), true)) {
        Hop W = hi.getInput().get(0);
        Hop U = hi.getInput().get(1).getInput().get(0).getInput().get(0);
        Hop V = hi.getInput().get(1).getInput().get(0).getInput().get(1);
        boolean mult = ((BinaryOp) hi).getOp() == OpOp2.MULT;
        OpOp1 op = ((UnaryOp) hi.getInput().get(1)).getOp();
        if (!HopRewriteUtils.isTransposeOperation(V))
            V = HopRewriteUtils.createTranspose(V);
        else
            V = V.getInput().get(0);
        hnew = new QuaternaryOp(hi.getName(), DataType.MATRIX, ValueType.DOUBLE, OpOp4.WUMM, W, U, V, mult, op, null);
        hnew.setOutputBlocksizes(W.getRowsInBlock(), W.getColsInBlock());
        hnew.refreshSizeInformation();
        appliedPattern = true;
        LOG.debug("Applied simplifyWeightedUnaryMM1 (line " + hi.getBeginLine() + ")");
    }
    // Pattern 2.7) (W*(U%*%t(V))*2 or 2*(W*(U%*%t(V))
    if (!appliedPattern && hi instanceof BinaryOp && HopRewriteUtils.isValidOp(((BinaryOp) hi).getOp(), OpOp2.MULT) && (HopRewriteUtils.isLiteralOfValue(hi.getInput().get(0), 2) || HopRewriteUtils.isLiteralOfValue(hi.getInput().get(1), 2))) {
        // non-literal
        final Hop nl;
        if (hi.getInput().get(0) instanceof LiteralOp) {
            nl = hi.getInput().get(1);
        } else {
            nl = hi.getInput().get(0);
        }
        if (HopRewriteUtils.isBinary(nl, OpOp2.MULT) && // ensure no foreign parents
        nl.getParent().size() == 1 && // prevent mv
        HopRewriteUtils.isEqualSize(nl.getInput().get(0), nl.getInput().get(1)) && // not applied for vector-vector mult
        nl.getDim2() > 1 && nl.getInput().get(0).getDataType() == DataType.MATRIX && nl.getInput().get(0).getDim2() > nl.getInput().get(0).getColsInBlock() && HopRewriteUtils.isOuterProductLikeMM(nl.getInput().get(1)) && // no mmchain
        (((AggBinaryOp) nl.getInput().get(1)).checkMapMultChain() == ChainType.NONE || nl.getInput().get(1).getInput().get(1).getDim2() > 1) && HopRewriteUtils.isSingleBlock(nl.getInput().get(1).getInput().get(0), true)) {
            final Hop W = nl.getInput().get(0);
            final Hop U = nl.getInput().get(1).getInput().get(0);
            Hop V = nl.getInput().get(1).getInput().get(1);
            if (!HopRewriteUtils.isTransposeOperation(V))
                V = HopRewriteUtils.createTranspose(V);
            else
                V = V.getInput().get(0);
            hnew = new QuaternaryOp(hi.getName(), DataType.MATRIX, ValueType.DOUBLE, OpOp4.WUMM, W, U, V, true, null, OpOp2.MULT);
            hnew.setOutputBlocksizes(W.getRowsInBlock(), W.getColsInBlock());
            hnew.refreshSizeInformation();
            appliedPattern = true;
            LOG.debug("Applied simplifyWeightedUnaryMM2.7 (line " + hi.getBeginLine() + ")");
        }
    }
    // Pattern 2) (W*sop(U%*%t(V),c)) for known sop translating to unary ops
    if (!appliedPattern && hi instanceof BinaryOp && HopRewriteUtils.isValidOp(((BinaryOp) hi).getOp(), LOOKUP_VALID_WDIVMM_BINARY) && // prevent mv
    HopRewriteUtils.isEqualSize(hi.getInput().get(0), hi.getInput().get(1)) && // not applied for vector-vector mult
    hi.getDim2() > 1 && hi.getInput().get(0).getDataType() == DataType.MATRIX && hi.getInput().get(0).getDim2() > hi.getInput().get(0).getColsInBlock() && hi.getInput().get(1) instanceof BinaryOp && HopRewriteUtils.isValidOp(((BinaryOp) hi.getInput().get(1)).getOp(), LOOKUP_VALID_WUMM_BINARY)) {
        Hop left = hi.getInput().get(1).getInput().get(0);
        Hop right = hi.getInput().get(1).getInput().get(1);
        Hop abop = null;
        // pattern 2a) matrix-scalar operations
        if (right.getDataType() == DataType.SCALAR && right instanceof LiteralOp && // pow2, mult2
        HopRewriteUtils.getDoubleValue((LiteralOp) right) == 2 && left instanceof AggBinaryOp && // BLOCKSIZE CONSTRAINT
        HopRewriteUtils.isSingleBlock(left.getInput().get(0), true)) {
            abop = left;
        } else // pattern 2b) scalar-matrix operations
        if (left.getDataType() == DataType.SCALAR && left instanceof LiteralOp && // mult2
        HopRewriteUtils.getDoubleValue((LiteralOp) left) == 2 && ((BinaryOp) hi.getInput().get(1)).getOp() == OpOp2.MULT && right instanceof AggBinaryOp && // BLOCKSIZE CONSTRAINT
        HopRewriteUtils.isSingleBlock(right.getInput().get(0), true)) {
            abop = right;
        }
        if (abop != null) {
            Hop W = hi.getInput().get(0);
            Hop U = abop.getInput().get(0);
            Hop V = abop.getInput().get(1);
            boolean mult = ((BinaryOp) hi).getOp() == OpOp2.MULT;
            OpOp2 op = ((BinaryOp) hi.getInput().get(1)).getOp();
            if (!HopRewriteUtils.isTransposeOperation(V))
                V = HopRewriteUtils.createTranspose(V);
            else
                V = V.getInput().get(0);
            hnew = new QuaternaryOp(hi.getName(), DataType.MATRIX, ValueType.DOUBLE, OpOp4.WUMM, W, U, V, mult, null, op);
            hnew.setOutputBlocksizes(W.getRowsInBlock(), W.getColsInBlock());
            hnew.refreshSizeInformation();
            appliedPattern = true;
            LOG.debug("Applied simplifyWeightedUnaryMM2 (line " + hi.getBeginLine() + ")");
        }
    }
    // relink new hop into original position
    if (hnew != null) {
        HopRewriteUtils.replaceChildReference(parent, hi, hnew, pos);
        hi = hnew;
    }
    return hi;
}
Also used : QuaternaryOp(org.apache.sysml.hops.QuaternaryOp) OpOp1(org.apache.sysml.hops.Hop.OpOp1) AggUnaryOp(org.apache.sysml.hops.AggUnaryOp) UnaryOp(org.apache.sysml.hops.UnaryOp) AggBinaryOp(org.apache.sysml.hops.AggBinaryOp) Hop(org.apache.sysml.hops.Hop) LiteralOp(org.apache.sysml.hops.LiteralOp) OpOp2(org.apache.sysml.hops.Hop.OpOp2) AggBinaryOp(org.apache.sysml.hops.AggBinaryOp) BinaryOp(org.apache.sysml.hops.BinaryOp)

Aggregations

UnaryOp (org.apache.sysml.hops.UnaryOp)42 AggUnaryOp (org.apache.sysml.hops.AggUnaryOp)39 Hop (org.apache.sysml.hops.Hop)35 LiteralOp (org.apache.sysml.hops.LiteralOp)24 AggBinaryOp (org.apache.sysml.hops.AggBinaryOp)18 BinaryOp (org.apache.sysml.hops.BinaryOp)18 DataOp (org.apache.sysml.hops.DataOp)9 IndexingOp (org.apache.sysml.hops.IndexingOp)8 ParameterizedBuiltinOp (org.apache.sysml.hops.ParameterizedBuiltinOp)6 TernaryOp (org.apache.sysml.hops.TernaryOp)6 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)5 ReorgOp (org.apache.sysml.hops.ReorgOp)5 DMLRuntimeException (org.apache.sysml.runtime.DMLRuntimeException)5 DataGenOp (org.apache.sysml.hops.DataGenOp)4 HopsException (org.apache.sysml.hops.HopsException)4 LeftIndexingOp (org.apache.sysml.hops.LeftIndexingOp)4 MatrixObject (org.apache.sysml.runtime.controlprogram.caching.MatrixObject)4 OpOp2 (org.apache.sysml.hops.Hop.OpOp2)3 CNode (org.apache.sysml.hops.codegen.cplan.CNode)3