blob: 67ad1edc3125c478b12fd13682c7cc2ca2857591 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.searchlib.rankingexpression.integration.ml.importer.tensorflow;
import com.yahoo.searchlib.rankingexpression.integration.ml.importer.OrderedTensorType;
import com.yahoo.tensor.TensorType;
import org.tensorflow.framework.AttrValue;
import org.tensorflow.framework.NodeDef;
import org.tensorflow.framework.TensorShapeProto;
import java.util.List;
/**
* Converts and verifies TensorFlow tensor types into Vespa tensor types.
*
* @author lesters
*/
public class TypeConverter {
public static void verifyType(NodeDef node, OrderedTensorType type) {
TensorShapeProto shape = tensorFlowShape(node);
if (shape != null) {
if (shape.getDimCount() != type.rank()) {
throw new IllegalArgumentException("TensorFlow shape of '" + node.getName() + "' " +
"does not match Vespa shape");
}
for (int tensorFlowIndex = 0; tensorFlowIndex < type.dimensions().size(); ++tensorFlowIndex) {
int vespaIndex = type.dimensionMap(tensorFlowIndex);
TensorShapeProto.Dim tensorFlowDimension = shape.getDim(tensorFlowIndex);
TensorType.Dimension vespaDimension = type.type().dimensions().get(vespaIndex);
if (tensorFlowDimension.getSize() != vespaDimension.size().orElse(-1L)) {
throw new IllegalArgumentException("TensorFlow dimensions of '" + node.getName() + "' " +
"does not match Vespa dimensions");
}
}
}
}
private static TensorShapeProto tensorFlowShape(NodeDef node) {
AttrValue attrValueList = node.getAttrMap().get("_output_shapes");
if (attrValueList == null) {
throw new IllegalArgumentException("_output_shapes attribute of '" + node.getName() + "' " +
"does not exist");
}
if (attrValueList.getValueCase() != AttrValue.ValueCase.LIST) {
throw new IllegalArgumentException("_output_shapes attribute of '" + node.getName() + "' " +
"is not of expected type");
}
List<TensorShapeProto> shapeList = attrValueList.getList().getShapeList();
return shapeList.get(0); // support multiple outputs?
}
public static OrderedTensorType fromTensorFlowType(NodeDef node) {
return fromTensorFlowType(node, "d"); // standard naming convention: d0, d1, ...
}
public static OrderedTensorType fromTensorFlowType(NodeDef node, String dimensionPrefix) {
OrderedTensorType.Builder builder = new OrderedTensorType.Builder();
TensorShapeProto shape = tensorFlowShape(node);
for (int i = 0; i < shape.getDimCount(); ++ i) {
String dimensionName = dimensionPrefix + i;
TensorShapeProto.Dim tensorFlowDimension = shape.getDim(i);
if (tensorFlowDimension.getSize() >= 0) {
builder.add(TensorType.Dimension.indexed(dimensionName, tensorFlowDimension.getSize()));
} else {
builder.add(TensorType.Dimension.indexed(dimensionName));
}
}
return builder.build();
}
}
|