Search in sources :

Example 31 with Tensor

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

the class TestableTensorFlowModel method assertEqualResult.

public void assertEqualResult(String inputName, String operationName) {
    Tensor tfResult = tensorFlowExecute(tensorFlowModel, inputName, operationName);
    Context context = contextFrom(model);
    Tensor placeholder = placeholderArgument();
    context.put(inputName, new TensorValue(placeholder));
    model.macros().forEach((k, v) -> evaluateMacro(context, model, k));
    Tensor vespaResult = model.expressions().get(operationName).evaluate(context).asTensor();
    assertEquals("Operation '" + operationName + "' produces equal results", tfResult, vespaResult);
}
Also used : Context(com.yahoo.searchlib.rankingexpression.evaluation.Context) MapContext(com.yahoo.searchlib.rankingexpression.evaluation.MapContext) TensorValue(com.yahoo.searchlib.rankingexpression.evaluation.TensorValue) Tensor(com.yahoo.tensor.Tensor)

Example 32 with Tensor

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

the class TestableTensorFlowModel method tensorFlowExecute.

private Tensor tensorFlowExecute(SavedModelBundle model, String inputName, String operationName) {
    Session.Runner runner = model.session().runner();
    FloatBuffer fb = FloatBuffer.allocate(d0Size * d1Size);
    for (int i = 0; i < d1Size; ++i) {
        fb.put(i, (float) (i * 1.0 / d1Size));
    }
    org.tensorflow.Tensor<?> placeholder = org.tensorflow.Tensor.create(new long[] { d0Size, d1Size }, fb);
    runner.feed(inputName, placeholder);
    List<org.tensorflow.Tensor<?>> results = runner.fetch(operationName).run();
    assertEquals(1, results.size());
    return TensorConverter.toVespaTensor(results.get(0));
}
Also used : Tensor(com.yahoo.tensor.Tensor) FloatBuffer(java.nio.FloatBuffer) Session(org.tensorflow.Session)

Example 33 with Tensor

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

the class TensorConformanceTest method testCase.

