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);
}
}
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);
}
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();
}
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;
}
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));
}
Aggregations