You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Perf #478] Audit and consolidate shared hot primitives: dot, squared distance, RBF, reductions and row partitioning #498
Several current losses share low-level primitives: dense dot products, squared distances/RBF kernels, feature/class reductions, centroid accumulation and sample partitioning. Audit whether estimator implementations duplicate slower scalar versions instead of using one optimized primitive layer.
Deliverables: call-site inventory; microbenchmarks by shape/layout; contiguous/strided variants; allocation/copy audit; vectorization evidence; shared implementations where semantics match; estimator-specific fallbacks only when justified.
Acceptance: every canonical hot loop maps to an identified primitive or documented estimator-specific kernel; duplicated implementations are eliminated where possible; primitive benchmarks become regressions; fixes can be attributed back to affected estimator issues.
Concrete result from the OpenProjects pass: the shared squared-distance primitive can be made zero-copy and bit-exact in strict f32 mode if it preserves the same logical element order and the same recurrence sum = f32(sum + f32(d*d)). A copied-row implementation and a direct strided implementation are then bit-identical by induction because the copy preserves each f32 operand exactly. This gives a safe primitive contract for KMeans / KernelRidge / SVM / neighbors without forcing norm expansion or reassociation.
Recommended substrate contract is conceptually sqdist_f32_strided(a_data,a_offset,a_stride,b_data,b_offset,b_stride,features) with no allocation, monotone logical feature order, and explicit numerical mode. RBF should wrap that distance primitive rather than duplicate the loop.
This is specifically useful because current KMeans already has a direct flat-storage _kmeans_sqdist, while KernelRidge has its own RBF loop and generic paths that materialize rows. For the current generic KernelRidge prediction shape, eliminating materialization removes exactly M*(N+1) temporary row vectors and M*F*(N+1) copied f32 values; at the existing 89-query / 353-train / 10-feature Diabetes shape that is 31,506 vectors and 315,060 copied f32s (1,260,240 payload bytes).
I also ran an independent local f32 oracle across random feature lengths 1,2,3,4,7,16,31,64; copied vs direct recurrence matched bit-for-bit in every tested pair.
Full proof + Flow witness are now in godofecht/OpenProjects:
The next production test should migrate callers one at a time and record N_calls, N_alloc, copied bytes, and primitive time. Strict mode should require zero hidden copies and unchanged estimator parity; relaxed mode can separately permit norm-expansion/GEMM/SIMD reassociation.
Activity
godofecht commentedon Aug 26, 2026
Concrete result from the OpenProjects pass: the shared squared-distance primitive can be made zero-copy and bit-exact in strict f32 mode if it preserves the same logical element order and the same recurrence
sum = f32(sum + f32(d*d)). A copied-row implementation and a direct strided implementation are then bit-identical by induction because the copy preserves each f32 operand exactly. This gives a safe primitive contract for KMeans / KernelRidge / SVM / neighbors without forcing norm expansion or reassociation.Recommended substrate contract is conceptually
sqdist_f32_strided(a_data,a_offset,a_stride,b_data,b_offset,b_stride,features)with no allocation, monotone logical feature order, and explicit numerical mode. RBF should wrap that distance primitive rather than duplicate the loop.This is specifically useful because current KMeans already has a direct flat-storage
_kmeans_sqdist, while KernelRidge has its own RBF loop and generic paths that materialize rows. For the current generic KernelRidge prediction shape, eliminating materialization removes exactlyM*(N+1)temporary row vectors andM*F*(N+1)copied f32 values; at the existing 89-query / 353-train / 10-feature Diabetes shape that is 31,506 vectors and 315,060 copied f32s (1,260,240 payload bytes).I also ran an independent local f32 oracle across random feature lengths 1,2,3,4,7,16,31,64; copied vs direct recurrence matched bit-for-bit in every tested pair.
Full proof + Flow witness are now in
godofecht/OpenProjects:ml/bit-exact-shared-squared-distance-primitive.mdml/bit-exact-shared-squared-distance-primitive.flowThe next production test should migrate callers one at a time and record
N_calls,N_alloc, copied bytes, and primitive time. Strict mode should require zero hidden copies and unchanged estimator parity; relaxed mode can separately permit norm-expansion/GEMM/SIMD reassociation.