private boolean testCase(String test, int count) {
    try {
        ObjectMapper mapper = new ObjectMapper();
        JsonNode node = mapper.readTree(test);
        if (node.has("num_tests")) {
            Assert.assertEquals(node.get("num_tests").asInt(), count);
            return true;
        }
        if (!node.has("expression")) {
            // ignore
            return true;
        }
        String expression = node.get("expression").asText();
        MapContext context = getInput(node.get("inputs"));
        Tensor expect = getTensor(node.get("result").get("expect").asText());
        Tensor result = evaluate(expression, context);
        boolean equals = Tensor.equals(result, expect);
        if (!equals) {
            System.out.println(count + " : Tensors not equal. Result: " + result.toString() + " Expected: " + expect.toString() + " -> expression \"" + expression + "\"");
        }
        return equals;
    } catch (Exception e) {
        System.out.println(count + " : " + e.toString());
    }
    return false;
}
Also used : Tensor(com.yahoo.tensor.Tensor) JsonNode(com.fasterxml.jackson.databind.JsonNode) MapContext(com.yahoo.searchlib.rankingexpression.evaluation.MapContext) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ParseException(com.yahoo.searchlib.rankingexpression.parser.ParseException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Example 34 with Tensor

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

the class TensorConformanceTest method getInput.

private MapContext getInput(JsonNode inputs) {
    MapContext context = new MapContext();
    for (Iterator<String> i = inputs.fieldNames(); i.hasNext(); ) {
        String name = i.next();
        String value = inputs.get(name).asText();
        Tensor tensor = getTensor(value);
        context.put(name, new TensorValue(tensor));
    }
    return context;
}
Also used : TensorValue(com.yahoo.searchlib.rankingexpression.evaluation.TensorValue) Tensor(com.yahoo.tensor.Tensor) MapContext(com.yahoo.searchlib.rankingexpression.evaluation.MapContext)

Example 35 with Tensor

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

the class Join method mappedHashJoin.

private Tensor mappedHashJoin(Tensor a, Tensor b, TensorType joinedType) {
    TensorType commonDimensionType = commonDimensions(a, b);
    if (commonDimensionType.dimensions().isEmpty()) {
        // fallback
        return mappedGeneralJoin(a, b, joinedType);
    }
    boolean swapTensors = a.size() > b.size();
    if (swapTensors) {
        Tensor temp = a;
        a = b;
        b = temp;
    }
    // Map dimension indexes to common and joined type
    int[] aIndexesInCommon = mapIndexes(commonDimensionType, a.type());
    int[] bIndexesInCommon = mapIndexes(commonDimensionType, b.type());
    int[] aIndexesInJoined = mapIndexes(a.type(), joinedType);
    int[] bIndexesInJoined = mapIndexes(b.type(), joinedType);
    // Iterate once through the smaller tensor and construct a hash map for common dimensions
    Map<TensorAddress, List<Tensor.Cell>> aCellsByCommonAddress = new HashMap<>();
    for (Iterator<Tensor.Cell> cellIterator = a.cellIterator(); cellIterator.hasNext(); ) {
        Tensor.Cell aCell = cellIterator.next();
        TensorAddress partialCommonAddress = partialCommonAddress(aCell, aIndexesInCommon);
        aCellsByCommonAddress.putIfAbsent(partialCommonAddress, new ArrayList<>());
        aCellsByCommonAddress.get(partialCommonAddress).add(aCell);
    }
    // Iterate once through the larger tensor and use the hash map to find joinable cells
    Tensor.Builder builder = Tensor.Builder.of(joinedType);
    for (Iterator<Tensor.Cell> cellIterator = b.cellIterator(); cellIterator.hasNext(); ) {
        Tensor.Cell bCell = cellIterator.next();
        TensorAddress partialCommonAddress = partialCommonAddress(bCell, bIndexesInCommon);
        for (Tensor.Cell aCell : aCellsByCommonAddress.getOrDefault(partialCommonAddress, Collections.emptyList())) {
            TensorAddress combinedAddress = joinAddresses(aCell.getKey(), aIndexesInJoined, bCell.getKey(), bIndexesInJoined, joinedType);
            // not combinable
            if (combinedAddress == null)
                continue;
            double combinedValue = swapTensors ? combinator.applyAsDouble(bCell.getValue(), aCell.getValue()) : combinator.applyAsDouble(aCell.getValue(), bCell.getValue());
            builder.cell(combinedAddress, combinedValue);
        }
    }
    return builder.build();
}
Also used : TensorAddress(com.yahoo.tensor.TensorAddress) IndexedTensor(com.yahoo.tensor.IndexedTensor) Tensor(com.yahoo.tensor.Tensor) HashMap(java.util.HashMap) TensorType(com.yahoo.tensor.TensorType) ArrayList(java.util.ArrayList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList)

Aggregations

Tensor (com.yahoo.tensor.Tensor)58 Test (org.junit.Test)26 TensorType (com.yahoo.tensor.TensorType)17 IndexedTensor (com.yahoo.tensor.IndexedTensor)10 TensorAddress (com.yahoo.tensor.TensorAddress)7 MixedTensor (com.yahoo.tensor.MixedTensor)5 HashMap (java.util.HashMap)5 Map (java.util.Map)4 MapContext (com.yahoo.searchlib.rankingexpression.evaluation.MapContext)3 TensorValue (com.yahoo.searchlib.rankingexpression.evaluation.TensorValue)3 OrderedTensorType (com.yahoo.searchlib.rankingexpression.integration.tensorflow.importer.OrderedTensorType)3 DimensionSizes (com.yahoo.tensor.DimensionSizes)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 ImmutableList (com.google.common.collect.ImmutableList)2 MappedTensor (com.yahoo.tensor.MappedTensor)2 IOException (java.io.IOException)2 List (java.util.List)2 GrowableByteBuffer (com.yahoo.io.GrowableByteBuffer)1 Path (com.yahoo.path.Path)1