Search in sources :

Example 1 with IndexedTensor

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

the class Join method joinTo.

private void joinTo(IndexedTensor a, IndexedTensor b, TensorType joinedType, DimensionSizes joinedSize, int[] aToIndexes, int[] bToIndexes, boolean reversedOrder, Tensor.Builder builder) {
    Set<String> sharedDimensions = Sets.intersection(a.type().dimensionNames(), b.type().dimensionNames());
    Set<String> dimensionsOnlyInA = Sets.difference(a.type().dimensionNames(), b.type().dimensionNames());
    DimensionSizes aIterateSize = joinedSizeOf(a.type(), joinedType, joinedSize);
    DimensionSizes bIterateSize = joinedSizeOf(b.type(), joinedType, joinedSize);
    // for each combination of dimensions only in a
    for (Iterator<IndexedTensor.SubspaceIterator> ia = a.subspaceIterator(dimensionsOnlyInA, aIterateSize); ia.hasNext(); ) {
        IndexedTensor.SubspaceIterator aSubspace = ia.next();
        // for each combination of dimensions in a which is also in b
        while (aSubspace.hasNext()) {
            Tensor.Cell aCell = aSubspace.next();
            PartialAddress matchingBCells = partialAddress(a.type(), aSubspace.address(), sharedDimensions);
            // for each matching combination of dimensions ony in b
            for (IndexedTensor.SubspaceIterator bSubspace = b.cellIterator(matchingBCells, bIterateSize); bSubspace.hasNext(); ) {
                Tensor.Cell bCell = bSubspace.next();
                TensorAddress joinedAddress = joinAddresses(aCell.getKey(), aToIndexes, bCell.getKey(), bToIndexes, joinedType);
                double joinedValue = reversedOrder ? combinator.applyAsDouble(bCell.getValue(), aCell.getValue()) : combinator.applyAsDouble(aCell.getValue(), bCell.getValue());
                builder.cell(joinedAddress, joinedValue);
            }
        }
    }
}
Also used : TensorAddress(com.yahoo.tensor.TensorAddress) IndexedTensor(com.yahoo.tensor.IndexedTensor) Tensor(com.yahoo.tensor.Tensor) DimensionSizes(com.yahoo.tensor.DimensionSizes) PartialAddress(com.yahoo.tensor.PartialAddress) IndexedTensor(com.yahoo.tensor.IndexedTensor)

Example 2 with IndexedTensor

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

the class Join method evaluate.

@Override
public <NAMETYPE extends TypeContext.Name> Tensor evaluate(EvaluationContext<NAMETYPE> context) {
    Tensor a = argumentA.evaluate(context);
    Tensor b = argumentB.evaluate(context);
    TensorType joinedType = new TensorType.Builder(a.type(), b.type()).build();
    // Choose join algorithm
    if (hasSingleIndexedDimension(a) && hasSingleIndexedDimension(b) && a.type().dimensions().get(0).name().equals(b.type().dimensions().get(0).name()))
        return indexedVectorJoin((IndexedTensor) a, (IndexedTensor) b, joinedType);
    else if (joinedType.dimensions().size() == a.type().dimensions().size() && joinedType.dimensions().size() == b.type().dimensions().size())
        return singleSpaceJoin(a, b, joinedType);
    else if (a.type().dimensions().containsAll(b.type().dimensions()))
        return subspaceJoin(b, a, joinedType, true);
    else if (b.type().dimensions().containsAll(a.type().dimensions()))
        return subspaceJoin(a, b, joinedType, false);
    else
        return generalJoin(a, b, joinedType);
}
Also used : IndexedTensor(com.yahoo.tensor.IndexedTensor) Tensor(com.yahoo.tensor.Tensor) TensorType(com.yahoo.tensor.TensorType) IndexedTensor(com.yahoo.tensor.IndexedTensor)

Example 3 with IndexedTensor

