Repository navigation
Vectorize GaussianNB class likelihood evaluation (#484) - #512
Conversation
The inner loop of gaussian_nb_predict was one running f32 sum written as a range loop. Flow emits a range loop with a runtime stride, and a single floating-point reduction may not be reordered, so the loop ran scalar with every term waiting on the add before it. The likelihood now goes through one kernel that keeps eight partial sums in a while loop, which compiles to vector code. predict and predict_proba both use it, and gaussian_nb_predict_joint_log_proba exposes the matrix they reduce. The per-class constant and the 1 / (2 * var) table are computed by fit and stored on the model. They were rebuilt on every inference call: 650 log calls and 640 divisions per call on the Digits shape. Digits predict, old and new interleaved in one process on one machine: 0.108 ms to 0.018 ms. No predicted label changes on the Iris or Digits test splits or on the 1437 Digits training rows, and the fitted priors, means and variances are untouched. tests/test_opt_gaussiannb_predict.flow checks every joint log-likelihood entry against an f64 reference at feature counts on both sides of the block width. benchmarks/bench_predict_gaussian_nb.flow and its .py counterpart time prediction alone across batch size, feature count and class count.
|
CI on this PR, run 44422898512. Passing: Canonical benchmark contract, Canonical 19/19 KMeans parity, FLOW syntax and imports, Python native package, sklearn execution architecture map, disparity-audit. The canonical rows measured by that run, on an AMD EPYC 9V45 runner:
The frozen artifact on Three jobs fail, and all three fail the same way on unmodified
None of those rows touch |
Closes #484
Reproduction
The gap reported in the issue (Digits predict 1.304 ms against scikit-learn 0.538 ms) is no longer on
main. Earlier merges closed it. Baseline onmainat 645e6e4, one machine (Apple M4 Max, Accelerate, Flow toolchain at the CI pin 88aac509,-O3):The macOS monotonic clock resolves 1 us, so the Iris predict row reads 0 after the change. The interleaved comparison below resolves it. The fit difference is inside the run to run spread (IQR 0.0065 ms before).
The scaling benchmark added here did find a loss on
mainthat the canonical rows do not show: at 128 and 256 features prediction ran at 0.97x and 0.75x of scikit-learn.Profile
Digits shape, 360 rows by 64 features by 10 classes, old and new interleaved in one process, median of 15 rounds:
logcalls per predict callBefore the change 98.8 percent of predict time was the inner loop, at 0.46 ns per term. That is the latency of one f32 add: the sum was a single dependency chain, and the range loop it sat in is emitted with a runtime stride that blocks vectorization (flooooooooooow/flow#1004).
Change
_gaussian_nb_weighted_sq_distevaluatessum_j (x[j] - mean[j])^2 / (2 var[j])with eight partial sums in a while loop and a scalar tail.predictandpredict_probaboth call it.gaussian_nb_fitcomputes the per-class constant and the1 / (2 var)table once and stores them on the model asclass_constandinv_two_var.gaussian_nb_predict_joint_log_probais new. It returns the n_samples by n_classes matrix that scikit-learn exposes under the same name.The expanded form that would turn the batch into one gemm was rejected.
var_smoothingleaves a constant feature with a weight near 1e8, and the three expanded terms then cancel in f32. The comment on the kernel records this.Parity
parity verifiedon Iris (0.966667) and Digits (0.741667).DETAIL|GaussianNBrecords for class priors, mean and variance row norms, Frobenius norms and variance min and max are byte identical before and after.Scaling, prediction only
benchmarks/bench_predict_gaussian_nb.flowandbenchmarks/bench_predict_gaussian_nb.pybuild bit-identical float32 inputs and timepredictalone. 2000 training rows. Same machine, same command, before and after.Tests
tests/test_opt_gaussiannb_predict.flowcompares every joint log-likelihood entry with an f64 reference at 1, 3, 7, 8, 9, 16, 23, 64 and 67 features, checks the constants fit stored, checkspredictagainst the reference argmax andpredict_probaagainst the reference softmax.mainit exits 1.python tools/run_all.pyat-O0: 106 of 106 tests onmain, 118 of 118 tests and examples on this branch.Not done here
mainwrites them from the CI runner, and a local run would replace them with numbers from a different machine.benchmarks/estimator_coverage.pyandbenchmarks/generate_estimator_bench.pywere re-run and produce no diff.CHANGELOG.mdand rejects a sentence that is already in it, at line 541.