C · Matrix Kernels

Matrix Kernels

#include <numkong/capabilities.h>#include <numkong/dots.h>#include <numkong/sets.h>#include <numkong/spatials.h>#include <numkong/attention.h>#include <numkong/types.h> 8.0.0-rc.1 · b1f866ab

This is a separate native subsystem from the raw vector kernels. It is the right tool when the right-hand side is reused many times.

#include <numkong/numkong.hpp>

namespace nk = ashvardanian::numkong;

auto [a, a_status] = nk::tensor<nk::f32_t>::full({2, 4}, nk::f32_t {1});
auto [b, b_status] = nk::tensor<nk::f32_t>::full({3, 4}, nk::f32_t {2});
if (nk::failed(a_status) || nk::failed(b_status)) return nk::failed(a_status) ? a_status : b_status;
auto [packed, packed_status] = nk::packed_matrix<nk::f32_t>::make(b.as_matrix_view());
if (nk::failed(packed_status)) return packed_status;

// Dot products, angular distances, and Euclidean distances all reuse the same packed B
auto [dots, dots_status] = nk::dots_packed<nk::f32_t>(a.as_matrix_view(), packed); // nk::matrix<nk::f64_t>
auto [angulars, angulars_status] = nk::angulars_packed<nk::f32_t>(a.as_matrix_view(), packed);
auto [euclideans, euclideans_status] = nk::euclideans_packed<nk::f32_t>(a.as_matrix_view(), packed);

This is GEMM-like in the workload shape, not in the strict BLAS API. The useful economics are:

  • one-time packing of B
  • one-time type preconversion where needed
  • depth padding handled internally
  • per-column norm reuse for angulars_packed and euclideans_packed
  • repeated reuse of the same packed RHS across many A batches

Caller-side alignment is not required. Owned packed_matrix storage uses its allocator. The C ABI also exposes nk_dots_pack_size_*_best, which writes the byte count of the capability the mask picks, so you can malloc the exact external buffer yourself. In C++, nk::dots_pack_size<nk::f32_t>(rows, depth) returns that count as an nk::expected<std::size_t>. A packed buffer records the capability that packed it, and a packed kernel of another capability refuses it with nk_pack_mismatch_k, so pack and multiply under the same mask.

§ Attention over Packed Key-Value Caches

Attention packs a ragged batch of keys and values once, then attends to it with any number of query batches. packed_attention keeps the segment offsets it was packed with, so the view overloads run self-attention over [tokens, heads, depth] queries without restating the batch, with query heads shared across fewer key-value heads.

#include <numkong/attention.hpp>

// Two sequences of 32 and 64 tokens, 8 query heads over 2 key-value heads of depth 64
auto [keys, keys_status] = nk::tensor<nk::bf16_t>::full({96, 2, 64}, nk::bf16_t::one());
auto [values, values_status] = nk::tensor<nk::bf16_t>::full({96, 2, 64}, nk::bf16_t::one());
auto [queries, queries_status] = nk::tensor<nk::bf16_t>::full({96, 8, 64}, nk::bf16_t::one());
auto [output, output_status] = nk::tensor<nk::f32_t>::zeros({96, 8, 64});
for (nk::status_t made : {keys_status, values_status, queries_status, output_status})
    if (nk::failed(made)) return made;
nk_u32_t const offsets[] = {0, 32, 96}, lengths[] = {32, 64};

auto [packed, packed_status] = nk::packed_attention<nk::bf16_t>::make(
    keys.view(), values.view(), nk::vector_view<nk_u32_t>(offsets, 3u), nk::vector_view<nk_u32_t>(lengths, 2u));
if (nk::failed(packed_status)) return packed_status;
std::size_t const all_keys = std::numeric_limits<std::size_t>::max(), causal = 0;
nk::status_t status = nk::attention_packed<nk::bf16_t>(queries.view(), packed, output.span(), 0.125f, all_keys, causal);
auto [fresh, fresh_status] = nk::attention_packed<nk::bf16_t>(queries.view(), packed, 0.125f); // nk::tensor<nk::f32_t>

Every overload takes the band of keys each query sees, keys_before and keys_after its position, every key by default. Queries align to the end of each segment’s keys, so keys_after = 0 is causal for prefill and decode alike, and keys_before = 4095 with it attends to at most 4096 keys per row. The raw-pointer overloads take the query offsets separately, for cross-attention, and a window over the query tokens × heads task grid, for sharding one launch across workers.

API

Types

nk_dots_pack_size_punned_tPack sizes are host arithmetic, so they take no stream.
capabilities.h:962 ↗
typedef nk_status_t(*) nk_dots_pack_size_punned_t(nk_size_t columns, nk_size_t depth, nk_size_t *bytes);
nk_sets_packed_punned_tBit vectors have no scaled form, so the set batches keep their unscaled shapes.
capabilities.h:992 ↗
typedef nk_status_t(*) nk_sets_packed_punned_t(void const *a, void const *b_packed, void *c, nk_size_t rows, nk_size_t columns, nk_size_t depth, nk_size_t a_stride, nk_size_t c_stride, void *workspace, nk_size_t workspace_bytes, nk_stream_t stream);
nk_attention_pack_size_punned_tPack sizes are host arithmetic, so they take no stream.
capabilities.h:1062 ↗
typedef nk_status_t(*) nk_attention_pack_size_punned_t(nk_size_t key_value_head_count, nk_size_t depth, nk_size_t token_count, nk_size_t segment_count, nk_size_t *bytes);
nk_attention_packed_workspace_size_punned_tWorkspace sizes of attention and its gradients, host arithmetic like pack sizes.
capabilities.h:1089 ↗
typedef nk_status_t(*) nk_attention_packed_workspace_size_punned_t(nk_size_t head_count, nk_size_t key_value_head_count, nk_size_t depth, nk_size_t query_token_count, nk_size_t keys_before, nk_size_t keys_after, nk_size_t *bytes);
nk_diagonal_band_tThe cells of a rows × columns grid a kernel visits, by their distance from the diagonal: column c of row r is visible when r − subdiagonals ≤ c ≤ r + superdiagonals, each side unbounded at NUMKONG_SIZE_MAX, like LAPACK's kl and ku.
types.h:1466 ↗
struct nk_diagonal_band_t

The symmetric products visit the upper triangle (0, NUMKONG_SIZE_MAX), because A × Aᵀ mirrors it. Masked attention visits (keys_before, keys_after) around each query's position, because the other keys are forbidden rather than mirrored: causal attention, (NUMKONG_SIZE_MAX, 0), skips the same triangle of tiles for a different reason. So the band is a parameter of packed attention rather than a verb like symmetric, as its operands and outputs don't change.

Fields

subdiagonalsnk_size_t
superdiagonalsnk_size_t
nk_diagonal_band_coverage_tWhere a banded grid's tile lies: no visible cell, every cell visible, or an edge across it.
types.h:1472 ↗
enum nk_diagonal_band_coverage_t

Values

nk_diagonal_band_outside_k0
nk_diagonal_band_inside_k1
nk_diagonal_band_crossing_k2

Functions

nk_dots_pack_size_*_best× 20Returns packed buffer size in bytes for second multiplier matrix (B).
dots.h:231 ↗
nk_status_t nk_dots_pack_size_bf16_best(
    nk_size_t columns,
    nk_size_t depth,
    nk_capability_t capabilities,
    nk_size_t *bytes);
SymbolBackends
nk_dots_pack_size_bf16_best24
nk_dots_pack_size_f16_best22
nk_dots_pack_size_e4m3_best22
nk_dots_pack_size_e5m2_best23
nk_dots_pack_size_e2m3_best24
nk_dots_pack_size_e2m1_best24
nk_dots_pack_size_nvfp4_best13
nk_dots_pack_size_mxfp4_best13
nk_dots_pack_size_mxfp6e2m3_best8
nk_dots_pack_size_mxfp6e3m2_best7
nk_dots_pack_size_mxfp8e4m3_best13
nk_dots_pack_size_mxfp8e5m2_best13
nk_dots_pack_size_e3m2_best20
nk_dots_pack_size_f32_best12
nk_dots_pack_size_f64_best12
nk_dots_pack_size_i8_best23
nk_dots_pack_size_u8_best23
nk_dots_pack_size_i4_best16
nk_dots_pack_size_u4_best16
nk_dots_pack_size_u1_best8

Backends

  • nk_dots_pack_size_bf16serialgenoasapphireamxsmehaswellskylakev128relaxedv128neonneonbfdotrvvpowervsxloongsonasxcudaamperehopperblackwellrocmcdna3cdna4cdna5metalapple9apple10
  • nk_dots_pack_size_f16serialgraniteamxsmehaswellskylakev128relaxedneonneonfhmrvvpowervsxloongsonasxcudaamperehopperblackwellrocmcdna3cdna4cdna5metalapple9apple10
  • nk_dots_pack_size_e4m3serialgenoadiamondsapphireamxsmehaswellskylakev128relaxedneonfhmneonfp8rvvcudaampereblackwellblackwellrtxrocmcdna3cdna4cdna5metalapple9apple10
  • nk_dots_pack_size_e5m2serialgenoadiamondsapphireamxgraniteamxsmehaswellskylakev128relaxedneonfhmneonfp8rvvcudaampereblackwellblackwellrtxrocmcdna3cdna4cdna5metalapple9apple10
  • nk_dots_pack_size_e2m3serialsapphireamxsmehaswellskylakeicelakealdersierrav128relaxedneonsdotneonfp8rvvcudaamperehopperblackwellblackwellrtxrocmcdna3cdna4cdna5metalapple9apple10
  • nk_dots_pack_size_e2m1serialsapphireamxsmehaswellskylakeicelakealdersierrav128relaxedneonsdotneonfp8rvvcudaamperehopperblackwellblackwellrtxrocmcdna3cdna4cdna5metalapple9apple10
  • nk_dots_pack_size_nvfp4serialsapphireamxsmeskylakeneonneonsdotcudaamperehopperblackwellmetalapple9apple10
  • nk_dots_pack_size_mxfp4serialsapphireamxsmeskylakeneonneonsdotcudaamperehopperblackwellmetalapple9apple10
  • nk_dots_pack_size_mxfp6e2m3serialsmeskylakeneonneonsdotmetalapple9apple10
  • nk_dots_pack_size_mxfp6e3m2serialsmeskylakeneonmetalapple9apple10
  • nk_dots_pack_size_mxfp8e4m3serialsapphireamxsmeskylakeneonneonfhmcudaamperehopperblackwellmetalapple9apple10
  • nk_dots_pack_size_mxfp8e5m2serialsapphireamxsmeskylakeneonneonfhmcudaamperehopperblackwellmetalapple9apple10
  • nk_dots_pack_size_e3m2serialsapphireamxsmehaswellskylakev128relaxedneonsdotneonfp8rvvcudaampereblackwellblackwellrtxrocmcdna3cdna4cdna5metalapple9apple10
  • nk_dots_pack_size_f32serialsmef64haswellskylakev128relaxedneonrvvpowervsxloongsonasxcudaampererocm
  • nk_dots_pack_size_f64serialsmef64haswellskylakev128relaxedneonrvvpowervsxloongsonasxcudaampererocm
  • nk_dots_pack_size_i8serialsapphireamxsmehaswellicelakealdersierrav128relaxedv128neonsdotrvvpowervsxloongsonasxcudaamperehopperblackwellrocmcdna3cdna4cdna5metalapple10
  • nk_dots_pack_size_u8serialsapphireamxsmehaswellicelakealdersierrav128relaxedv128neonsdotrvvpowervsxloongsonasxcudaamperehopperblackwellrocmcdna3cdna4cdna5metalapple10
  • nk_dots_pack_size_i4serialsmehaswellicelakev128relaxedneonsdotcudaamperehopperblackwellrocmcdna3cdna4cdna5metalapple10
  • nk_dots_pack_size_u4serialsmehaswellicelakev128relaxedneonsdotcudaamperehopperblackwellrocmcdna3cdna4cdna5metalapple10
  • nk_dots_pack_size_u1serialsmebi32haswellicelakev128neonpowervsxloongsonasx

Parameters

columnsinThe number of rows in B (output columns).
depthinColumns in B, counting dimensions, a multiple of the values per byte.
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_dots_packed_shape_*_best× 20Reads a packed B matrix's shape from its header.
dots.h:304 ↗
nk_status_t nk_dots_packed_shape_bf16_best(
    void const *b_packed,
    nk_size_t *columns,
    nk_size_t *depth,
    nk_capability_t capabilities,
    nk_stream_t stream);
SymbolBackends
nk_dots_packed_shape_bf16_best24
nk_dots_packed_shape_f16_best22
nk_dots_packed_shape_e4m3_best22
nk_dots_packed_shape_e5m2_best23
nk_dots_packed_shape_e2m3_best24
nk_dots_packed_shape_e2m1_best24
nk_dots_packed_shape_nvfp4_best13
nk_dots_packed_shape_mxfp4_best13
nk_dots_packed_shape_mxfp6e2m3_best8
nk_dots_packed_shape_mxfp6e3m2_best7
nk_dots_packed_shape_mxfp8e4m3_best13
nk_dots_packed_shape_mxfp8e5m2_best13
nk_dots_packed_shape_e3m2_best20
nk_dots_packed_shape_f32_best12
nk_dots_packed_shape_f64_best12
nk_dots_packed_shape_i8_best23
nk_dots_packed_shape_u8_best23
nk_dots_packed_shape_i4_best16
nk_dots_packed_shape_u4_best16
nk_dots_packed_shape_u1_best8

Backends

  • nk_dots_packed_shape_bf16serialgenoasapphireamxsmehaswellskylakev128relaxedv128neonneonbfdotrvvpowervsxloongsonasxcudaamperehopperblackwellrocmcdna3cdna4cdna5metalapple9apple10
  • nk_dots_packed_shape_f16serialgraniteamxsmehaswellskylakev128relaxedneonneonfhmrvvpowervsxloongsonasxcudaamperehopperblackwellrocmcdna3cdna4cdna5metalapple9apple10
  • nk_dots_packed_shape_e4m3serialgenoadiamondsapphireamxsmehaswellskylakev128relaxedneonfhmneonfp8rvvcudaampereblackwellblackwellrtxrocmcdna3cdna4cdna5metalapple9apple10
  • nk_dots_packed_shape_e5m2serialgenoadiamondsapphireamxgraniteamxsmehaswellskylakev128relaxedneonfhmneonfp8rvvcudaampereblackwellblackwellrtxrocmcdna3cdna4cdna5metalapple9apple10
  • nk_dots_packed_shape_e2m3serialsapphireamxsmehaswellskylakeicelakealdersierrav128relaxedneonsdotneonfp8rvvcudaamperehopperblackwellblackwellrtxrocmcdna3cdna4cdna5metalapple9apple10
  • nk_dots_packed_shape_e2m1serialsapphireamxsmehaswellskylakeicelakealdersierrav128relaxedneonsdotneonfp8rvvcudaamperehopperblackwellblackwellrtxrocmcdna3cdna4cdna5metalapple9apple10
  • nk_dots_packed_shape_nvfp4serialsapphireamxsmeskylakeneonneonsdotcudaamperehopperblackwellmetalapple9apple10
  • nk_dots_packed_shape_mxfp4serialsapphireamxsmeskylakeneonneonsdotcudaamperehopperblackwellmetalapple9apple10
  • nk_dots_packed_shape_mxfp6e2m3serialsmeskylakeneonneonsdotmetalapple9apple10
  • nk_dots_packed_shape_mxfp6e3m2serialsmeskylakeneonmetalapple9apple10
  • nk_dots_packed_shape_mxfp8e4m3serialsapphireamxsmeskylakeneonneonfhmcudaamperehopperblackwellmetalapple9apple10
  • nk_dots_packed_shape_mxfp8e5m2serialsapphireamxsmeskylakeneonneonfhmcudaamperehopperblackwellmetalapple9apple10
  • nk_dots_packed_shape_e3m2serialsapphireamxsmehaswellskylakev128relaxedneonsdotneonfp8rvvcudaampereblackwellblackwellrtxrocmcdna3cdna4cdna5metalapple9apple10
  • nk_dots_packed_shape_f32serialsmef64haswellskylakev128relaxedneonrvvpowervsxloongsonasxcudaampererocm
  • nk_dots_packed_shape_f64serialsmef64haswellskylakev128relaxedneonrvvpowervsxloongsonasxcudaampererocm
  • nk_dots_packed_shape_i8serialsapphireamxsmehaswellicelakealdersierrav128relaxedv128neonsdotrvvpowervsxloongsonasxcudaamperehopperblackwellrocmcdna3cdna4cdna5metalapple10
  • nk_dots_packed_shape_u8serialsapphireamxsmehaswellicelakealdersierrav128relaxedv128neonsdotrvvpowervsxloongsonasxcudaamperehopperblackwellrocmcdna3cdna4cdna5metalapple10
  • nk_dots_packed_shape_i4serialsmehaswellicelakev128relaxedneonsdotcudaamperehopperblackwellrocmcdna3cdna4cdna5metalapple10
  • nk_dots_packed_shape_u4serialsmehaswellicelakev128relaxedneonsdotcudaamperehopperblackwellrocmcdna3cdna4cdna5metalapple10
  • nk_dots_packed_shape_u1serialsmebi32haswellicelakev128neonpowervsxloongsonasx

