Skip to content

C4 memory, zero certificates for plr, and C5 threads inside one lr/plr fit - #124

Merged
msuchard merged 3 commits into
developfrom
ccd-threads-memory
Nov 20, 2026
Merged

msuchard merged 3 commits into
developfrom
ccd-threads-memory

Conversation

@msuchard

Copy link
Copy Markdown
Member

Summary

Three commits from the Phase 3 scale plan (NEWS 49–51), based on #123. Results are bitwise unchanged throughout. Each item was implemented, then adversarially reviewed, and every confirmed finding was fixed.

1. C4: less memory per data object and per copy (NEWS 49)

  • Row-ID map. Outcomes with consecutive row IDs keep no row-ID map and no row labels. Row i has ID firstRowId + i, checked with unsigned arithmetic, and labels are made on demand. The stratified time-effect loader and append fill the map first.

    • This applies to lr data built from sorted tables, such as CCW's expanded rows.
    • Converting 3 million lr rows takes 12.2 s instead of 15.3 s.
  • Unused arrays. The engines no longer allocate:

    • the two-way-scan censoring weights, for models without two-way scans;
    • numerPid3, numerPid4 and denomPid2, which only the GPU engines read and now size themselves.

    That saves about 40 bytes per row in every cross-validation or bootstrap copy.

  • Deferred:

    • a streaming loader from Arrow/DuckDB, which needs a benchmark at 10⁶ and an optional arrow dependency;
    • float storage of X, which would make the whole engine single precision and turn off plr's Newton path.

2. Zero certificates for plr coordinate descent (NEWS 50)

lr's coordinate descent skips a visit to a Laplace column at zero when drift bounds prove its step is zero. plr now keeps the same drift totals over its implicit person-interval rows. It certifies columns of 1s, with or without a 0/1 time-basis column, which covers CCW's indicator basis and its day-0 interactions. This works in both the serial and the concurrent cycle.

  • Design:

    • one definition of a certifiable column, getCertifiableRows;
    • drift structures built lazily, so auto and newton fits pay nothing;
    • rounding bounds that stay valid across epochs and across non-normal exp values;
    • lr's exclusions plus the cloglog link.
  • Debug check: CYCLOPS_ZVERIFY recomputes the step at every certified visit and fails if it is not zero. It found 0 violations in 1,105,682 certified visits on a CCW model, and in stress data with offsets up to ±900, zero weights, 400 columns and cross-validation.

  • Timings on a fixed-variance CCW model:

    Fit Before After
    plr ccd, 1 thread 96.6 s 65.0 s
    plr ccd, 4 threads 78.9 s 62.9 s
    plr auto 2.2 s 2.2 s
    lr 13.7 s 13.7 s

3. C5: threads inside one lr or plr fit, bitwise identical (NEWS 51)

Inside one fit, terms are computed on the fit's threads and added on the calling thread in the serial order, using the serial expressions. Each kernel uses one template for both paths, and its reduction loops are kept scalar. On Apple clang, identical source expressions had vectorized the threaded sum but fused the serial one, so their last bits differed; the new tests catch that.

  • What runs on threads:
    • plr coordinate descent: a column of at least 4,096 subjects computes its gradient and update on up to 4 threads;
    • lr Newton passes: row weights, full gradient, Hessian-block transposition, trial log-likelihood and the step;
    • plr Newton passes: the per-subject column layout, full gradient, direction, log-likelihoods and refreshes.
  • Supporting changes:
    • WorkerPool runs wake and wait for only the workers they use;
    • ordered sums keep at most 2¹⁹ rows of terms at a time, so memory with threads is about 20 MB whatever the size;
    • cross-validation folds keep coordinate descent on one thread and give their spare threads to the second-order kernels.

Timings on CCW fixed-variance models, 25,000 persons, medians of 3 paired runs:

Fit 1 thread 2 threads 4 threads 8 threads
plr ccd 66.0 → 64.0 s 63.8 → 42.8 s 63.9 → 30.3 s 64.8 → 29.1 s
plr auto 4.27 → 4.27 s 2.84 → 2.45 s 2.16 → 1.55 s 1.81 → 1.16 s
lr auto 6.54 → 6.54 s 3.91 → 3.57 s 2.68 → 2.23 s 2.05 → 1.52 s
lr ccd unchanged unchanged unchanged unchanged

A 10-fold cross-validated plr fit on 4 threads went from 132 s to 130 s.

Coefficients, log-likelihoods and certified visits are identical at 1, 2, 3, 4 and 8 threads, including builds with forced small windows. Profiles, confidence intervals and bootstrap samples are identical too.

Not verified

  • Bitwise identity on GCC with -ffp-contract=fast, on arm64 or with -march=native. The new tests would catch a mismatch.
  • The per-coordinate cap of 4 threads was tuned on one machine and one model.
  • 10⁶-scale runs.

Tests

  • New quick tests, each under 0.4 s:
    • test-consecutiveRowIds.R;
    • certificate tests in test-pooledLogistic.R: on vs off, threads, large offsets, cloglog and cross-validation;
    • test-threadsOneFit.R: lr and plr, cloglog, weights and offsets, at uneven thread counts.
  • Full suite (test_dir, without NOT_CRAN): 363 tests, 1,947 expectations, 0 failed, 0 errors, 30 skipped.
  • No version bump.

