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