Parameters

b_packedinA buffer produced by the matching nk_dots_pack_bf16_best.
columnsoutReceives the column count (B rows).
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_dots_pack_*_best× 20Packs the second multiplier (B) matrix into a backend-specific layout.
dots.h:380 ↗
nk_status_t nk_dots_pack_bf16_best(
    nk_bf16_t const *b,
    nk_size_t columns,
    nk_size_t depth,
    nk_size_t b_stride,
    void *b_packed,
    nk_size_t columns_begin,
    nk_size_t columns_end,
    nk_capability_t capabilities,
    nk_stream_t stream);
SymbolbBackends
nk_dots_pack_bf16_bestnk_bf16_t24
nk_dots_pack_f16_bestnk_f16_t22
nk_dots_pack_e4m3_bestnk_e4m3_t22
nk_dots_pack_e5m2_bestnk_e5m2_t23
nk_dots_pack_e2m3_bestnk_e2m3_t24
nk_dots_pack_e2m1_bestnk_e2m1x2_t24
nk_dots_pack_nvfp4_bestnk_nvfp4_cref_t13
nk_dots_pack_mxfp4_bestnk_mxfp4_cref_t13
nk_dots_pack_mxfp6e2m3_bestnk_mxfp6e2m3_cref_t8
nk_dots_pack_mxfp6e3m2_bestnk_mxfp6e3m2_cref_t7
nk_dots_pack_mxfp8e4m3_bestnk_mxfp8e4m3_cref_t13
nk_dots_pack_mxfp8e5m2_bestnk_mxfp8e5m2_cref_t13
nk_dots_pack_e3m2_bestnk_e3m2_t20
nk_dots_pack_f32_bestnk_f32_t12
nk_dots_pack_f64_bestnk_f64_t12
nk_dots_pack_i8_bestnk_i8_t23
nk_dots_pack_u8_bestnk_u8_t23
nk_dots_pack_i4_bestnk_i4x2_t16
nk_dots_pack_u4_bestnk_u4x2_t16
nk_dots_pack_u1_bestnk_u1x8_t8

Backends

  • nk_dots_pack_bf16serialgenoasapphireamxsmehaswellskylakev128relaxedv128neonneonbfdotrvvpowervsxloongsonasxcudaamperehopperblackwellrocmcdna3cdna4cdna5metalapple9apple10
  • nk_dots_pack_f16serialgraniteamxsmehaswellskylakev128relaxedneonneonfhmrvvpowervsxloongsonasxcudaamperehopperblackwellrocmcdna3cdna4cdna5metalapple9apple10
  • nk_dots_pack_e4m3serialgenoadiamondsapphireamxsmehaswellskylakev128relaxedneonfhmneonfp8rvvcudaampereblackwellblackwellrtxrocmcdna3cdna4cdna5metalapple9apple10
  • nk_dots_pack_e5m2serialgenoadiamondsapphireamxgraniteamxsmehaswellskylakev128relaxedneonfhmneonfp8rvvcudaampereblackwellblackwellrtxrocmcdna3cdna4cdna5metalapple9apple10
  • nk_dots_pack_e2m3serialsapphireamxsmehaswellskylakeicelakealdersierrav128relaxedneonsdotneonfp8rvvcudaamperehopperblackwellblackwellrtxrocmcdna3cdna4cdna5metalapple9apple10
  • nk_dots_pack_e2m1serialsapphireamxsmehaswellskylakeicelakealdersierrav128relaxedneonsdotneonfp8rvvcudaamperehopperblackwellblackwellrtxrocmcdna3cdna4cdna5metalapple9apple10
  • nk_dots_pack_nvfp4serialsapphireamxsmeskylakeneonneonsdotcudaamperehopperblackwellmetalapple9apple10
  • nk_dots_pack_mxfp4serialsapphireamxsmeskylakeneonneonsdotcudaamperehopperblackwellmetalapple9apple10
  • nk_dots_pack_mxfp6e2m3serialsmeskylakeneonneonsdotmetalapple9apple10
  • nk_dots_pack_mxfp6e3m2serialsmeskylakeneonmetalapple9apple10
  • nk_dots_pack_mxfp8e4m3serialsapphireamxsmeskylakeneonneonfhmcudaamperehopperblackwellmetalapple9apple10
  • nk_dots_pack_mxfp8e5m2serialsapphireamxsmeskylakeneonneonfhmcudaamperehopperblackwellmetalapple9apple10
  • nk_dots_pack_e3m2serialsapphireamxsmehaswellskylakev128relaxedneonsdotneonfp8rvvcudaampereblackwellblackwellrtxrocmcdna3cdna4cdna5metalapple9apple10
  • nk_dots_pack_f32serialsmef64haswellskylakev128relaxedneonrvvpowervsxloongsonasxcudaampererocm
  • nk_dots_pack_f64serialsmef64haswellskylakev128relaxedneonrvvpowervsxloongsonasxcudaampererocm
  • nk_dots_pack_i8serialsapphireamxsmehaswellicelakealdersierrav128relaxedv128neonsdotrvvpowervsxloongsonasxcudaamperehopperblackwellrocmcdna3cdna4cdna5metalapple10
  • nk_dots_pack_u8serialsapphireamxsmehaswellicelakealdersierrav128relaxedv128neonsdotrvvpowervsxloongsonasxcudaamperehopperblackwellrocmcdna3cdna4cdna5metalapple10
  • nk_dots_pack_i4serialsmehaswellicelakev128relaxedneonsdotcudaamperehopperblackwellrocmcdna3cdna4cdna5metalapple10
  • nk_dots_pack_u4serialsmehaswellicelakev128relaxedneonsdotcudaamperehopperblackwellrocmcdna3cdna4cdna5metalapple10
  • nk_dots_pack_u1serialsmebi32haswellicelakev128neonpowervsxloongsonasx

Parameters

binThe input B matrix in row-major order. Packing fuses no transposition, so a transposed B is transposed in a separate pass first.
columnsinThe number of rows in B (output columns).
depthinColumns in B, counting dimensions, a multiple of the values per byte.
b_strideinThe row stride in bytes for B.
b_packedoutThe output packed buffer from nk_dots_pack_size_bf16_best.
columns_begininFirst output column to pack; 0 for a full pack.
columns_endinOne past the last output column to pack; columns for a full pack.
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

Notes

Block-scaled B arrives as a reference to its first codes and scales. b_stride counts bytes of codes and whole blocks, and rows of scales sit b_stride / block_bytes apart, as nk_block_scaled_format_t says. The pack keeps B's tensor scale and every product applies it. depth must be a multiple of the block size: the CPU backends assert it, and the GPU backends return nk_unexpected_dimensions_k.

nk_dots_packed_workspace_size_*_best× 20Returns the scratch bytes that nk_dots_packed_bf16_best can use for these shapes.
dots.h:485 ↗
nk_status_t nk_dots_packed_workspace_size_bf16_best(
    nk_size_t rows,
    nk_size_t columns,
    nk_size_t depth,
    nk_capability_t capabilities,
    nk_size_t *bytes);
SymbolBackends
nk_dots_packed_workspace_size_bf16_best24
nk_dots_packed_workspace_size_f16_best22
nk_dots_packed_workspace_size_e4m3_best22
nk_dots_packed_workspace_size_e5m2_best23
nk_dots_packed_workspace_size_e2m3_best24
nk_dots_packed_workspace_size_e2m1_best24
nk_dots_packed_workspace_size_nvfp4_best13
nk_dots_packed_workspace_size_mxfp4_best13
nk_dots_packed_workspace_size_mxfp6e2m3_best8
nk_dots_packed_workspace_size_mxfp6e3m2_best7
nk_dots_packed_workspace_size_mxfp8e4m3_best13
nk_dots_packed_workspace_size_mxfp8e5m2_best13
nk_dots_packed_workspace_size_e3m2_best20
nk_dots_packed_workspace_size_f32_best12
nk_dots_packed_workspace_size_f64_best12
nk_dots_packed_workspace_size_i8_best23
nk_dots_packed_workspace_size_u8_best23
nk_dots_packed_workspace_size_i4_best16
nk_dots_packed_workspace_size_u4_best16
nk_dots_packed_workspace_size_u1_best8

Backends

  • nk_dots_packed_workspace_size_bf16serialgenoasapphireamxsmehaswellskylakev128relaxedv128neonneonbfdotrvvpowervsxloongsonasxcudaamperehopperblackwellrocmcdna3cdna4cdna5metalapple9apple10
  • nk_dots_packed_workspace_size_f16serialgraniteamxsmehaswellskylakev128relaxedneonneonfhmrvvpowervsxloongsonasxcudaamperehopperblackwellrocmcdna3cdna4cdna5metalapple9apple10
  • nk_dots_packed_workspace_size_e4m3serialgenoadiamondsapphireamxsmehaswellskylakev128relaxedneonfhmneonfp8rvvcudaampereblackwellblackwellrtxrocmcdna3cdna4cdna5metalapple9apple10
  • nk_dots_packed_workspace_size_e5m2serialgenoadiamondsapphireamxgraniteamxsmehaswellskylakev128relaxedneonfhmneonfp8rvvcudaampereblackwellblackwellrtxrocmcdna3cdna4cdna5metalapple9apple10
  • nk_dots_packed_workspace_size_e2m3serialsapphireamxsmehaswellskylakeicelakealdersierrav128relaxedneonsdotneonfp8rvvcudaamperehopperblackwellblackwellrtxrocmcdna3cdna4cdna5metalapple9apple10
  • nk_dots_packed_workspace_size_e2m1serialsapphireamxsmehaswellskylakeicelakealdersierrav128relaxedneonsdotneonfp8rvvcudaamperehopperblackwellblackwellrtxrocmcdna3cdna4cdna5metalapple9apple10
  • nk_dots_packed_workspace_size_nvfp4serialsapphireamxsmeskylakeneonneonsdotcudaamperehopperblackwellmetalapple9apple10
  • nk_dots_packed_workspace_size_mxfp4serialsapphireamxsmeskylakeneonneonsdotcudaamperehopperblackwellmetalapple9apple10
  • nk_dots_packed_workspace_size_mxfp6e2m3serialsmeskylakeneonneonsdotmetalapple9apple10
  • nk_dots_packed_workspace_size_mxfp6e3m2serialsmeskylakeneonmetalapple9apple10
  • nk_dots_packed_workspace_size_mxfp8e4m3serialsapphireamxsmeskylakeneonneonfhmcudaamperehopperblackwellmetalapple9apple10
  • nk_dots_packed_workspace_size_mxfp8e5m2serialsapphireamxsmeskylakeneonneonfhmcudaamperehopperblackwellmetalapple9apple10
  • nk_dots_packed_workspace_size_e3m2serialsapphireamxsmehaswellskylakev128relaxedneonsdotneonfp8rvvcudaampereblackwellblackwellrtxrocmcdna3cdna4cdna5metalapple9apple10
  • nk_dots_packed_workspace_size_f32serialsmef64haswellskylakev128relaxedneonrvvpowervsxloongsonasxcudaampererocm
  • nk_dots_packed_workspace_size_f64serialsmef64haswellskylakev128relaxedneonrvvpowervsxloongsonasxcudaampererocm
  • nk_dots_packed_workspace_size_i8serialsapphireamxsmehaswellicelakealdersierrav128relaxedv128neonsdotrvvpowervsxloongsonasxcudaamperehopperblackwellrocmcdna3cdna4cdna5metalapple10
  • nk_dots_packed_workspace_size_u8serialsapphireamxsmehaswellicelakealdersierrav128relaxedv128neonsdotrvvpowervsxloongsonasxcudaamperehopperblackwellrocmcdna3cdna4cdna5metalapple10
  • nk_dots_packed_workspace_size_i4serialsmehaswellicelakev128relaxedneonsdotcudaamperehopperblackwellrocmcdna3cdna4cdna5metalapple10
  • nk_dots_packed_workspace_size_u4serialsmehaswellicelakev128relaxedneonsdotcudaamperehopperblackwellrocmcdna3cdna4cdna5metalapple10
  • nk_dots_packed_workspace_size_u1serialsmebi32haswellicelakev128neonpowervsxloongsonasx

Parameters

rowsinThe number of rows in A.
columnsinThe number of rows in B (output columns).
depthinColumns in A and B, counting dimensions.
capabilitiesinOne device's capabilities, like nk_cpu_capabilities_enabled reports.
bytesoutThe workspace size, zero when no scratch is needed.

Statuses

nk_dots_packed_*_best× 20Computes C = A × Bᵀ using packed second multiplier matrix (B), accumulating into C.
dots.h:509 ↗
nk_status_t nk_dots_packed_bf16_best(
    nk_bf16_t const *a,
    void const *b_packed,
    nk_f32_t *c,
    nk_size_t rows,
    nk_size_t columns,
    nk_size_t depth,
    nk_size_t a_stride,
    nk_size_t c_stride,
    void *workspace,
    nk_size_t workspace_bytes,
    nk_capability_t capabilities,
    nk_stream_t stream);
SymbolacBackends
nk_dots_packed_bf16_bestnk_bf16_tnk_f32_t24
nk_dots_packed_f16_bestnk_f16_tnk_f32_t22
nk_dots_packed_e4m3_bestnk_e4m3_tnk_f32_t22
nk_dots_packed_e5m2_bestnk_e5m2_tnk_f32_t23
nk_dots_packed_e2m3_bestnk_e2m3_tnk_f32_t24
nk_dots_packed_e2m1_bestnk_e2m1x2_tnk_f32_t24
nk_dots_packed_nvfp4_bestnk_nvfp4_cref_tnk_f32_t13
nk_dots_packed_mxfp4_bestnk_mxfp4_cref_tnk_f32_t13
nk_dots_packed_mxfp6e2m3_bestnk_mxfp6e2m3_cref_tnk_f32_t8
nk_dots_packed_mxfp6e3m2_bestnk_mxfp6e3m2_cref_tnk_f32_t7
nk_dots_packed_mxfp8e4m3_bestnk_mxfp8e4m3_cref_tnk_f32_t13
nk_dots_packed_mxfp8e5m2_bestnk_mxfp8e5m2_cref_tnk_f32_t13
nk_dots_packed_e3m2_bestnk_e3m2_tnk_f32_t20
nk_dots_packed_f32_bestnk_f32_tnk_f64_t12
nk_dots_packed_f64_bestnk_f64_tnk_f64_t12
nk_dots_packed_i8_bestnk_i8_tnk_i32_t23
nk_dots_packed_u8_bestnk_u8_tnk_u32_t23
nk_dots_packed_i4_bestnk_i4x2_tnk_i32_t16
nk_dots_packed_u4_bestnk_u4x2_tnk_u32_t16
nk_dots_packed_u1_bestnk_u1x8_tnk_u32_t8

Backends

  • nk_dots_packed_bf16serialgenoasapphireamxsmehaswellskylakev128relaxedv128neonneonbfdotrvvpowervsxloongsonasxcudaamperehopperblackwellrocmcdna3cdna4cdna5metalapple9apple10
  • nk_dots_packed_f16serialgraniteamxsmehaswellskylakev128relaxedneonneonfhmrvvpowervsxloongsonasxcudaamperehopperblackwellrocmcdna3cdna4cdna5metalapple9apple10
  • nk_dots_packed_e4m3serialgenoadiamondsapphireamxsmehaswellskylakev128relaxedneonfhmneonfp8rvvcudaampereblackwellblackwellrtxrocmcdna3cdna4cdna5metalapple9apple10
  • nk_dots_packed_e5m2serialgenoadiamondsapphireamxgraniteamxsmehaswellskylakev128relaxedneonfhmneonfp8rvvcudaampereblackwellblackwellrtxrocmcdna3cdna4cdna5metalapple9apple10
  • nk_dots_packed_e2m3serialsapphireamxsmehaswellskylakeicelakealdersierrav128relaxedneonsdotneonfp8rvvcudaamperehopperblackwellblackwellrtxrocmcdna3cdna4cdna5metalapple9apple10
  • nk_dots_packed_e2m1serialsapphireamxsmehaswellskylakeicelakealdersierrav128relaxedneonsdotneonfp8rvvcudaamperehopperblackwellblackwellrtxrocmcdna3cdna4cdna5metalapple9apple10
  • nk_dots_packed_nvfp4serialsapphireamxsmeskylakeneonneonsdotcudaamperehopperblackwellmetalapple9apple10
  • nk_dots_packed_mxfp4serialsapphireamxsmeskylakeneonneonsdotcudaamperehopperblackwellmetalapple9apple10
  • nk_dots_packed_mxfp6e2m3serialsmeskylakeneonneonsdotmetalapple9apple10
  • nk_dots_packed_mxfp6e3m2serialsmeskylakeneonmetalapple9apple10
  • nk_dots_packed_mxfp8e4m3serialsapphireamxsmeskylakeneonneonfhmcudaamperehopperblackwellmetalapple9apple10
  • nk_dots_packed_mxfp8e5m2serialsapphireamxsmeskylakeneonneonfhmcudaamperehopperblackwellmetalapple9apple10
  • nk_dots_packed_e3m2serialsapphireamxsmehaswellskylakev128relaxedneonsdotneonfp8rvvcudaampereblackwellblackwellrtxrocmcdna3cdna4cdna5metalapple9apple10
  • nk_dots_packed_f32serialsmef64haswellskylakev128relaxedneonrvvpowervsxloongsonasxcudaampererocm
  • nk_dots_packed_f64serialsmef64haswellskylakev128relaxedneonrvvpowervsxloongsonasxcudaampererocm
  • nk_dots_packed_i8serialsapphireamxsmehaswellicelakealdersierrav128relaxedv128neonsdotrvvpowervsxloongsonasxcudaamperehopperblackwellrocmcdna3cdna4cdna5metalapple10
  • nk_dots_packed_u8serialsapphireamxsmehaswellicelakealdersierrav128relaxedv128neonsdotrvvpowervsxloongsonasxcudaamperehopperblackwellrocmcdna3cdna4cdna5metalapple10
  • nk_dots_packed_i4serialsmehaswellicelakev128relaxedneonsdotcudaamperehopperblackwellrocmcdna3cdna4cdna5metalapple10
  • nk_dots_packed_u4serialsmehaswellicelakev128relaxedneonsdotcudaamperehopperblackwellrocmcdna3cdna4cdna5metalapple10
  • nk_dots_packed_u1serialsmebi32haswellicelakev128neonpowervsxloongsonasx

