Skip to content

Roadmap #11: optimize GaussianNB.predict_proba #220

Closed

Description

@godofecht

Tracks roadmap rank 11: GaussianNB.predict_proba. Complete the roadmap optimization hypothesis with sklearn-parity-gated benchmark evidence, then update roadmap evidence/disposition before closing.

Implementation ready

The inference optimization landed in #317: repeated Gaussian constants/logs were moved out of the sample × class × feature loop, direct row-major Matrix.data access replaced helper calls in the hot path, one class-score scratch buffer is reused, and probabilities are written directly to the output matrix.

PR #322 supplies the missing dedicated predict_proba evidence. It preserves the pre-#317 implementation as an in-test reference, checks every probability cell on a deterministic 1000 × 32 workload, then runs an identical-workload A/B timing harness. The branch has been rebuilt on current main and contains only that benchmark/regression test.

Fresh CI is fully green: the dedicated GaussianNB.predict_proba benchmark test passes, all 36 Flow tests/examples pass, canonical 19/19 parity passes, the scaled self-baseline gate passes, and the Python native package passes. PR #322 is ready for review. Keep this issue open until #322 lands and the roadmap evidence is regenerated on main.

Activity

  1. self-assigned this
    on Aug 17, 2026
  2. godofecht commented on Aug 18, 2026

    @godofecht
    OwnerAuthor

    Closing: the work this issue tracks is already on main.

    Implementation landed in #317 (commit f09a54e, lib/scikit/naive_bayes.flow). gaussian_nb_predict_proba now:

    • hoists every fitted-state-only term out of the sample x class x feature loop into _gaussian_nb_prepare_terms, which precomputes class_log_prior[c], log_norm[c*p+j] = -0.5*log(2pi) - 0.5*log(var) and inv_two_var[c*p+j] = 1/(2*var) once per call. The pre-Optimize GaussianNB predict and predict_proba hot loops #317 code called log() three times per (sample, class, feature).
    • reads the query row through direct row-major X.data[i*p + j] instead of matrix_at.
    • allocates the per-class score scratch buffer (log_probs) once outside the sample loop instead of per sample.
    • writes probabilities straight into probs.data[i*n_classes + c] instead of going through matrix_set/matrix_at.

    Evidence landed in #322 (commit e7aaab5), tests/test_gaussian_nb_predict_proba_benchmark.flow. It keeps the pre-#317 implementation in-test as a reference, checks every probability cell on a deterministic 1000 x 32 / 2-class workload, then times both on the identical workload.

    Re-ran it on current main (c1e01df, macOS arm64, FLOW_HOST=python, FLOW_OPT_LEVEL=0):

    GaussianNB predict_proba A/B: reference=5.729000 ms optimized=1.074000 ms speedup=5.334x
    OK: GaussianNB predict_proba preserves reference probabilities
    

    Parity holds cell-by-cell against the old implementation, and the hot path is 5.3x faster. There is no further hoistable work in that loop, so opening another PR for this issue would be a cosmetic diff. Closing as done by #317 + #322.

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

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions