use of com.yahoo.searchlib.rankingexpression.RankingExpression 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.RankingExpression in project vespa by vespa-engine.
the class MacroShadower method transform.
@Override
public RankingExpression transform(RankingExpression expression, RankProfileTransformContext context) {
String name = expression.getName();
ExpressionNode node = expression.getRoot();
ExpressionNode result = transform(node, context);
return new RankingExpression(name, result);
}
use of com.yahoo.searchlib.rankingexpression.RankingExpression in project vespa by vespa-engine.
the class TensorFlowFeatureConverter method transformFromTensorFlowModel.
private ExpressionNode transformFromTensorFlowModel(ModelStore store, RankProfile profile, QueryProfileRegistry queryProfiles) {
TensorFlowModel model = importedModels.computeIfAbsent(store.arguments().modelPath(), k -> tensorFlowImporter.importModel(store.arguments().modelName(), store.tensorFlowModelDir()));
// Add constants
Set<String> constantsReplacedByMacros = new HashSet<>();
model.smallConstants().forEach((k, v) -> transformSmallConstant(store, profile, k, v));
model.largeConstants().forEach((k, v) -> transformLargeConstant(store, profile, queryProfiles, constantsReplacedByMacros, k, v));
// Find the specified expression
Signature signature = chooseSignature(model, store.arguments().signature());
String output = chooseOutput(signature, store.arguments().output());
RankingExpression expression = model.expressions().get(output);
expression = replaceConstantsByMacros(expression, constantsReplacedByMacros);
verifyRequiredMacros(expression, model, profile, queryProfiles);
addGeneratedMacros(model, profile);
reduceBatchDimensions(expression, model, profile, queryProfiles);
model.macros().forEach((k, v) -> transformGeneratedMacro(store, profile, constantsReplacedByMacros, k, v));
store.writeConverted(expression);
return expression.getRoot();
}
use of com.yahoo.searchlib.rankingexpression.RankingExpression 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.RankingExpression in project vespa by vespa-engine.
the class RankProfile method parseMacros.
/**
* Passes the contents of macros on to parser. Then put all the implied rank properties
* from those macros into the profile's props map.
*/
private void parseMacros() throws ParseException {
for (Map.Entry<String, Macro> e : getMacros().entrySet()) {
String macroName = e.getKey();
Macro macro = e.getValue();
if (macro.getRankingExpression() == null) {
RankingExpression expr = parseRankingExpression(macroName, macro.getTextualExpression());
macro.setRankingExpression(expr);
macro.setTextualExpression(expr.getRoot().toString());
}
}
}
Aggregations