Search in sources :

Example 1 with ParseException

use of com.yahoo.searchlib.rankingexpression.parser.ParseException in project vespa by vespa-engine.

the class NeuralNetEvaluationTestCase method assertEvaluates.

private RankingExpression assertEvaluates(Value value, String expressionString, Context context) {
    try {
        RankingExpression expression = new RankingExpression(expressionString);
        assertEquals(expression.toString(), value, expression.evaluate(context));
        return expression;
    } catch (ParseException e) {
        throw new RuntimeException(e);
    }
}
Also used : RankingExpression(com.yahoo.searchlib.rankingexpression.RankingExpression) ParseException(com.yahoo.searchlib.rankingexpression.parser.ParseException)

Example 2 with ParseException

use of com.yahoo.searchlib.rankingexpression.parser.ParseException in project vespa by vespa-engine.

the class TypeResolutionTestCase method assertIncompatibleType.

private void assertIncompatibleType(String expression, TypeContext<Reference> context) {
    try {
        new RankingExpression(expression).type(context);
        fail("Expected type incompatibility exception");
    } catch (IllegalArgumentException expected) {
        assertEquals("An if expression must produce compatible types in both alternatives, " + "but the 'true' type is tensor(x[]) while the 'false' type is tensor(y[])", expected.getMessage());
    } catch (ParseException e) {
        throw new RuntimeException(e);
    }
}
Also used : RankingExpression(com.yahoo.searchlib.rankingexpression.RankingExpression) ParseException(com.yahoo.searchlib.rankingexpression.parser.ParseException)

Example 3 with ParseException

use of com.yahoo.searchlib.rankingexpression.parser.ParseException in project vespa by vespa-engine.

the class TensorFlowImporter method importRankingExpression.

private static void importRankingExpression(TensorFlowModel model, TensorFlowOperation operation) {
    if (operation.function().isPresent()) {
        String name = operation.node().getName();
        if (!model.expressions().containsKey(operation.node().getName())) {
            TensorFunction function = operation.function().get();
            // Make sure output adheres to standard naming convention
            if (isSignatureOutput(model, operation)) {
                OrderedTensorType operationType = operation.type().get();
                OrderedTensorType standardNamingType = OrderedTensorType.fromTensorFlowType(operation.node());
                if (!operationType.equals(standardNamingType)) {
                    List<String> renameFrom = operationType.dimensionNames();
                    List<String> renameTo = standardNamingType.dimensionNames();
                    function = new Rename(function, renameFrom, renameTo);
                }
            }
            try {
                // We add all intermediate nodes imported as separate expressions. Only
                // those referenced  in a signature output will be used. We parse the
                // TensorFunction here to convert it to a RankingExpression tree.
                model.expression(name, new RankingExpression(name, function.toString()));
            } catch (ParseException e) {
                throw new RuntimeException("Tensorflow function " + function + " cannot be parsed as a ranking expression", e);
            }
        }
    }
}
Also used : RankingExpression(com.yahoo.searchlib.rankingexpression.RankingExpression) TensorFunction(com.yahoo.tensor.functions.TensorFunction) OrderedTensorType(com.yahoo.searchlib.rankingexpression.integration.tensorflow.importer.OrderedTensorType) ParseException(com.yahoo.searchlib.rankingexpression.parser.ParseException) Rename(com.yahoo.tensor.functions.Rename)

Example 4 with ParseException

use of com.yahoo.searchlib.rankingexpression.parser.ParseException in project vespa by vespa-engine.

the class MapEvaluationTypeContext method getType.