use of com.yahoo.tensor.IndexedTensor 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();
}
Also used : Arrays(java.util.Arrays) Iterator(java.util.Iterator) Set(java.util.Set) TensorType(com.yahoo.tensor.TensorType) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) TensorAddress(com.yahoo.tensor.TensorAddress) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) EvaluationContext(com.yahoo.tensor.evaluation.EvaluationContext) DimensionSizes(com.yahoo.tensor.DimensionSizes) TypeContext(com.yahoo.tensor.evaluation.TypeContext) Optional(java.util.Optional) IndexedTensor(com.yahoo.tensor.IndexedTensor) Tensor(com.yahoo.tensor.Tensor) IndexedTensor(com.yahoo.tensor.IndexedTensor) Tensor(com.yahoo.tensor.Tensor) DimensionSizes(com.yahoo.tensor.DimensionSizes) TensorType(com.yahoo.tensor.TensorType) IndexedTensor(com.yahoo.tensor.IndexedTensor)

Example 4 with IndexedTensor

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

the class Concat method concatenateTo.

private void concatenateTo(IndexedTensor a, IndexedTensor b, long offset, TensorType concatType, int[] aToIndexes, int[] bToIndexes, Tensor.Builder builder) {
    Set<String> otherADimensions = a.type().dimensionNames().stream().filter(d -> !d.equals(dimension)).collect(Collectors.toSet());
    for (Iterator<IndexedTensor.SubspaceIterator> ia = a.subspaceIterator(otherADimensions); ia.hasNext(); ) {
        IndexedTensor.SubspaceIterator iaSubspace = ia.next();
        TensorAddress aAddress = iaSubspace.address();
        for (Iterator<IndexedTensor.SubspaceIterator> ib = b.subspaceIterator(otherADimensions); ib.hasNext(); ) {
            IndexedTensor.SubspaceIterator ibSubspace = ib.next();
            while (ibSubspace.hasNext()) {
                Tensor.Cell bCell = ibSubspace.next();
                TensorAddress combinedAddress = combineAddresses(aAddress, aToIndexes, bCell.getKey(), bToIndexes, concatType, offset, dimension);
                // incompatible
                if (combinedAddress == null)
                    continue;
                builder.cell(combinedAddress, bCell.getValue());
            }
            iaSubspace.reset();
        }
    }
}
Also used : Arrays(java.util.Arrays) Iterator(java.util.Iterator) Set(java.util.Set) TensorType(com.yahoo.tensor.TensorType) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) TensorAddress(com.yahoo.tensor.TensorAddress) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) EvaluationContext(com.yahoo.tensor.evaluation.EvaluationContext) DimensionSizes(com.yahoo.tensor.DimensionSizes) TypeContext(com.yahoo.tensor.evaluation.TypeContext) Optional(java.util.Optional) IndexedTensor(com.yahoo.tensor.IndexedTensor) Tensor(com.yahoo.tensor.Tensor) TensorAddress(com.yahoo.tensor.TensorAddress) IndexedTensor(com.yahoo.tensor.IndexedTensor) Tensor(com.yahoo.tensor.Tensor) IndexedTensor(com.yahoo.tensor.IndexedTensor)

Example 5 with IndexedTensor

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

the class Join method indexedSubspaceJoin.

private Tensor indexedSubspaceJoin(IndexedTensor subspace, IndexedTensor superspace, TensorType joinedType, boolean reversedArgumentOrder) {
    if (// special case empty here to avoid doing it when finding sizes
    subspace.size() == 0 || superspace.size() == 0)
        return Tensor.Builder.of(joinedType, new DimensionSizes.Builder(joinedType.dimensions().size()).build()).build();
    DimensionSizes joinedSizes = joinedSize(joinedType, subspace, superspace);
    IndexedTensor.Builder builder = (IndexedTensor.Builder) Tensor.Builder.of(joinedType, joinedSizes);
    // Find dimensions which are only in the supertype
    Set<String> superDimensionNames = new HashSet<>(superspace.type().dimensionNames());
    superDimensionNames.removeAll(subspace.type().dimensionNames());
    for (Iterator<IndexedTensor.SubspaceIterator> i = superspace.subspaceIterator(superDimensionNames, joinedSizes); i.hasNext(); ) {
        IndexedTensor.SubspaceIterator subspaceInSuper = i.next();
        joinSubspaces(subspace.valueIterator(), subspace.size(), subspaceInSuper, subspaceInSuper.size(), reversedArgumentOrder, builder);
    }
    return builder.build();
}
Also used : DimensionSizes(com.yahoo.tensor.DimensionSizes) IndexedTensor(com.yahoo.tensor.IndexedTensor) HashSet(java.util.HashSet)

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