C · Curved Metrics

Curved Metrics

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

Curved-space kernels are separate from the flat Euclidean family because their dataflow is different. They combine vectors with an extra metric tensor or covariance inverse.

// Complex bilinear form: aᴴ M b
nk_f32c_t a[32], b[32], metric[32 * 32];
nk_f64c_t result = {0, 0};
nk_bilinear_f32c_best(a, b, metric, 32, &result, capabilities, NULL);

// Real Mahalanobis distance: √((a−b)ᵀ M⁻¹ (a−b))
nk_f32_t x[64], y[64], inv_cov[64 * 64];
nk_f64_t distance = 0;
nk_mahalanobis_f32_best(x, y, inv_cov, 64, &distance, capabilities, NULL);

API

Functions

nk_bilinear_*_best× 4Bilinear form between vectors a and b under metric tensor C.
curved.h:76 ↗
nk_status_t nk_bilinear_f64_best(
    nk_f64_t const *a,
    nk_f64_t const *b,
    nk_f64_t const *c,
    nk_size_t n,
    nk_f64_t *result,
    nk_capability_t capabilities,
    nk_stream_t stream);
Symbola, b, cresultBackends
nk_bilinear_f64_bestnk_f64_tnk_f64_t6
nk_bilinear_f32_bestnk_f32_tnk_f64_t6
nk_bilinear_f16_bestnk_f16_tnk_f32_t4
nk_bilinear_bf16_bestnk_bf16_tnk_f32_t5

Backends

  • nk_bilinear_f64serialneonsmef64haswellskylakervv
  • nk_bilinear_f32serialneonsmef64haswellskylakervv
  • nk_bilinear_f16serialneonhaswellrvv
  • nk_bilinear_bf16serialneonbfdothaswellgenoarvv

Computes aᵀ × C × b = Σᵢ Σⱼ aᵢ × cᵢⱼ × bⱼ

Parameters

ainThe first vector.
binThe second vector.
cinThe metric tensor or covariance matrix, stored row-major as an n × n matrix.
ninThe number of dimensions in the vectors.
resultoutThe output bilinear form value.
capabilitiesinOne device's capabilities, like nk_cpu_capabilities_enabled reports.
streaminNull on the CPU, or the GPU stream of that device to queue on.

Notes

The output value can be negative.

Statuses

nk_mahalanobis_*_best× 4Mahalanobis distance between vectors a and b under metric tensor C.
curved.h:110 ↗
nk_status_t nk_mahalanobis_f64_best(
    nk_f64_t const *a,
    nk_f64_t const *b,
    nk_f64_t const *c,
    nk_size_t n,
    nk_f64_t *result,
    nk_capability_t capabilities,
    nk_stream_t stream);
Symbola, b, cresultBackends
nk_mahalanobis_f64_bestnk_f64_tnk_f64_t6
nk_mahalanobis_f32_bestnk_f32_tnk_f64_t6
nk_mahalanobis_f16_bestnk_f16_tnk_f32_t4
nk_mahalanobis_bf16_bestnk_bf16_tnk_f32_t5

Backends

  • nk_mahalanobis_f64serialneonsmef64haswellskylakervv
  • nk_mahalanobis_f32serialneonsmef64haswellskylakervv
  • nk_mahalanobis_f16serialneonhaswellrvv
  • nk_mahalanobis_bf16serialneonbfdothaswellgenoarvv

Computes √((a-b)ᵀ × C × (a-b)) = √(Σᵢ Σⱼ (aᵢ-bᵢ) × cᵢⱼ × (aⱼ-bⱼ))

Parameters

ainThe first vector.
binThe second vector.
cinThe Positive Semi-Definite (PSD) matrix, stored row-major as an n × n matrix.
ninThe number of dimensions in the vectors.
resultoutThe output distance value.
capabilitiesinOne device's capabilities, like nk_cpu_capabilities_enabled reports.
streaminNull on the CPU, or the GPU stream of that device to queue on.

Notes

  • The output value is non-negative when C is PSD.
  • The output value is zero if and only if the two vectors are identical.
  • The matrix C must be positive semi-definite. If C is not PSD, the quadratic form (a-b)ᵀ C (a-b) may be negative, and the square root will produce NaN.

Statuses

nk_bilinear_*_best× 4Complex bilinear form between vectors a and b under metric tensor C.
curved.h:137 ↗
nk_status_t nk_bilinear_f64c_best(
    nk_f64c_t const *a,
    nk_f64c_t const *b,
    nk_f64c_t const *c,
    nk_size_t n,
    nk_f64c_t *results,
    nk_capability_t capabilities,
    nk_stream_t stream);
Symbola, b, cresultsBackends
nk_bilinear_f64c_bestnk_f64c_tnk_f64c_t3
nk_bilinear_f32c_bestnk_f32c_tnk_f64c_t4
nk_bilinear_f16c_bestnk_f16c_tnk_f32c_t2
nk_bilinear_bf16c_bestnk_bf16c_tnk_f32c_t3

Backends

  • nk_bilinear_f64cserialsmef64skylake
  • nk_bilinear_f32cserialneonsmef64skylake
  • nk_bilinear_f16cserialneon
  • nk_bilinear_bf16cserialneonbfdotgenoa

Parameters

ainThe first complex vector.
binThe second complex vector.
cinThe complex metric tensor, stored row-major as an n × n matrix.
ninThe number of dimensions in the vectors.
resultsoutThe output complex value with real and imaginary parts.
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_bilinear_output_dtypeReturns the output dtype for bilinear forms.
curved.h:335 ↗
nk_dtype_t nk_bilinear_output_dtype(nk_dtype_t dtype);
nk_mahalanobis_output_dtypeReturns the output dtype for Mahalanobis metrics.
curved.h:350 ↗
nk_dtype_t nk_mahalanobis_output_dtype(nk_dtype_t dtype);
nk_bilinear_error_boundReturns the error bound of bilinear forms, per nk_accumulation_error_bound of their output.
curved.h:362 ↗
nk_f64_t nk_bilinear_error_bound(nk_dtype_t dtype);
nk_mahalanobis_error_boundReturns the error bound of Mahalanobis metrics, per nk_accumulation_error_bound of their output.
curved.h:368 ↗
nk_f64_t nk_mahalanobis_error_bound(nk_dtype_t dtype);
nk_curved_find_kernelFinds the curved kernel of kind for dtype, from the best of capabilities.
curved.h:384 ↗
nk_status_t nk_curved_find_kernel(
    nk_kernel_kind_t kind,
    nk_dtype_t dtype,
    nk_capability_t capabilities,
    nk_kernel_punned_t *kernel,
    nk_capability_t *capability);

Parameters

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

Statuses