@Override
public TensorType getType(Reference reference) {
    // A reference to a macro argument?
    Optional<String> binding = boundIdentifier(reference);
    if (binding.isPresent()) {
        try {
            // than their string values requires deeper changes
            return new RankingExpression(binding.get()).type(this);
        } catch (ParseException e) {
            throw new IllegalArgumentException(e);
        }
    }
    // A reference to an attribute, query or constant feature?
    if (FeatureNames.isSimpleFeature(reference)) {
        // The argument may be a local identifier bound to the actual value
        String argument = reference.simpleArgument().get();
        reference = Reference.simple(reference.name(), bindings.getOrDefault(argument, argument));
        return featureTypes.getOrDefault(reference, defaultTypeOf(reference));
    }
    // A reference to a function?
    Optional<ExpressionFunction> function = functionInvocation(reference);
    if (function.isPresent()) {
        return function.get().getBody().type(this.withBindings(bind(function.get().arguments(), reference.arguments())));
    }
    // A reference to a feature which returns a tensor?
    Optional<TensorType> featureTensorType = tensorFeatureType(reference);
    if (featureTensorType.isPresent()) {
        return featureTensorType.get();
    }
    // all match features
    return TensorType.empty;
}
Also used : RankingExpression(com.yahoo.searchlib.rankingexpression.RankingExpression) ExpressionFunction(com.yahoo.searchlib.rankingexpression.ExpressionFunction) ParseException(com.yahoo.searchlib.rankingexpression.parser.ParseException) TensorType(com.yahoo.tensor.TensorType)

Example 5 with ParseException

use of com.yahoo.searchlib.rankingexpression.parser.ParseException in project vespa by vespa-engine.

the class EvaluationBenchmark method runNativeComparison.

public void runNativeComparison(int iterations) {
    oul("Running native expression...");
    MapContext arguments = new MapContext();
    arguments.put("one", 1d);
    out("  warming up...");
    double nativeTotal = 0;
    for (int i = 0; i < iterations / 5; i++) {
        arguments.put("i", (double) i);
        nativeTotal += nativeExpression(arguments);
    }
    oul("done");
    out("  running " + iterations + " iterations...");
    long startTime = System.currentTimeMillis();
    for (int i = 0; i < iterations; i++) {
        arguments.put("i", (double) i);
        nativeTotal += nativeExpression(arguments);
    }
    long nativeTotalTime = System.currentTimeMillis() - startTime;
    oul("done");
    oul("  Total time running native:     " + nativeTotalTime + " ms (" + iterations / nativeTotalTime + " expressions/ms)");
    oul("Running ranking expression...");
    RankingExpression expression;
    try {
        expression = new RankingExpression(comparisonExpression);
    } catch (ParseException e) {
        throw new RuntimeException(e);
    }
    out("  warming up...");
    double rankingTotal = 0;
    for (int i = 0; i < iterations / 5; i++) {
        arguments.put("i", (double) i);
        rankingTotal += expression.evaluate(arguments).asDouble();
    }
    oul("done");
    out("  running " + iterations + " iterations...");
    startTime = System.currentTimeMillis();
    for (int i = 0; i < iterations; i++) {
        arguments.put("i", (double) i);
        rankingTotal += expression.evaluate(arguments).asDouble();
    }
    long rankingTotalTime = System.currentTimeMillis() - startTime;
    if (rankingTotal != nativeTotal)
        throw new IllegalStateException("Expressions are not the same, native: " + nativeTotal + " rankingExpression: " + rankingTotal);
    oul("done");
    oul("  Total time running expression: " + rankingTotalTime + " ms (" + iterations / rankingTotalTime + " expressions/ms)");
    oul("Expression % of max possible speed: " + ((int) ((100 * nativeTotalTime) / rankingTotalTime)) + " %");
}
Also used : RankingExpression(com.yahoo.searchlib.rankingexpression.RankingExpression) ParseException(com.yahoo.searchlib.rankingexpression.parser.ParseException)

Aggregations

ParseException (com.yahoo.searchlib.rankingexpression.parser.ParseException)8 RankingExpression (com.yahoo.searchlib.rankingexpression.RankingExpression)6 ArrayList (java.util.ArrayList)2 ExpressionFunction (com.yahoo.searchlib.rankingexpression.ExpressionFunction)1 OrderedTensorType (com.yahoo.searchlib.rankingexpression.integration.tensorflow.importer.OrderedTensorType)1 RankingExpressionParser (com.yahoo.searchlib.rankingexpression.parser.RankingExpressionParser)1 TokenMgrError (com.yahoo.searchlib.rankingexpression.parser.TokenMgrError)1 ReferenceNode (com.yahoo.searchlib.rankingexpression.rule.ReferenceNode)1 TensorType (com.yahoo.tensor.TensorType)1 Rename (com.yahoo.tensor.functions.Rename)1 TensorFunction (com.yahoo.tensor.functions.TensorFunction)1 BufferedReader (java.io.BufferedReader)1 FileReader (java.io.FileReader)1 IOException (java.io.IOException)1