summaryrefslogtreecommitdiffstats
path: root/vespajlib/src/main/java/com/yahoo/tensor/serialization/DenseBinaryFormat.java
diff options
context:
space:
mode:
authorHenning Baldersheim <balder@yahoo-inc.com>2019-04-01 14:40:24 +0200
committerHenning Baldersheim <balder@yahoo-inc.com>2019-04-01 14:40:24 +0200
commite8440bd1dbafac4ce09797bb3395b0cac54c6d82 (patch)
treece9330e6bf31907e59f197b84788623fef3d3de6 /vespajlib/src/main/java/com/yahoo/tensor/serialization/DenseBinaryFormat.java
parent14c6d045b501b11b82c19bfb1029fa4ff10ec350 (diff)
Initial skeleton for type information in serialization format.
Diffstat (limited to 'vespajlib/src/main/java/com/yahoo/tensor/serialization/DenseBinaryFormat.java')
-rw-r--r--vespajlib/src/main/java/com/yahoo/tensor/serialization/DenseBinaryFormat.java43
1 files changed, 43 insertions, 0 deletions
diff --git a/vespajlib/src/main/java/com/yahoo/tensor/serialization/DenseBinaryFormat.java b/vespajlib/src/main/java/com/yahoo/tensor/serialization/DenseBinaryFormat.java
index 46d533cc6bd..6dcc870ef94 100644
--- a/vespajlib/src/main/java/com/yahoo/tensor/serialization/DenseBinaryFormat.java
+++ b/vespajlib/src/main/java/com/yahoo/tensor/serialization/DenseBinaryFormat.java
@@ -22,14 +22,40 @@ import java.util.Optional;
*/
public class DenseBinaryFormat implements BinaryFormat {
+ static private final int DOUBLE_VALUE_TYPE = 0; // Not encoded as it is default, and you know the type when deserializing
+ static private final int FLOAT_VALUE_TYPE = 1;
+
+ enum EncodeType {NO_DEFAULT, DOUBLE_IS_DEFAULT}
+ private final EncodeType encodeType;
+ DenseBinaryFormat() {
+ encodeType = EncodeType.DOUBLE_IS_DEFAULT;
+ }
+ DenseBinaryFormat(EncodeType encodeType) {
+ this.encodeType = encodeType;
+ }
+
@Override
public void encode(GrowableByteBuffer buffer, Tensor tensor) {
if ( ! ( tensor instanceof IndexedTensor))
throw new RuntimeException("The dense format is only supported for indexed tensors");
+ encodeValueType(buffer, tensor.type().valueType());
encodeDimensions(buffer, (IndexedTensor)tensor);
encodeCells(buffer, tensor);
}
+ private void encodeValueType(GrowableByteBuffer buffer, TensorType.ValueType valueType) {
+ switch (valueType) {
+ case DOUBLE:
+ if (encodeType != EncodeType.DOUBLE_IS_DEFAULT) {
+ buffer.putInt1_4Bytes(DOUBLE_VALUE_TYPE);
+ }
+ break;
+ case FLOAT:
+ buffer.putInt1_4Bytes(FLOAT_VALUE_TYPE);
+ break;
+ }
+ }
+
private void encodeDimensions(GrowableByteBuffer buffer, IndexedTensor tensor) {
buffer.putInt1_4Bytes(tensor.type().dimensions().size());
for (int i = 0; i < tensor.type().dimensions().size(); i++) {
@@ -39,11 +65,28 @@ public class DenseBinaryFormat implements BinaryFormat {
}
private void encodeCells(GrowableByteBuffer buffer, Tensor tensor) {
+ switch (tensor.type().valueType()) {
+ case DOUBLE:
+ encodeCellsAsDouble(buffer, tensor);
+ break;
+ case FLOAT:
+ encodeCellsAsFloat(buffer, tensor);
+ break;
+ }
+ }
+
+ private void encodeCellsAsDouble(GrowableByteBuffer buffer, Tensor tensor) {
Iterator<Double> i = tensor.valueIterator();
while (i.hasNext())
buffer.putDouble(i.next());
}
+ private void encodeCellsAsFloat(GrowableByteBuffer buffer, Tensor tensor) {
+ Iterator<Double> i = tensor.valueIterator();
+ while (i.hasNext())
+ buffer.putFloat(i.next().floatValue());
+ }
+
@Override
public Tensor decode(Optional<TensorType> optionalType, GrowableByteBuffer buffer) {
TensorType type;