Swift · Matrix Kernels

Matrix Kernels

import NumKong 8.0.0-rc.1 · b1f866ab

Packed kernels are the GEMM-like throughput path. They are useful when the right-hand side is reused across many query batches.

import NumKong

let a = try Tensor<Float32>.fromArray([1, 2, 3, 4, 5, 6], rows: 2, columns: 3)
let b = try Tensor<Float32>.fromArray([7, 8, 9, 1, 0, 1], rows: 2, columns: 3)

let packed = try b.packForDots()           // PackedMatrix<Float32>, owned
let dots   = try a.dotsPacked(packed)      // Tensor<Float64>, 2x2
let angs   = try a.angularsPacked(packed)  // Tensor<Float64>, 2x2
let eucs   = try a.euclideansPacked(packed)// Tensor<Float64>, 2x2

assert(dots.rows == 2 && dots.columns == 2)

The free-function API (dotsPacked, angularsPacked, etc.) accepts MatrixView, PackedMatrix, and MatrixSpan directly for cases where you need manual buffer management — see the verbosity comparison in the Tensors section above.

§ Symmetric Matrix Kernels

Symmetric kernels compute self-similarity or self-distance matrices. They are the right shape for SYRK-like workloads and row-window partitioning.

import NumKong

let vectors = try Tensor<Float32>.fromArray([
    1, 0, 0,
    0, 1, 0,
    0, 0, 1,
], rows: 3, columns: 3)

let gram = try vectors.dotsSymmetric()       // Tensor<Float64>, 3x3
let dists = try vectors.euclideansSymmetric()// Tensor<Float64>, 3x3
let angs  = try vectors.angularsSymmetric()  // Tensor<Float64>, 3x3

assert(gram.rows == 3 && gram.columns == 3)

The free-function form (dotsSymmetric, angularsSymmetric, etc.) exposes rowsBegin and rowsEnd parameters for external partitioning.

API

ClassPackedMatrixOwns a kernel-optimized packed copy of a matrix for batch distance computations.6
Matrix.swift:55 ↗
final class PackedMatrix<Element>

Conforms to

Sendable
rows
Matrix.swift:56 ↗
let rows: Int
columns
Matrix.swift:57 ↗
let columns: Int
byteCount
Matrix.swift:58 ↗
let byteCount: Int
rawBufferProvides read-only access to the underlying packed bytes.
Matrix.swift:77 ↗
var rawBuffer: UnsafeRawBufferPointer { get }
initPacks the given matrix view into a kernel-optimized layout for batch dot products.
Matrix.swift:243 ↗
convenience init(packing matrix: MatrixView<Element>) throws where Element : NumKongDotsMatrixElement
convenience init(packing tensor: Tensor<Element>) throws where Element : NumKongDotsMatrixElement

Packs a tensor's storage into a kernel-optimized layout for batch dot products.

shapeReads the packed matrix shape, [rows,columns], from the buffer's self-describing header.
Matrix.swift:253 ↗
var shape: (rows: Int, columns: Int) { get throws } where Element : NumKongDotsMatrixElement
ProtocolNumKongDotsMatrixElementElement type that supports batch dot-product matrix operations.1
Matrix.swift:109 ↗
protocol NumKongDotsMatrixElement

Conforming types

DotsOutputThe element type of the result matrices that dotsPacked and dotsSymmetric write.
Matrix.swift:111 ↗
associatedtype DotsOutput
ProtocolNumKongSpatialsMatrixElementElement type that supports batch angular and Euclidean matrix operations.1
Matrix.swift:142 ↗
protocol NumKongSpatialsMatrixElement : NumKongDotsMatrixElement

Conforms to

Conforming types

SpatialOutputThe element type of the result matrices of the angular and Euclidean batch functions.
Matrix.swift:144 ↗
associatedtype SpatialOutput
ProtocolNumKongSetsMatrixElementElement type that supports batch Hamming and Jaccard matrix operations.2
Matrix.swift:191 ↗
protocol NumKongSetsMatrixElement : NumKongDotsMatrixElement

Conforms to

Conforming types

