aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJon Bratseth <bratseth@verizonmedia.com>2019-04-26 14:24:17 +0200
committerJon Bratseth <bratseth@verizonmedia.com>2019-04-26 14:24:17 +0200
commitae5d5e058f1bb2fd197886ac374ce807065fdb77 (patch)
tree2966fda95d45f68ccf212e9fe8884528b7ce23f6
parent94b4b3ad837f9d3f9d43b158c4de8475ff2c2a2d (diff)
Build tensors purely with floats
-rw-r--r--linguistics/src/main/java/com/yahoo/language/process/Transformer.java2
-rw-r--r--vespajlib/abi-spec.json71
-rw-r--r--vespajlib/src/main/java/com/yahoo/tensor/IndexedDoubleTensor.java25
-rw-r--r--vespajlib/src/main/java/com/yahoo/tensor/IndexedFloatTensor.java33
-rw-r--r--vespajlib/src/main/java/com/yahoo/tensor/IndexedTensor.java13
-rw-r--r--vespajlib/src/main/java/com/yahoo/tensor/MappedTensor.java10
-rw-r--r--vespajlib/src/main/java/com/yahoo/tensor/MixedTensor.java15
-rw-r--r--vespajlib/src/main/java/com/yahoo/tensor/Tensor.java21
8 files changed, 169 insertions, 21 deletions
diff --git a/linguistics/src/main/java/com/yahoo/language/process/Transformer.java b/linguistics/src/main/java/com/yahoo/language/process/Transformer.java
index 2b84c8ab570..46f3c060d4e 100644
--- a/linguistics/src/main/java/com/yahoo/language/process/Transformer.java
+++ b/linguistics/src/main/java/com/yahoo/language/process/Transformer.java
@@ -6,7 +6,7 @@ import com.yahoo.language.Language;
/**
* Interface for providers of text transformations such as accent removal.
*
- * @authorMathias Mølster Lidal
+ * @author Mathias Mølster Lidal
*/
public interface Transformer {
diff --git a/vespajlib/abi-spec.json b/vespajlib/abi-spec.json
index e4b6162eeca..c7363dbbd86 100644
--- a/vespajlib/abi-spec.json
+++ b/vespajlib/abi-spec.json
@@ -706,27 +706,77 @@
],
"fields": []
},
- "com.yahoo.tensor.IndexedTensor$BoundBuilder": {
- "superClass": "com.yahoo.tensor.IndexedTensor$Builder",
+ "com.yahoo.tensor.IndexedDoubleTensor$BoundDoubleBuilder": {
+ "superClass": "com.yahoo.tensor.IndexedTensor$BoundBuilder",
+ "interfaces": [],
+ "attributes": [
+ "public"
+ ],
+ "methods": [
+ "public varargs com.yahoo.tensor.IndexedTensor$BoundBuilder cell(float, long[])",
+ "public varargs com.yahoo.tensor.IndexedTensor$BoundBuilder cell(double, long[])",
+ "public com.yahoo.tensor.Tensor$Builder$CellBuilder cell()",
+ "public com.yahoo.tensor.IndexedTensor$Builder cell(com.yahoo.tensor.TensorAddress, float)",
+ "public com.yahoo.tensor.IndexedTensor$Builder cell(com.yahoo.tensor.TensorAddress, double)",
+ "public com.yahoo.tensor.IndexedTensor build()",
+ "public com.yahoo.tensor.IndexedTensor$Builder cell(com.yahoo.tensor.Tensor$Cell, float)",
+ "public com.yahoo.tensor.IndexedTensor$Builder cell(com.yahoo.tensor.Tensor$Cell, double)",
+ "public void cellByDirectIndex(long, float)",
+ "public void cellByDirectIndex(long, double)",
+ "public bridge synthetic com.yahoo.tensor.IndexedTensor$Builder cell(float, long[])",
+ "public bridge synthetic com.yahoo.tensor.IndexedTensor$Builder cell(double, long[])",
+ "public bridge synthetic com.yahoo.tensor.Tensor build()",
+ "public bridge synthetic com.yahoo.tensor.Tensor$Builder cell(com.yahoo.tensor.Tensor$Cell, float)",
+ "public bridge synthetic com.yahoo.tensor.Tensor$Builder cell(com.yahoo.tensor.Tensor$Cell, double)",
+ "public bridge synthetic com.yahoo.tensor.Tensor$Builder cell(float, long[])",
+ "public bridge synthetic com.yahoo.tensor.Tensor$Builder cell(double, long[])",
+ "public bridge synthetic com.yahoo.tensor.Tensor$Builder cell(com.yahoo.tensor.TensorAddress, float)",
+ "public bridge synthetic com.yahoo.tensor.Tensor$Builder cell(com.yahoo.tensor.TensorAddress, double)"
+ ],
+ "fields": []
+ },
+ "com.yahoo.tensor.IndexedFloatTensor$BoundFloatBuilder": {
+ "superClass": "com.yahoo.tensor.IndexedTensor$BoundBuilder",
"interfaces": [],
"attributes": [
"public"
],
"methods": [
"public varargs com.yahoo.tensor.IndexedTensor$BoundBuilder cell(double, long[])",
+ "public varargs com.yahoo.tensor.IndexedTensor$BoundBuilder cell(float, long[])",
"public com.yahoo.tensor.Tensor$Builder$CellBuilder cell()",
"public com.yahoo.tensor.IndexedTensor$Builder cell(com.yahoo.tensor.TensorAddress, double)",
+ "public com.yahoo.tensor.IndexedTensor$Builder cell(com.yahoo.tensor.TensorAddress, float)",
"public com.yahoo.tensor.IndexedTensor build()",
"public com.yahoo.tensor.IndexedTensor$Builder cell(com.yahoo.tensor.Tensor$Cell, double)",
+ "public com.yahoo.tensor.IndexedTensor$Builder cell(com.yahoo.tensor.Tensor$Cell, float)",
"public void cellByDirectIndex(long, double)",
+ "public void cellByDirectIndex(long, float)",
+ "public bridge synthetic com.yahoo.tensor.IndexedTensor$Builder cell(float, long[])",
"public bridge synthetic com.yahoo.tensor.IndexedTensor$Builder cell(double, long[])",
"public bridge synthetic com.yahoo.tensor.Tensor build()",
+ "public bridge synthetic com.yahoo.tensor.Tensor$Builder cell(com.yahoo.tensor.Tensor$Cell, float)",
"public bridge synthetic com.yahoo.tensor.Tensor$Builder cell(com.yahoo.tensor.Tensor$Cell, double)",
+ "public bridge synthetic com.yahoo.tensor.Tensor$Builder cell(float, long[])",
"public bridge synthetic com.yahoo.tensor.Tensor$Builder cell(double, long[])",
+ "public bridge synthetic com.yahoo.tensor.Tensor$Builder cell(com.yahoo.tensor.TensorAddress, float)",
"public bridge synthetic com.yahoo.tensor.Tensor$Builder cell(com.yahoo.tensor.TensorAddress, double)"
],
"fields": []
},
+ "com.yahoo.tensor.IndexedTensor$BoundBuilder": {
+ "superClass": "com.yahoo.tensor.IndexedTensor$Builder",
+ "interfaces": [],
+ "attributes": [
+ "public",
+ "abstract"
+ ],
+ "methods": [
+ "public abstract void cellByDirectIndex(long, double)",
+ "public abstract void cellByDirectIndex(long, float)"
+ ],
+ "fields": []
+ },
"com.yahoo.tensor.IndexedTensor$Builder": {
"superClass": "java.lang.Object",
"interfaces": [
@@ -740,9 +790,11 @@
"public static com.yahoo.tensor.IndexedTensor$Builder of(com.yahoo.tensor.TensorType)",
"public static com.yahoo.tensor.IndexedTensor$Builder of(com.yahoo.tensor.TensorType, com.yahoo.tensor.DimensionSizes)",
"public varargs abstract com.yahoo.tensor.IndexedTensor$Builder cell(double, long[])",
+ "public varargs abstract com.yahoo.tensor.IndexedTensor$Builder cell(float, long[])",
"public com.yahoo.tensor.TensorType type()",
"public abstract com.yahoo.tensor.IndexedTensor build()",
"public bridge synthetic com.yahoo.tensor.Tensor build()",
+ "public bridge synthetic com.yahoo.tensor.Tensor$Builder cell(float, long[])",
"public bridge synthetic com.yahoo.tensor.Tensor$Builder cell(double, long[])"
],
"fields": []
@@ -828,11 +880,15 @@
"public static com.yahoo.tensor.MappedTensor$Builder of(com.yahoo.tensor.TensorType)",
"public com.yahoo.tensor.Tensor$Builder$CellBuilder cell()",
"public com.yahoo.tensor.TensorType type()",
+ "public com.yahoo.tensor.MappedTensor$Builder cell(com.yahoo.tensor.TensorAddress, float)",
"public com.yahoo.tensor.MappedTensor$Builder cell(com.yahoo.tensor.TensorAddress, double)",
+ "public varargs com.yahoo.tensor.MappedTensor$Builder cell(float, long[])",
"public varargs com.yahoo.tensor.MappedTensor$Builder cell(double, long[])",
"public com.yahoo.tensor.MappedTensor build()",
"public bridge synthetic com.yahoo.tensor.Tensor build()",
+ "public bridge synthetic com.yahoo.tensor.Tensor$Builder cell(float, long[])",
"public bridge synthetic com.yahoo.tensor.Tensor$Builder cell(double, long[])",
+ "public bridge synthetic com.yahoo.tensor.Tensor$Builder cell(com.yahoo.tensor.TensorAddress, float)",
"public bridge synthetic com.yahoo.tensor.Tensor$Builder cell(com.yahoo.tensor.TensorAddress, double)"
],
"fields": []
@@ -869,6 +925,7 @@
],
"methods": [
"public long denseSubspaceSize()",
+ "public com.yahoo.tensor.Tensor$Builder cell(com.yahoo.tensor.TensorAddress, float)",
"public com.yahoo.tensor.Tensor$Builder cell(com.yahoo.tensor.TensorAddress, double)",
"public com.yahoo.tensor.Tensor$Builder block(com.yahoo.tensor.TensorAddress, double[])",
"public com.yahoo.tensor.MixedTensor build()",
@@ -888,6 +945,7 @@
"methods": [
"public static com.yahoo.tensor.MixedTensor$Builder of(com.yahoo.tensor.TensorType)",
"public com.yahoo.tensor.TensorType type()",
+ "public varargs com.yahoo.tensor.Tensor$Builder cell(float, long[])",
"public varargs com.yahoo.tensor.Tensor$Builder cell(double, long[])",
"public com.yahoo.tensor.Tensor$Builder$CellBuilder cell()",
"public abstract com.yahoo.tensor.MixedTensor build()",
@@ -916,6 +974,7 @@
"public"
],
"methods": [
+ "public com.yahoo.tensor.Tensor$Builder cell(com.yahoo.tensor.TensorAddress, float)",
"public com.yahoo.tensor.Tensor$Builder cell(com.yahoo.tensor.TensorAddress, double)",
"public com.yahoo.tensor.MixedTensor build()",
"public void trackBounds(com.yahoo.tensor.TensorAddress)",
@@ -981,7 +1040,8 @@
"methods": [
"public com.yahoo.tensor.Tensor$Builder$CellBuilder label(java.lang.String, java.lang.String)",
"public com.yahoo.tensor.Tensor$Builder$CellBuilder label(java.lang.String, long)",
- "public com.yahoo.tensor.Tensor$Builder value(double)"
+ "public com.yahoo.tensor.Tensor$Builder value(double)",
+ "public com.yahoo.tensor.Tensor$Builder value(float)"
],
"fields": []
},
@@ -999,8 +1059,11 @@
"public abstract com.yahoo.tensor.TensorType type()",
"public abstract com.yahoo.tensor.Tensor$Builder$CellBuilder cell()",
"public abstract com.yahoo.tensor.Tensor$Builder cell(com.yahoo.tensor.TensorAddress, double)",
+ "public abstract com.yahoo.tensor.Tensor$Builder cell(com.yahoo.tensor.TensorAddress, float)",
"public varargs abstract com.yahoo.tensor.Tensor$Builder cell(double, long[])",
+ "public varargs abstract com.yahoo.tensor.Tensor$Builder cell(float, long[])",
"public com.yahoo.tensor.Tensor$Builder cell(com.yahoo.tensor.Tensor$Cell, double)",
+ "public com.yahoo.tensor.Tensor$Builder cell(com.yahoo.tensor.Tensor$Cell, float)",
"public abstract com.yahoo.tensor.Tensor build()"
],
"fields": []
@@ -1016,6 +1079,8 @@
"methods": [
"public com.yahoo.tensor.TensorAddress getKey()",
"public java.lang.Double getValue()",
+ "public float getFloatValue()",
+ "public double getDoubleValue()",
"public java.lang.Double setValue(java.lang.Double)",
"public boolean equals(java.lang.Object)",
"public int hashCode()",
diff --git a/vespajlib/src/main/java/com/yahoo/tensor/IndexedDoubleTensor.java b/vespajlib/src/main/java/com/yahoo/tensor/IndexedDoubleTensor.java
index 5d5c2be4576..c9e5be31c15 100644
--- a/vespajlib/src/main/java/com/yahoo/tensor/IndexedDoubleTensor.java
+++ b/vespajlib/src/main/java/com/yahoo/tensor/IndexedDoubleTensor.java
@@ -52,6 +52,11 @@ class IndexedDoubleTensor extends IndexedTensor {
}
@Override
+ public IndexedTensor.BoundBuilder cell(float value, long ... indexes) {
+ return cell((double)value, indexes);
+ }
+
+ @Override
public IndexedTensor.BoundBuilder cell(double value, long ... indexes) {
values[(int)toValueIndex(indexes, sizes())] = value;
return this;
@@ -63,6 +68,11 @@ class IndexedDoubleTensor extends IndexedTensor {
}
@Override
+ public Builder cell(TensorAddress address, float value) {
+ return cell(address, (double)value);
+ }
+
+ @Override
public Builder cell(TensorAddress address, double value) {
values[(int)toValueIndex(address, sizes())] = value;
return this;
@@ -77,6 +87,11 @@ class IndexedDoubleTensor extends IndexedTensor {
}
@Override
+ public Builder cell(Cell cell, float value) {
+ return cell(cell, (double)value);
+ }
+
+ @Override
public Builder cell(Cell cell, double value) {
long directIndex = cell.getDirectIndex();
if (directIndex >= 0) // optimization
@@ -86,11 +101,11 @@ class IndexedDoubleTensor extends IndexedTensor {
return this;
}
- /**
- * Set a cell value by the index in the internal layout of this cell.
- * This requires knowledge of the internal layout of cells in this implementation, and should therefore
- * probably not be used (but when it can be used it is fast).
- */
+ @Override
+ public void cellByDirectIndex(long index, float value) {
+ cellByDirectIndex(index, (double)value);
+ }
+
@Override
public void cellByDirectIndex(long index, double value) {
values[(int)index] = value;
diff --git a/vespajlib/src/main/java/com/yahoo/tensor/IndexedFloatTensor.java b/vespajlib/src/main/java/com/yahoo/tensor/IndexedFloatTensor.java
index 1e2aed1f5b4..4c8af0cbfd6 100644
--- a/vespajlib/src/main/java/com/yahoo/tensor/IndexedFloatTensor.java
+++ b/vespajlib/src/main/java/com/yahoo/tensor/IndexedFloatTensor.java
@@ -53,7 +53,12 @@ class IndexedFloatTensor extends IndexedTensor {
@Override
public IndexedTensor.BoundBuilder cell(double value, long ... indexes) {
- values[(int)toValueIndex(indexes, sizes())] = (float)value;
+ return cell((float)value, indexes);
+ }
+
+ @Override
+ public IndexedTensor.BoundBuilder cell(float value, long ... indexes) {
+ values[(int)toValueIndex(indexes, sizes())] = value;
return this;
}
@@ -64,7 +69,12 @@ class IndexedFloatTensor extends IndexedTensor {
@Override
public Builder cell(TensorAddress address, double value) {
- values[(int)toValueIndex(address, sizes())] = (float)value;
+ return cell(address, (float)value);
+ }
+
+ @Override
+ public Builder cell(TensorAddress address, float value) {
+ values[(int)toValueIndex(address, sizes())] = value;
return this;
}
@@ -78,22 +88,27 @@ class IndexedFloatTensor extends IndexedTensor {
@Override
public Builder cell(Cell cell, double value) {
+ return cell(cell, (float)value);
+ }
+
+ @Override
+ public Builder cell(Cell cell, float value) {
long directIndex = cell.getDirectIndex();
if (directIndex >= 0) // optimization
- values[(int)directIndex] = (float)value;
+ values[(int)directIndex] = value;
else
super.cell(cell, value);
return this;
}
- /**
- * Set a cell value by the index in the internal layout of this cell.
- * This requires knowledge of the internal layout of cells in this implementation, and should therefore
- * probably not be used (but when it can be used it is fast).
- */
@Override
public void cellByDirectIndex(long index, double value) {
- values[(int)index] = (float)value;
+ cellByDirectIndex(index, (float)value);
+ }
+
+ @Override
+ public void cellByDirectIndex(long index, float value) {
+ values[(int)index] = value;
}
}
diff --git a/vespajlib/src/main/java/com/yahoo/tensor/IndexedTensor.java b/vespajlib/src/main/java/com/yahoo/tensor/IndexedTensor.java
index 8e2223def83..07375cfa604 100644
--- a/vespajlib/src/main/java/com/yahoo/tensor/IndexedTensor.java
+++ b/vespajlib/src/main/java/com/yahoo/tensor/IndexedTensor.java
@@ -243,6 +243,7 @@ public abstract class IndexedTensor implements Tensor {
}
public abstract Builder cell(double value, long ... indexes);
+ public abstract Builder cell(float value, long ... indexes);
@Override
public TensorType type() { return type; }
@@ -275,6 +276,8 @@ public abstract class IndexedTensor implements Tensor {
public abstract void cellByDirectIndex(long index, double value);
+ public abstract void cellByDirectIndex(long index, float value);
+
}
/**
@@ -353,6 +356,11 @@ public abstract class IndexedTensor implements Tensor {
}
@Override
+ public Builder cell(TensorAddress address, float value) {
+ return cell(address, (double)value);
+ }
+
+ @Override
public Builder cell(TensorAddress address, double value) {
long[] indexes = new long[address.size()];
for (int i = 0; i < address.size(); i++) {
@@ -362,6 +370,11 @@ public abstract class IndexedTensor implements Tensor {
return this;
}
+ @Override
+ public Builder cell(float value, long... indexes) {
+ return cell((double)value, indexes);
+ }
+
/**
* Set a value using an index API. The number of indexes must be the same as the dimensions in the type of this.
* Values can be written in any order but all values needed to make this dense must be provided
diff --git a/vespajlib/src/main/java/com/yahoo/tensor/MappedTensor.java b/vespajlib/src/main/java/com/yahoo/tensor/MappedTensor.java
index 22ceed22d3e..693c4b5f2b0 100644
--- a/vespajlib/src/main/java/com/yahoo/tensor/MappedTensor.java
+++ b/vespajlib/src/main/java/com/yahoo/tensor/MappedTensor.java
@@ -115,12 +115,22 @@ public class MappedTensor implements Tensor {
public TensorType type() { return type; }
@Override
+ public Builder cell(TensorAddress address, float value) {
+ return cell(address, (double)value);
+ }
+
+ @Override
public Builder cell(TensorAddress address, double value) {
cells.put(address, value);
return this;
}
@Override
+ public Builder cell(float value, long... labels) {
+ return cell((double)value, labels);
+ }
+
+ @Override
public Builder cell(double value, long... labels) {
cells.put(TensorAddress.of(labels), value);
return this;
diff --git a/vespajlib/src/main/java/com/yahoo/tensor/MixedTensor.java b/vespajlib/src/main/java/com/yahoo/tensor/MixedTensor.java
index c06cb2a0986..95f64cec0c1 100644
--- a/vespajlib/src/main/java/com/yahoo/tensor/MixedTensor.java
+++ b/vespajlib/src/main/java/com/yahoo/tensor/MixedTensor.java
@@ -193,6 +193,11 @@ public class MixedTensor implements Tensor {
}
@Override
+ public Tensor.Builder cell(float value, long... labels) {
+ return cell((double)value, labels);
+ }
+
+ @Override
public Tensor.Builder cell(double value, long... labels) {
throw new UnsupportedOperationException("Not implemented.");
}
@@ -236,6 +241,11 @@ public class MixedTensor implements Tensor {
}
@Override
+ public Tensor.Builder cell(TensorAddress address, float value) {
+ return cell(address, (double)value);
+ }
+
+ @Override
public Tensor.Builder cell(TensorAddress address, double value) {
TensorAddress sparsePart = index.sparsePartialAddress(address);
long denseOffset = index.denseOffset(address);
@@ -293,6 +303,11 @@ public class MixedTensor implements Tensor {
}
@Override
+ public Tensor.Builder cell(TensorAddress address, float value) {
+ return cell(address, (double)value);
+ }
+
+ @Override
public Tensor.Builder cell(TensorAddress address, double value) {
cells.put(address, value);
trackBounds(address);
diff --git a/vespajlib/src/main/java/com/yahoo/tensor/Tensor.java b/vespajlib/src/main/java/com/yahoo/tensor/Tensor.java
index eb16801c306..ebb341147cf 100644
--- a/vespajlib/src/main/java/com/yahoo/tensor/Tensor.java
+++ b/vespajlib/src/main/java/com/yahoo/tensor/Tensor.java
@@ -370,9 +370,9 @@ public interface Tensor {
class Cell implements Map.Entry<TensorAddress, Double> {
private final TensorAddress address;
- private final Double value;
+ private final Number value;
- Cell(TensorAddress address, Double value) {
+ Cell(TensorAddress address, Number value) {
this.address = address;
this.value = value;
}
@@ -387,8 +387,15 @@ public interface Tensor {
*/
long getDirectIndex() { return -1; }
+ /** Returns the value as a double */
@Override
- public Double getValue() { return value; }
+ public Double getValue() { return value.doubleValue(); }
+
+ /** Returns the value as a float */
+ public float getFloatValue() { return value.floatValue(); }
+
+ /** Returns the value as a double */
+ public double getDoubleValue() { return value.doubleValue(); }
@Override
public Double setValue(Double value) {
@@ -446,9 +453,11 @@ public interface Tensor {
/** Add a cell */
Builder cell(TensorAddress address, double value);
+ Builder cell(TensorAddress address, float value);
/** Add a cell */
Builder cell(double value, long ... labels);
+ Builder cell(float value, long ... labels);
/**
* Add a cell
@@ -459,6 +468,9 @@ public interface Tensor {
default Builder cell(Cell cell, double value) {
return cell(cell.getKey(), value);
}
+ default Builder cell(Cell cell, float value) {
+ return cell(cell.getKey(), value);
+ }
Tensor build();
@@ -484,6 +496,9 @@ public interface Tensor {
public Builder value(double cellValue) {
return tensorBuilder.cell(addressBuilder.build(), cellValue);
}
+ public Builder value(float cellValue) {
+ return tensorBuilder.cell(addressBuilder.build(), cellValue);
+ }
}