C++ · Linear Algebra

Linear Algebra

#include <usearch/types.hpp>#include <usearch/dense_index.hpp> 3.0.0-rc.1 · cabb6603

Ask your coding agent about this page, or add the docs server to it

curl -s 'https://www.unum.cloud/docs/usearch/cpp/linear-algebra.md' | claude -p "How would this fit my code?"

§ Sparse Multiplication

multiply, maxsim and attend treat the index as a sparse operand rather than as a kNN store, and each folds its arithmetic into the graph walk instead of materializing a match list. All three need logits rather than distances, so they run on an inner-product index alone.

multiply is a sparse matrix product: every left row is walked against the index, and every pair the walk scored lands in an arena as one ragged run, in traversal order. wanted keeps the first pairs of each row, where zero keeps every pair, and required counts how many there were. The run’s Apache Arrow offsets land in the arena beside the keys and products.

auto [products, status] = index.on(executor).multiply<float>({left, rows, 768}, arena, {.wanted = 32});
for (std::size_t row = 0; row != rows; ++row)
    std::printf("%zu pairs\n", products.view.offsets[row + 1] - products.view.offsets[row]);

§ Late Interaction

maxsim is late interaction: every query row takes its best match within a key, and those maxima sum to that key’s score. A document is one key holding its token rows, which is what a values_per_key above one exists for, and the wanted count is over documents rather than rows. Each query lands its best documents, largest score first, in the same ragged dense_products_t run multiply answers, and a wanted of zero keeps every document the query scored.

auto [documents, status] = index.on(executor).maxsim<float>({tokens, rows, 768}, tokens_per_query, 10, arena);
for (std::size_t query = 0; query != rows / tokens_per_query; ++query)
    std::printf("%zu documents\n", documents.view.offsets[query + 1] - documents.view.offsets[query]);

§ Attention

attend is Flash Attention folded into the walk: every key it scores folds into each head’s running maximum, partition and weighted value sum, with no top-k cut, and the outputs land in an arena already divided by the partition, as rows of the scalar type you name. The index stores no values, so a value_lookup_t handler resolves every scored key to its value row, as wide as the keys, stored in the index’s own quantization, and read in place from storage the caller keeps until the call is final. A null row, or rows of another scalar type or width than the library reads, refuses the call with status_code_t::shape_mismatch_k. It runs on the executor’s threads together. A VRAM walk resolves values on the device, where no host handler runs, so value_lookup_t::device names a device function and its state instead, and a host handler with an arena from a VRAM executor is refused with status_code_t::device_k. With a head_count of 8, row t * 8 + h of the queries is head h of token t, and the heads of one token share its walk. A carry of one usearch_dense_softmax_state_t per query row, owned by you, folds this branch into another over the same queries, such as a recent window kept out of the index. score_scale is conventionally the inverse square root of the head width, and zero derives it from the frozen geometry.

auto value_lookup = [&](usearch_key_t const* keys, std::size_t count, usearch_scalar_t, std::size_t,
                        void const** rows) noexcept {
    for (std::size_t position = 0; position != count; ++position) rows[position] = cache.find(keys[position]);
};
std::vector<usearch_dense_softmax_state_t> carry(queries.count);
auto [context, status] = index.on(executor).attend<float, float>(
    queries, value_lookup, arena, {.head_count = 8, .carry = carry.data()});
std::printf("%zu rows of %zu\n", context.written, context.view.columns);

API

Types

answer_valueWhat a final answer_type_ carries beside its status, void for a bare status_t.
types.hpp:203 ↗
template <typename answer_type_>
struct answer_value
dense_products_tThe ragged products of multiply or the matches of maxsim, laid out in the arena.
dense_index.hpp:70 ↗
struct dense_products_t

Fields

viewusearch_matches_tape_view_tKeys, scores and offsets, end to end.
writtenstd::size_tPairs written across every row or query.
requiredstd::size_tPairs scored before wanted cut any.
value_lookup_tThe value rows attend gathers: a host handler read only during the call, or a device function.
dense_index.hpp:93 ↗
class value_lookup_t
dense_multiply_config_tThe knobs of multiply.
dense_index.hpp:194 ↗
struct dense_multiply_config_t

Fields

wantedstd::size_tPairs kept per left row, the first ones its walk scored; zero keeps every pair.
expansion_factorstd::size_tThe breadth of the candidates the walk explores; zero derives.
traversalusearch_traversal_tA walk or a scan; unknown derives.
predicatepredicate_tWhich candidate keys may be reported; empty admits every key.
progressprogress_tHears the rows answered, and may cancel.
statsusearch_traversal_stats_t *The traversal telemetry; nullptr skips reporting.
dense_attend_config_tThe knobs of attend.
dense_index.hpp:216 ↗
struct dense_attend_config_t

Fields

head_countstd::size_tQuery heads sharing the index's key-value head, dividing the row count; zero means one.
carryusearch_dense_softmax_state_t *One online-softmax state per query row, passed by the caller; nullptr skips it.
expansion_factorstd::size_tThe breadth of the candidates the walk explores; zero derives.
score_scalefloatThe logit scale; zero derives it from the frozen geometry.
traversalusearch_traversal_tA walk or a scan; unknown derives.
predicatepredicate_tWhich candidate keys may be attended; empty admits every key.
progressprogress_tHears the rows answered, and may cancel.
statsusearch_traversal_stats_t *The traversal telemetry; nullptr skips reporting.
dense_maxsim_config_tThe knobs of maxsim.
dense_index.hpp:244 ↗
struct dense_maxsim_config_t

Fields

expansion_factorstd::size_tThe breadth of the candidates the walk explores; zero derives.
traversalusearch_traversal_tA walk or a scan; unknown derives.
predicatepredicate_tWhich candidate keys may be reported; empty admits every key.
hintsusearch_matches_view_t const *Matches of an earlier search to start from; nullptr starts where searches start.
progressprogress_tHears the queries answered, and may cancel.
statsusearch_traversal_stats_t *The traversal telemetry; nullptr skips reporting.