Repository navigation
Evaluate the KernelRidge RBF kernel in blocks with a vectorizable exp (#488) - #513
Conversation
…#488) Prediction was already one sgemm for the cross terms. What remained was a libm exp call per query and training pair inside a range loop, feeding one running f64 sum. That loop was 96 percent of predict time on the Diabetes row. rbf_exp_block computes exp(x) as exp(x / 1024) ^ 1024, a degree 8 polynomial and ten squarings. The body is straight-line arithmetic, so the loop vectorizes, which it cannot do through a libm call. The measured worst relative error against libm on [-104, 104] is 3.6e-12, and exponents below -104 return exactly 0. Prediction now works a block of rows at a time: one sgemm, one pass to form the exponents, the block exponential, then a dot product over eight partial sums. The scratch buffers are bounded at 64 KiB and 128 KiB where the full n_queries by n_train cross term used to be allocated. kernel_ridge_fit stores the training-row squared norms and the f64 view of the dual coefficients on the model. Both were rebuilt on every predict call. Diabetes predict, old and new interleaved in one process on one machine: 0.0766 ms to 0.0522 ms, and 2.68 us to 0.62 us for a single query row. The canonical R2 moves from 0.461917520 to 0.461917579 against scikit-learn at 0.461917818, inside the row's 0.005 tolerance. Fit numerics are untouched. tests/test_opt_kernelridge_predict.flow bounds the exponential against libm and checks predictions against an f64 reference across block seams. benchmarks/bench_predict_kernel_ridge.flow and its .py counterpart time prediction alone across batch size, training-set size and feature count.
|
CI on this PR, runs 44429712312 and 44429712322. Passing: Run all tests and examples (118 of 118, including The canonical row from this run, which is the Linux number the description said I had not seen:
These are three separate runner sessions, so they are not a controlled pair. The first and third share a CPU model and scikit-learn's time agrees between them to 2 percent, and Flow's predict time roughly halves. R2 is 0.461918 on both sides and the row stays Two jobs fail, and both fail the same way on unmodified
The
|
Closes #488
Reproduction
The gap reported in the issue (Diabetes predict 4.861 ms against scikit-learn 0.628 ms) is no longer on
main. Earlier merges closed it, including thekernel_type == 1branch selection the issue comment points at. Baseline onmainat 645e6e4, one machine (Apple M4 Max, Accelerate, Flow toolchain at the CI pin 88aac509,-O3):Both targets in the issue, 0.95x and then 1.05x, were already met at baseline. The change below is what was left to take.
Profile
Diabetes shape, 89 queries by 353 training rows by 10 features, median of 15 rounds in one process:
mainexp, weighted sum: 31,417 pairs (the remainder)The first two stages were timed on their own and the third is the whole call less those two. A replica of the pair loop timed on its own gave 0.078 ms. The kernel was already one sgemm. What remained was a libm
expcall per pair inside a range loop, feeding one running f64 sum.Allocation audit: there was no allocation per element or per row before this change, and there is none after. Per call,
mainallocated five buffers including the full n_queries by n_train cross term. This branch allocates three, and the two scratch buffers are bounded at 64 KiB and 128 KiB whatever the batch size.Change
rbf_exp_blockcomputesexpfor a block of exponents asexp(x / 1024) ^ 1024: a degree 8 polynomial and ten squarings. The body is straight-line arithmetic, so the compiler vectorizes the loop, which it cannot do through a libm call. Measured worst relative error against libm on [-104, 104] is 3.6e-12. Exponents below -104, where the result is under the smallest f32 denormal, return exactly 0.rbf_exp_block, then a dot product with eight partial sums.kernel_ridge_fitstores the training-row squared norms and the f64 view of the dual coefficients on the model asx_train_sqanddual_coef_d. Both were rebuilt on every predict call.The dot product stays in Flow. A block is small enough that a threaded BLAS would spend longer dispatching than computing, which AGENTS.md records at about 0.1 ms per call on OpenBLAS.
Parity
parity verifiedunder its 0.005 tolerance. All 19 rows keep their parity status.DETAIL|KernelRidge_RBFrecords for alpha, gamma, training size and the dual coefficient norm, sum, min, max and mean are byte identical before and after. Fit numerics are untouched.Scaling, prediction only
benchmarks/bench_predict_kernel_ridge.flowandbenchmarks/bench_predict_kernel_ridge.pybuild bit-identical float32 inputs and timepredictalone. For kernel ridge the support size is the training-set size. Same machine, same command, before and after.Every case on
mainwas already above 1.05x. The thinnest, 1000 queries against 1000 training rows, moves from 1.17x to 1.59x.The old and new paths interleaved in one process give 0.0766 ms to 0.0522 ms on the Diabetes test split, 1.47x, and 2.68 us to 0.62 us for a single query row, 4.3x.
Tests
tests/test_opt_kernelridge_predict.flow:mainit exits 1.python tools/run_all.pyat-O0: 106 of 106 tests onmain, 118 of 118 tests and examples on this branch. A comment in the library was reworded while that run was in progress, so the 50 tests that had already run were run again on the final source. All 50 pass.Not done here
expis already about 2 ns. glibc's is slower, so the CI runner should gain more. The canonical row from this PR's own CI run is the number to read, and I have not seen it yet.mainwrites them from the CI runner.benchmarks/estimator_coverage.pyandbenchmarks/generate_estimator_bench.pywere re-run and produce no diff.expper pair and belongs to [Perf #478] KernelRidge RBF fit: eliminate the ~9.7x training gap #487.lssvm_classifier_fitinlib/scikit/svm.flowpasses kernel type 1 tokernel_ridge_fit, which is the polynomial kernel. Its comments describe an RBF model. I left it alone because it changes what that estimator computes.