Parameters

ainThe input A matrix in row-major order.
b_packedinThe packed B matrix produced.
coutThe output C matrix in row-major order.
rowsinThe number of rows in A.
columnsinThe number of rows in B (output columns).
depthinColumns in A and B, counting dimensions, a multiple of the values per byte.
a_strideinThe row stride in bytes for A.
c_strideinThe row stride in bytes for C.
workspaceinScratch memory aligned to nk_default_alignment_k, device memory ordered by stream for GPU capabilities. Null with zero bytes selects the scratch-free path.
workspace_bytesinAt least what nk_dots_packed_workspace_size_bf16_best reports, or zero. Anything between zero and that size returns nk_unexpected_dimensions_k.
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

Notes

Block-scaled A arrives as a reference, laid out as nk_dots_pack_nvfp4_best describes for B, and C holds true values, so every product takes the block scales and the tensor scales of both operands.

nk_dots_symmetric_workspace_size_*_best× 20Returns the scratch bytes that nk_dots_symmetric_bf16_best can use for these shapes.
dots.h:691 ↗
nk_status_t nk_dots_symmetric_workspace_size_bf16_best(
    nk_size_t vector_count,
    nk_size_t depth,
    nk_size_t rows_begin,
    nk_size_t rows_end,
    nk_capability_t capabilities,
    nk_size_t *bytes);
SymbolBackends
nk_dots_symmetric_workspace_size_bf16_best24
nk_dots_symmetric_workspace_size_f16_best22
nk_dots_symmetric_workspace_size_e4m3_best22
nk_dots_symmetric_workspace_size_e5m2_best23
nk_dots_symmetric_workspace_size_e2m3_best24
nk_dots_symmetric_workspace_size_e2m1_best24
nk_dots_symmetric_workspace_size_nvfp4_best13
nk_dots_symmetric_workspace_size_mxfp4_best13
nk_dots_symmetric_workspace_size_mxfp6e2m3_best8
nk_dots_symmetric_workspace_size_mxfp6e3m2_best7
nk_dots_symmetric_workspace_size_mxfp8e4m3_best13
nk_dots_symmetric_workspace_size_mxfp8e5m2_best13
nk_dots_symmetric_workspace_size_e3m2_best20
nk_dots_symmetric_workspace_size_f32_best12
nk_dots_symmetric_workspace_size_f64_best12
nk_dots_symmetric_workspace_size_i8_best23
nk_dots_symmetric_workspace_size_u8_best23
nk_dots_symmetric_workspace_size_i4_best16
nk_dots_symmetric_workspace_size_u4_best16
nk_dots_symmetric_workspace_size_u1_best8

Backends

  • nk_dots_symmetric_workspace_size_bf16serialgenoasapphireamxsmehaswellskylakev128relaxedv128neonneonbfdotrvvpowervsxloongsonasxcudaamperehopperblackwellrocmcdna3cdna4cdna5metalapple9apple10
  • nk_dots_symmetric_workspace_size_f16serialgraniteamxsmehaswellskylakev128relaxedneonneonfhmrvvpowervsxloongsonasxcudaamperehopperblackwellrocmcdna3cdna4cdna5metalapple9apple10
  • nk_dots_symmetric_workspace_size_e4m3serialgenoadiamondsapphireamxsmehaswellskylakev128relaxedneonfhmneonfp8rvvcudaampereblackwellblackwellrtxrocmcdna3cdna4cdna5metalapple9apple10
  • nk_dots_symmetric_workspace_size_e5m2serialgenoadiamondsapphireamxgraniteamxsmehaswellskylakev128relaxedneonfhmneonfp8rvvcudaampereblackwellblackwellrtxrocmcdna3cdna4cdna5metalapple9apple10
  • nk_dots_symmetric_workspace_size_e2m3serialsapphireamxsmehaswellskylakeicelakealdersierrav128relaxedneonsdotneonfp8rvvcudaamperehopperblackwellblackwellrtxrocmcdna3cdna4cdna5metalapple9apple10
  • nk_dots_symmetric_workspace_size_e2m1serialsapphireamxsmehaswellskylakeicelakealdersierrav128relaxedneonsdotneonfp8rvvcudaamperehopperblackwellblackwellrtxrocmcdna3cdna4cdna5metalapple9apple10
  • nk_dots_symmetric_workspace_size_nvfp4serialsapphireamxsmeskylakeneonneonsdotcudaamperehopperblackwellmetalapple9apple10
  • nk_dots_symmetric_workspace_size_mxfp4serialsapphireamxsmeskylakeneonneonsdotcudaamperehopperblackwellmetalapple9apple10
  • nk_dots_symmetric_workspace_size_mxfp6e2m3serialsmeskylakeneonneonsdotmetalapple9apple10
  • nk_dots_symmetric_workspace_size_mxfp6e3m2serialsmeskylakeneonmetalapple9apple10
  • nk_dots_symmetric_workspace_size_mxfp8e4m3serialsapphireamxsmeskylakeneonneonfhmcudaamperehopperblackwellmetalapple9apple10
  • nk_dots_symmetric_workspace_size_mxfp8e5m2serialsapphireamxsmeskylakeneonneonfhmcudaamperehopperblackwellmetalapple9apple10
  • nk_dots_symmetric_workspace_size_e3m2serialsapphireamxsmehaswellskylakev128relaxedneonsdotneonfp8rvvcudaampereblackwellblackwellrtxrocmcdna3cdna4cdna5metalapple9apple10
  • nk_dots_symmetric_workspace_size_f32serialsmef64haswellskylakev128relaxedneonrvvpowervsxloongsonasxcudaampererocm
  • nk_dots_symmetric_workspace_size_f64serialsmef64haswellskylakev128relaxedneonrvvpowervsxloongsonasxcudaampererocm
  • nk_dots_symmetric_workspace_size_i8serialsapphireamxsmehaswellicelakealdersierrav128relaxedv128neonsdotrvvpowervsxloongsonasxcudaamperehopperblackwellrocmcdna3cdna4cdna5metalapple10
  • nk_dots_symmetric_workspace_size_u8serialsapphireamxsmehaswellicelakealdersierrav128relaxedv128neonsdotrvvpowervsxloongsonasxcudaamperehopperblackwellrocmcdna3cdna4cdna5metalapple10
  • nk_dots_symmetric_workspace_size_i4serialsmehaswellicelakev128relaxedneonsdotcudaamperehopperblackwellrocmcdna3cdna4cdna5metalapple10
  • nk_dots_symmetric_workspace_size_u4serialsmehaswellicelakev128relaxedneonsdotcudaamperehopperblackwellrocmcdna3cdna4cdna5metalapple10
  • nk_dots_symmetric_workspace_size_u1serialsmebi32haswellicelakev128neonpowervsxloongsonasx

Parameters

vector_countinNumber of vectors (rows) in the input matrix.
depthinCounts dimensions.
rows_begininFirst result row to compute.
rows_endinResult row after the last to compute, clamped to vector_count.
capabilitiesinOne device's capabilities, like nk_cpu_capabilities_enabled reports.
bytesoutThe workspace size, zero when no scratch is needed.

Statuses

nk_dots_symmetric_*_best× 20Computes C = A × Aᵀ symmetric Gram matrix.
dots.h:719 ↗
nk_status_t nk_dots_symmetric_bf16_best(
    nk_bf16_t const *vectors,
    nk_size_t vector_count,
    nk_size_t depth,
    nk_size_t stride,
    nk_f32_t *result,
    nk_size_t result_stride,
    nk_size_t rows_begin,
    nk_size_t rows_end,
    void *workspace,
    nk_size_t workspace_bytes,
    nk_capability_t capabilities,
    nk_stream_t stream);
SymbolvectorsresultBackends
nk_dots_symmetric_bf16_bestnk_bf16_tnk_f32_t24
nk_dots_symmetric_f16_bestnk_f16_tnk_f32_t22
nk_dots_symmetric_e4m3_bestnk_e4m3_tnk_f32_t22
nk_dots_symmetric_e5m2_bestnk_e5m2_tnk_f32_t23
nk_dots_symmetric_e2m3_bestnk_e2m3_tnk_f32_t24
nk_dots_symmetric_e2m1_bestnk_e2m1x2_tnk_f32_t24
nk_dots_symmetric_nvfp4_bestnk_nvfp4_cref_tnk_f32_t13
nk_dots_symmetric_mxfp4_bestnk_mxfp4_cref_tnk_f32_t13
nk_dots_symmetric_mxfp6e2m3_bestnk_mxfp6e2m3_cref_tnk_f32_t8
nk_dots_symmetric_mxfp6e3m2_bestnk_mxfp6e3m2_cref_tnk_f32_t7
nk_dots_symmetric_mxfp8e4m3_bestnk_mxfp8e4m3_cref_tnk_f32_t13
nk_dots_symmetric_mxfp8e5m2_bestnk_mxfp8e5m2_cref_tnk_f32_t13
nk_dots_symmetric_e3m2_bestnk_e3m2_tnk_f32_t20
nk_dots_symmetric_f32_bestnk_f32_tnk_f64_t12
nk_dots_symmetric_f64_bestnk_f64_tnk_f64_t12
nk_dots_symmetric_i8_bestnk_i8_tnk_i32_t23
nk_dots_symmetric_u8_bestnk_u8_tnk_u32_t23
nk_dots_symmetric_i4_bestnk_i4x2_tnk_i32_t16
nk_dots_symmetric_u4_bestnk_u4x2_tnk_u32_t16
nk_dots_symmetric_u1_bestnk_u1x8_tnk_u32_t8

Backends

  • nk_dots_symmetric_bf16serialgenoasapphireamxsmehaswellskylakev128relaxedv128neonneonbfdotrvvpowervsxloongsonasxcudaamperehopperblackwellrocmcdna3cdna4cdna5metalapple9apple10
  • nk_dots_symmetric_f16serialgraniteamxsmehaswellskylakev128relaxedneonneonfhmrvvpowervsxloongsonasxcudaamperehopperblackwellrocmcdna3cdna4cdna5metalapple9apple10
  • nk_dots_symmetric_e4m3serialgenoadiamondsapphireamxsmehaswellskylakev128relaxedneonfhmneonfp8rvvcudaampereblackwellblackwellrtxrocmcdna3cdna4cdna5metalapple9apple10
  • nk_dots_symmetric_e5m2serialgenoadiamondsapphireamxgraniteamxsmehaswellskylakev128relaxedneonfhmneonfp8rvvcudaampereblackwellblackwellrtxrocmcdna3cdna4cdna5metalapple9apple10
  • nk_dots_symmetric_e2m3serialsapphireamxsmehaswellskylakeicelakealdersierrav128relaxedneonsdotneonfp8rvvcudaamperehopperblackwellblackwellrtxrocmcdna3cdna4cdna5metalapple9apple10
  • nk_dots_symmetric_e2m1serialsapphireamxsmehaswellskylakeicelakealdersierrav128relaxedneonsdotneonfp8rvvcudaamperehopperblackwellblackwellrtxrocmcdna3cdna4cdna5metalapple9apple10
  • nk_dots_symmetric_nvfp4serialsapphireamxsmeskylakeneonneonsdotcudaamperehopperblackwellmetalapple9apple10
  • nk_dots_symmetric_mxfp4serialsapphireamxsmeskylakeneonneonsdotcudaamperehopperblackwellmetalapple9apple10
  • nk_dots_symmetric_mxfp6e2m3serialsmeskylakeneonneonsdotmetalapple9apple10
  • nk_dots_symmetric_mxfp6e3m2serialsmeskylakeneonmetalapple9apple10
  • nk_dots_symmetric_mxfp8e4m3serialsapphireamxsmeskylakeneonneonfhmcudaamperehopperblackwellmetalapple9apple10
  • nk_dots_symmetric_mxfp8e5m2serialsapphireamxsmeskylakeneonneonfhmcudaamperehopperblackwellmetalapple9apple10
  • nk_dots_symmetric_e3m2serialsapphireamxsmehaswellskylakev128relaxedneonsdotneonfp8rvvcudaampereblackwellblackwellrtxrocmcdna3cdna4cdna5metalapple9apple10
  • nk_dots_symmetric_f32serialsmef64haswellskylakev128relaxedneonrvvpowervsxloongsonasxcudaampererocm
  • nk_dots_symmetric_f64serialsmef64haswellskylakev128relaxedneonrvvpowervsxloongsonasxcudaampererocm
  • nk_dots_symmetric_i8serialsapphireamxsmehaswellicelakealdersierrav128relaxedv128neonsdotrvvpowervsxloongsonasxcudaamperehopperblackwellrocmcdna3cdna4cdna5metalapple10
  • nk_dots_symmetric_u8serialsapphireamxsmehaswellicelakealdersierrav128relaxedv128neonsdotrvvpowervsxloongsonasxcudaamperehopperblackwellrocmcdna3cdna4cdna5metalapple10
  • nk_dots_symmetric_i4serialsmehaswellicelakev128relaxedneonsdotcudaamperehopperblackwellrocmcdna3cdna4cdna5metalapple10
  • nk_dots_symmetric_u4serialsmehaswellicelakev128relaxedneonsdotcudaamperehopperblackwellrocmcdna3cdna4cdna5metalapple10
  • nk_dots_symmetric_u1serialsmebi32haswellicelakev128neonpowervsxloongsonasx

Visits the upper triangle, the band (0, NUMKONG_SIZE_MAX) of nk_diagonal_band_t.

Parameters

vectorsinInput matrix of row vectors in row-major order.
vector_countinNumber of vectors (rows) in the input matrix.
depthinCounts dimensions, a multiple of the values per byte.
strideinRow stride in bytes for the input matrix.
resultoutOutput symmetric matrix of vector_count × vector_count.
result_strideinRow stride in bytes for the result matrix.
rows_begininFirst result row to compute, so threads split the work by rows.
rows_endinResult row after the last to compute, clamped to vector_count.
workspaceinScratch memory aligned to nk_default_alignment_k, device memory ordered by stream for GPU capabilities. Null with zero bytes selects the scratch-free path.
workspace_bytesinAt least what nk_dots_symmetric_workspace_size_bf16_best reports, or zero. Anything between zero and that size returns nk_unexpected_dimensions_k.
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_*_find_kernel× 4Finds the dots kernel of kind for dtype, from the best of capabilities.
dots.h:10147 ↗
nk_status_t nk_dots_find_kernel(
    nk_kernel_kind_t kind,
    nk_dtype_t dtype,
    nk_capability_t capabilities,
    nk_kernel_punned_t *kernel,
    nk_capability_t *capability);
Symbol
nk_dots_find_kernel
nk_sets_find_kernel
nk_spatials_find_kernel
nk_attention_find_kernel

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

nk_hammings_packed_u1_bestCompute Hamming distances between V rows and packed Q rows.
sets.h:79 ↗
nk_status_t nk_hammings_packed_u1_best(
    nk_u1x8_t const *v,
    void const *query_packed,
    nk_u32_t *result,
    nk_size_t rows,
    nk_size_t columns,
    nk_size_t d,
    nk_size_t v_stride,
    nk_size_t r_stride,
    void *workspace,
    nk_size_t workspace_bytes,
    nk_capability_t capabilities,
    nk_stream_t stream);

Backends

  • nk_hammings_packed_u1serialsmebi32haswellicelakeneonv128powervsxloongsonasx

Parameters

vinInput values matrix
query_packedinPacked queries matrix
resultoutRow-major results matrix
rowsinNumber of rows in the results matrix
columnsinNumber of columns in the results matrix
dinCounts dimensions, a multiple of the values per byte.
v_strideinByte stride between rows of A
r_strideinByte stride between rows of C
workspaceinScratch aligned to nk_default_alignment_k, or null with zero bytes.
workspace_bytesinAt least what the matching workspace-size query reports, or zero.
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_hammings_symmetric_u1_bestComputes C = A × Aᵀ symmetric Gram matrix of Hamming distances.
sets.h:102 ↗
nk_status_t nk_hammings_symmetric_u1_best(
    nk_u1x8_t const *vectors,
    nk_size_t vector_count,
    nk_size_t d,
    nk_size_t stride,
    nk_u32_t *result,
    nk_size_t result_stride,
    nk_size_t rows_begin,
    nk_size_t rows_end,
    void *workspace,
    nk_size_t workspace_bytes,
    nk_capability_t capabilities,
    nk_stream_t stream);

