Search in sources :

Example 6 with IndexedTensor

use of com.yahoo.tensor.IndexedTensor in project vespa by vespa-engine.

the class Join method indexedGeneralJoin.

private Tensor indexedGeneralJoin(IndexedTensor a, IndexedTensor b, TensorType joinedType) {
    DimensionSizes joinedSize = joinedSize(joinedType, a, b);
    Tensor.Builder builder = Tensor.Builder.of(joinedType, joinedSize);
    int[] aToIndexes = mapIndexes(a.type(), joinedType);
    int[] bToIndexes = mapIndexes(b.type(), joinedType);
    joinTo(a, b, joinedType, joinedSize, aToIndexes, bToIndexes, false, builder);
    // joinTo(b, a, joinedType, joinedSize, bToIndexes, aToIndexes, true, builder);
    return builder.build();
}
Also used : IndexedTensor(com.yahoo.tensor.IndexedTensor) Tensor(com.yahoo.tensor.Tensor) DimensionSizes(com.yahoo.tensor.DimensionSizes)

Example 7 with IndexedTensor

use of com.yahoo.tensor.IndexedTensor in project vespa by vespa-engine.

the class Reduce method evaluate.

@Override
public <NAMETYPE extends TypeContext.Name> Tensor evaluate(EvaluationContext<NAMETYPE> context) {
    Tensor argument = this.argument.evaluate(context);
    if (!dimensions.isEmpty() && !argument.type().dimensionNames().containsAll(dimensions))
        throw new IllegalArgumentException("Cannot reduce " + argument + " over dimensions " + dimensions + ": Not all those dimensions are present in this tensor");
    // Special case: Reduce all
    if (dimensions.isEmpty() || dimensions.size() == argument.type().dimensions().size())
        if (argument.type().dimensions().size() == 1 && argument instanceof IndexedTensor)
            return reduceIndexedVector((IndexedTensor) argument);
        else
            return reduceAllGeneral(argument);
    TensorType reducedType = type(argument.type());
    // Reduce cells
    Map<TensorAddress, ValueAggregator> aggregatingCells = new HashMap<>();
    for (Iterator<Tensor.Cell> i = argument.cellIterator(); i.hasNext(); ) {
        Map.Entry<TensorAddress, Double> cell = i.next();
        TensorAddress reducedAddress = reduceDimensions(cell.getKey(), argument.type(), reducedType);
        aggregatingCells.putIfAbsent(reducedAddress, ValueAggregator.ofType(aggregator));
        aggregatingCells.get(reducedAddress).aggregate(cell.getValue());
    }
    Tensor.Builder reducedBuilder = Tensor.Builder.of(reducedType);
    for (Map.Entry<TensorAddress, ValueAggregator> aggregatingCell : aggregatingCells.entrySet()) reducedBuilder.cell(aggregatingCell.getKey(), aggregatingCell.getValue().aggregatedValue());
    return reducedBuilder.build();
}
Also used : TensorAddress(com.yahoo.tensor.TensorAddress) IndexedTensor(com.yahoo.tensor.IndexedTensor) Tensor(com.yahoo.tensor.Tensor) HashMap(java.util.HashMap) TensorType(com.yahoo.tensor.TensorType) HashMap(java.util.HashMap) Map(java.util.Map) IndexedTensor(com.yahoo.tensor.IndexedTensor)

Example 8 with IndexedTensor

use of com.yahoo.tensor.IndexedTensor in project vespa by vespa-engine.

the class TypedBinaryFormat method encode.

public static byte[] encode(Tensor tensor) {
    GrowableByteBuffer buffer = new GrowableByteBuffer();
    if (tensor instanceof MixedTensor) {
        buffer.putInt1_4Bytes(MIXED_BINARY_FORMAT_TYPE);
        new MixedBinaryFormat().encode(buffer, tensor);
    } else if (tensor instanceof IndexedTensor) {
        buffer.putInt1_4Bytes(DENSE_BINARY_FORMAT_TYPE);
        new DenseBinaryFormat().encode(buffer, tensor);
    } else {
        buffer.putInt1_4Bytes(SPARSE_BINARY_FORMAT_TYPE);
        new SparseBinaryFormat().encode(buffer, tensor);
    }
    buffer.flip();
    byte[] result = new byte[buffer.remaining()];
    buffer.get(result);
    return result;
}
Also used : GrowableByteBuffer(com.yahoo.io.GrowableByteBuffer) MixedTensor(com.yahoo.tensor.MixedTensor) IndexedTensor(com.yahoo.tensor.IndexedTensor)

Example 9 with IndexedTensor

use of com.yahoo.tensor.IndexedTensor in project vespa by vespa-engine.

the class JsonReaderTestCase method testParsingOfIndexedTensorWithCells.

@Test
public void testParsingOfIndexedTensorWithCells() {
    Tensor tensor = assertTensorField("{{x:0,y:0}:2.0,{x:1,y:0}:3.0}}", createPutWithTensor("{ " + "  \"cells\": [ " + "    { \"address\": { \"x\": \"0\", \"y\": \"0\" }, " + "      \"value\": 2.0 }, " + "    { \"address\": { \"x\": \"1\", \"y\": \"0\" }, " + "      \"value\": 3.0 } " + "  ]" + "}", "indexedtensorfield"), "indexedtensorfield");
    // this matters for performance
    assertTrue(tensor instanceof IndexedTensor);
}
Also used : Tensor(com.yahoo.tensor.Tensor) IndexedTensor(com.yahoo.tensor.IndexedTensor) MappedTensor(com.yahoo.tensor.MappedTensor) IndexedTensor(com.yahoo.tensor.IndexedTensor) Test(org.junit.Test)

Aggregations

IndexedTensor (com.yahoo.tensor.IndexedTensor)9 Tensor (com.yahoo.tensor.Tensor)7 DimensionSizes (com.yahoo.tensor.DimensionSizes)5 TensorAddress (com.yahoo.tensor.TensorAddress)4 TensorType (com.yahoo.tensor.TensorType)4 ImmutableList (com.google.common.collect.ImmutableList)2 EvaluationContext (com.yahoo.tensor.evaluation.EvaluationContext)2 TypeContext (com.yahoo.tensor.evaluation.TypeContext)2 Arrays (java.util.Arrays)2 Iterator (java.util.Iterator)2 List (java.util.List)2 Objects (java.util.Objects)2 Optional (java.util.Optional)2 Set (java.util.Set)2 Collectors (java.util.stream.Collectors)2 GrowableByteBuffer (com.yahoo.io.GrowableByteBuffer)1 MappedTensor (com.yahoo.tensor.MappedTensor)1 MixedTensor (com.yahoo.tensor.MixedTensor)1 PartialAddress (com.yahoo.tensor.PartialAddress)1 HashMap (java.util.HashMap)1