Search in sources :

Example 6 with ReferenceNode

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

the class MapEvaluationTypeContext method tensorFeatureType.

/**
 * There are two features which returns the (non-empty) tensor type: tensorFromLabels and tensorFromWeightedSet.
 * This returns the type of those features if this is a reference to either of them, or empty otherwise.
 */
private Optional<TensorType> tensorFeatureType(Reference reference) {
    if (!reference.name().equals("tensorFromLabels") && !reference.name().equals("tensorFromWeightedSet"))
        return Optional.empty();
    if (reference.arguments().size() != 1 && reference.arguments().size() != 2)
        throw new IllegalArgumentException(reference.name() + " must have one or two arguments");
    ExpressionNode arg0 = reference.arguments().expressions().get(0);
    if (!(arg0 instanceof ReferenceNode) || !FeatureNames.isSimpleFeature(((ReferenceNode) arg0).reference()))
        throw new IllegalArgumentException("The first argument of " + reference.name() + " must be a simple feature, not " + arg0);
    String dimension;
    if (reference.arguments().size() > 1) {
        ExpressionNode arg1 = reference.arguments().expressions().get(1);
        if ((!(arg1 instanceof ReferenceNode) || !(((ReferenceNode) arg1).reference().isIdentifier())) && (!(arg1 instanceof NameNode)))
            throw new IllegalArgumentException("The second argument of " + reference.name() + " must be a dimension name, not " + arg1);
        dimension = reference.arguments().expressions().get(1).toString();
    } else {
        // default
        dimension = ((ReferenceNode) arg0).reference().arguments().expressions().get(0).toString();
    }
    return Optional.of(new TensorType.Builder().mapped(dimension).build());
}
Also used : NameNode(com.yahoo.searchlib.rankingexpression.rule.NameNode) ReferenceNode(com.yahoo.searchlib.rankingexpression.rule.ReferenceNode) ExpressionNode(com.yahoo.searchlib.rankingexpression.rule.ExpressionNode) TensorType(com.yahoo.tensor.TensorType)

Example 7 with ReferenceNode

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

the class TensorTransformer method replaceMaxAndMinFunction.

private ExpressionNode replaceMaxAndMinFunction(FunctionNode node) {
    ExpressionNode arg1 = node.children().get(0);
    ExpressionNode arg2 = node.children().get(1);
    TensorFunctionNode.TensorFunctionExpressionNode expression = TensorFunctionNode.wrapArgument(arg1);
    Reduce.Aggregator aggregator = Reduce.Aggregator.valueOf(node.getFunction().name());
    String dimension = ((ReferenceNode) arg2).getName();
    return new TensorFunctionNode(new Reduce(expression, aggregator, dimension));
}
Also used : TensorFunctionNode(com.yahoo.searchlib.rankingexpression.rule.TensorFunctionNode) ReferenceNode(com.yahoo.searchlib.rankingexpression.rule.ReferenceNode) ExpressionNode(com.yahoo.searchlib.rankingexpression.rule.ExpressionNode) Reduce(com.yahoo.tensor.functions.Reduce)

Example 8 with ReferenceNode

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

the class ReferenceTestCase method testSimple.

@Test
public void testSimple() {
    assertTrue(new Reference("foo", new Arguments(new ReferenceNode("arg")), null).isSimple());
    assertTrue(new Reference("foo", new Arguments(new ReferenceNode("arg")), "out").isSimple());
    assertTrue(new Reference("foo", new Arguments(new NameNode("arg")), "out").isSimple());
    assertFalse(new Reference("foo", new Arguments(), null).isSimple());
}
Also used : NameNode(com.yahoo.searchlib.rankingexpression.rule.NameNode) ReferenceNode(com.yahoo.searchlib.rankingexpression.rule.ReferenceNode) Arguments(com.yahoo.searchlib.rankingexpression.rule.Arguments) Test(org.junit.Test)

Example 9 with ReferenceNode

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

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

ReferenceNode (com.yahoo.searchlib.rankingexpression.rule.ReferenceNode)12 ExpressionNode (com.yahoo.searchlib.rankingexpression.rule.ExpressionNode)6 NameNode (com.yahoo.searchlib.rankingexpression.rule.NameNode)4 TensorFunctionNode (com.yahoo.searchlib.rankingexpression.rule.TensorFunctionNode)4 ArrayList (java.util.ArrayList)3 Test (org.junit.Test)3 Arguments (com.yahoo.searchlib.rankingexpression.rule.Arguments)2 ArithmeticNode (com.yahoo.searchlib.rankingexpression.rule.ArithmeticNode)2 CompositeNode (com.yahoo.searchlib.rankingexpression.rule.CompositeNode)2 TensorType (com.yahoo.tensor.TensorType)2 Reduce (com.yahoo.tensor.functions.Reduce)2 RankProfile (com.yahoo.searchdefinition.RankProfile)1 DoubleValue (com.yahoo.searchlib.rankingexpression.evaluation.DoubleValue)1 TensorValue (com.yahoo.searchlib.rankingexpression.evaluation.TensorValue)1 Value (com.yahoo.searchlib.rankingexpression.evaluation.Value)1 OrderedTensorType (com.yahoo.searchlib.rankingexpression.integration.tensorflow.importer.OrderedTensorType)1 ParseException (com.yahoo.searchlib.rankingexpression.parser.ParseException)1 RankingExpressionParser (com.yahoo.searchlib.rankingexpression.parser.RankingExpressionParser)1 TokenMgrError (com.yahoo.searchlib.rankingexpression.parser.TokenMgrError)1 ArithmeticOperator (com.yahoo.searchlib.rankingexpression.rule.ArithmeticOperator)1