Backends

  • nk_hammings_symmetric_u1serialsmebi32haswellicelakeneonv128powervsxloongsonasx

Parameters

vectorsinInput matrix of row vectors in row-major order.
vector_countinNumber of vectors (rows) in the input matrix.
dinCounts dimensions, a multiple of the values per byte.
strideinRow stride in bytes for the input matrix.
resultoutOutput symmetric matrix of vector_count × vector_count.
result_strideinRow stride in bytes for the result matrix.
rows_begininFirst result row to compute, so threads split the work by rows.
rows_endinResult row after the last to compute, clamped to vector_count.
workspaceinScratch aligned to nk_default_alignment_k, or null with zero bytes.
workspace_bytesinAt least what the matching workspace-size query reports, or zero.
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_jaccards_packed_u1_bestCompute Jaccard distances between V rows and packed Q rows.
sets.h:126 ↗
nk_status_t nk_jaccards_packed_u1_best(
    nk_u1x8_t const *v,
    void const *query_packed,
    nk_f32_t *result,
    nk_size_t rows,
    nk_size_t columns,
    nk_size_t d,
    nk_size_t v_stride,
    nk_size_t r_stride,
    void *workspace,
    nk_size_t workspace_bytes,
    nk_capability_t capabilities,
    nk_stream_t stream);

Backends

  • nk_jaccards_packed_u1serialsmebi32haswellicelakeneonv128powervsxloongsonasx

Parameters

vinInput values matrix
query_packedinPacked queries matrix (with norms)
resultoutRow-major f32 results matrix
rowsinNumber of rows in the results matrix
columnsinNumber of columns in the results matrix
dinCounts dimensions, a multiple of the values per byte.
v_strideinByte stride between rows of A
r_strideinByte stride between rows of C
workspaceinScratch aligned to nk_default_alignment_k, or null with zero bytes.
workspace_bytesinAt least what the matching workspace-size query reports, or zero.
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_jaccards_symmetric_u1_bestComputes C = f(A, Aᵀ) symmetric Gram matrix of Jaccard distances.
sets.h:149 ↗
nk_status_t nk_jaccards_symmetric_u1_best(
    nk_u1x8_t const *vectors,
    nk_size_t vector_count,
    nk_size_t d,
    nk_size_t stride,
    nk_f32_t *result,
    nk_size_t result_stride,
    nk_size_t rows_begin,
    nk_size_t rows_end,
    void *workspace,
    nk_size_t workspace_bytes,
    nk_capability_t capabilities,
    nk_stream_t stream);

Backends

  • nk_jaccards_symmetric_u1serialsmebi32haswellicelakeneonv128powervsxloongsonasx

Parameters

vectorsinInput matrix of row vectors in row-major order.
vector_countinNumber of vectors (rows).
dinCounts dimensions, a multiple of the values per byte.
strideinRow stride in bytes.
resultoutOutput symmetric f32 matrix of vector_count × vector_count.
result_strideinRow stride in bytes for the result matrix.
rows_begininFirst result row to compute, so threads split the work by rows.
rows_endinResult row after the last to compute, clamped to vector_count.
workspaceinScratch aligned to nk_default_alignment_k, or null with zero bytes.
workspace_bytesinAt least what the matching workspace-size query reports, or zero.
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_*_packed_workspace_size_u1_best× 2Returns the workspace bytes of nk_hammings_packed_u1_best for these shapes.
sets.h:163 ↗
nk_status_t nk_hammings_packed_workspace_size_u1_best(
    nk_size_t rows,
    nk_size_t columns,
    nk_size_t d,
    nk_capability_t capabilities,
    nk_size_t *bytes);
SymbolBackends
nk_hammings_packed_workspace_size_u1_best8
nk_jaccards_packed_workspace_size_u1_best8

Backends

  • nk_hammings_packed_workspace_size_u1serialsmebi32haswellicelakeneonv128powervsxloongsonasx
  • nk_jaccards_packed_workspace_size_u1serialsmebi32haswellicelakeneonv128powervsxloongsonasx

Parameters

rowsinNumber of rows in the results matrix
columnsinNumber of columns in the results matrix
dinCounts dimensions, a multiple of the values per byte.
capabilitiesinOne device's capabilities, like nk_cpu_capabilities_enabled reports.
bytesoutThe scratch the fast path takes, zero when the kernel needs none.
nk_*_symmetric_workspace_size_u1_best× 2Returns the workspace bytes of nk_hammings_symmetric_u1_best for these shapes.
sets.h:175 ↗
nk_status_t nk_hammings_symmetric_workspace_size_u1_best(
    nk_size_t vector_count,
    nk_size_t d,
    nk_size_t rows_begin,
    nk_size_t rows_end,
    nk_capability_t capabilities,
    nk_size_t *bytes);
SymbolBackends
nk_hammings_symmetric_workspace_size_u1_best8
nk_jaccards_symmetric_workspace_size_u1_best8

Backends

  • nk_hammings_symmetric_workspace_size_u1serialsmebi32haswellicelakeneonv128powervsxloongsonasx
  • nk_jaccards_symmetric_workspace_size_u1serialsmebi32haswellicelakeneonv128powervsxloongsonasx

Parameters

vector_countinNumber of vectors (rows) in the input matrix.
dinCounts dimensions, a multiple of the values per byte.
rows_begininFirst result row to compute.
rows_endinResult row after the last to compute.
capabilitiesinOne device's capabilities, like nk_cpu_capabilities_enabled reports.
bytesoutThe scratch the fast path takes, zero when the kernel needs none.
nk_angulars_packed_workspace_size_*_best× 19Returns the workspace bytes of nk_angulars_packed_f32_best for one shape.
spatials.h:95 ↗
nk_status_t nk_angulars_packed_workspace_size_f32_best(
    nk_size_t rows,
    nk_size_t columns,
    nk_size_t depth,
    nk_capability_t capabilities,
    nk_size_t *bytes);
SymbolBackends
nk_angulars_packed_workspace_size_f32_best12
nk_angulars_packed_workspace_size_f64_best12
nk_angulars_packed_workspace_size_f16_best22
nk_angulars_packed_workspace_size_bf16_best24
nk_angulars_packed_workspace_size_e4m3_best22
nk_angulars_packed_workspace_size_e5m2_best23
nk_angulars_packed_workspace_size_e2m3_best24
nk_angulars_packed_workspace_size_e2m1_best24
nk_angulars_packed_workspace_size_nvfp4_best11
nk_angulars_packed_workspace_size_mxfp4_best11
nk_angulars_packed_workspace_size_mxfp6e2m3_best8
nk_angulars_packed_workspace_size_mxfp6e3m2_best7
nk_angulars_packed_workspace_size_mxfp8e4m3_best11
nk_angulars_packed_workspace_size_mxfp8e5m2_best11
nk_angulars_packed_workspace_size_e3m2_best20
nk_angulars_packed_workspace_size_i8_best23
nk_angulars_packed_workspace_size_u8_best23
nk_angulars_packed_workspace_size_i4_best16
nk_angulars_packed_workspace_size_u4_best16

Backends

  • nk_angulars_packed_workspace_size_f32serialsmef64haswellskylakev128relaxedneonrvvpowervsxloongsonasxcudaampererocm
  • nk_angulars_packed_workspace_size_f64serialsmef64haswellskylakev128relaxedneonrvvpowervsxloongsonasxcudaampererocm
  • nk_angulars_packed_workspace_size_f16serialgraniteamxsmehaswellskylakev128relaxedneonneonfhmrvvpowervsxloongsonasxcudaamperehopperblackwellrocmcdna3cdna4cdna5metalapple9apple10
  • nk_angulars_packed_workspace_size_bf16serialgenoasapphireamxsmehaswellskylakev128relaxedv128neonneonbfdotrvvpowervsxloongsonasxcudaamperehopperblackwellrocmcdna3cdna4cdna5metalapple9apple10
  • nk_angulars_packed_workspace_size_e4m3serialgenoadiamondsapphireamxsmehaswellskylakev128relaxedneonfhmneonfp8rvvcudaampereblackwellblackwellrtxrocmcdna3cdna4cdna5metalapple9apple10
  • nk_angulars_packed_workspace_size_e5m2serialgenoadiamondsapphireamxgraniteamxsmehaswellskylakev128relaxedneonfhmneonfp8rvvcudaampereblackwellblackwellrtxrocmcdna3cdna4cdna5metalapple9apple10
  • nk_angulars_packed_workspace_size_e2m3serialsapphireamxsmehaswellskylakeicelakealdersierrav128relaxedneonsdotneonfp8rvvcudaamperehopperblackwellblackwellrtxrocmcdna3cdna4cdna5metalapple9apple10
  • nk_angulars_packed_workspace_size_e2m1serialsapphireamxsmehaswellskylakeicelakealdersierrav128relaxedneonsdotneonfp8rvvcudaamperehopperblackwellblackwellrtxrocmcdna3cdna4cdna5metalapple9apple10
  • nk_angulars_packed_workspace_size_nvfp4serialsapphireamxsmeskylakeneonneonsdotcudablackwellmetalapple9apple10
  • nk_angulars_packed_workspace_size_mxfp4serialsapphireamxsmeskylakeneonneonsdotcudablackwellmetalapple9apple10
  • nk_angulars_packed_workspace_size_mxfp6e2m3serialsmeskylakeneonneonsdotmetalapple9apple10
  • nk_angulars_packed_workspace_size_mxfp6e3m2serialsmeskylakeneonmetalapple9apple10
  • nk_angulars_packed_workspace_size_mxfp8e4m3serialsapphireamxsmeskylakeneonneonfhmcudablackwellmetalapple9apple10
  • nk_angulars_packed_workspace_size_mxfp8e5m2serialsapphireamxsmeskylakeneonneonfhmcudablackwellmetalapple9apple10
  • nk_angulars_packed_workspace_size_e3m2serialsapphireamxsmehaswellskylakev128relaxedneonsdotneonfp8rvvcudaampereblackwellblackwellrtxrocmcdna3cdna4cdna5metalapple9apple10
  • nk_angulars_packed_workspace_size_i8serialsapphireamxsmehaswellicelakealdersierrav128relaxedv128neonsdotrvvpowervsxloongsonasxcudaamperehopperblackwellrocmcdna3cdna4cdna5metalapple10
  • nk_angulars_packed_workspace_size_u8serialsapphireamxsmehaswellicelakealdersierrav128relaxedv128neonsdotrvvpowervsxloongsonasxcudaamperehopperblackwellrocmcdna3cdna4cdna5metalapple10
  • nk_angulars_packed_workspace_size_i4serialsmehaswellicelakev128relaxedneonsdotcudaamperehopperblackwellrocmcdna3cdna4cdna5metalapple10
  • nk_angulars_packed_workspace_size_u4serialsmehaswellicelakev128relaxedneonsdotcudaamperehopperblackwellrocmcdna3cdna4cdna5metalapple10

Parameters

rowsinNumber of rows in A.
columnsinNumber of columns in B (packed).
depthinShared inner dimension in dimensions.
capabilitiesinOne device's capabilities, like nk_cpu_capabilities_enabled reports.
bytesoutThe scratch size, 0 when the capability needs none.

Statuses

nk_angulars_symmetric_workspace_size_*_best× 19Returns the workspace bytes of nk_angulars_symmetric_f32_best for one shape.
spatials.h:110 ↗
nk_status_t nk_angulars_symmetric_workspace_size_f32_best(
    nk_size_t vector_count,
    nk_size_t depth,
    nk_size_t rows_begin,
    nk_size_t rows_end,
    nk_capability_t capabilities,
    nk_size_t *bytes);
SymbolBackends
nk_angulars_symmetric_workspace_size_f32_best12
nk_angulars_symmetric_workspace_size_f64_best12
nk_angulars_symmetric_workspace_size_f16_best22
nk_angulars_symmetric_workspace_size_bf16_best24
nk_angulars_symmetric_workspace_size_e4m3_best22
nk_angulars_symmetric_workspace_size_e5m2_best23
nk_angulars_symmetric_workspace_size_e2m3_best24
nk_angulars_symmetric_workspace_size_e2m1_best24
nk_angulars_symmetric_workspace_size_nvfp4_best11
nk_angulars_symmetric_workspace_size_mxfp4_best11
nk_angulars_symmetric_workspace_size_mxfp6e2m3_best8
nk_angulars_symmetric_workspace_size_mxfp6e3m2_best7
nk_angulars_symmetric_workspace_size_mxfp8e4m3_best11
nk_angulars_symmetric_workspace_size_mxfp8e5m2_best11
nk_angulars_symmetric_workspace_size_e3m2_best20
nk_angulars_symmetric_workspace_size_i8_best23
nk_angulars_symmetric_workspace_size_u8_best23
nk_angulars_symmetric_workspace_size_i4_best16
nk_angulars_symmetric_workspace_size_u4_best16

Backends

  • nk_angulars_symmetric_workspace_size_f32serialsmef64haswellskylakev128relaxedneonrvvpowervsxloongsonasxcudaampererocm
  • nk_angulars_symmetric_workspace_size_f64serialsmef64haswellskylakev128relaxedneonrvvpowervsxloongsonasxcudaampererocm
  • nk_angulars_symmetric_workspace_size_f16serialgraniteamxsmehaswellskylakev128relaxedneonneonfhmrvvpowervsxloongsonasxcudaamperehopperblackwellrocmcdna3cdna4cdna5metalapple9apple10
  • nk_angulars_symmetric_workspace_size_bf16serialgenoasapphireamxsmehaswellskylakev128relaxedv128neonneonbfdotrvvpowervsxloongsonasxcudaamperehopperblackwellrocmcdna3cdna4cdna5metalapple9apple10
  • nk_angulars_symmetric_workspace_size_e4m3serialgenoadiamondsapphireamxsmehaswellskylakev128relaxedneonfhmneonfp8rvvcudaampereblackwellblackwellrtxrocmcdna3cdna4cdna5metalapple9apple10
  • nk_angulars_symmetric_workspace_size_e5m2serialgenoadiamondsapphireamxgraniteamxsmehaswellskylakev128relaxedneonfhmneonfp8rvvcudaampereblackwellblackwellrtxrocmcdna3cdna4cdna5metalapple9apple10
  • nk_angulars_symmetric_workspace_size_e2m3serialsapphireamxsmehaswellskylakeicelakealdersierrav128relaxedneonsdotneonfp8rvvcudaamperehopperblackwellblackwellrtxrocmcdna3cdna4cdna5metalapple9apple10
  • nk_angulars_symmetric_workspace_size_e2m1serialsapphireamxsmehaswellskylakeicelakealdersierrav128relaxedneonsdotneonfp8rvvcudaamperehopperblackwellblackwellrtxrocmcdna3cdna4cdna5metalapple9apple10
  • nk_angulars_symmetric_workspace_size_nvfp4serialsapphireamxsmeskylakeneonneonsdotcudablackwellmetalapple9apple10
  • nk_angulars_symmetric_workspace_size_mxfp4serialsapphireamxsmeskylakeneonneonsdotcudablackwellmetalapple9apple10
  • nk_angulars_symmetric_workspace_size_mxfp6e2m3serialsmeskylakeneonneonsdotmetalapple9apple10
  • nk_angulars_symmetric_workspace_size_mxfp6e3m2serialsmeskylakeneonmetalapple9apple10
  • nk_angulars_symmetric_workspace_size_mxfp8e4m3serialsapphireamxsmeskylakeneonneonfhmcudablackwellmetalapple9apple10
  • nk_angulars_symmetric_workspace_size_mxfp8e5m2serialsapphireamxsmeskylakeneonneonfhmcudablackwellmetalapple9apple10
  • nk_angulars_symmetric_workspace_size_e3m2serialsapphireamxsmehaswellskylakev128relaxedneonsdotneonfp8rvvcudaampereblackwellblackwellrtxrocmcdna3cdna4cdna5metalapple9apple10
  • nk_angulars_symmetric_workspace_size_i8serialsapphireamxsmehaswellicelakealdersierrav128relaxedv128neonsdotrvvpowervsxloongsonasxcudaamperehopperblackwellrocmcdna3cdna4cdna5metalapple10
  • nk_angulars_symmetric_workspace_size_u8serialsapphireamxsmehaswellicelakealdersierrav128relaxedv128neonsdotrvvpowervsxloongsonasxcudaamperehopperblackwellrocmcdna3cdna4cdna5metalapple10
  • nk_angulars_symmetric_workspace_size_i4serialsmehaswellicelakev128relaxedneonsdotcudaamperehopperblackwellrocmcdna3cdna4cdna5metalapple10
  • nk_angulars_symmetric_workspace_size_u4serialsmehaswellicelakev128relaxedneonsdotcudaamperehopperblackwellrocmcdna3cdna4cdna5metalapple10

Parameters

