Search in sources :

Example 6 with TensorType

use of com.yahoo.tensor.TensorType in project vespa by vespa-engine.

the class TensorFlowFeatureConverter method verifyRequiredMacros.

/**
 * Verify that the macros referred in the given expression exists in the given rank profile,
 * and return tensors of the types specified in requiredMacros.
 */
private void verifyRequiredMacros(RankingExpression expression, TensorFlowModel model, RankProfile profile, QueryProfileRegistry queryProfiles) {
    Set<String> macroNames = new HashSet<>();
    addMacroNamesIn(expression.getRoot(), macroNames, model);
    for (String macroName : macroNames) {
        TensorType requiredType = model.requiredMacros().get(macroName);
        // Not a required macro
        if (requiredType == null)
            continue;
        RankProfile.Macro macro = profile.getMacros().get(macroName);
        if (macro == null)
            throw new IllegalArgumentException("Model refers Placeholder '" + macroName + "' of type " + requiredType + " but this macro is not present in " + profile);
        // TODO: We should verify this in the (function reference(s) this is invoked (starting from first/second
        // phase and summary features), as it may only resolve correctly given those bindings
        // Or, probably better, annotate the macros with type constraints here and verify during general
        // type verification
        TensorType actualType = macro.getRankingExpression().getRoot().type(profile.typeContext(queryProfiles));
        if (actualType == null)
            throw new IllegalArgumentException("Model refers Placeholder '" + macroName + "' of type " + requiredType + " which must be produced by a macro in the rank profile, but " + "this macro references a feature which is not declared");
        if (!actualType.isAssignableTo(requiredType))
            throw new IllegalArgumentException("Model refers Placeholder '" + macroName + "' of type " + requiredType + " which must be produced by a macro in the rank profile, but " + "this macro produces type " + actualType);
    }
}
Also used : TensorType(com.yahoo.tensor.TensorType) RankProfile(com.yahoo.searchdefinition.RankProfile) HashSet(java.util.HashSet)

Example 7 with TensorType

use of com.yahoo.tensor.TensorType in project vespa by vespa-engine.

the class MixedBinaryFormatTestCase method testTwoMappedSerialization.

@Test
public void testTwoMappedSerialization() {
    TensorType type = new TensorType.Builder().mapped("x").mapped("y").build();
    Tensor tensor = MixedTensor.Builder.of(type).cell().label("x", "0").label("y", "0").value(1).cell().label("x", "0").label("y", "1").value(2).cell().label("x", "1").label("y", "0").value(4).cell().label("x", "1").label("y", "1").value(5).cell().label("x", "1").label("y", "2").value(6).build();
    assertSerialization(tensor);
}
Also used : MixedTensor(com.yahoo.tensor.MixedTensor) Tensor(com.yahoo.tensor.Tensor) TensorType(com.yahoo.tensor.TensorType) Test(org.junit.Test)

Example 8 with TensorType

use of com.yahoo.tensor.TensorType in project vespa by vespa-engine.

the class SparseBinaryFormat method decode.

@Override
public Tensor decode(Optional<TensorType> optionalType, GrowableByteBuffer buffer) {
    TensorType type;
    if (optionalType.isPresent()) {
        type = optionalType.get();
        TensorType serializedType = decodeType(buffer);
        if (!serializedType.isAssignableTo(type))
            throw new IllegalArgumentException("Type/instance mismatch: A tensor of type " + serializedType + " cannot be assigned to type " + type);
    } else {
        type = decodeType(buffer);
    }
    Tensor.Builder builder = Tensor.Builder.of(type);
    decodeCells(buffer, builder, type);
    return builder.build();
}
Also used : Tensor(com.yahoo.tensor.Tensor) TensorType(com.yahoo.tensor.TensorType)

Example 9 with TensorType

use of com.yahoo.tensor.TensorType in project vespa by vespa-engine.

the class VariableTensor method type.

@Override
public <NAMETYPE extends TypeContext.Name> TensorType type(TypeContext<NAMETYPE> context) {
    TensorType givenType = context.getType(name);
    if (givenType == null)
        return null;
    verifyType(givenType);
    return givenType;
}
Also used : TensorType(com.yahoo.tensor.TensorType)

Example 10 with TensorType

use of com.yahoo.tensor.TensorType in project vespa by vespa-engine.

the class IfNode method type.

@Override
public TensorType type(TypeContext<Reference> context) {
    TensorType trueType = trueExpression.type(context);
    TensorType falseType = falseExpression.type(context);
    return trueType.dimensionwiseGeneralizationWith(falseType).orElseThrow(() -> new IllegalArgumentException("An if expression must produce compatible types in both " + "alternatives, but the 'true' type is " + trueType + " while the " + "'false' type is " + falseType));
}
Also used : TensorType(com.yahoo.tensor.TensorType)

Aggregations

TensorType (com.yahoo.tensor.TensorType)38 Tensor (com.yahoo.tensor.Tensor)18 ExpressionNode (com.yahoo.searchlib.rankingexpression.rule.ExpressionNode)8 IndexedTensor (com.yahoo.tensor.IndexedTensor)7 MixedTensor (com.yahoo.tensor.MixedTensor)6 TensorAddress (com.yahoo.tensor.TensorAddress)6 Test (org.junit.Test)6 DoubleValue (com.yahoo.searchlib.rankingexpression.evaluation.DoubleValue)4 OrderedTensorType (com.yahoo.searchlib.rankingexpression.integration.tensorflow.importer.OrderedTensorType)4 ConstantNode (com.yahoo.searchlib.rankingexpression.rule.ConstantNode)4 GeneratorLambdaFunctionNode (com.yahoo.searchlib.rankingexpression.rule.GeneratorLambdaFunctionNode)4 Generate (com.yahoo.tensor.functions.Generate)4 List (java.util.List)4 ImmutableList (com.google.common.collect.ImmutableList)3 RankProfile (com.yahoo.searchdefinition.RankProfile)3 RankingExpression (com.yahoo.searchlib.rankingexpression.RankingExpression)3 DimensionSizes (com.yahoo.tensor.DimensionSizes)3 Reduce (com.yahoo.tensor.functions.Reduce)3 TensorFunction (com.yahoo.tensor.functions.TensorFunction)3 ArrayList (java.util.ArrayList)3