<commit_before>// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include <vespa/fastos/fastos.h>
#include "dense_dot_product_function.h"
#include "dense_tensor.h"
#include "dense_tensor_view.h"
#include <vespa/vespalib/eval/value.h>
#include <vespa/vespalib/tensor/tensor.h>

namespace vespalib {
namespace tensor {

using CellsRef = DenseTensorView::CellsRef;

DenseDotProductFunction::DenseDotProductFunction(size_t lhsTensorId_, size_t rhsTensorId_)
    : _lhsTensorId(lhsTensorId_),
      _rhsTensorId(rhsTensorId_),
      _hwAccelerator(hwaccelrated::IAccelrated::getAccelrator())
{
}

namespace {

CellsRef
getCellsRef(const eval::Value &value)
{
    assert(value.is_tensor());
    const Tensor *tensor = static_cast<const Tensor *>(value.as_tensor());
    const DenseTensorView *denseTensor = static_cast<const DenseTensorView *>(tensor);
    return denseTensor->cellsRef();
}

}

const eval::Value &
DenseDotProductFunction::eval(const Input &input, Stash &stash) const
{
    DenseTensorView::CellsRef lhsCells = getCellsRef(input.get_tensor(_lhsTensorId));
    DenseTensorView::CellsRef rhsCells = getCellsRef(input.get_tensor(_rhsTensorId));
    size_t numCells = std::min(lhsCells.size(), rhsCells.size());
    double result = _hwAccelerator->dotProduct(lhsCells.cbegin(), rhsCells.cbegin(), numCells);
    return stash.create<eval::DoubleValue>(result);
}

} // namespace tensor
} // namespace vespalib<commit_msg>Avoid assert in inner loop.<commit_after>// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include <vespa/fastos/fastos.h>
#include "dense_dot_product_function.h"
#include "dense_tensor.h"
#include "dense_tensor_view.h"
#include <vespa/vespalib/eval/value.h>
#include <vespa/vespalib/tensor/tensor.h>

namespace vespalib {
namespace tensor {

using CellsRef = DenseTensorView::CellsRef;

DenseDotProductFunction::DenseDotProductFunction(size_t lhsTensorId_, size_t rhsTensorId_)
    : _lhsTensorId(lhsTensorId_),
      _rhsTensorId(rhsTensorId_),
      _hwAccelerator(hwaccelrated::IAccelrated::getAccelrator())
{
}

namespace {

CellsRef
getCellsRef(const eval::Value &value)
{
    const Tensor *tensor = static_cast<const Tensor *>(value.as_tensor());
    const DenseTensorView *denseTensor = static_cast<const DenseTensorView *>(tensor);
    return denseTensor->cellsRef();
}

}

const eval::Value &
DenseDotProductFunction::eval(const Input &input, Stash &stash) const
{
    DenseTensorView::CellsRef lhsCells = getCellsRef(input.get_tensor(_lhsTensorId));
    DenseTensorView::CellsRef rhsCells = getCellsRef(input.get_tensor(_rhsTensorId));
    size_t numCells = std::min(lhsCells.size(), rhsCells.size());
    double result = _hwAccelerator->dotProduct(lhsCells.cbegin(), rhsCells.cbegin(), numCells);
    return stash.create<eval::DoubleValue>(result);
}

} // namespace tensor
} // namespace vespalib
{'fraction_non_alphanumeric': 0.11897539602291878, 'fraction_numerical': 0.004044489383215369, 'mean_word_length': 5.967136150234742, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2894283', 'n_tokens_mistral': 1026, 'n_tokens_neox': 971, 'n_words': 192}