Search in sources :

Example 16 with ExpressionNode

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

the class TensorTransformer method transformMaxAndMinFunctionNode.

/**
 * Transforms max and min functions if the first
 * argument returns a tensor type and the second argument is a valid
 * dimension in the tensor.
 */
private ExpressionNode transformMaxAndMinFunctionNode(FunctionNode node, RankProfileTransformContext context) {
    if (node.children().size() != 2) {
        return node;
    }
    ExpressionNode arg1 = node.children().get(0);
    Optional<String> dimension = dimensionName(node.children().get(1));
    if (dimension.isPresent()) {
        TensorType type = arg1.type(context.rankProfile().typeContext(context.queryProfiles()));
        if (type.dimension(dimension.get()).isPresent()) {
            return replaceMaxAndMinFunction(node);
        }
    }
    return node;
}
Also used : ExpressionNode(com.yahoo.searchlib.rankingexpression.rule.ExpressionNode) TensorType(com.yahoo.tensor.TensorType)

Example 17 with ExpressionNode

use of com.yahoo.searchlib.rankingexpression.rule.ExpressionNode 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 18 with ExpressionNode

use of com.yahoo.searchlib.rankingexpression.rule.ExpressionNode 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 19 with ExpressionNode

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

the class RankingExpressionTestCase method testIsNan.

@Test
public void testIsNan() throws ParseException {
    String strExpr = "if (isNan(attribute(foo)) == 1.0, 1.0, attribute(foo))";
    RankingExpression expr = new RankingExpression(strExpr);
    CompositeNode root = (CompositeNode) expr.getRoot();
    CompositeNode comparison = (CompositeNode) root.children().get(0);
    ExpressionNode isNan = comparison.children().get(0);
    assertTrue(isNan instanceof FunctionNode);
    assertEquals("isNan(attribute(foo))", isNan.toString());
}
Also used : CompositeNode(com.yahoo.searchlib.rankingexpression.rule.CompositeNode) ExpressionNode(com.yahoo.searchlib.rankingexpression.rule.ExpressionNode) FunctionNode(com.yahoo.searchlib.rankingexpression.rule.FunctionNode) TensorFunctionNode(com.yahoo.searchlib.rankingexpression.rule.TensorFunctionNode) Test(org.junit.Test)

Example 20 with ExpressionNode

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

the class ExpandDims method lazyGetFunction.

@Override
protected TensorFunction lazyGetFunction() {
    if (!allInputFunctionsPresent(2)) {
        return null;
    }
    // multiply with a generated tensor created from the reduced dimensions
    TensorType.Builder typeBuilder = new TensorType.Builder();
    for (String name : expandDimensions) {
        typeBuilder.indexed(name, 1);
    }
    TensorType generatedType = typeBuilder.build();
    ExpressionNode generatedExpression = new ConstantNode(new DoubleValue(1));
    Generate generatedFunction = new Generate(generatedType, new GeneratorLambdaFunctionNode(generatedType, generatedExpression).asLongListToDoubleOperator());
    return new com.yahoo.tensor.functions.Join(inputs().get(0).function().get(), generatedFunction, ScalarFunctions.multiply());
}
Also used : GeneratorLambdaFunctionNode(com.yahoo.searchlib.rankingexpression.rule.GeneratorLambdaFunctionNode) ConstantNode(com.yahoo.searchlib.rankingexpression.rule.ConstantNode) DoubleValue(com.yahoo.searchlib.rankingexpression.evaluation.DoubleValue) ExpressionNode(com.yahoo.searchlib.rankingexpression.rule.ExpressionNode) Generate(com.yahoo.tensor.functions.Generate) OrderedTensorType(com.yahoo.searchlib.rankingexpression.integration.tensorflow.importer.OrderedTensorType) TensorType(com.yahoo.tensor.TensorType)

Aggregations

ExpressionNode (com.yahoo.searchlib.rankingexpression.rule.ExpressionNode)21 TensorType (com.yahoo.tensor.TensorType)8 ArrayList (java.util.ArrayList)7 DoubleValue (com.yahoo.searchlib.rankingexpression.evaluation.DoubleValue)6 ConstantNode (com.yahoo.searchlib.rankingexpression.rule.ConstantNode)6 ReferenceNode (com.yahoo.searchlib.rankingexpression.rule.ReferenceNode)6 TensorFunctionNode (com.yahoo.searchlib.rankingexpression.rule.TensorFunctionNode)5 OrderedTensorType (com.yahoo.searchlib.rankingexpression.integration.tensorflow.importer.OrderedTensorType)4 ArithmeticNode (com.yahoo.searchlib.rankingexpression.rule.ArithmeticNode)4 CompositeNode (com.yahoo.searchlib.rankingexpression.rule.CompositeNode)4 GeneratorLambdaFunctionNode (com.yahoo.searchlib.rankingexpression.rule.GeneratorLambdaFunctionNode)4 Generate (com.yahoo.tensor.functions.Generate)4 ArithmeticOperator (com.yahoo.searchlib.rankingexpression.rule.ArithmeticOperator)3 Reduce (com.yahoo.tensor.functions.Reduce)3 TensorFunction (com.yahoo.tensor.functions.TensorFunction)3 RankingExpression (com.yahoo.searchlib.rankingexpression.RankingExpression)2 RankProfile (com.yahoo.searchdefinition.RankProfile)1 Reference (com.yahoo.searchlib.rankingexpression.Reference)1 Value (com.yahoo.searchlib.rankingexpression.evaluation.Value)1 ComparisonNode (com.yahoo.searchlib.rankingexpression.rule.ComparisonNode)1