Search in sources :

Example 21 with RankingExpression

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

the class GbdtModelTestCase method requireThatFactoryMethodWorks.

@Test
public void requireThatFactoryMethodWorks() throws Exception {
    GbdtModel model = GbdtModel.fromXmlFile("src/test/files/gbdt.xml");
    assertEquals(10, model.trees().size());
    String exp = model.toRankingExpression();
    assertEquals(readFile("src/test/files/gbdt.expression").trim(), exp.trim());
    assertNotNull(new RankingExpression(exp));
}
Also used : RankingExpression(com.yahoo.searchlib.rankingexpression.RankingExpression) Test(org.junit.Test)

Example 22 with RankingExpression

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

the class EvaluationBenchmark method runGBDT.

private void runGBDT(int iterations, String gbdtString) throws ParseException {
    // Unoptimized...............
    double total = benchmark(new RankingExpression(gbdtString), new MapContext(), iterations, "Unoptimized");
    System.out.println("-----------------------------------------------------------------------------------------------------");
    // Tree optimized...................
    RankingExpression treeOptimized = new RankingExpression(gbdtString);
    ArrayContext treeContext = new ArrayContext(treeOptimized, true);
    ExpressionOptimizer optimizer = new ExpressionOptimizer();
    optimizer.getOptimizer(GBDTForestOptimizer.class).setEnabled(false);
    System.out.print("Tree optimizing ... ");
    OptimizationReport treeOptimizationReport = optimizer.optimize(treeOptimized, treeContext);
    System.out.println("done");
    System.out.println(treeOptimizationReport);
    double treeTotal = benchmark(treeOptimized, treeContext, iterations, "Tree optimized");
    assertEqualish(total, treeTotal);
    System.out.println("-----------------------------------------------------------------------------------------------------");
    // Forest optimized...................
    RankingExpression forestOptimized = new RankingExpression(gbdtString);
    DoubleOnlyArrayContext forestContext = new DoubleOnlyArrayContext(forestOptimized, true);
    System.out.print("Forest optimizing ... ");
    OptimizationReport forestOptimizationReport = new ExpressionOptimizer().optimize(forestOptimized, forestContext);
    System.out.println("done");
    System.out.println(forestOptimizationReport);
    double forestTotal = benchmark(forestOptimized, forestContext, iterations, "Forest optimized");
    assertEqualish(total, forestTotal);
    System.out.println("-----------------------------------------------------------------------------------------------------");
}
Also used : RankingExpression(com.yahoo.searchlib.rankingexpression.RankingExpression) GBDTForestOptimizer(com.yahoo.searchlib.rankingexpression.evaluation.gbdtoptimization.GBDTForestOptimizer)

Example 23 with RankingExpression

use of com.yahoo.searchlib.rankingexpression.RankingExpression 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)

Example 24 with RankingExpression

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

the class EvaluationTester method assertEvaluates.

public RankingExpression assertEvaluates(Value value, String expressionString, Context context, String explanation) {
    try {
        RankingExpression expression = new RankingExpression(expressionString);
        if (!explanation.isEmpty())
            explanation = explanation + ": ";
        assertEquals(explanation + 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 25 with RankingExpression

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

the class ContextReuseTestCase method testIt.

public void testIt() throws ParseException, IOException {
    // Prepare
    RankingExpression expression = new RankingExpression(IOUtils.readFile(new File("src/test/files/s-expression.vre")));
    ArrayContext contextPrototype = new ArrayContext(expression);
    new ExpressionOptimizer().optimize(expression, contextPrototype);
    // Execute
    ArrayContext context = contextPrototype.clone();
    for (String contextValueString : contextString.split("\n")) {
        String[] contextValueParts = contextValueString.split("=");
        context.put(contextValueParts[0].trim(), Double.valueOf(contextValueParts[1].trim()));
    }
    assertEquals(-2.3450294999999994, expression.evaluate(context).asDouble());
}
Also used : RankingExpression(com.yahoo.searchlib.rankingexpression.RankingExpression) ArrayContext(com.yahoo.searchlib.rankingexpression.evaluation.ArrayContext) ExpressionOptimizer(com.yahoo.searchlib.rankingexpression.evaluation.ExpressionOptimizer) File(java.io.File)

Aggregations

RankingExpression (com.yahoo.searchlib.rankingexpression.RankingExpression)34 Test (org.junit.Test)10 ParseException (com.yahoo.searchlib.rankingexpression.parser.ParseException)6 ArrayContext (com.yahoo.searchlib.rankingexpression.evaluation.ArrayContext)5 OptimizationReport (com.yahoo.searchlib.rankingexpression.evaluation.OptimizationReport)4 ExpressionOptimizer (com.yahoo.searchlib.rankingexpression.evaluation.ExpressionOptimizer)3 MapContext (com.yahoo.searchlib.rankingexpression.evaluation.MapContext)3 TensorType (com.yahoo.tensor.TensorType)3 LinkedList (java.util.LinkedList)3 GBDTForestOptimizer (com.yahoo.searchlib.rankingexpression.evaluation.gbdtoptimization.GBDTForestOptimizer)2 ExpressionNode (com.yahoo.searchlib.rankingexpression.rule.ExpressionNode)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 RankProfile (com.yahoo.searchdefinition.RankProfile)1 ExpressionFunction (com.yahoo.searchlib.rankingexpression.ExpressionFunction)1 Reference (com.yahoo.searchlib.rankingexpression.Reference)1 Context (com.yahoo.searchlib.rankingexpression.evaluation.Context)1 TensorValue (com.yahoo.searchlib.rankingexpression.evaluation.TensorValue)1 Value (com.yahoo.searchlib.rankingexpression.evaluation.Value)1 TensorFlowModel (com.yahoo.searchlib.rankingexpression.integration.tensorflow.TensorFlowModel)1