C · MaxSim

MaxSim

#include <numkong/maxsim.h> 8.0.0-rc.1 · b1f866ab

MaxSim is the late-interaction primitive used by systems such as ColBERT. It is not generic matrix multiplication. It packs query and document token vectors into a scoring-specific layout and computes a late-interaction score.

auto [queries, queries_status] = nk::tensor<nk::bf16_t>::full({32, 128}, nk::bf16_t::one());
auto [docs, docs_status] = nk::tensor<nk::bf16_t>::full({192, 128}, nk::bf16_t::one());
if (nk::failed(queries_status) || nk::failed(docs_status)) return nk::failed(queries_status) ? queries_status : docs_status;

auto [q, q_status] = nk::packed_maxsim<nk::bf16_t>::make(queries.as_matrix_view());
auto [d, d_status] = nk::packed_maxsim<nk::bf16_t>::make(docs.as_matrix_view());
if (nk::failed(q_status) || nk::failed(d_status)) return nk::failed(q_status) ? q_status : d_status;
auto score = nk::maxsim(q, d);

packed_maxsim is allocator-aware in the same way as packed_matrix. Its footprint is exposed through size_bytes().

API

Functions

nk_maxsim_pack_size_*_best× 3Returns packed buffer size in bytes for a maxsim vector set.
maxsim.h:81 ↗
nk_status_t nk_maxsim_pack_size_bf16_best(
    nk_size_t vector_count,
    nk_size_t depth,
    nk_capability_t capabilities,
    nk_size_t *bytes);
SymbolBackends
nk_maxsim_pack_size_bf16_best8
nk_maxsim_pack_size_f32_best8
nk_maxsim_pack_size_f16_best8

Backends

  • nk_maxsim_pack_size_bf16serialgenoasapphireamxhaswellalderv128relaxedneonsdotsme
  • nk_maxsim_pack_size_f32serialicelakesapphireamxhaswellalderv128relaxedneonsdotsme
  • nk_maxsim_pack_size_f16serialicelakesapphireamxhaswellalderv128relaxedneonsdotsme

Parameters

vector_countinThe number of vectors to pack.
depthinThe number of dimensions per vector.
capabilitiesinOne device's capabilities, like nk_cpu_capabilities_enabled reports.
bytesoutThe size of the packed buffer.

Notes

The packed layout is backend-specific and must be produced by the matching pack function.

Statuses

nk_maxsim_packed_shape_*_best× 3Reads a packed MaxSim buffer's shape from its header.
maxsim.h:101 ↗
nk_status_t nk_maxsim_packed_shape_bf16_best(
    void const *packed,
    nk_size_t *vectors,
    nk_size_t *depth,
    nk_capability_t capabilities,
    nk_stream_t stream);
SymbolBackends
nk_maxsim_packed_shape_bf16_best8
nk_maxsim_packed_shape_f32_best8
nk_maxsim_packed_shape_f16_best8

Backends

  • nk_maxsim_packed_shape_bf16serialgenoasapphireamxhaswellalderv128relaxedneonsdotsme
  • nk_maxsim_packed_shape_f32serialicelakesapphireamxhaswellalderv128relaxedneonsdotsme
  • nk_maxsim_packed_shape_f16serialicelakesapphireamxhaswellalderv128relaxedneonsdotsme

Parameters

packedinA buffer produced by the matching nk_maxsim_pack_bf16_best.
vectorsoutReceives the vector count.
depthoutReceives the inner dimension.
capabilitiesinOne device's capabilities, like nk_cpu_capabilities_enabled reports.
streaminNull on the CPU, or the GPU stream of that device to queue on.

Statuses

nk_maxsim_pack_*_best× 3Packs vectors into a backend-specific layout for maxsim computation.
maxsim.h:124 ↗
nk_status_t nk_maxsim_pack_bf16_best(
    nk_bf16_t const *vectors,
    nk_size_t vector_count,
    nk_size_t depth,
    nk_size_t stride,
    void *packed,
    nk_capability_t capabilities,
    nk_stream_t stream);
