diff options
author | Jon Bratseth <bratseth@gmail.com> | 2020-09-03 12:01:39 +0200 |
---|---|---|
committer | Jon Bratseth <bratseth@gmail.com> | 2020-09-03 12:01:39 +0200 |
commit | 91b2cf49d8e25c378f4aa00833ca8245f9c1ca65 (patch) | |
tree | d316b36715016249a4db13f5aebf0805967a2dad /vespajlib/src/main/java/com/yahoo/tensor | |
parent | bed63d34ef760934ba45bb80d36699345c9416f5 (diff) |
Use the tensor type to switch tensor binary format
The binary format of a tensor should depend on the tensor type,
not the implementation type as the API permits the user choosing that
(and it may not be 1-1 anyway).
This makes this change for sparse tensors using the mixed implementation
type but not dense tensors using the mixed implementation type as that
would be more work given the unfinished state of the mixed implementation.
Diffstat (limited to 'vespajlib/src/main/java/com/yahoo/tensor')
3 files changed, 19 insertions, 12 deletions
diff --git a/vespajlib/src/main/java/com/yahoo/tensor/IndexedTensor.java b/vespajlib/src/main/java/com/yahoo/tensor/IndexedTensor.java index ad82dd6c3ac..dc17c657db9 100644 --- a/vespajlib/src/main/java/com/yahoo/tensor/IndexedTensor.java +++ b/vespajlib/src/main/java/com/yahoo/tensor/IndexedTensor.java @@ -178,9 +178,7 @@ public abstract class IndexedTensor implements Tensor { @Override public abstract IndexedTensor withType(TensorType type); - public DimensionSizes dimensionSizes() { - return dimensionSizes; - } + public DimensionSizes dimensionSizes() { return dimensionSizes; } @Override public Map<TensorAddress, Double> cells() { diff --git a/vespajlib/src/main/java/com/yahoo/tensor/MixedTensor.java b/vespajlib/src/main/java/com/yahoo/tensor/MixedTensor.java index 1ec4993bf57..f608aead347 100644 --- a/vespajlib/src/main/java/com/yahoo/tensor/MixedTensor.java +++ b/vespajlib/src/main/java/com/yahoo/tensor/MixedTensor.java @@ -152,7 +152,6 @@ public class MixedTensor implements Tensor { return index.denseSubspaceSize(); } - /** * Base class for building mixed tensors. */ diff --git a/vespajlib/src/main/java/com/yahoo/tensor/serialization/TypedBinaryFormat.java b/vespajlib/src/main/java/com/yahoo/tensor/serialization/TypedBinaryFormat.java index bcff4392c9a..5c47572c779 100644 --- a/vespajlib/src/main/java/com/yahoo/tensor/serialization/TypedBinaryFormat.java +++ b/vespajlib/src/main/java/com/yahoo/tensor/serialization/TypedBinaryFormat.java @@ -51,31 +51,41 @@ public class TypedBinaryFormat { } private static BinaryFormat getFormatEncoder(GrowableByteBuffer buffer, Tensor tensor) { - if (tensor instanceof MixedTensor && tensor.type().valueType() == TensorType.Value.DOUBLE) { + boolean hasMappedDimensions = tensor.type().dimensions().stream().anyMatch(d -> d.isMapped()); + boolean hasIndexedDimensions = tensor.type().dimensions().stream().anyMatch(d -> d.isIndexed()); + boolean isMixed = hasMappedDimensions && hasIndexedDimensions; + + // TODO: Encoding as indexed if the implementation is mixed is not yet supported so use mixed format instead + if (tensor instanceof MixedTensor && ! isMixed && hasIndexedDimensions) + isMixed = true; + + if (isMixed && tensor.type().valueType() == TensorType.Value.DOUBLE) { encodeFormatType(buffer, MIXED_BINARY_FORMAT_TYPE); return new MixedBinaryFormat(); } - if (tensor instanceof MixedTensor) { + else if (isMixed) { encodeFormatType(buffer, MIXED_BINARY_FORMAT_WITH_CELLTYPE); encodeValueType(buffer, tensor.type().valueType()); return new MixedBinaryFormat(tensor.type().valueType()); } - if (tensor instanceof IndexedTensor && tensor.type().valueType() == TensorType.Value.DOUBLE) { + else if (hasIndexedDimensions && tensor.type().valueType() == TensorType.Value.DOUBLE) { encodeFormatType(buffer, DENSE_BINARY_FORMAT_TYPE); return new DenseBinaryFormat(); } - if (tensor instanceof IndexedTensor) { + else if (hasIndexedDimensions) { encodeFormatType(buffer, DENSE_BINARY_FORMAT_WITH_CELLTYPE); encodeValueType(buffer, tensor.type().valueType()); return new DenseBinaryFormat(tensor.type().valueType()); } - if (tensor.type().valueType() == TensorType.Value.DOUBLE) { + else if (tensor.type().valueType() == TensorType.Value.DOUBLE) { encodeFormatType(buffer, SPARSE_BINARY_FORMAT_TYPE); return new SparseBinaryFormat(); } - encodeFormatType(buffer, SPARSE_BINARY_FORMAT_WITH_CELLTYPE); - encodeValueType(buffer, tensor.type().valueType()); - return new SparseBinaryFormat(tensor.type().valueType()); + else { + encodeFormatType(buffer, SPARSE_BINARY_FORMAT_WITH_CELLTYPE); + encodeValueType(buffer, tensor.type().valueType()); + return new SparseBinaryFormat(tensor.type().valueType()); + } } private static BinaryFormat getFormatDecoder(GrowableByteBuffer buffer) { |