vector_countinNumber of vectors (rows) in the input matrix.
depthinCounts dimensions.
rows_begininFirst row of results to compute.
rows_endinOne past the last row of results to compute.
capabilitiesinOne device's capabilities, like nk_cpu_capabilities_enabled reports.
bytesoutThe scratch size, 0 when the capability needs none.

Statuses

nk_euclideans_packed_workspace_size_*_best× 19Returns the workspace bytes of nk_euclideans_packed_f32_best for one shape.
spatials.h:125 ↗
nk_status_t nk_euclideans_packed_workspace_size_f32_best(
    nk_size_t rows,
    nk_size_t columns,
    nk_size_t depth,
    nk_capability_t capabilities,
    nk_size_t *bytes);
SymbolBackends
nk_euclideans_packed_workspace_size_f32_best12
nk_euclideans_packed_workspace_size_f64_best12
nk_euclideans_packed_workspace_size_f16_best22
nk_euclideans_packed_workspace_size_bf16_best24
nk_euclideans_packed_workspace_size_e4m3_best22
nk_euclideans_packed_workspace_size_e5m2_best23
nk_euclideans_packed_workspace_size_e2m3_best24
nk_euclideans_packed_workspace_size_e2m1_best24
nk_euclideans_packed_workspace_size_nvfp4_best11
nk_euclideans_packed_workspace_size_mxfp4_best11
nk_euclideans_packed_workspace_size_mxfp6e2m3_best8
nk_euclideans_packed_workspace_size_mxfp6e3m2_best7
nk_euclideans_packed_workspace_size_mxfp8e4m3_best11
nk_euclideans_packed_workspace_size_mxfp8e5m2_best11
nk_euclideans_packed_workspace_size_e3m2_best20
nk_euclideans_packed_workspace_size_i8_best23
nk_euclideans_packed_workspace_size_u8_best23
nk_euclideans_packed_workspace_size_i4_best16
nk_euclideans_packed_workspace_size_u4_best16

Backends

  • nk_euclideans_packed_workspace_size_f32serialsmef64haswellskylakev128relaxedneonrvvpowervsxloongsonasxcudaampererocm
  • nk_euclideans_packed_workspace_size_f64serialsmef64haswellskylakev128relaxedneonrvvpowervsxloongsonasxcudaampererocm
  • nk_euclideans_packed_workspace_size_f16serialgraniteamxsmehaswellskylakev128relaxedneonneonfhmrvvpowervsxloongsonasxcudaamperehopperblackwellrocmcdna3cdna4cdna5metalapple9apple10
  • nk_euclideans_packed_workspace_size_bf16serialgenoasapphireamxsmehaswellskylakev128relaxedv128neonneonbfdotrvvpowervsxloongsonasxcudaamperehopperblackwellrocmcdna3cdna4cdna5metalapple9apple10
  • nk_euclideans_packed_workspace_size_e4m3serialgenoadiamondsapphireamxsmehaswellskylakev128relaxedneonfhmneonfp8rvvcudaampereblackwellblackwellrtxrocmcdna3cdna4cdna5metalapple9apple10
  • nk_euclideans_packed_workspace_size_e5m2serialgenoadiamondsapphireamxgraniteamxsmehaswellskylakev128relaxedneonfhmneonfp8rvvcudaampereblackwellblackwellrtxrocmcdna3cdna4cdna5metalapple9apple10
  • nk_euclideans_packed_workspace_size_e2m3serialsapphireamxsmehaswellskylakeicelakealdersierrav128relaxedneonsdotneonfp8rvvcudaamperehopperblackwellblackwellrtxrocmcdna3cdna4cdna5metalapple9apple10
  • nk_euclideans_packed_workspace_size_e2m1serialsapphireamxsmehaswellskylakeicelakealdersierrav128relaxedneonsdotneonfp8rvvcudaamperehopperblackwellblackwellrtxrocmcdna3cdna4cdna5metalapple9apple10
  • nk_euclideans_packed_workspace_size_nvfp4serialsapphireamxsmeskylakeneonneonsdotcudablackwellmetalapple9apple10
  • nk_euclideans_packed_workspace_size_mxfp4serialsapphireamxsmeskylakeneonneonsdotcudablackwellmetalapple9apple10
  • nk_euclideans_packed_workspace_size_mxfp6e2m3serialsmeskylakeneonneonsdotmetalapple9apple10
  • nk_euclideans_packed_workspace_size_mxfp6e3m2serialsmeskylakeneonmetalapple9apple10
  • nk_euclideans_packed_workspace_size_mxfp8e4m3serialsapphireamxsmeskylakeneonneonfhmcudablackwellmetalapple9apple10
  • nk_euclideans_packed_workspace_size_mxfp8e5m2serialsapphireamxsmeskylakeneonneonfhmcudablackwellmetalapple9apple10
  • nk_euclideans_packed_workspace_size_e3m2serialsapphireamxsmehaswellskylakev128relaxedneonsdotneonfp8rvvcudaampereblackwellblackwellrtxrocmcdna3cdna4cdna5metalapple9apple10
  • nk_euclideans_packed_workspace_size_i8serialsapphireamxsmehaswellicelakealdersierrav128relaxedv128neonsdotrvvpowervsxloongsonasxcudaamperehopperblackwellrocmcdna3cdna4cdna5metalapple10
  • nk_euclideans_packed_workspace_size_u8serialsapphireamxsmehaswellicelakealdersierrav128relaxedv128neonsdotrvvpowervsxloongsonasxcudaamperehopperblackwellrocmcdna3cdna4cdna5metalapple10
  • nk_euclideans_packed_workspace_size_i4serialsmehaswellicelakev128relaxedneonsdotcudaamperehopperblackwellrocmcdna3cdna4cdna5metalapple10
  • nk_euclideans_packed_workspace_size_u4serialsmehaswellicelakev128relaxedneonsdotcudaamperehopperblackwellrocmcdna3cdna4cdna5metalapple10

Parameters

rowsinNumber of rows in A.
columnsinNumber of columns in B (packed).
depthinShared inner dimension in dimensions.
capabilitiesinOne device's capabilities, like nk_cpu_capabilities_enabled reports.
bytesoutThe scratch size, 0 when the capability needs none.

Statuses

nk_euclideans_symmetric_workspace_size_*_best× 19Returns the workspace bytes of nk_euclideans_symmetric_f32_best for one shape.
spatials.h:140 ↗
nk_status_t nk_euclideans_symmetric_workspace_size_f32_best(
    nk_size_t vector_count,
    nk_size_t depth,
    nk_size_t rows_begin,
    nk_size_t rows_end,
    nk_capability_t capabilities,
    nk_size_t *bytes);
SymbolBackends
nk_euclideans_symmetric_workspace_size_f32_best12
nk_euclideans_symmetric_workspace_size_f64_best12
nk_euclideans_symmetric_workspace_size_f16_best22
nk_euclideans_symmetric_workspace_size_bf16_best24
nk_euclideans_symmetric_workspace_size_e4m3_best22
nk_euclideans_symmetric_workspace_size_e5m2_best23
nk_euclideans_symmetric_workspace_size_e2m3_best24
nk_euclideans_symmetric_workspace_size_e2m1_best24
nk_euclideans_symmetric_workspace_size_nvfp4_best11
nk_euclideans_symmetric_workspace_size_mxfp4_best11
nk_euclideans_symmetric_workspace_size_mxfp6e2m3_best8
nk_euclideans_symmetric_workspace_size_mxfp6e3m2_best7
nk_euclideans_symmetric_workspace_size_mxfp8e4m3_best11
nk_euclideans_symmetric_workspace_size_mxfp8e5m2_best11
nk_euclideans_symmetric_workspace_size_e3m2_best20
nk_euclideans_symmetric_workspace_size_i8_best23
nk_euclideans_symmetric_workspace_size_u8_best23
nk_euclideans_symmetric_workspace_size_i4_best16
nk_euclideans_symmetric_workspace_size_u4_best16

Backends

  • nk_euclideans_symmetric_workspace_size_f32serialsmef64haswellskylakev128relaxedneonrvvpowervsxloongsonasxcudaampererocm
  • nk_euclideans_symmetric_workspace_size_f64serialsmef64haswellskylakev128relaxedneonrvvpowervsxloongsonasxcudaampererocm
  • nk_euclideans_symmetric_workspace_size_f16serialgraniteamxsmehaswellskylakev128relaxedneonneonfhmrvvpowervsxloongsonasxcudaamperehopperblackwellrocmcdna3cdna4cdna5metalapple9apple10
  • nk_euclideans_symmetric_workspace_size_bf16serialgenoasapphireamxsmehaswellskylakev128relaxedv128neonneonbfdotrvvpowervsxloongsonasxcudaamperehopperblackwellrocmcdna3cdna4cdna5metalapple9apple10
  • nk_euclideans_symmetric_workspace_size_e4m3serialgenoadiamondsapphireamxsmehaswellskylakev128relaxedneonfhmneonfp8rvvcudaampereblackwellblackwellrtxrocmcdna3cdna4cdna5metalapple9apple10
  • nk_euclideans_symmetric_workspace_size_e5m2serialgenoadiamondsapphireamxgraniteamxsmehaswellskylakev128relaxedneonfhmneonfp8rvvcudaampereblackwellblackwellrtxrocmcdna3cdna4cdna5metalapple9apple10
  • nk_euclideans_symmetric_workspace_size_e2m3serialsapphireamxsmehaswellskylakeicelakealdersierrav128relaxedneonsdotneonfp8rvvcudaamperehopperblackwellblackwellrtxrocmcdna3cdna4cdna5metalapple9apple10
  • nk_euclideans_symmetric_workspace_size_e2m1serialsapphireamxsmehaswellskylakeicelakealdersierrav128relaxedneonsdotneonfp8rvvcudaamperehopperblackwellblackwellrtxrocmcdna3cdna4cdna5metalapple9apple10
  • nk_euclideans_symmetric_workspace_size_nvfp4serialsapphireamxsmeskylakeneonneonsdotcudablackwellmetalapple9apple10
  • nk_euclideans_symmetric_workspace_size_mxfp4serialsapphireamxsmeskylakeneonneonsdotcudablackwellmetalapple9apple10
  • nk_euclideans_symmetric_workspace_size_mxfp6e2m3serialsmeskylakeneonneonsdotmetalapple9apple10
  • nk_euclideans_symmetric_workspace_size_mxfp6e3m2serialsmeskylakeneonmetalapple9apple10
  • nk_euclideans_symmetric_workspace_size_mxfp8e4m3serialsapphireamxsmeskylakeneonneonfhmcudablackwellmetalapple9apple10
  • nk_euclideans_symmetric_workspace_size_mxfp8e5m2serialsapphireamxsmeskylakeneonneonfhmcudablackwellmetalapple9apple10
  • nk_euclideans_symmetric_workspace_size_e3m2serialsapphireamxsmehaswellskylakev128relaxedneonsdotneonfp8rvvcudaampereblackwellblackwellrtxrocmcdna3cdna4cdna5metalapple9apple10
  • nk_euclideans_symmetric_workspace_size_i8serialsapphireamxsmehaswellicelakealdersierrav128relaxedv128neonsdotrvvpowervsxloongsonasxcudaamperehopperblackwellrocmcdna3cdna4cdna5metalapple10
  • nk_euclideans_symmetric_workspace_size_u8serialsapphireamxsmehaswellicelakealdersierrav128relaxedv128neonsdotrvvpowervsxloongsonasxcudaamperehopperblackwellrocmcdna3cdna4cdna5metalapple10
  • nk_euclideans_symmetric_workspace_size_i4serialsmehaswellicelakev128relaxedneonsdotcudaamperehopperblackwellrocmcdna3cdna4cdna5metalapple10
  • nk_euclideans_symmetric_workspace_size_u4serialsmehaswellicelakev128relaxedneonsdotcudaamperehopperblackwellrocmcdna3cdna4cdna5metalapple10

Parameters

vector_countinNumber of vectors (rows) in the input matrix.
depthinCounts dimensions.
rows_begininFirst row of results to compute.
rows_endinOne past the last row of results to compute.
capabilitiesinOne device's capabilities, like nk_cpu_capabilities_enabled reports.
bytesoutThe scratch size, 0 when the capability needs none.

Statuses

nk_angulars_packed_*_best× 19Computes batched angular distances using a packed second matrix.
spatials.h:165 ↗
nk_status_t nk_angulars_packed_f32_best(
    nk_f32_t const *a,
    void const *b_packed,
    nk_f64_t *result,
    nk_size_t rows,
    nk_size_t columns,
    nk_size_t depth,
    nk_size_t a_stride,
    nk_size_t r_stride,
    void *workspace,
    nk_size_t workspace_bytes,
    nk_capability_t capabilities,
    nk_stream_t stream);
SymbolaresultBackends
nk_angulars_packed_f32_bestnk_f32_tnk_f64_t12
nk_angulars_packed_f64_bestnk_f64_tnk_f64_t12
nk_angulars_packed_f16_bestnk_f16_tnk_f32_t22
nk_angulars_packed_bf16_bestnk_bf16_tnk_f32_t24
nk_angulars_packed_e4m3_bestnk_e4m3_tnk_f32_t22
nk_angulars_packed_e5m2_bestnk_e5m2_tnk_f32_t23
nk_angulars_packed_e2m3_bestnk_e2m3_tnk_f32_t24
nk_angulars_packed_e2m1_bestnk_e2m1x2_tnk_f32_t24
nk_angulars_packed_nvfp4_bestnk_nvfp4_cref_tnk_f32_t11
nk_angulars_packed_mxfp4_bestnk_mxfp4_cref_tnk_f32_t11
nk_angulars_packed_mxfp6e2m3_bestnk_mxfp6e2m3_cref_tnk_f32_t8
nk_angulars_packed_mxfp6e3m2_bestnk_mxfp6e3m2_cref_tnk_f32_t7
nk_angulars_packed_mxfp8e4m3_bestnk_mxfp8e4m3_cref_tnk_f32_t11
nk_angulars_packed_mxfp8e5m2_bestnk_mxfp8e5m2_cref_tnk_f32_t11
nk_angulars_packed_e3m2_bestnk_e3m2_tnk_f32_t20
nk_angulars_packed_i8_bestnk_i8_tnk_f32_t23
nk_angulars_packed_u8_bestnk_u8_tnk_f32_t23
nk_angulars_packed_i4_bestnk_i4x2_tnk_f32_t16
nk_angulars_packed_u4_bestnk_u4x2_tnk_f32_t16

Backends

  • nk_angulars_packed_f32serialsmef64haswellskylakev128relaxedneonrvvpowervsxloongsonasxcudaampererocm
  • nk_angulars_packed_f64serialsmef64haswellskylakev128relaxedneonrvvpowervsxloongsonasxcudaampererocm
  • nk_angulars_packed_f16serialgraniteamxsmehaswellskylakev128relaxedneonneonfhmrvvpowervsxloongsonasxcudaamperehopperblackwellrocmcdna3cdna4cdna5metalapple9apple10
  • nk_angulars_packed_bf16serialgenoasapphireamxsmehaswellskylakev128relaxedv128neonneonbfdotrvvpowervsxloongsonasxcudaamperehopperblackwellrocmcdna3cdna4cdna5metalapple9apple10
  • nk_angulars_packed_e4m3serialgenoadiamondsapphireamxsmehaswellskylakev128relaxedneonfhmneonfp8rvvcudaampereblackwellblackwellrtxrocmcdna3cdna4cdna5metalapple9apple10
  • nk_angulars_packed_e5m2serialgenoadiamondsapphireamxgraniteamxsmehaswellskylakev128relaxedneonfhmneonfp8rvvcudaampereblackwellblackwellrtxrocmcdna3cdna4cdna5metalapple9apple10
  • nk_angulars_packed_e2m3serialsapphireamxsmehaswellskylakeicelakealdersierrav128relaxedneonsdotneonfp8rvvcudaamperehopperblackwellblackwellrtxrocmcdna3cdna4cdna5metalapple9apple10
  • nk_angulars_packed_e2m1serialsapphireamxsmehaswellskylakeicelakealdersierrav128relaxedneonsdotneonfp8rvvcudaamperehopperblackwellblackwellrtxrocmcdna3cdna4cdna5metalapple9apple10
  • nk_angulars_packed_nvfp4serialsapphireamxsmeskylakeneonneonsdotcudablackwellmetalapple9apple10
  • nk_angulars_packed_mxfp4serialsapphireamxsmeskylakeneonneonsdotcudablackwellmetalapple9apple10
  • nk_angulars_packed_mxfp6e2m3serialsmeskylakeneonneonsdotmetalapple9apple10
  • nk_angulars_packed_mxfp6e3m2serialsmeskylakeneonmetalapple9apple10
  • nk_angulars_packed_mxfp8e4m3serialsapphireamxsmeskylakeneonneonfhmcudablackwellmetalapple9apple10
  • nk_angulars_packed_mxfp8e5m2serialsapphireamxsmeskylakeneonneonfhmcudablackwellmetalapple9apple10
  • nk_angulars_packed_e3m2serialsapphireamxsmehaswellskylakev128relaxedneonsdotneonfp8rvvcudaampereblackwellblackwellrtxrocmcdna3cdna4cdna5metalapple9apple10
  • nk_angulars_packed_i8serialsapphireamxsmehaswellicelakealdersierrav128relaxedv128neonsdotrvvpowervsxloongsonasxcudaamperehopperblackwellrocmcdna3cdna4cdna5metalapple10
  • nk_angulars_packed_u8serialsapphireamxsmehaswellicelakealdersierrav128relaxedv128neonsdotrvvpowervsxloongsonasxcudaamperehopperblackwellrocmcdna3cdna4cdna5metalapple10
  • nk_angulars_packed_i4serialsmehaswellicelakev128relaxedneonsdotcudaamperehopperblackwellrocmcdna3cdna4cdna5metalapple10
  • nk_angulars_packed_u4serialsmehaswellicelakev128relaxedneonsdotcudaamperehopperblackwellrocmcdna3cdna4cdna5metalapple10