HammingOutputThe element type of the result matrices that hammingsPacked and hammingsSymmetric write.
Matrix.swift:193 ↗
associatedtype HammingOutput
JaccardOutputThe element type of the result matrices that jaccardsPacked and jaccardsSymmetric write.
Matrix.swift:195 ↗
associatedtype JaccardOutput

Functions

dotsPackedComputes dot products between each row of a and every row packed in bPacked.
NumKong.swift:267 ↗
func dotsPacked<Element>(
    _ a: MatrixView<Element>,
    _ bPacked: PackedMatrix<Element>,
    _ result: inout MatrixSpan<Element.DotsOutput>
) throws where Element : NumKongDotsMatrixElement
dotsSymmetricComputes the symmetric dot-product matrix for all row pairs in vectors.
NumKong.swift:294 ↗
func dotsSymmetric<Element>(
    _ vectors: MatrixView<Element>,
    _ result: inout MatrixSpan<Element.DotsOutput>,
    rowsBegin: Int = 0,
    rowsEnd: Int? = nil
) throws where Element : NumKongDotsMatrixElement
angularsPackedComputes angular distances between each row of a and every row packed in bPacked.
NumKong.swift:328 ↗
func angularsPacked<Element>(
    _ a: MatrixView<Element>,
    _ bPacked: PackedMatrix<Element>,
    _ result: inout MatrixSpan<Element.SpatialOutput>
) throws where Element : NumKongSpatialsMatrixElement
euclideansPackedComputes Euclidean distances between each row of a and every row packed in bPacked.
NumKong.swift:355 ↗
func euclideansPacked<Element>(
    _ a: MatrixView<Element>,
    _ bPacked: PackedMatrix<Element>,
    _ result: inout MatrixSpan<Element.SpatialOutput>
) throws where Element : NumKongSpatialsMatrixElement
angularsSymmetricComputes the symmetric angular-distance matrix for all row pairs in vectors.
NumKong.swift:382 ↗
func angularsSymmetric<Element>(
    _ vectors: MatrixView<Element>,
    _ result: inout MatrixSpan<Element.SpatialOutput>,
    rowsBegin: Int = 0,
    rowsEnd: Int? = nil
) throws where Element : NumKongSpatialsMatrixElement
euclideansSymmetricComputes the symmetric Euclidean-distance matrix for all row pairs in vectors.
NumKong.swift:414 ↗
func euclideansSymmetric<Element>(
    _ vectors: MatrixView<Element>,
    _ result: inout MatrixSpan<Element.SpatialOutput>,
    rowsBegin: Int = 0,
    rowsEnd: Int? = nil
) throws where Element : NumKongSpatialsMatrixElement
hammingsPackedComputes Hamming distances between each row of a and every row packed in bPacked.
NumKong.swift:448 ↗
func hammingsPacked<Element>(
    _ a: MatrixView<Element>,
    _ bPacked: PackedMatrix<Element>,
    _ result: inout MatrixSpan<Element.HammingOutput>
) throws where Element : NumKongSetsMatrixElement
hammingsSymmetricComputes the symmetric Hamming-distance matrix for all row pairs in vectors.
NumKong.swift:475 ↗
func hammingsSymmetric<Element>(
    _ vectors: MatrixView<Element>,
    _ result: inout MatrixSpan<Element.HammingOutput>,
    rowsBegin: Int = 0,
    rowsEnd: Int? = nil
) throws where Element : NumKongSetsMatrixElement
jaccardsPackedComputes Jaccard distances between each row of a and every row packed in bPacked.
NumKong.swift:507 ↗
func jaccardsPacked<Element>(
    _ a: MatrixView<Element>,
    _ bPacked: PackedMatrix<Element>,
    _ result: inout MatrixSpan<Element.JaccardOutput>
) throws where Element : NumKongSetsMatrixElement
jaccardsSymmetricComputes the symmetric Jaccard-distance matrix for all row pairs in vectors.
NumKong.swift:534 ↗
func jaccardsSymmetric<Element>(
    _ vectors: MatrixView<Element>,
    _ result: inout MatrixSpan<Element.JaccardOutput>,
    rowsBegin: Int = 0,
    rowsEnd: Int? = nil
) throws where Element : NumKongSetsMatrixElement