Search in sources :

Example 1 with ArithmeticNode

use of com.yahoo.searchlib.rankingexpression.rule.ArithmeticNode in project vespa by vespa-engine.

the class Simplifier method transformArithmetic.

private ExpressionNode transformArithmetic(ArithmeticNode node) {
    if (node.children().size() > 1) {
        List<ExpressionNode> children = new ArrayList<>(node.children());
        List<ArithmeticOperator> operators = new ArrayList<>(node.operators());
        for (ArithmeticOperator operator : ArithmeticOperator.operatorsByPrecedence) transform(operator, children, operators);
        node = new ArithmeticNode(children, operators);
    }
    if (isConstant(node))
        return new ConstantNode(node.evaluate(null));
    else if (// disregarding the /0 case
    allMultiplicationOrDivision(node) && hasZero(node))
        return new ConstantNode(new DoubleValue(0));
    else
        return node;
}
Also used : ArithmeticNode(com.yahoo.searchlib.rankingexpression.rule.ArithmeticNode) ConstantNode(com.yahoo.searchlib.rankingexpression.rule.ConstantNode) DoubleValue(com.yahoo.searchlib.rankingexpression.evaluation.DoubleValue) ExpressionNode(com.yahoo.searchlib.rankingexpression.rule.ExpressionNode) ArrayList(java.util.ArrayList) ArithmeticOperator(com.yahoo.searchlib.rankingexpression.rule.ArithmeticOperator)

Example 2 with ArithmeticNode

use of com.yahoo.searchlib.rankingexpression.rule.ArithmeticNode in project vespa by vespa-engine.

the class GBDTForestOptimizer method optimize.

/**
 * Optimize the given node, if it is the root of a gdbt forest. Otherwise do nothing and return false
 */
private boolean optimize(ExpressionNode node, List<Double> forest) {
    if (node instanceof GBDTNode) {
        addTo(forest, (GBDTNode) node);
        currentTreesOptimized++;
        return true;
    }
    if (!(node instanceof ArithmeticNode)) {
        return false;
    }
    ArithmeticNode aNode = (ArithmeticNode) node;
    for (ArithmeticOperator op : aNode.operators()) {
        if (op != ArithmeticOperator.PLUS) {
            return false;
        }
    }
    for (ExpressionNode child : aNode.children()) {
        if (!optimize(child, forest)) {
            return false;
        }
    }
    return true;
}
Also used : ArithmeticNode(com.yahoo.searchlib.rankingexpression.rule.ArithmeticNode) ExpressionNode(com.yahoo.searchlib.rankingexpression.rule.ExpressionNode) ArithmeticOperator(com.yahoo.searchlib.rankingexpression.rule.ArithmeticOperator)

Example 3 with ArithmeticNode

use of com.yahoo.searchlib.rankingexpression.rule.ArithmeticNode in project vespa by vespa-engine.

the class Reshape method unrollTensorExpression.

private static ExpressionNode unrollTensorExpression(TensorType type) {
    if (type.rank() == 0) {
        return new ConstantNode(DoubleValue.zero);
    }
    List<ExpressionNode> children = new ArrayList<>();
    List<ArithmeticOperator> operators = new ArrayList<>();
    int size = 1;
    for (int i = type.dimensions().size() - 1; i >= 0; --i) {
        TensorType.Dimension dimension = type.dimensions().get(i);
        children.add(0, new ReferenceNode(dimension.name()));
        if (size > 1) {
            operators.add(0, ArithmeticOperator.MULTIPLY);
            children.add(0, new ConstantNode(new DoubleValue(size)));
        }
        size *= TensorConverter.dimensionSize(dimension);
        if (i > 0) {
            operators.add(0, ArithmeticOperator.PLUS);
        }
    }
    return new ArithmeticNode(children, operators);
}
Also used : ArithmeticNode(com.yahoo.searchlib.rankingexpression.rule.ArithmeticNode) ConstantNode(com.yahoo.searchlib.rankingexpression.rule.ConstantNode) ReferenceNode(com.yahoo.searchlib.rankingexpression.rule.ReferenceNode) DoubleValue(com.yahoo.searchlib.rankingexpression.evaluation.DoubleValue) ExpressionNode(com.yahoo.searchlib.rankingexpression.rule.ExpressionNode) ArrayList(java.util.ArrayList) ArithmeticOperator(com.yahoo.searchlib.rankingexpression.rule.ArithmeticOperator) OrderedTensorType(com.yahoo.searchlib.rankingexpression.integration.tensorflow.importer.OrderedTensorType) TensorType(com.yahoo.tensor.TensorType)

