Skip to content

perf(eval): store predictions as CSR — never materialise dense N×T (11.9→7.0 GB) - #16

Merged
frapercan merged 6 commits into
developfrom
perf/sparse-pred-matrix
Jun 20, 2026
Merged

frapercan merged 6 commits into
developfrom
perf/sparse-pred-matrix

Conversation

@frapercan

Copy link
Copy Markdown
Owner

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.

… 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 prediction score matrix was a dense float (n_prot x n_terms) — ~20 GB at
full-GO scale, almost all zeros. Build, propagate and consume it sparse:

- graph.propagate_to_coo(): sparse-native max-propagation (COO in -> propagated
  COO out), bit-identical to the dense propagate it replaces.
- parser: the fast (vectorised) path builds a scipy CSR straight from the COO it
  already computes; the legacy fallback densifies then re-sparsifies. No dense
  (n_prot x n_terms) is allocated on the common path.
- evaluation: the sparse NK/PK kernels read the CSR's non-zeros directly
  (data/indices/indptr — not scipy.sparse.find, which re-sorts every call); the
  dense fallback kernels (CAFAEVAL_SPARSE=0) densify just their slice. Ground
  truth stays dense, so the TP gather is unchanged.

Bit-exact: identical output TSVs across sparse/dense/no_orphans/IA/NK/PK and on a
24k-term x 30k-protein synthetic corpus. Tests green.

Measured (24k-term x 30k-protein corpus): peak RSS 11.9 GB -> 7.0 GB (-41%),
wall-clock 51s -> 42s (faster: the sparse kernel skips the dense per-threshold scan).
@frapercan
frapercan force-pushed the perf/sparse-pred-matrix branch from 7dbe366 to 50a93ae Compare June 19, 2026 11:37
…ency

- architecture.rst: "Sparse DAG adjacency" and "Sparse prediction storage"
  sections (how the dense (n_terms,n_terms) and (n_prot,n_terms) matrices are
  removed, and why it stays bit-exact).
- performance.rst: "Phase C — memory" with measured build (312->108 MB, 1.87->
  0.61 s) and end-to-end (11.9->7.0 GB, 51->42 s) numbers; "what's left" notes
  the unmerged sparse-GT prototype and why (time regression).
- parity.rst: Phase C is bit-exact (atol=0) vs the pre-C fork, keeps the
  Phase B oracle tolerance.
- CHANGES.md: dated Phase C entry (GPLv3 §5.a).
- README.md: scope table rows for C1/C3; memory subsection; branch strategy
  updated to feature->develop->main.
- pyproject.toml: add scipy (now a hard dependency via scipy.sparse).

Sphinx builds clean with -W. No behaviour change in this commit.
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.
Base automatically changed from perf/sparse-dag to develop June 20, 2026 01:48
@frapercan
frapercan merged commit 2844b81 into develop Jun 20, 2026
8 checks passed
@frapercan
frapercan deleted the perf/sparse-pred-matrix branch June 20, 2026 01:48
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant