Skip to content

Find the draw prefix of _fovs_until chunk by chunk (2x, same draws; refs #646, #501) - #653

Merged
guijacquemet merged 2 commits into
mainfrom
claude/fovs-until-chunked
Nov 28, 2026
Merged

guijacquemet merged 2 commits into
mainfrom
claude/fovs-until-chunked

Conversation

@guijacquemet

Copy link
Copy Markdown
Contributor

Refs #646 (partly: this does not fix it) and #501.

What

object_rules._fovs_until drew a batch of about twice the fields a draw needs, then gathered, cumulated, searched and summed the whole batch, and gathered the chosen prefix twice more. It now finds the prefix that reaches the rule's n objects chunk by chunk (16,384 integer prefix sums at a time, _prefix_reaching) and never touches the unused tail.

Kept exactly: the random numbers (same rng.integers call, same batch size, unused tail still discarded), the one sums[chosen].sum() call per batch (same float rounding), and the generator's state afterwards. Every output is equal bit for bit.

Why this does not close #646

The 15-minute hang is not a loop that re-scans or re-sorts. On the v27 export the x control mean of repeat rep1 is 0.25; a DrugA field raised by 100, read relative to that mean, makes the rule ask for 11 and 45 million cells (targets 5% and 2.5%). Each of the 4,000 draws then picks millions of fields of 2 cells: the work is linear in n. Generating those random numbers alone is about 30% of the time and must stay for identical results, so an identical swap is capped at about 3×. Fixing #646 needs a behaviour change; one is prepared on claude/rule-draw-limit (not opened as a PR), awaiting the owner's decision and limit.

Equivalence

tests/test_object_rules.py::test_fovs_until_draws_exactly_as_before_646: the former function is kept in the test as the reference; 24 seeded random field sets (empty fields, one outlier field, n from 1 to 5 × 16,384 × 3, mostly-empty sets whose draws need a second batch). np.testing.assert_array_equal on means, objects and fields of every draw, and equal bit_generator.state afterwards. A spy run confirmed the cases include prefixes that cross a chunk boundary (6) and batches that fall short of n (35).

Timing (small scale only)

5 fields of 2 objects, one outlier sum, single thread, best of 3, NumPy 2.5.5, on a shared machine with another heavy job running:

n (objects asked) before (ms per draw) after (ms per draw) ratio 4,000 draws
100,000 1.40 0.68 2.06× ~6 s → ~3 s
1,000,000 12.37 5.85 2.12× ~49 s → ~23 s
25,000,000 (2 draws) 295 176 1.68× ~20 min → ~12 min (projected)

These time the helper alone, not object_rule_check or report. The definitive timing waits for a quiet window on the machine.

Local gate (on 4c1c83c)

pytest -n 0 tests/test_guards.py tests/test_function_guards.py tests/test_function_tools.py
102 passed, 1 warning in 71.66s
python tools/audit.py --check-against origin/main
baselines only tightened against origin/main

Also run: tests/test_reference.py (20 passed), ruff check and ruff format --check on src tests data (clean); tests/test_object_rules.py, test_sampling_unit.py, test_morphofeature_exports.py, test_one_per_field.py, test_repeat_groups.py, test_biological_repeat.py, test_acquisition_identity.py (271 passed, 4 workers).

Reference check (behaviour unchanged): 0 differences

data/reference/reference.py run on origin/main and on this change (--parallel 2 --skip idr: the 50 scenarios, 3 lab datasets, synthetic and fiberfeatures, 55 jobs, all exit 0), then compare: 0 differences. The IDR job was skipped because it uses every CPU.

The first compare reported 382 differences (335 PDFs and 47 pages). Every one was a PDF /CreationDate written with this machine's offset (D:20261128205345+03'00'); the comparer removed only UTC dates (D:...Z). The second commit fixes reference_compare.PDF_STAMP to accept an offset or no zone, with a test (Z, +03'00', -05'30', none; in a PDF and inside a page). The same two trees then compare with 0 differences.

Not run: the whole suite (pytest on every CPU). It was held back because of the machine's resource limit while another session runs a large end-to-end gate; it needs running before merge.

🤖 Generated with Claude Code

guijacquemet and others added 2 commits November 28, 2026 20:57
_fovs_until drew one batch of about twice the fields a draw needs, then gathered,
summed and searched the whole batch. It now finds the prefix that reaches n in chunks
of 16384 integer prefix sums and never touches the unused tail, keeping the same
random numbers, the same batch, the same one-call float sum and the generator's
state afterwards, so every output is equal bit for bit. About 2x faster at small scale.

This does not fix issue 646: there the rule asks for about 25 million objects, the
work is linear in that number, and the random draws that must be kept for identical
results set the floor. Refs 646, 501.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
reference_compare removed /CreationDate (D:...Z) only. On a machine off UTC,
matplotlib writes D:20261128205345+03'00', so every PDF and every page carrying one
was reported as changed (382 false differences on the 646 change; 0 once fixed).
Tested with Z, two offsets and no zone, in a PDF and inside a page.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@guijacquemet
guijacquemet merged commit a4d1e11 into main Nov 28, 2026
2 checks passed
@guijacquemet
guijacquemet deleted the claude/fovs-until-chunked branch November 28, 2026 19:24
@guijacquemet

Copy link
Copy Markdown
Contributor Author

Whole suite run locally after merge, on main at 1b9bc00, which includes #653 and #654: pytest -n 4 (4 workers, one BLAS thread each, nice), 13 min 45 s.

1 failed, 4816 passed, 2 skipped, 6 xfailed, 42 warnings in 825.09s
FAILED tests/test_duplicates_tool.py::test_a_pass_that_cannot_run_is_skipped_with_its_reason

The one failure has nothing to do with these changes. It is about the machine: npx (Node.js) is not installed, so jscpd_review reports skipped: npx not found, while the test expects the message for a model that has not been downloaded. It fails the same way on main before both PRs (9fa2402). Neither PR touches tools/ or the duplicates test.

GitHub Actions also ran on both PRs: check and spec-pin pass on #653 and on #654.

🤖 Generated with Claude Code

@guijacquemet

Copy link
Copy Markdown
Contributor Author

Definitive timing (quiet machine, 2026-11-29)

One process pinned to one core, OMP_NUM_THREADS=1, NumPy 2.5.5, run one after another. Load average was under 1 at the start; it reached 4 near the end of the run (on a 20-core machine).

object_rule_check on the 646 case (v27, a DrugA rep1 field raised by 100, control CTRL; 16 rows):

code commit time largest rule_objects
before 9fa2402 1976 s (32.9 min) 45,397,547
chunked draws, identical outputs (#653) a4d1e11 1137 s (18.9 min), 1.74× 45,397,547
draw limit at 100× (#654), current main 5c5f582 0.39 s 214

Draw helper alone, best of 5 (5 fields of 2 objects, one outlier sum):

n before (ms/draw) after (ms/draw) speed-up
100,000 0.81 0.39 2.10×
1,000,000 7.77 3.53 2.20×
25,000,000 203.6 111.7 1.82×

The identical-output change alone takes the 646 case from 33 to 19 minutes. The draw limit is what brings it down to under half a second.

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.

report runs >15 min in object_rules._fovs_until when one field is an outlier

1 participant