🤖 Generated with Claude Code

msuchard and others added 3 commits November 20, 2026 09:18
Outcomes whose row IDs are consecutive keep neither the row-ID map nor
the row labels: a row's ID is firstRowId + i, checked with unsigned
arithmetic, and labels are made on demand. The loaders that add rows or
look rows up through the map (stratified time effects, append) fill
them first. The engines no longer allocate hYWeight/hYWeightDouble for
models without two-way scans, or numerPid3/numerPid4/denomPid2, which
only the GPU engines read (they size denomPid2 themselves). About 40
bytes per row in every data copy (cross-validation and bootstrap
clones), and 16 bytes per row plus the label strings in the data.

No kernel or ordering change: results identical; suite 353 tests / 1921
expectations, 0 failed. New quick test test-consecutiveRowIds.R. NEWS 49.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
plr's coordinate descent visited every Laplace column at zero; lr skips
a visit when drift bounds prove its step is zero. plr now keeps the
same drift totals over its implicit person-interval rows and certifies
columns of 1s, with or without a 0/1 time-basis column (CCW's indicator
basis and day0 interactions), in the serial and the concurrent cycle.
One definition of a certifiable column (getCertifiableRows), drift
structures built lazily (auto/newton fits pay nothing), rounding bounds
that stay valid across epochs and non-normal exp values, and the
exclusions of lr plus the cloglog link. CYCLOPS_ZVERIFY recomputes the
step at every certified visit and fails if it is not zero (debug).

Results bitwise identical with certificates on or off and on 1-4
threads; ZVERIFY: 0 violations over 1,105,682 certified visits on a CCW
model and on stress data (offsets to +-900, zero weights, 400 columns,
CV). CCW fixed-variance plr ccd fit: 96.6 -> 65.0 s on 1 thread, 78.9
-> 62.9 s on 4; auto (2.2 s) and lr (13.7 s) unchanged. Suite 356
tests / 1931 expectations, 0 failed. NEWS 50.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Plan item C5. Within one fit, terms are computed on the fit's threads
and added on the calling thread in the serial order with the serial
expressions (one template per kernel; the reduction loops are kept
scalar so the compiler cannot vectorize one path and fuse the other),
so results are bitwise identical on any number of threads, and on one
thread unchanged.

- plr coordinate descent: a column of at least 4,096 subjects computes
  its gradient and update on up to 4 threads (about one per 2,048
  entries); zero-certificate stores stay on the calling thread
- Newton steps: lr row weights, full gradient, Hessian-block
  transposition, trial log likelihoods and step; plr's per-subject
  column layout (part-wise column pass), full gradient, direction, log
  likelihoods and refreshes
- WorkerPool runs wake and wait for only the workers they use
- ordered sums keep at most 2^19 rows of terms (about 20 MB with
  threads, whatever the size)
- cross-validation folds keep coordinate descent on one thread and
  give spare threads to the second-order kernels

CCW fixed-variance models, 25k persons, medians of 3 paired runs,
1/2/4/8 threads: plr ccd 66.0/63.8/63.9/64.8 -> 64.0/42.8/30.3/29.1 s,
plr auto 4.27/2.84/2.16/1.81 -> 4.27/2.45/1.55/1.16 s, lr auto
6.54/3.91/2.68/2.05 -> 6.54/3.57/2.23/1.52 s, lr ccd unchanged; a
10-fold cross-validated plr fit on 4 threads 132 -> 130 s. Identical
coefficients, log likelihoods and certified visits at 1, 2, 3, 4 and 8
threads (also with forced small windows). New quick tests
test-threadsOneFit.R; suite 363 tests / 1947 expectations, 0 failed.
NEWS 51.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@msuchard
msuchard merged commit 4130cc1 into develop Nov 20, 2026
22 checks passed
@msuchard
msuchard deleted the ccd-threads-memory branch November 20, 2026 22:08
@codecov

codecov Bot commented Nov 20, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 96.78030% with 17 lines in your changes missing coverage. Please review.
✅ Project coverage is 85.91%. Comparing base (6ecb2f4) to head (ed9b141).
⚠️ Report is 8 commits behind head on develop.

Files with missing lines Patch % Lines
...rc/cyclops/engine/PooledLogisticModelSpecifics.hpp 97.66% 8 Missing ⚠️
src/cyclops/ModelData.h 75.00% 4 Missing ⚠️
src/cyclops/CyclicCoordinateDescent.cpp 93.75% 3 Missing ⚠️
src/cyclops/ModelData.cpp 94.44% 1 Missing ⚠️
src/cyclops/engine/AbstractModelSpecifics.h 50.00% 1 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop     #124      +/-   ##
===========================================
+ Coverage    85.60%   85.91%   +0.30%     
===========================================
  Files           73       73              
  Lines        15945    16338     +393     
===========================================
+ Hits         13650    14036     +386     
- Misses        2295     2302       +7     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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