Skip to content

[Perf #478] Audit and consolidate shared hot primitives: dot, squared distance, RBF, reductions and row partitioning #498

Open

Description

@godofecht

Parent: #478. Related: #479 #480 #482 #483 #484 #485 #487 #488.

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.

Activity

  1. godofecht commented on Aug 26, 2026

    @godofecht
    OwnerAuthor

    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:

    • ml/bit-exact-shared-squared-distance-primitive.md
    • ml/bit-exact-shared-squared-distance-primitive.flow

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions