Search in sources :

Example 1 with DimensionSizes

use of com.yahoo.tensor.DimensionSizes 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 DimensionSizes

use of com.yahoo.tensor.DimensionSizes 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 3 with DimensionSizes

use of com.yahoo.tensor.DimensionSizes 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)

Example 4 with DimensionSizes

use of com.yahoo.tensor.DimensionSizes 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 5 with DimensionSizes

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

the class DenseBinaryFormat method decode.

@Override
public Tensor decode(Optional<TensorType> optionalType, GrowableByteBuffer buffer) {
    TensorType type;
    DimensionSizes sizes;
    if (optionalType.isPresent()) {
        type = optionalType.get();
        TensorType serializedType = decodeType(buffer);
        if (!serializedType.isAssignableTo(type))
            throw new IllegalArgumentException("Type/instance mismatch: A tensor of type " + serializedType + " cannot be assigned to type " + type);
        sizes = sizesFromType(serializedType);
    } else {
        type = decodeType(buffer);
        sizes = sizesFromType(type);
    }
    Tensor.Builder builder = Tensor.Builder.of(type, sizes);
    decodeCells(sizes, buffer, (IndexedTensor.BoundBuilder) builder);
    return builder.build();
}
Also used : 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)

Aggregations

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