Parameters

ainInput A matrix in row-major order.
b_packedinPacked B with norms from nk_dots_pack_*_best on the same capabilities.
resultoutOutput matrix (rows x columns) of angular distances.
rowsinNumber of rows in A.
columnsinNumber of columns in B (packed).
depthinShared inner dimension in dimensions, a multiple of the values per byte.
a_strideinRow stride in bytes for A.
r_strideinRow stride in bytes for the result matrix.
workspaceinScratch memory aligned to nk_default_alignment_k, in device memory ordered by stream on GPU capabilities. May be null with workspace_bytes of 0.
workspace_bytesinAt least what the matching workspace-size query reports for the fast path, or 0 for the scratch-free path. Anything in between is nk_unexpected_dimensions_k.
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_angulars_symmetric_*_best× 19Computes symmetric angular distance matrix (Gram-style) for a set of vectors.
spatials.h:192 ↗
nk_status_t nk_angulars_symmetric_f32_best(
    nk_f32_t const *vectors,
    nk_size_t vector_count,
    nk_size_t depth,
    nk_size_t stride,
    nk_f64_t *result,
    nk_size_t result_stride,
    nk_size_t rows_begin,
    nk_size_t rows_end,
    void *workspace,
    nk_size_t workspace_bytes,
    nk_capability_t capabilities,
    nk_stream_t stream);
SymbolvectorsresultBackends
nk_angulars_symmetric_f32_bestnk_f32_tnk_f64_t12
nk_angulars_symmetric_f64_bestnk_f64_tnk_f64_t12
nk_angulars_symmetric_f16_bestnk_f16_tnk_f32_t22
nk_angulars_symmetric_bf16_bestnk_bf16_tnk_f32_t24
nk_angulars_symmetric_e4m3_bestnk_e4m3_tnk_f32_t22
nk_angulars_symmetric_e5m2_bestnk_e5m2_tnk_f32_t23
nk_angulars_symmetric_e2m3_bestnk_e2m3_tnk_f32_t24
nk_angulars_symmetric_e2m1_bestnk_e2m1x2_tnk_f32_t24
nk_angulars_symmetric_nvfp4_bestnk_nvfp4_cref_tnk_f32_t11
nk_angulars_symmetric_mxfp4_bestnk_mxfp4_cref_tnk_f32_t11
nk_angulars_symmetric_mxfp6e2m3_bestnk_mxfp6e2m3_cref_tnk_f32_t8
nk_angulars_symmetric_mxfp6e3m2_bestnk_mxfp6e3m2_cref_tnk_f32_t7
nk_angulars_symmetric_mxfp8e4m3_bestnk_mxfp8e4m3_cref_tnk_f32_t11
nk_angulars_symmetric_mxfp8e5m2_bestnk_mxfp8e5m2_cref_tnk_f32_t11
nk_angulars_symmetric_e3m2_bestnk_e3m2_tnk_f32_t20
nk_angulars_symmetric_i8_bestnk_i8_tnk_f32_t23
nk_angulars_symmetric_u8_bestnk_u8_tnk_f32_t23
nk_angulars_symmetric_i4_bestnk_i4x2_tnk_f32_t16
nk_angulars_symmetric_u4_bestnk_u4x2_tnk_f32_t16

Backends

  • nk_angulars_symmetric_f32serialsmef64haswellskylakev128relaxedneonrvvpowervsxloongsonasxcudaampererocm
  • nk_angulars_symmetric_f64serialsmef64haswellskylakev128relaxedneonrvvpowervsxloongsonasxcudaampererocm
  • nk_angulars_symmetric_f16serialgraniteamxsmehaswellskylakev128relaxedneonneonfhmrvvpowervsxloongsonasxcudaamperehopperblackwellrocmcdna3cdna4cdna5metalapple9apple10
  • nk_angulars_symmetric_bf16serialgenoasapphireamxsmehaswellskylakev128relaxedv128neonneonbfdotrvvpowervsxloongsonasxcudaamperehopperblackwellrocmcdna3cdna4cdna5metalapple9apple10
  • nk_angulars_symmetric_e4m3serialgenoadiamondsapphireamxsmehaswellskylakev128relaxedneonfhmneonfp8rvvcudaampereblackwellblackwellrtxrocmcdna3cdna4cdna5metalapple9apple10
  • nk_angulars_symmetric_e5m2serialgenoadiamondsapphireamxgraniteamxsmehaswellskylakev128relaxedneonfhmneonfp8rvvcudaampereblackwellblackwellrtxrocmcdna3cdna4cdna5metalapple9apple10
  • nk_angulars_symmetric_e2m3serialsapphireamxsmehaswellskylakeicelakealdersierrav128relaxedneonsdotneonfp8rvvcudaamperehopperblackwellblackwellrtxrocmcdna3cdna4cdna5metalapple9apple10
  • nk_angulars_symmetric_e2m1serialsapphireamxsmehaswellskylakeicelakealdersierrav128relaxedneonsdotneonfp8rvvcudaamperehopperblackwellblackwellrtxrocmcdna3cdna4cdna5metalapple9apple10
  • nk_angulars_symmetric_nvfp4serialsapphireamxsmeskylakeneonneonsdotcudablackwellmetalapple9apple10
  • nk_angulars_symmetric_mxfp4serialsapphireamxsmeskylakeneonneonsdotcudablackwellmetalapple9apple10
  • nk_angulars_symmetric_mxfp6e2m3serialsmeskylakeneonneonsdotmetalapple9apple10
  • nk_angulars_symmetric_mxfp6e3m2serialsmeskylakeneonmetalapple9apple10
  • nk_angulars_symmetric_mxfp8e4m3serialsapphireamxsmeskylakeneonneonfhmcudablackwellmetalapple9apple10
  • nk_angulars_symmetric_mxfp8e5m2serialsapphireamxsmeskylakeneonneonfhmcudablackwellmetalapple9apple10
  • nk_angulars_symmetric_e3m2serialsapphireamxsmehaswellskylakev128relaxedneonsdotneonfp8rvvcudaampereblackwellblackwellrtxrocmcdna3cdna4cdna5metalapple9apple10
  • nk_angulars_symmetric_i8serialsapphireamxsmehaswellicelakealdersierrav128relaxedv128neonsdotrvvpowervsxloongsonasxcudaamperehopperblackwellrocmcdna3cdna4cdna5metalapple10
  • nk_angulars_symmetric_u8serialsapphireamxsmehaswellicelakealdersierrav128relaxedv128neonsdotrvvpowervsxloongsonasxcudaamperehopperblackwellrocmcdna3cdna4cdna5metalapple10
  • nk_angulars_symmetric_i4serialsmehaswellicelakev128relaxedneonsdotcudaamperehopperblackwellrocmcdna3cdna4cdna5metalapple10
  • nk_angulars_symmetric_u4serialsmehaswellicelakev128relaxedneonsdotcudaamperehopperblackwellrocmcdna3cdna4cdna5metalapple10

Parameters

vectorsinInput matrix of row vectors in row-major order.
vector_countinNumber of vectors (rows) in the input matrix.
depthinCounts dimensions, a multiple of the values per byte.
strideinRow stride in bytes for the input matrix.
resultoutOutput symmetric matrix of vector_count × vector_count.
result_strideinRow stride in bytes for the result matrix.
rows_begininFirst row of results to compute (for parallelism).
rows_endinOne past the last row of results to compute, clamped to vector_count.
workspaceinScratch memory aligned to nk_default_alignment_k, in device memory ordered by stream on GPU capabilities. May be null with workspace_bytes of 0.
workspace_bytesinAt least what the matching workspace-size query reports for the fast path, or 0 for the scratch-free path. Anything in between is nk_unexpected_dimensions_k.
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_euclideans_packed_*_best× 19Computes batched euclidean distances using a packed second matrix.
spatials.h:219 ↗
nk_status_t nk_euclideans_packed_f32_best(
    nk_f32_t const *a,
    void const *b_packed,
    nk_f64_t *result,
    nk_size_t rows,
    nk_size_t columns,
    nk_size_t depth,
    nk_size_t a_stride,
    nk_size_t r_stride,
    void *workspace,
    nk_size_t workspace_bytes,
    nk_capability_t capabilities,
    nk_stream_t stream);
SymbolaresultBackends
nk_euclideans_packed_f32_bestnk_f32_tnk_f64_t12
nk_euclideans_packed_f64_bestnk_f64_tnk_f64_t12
nk_euclideans_packed_f16_bestnk_f16_tnk_f32_t22
nk_euclideans_packed_bf16_bestnk_bf16_tnk_f32_t24
nk_euclideans_packed_e4m3_bestnk_e4m3_tnk_f32_t22
nk_euclideans_packed_e5m2_bestnk_e5m2_tnk_f32_t23
nk_euclideans_packed_e2m3_bestnk_e2m3_tnk_f32_t24
nk_euclideans_packed_e2m1_bestnk_e2m1x2_tnk_f32_t24
nk_euclideans_packed_nvfp4_bestnk_nvfp4_cref_tnk_f32_t11
nk_euclideans_packed_mxfp4_bestnk_mxfp4_cref_tnk_f32_t11
nk_euclideans_packed_mxfp6e2m3_bestnk_mxfp6e2m3_cref_tnk_f32_t8
nk_euclideans_packed_mxfp6e3m2_bestnk_mxfp6e3m2_cref_tnk_f32_t7
nk_euclideans_packed_mxfp8e4m3_bestnk_mxfp8e4m3_cref_tnk_f32_t11
nk_euclideans_packed_mxfp8e5m2_bestnk_mxfp8e5m2_cref_tnk_f32_t11
nk_euclideans_packed_e3m2_bestnk_e3m2_tnk_f32_t20
nk_euclideans_packed_i8_bestnk_i8_tnk_f32_t23
nk_euclideans_packed_u8_bestnk_u8_tnk_f32_t23
nk_euclideans_packed_i4_bestnk_i4x2_tnk_f32_t16
nk_euclideans_packed_u4_bestnk_u4x2_tnk_f32_t16

Backends

  • nk_euclideans_packed_f32serialsmef64haswellskylakev128relaxedneonrvvpowervsxloongsonasxcudaampererocm
  • nk_euclideans_packed_f64serialsmef64haswellskylakev128relaxedneonrvvpowervsxloongsonasxcudaampererocm
  • nk_euclideans_packed_f16serialgraniteamxsmehaswellskylakev128relaxedneonneonfhmrvvpowervsxloongsonasxcudaamperehopperblackwellrocmcdna3cdna4cdna5metalapple9apple10
  • nk_euclideans_packed_bf16serialgenoasapphireamxsmehaswellskylakev128relaxedv128neonneonbfdotrvvpowervsxloongsonasxcudaamperehopperblackwellrocmcdna3cdna4cdna5metalapple9apple10
  • nk_euclideans_packed_e4m3serialgenoadiamondsapphireamxsmehaswellskylakev128relaxedneonfhmneonfp8rvvcudaampereblackwellblackwellrtxrocmcdna3cdna4cdna5metalapple9apple10
  • nk_euclideans_packed_e5m2serialgenoadiamondsapphireamxgraniteamxsmehaswellskylakev128relaxedneonfhmneonfp8rvvcudaampereblackwellblackwellrtxrocmcdna3cdna4cdna5metalapple9apple10
  • nk_euclideans_packed_e2m3serialsapphireamxsmehaswellskylakeicelakealdersierrav128relaxedneonsdotneonfp8rvvcudaamperehopperblackwellblackwellrtxrocmcdna3cdna4cdna5metalapple9apple10
  • nk_euclideans_packed_e2m1serialsapphireamxsmehaswellskylakeicelakealdersierrav128relaxedneonsdotneonfp8rvvcudaamperehopperblackwellblackwellrtxrocmcdna3cdna4cdna5metalapple9apple10
  • nk_euclideans_packed_nvfp4serialsapphireamxsmeskylakeneonneonsdotcudablackwellmetalapple9apple10
  • nk_euclideans_packed_mxfp4serialsapphireamxsmeskylakeneonneonsdotcudablackwellmetalapple9apple10
  • nk_euclideans_packed_mxfp6e2m3serialsmeskylakeneonneonsdotmetalapple9apple10
  • nk_euclideans_packed_mxfp6e3m2serialsmeskylakeneonmetalapple9apple10
  • nk_euclideans_packed_mxfp8e4m3serialsapphireamxsmeskylakeneonneonfhmcudablackwellmetalapple9apple10
  • nk_euclideans_packed_mxfp8e5m2serialsapphireamxsmeskylakeneonneonfhmcudablackwellmetalapple9apple10
  • nk_euclideans_packed_e3m2serialsapphireamxsmehaswellskylakev128relaxedneonsdotneonfp8rvvcudaampereblackwellblackwellrtxrocmcdna3cdna4cdna5metalapple9apple10
  • nk_euclideans_packed_i8serialsapphireamxsmehaswellicelakealdersierrav128relaxedv128neonsdotrvvpowervsxloongsonasxcudaamperehopperblackwellrocmcdna3cdna4cdna5metalapple10
  • nk_euclideans_packed_u8serialsapphireamxsmehaswellicelakealdersierrav128relaxedv128neonsdotrvvpowervsxloongsonasxcudaamperehopperblackwellrocmcdna3cdna4cdna5metalapple10
  • nk_euclideans_packed_i4serialsmehaswellicelakev128relaxedneonsdotcudaamperehopperblackwellrocmcdna3cdna4cdna5metalapple10
  • nk_euclideans_packed_u4serialsmehaswellicelakev128relaxedneonsdotcudaamperehopperblackwellrocmcdna3cdna4cdna5metalapple10

Parameters

ainInput A matrix in row-major order.
b_packedinPacked B with norms from nk_dots_pack_*_best on the same capabilities.
resultoutOutput matrix (rows x columns) of euclidean distances.
rowsinNumber of rows in A.
columnsinNumber of columns in B (packed).
depthinShared inner dimension in dimensions, a multiple of the values per byte.
a_strideinRow stride in bytes for A.
r_strideinRow stride in bytes for the result matrix.
workspaceinScratch memory aligned to nk_default_alignment_k, in device memory ordered by stream on GPU capabilities. May be null with workspace_bytes of 0.
workspace_bytesinAt least what the matching workspace-size query reports for the fast path, or 0 for the scratch-free path. Anything in between is nk_unexpected_dimensions_k.
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_euclideans_symmetric_*_best× 19Computes symmetric euclidean distance matrix (Gram-style) for a set of vectors.
spatials.h:246 ↗
nk_status_t nk_euclideans_symmetric_f32_best(
    nk_f32_t const *vectors,
    nk_size_t vector_count,
    nk_size_t depth,
    nk_size_t stride,
    nk_f64_t *result,
    nk_size_t result_stride,
    nk_size_t rows_begin,
    nk_size_t rows_end,
    void *workspace,
    nk_size_t workspace_bytes,
    nk_capability_t capabilities,
    nk_stream_t stream);
SymbolvectorsresultBackends
nk_euclideans_symmetric_f32_bestnk_f32_tnk_f64_t12
nk_euclideans_symmetric_f64_bestnk_f64_tnk_f64_t12
nk_euclideans_symmetric_f16_bestnk_f16_tnk_f32_t22
nk_euclideans_symmetric_bf16_bestnk_bf16_tnk_f32_t24
nk_euclideans_symmetric_e4m3_bestnk_e4m3_tnk_f32_t22
nk_euclideans_symmetric_e5m2_bestnk_e5m2_tnk_f32_t23
nk_euclideans_symmetric_e2m3_bestnk_e2m3_tnk_f32_t24
nk_euclideans_symmetric_e2m1_bestnk_e2m1x2_tnk_f32_t24
nk_euclideans_symmetric_nvfp4_bestnk_nvfp4_cref_tnk_f32_t11
nk_euclideans_symmetric_mxfp4_bestnk_mxfp4_cref_tnk_f32_t11
nk_euclideans_symmetric_mxfp6e2m3_bestnk_mxfp6e2m3_cref_tnk_f32_t8
nk_euclideans_symmetric_mxfp6e3m2_bestnk_mxfp6e3m2_cref_tnk_f32_t7
nk_euclideans_symmetric_mxfp8e4m3_bestnk_mxfp8e4m3_cref_tnk_f32_t11
nk_euclideans_symmetric_mxfp8e5m2_bestnk_mxfp8e5m2_cref_tnk_f32_t11
nk_euclideans_symmetric_e3m2_bestnk_e3m2_tnk_f32_t20
nk_euclideans_symmetric_i8_bestnk_i8_tnk_f32_t23
nk_euclideans_symmetric_u8_bestnk_u8_tnk_f32_t23
nk_euclideans_symmetric_i4_bestnk_i4x2_tnk_f32_t16
nk_euclideans_symmetric_u4_bestnk_u4x2_tnk_f32_t16

