template <typename answer_type_>
struct answer_value § 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.
dense_products_tThe ragged products of multiply or the matches of maxsim, laid out in the arena.
struct dense_products_t Fields
view | usearch_matches_tape_view_t | Keys, scores and offsets, end to end. |
|---|---|---|
written | std::size_t | Pairs written across every row or query. |
required | std::size_t | Pairs scored before wanted cut any. |
value_lookup_tThe value rows attend gathers: a host handler read only during the call, or a device function.
class value_lookup_t dense_multiply_config_tThe knobs of multiply.
struct dense_multiply_config_t Fields
wanted | std::size_t | Pairs kept per left row, the first ones its walk scored; zero keeps every pair. |
|---|---|---|
expansion_factor | std::size_t | The breadth of the candidates the walk explores; zero derives. |
traversal | usearch_traversal_t | A walk or a scan; unknown derives. |
predicate | predicate_t | Which candidate keys may be reported; empty admits every key. |
progress | progress_t | Hears the rows answered, and may cancel. |
stats | usearch_traversal_stats_t * | The traversal telemetry; nullptr skips reporting. |
dense_attend_config_tThe knobs of attend.
struct dense_attend_config_t Fields
head_count | std::size_t | Query heads sharing the index's key-value head, dividing the row count; zero means one. |
|---|---|---|
carry | usearch_dense_softmax_state_t * | One online-softmax state per query row, passed by the caller; nullptr skips it. |
expansion_factor | std::size_t | The breadth of the candidates the walk explores; zero derives. |
score_scale | float | The logit scale; zero derives it from the frozen geometry. |
traversal | usearch_traversal_t | A walk or a scan; unknown derives. |
predicate | predicate_t | Which candidate keys may be attended; empty admits every key. |
progress | progress_t | Hears the rows answered, and may cancel. |
stats | usearch_traversal_stats_t * | The traversal telemetry; nullptr skips reporting. |
dense_maxsim_config_tThe knobs of maxsim.
struct dense_maxsim_config_t Fields
expansion_factor | std::size_t | The breadth of the candidates the walk explores; zero derives. |
|---|---|---|
traversal | usearch_traversal_t | A walk or a scan; unknown derives. |
predicate | predicate_t | Which candidate keys may be reported; empty admits every key. |
hints | usearch_matches_view_t const * | Matches of an earlier search to start from; nullptr starts where searches start. |
progress | progress_t | Hears the queries answered, and may cancel. |
stats | usearch_traversal_stats_t * | The traversal telemetry; nullptr skips reporting. |