Search in sources :

Example 1 with EvaluationContext

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

Aggregations

ImmutableList (com.google.common.collect.ImmutableList)1 DimensionSizes (com.yahoo.tensor.DimensionSizes)1 IndexedTensor (com.yahoo.tensor.IndexedTensor)1 Tensor (com.yahoo.tensor.Tensor)1 TensorAddress (com.yahoo.tensor.TensorAddress)1 TensorType (com.yahoo.tensor.TensorType)1 EvaluationContext (com.yahoo.tensor.evaluation.EvaluationContext)1 TypeContext (com.yahoo.tensor.evaluation.TypeContext)1 Arrays (java.util.Arrays)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