diff options
author | Jon Bratseth <bratseth@verizonmedia.com> | 2019-04-26 14:55:04 +0200 |
---|---|---|
committer | Jon Bratseth <bratseth@verizonmedia.com> | 2019-04-26 14:55:04 +0200 |
commit | e92b8dd81cfc469d42f858785919964baf8afb0e (patch) | |
tree | 9dee2f63092b51082f842b9b84b740c2f4ebfdea /vespajlib | |
parent | f0f7f4962e6339ad2b4fbd293e89df86a6ec7a0a (diff) |
Encode directly as float
Diffstat (limited to 'vespajlib')
4 files changed, 25 insertions, 28 deletions
diff --git a/vespajlib/src/main/java/com/yahoo/tensor/IndexedDoubleTensor.java b/vespajlib/src/main/java/com/yahoo/tensor/IndexedDoubleTensor.java index c9e5be31c15..285837a1bc6 100644 --- a/vespajlib/src/main/java/com/yahoo/tensor/IndexedDoubleTensor.java +++ b/vespajlib/src/main/java/com/yahoo/tensor/IndexedDoubleTensor.java @@ -22,17 +22,13 @@ class IndexedDoubleTensor extends IndexedTensor { return values.length; } - /** - * Returns the value at the given index by direct lookup. Only use - * if you know the underlying data layout. - * - * @param valueIndex the direct index into the underlying data. - * @throws IndexOutOfBoundsException if index is out of bounds - */ @Override public double get(long valueIndex) { return values[(int)valueIndex]; } @Override + public float getFloat(long valueIndex) { return (float)get(valueIndex); } + + @Override public IndexedTensor withType(TensorType type) { throwOnIncompatibleType(type); return new IndexedDoubleTensor(type, dimensionSizes(), values); diff --git a/vespajlib/src/main/java/com/yahoo/tensor/IndexedFloatTensor.java b/vespajlib/src/main/java/com/yahoo/tensor/IndexedFloatTensor.java index 4c8af0cbfd6..8f8c24c8421 100644 --- a/vespajlib/src/main/java/com/yahoo/tensor/IndexedFloatTensor.java +++ b/vespajlib/src/main/java/com/yahoo/tensor/IndexedFloatTensor.java @@ -22,15 +22,11 @@ class IndexedFloatTensor extends IndexedTensor { return values.length; } - /** - * Returns the value at the given index by direct lookup. Only use - * if you know the underlying data layout. - * - * @param valueIndex the direct index into the underlying data. - * @throws IndexOutOfBoundsException if index is out of bounds - */ @Override - public double get(long valueIndex) { return values[(int)valueIndex]; } + public double get(long valueIndex) { return getFloat(valueIndex); } + + @Override + public float getFloat(long valueIndex) { return values[(int)valueIndex]; } @Override public IndexedTensor withType(TensorType type) { diff --git a/vespajlib/src/main/java/com/yahoo/tensor/IndexedTensor.java b/vespajlib/src/main/java/com/yahoo/tensor/IndexedTensor.java index 07375cfa604..f6af1cf0ed2 100644 --- a/vespajlib/src/main/java/com/yahoo/tensor/IndexedTensor.java +++ b/vespajlib/src/main/java/com/yahoo/tensor/IndexedTensor.java @@ -110,7 +110,7 @@ public abstract class IndexedTensor implements Tensor { } /** - * Returns the value at the given index by direct lookup. Only use + * Returns the value at the given index as a double by direct lookup. Only use * if you know the underlying data layout. * * @param valueIndex the direct index into the underlying data. @@ -118,6 +118,15 @@ public abstract class IndexedTensor implements Tensor { */ public abstract double get(long valueIndex); + /** + * Returns the value at the given index as a float by direct lookup. Only use + * if you know the underlying data layout. + * + * @param valueIndex the direct index into the underlying data. + * @throws IndexOutOfBoundsException if index is out of bounds + */ + public abstract float getFloat(long valueIndex); + static long toValueIndex(long[] indexes, DimensionSizes sizes) { if (indexes.length == 1) return indexes[0]; // for speed if (indexes.length == 0) return 0; // for speed 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 ec8c7de1e72..0cec09157fb 100644 --- a/vespajlib/src/main/java/com/yahoo/tensor/serialization/DenseBinaryFormat.java +++ b/vespajlib/src/main/java/com/yahoo/tensor/serialization/DenseBinaryFormat.java @@ -38,7 +38,7 @@ public class DenseBinaryFormat implements BinaryFormat { if ( ! ( tensor instanceof IndexedTensor)) throw new RuntimeException("The dense format is only supported for indexed tensors"); encodeDimensions(buffer, (IndexedTensor)tensor); - encodeCells(buffer, tensor); + encodeCells(buffer, (IndexedTensor)tensor); } private void encodeDimensions(GrowableByteBuffer buffer, IndexedTensor tensor) { @@ -49,25 +49,21 @@ public class DenseBinaryFormat implements BinaryFormat { } } - private void encodeCells(GrowableByteBuffer buffer, Tensor tensor) { + private void encodeCells(GrowableByteBuffer buffer, IndexedTensor tensor) { switch (serializationValueType) { case DOUBLE: encodeDoubleCells(tensor, buffer); break; case FLOAT: encodeFloatCells(tensor, buffer); break; } } - private void encodeDoubleCells(Tensor tensor, GrowableByteBuffer buffer) { - Iterator<Double> i = tensor.valueIterator(); - while (i.hasNext()) { - buffer.putDouble(i.next()); - } + private void encodeDoubleCells(IndexedTensor tensor, GrowableByteBuffer buffer) { + for (int i = 0; i < tensor.size(); i++) + buffer.putDouble(tensor.get(i)); } - private void encodeFloatCells(Tensor tensor, GrowableByteBuffer buffer) { - Iterator<Double> i = tensor.valueIterator(); // TODO: floatValueIterator - while (i.hasNext()) { - buffer.putFloat(i.next().floatValue()); - } + private void encodeFloatCells(IndexedTensor tensor, GrowableByteBuffer buffer) { + for (int i = 0; i < tensor.size(); i++) + buffer.putFloat(tensor.getFloat(i)); } @Override |