SymbolvectorsBackends
nk_maxsim_pack_bf16_bestnk_bf16_t8
nk_maxsim_pack_f32_bestnk_f32_t8
nk_maxsim_pack_f16_bestnk_f16_t8

Backends

  • nk_maxsim_pack_bf16serialgenoasapphireamxhaswellalderv128relaxedneonsdotsme
  • nk_maxsim_pack_f32serialicelakesapphireamxhaswellalderv128relaxedneonsdotsme
  • nk_maxsim_pack_f16serialicelakesapphireamxhaswellalderv128relaxedneonsdotsme

Parameters

vectorsinThe input vectors in row-major order. Packing fuses no transposition, so depth-major vectors are transposed in a separate pass first.
vector_countinThe number of vectors.
depthinThe number of dimensions per vector.
strideinThe row stride in bytes for the input vectors.
packedoutThe output packed buffer from nk_maxsim_pack_size_bf16_best.
capabilitiesinOne device's capabilities, like nk_cpu_capabilities_enabled reports.
streaminNull on the CPU, or the GPU stream of that device to queue on.

Statuses

nk_maxsim_packed_*_best× 3Computes angular distance late-interaction on pre-packed vectors.
maxsim.h:153 ↗
nk_status_t nk_maxsim_packed_bf16_best(
    void const *query_packed,
    void const *document_packed,
    nk_size_t query_count,
    nk_size_t document_count,
    nk_size_t depth,
    nk_f32_t *result,
    nk_capability_t capabilities,
    nk_stream_t stream);
SymbolresultBackends
nk_maxsim_packed_bf16_bestnk_f32_t8
nk_maxsim_packed_f32_bestnk_f64_t8
nk_maxsim_packed_f16_bestnk_f32_t8

Backends

  • nk_maxsim_packed_bf16serialgenoasapphireamxhaswellalderv128relaxedneonsdotsme
  • nk_maxsim_packed_f32serialicelakesapphireamxhaswellalderv128relaxedneonsdotsme
  • nk_maxsim_packed_f16serialicelakesapphireamxhaswellalderv128relaxedneonsdotsme

Returns Σᵢ minⱼ angular(qᵢ, dⱼ) where angular = 1 - dot / sqrt(||q||² × ||d||²), and 0 for two zero vectors. A NaN element in any query or document makes its angles, their minima and so the result NaN.

Parameters

query_packedinPacked query vectors (from nk_maxsim_pack_bf16_best).
document_packedinPacked document vectors (from nk_maxsim_pack_bf16_best).
query_countinNumber of query vectors.
document_countinNumber of document vectors.
depthinNumber of dimensions per vector.
resultoutPointer to store the sum of per-query minimum angular distances.
capabilitiesinOne device's capabilities, like nk_cpu_capabilities_enabled reports.
streaminNull on the CPU, or the GPU stream of that device to queue on.

Statuses

nk_maxsim_output_dtypeReturns the output dtype for MaxSim late-interaction.
maxsim.h:491 ↗
nk_dtype_t nk_maxsim_output_dtype(nk_dtype_t dtype);
nk_maxsim_error_boundReturns the error bound of MaxSim scores, per nk_accumulation_error_bound of their output.
maxsim.h:501 ↗
nk_f64_t nk_maxsim_error_bound(nk_dtype_t dtype);
nk_maxsim_find_kernelFinds the MaxSim kernel of kind for dtype, from the best of capabilities.
maxsim.h:517 ↗
nk_status_t nk_maxsim_find_kernel(
    nk_kernel_kind_t kind,
    nk_dtype_t dtype,
    nk_capability_t capabilities,
    nk_kernel_punned_t *kernel,
    nk_capability_t *capability);

Parameters

kindinThe verb whose kernel to find.
dtypeinThe input type the kernel reads.
capabilitiesinThe mask to pick the best capability from.
kerneloutThe kernel, or null when none of capabilities has it.
capabilityoutThe capability the kernel belongs to, or zero.

Statuses