Backends

  • nk_euclideans_symmetric_f32serialsmef64haswellskylakev128relaxedneonrvvpowervsxloongsonasxcudaampererocm
  • nk_euclideans_symmetric_f64serialsmef64haswellskylakev128relaxedneonrvvpowervsxloongsonasxcudaampererocm
  • nk_euclideans_symmetric_f16serialgraniteamxsmehaswellskylakev128relaxedneonneonfhmrvvpowervsxloongsonasxcudaamperehopperblackwellrocmcdna3cdna4cdna5metalapple9apple10
  • nk_euclideans_symmetric_bf16serialgenoasapphireamxsmehaswellskylakev128relaxedv128neonneonbfdotrvvpowervsxloongsonasxcudaamperehopperblackwellrocmcdna3cdna4cdna5metalapple9apple10
  • nk_euclideans_symmetric_e4m3serialgenoadiamondsapphireamxsmehaswellskylakev128relaxedneonfhmneonfp8rvvcudaampereblackwellblackwellrtxrocmcdna3cdna4cdna5metalapple9apple10
  • nk_euclideans_symmetric_e5m2serialgenoadiamondsapphireamxgraniteamxsmehaswellskylakev128relaxedneonfhmneonfp8rvvcudaampereblackwellblackwellrtxrocmcdna3cdna4cdna5metalapple9apple10
  • nk_euclideans_symmetric_e2m3serialsapphireamxsmehaswellskylakeicelakealdersierrav128relaxedneonsdotneonfp8rvvcudaamperehopperblackwellblackwellrtxrocmcdna3cdna4cdna5metalapple9apple10
  • nk_euclideans_symmetric_e2m1serialsapphireamxsmehaswellskylakeicelakealdersierrav128relaxedneonsdotneonfp8rvvcudaamperehopperblackwellblackwellrtxrocmcdna3cdna4cdna5metalapple9apple10
  • nk_euclideans_symmetric_nvfp4serialsapphireamxsmeskylakeneonneonsdotcudablackwellmetalapple9apple10
  • nk_euclideans_symmetric_mxfp4serialsapphireamxsmeskylakeneonneonsdotcudablackwellmetalapple9apple10
  • nk_euclideans_symmetric_mxfp6e2m3serialsmeskylakeneonneonsdotmetalapple9apple10
  • nk_euclideans_symmetric_mxfp6e3m2serialsmeskylakeneonmetalapple9apple10
  • nk_euclideans_symmetric_mxfp8e4m3serialsapphireamxsmeskylakeneonneonfhmcudablackwellmetalapple9apple10
  • nk_euclideans_symmetric_mxfp8e5m2serialsapphireamxsmeskylakeneonneonfhmcudablackwellmetalapple9apple10
  • nk_euclideans_symmetric_e3m2serialsapphireamxsmehaswellskylakev128relaxedneonsdotneonfp8rvvcudaampereblackwellblackwellrtxrocmcdna3cdna4cdna5metalapple9apple10
  • nk_euclideans_symmetric_i8serialsapphireamxsmehaswellicelakealdersierrav128relaxedv128neonsdotrvvpowervsxloongsonasxcudaamperehopperblackwellrocmcdna3cdna4cdna5metalapple10
  • nk_euclideans_symmetric_u8serialsapphireamxsmehaswellicelakealdersierrav128relaxedv128neonsdotrvvpowervsxloongsonasxcudaamperehopperblackwellrocmcdna3cdna4cdna5metalapple10
  • nk_euclideans_symmetric_i4serialsmehaswellicelakev128relaxedneonsdotcudaamperehopperblackwellrocmcdna3cdna4cdna5metalapple10
  • nk_euclideans_symmetric_u4serialsmehaswellicelakev128relaxedneonsdotcudaamperehopperblackwellrocmcdna3cdna4cdna5metalapple10

Parameters

vectorsinInput matrix of row vectors in row-major order.
vector_countinNumber of vectors (rows) in the input matrix.
depthinCounts dimensions, a multiple of the values per byte.
strideinRow stride in bytes for the input matrix.
resultoutOutput symmetric matrix of vector_count × vector_count.
result_strideinRow stride in bytes for the result matrix.
rows_begininFirst row of results to compute (for parallelism).
rows_endinOne past the last row of results to compute, clamped to vector_count.
workspaceinScratch memory aligned to nk_default_alignment_k, in device memory ordered by stream on GPU capabilities. May be null with workspace_bytes of 0.
workspace_bytesinAt least what the matching workspace-size query reports for the fast path, or 0 for the scratch-free path. Anything in between is nk_unexpected_dimensions_k.
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_attention_pack_size_*_best× 10Returns bytes enough to pack a ragged batch of segments however its tokens split.
attention.h:162 ↗
nk_status_t nk_attention_pack_size_bf16_best(
    nk_size_t key_value_head_count,
    nk_size_t depth,
    nk_size_t token_count,
    nk_size_t segment_count,
    nk_capability_t capabilities,
    nk_size_t *bytes);
SymbolBackends
nk_attention_pack_size_bf16_best22
nk_attention_pack_size_f16_best11
nk_attention_pack_size_e4m3_best23
nk_attention_pack_size_i8_best20
nk_attention_pack_size_nvfp4_best7
nk_attention_pack_size_mxfp4_best7
nk_attention_pack_size_mxfp6e2m3_best7
nk_attention_pack_size_mxfp6e3m2_best7
nk_attention_pack_size_mxfp8e4m3_best7
nk_attention_pack_size_mxfp8e5m2_best7

Backends

  • nk_attention_pack_size_bf16serialhaswellskylakegenoasapphireamxsmeneonneonbfdotrvvv128relaxedcudaamperehopperblackwellblackwellultrarocmcdna3cdna4cdna5metalapple9apple10
  • nk_attention_pack_size_f16serialsmeneonneonfhmcudaampereblackwellblackwellultrametalapple9apple10
  • nk_attention_pack_size_e4m3serialhaswellskylakegenoasapphireamxdiamondamxsmeneonneonfhmrvvv128relaxedcudaamperehopperblackwellblackwellrtxblackwellultrarocmcdna4cdna5metalapple9apple10
  • nk_attention_pack_size_i8serialhaswellicelakesapphireamxsmeneonneonsdotrvvv128relaxedcudaamperehopperblackwellblackwellultrarocmcdna3cdna4cdna5metalapple10
  • nk_attention_pack_size_nvfp4serialsmeneonneonfhmmetalapple9apple10
  • nk_attention_pack_size_mxfp4serialsmeneonneonbfdotmetalapple9apple10
  • nk_attention_pack_size_mxfp6e2m3serialsmeneonneonbfdotmetalapple9apple10
  • nk_attention_pack_size_mxfp6e3m2serialsmeneonneonbfdotmetalapple9apple10
  • nk_attention_pack_size_mxfp8e4m3serialsmeneonneonbfdotmetalapple9apple10
  • nk_attention_pack_size_mxfp8e5m2serialsmeneonneonbfdotmetalapple9apple10

Parameters

key_value_head_countinNumber of K/V heads, a nonzero divisor of the query head count; attention must take it and depth as packed, and only debug builds assert these rules.
depthinHead dimension; any value ≥ 1.
token_countinTokens across all segments, the sum of the pack's key counts.
segment_countinNumber of segments packed together.
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_attention_packed_shape_*_best× 10Reads a packed KV cache's shape from its header.
attention.h:216 ↗
nk_status_t nk_attention_packed_shape_bf16_best(
    void const *key_value_packed,
    nk_size_t *key_value_head_count,
    nk_size_t *depth,
    nk_size_t *segments,
    nk_capability_t capabilities,
    nk_stream_t stream);
SymbolBackends
nk_attention_packed_shape_bf16_best22
nk_attention_packed_shape_f16_best11
nk_attention_packed_shape_e4m3_best23
nk_attention_packed_shape_i8_best20
nk_attention_packed_shape_nvfp4_best7
nk_attention_packed_shape_mxfp4_best7
nk_attention_packed_shape_mxfp6e2m3_best7
nk_attention_packed_shape_mxfp6e3m2_best7
nk_attention_packed_shape_mxfp8e4m3_best7
nk_attention_packed_shape_mxfp8e5m2_best7

Backends

  • nk_attention_packed_shape_bf16serialhaswellskylakegenoasapphireamxsmeneonneonbfdotrvvv128relaxedcudaamperehopperblackwellblackwellultrarocmcdna3cdna4cdna5metalapple9apple10
  • nk_attention_packed_shape_f16serialsmeneonneonfhmcudaampereblackwellblackwellultrametalapple9apple10
  • nk_attention_packed_shape_e4m3serialhaswellskylakegenoasapphireamxdiamondamxsmeneonneonfhmrvvv128relaxedcudaamperehopperblackwellblackwellrtxblackwellultrarocmcdna4cdna5metalapple9apple10
  • nk_attention_packed_shape_i8serialhaswellicelakesapphireamxsmeneonneonsdotrvvv128relaxedcudaamperehopperblackwellblackwellultrarocmcdna3cdna4cdna5metalapple10
  • nk_attention_packed_shape_nvfp4serialsmeneonneonfhmmetalapple9apple10
  • nk_attention_packed_shape_mxfp4serialsmeneonneonbfdotmetalapple9apple10
  • nk_attention_packed_shape_mxfp6e2m3serialsmeneonneonbfdotmetalapple9apple10
  • nk_attention_packed_shape_mxfp6e3m2serialsmeneonneonbfdotmetalapple9apple10
  • nk_attention_packed_shape_mxfp8e4m3serialsmeneonneonbfdotmetalapple9apple10
  • nk_attention_packed_shape_mxfp8e5m2serialsmeneonneonbfdotmetalapple9apple10

Parameters

key_value_packedinA buffer produced by the matching nk_attention_pack_bf16_best.
key_value_head_countoutReceives the K/V head count.
depthoutReceives the head dimension.
segmentsoutReceives the segment count.
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_attention_packed_segmentsLocates a pack's key slot boundaries and key counts.
attention.h:274 ↗
nk_status_t nk_attention_packed_segments(
    void const *key_value_packed,
    nk_size_t segment_count,
    nk_u32_t const **key_offsets,
    nk_u32_t const **key_lengths);

Parameters

key_value_packedinA buffer produced by the matching nk_attention_pack_bf16_best.
segment_countinThe pack's segment count, from nk_attention_packed_shape_bf16_best.
key_offsetsoutPoints at the pack's segment_count + 1 slot boundaries.
key_lengthsoutPoints at the pack's segment_count key counts.

Notes

Pure address arithmetic that never dereferences the pack, so it works on device packs too.

nk_attention_pack_*_best× 4Packs a ragged batch of K and V segments into a backend-opaque layout.
attention.h:308 ↗
nk_status_t nk_attention_pack_bf16_best(
    nk_size_t key_value_head_count,
    nk_size_t depth,
    nk_u32_t const *key_offsets,
    nk_u32_t const *key_lengths,
    nk_size_t segment_count,
    nk_bf16_t const *keys,
    nk_size_t key_stride,
    nk_bf16_t const *values,
    nk_size_t value_stride,
    void *key_value_packed,
    nk_size_t tasks_begin,
    nk_size_t tasks_end,
    nk_capability_t capabilities,
    nk_stream_t stream);
Symbolkeys, valuesBackends
nk_attention_pack_bf16_bestnk_bf16_t22
nk_attention_pack_f16_bestnk_f16_t11
nk_attention_pack_e4m3_bestnk_e4m3_t23
nk_attention_pack_i8_bestnk_i8_t20

Backends

  • nk_attention_pack_bf16serialhaswellskylakegenoasapphireamxsmeneonneonbfdotrvvv128relaxedcudaamperehopperblackwellblackwellultrarocmcdna3cdna4cdna5metalapple9apple10
  • nk_attention_pack_f16serialsmeneonneonfhmcudaampereblackwellblackwellultrametalapple9apple10
  • nk_attention_pack_e4m3serialhaswellskylakegenoasapphireamxdiamondamxsmeneonneonfhmrvvv128relaxedcudaamperehopperblackwellblackwellrtxblackwellultrarocmcdna4cdna5metalapple9apple10
  • nk_attention_pack_i8serialhaswellicelakesapphireamxsmeneonneonsdotrvvv128relaxedcudaamperehopperblackwellblackwellultrarocmcdna3cdna4cdna5metalapple10

GPU packs read the arrays on the device, so only their counts are checked. Windows let callers pack in parallel and in any order: tasks write disjoint ranges, the window starting at task 0 also writes the header and directory, and no window reads them.

Parameters

key_value_head_countinK/V heads, at most the query head count.
depthinHead dimension.
key_offsetsinThe segment_count + 1 slot boundaries: segment s keeps its keys in rows key_offsets[s] onward, and may leave spare capacity before key_offsets[s + 1]. Must not decrease.
key_lengthsinKeys each segment holds, at most its slot's width; zeros mark padding slots. Null means every slot is full: the difference of consecutive key_offsets.
segment_countinNumber of segments in the batch.
keysinToken-major matrix, one row of key_value_head_count × depth contiguous elements per token, with strided rows. Packing fuses no transposition, so depth-major K is transposed in a separate pass first.
key_strideinRow (token) stride of keys in bytes.
valuesinToken-major matrix shaped like keys. Depth-major V is likewise transposed in a separate pass first.
value_strideinRow (token) stride of values in bytes.
key_value_packedout64-byte-aligned buffer of nk_attention_pack_size_* bytes.
tasks_begininFirst task of a window over the segments × K/V heads grid.
tasks_endinEnd of that half-open window, clipped to the grid.
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_attention_pack_*_best× 6Packs block-scaled K and V as nk_attention_pack_bf16_best does, from references to their codes, block scales and NVFP4 tensor scale; depth is a whole number of blocks, and the strides count bytes of codes.
attention.h:341 ↗
nk_status_t nk_attention_pack_nvfp4_best(
    nk_size_t key_value_head_count,
    nk_size_t depth,
    nk_u32_t const *key_offsets,
    nk_u32_t const *key_lengths,
    nk_size_t segment_count,
    nk_nvfp4_cref_t const *keys,
    nk_size_t key_stride,
    nk_nvfp4_cref_t const *values,
    nk_size_t value_stride,
    void *key_value_packed,
    nk_size_t tasks_begin,
    nk_size_t tasks_end,
    nk_capability_t capabilities,
    nk_stream_t stream);
Symbolkeys, valuesBackends
nk_attention_pack_nvfp4_bestnk_nvfp4_cref_t7
nk_attention_pack_mxfp4_bestnk_mxfp4_cref_t7
nk_attention_pack_mxfp6e2m3_bestnk_mxfp6e2m3_cref_t7
nk_attention_pack_mxfp6e3m2_bestnk_mxfp6e3m2_cref_t7
nk_attention_pack_mxfp8e4m3_bestnk_mxfp8e4m3_cref_t7
nk_attention_pack_mxfp8e5m2_bestnk_mxfp8e5m2_cref_t7

Backends

  • nk_attention_pack_nvfp4serialsmeneonneonfhmmetalapple9apple10
  • nk_attention_pack_mxfp4serialsmeneonneonbfdotmetalapple9apple10
  • nk_attention_pack_mxfp6e2m3serialsmeneonneonbfdotmetalapple9apple10
  • nk_attention_pack_mxfp6e3m2serialsmeneonneonbfdotmetalapple9apple10
  • nk_attention_pack_mxfp8e4m3serialsmeneonneonbfdotmetalapple9apple10
  • nk_attention_pack_mxfp8e5m2serialsmeneonneonbfdotmetalapple9apple10
nk_attention_packed_*_best× 10Ragged scaled-dot-product attention under a band of visible keys.
attention.h:439 ↗
nk_status_t nk_attention_packed_bf16_best(
    nk_size_t head_count,
    nk_size_t key_value_head_count,
    nk_size_t depth,
    nk_u32_t const *query_offsets,
    nk_size_t query_token_count,
    nk_f32_t scale,
    nk_size_t keys_before,
    nk_size_t keys_after,
    nk_bf16_t const *queries,
    nk_size_t query_stride,
    void const *key_value_packed,
    nk_f32_t *output,
    nk_size_t output_stride,
    nk_f32_t *log_sum_exp,
    nk_size_t tasks_begin,
    nk_size_t tasks_end,
    void *workspace,
    nk_size_t workspace_bytes,
    nk_capability_t capabilities,
    nk_stream_t stream);
SymbolqueriesBackends
nk_attention_packed_bf16_bestnk_bf16_t22
nk_attention_packed_f16_bestnk_f16_t11
nk_attention_packed_e4m3_bestnk_e4m3_t23
nk_attention_packed_i8_bestnk_i8_t20
nk_attention_packed_nvfp4_bestnk_nvfp4_cref_t7
nk_attention_packed_mxfp4_bestnk_mxfp4_cref_t7
nk_attention_packed_mxfp6e2m3_bestnk_mxfp6e2m3_cref_t7
nk_attention_packed_mxfp6e3m2_bestnk_mxfp6e3m2_cref_t7
nk_attention_packed_mxfp8e4m3_bestnk_mxfp8e4m3_cref_t7
nk_attention_packed_mxfp8e5m2_bestnk_mxfp8e5m2_cref_t7

