blob: 6e5504962c0847fa3ac5e83108c3eebe72f9ddd9 (
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
|
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package ai.vespa.rankingexpression.importer.onnx;
import ai.vespa.rankingexpression.importer.OrderedTensorType;
import ai.vespa.rankingexpression.importer.operations.IntermediateOperation;
import com.yahoo.searchlib.rankingexpression.evaluation.DoubleValue;
import com.yahoo.searchlib.rankingexpression.evaluation.StringValue;
import com.yahoo.searchlib.rankingexpression.evaluation.TensorValue;
import com.yahoo.searchlib.rankingexpression.evaluation.Value;
import onnx.Onnx;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
/**
* Converts Onnx node attributes to Vespa attribute values.
*
* @author lesters
*/
class AttributeConverter implements IntermediateOperation.AttributeMap {
private final Onnx.NodeProto node;
private AttributeConverter(Onnx.NodeProto node) {
this.node = node;
}
static AttributeConverter convert(Onnx.NodeProto node) {
return new AttributeConverter(node);
}
@Override
public Optional<Value> get(String name) {
for (Onnx.AttributeProto attr : node.getAttributeList()) {
if (attr.getName().equals(name)) {
switch (attr.getType()) {
case INT: return Optional.of(DoubleValue.frozen(attr.getI()));
case FLOAT: return Optional.of(DoubleValue.frozen(attr.getF()));
case STRING: return Optional.of(StringValue.frozen(attr.getS().toString()));
case TENSOR: return Optional.of(new TensorValue(TensorConverter.toVespaTensor(attr.getT(), TypeConverter.typeFrom(attr.getT()))));
default:
return Optional.empty();
}
}
}
return Optional.empty();
}
@Override
public Optional<Value> get(String name, OrderedTensorType type) {
return Optional.empty();
}
@Override
public Optional<List<Value>> getList(String name) {
for (Onnx.AttributeProto attr : node.getAttributeList()) {
if (attr.getName().equals(name)) {
switch (attr.getType()) {
case INTS: return Optional.of(attr.getIntsList().stream().map(DoubleValue::new).collect(Collectors.toList()));
case FLOATS: return Optional.of(attr.getFloatsList().stream().map(DoubleValue::new).collect(Collectors.toList()));
case STRINGS: return Optional.of(attr.getStringsList().stream().map((s) -> StringValue.frozen(s.toString())).collect(Collectors.toList()));
default:
return Optional.empty();
}
}
}
return Optional.empty();
}
}
|