Search in sources :

Example 16 with RankingExpression

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);
            }
        }
    }
}
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 17 with RankingExpression

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);
}
Also used : RankingExpression(com.yahoo.searchlib.rankingexpression.RankingExpression) ExpressionNode(com.yahoo.searchlib.rankingexpression.rule.ExpressionNode)

Example 18 with RankingExpression

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();
}
Also used : TensorFlowModel(com.yahoo.searchlib.rankingexpression.integration.tensorflow.TensorFlowModel) RankingExpression(com.yahoo.searchlib.rankingexpression.RankingExpression) Signature(com.yahoo.searchlib.rankingexpression.integration.tensorflow.TensorFlowModel.Signature) HashSet(java.util.HashSet)

Example 19 with RankingExpression

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;
}
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 20 with RankingExpression

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());
        }
    }
}
Also used : RankingExpression(com.yahoo.searchlib.rankingexpression.RankingExpression) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

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