Backends

  • nk_attention_packed_bf16serialhaswellskylakegenoasapphireamxsmeneonneonbfdotrvvv128relaxedcudaamperehopperblackwellblackwellultrarocmcdna3cdna4cdna5metalapple9apple10
  • nk_attention_packed_f16serialsmeneonneonfhmcudaampereblackwellblackwellultrametalapple9apple10
  • nk_attention_packed_e4m3serialhaswellskylakegenoasapphireamxdiamondamxsmeneonneonfhmrvvv128relaxedcudaamperehopperblackwellblackwellrtxblackwellultrarocmcdna4cdna5metalapple9apple10
  • nk_attention_packed_i8serialhaswellicelakesapphireamxsmeneonneonsdotrvvv128relaxedcudaamperehopperblackwellblackwellultrarocmcdna3cdna4cdna5metalapple10
  • nk_attention_packed_nvfp4serialsmeneonneonfhmmetalapple9apple10
  • nk_attention_packed_mxfp4serialsmeneonneonbfdotmetalapple9apple10
  • nk_attention_packed_mxfp6e2m3serialsmeneonneonbfdotmetalapple9apple10
  • nk_attention_packed_mxfp6e3m2serialsmeneonneonbfdotmetalapple9apple10
  • nk_attention_packed_mxfp8e4m3serialsmeneonneonbfdotmetalapple9apple10
  • nk_attention_packed_mxfp8e5m2serialsmeneonneonbfdotmetalapple9apple10

Computes O[s] = softmax(Q[s] × K[s]ᵀ × scale) × V[s] for every segment s over the keys each query row sees, as Causal, Sliding-Window and Bidirectional Masks defines. Covers self-attention, where query_offsets equal the pack-time key_offsets, cross-attention, and pooling, where query_offsets = {0, 1, 2, …} holds one query per segment, plus GQA/MQA via key_value_head_count < head_count. Pack key_value_packed with the same capabilities: CPU kernels return nk_pack_mismatch_k for buffers another capability packed, while GPU kernels trust it, as reading its device header would wait on the stream.

Parameters

head_countinQuery heads.
key_value_head_countinK/V heads, at most head_count.
depthinHead dimension.
query_offsetsinFirst query row of each segment, as segment count + 1 prefix sums.
query_token_countinQuery rows the call addresses, query_offsets[segments]; GPU kernels size tensor maps and workspaces from it without reading the offsets.
scaleinScore multiplier, typically 1 / √depth.
keys_beforeinKeys visible before each query's position: NUMKONG_SIZE_MAX for causal and bidirectional attention, w − 1 for a sliding window of w keys.
keys_afterinKeys visible after each query's position: 0 for causal attention, NUMKONG_SIZE_MAX for bidirectional.
queriesinToken-major matrix, one row of head_count × depth elements per query token, with query_stride bytes between rows.
query_strideinRow (token) stride of queries in bytes.
key_value_packedinBuffer produced by the matching nk_attention_pack_* backend.
outputoutToken-major F32 matrix, one row of head_count × depth elements per query token, with output_stride bytes between rows.
output_strideinRow (token) stride of output in bytes.
log_sum_expoutOptional natural log of Σ exp(score multiplier × q · k) over each row's keys, one per query token and head, [query_tokens,heads] with no padding, −∞ for a row without keys: what the backward pass recomputes the weights from. Null skips it.
tasks_begininFirst task of a window over the query tokens × heads grid, task t × head_count + h being head h of query token t, as in log_sum_exp.
tasks_endinEnd of that half-open window, clipped to the grid's live rows from query_offsets[0] × head_count. A window partitions the rows of output and log_sum_exp the call writes, per query token × head, so callers parallelize freely; a window may split a segment or a head group.
workspaceinScratch memory on nk_default_alignment_k, on the device of stream, used in its order, so the caller frees it only once the stream passes the call.
workspace_bytesinAt least what nk_attention_packed_workspace_size_bf16_best reports for the fast path, or 0 for the path without scratch.
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_attention_packed_workspace_size_*_best× 10Returns the workspace bytes nk_attention_packed_bf16_best takes for its fast path on these shapes, by host arithmetic alone.
attention.h:468 ↗
nk_status_t nk_attention_packed_workspace_size_bf16_best(
    nk_size_t head_count,
    nk_size_t key_value_head_count,
    nk_size_t depth,
    nk_size_t query_token_count,
    nk_size_t keys_before,
    nk_size_t keys_after,
    nk_capability_t capabilities,
    nk_size_t *bytes);
SymbolBackends
nk_attention_packed_workspace_size_bf16_best22
nk_attention_packed_workspace_size_f16_best11
nk_attention_packed_workspace_size_e4m3_best23
nk_attention_packed_workspace_size_i8_best20
nk_attention_packed_workspace_size_nvfp4_best7
nk_attention_packed_workspace_size_mxfp4_best7
nk_attention_packed_workspace_size_mxfp6e2m3_best7
nk_attention_packed_workspace_size_mxfp6e3m2_best7
nk_attention_packed_workspace_size_mxfp8e4m3_best7
nk_attention_packed_workspace_size_mxfp8e5m2_best7

Backends

  • nk_attention_packed_workspace_size_bf16serialhaswellskylakegenoasapphireamxsmeneonneonbfdotrvvv128relaxedcudaamperehopperblackwellblackwellultrarocmcdna3cdna4cdna5metalapple9apple10
  • nk_attention_packed_workspace_size_f16serialsmeneonneonfhmcudaampereblackwellblackwellultrametalapple9apple10
  • nk_attention_packed_workspace_size_e4m3serialhaswellskylakegenoasapphireamxdiamondamxsmeneonneonfhmrvvv128relaxedcudaamperehopperblackwellblackwellrtxblackwellultrarocmcdna4cdna5metalapple9apple10
  • nk_attention_packed_workspace_size_i8serialhaswellicelakesapphireamxsmeneonneonsdotrvvv128relaxedcudaamperehopperblackwellblackwellultrarocmcdna3cdna4cdna5metalapple10
  • nk_attention_packed_workspace_size_nvfp4serialsmeneonneonfhmmetalapple9apple10
  • nk_attention_packed_workspace_size_mxfp4serialsmeneonneonbfdotmetalapple9apple10
  • nk_attention_packed_workspace_size_mxfp6e2m3serialsmeneonneonbfdotmetalapple9apple10
  • nk_attention_packed_workspace_size_mxfp6e3m2serialsmeneonneonbfdotmetalapple9apple10
  • nk_attention_packed_workspace_size_mxfp8e4m3serialsmeneonneonbfdotmetalapple9apple10
  • nk_attention_packed_workspace_size_mxfp8e5m2serialsmeneonneonbfdotmetalapple9apple10

Only the Metal capabilities need one, for decoding at most four query rows against long keys, so every other capability reports 0. The bytes hold whenever the shapes allow the fast path, which may still decline it for the call's pointers or key count.

Every other parameter follows nk_attention_packed_bf16_best.

Parameters

head_countinQuery heads.
key_value_head_countinK/V heads, at most head_count.
depthinHead dimension.
query_token_countinQuery rows the call addresses.
keys_beforeinKeys visible before each query's position.
keys_afterinKeys visible after each query's position.
capabilitiesinOne device's capabilities, like nk_cpu_capabilities_enabled reports.
bytesoutThe workspace size, 0 when the capability needs none.

Statuses

nk_attention_packed_gradients_*_best× 7Gradients of ragged attention with respect to its queries, keys and values.
attention.h:571 ↗
nk_status_t nk_attention_packed_gradients_bf16_best(
    nk_size_t head_count,
    nk_size_t key_value_head_count,
    nk_size_t depth,
    nk_u32_t const *query_offsets,
    nk_size_t query_token_count,
    nk_f32_t scale,
    nk_size_t keys_before,
    nk_size_t keys_after,
    nk_bf16_t const *queries,
    nk_size_t query_stride,
    void const *key_value_packed,
    nk_f32_t const *output,
    nk_f32_t const *output_gradient,
    nk_size_t output_stride,
    nk_f32_t const *log_sum_exp,
    nk_f32_t *query_gradient,
    nk_size_t query_gradient_stride,
    nk_f32_t *key_gradient,
    nk_f32_t *value_gradient,
    nk_size_t key_value_gradient_stride,
    nk_size_t tasks_begin,
    nk_size_t tasks_end,
    void *workspace,
    nk_size_t workspace_bytes,
    nk_capability_t capabilities,
    nk_stream_t stream);
SymbolqueriesBackends
nk_attention_packed_gradients_bf16_bestnk_bf16_t13
nk_attention_packed_gradients_nvfp4_bestnk_nvfp4_cref_t1
nk_attention_packed_gradients_mxfp4_bestnk_mxfp4_cref_t1
nk_attention_packed_gradients_mxfp6e2m3_bestnk_mxfp6e2m3_cref_t1
nk_attention_packed_gradients_mxfp6e3m2_bestnk_mxfp6e3m2_cref_t1
nk_attention_packed_gradients_mxfp8e4m3_bestnk_mxfp8e4m3_cref_t1
nk_attention_packed_gradients_mxfp8e5m2_bestnk_mxfp8e5m2_cref_t1

Backends

  • nk_attention_packed_gradients_bf16serialcudaamperehopperblackwellblackwellultrarocmcdna3cdna4cdna5metalapple9apple10
  • nk_attention_packed_gradients_nvfp4serial
  • nk_attention_packed_gradients_mxfp4serial
  • nk_attention_packed_gradients_mxfp6e2m3serial
  • nk_attention_packed_gradients_mxfp6e3m2serial
  • nk_attention_packed_gradients_mxfp8e4m3serial
  • nk_attention_packed_gradients_mxfp8e5m2serial

Recomputes every attention weight from log_sum_exp, so nothing of the forward but its output and that per-row statistic is kept. The gradients are F32 and overwritten: query_gradient holds one row of head_count × depth elements per query token, while key_gradient and value_gradient hold one row of key_value_head_count × depth elements per key token, token t of segment s at row key_offsets[s] + t, with the offsets the pack stored. Rows past a segment's key count, the gaps and tails before the next segment's first key, are never written, so callers zero them when they matter. Tasks cover the segments × key-value heads grid, each owning one KV head of one segment together with the query heads sharing it, so tasks write disjoint gradients and each sums in a fixed order. The Blackwell kernels run a single pass for BF16 heads of 64 or 128 dimensions with 16-byte aligned rows, given a workspace of query_token_count × head_count × (2 × depth + 4) bytes, as nk_attention_packed_gradients_workspace_size_bf16_best reports, and add the query_gradient partials of different key blocks in F32 in any order, so their low bits may vary run to run. Other calls, and calls without a workspace, take the two-pass path.

Every other parameter follows nk_attention_packed_bf16_best.

Parameters

head_countinQuery heads.
key_value_head_countinK/V heads, at most head_count.
depthinHead dimension.
query_offsetsinFirst query row of each segment, as in the forward pass.
query_token_countinQuery rows the call addresses, as in the forward pass.
scaleinScore multiplier of the forward pass.
keys_beforeinKeys before each query that it saw in the forward pass.
keys_afterinKeys after each query that it saw in the forward pass.
queriesinThe forward's queries, with query_stride bytes between token rows.
query_strideinRow (token) stride of queries in bytes.
key_value_packedinThe pack the forward pass read.
outputinThe forward's output for these queries and pack.
output_gradientinGradient of the loss with respect to output, laid out like it.
output_strideinRow (token) stride of output and output_gradient in bytes.
log_sum_expinThe forward's natural log of Σ exp(scale × score) over each row's keys, one per query token and head, [query_tokens,heads] with no padding.
query_gradientoutGradient with respect to queries.
query_gradient_strideinBytes between the query_gradient rows of consecutive tokens.
key_gradientoutGradient with respect to the packed keys.
value_gradientoutGradient with respect to the packed values.
key_value_gradient_strideinBytes between the gradient rows of consecutive key tokens.
tasks_begininFirst task of a window over the segments × key-value heads grid.
tasks_endinEnd of that half-open window, clipped to the grid. A window partitions the outputs the call writes: segments × K/V heads own their key_gradient and value_gradient rows and the query_gradient rows of their query heads.
workspaceinScratch memory on nk_default_alignment_k, on the device of stream.
workspace_bytesinAt least what nk_attention_packed_gradients_workspace_size_bf16_best reports for the fast path, or 0 for the path without scratch.
capabilitiesinOne device's capabilities, like nk_cpu_capabilities_enabled reports.
streaminNull on the CPU, or the GPU stream of that device to queue on.
nk_attention_packed_gradients_workspace_size_*_best× 7Returns the workspace bytes nk_attention_packed_gradients_bf16_best takes for its fast path on these shapes, as nk_attention_packed_workspace_size_bf16_best does for attention: the Blackwell single pass for BF16 is the only one that needs any.
attention.h:646 ↗
nk_status_t nk_attention_packed_gradients_workspace_size_bf16_best(
    nk_size_t head_count,
    nk_size_t key_value_head_count,
    nk_size_t depth,
    nk_size_t query_token_count,
    nk_size_t keys_before,
    nk_size_t keys_after,
    nk_capability_t capabilities,
    nk_size_t *bytes);
SymbolBackends
nk_attention_packed_gradients_workspace_size_bf16_best13
nk_attention_packed_gradients_workspace_size_nvfp4_best1
nk_attention_packed_gradients_workspace_size_mxfp4_best1
nk_attention_packed_gradients_workspace_size_mxfp6e2m3_best1
nk_attention_packed_gradients_workspace_size_mxfp6e3m2_best1
nk_attention_packed_gradients_workspace_size_mxfp8e4m3_best1
nk_attention_packed_gradients_workspace_size_mxfp8e5m2_best1

Backends

  • nk_attention_packed_gradients_workspace_size_bf16serialcudaamperehopperblackwellblackwellultrarocmcdna3cdna4cdna5metalapple9apple10
  • nk_attention_packed_gradients_workspace_size_nvfp4serial
  • nk_attention_packed_gradients_workspace_size_mxfp4serial
  • nk_attention_packed_gradients_workspace_size_mxfp6e2m3serial
  • nk_attention_packed_gradients_workspace_size_mxfp6e3m2serial
  • nk_attention_packed_gradients_workspace_size_mxfp8e4m3serial
  • nk_attention_packed_gradients_workspace_size_mxfp8e5m2serial

Parameters

head_countinQuery heads.
key_value_head_countinK/V heads, at most head_count.
depthinHead dimension.
query_token_countinQuery rows the call addresses.
keys_beforeinKeys before each query that it saw in the forward pass.
keys_afterinKeys after each query that it saw in the forward pass.
capabilitiesinOne device's capabilities, like nk_cpu_capabilities_enabled reports.
bytesoutThe workspace size, 0 when the capability needs none.
nk_attention_rope_*_best× 3NeoX split-half rotary position embedding (RoPE): rotates channel pairs by per-token angles.
attention.h:766 ↗
nk_status_t nk_attention_rope_f32_best(
    nk_f32_t const *x,
    nk_f32_t const *cos,
    nk_f32_t const *sin,
    nk_f32_t *y,
    nk_size_t rows,
    nk_size_t head_count,
    nk_size_t depth,
    nk_size_t x_stride,
    nk_size_t y_stride,
    nk_capability_t capabilities,
    nk_stream_t stream);
Symbolx, yBackends
nk_attention_rope_f32_bestnk_f32_t6
nk_attention_rope_bf16_bestnk_bf16_t6
nk_attention_rope_e4m3_bestnk_e4m3_t6

Backends

  • nk_attention_rope_f32serialhaswellskylakeneoncudametal
  • nk_attention_rope_bf16serialhaswellskylakeneoncudametal
  • nk_attention_rope_e4m3serialhaswellskylakeneoncudametal

Rotates the channel pair i and i + h of every head in every row, h being half the depth:

*  y[i]     = x[i] · cos - x[i + h] · sin
*  y[i + h] = x[i] · sin + x[i + h] · cos
*  

The rotation is linear, so FP8 codes rotate at their own scale and need none passed.

Parameters

xinInput token matrix of shape rows by head_count × depth.
cosinPer-token cosine angle grid of shape rows by depth / 2, shared by heads.
sininPer-token sine angle grid of shape rows by depth / 2, shared across heads.
youtOutput matrix, same shape and dtype as x; may alias x for in-place rotation.
rowsinThe number of token rows.
head_countinThe number of heads per token.
depthinThe even number of channels per head.
x_strideinRow stride of x in bytes.
y_strideinRow stride of y in bytes.
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_attention_output_dtypeReturns the output dtype for attention: accumulator-precision F32 for all inputs.
attention.h:4371 ↗
nk_dtype_t nk_attention_output_dtype(nk_dtype_t dtype);
nk_attention_rope_error_boundReturns the error bound of RoPE outputs before they round into their type, relative to the two products each of them sums: 3 roundings in F32, which every input type rotates in, through the descale, the product and the sum.
attention.h:4384 ↗
nk_f64_t nk_attention_rope_error_bound(nk_dtype_t dtype);