Repository navigation
perf(graph): sparse GO DAG + O(1) top_sort (3× faster build, ~3× less RAM) - #15
Merged
Merged
Conversation
… less RAM Replace the dense boolean adjacency `self.dag = np.zeros((T, T), bool)` with CSR parent/child index arrays + degree vectors, built from the per-term adj/children sets the code already maintained. Bit-exact by construction: parent/child counts reproduce the old dag.sum(axis=1)/sum(axis=0), and CSR slices are ascending — matching what np.nonzero/np.flatnonzero yielded from the dense matrix. Verified identical outputs across sparse / dense / no_orphans / IA-weighted / NK scenarios and end-to-end on a 25k-protein corpus; repo test suite green (9 passed, 6 xfailed). Full GO (24.5k BPO terms): build 1.87s → 0.61s, peak RSS 312 → 108 MB. Touches only graph.py (__init__, top_sort, toi, logging, _children_cache, _ancestors_csr). No remaining `.dag` references.
Kahn's queue used list.pop(0) (O(n) per pop -> O(n^2) total). A collections.deque with popleft() is O(1); FIFO order is identical so self.order is unchanged (bit-exact). Verified identical outputs across all scenarios.
The py3.10 CI leg installs pandas 2.x (the last release line that supports Python 3.10), which keeps the filename/ns MultiIndex levels as object dtype. The py3.11/3.12 legs install pandas 3.x, which infers the PyArrow-backed StringDtype for those same levels. The parity oracle was frozen under pandas 3.x, so Index.equals returned False on py3.10 despite identical labels and ordering, failing the parity gate only on 3.10. Comparing the materialised index tuples is dtype-agnostic and still pins exact row alignment, so the sparse kernels stay bit/ULP-exact across all three Python versions without changing any math.
frapercan
added a commit
that referenced
this pull request
Jun 20, 2026
…1.9→7.0 GB) (#16) Stacked on #15. Shows only the prediction-matrix change. ## What The prediction score matrix was a dense `float` (n_prot × n_terms) — ~20 GB at full-GO scale, almost all zeros. It is now built, propagated and consumed sparse: - `graph.propagate_to_coo()` — sparse-native max-propagation (COO in → propagated COO out), bit-identical to the dense `propagate(mode='max')`. - **parser** — the vectorised path builds a `scipy.sparse` CSR straight from the COO it already computes; legacy fallback densifies then re-sparsifies. No dense N×T on the common path. - **evaluation** — sparse NK/PK kernels pull non-zeros from the CSR via `find()`; dense fallback kernels (`CAFAEVAL_SPARSE=0`) densify only their slice. Ground truth stays dense, so the TP gather is unchanged. ## Correctness (bit-exact) Identical output TSVs across **8 scenarios** (sparse / dense / no_orphans / IA-weighted / NK / PK / large-corpus) and on a **24k-term × 30k-protein** synthetic corpus. Repo tests green (9 passed, 6 xfailed). ## Measured (24k-term × 30k-protein corpus) - Peak RSS **11.9 GB → 7.0 GB (−41%)**; wall-clock unchanged (~53s). ## Remaining Ground-truth / exclude matrices are still dense `bool` N×T (smaller). Sparsifying them is the next increment (needs a CSR-aware TP gather), for a further ~1.4 GB.
frapercan
added a commit
that referenced
this pull request
Jun 24, 2026
Promotes develop's sparse evaluation work to main so the branch PROTEA installs (`@main`) actually uses it. main had drifted: it only carried today's docs/CI commits (#17, #18) while the sparse perf landed on develop and was never promoted. ## Brings to main - **#16** `perf(eval)`: predictions stored as `scipy.sparse` CSR, never materialising the dense N x T matrix (11.9 -> 7.0 GB). - **#15** `perf(graph)`: sparse CSR GO DAG adjacency + O(1) `deque` topological sort (about 3x faster build, ~3x less RAM). ## Verified (py3.12, the only interpreter the stack uses) - ruff + mypy clean. - Parity suite `CAFAEVAL_PARITY_PHASE=B pytest tests/diff`: 7 passed, 6 xfailed, **0 hard failures** (the sparse path stays bit-/ULP-exact vs the dense fallback and the upstream oracle). ## Conflict resolution Only README.md conflicted (both branches edited the optimisation table): took develop's rows (they document the C1/C3 sparse kernels) and re-applied the em-dash purge from #17. pyproject auto-merged (`requires-python >=3.12` from #18 + the scipy/pyarrow sparse-era deps). Follow-up: bump PROTEA's `poetry.lock` cafaeval pin so the platform consumes this.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Two bit-exact performance optimizations to the ontology layer, following a dense→sparse principle.
1. Sparse GO DAG (
graph.py)Replace the dense boolean adjacency
np.zeros((T, T), bool)with CSR parent/child index arrays + degree vectors, built from the per-termadj/childrensets already maintained. Reproduces the olddag.sum(axis=1/0)andnp.nonzero/flatnonzeroslices exactly.2. O(1) deque in
top_sortKahn's queue used
list.pop(0)(O(n) per pop → O(n²));collections.deque.popleft()is O(1). FIFO order identical →self.orderunchanged.Correctness (values preserved — bit-exact)
Verified identical output TSVs vs the base across 7 scenarios (sparse / dense / no_orphans / IA-weighted / NK-plain / large-corpus NK & PK). Repo test suite green (9 passed, 6 xfailed, incl.
test_nk_sparse_matches_dense).Measured
Follow-up (not in this PR)
A GO-scale eval profiled at ~11.9 GB peak RSS, dominated by the dense N×T prediction (
float) matrix. A contained CSR-native sparse-path design (no dense N×T materialised; dense kernels untouched) is scoped and a synthetic GO-scale corpus + bit-exact harness are ready for a dedicated follow-up.