use of com.yahoo.tensor.Tensor in project vespa by vespa-engine.
the class Join method generalSubspaceJoin.
private Tensor generalSubspaceJoin(Tensor subspace, Tensor superspace, TensorType joinedType, boolean reversedArgumentOrder) {
int[] subspaceIndexes = subspaceIndexes(superspace.type(), subspace.type());
Tensor.Builder builder = Tensor.Builder.of(joinedType);
for (Iterator<Tensor.Cell> i = superspace.cellIterator(); i.hasNext(); ) {
Map.Entry<TensorAddress, Double> supercell = i.next();
TensorAddress subaddress = mapAddressToSubspace(supercell.getKey(), subspaceIndexes);
double subspaceValue = subspace.get(subaddress);
if (!Double.isNaN(subspaceValue))
builder.cell(supercell.getKey(), reversedArgumentOrder ? combinator.applyAsDouble(supercell.getValue(), subspaceValue) : combinator.applyAsDouble(subspaceValue, supercell.getValue()));
}
return builder.build();
}
use of com.yahoo.tensor.Tensor in project vespa by vespa-engine.
the class Concat method evaluate.
@Override
public <NAMETYPE extends TypeContext.Name> Tensor evaluate(EvaluationContext<NAMETYPE> context) {
Tensor a = argumentA.evaluate(context);
Tensor b = argumentB.evaluate(context);
a = ensureIndexedDimension(dimension, a);
b = ensureIndexedDimension(dimension, b);
// If you get an exception here you have implemented a mixed tensor
IndexedTensor aIndexed = (IndexedTensor) a;
IndexedTensor bIndexed = (IndexedTensor) b;
TensorType concatType = type(a.type(), b.type());
DimensionSizes concatSize = concatSize(concatType, aIndexed, bIndexed, dimension);
Tensor.Builder builder = Tensor.Builder.of(concatType, concatSize);
long aDimensionLength = aIndexed.type().indexOfDimension(dimension).map(d -> aIndexed.dimensionSizes().size(d)).orElseThrow(RuntimeException::new);
int[] aToIndexes = mapIndexes(a.type(), concatType);
int[] bToIndexes = mapIndexes(b.type(), concatType);
concatenateTo(aIndexed, bIndexed, aDimensionLength, concatType, aToIndexes, bToIndexes, builder);
concatenateTo(bIndexed, aIndexed, 0, concatType, bToIndexes, aToIndexes, builder);
return builder.build();
}
use of com.yahoo.tensor.Tensor in project vespa by vespa-engine.
the class SlimeSummaryTestCase method testDecoding.
@Test
public void testDecoding() {
Tensor tensor1 = Tensor.from("tensor(x{},y{}):{{x:foo,y:bar}:0.1}");
Tensor tensor2 = Tensor.from("tensor(x[],y[1]):{{x:0,y:0}:-0.3}");
String summary_cf = "file:src/test/java/com/yahoo/prelude/fastsearch/summary.cfg";
DocsumDefinitionSet set = createDocsumDefinitionSet(summary_cf);
byte[] docsum = makeDocsum(tensor1, tensor2);
FastHit hit = new FastHit();
assertNull(set.lazyDecode("default", docsum, hit));
assertThat(hit.getField("integer_field"), equalTo(4));
assertThat(hit.getField("short_field"), equalTo((short) 2));
assertThat(hit.getField("byte_field"), equalTo((byte) 1));
assertThat(hit.getField("float_field"), equalTo(4.5f));
assertThat(hit.getField("double_field"), equalTo(8.75));
assertThat(hit.getField("int64_field"), equalTo(8L));
assertThat(hit.getField("string_field"), equalTo("string_value"));
assertThat(hit.getField("data_field"), instanceOf(RawData.class));
assertThat(hit.getField("data_field").toString(), equalTo("data_value"));
assertThat(hit.getField("longstring_field"), equalTo((Object) "longstring_value"));
assertThat(hit.getField("longdata_field"), instanceOf(RawData.class));
assertThat(hit.getField("longdata_field").toString(), equalTo("longdata_value"));
assertThat(hit.getField("xmlstring_field"), instanceOf(XMLString.class));
assertThat(hit.getField("xmlstring_field").toString(), equalTo("<tag>xmlstring_value</tag>"));
if (hit.getField("jsonstring_field") instanceof JSONString) {
JSONString jstr = (JSONString) hit.getField("jsonstring_field");
assertThat(jstr.getContent(), equalTo("{\"foo\":1,\"bar\":2}"));
assertThat(jstr.getParsedJSON(), notNullValue());
com.yahoo.data.access.Inspectable obj = jstr;
com.yahoo.data.access.Inspector value = obj.inspect();
assertThat(value.field("foo").asLong(), equalTo(1L));
assertThat(value.field("bar").asLong(), equalTo(2L));
} else {
StructuredData sdata = (StructuredData) hit.getField("jsonstring_field");
assertThat(sdata.toJson(), equalTo("{\"foo\":1,\"bar\":2}"));
com.yahoo.data.access.Inspectable obj = sdata;
com.yahoo.data.access.Inspector value = obj.inspect();
assertThat(value.field("foo").asLong(), equalTo(1L));
assertThat(value.field("bar").asLong(), equalTo(2L));
}
assertEquals(tensor1, hit.getField("tensor_field1"));
assertEquals(tensor2, hit.getField("tensor_field2"));
}
use of com.yahoo.tensor.Tensor in project vespa by vespa-engine.
the class JoinTestCase method testJoinIndexedSubspace.
/**
* Test the indexed subspace join optimization
*/
@Test
public void testJoinIndexedSubspace() {
Tensor t1, t2;
t1 = Tensor.from("tensor(x[]):{{x:0}:1.0,{x:1}:2.0}");
t2 = Tensor.from("tensor(x[],y[],z[]):{{x:0,y:0,z:0}:6,{x:0,y:1,z:0}:0.0,{x:1,y:0,z:0}:10,{x:1,y:1,z:0}:0.0}");
assertEquals(Tensor.from("tensor(x[],y[],z[]):{{x:0,y:0,z:0}:6,{x:0,y:1,z:0}:0.0,{x:1,y:0,z:0}:20.0,{x:1,y:1,z:0}:0.0}"), t1.multiply(t2));
t1 = Tensor.from("tensor(x[]):{{x:0}:1.0,{x:1}:2.0}");
t2 = Tensor.from("tensor(x[],y[],z[]):{{x:0,y:0,z:0}:6,{x:0,y:1,z:0}:0.0,{x:1,y:0,z:0}:10,{x:1,y:1,z:0}:0.0}");
assertEquals(Tensor.from("tensor(x[],y[],z[]):{{x:0,y:0,z:0}:6,{x:0,y:1,z:0}:0.0,{x:1,y:0,z:0}:5.0,{x:1,y:1,z:0}:0.0}"), t2.divide(t1));
t1 = Tensor.from("tensor(y[]):{{y:0}:1.0,{y:1}:2.0}");
t2 = Tensor.from("tensor(x[],y[],z[]):{{x:0,y:0,z:0}:6,{x:0,y:1,z:0}:0.0,{x:1,y:0,z:0}:10,{x:1,y:1,z:0}:0.0}");
assertEquals(Tensor.from("tensor(x[],y[],z[]):{{x:0,y:0,z:0}:6,{x:0,y:1,z:0}:0.0,{x:1,y:0,z:0}:10.0,{x:1,y:1,z:0}:0.0}"), t1.multiply(t2));
t1 = Tensor.from("tensor(y[]):{{y:0}:1.0,{y:1}:2.0}");
t2 = Tensor.from("tensor(x[],y[],z[]):{{x:0,y:0,z:0}:6,{x:0,y:1,z:0}:0.0,{x:1,y:0,z:0}:10,{x:1,y:1,z:0}:0.0}");
assertEquals(Tensor.from("tensor(x[],y[],z[]):{{x:0,y:0,z:0}:6,{x:0,y:1,z:0}:0.0,{x:1,y:0,z:0}:10.0,{x:1,y:1,z:0}:0.0}"), t2.divide(t1));
}
use of com.yahoo.tensor.Tensor in project vespa by vespa-engine.
the class JsonFormatTestCase method testJsonEncodingOfDenseTensor.
@Test
public void testJsonEncodingOfDenseTensor() {
Tensor.Builder builder = Tensor.Builder.of(TensorType.fromSpec("tensor(x{},y{})"));
builder.cell().label("x", 0).label("y", 0).value(2.0);
builder.cell().label("x", 0).label("y", 1).value(3.0);
builder.cell().label("x", 1).label("y", 0).value(5.0);
builder.cell().label("x", 1).label("y", 1).value(7.0);
Tensor tensor = builder.build();
byte[] json = JsonFormat.encode(tensor);
assertEquals("{\"cells\":[" + "{\"address\":{\"x\":\"0\",\"y\":\"0\"},\"value\":2.0}," + "{\"address\":{\"x\":\"0\",\"y\":\"1\"},\"value\":3.0}," + "{\"address\":{\"x\":\"1\",\"y\":\"0\"},\"value\":5.0}," + "{\"address\":{\"x\":\"1\",\"y\":\"1\"},\"value\":7.0}" + "]}", new String(json, StandardCharsets.UTF_8));
}
Aggregations