Example 4 with ArithmeticNode

use of com.yahoo.searchlib.rankingexpression.rule.ArithmeticNode in project vespa by vespa-engine.

the class Simplifier method transform.

private void transform(ArithmeticOperator operator, List<ExpressionNode> children, List<ArithmeticOperator> operators) {
    int i = 0;
    while (i < children.size() - 1) {
        if (!operators.get(i).equals(operator)) {
            i++;
            continue;
        }
        ExpressionNode child1 = children.get(i);
        ExpressionNode child2 = children.get(i + 1);
        if (isConstant(child1) && isConstant(child2) && hasPrecedence(operators, i)) {
            Value evaluated = new ArithmeticNode(child1, operators.remove(i), child2).evaluate(null);
            children.set(i, new ConstantNode(evaluated.freeze()));
            children.remove(i + 1);
        } else {
            // try the next index
            i++;
        }
    }
}
Also used : ArithmeticNode(com.yahoo.searchlib.rankingexpression.rule.ArithmeticNode) ConstantNode(com.yahoo.searchlib.rankingexpression.rule.ConstantNode) ExpressionNode(com.yahoo.searchlib.rankingexpression.rule.ExpressionNode) DoubleValue(com.yahoo.searchlib.rankingexpression.evaluation.DoubleValue) Value(com.yahoo.searchlib.rankingexpression.evaluation.Value)

Example 5 with ArithmeticNode

use of com.yahoo.searchlib.rankingexpression.rule.ArithmeticNode in project vespa by vespa-engine.

the class RankingExpressionTestCase method testProgrammaticBuilding.

@Test
public void testProgrammaticBuilding() throws ParseException {
    ReferenceNode input = new ReferenceNode("input");
    ReferenceNode constant = new ReferenceNode("constant");
    ArithmeticNode product = new ArithmeticNode(input, ArithmeticOperator.MULTIPLY, constant);
    Reduce sum = new Reduce(new TensorFunctionNode.TensorFunctionExpressionNode(product), Reduce.Aggregator.sum);
    RankingExpression expression = new RankingExpression(new TensorFunctionNode(sum));
    RankingExpression expected = new RankingExpression("sum(input * constant)");
    assertEquals(expected.toString(), expression.toString());
}
Also used : ArithmeticNode(com.yahoo.searchlib.rankingexpression.rule.ArithmeticNode) ReferenceNode(com.yahoo.searchlib.rankingexpression.rule.ReferenceNode) TensorFunctionNode(com.yahoo.searchlib.rankingexpression.rule.TensorFunctionNode) Reduce(com.yahoo.tensor.functions.Reduce) Test(org.junit.Test)

Aggregations

ArithmeticNode (com.yahoo.searchlib.rankingexpression.rule.ArithmeticNode)6 ExpressionNode (com.yahoo.searchlib.rankingexpression.rule.ExpressionNode)4 DoubleValue (com.yahoo.searchlib.rankingexpression.evaluation.DoubleValue)3 ArithmeticOperator (com.yahoo.searchlib.rankingexpression.rule.ArithmeticOperator)3 ConstantNode (com.yahoo.searchlib.rankingexpression.rule.ConstantNode)3 ReferenceNode (com.yahoo.searchlib.rankingexpression.rule.ReferenceNode)2 ArrayList (java.util.ArrayList)2 Test (org.junit.Test)2 RankingExpression (com.yahoo.searchlib.rankingexpression.RankingExpression)1 Value (com.yahoo.searchlib.rankingexpression.evaluation.Value)1 OrderedTensorType (com.yahoo.searchlib.rankingexpression.integration.tensorflow.importer.OrderedTensorType)1 TensorFunctionNode (com.yahoo.searchlib.rankingexpression.rule.TensorFunctionNode)1 TensorType (com.yahoo.tensor.TensorType)1 Reduce (com.yahoo.tensor.functions.Reduce)1