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);
}
}
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);
}
}
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);
}
}
}
}
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;
}
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)) + " %");
}
Aggregations